1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#![deny(non_upper_case_globals)]
#![deny(non_camel_case_types)]
#![deny(non_snake_case)]
#![deny(unused_mut)]
#![warn(missing_docs)]
pub use self::common::EdgeType;
pub use self::types::*;
use crate::core::{Block, BlockHeader};
use crate::genesis;
use crate::global;
#[macro_use]
mod common;
pub mod cuckaroo;
pub mod cuckarood;
pub mod cuckaroom;
pub mod cuckarooz;
pub mod cuckatoo;
mod error;
#[allow(dead_code)]
pub mod lean;
mod siphash;
mod types;
pub use crate::pow::cuckaroo::{new_cuckaroo_ctx, no_cuckaroo_ctx, CuckarooContext};
pub use crate::pow::cuckarood::{new_cuckarood_ctx, CuckaroodContext};
pub use crate::pow::cuckaroom::{new_cuckaroom_ctx, CuckaroomContext};
pub use crate::pow::cuckarooz::{new_cuckarooz_ctx, CuckaroozContext};
pub use crate::pow::cuckatoo::{new_cuckatoo_ctx, CuckatooContext};
pub use crate::pow::error::Error;
use chrono::prelude::{DateTime, NaiveDateTime, Utc};
const MAX_SOLS: u32 = 10;
pub fn verify_size(bh: &BlockHeader) -> Result<(), Error> {
let mut ctx = global::create_pow_context::<u64>(
bh.height,
bh.pow.edge_bits(),
bh.pow.proof.nonces.len(),
MAX_SOLS,
)?;
ctx.set_header_nonce(bh.pre_pow(), None, false)?;
ctx.verify(&bh.pow.proof)
}
pub fn mine_genesis_block() -> Result<Block, Error> {
let mut gen = genesis::genesis_dev();
let genesis_difficulty = gen.header.pow.total_difficulty;
let sz = global::min_edge_bits();
let proof_size = global::proofsize();
pow_size(&mut gen.header, genesis_difficulty, proof_size, sz)?;
Ok(gen)
}
pub fn pow_size(
bh: &mut BlockHeader,
diff: Difficulty,
proof_size: usize,
sz: u8,
) -> Result<(), Error> {
let start_nonce = bh.pow.nonce;
loop {
let mut ctx = global::create_pow_context::<u32>(bh.height, sz, proof_size, MAX_SOLS)?;
ctx.set_header_nonce(bh.pre_pow(), None, true)?;
if let Ok(proofs) = ctx.find_cycles() {
bh.pow.proof = proofs[0].clone();
if bh.pow.to_difficulty(bh.height) >= diff {
return Ok(());
}
}
let (res, _) = bh.pow.nonce.overflowing_add(1);
bh.pow.nonce = res;
if bh.pow.nonce == start_nonce {
bh.timestamp = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(0, 0), Utc);
}
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::genesis;
use crate::global;
use crate::global::ChainTypes;
#[test]
fn genesis_pow() {
global::set_local_chain_type(ChainTypes::UserTesting);
let mut b = genesis::genesis_dev();
b.header.pow.nonce = 28106;
b.header.pow.proof.edge_bits = global::min_edge_bits();
println!("proof {}", global::proofsize());
pow_size(
&mut b.header,
Difficulty::min_dma(),
global::proofsize(),
global::min_edge_bits(),
)
.unwrap();
println!("nonce {}", b.header.pow.nonce);
assert_ne!(b.header.pow.nonce, 310);
assert!(b.header.pow.to_difficulty(0) >= Difficulty::min_dma());
assert!(verify_size(&b.header).is_ok());
}
}