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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use crate::{error::ConsensusError, Consensus};
use snarkos_storage::{SerialBlock, SerialBlockHeader, SerialRecord, SerialTransaction};
use snarkvm_algorithms::SNARKError;
use snarkvm_dpc::{testnet1::instantiated::*, Address, BlockHeader, BlockHeaderHash, DPCComponents, ProgramScheme};
use snarkvm_posw::{error::PoswError, txids_to_roots, PoswMarlin};
use snarkvm_utilities::{to_bytes_le, ToBytes};
use chrono::Utc;
use rand::thread_rng;
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
use tokio::task;
lazy_static::lazy_static! {
static ref POSW: PoswMarlin = {
PoswMarlin::load().expect("could not instantiate the miner")
};
}
#[derive(Clone)]
pub struct MineContext {
address: Address<Components>,
pub consensus: Arc<Consensus>,
}
impl MineContext {
pub async fn prepare(address: Address<Components>, consensus: Arc<Consensus>) -> Result<Self, ConsensusError> {
Ok(Self { address, consensus })
}
async fn add_coinbase_transaction(
&self,
block_number: u32,
transactions: &mut Vec<SerialTransaction>,
) -> Result<Vec<SerialRecord>, ConsensusError> {
let program_vk_hash = self.consensus.dpc.noop_program.id();
let new_birth_programs = vec![program_vk_hash.clone(); Components::NUM_OUTPUT_RECORDS];
let new_death_programs = vec![program_vk_hash.clone(); Components::NUM_OUTPUT_RECORDS];
for transaction in transactions.iter() {
if self.consensus.parameters.network_id != transaction.network {
return Err(ConsensusError::ConflictingNetworkId(
self.consensus.parameters.network_id.id(),
transaction.network.id(),
));
}
}
let response = self
.consensus
.create_coinbase_transaction(
block_number,
transactions.clone(),
program_vk_hash,
new_birth_programs,
new_death_programs,
vec![self.address.clone(); Components::NUM_OUTPUT_RECORDS]
.into_iter()
.map(|x| x.into())
.collect(),
)
.await?;
transactions.push(response.transaction);
Ok(response.records)
}
#[allow(clippy::type_complexity)]
pub async fn establish_block(
&self,
block_number: u32,
mut transactions: Vec<SerialTransaction>,
) -> Result<(Vec<SerialTransaction>, Vec<SerialRecord>), ConsensusError> {
let coinbase_records = self.add_coinbase_transaction(block_number, &mut transactions).await?;
Ok((transactions, coinbase_records))
}
pub fn find_block(
&self,
transactions: &[SerialTransaction],
parent_header: &SerialBlockHeader,
terminator: &AtomicBool,
) -> Result<SerialBlockHeader, ConsensusError> {
let txids = transactions.iter().map(|x| x.id).collect::<Vec<_>>();
let (merkle_root_hash, pedersen_merkle_root_hash, subroots) = txids_to_roots(&txids);
let time = Utc::now().timestamp();
let difficulty_target = self.consensus.parameters.get_block_difficulty(parent_header, time);
let mined = POSW.mine(
&subroots,
difficulty_target,
terminator,
&mut thread_rng(),
self.consensus.parameters.max_nonce,
);
let (nonce, proof) = match mined {
Err(PoswError::SnarkError(SNARKError::Terminated)) => {
debug!("terminated miner due to canon block received");
terminator.store(false, Ordering::SeqCst);
return Err(ConsensusError::PoswError(PoswError::SnarkError(SNARKError::Terminated)));
}
Err(PoswError::SnarkError(SNARKError::Crate("marlin", message)))
if message == "Failed to generate proof - Terminated" =>
{
debug!("terminated miner due to canon block received");
terminator.store(false, Ordering::SeqCst);
return Err(ConsensusError::PoswError(PoswError::SnarkError(SNARKError::Terminated)));
}
Err(e) => return Err(e.into()),
Ok(x) => x,
};
Ok(BlockHeader {
previous_block_hash: BlockHeaderHash(parent_header.hash().bytes().unwrap()),
merkle_root_hash,
pedersen_merkle_root_hash,
time,
difficulty_target,
nonce,
proof: proof.into(),
}
.into())
}
pub async fn mine_block(
&self,
terminator: Arc<AtomicBool>,
) -> Result<(SerialBlock, Vec<SerialRecord>), ConsensusError> {
let candidate_transactions = self.consensus.fetch_memory_pool().await;
let canon = self.consensus.storage.canon().await?;
let canon_header = self.consensus.storage.get_block_header(&canon.hash).await?;
debug!("Miner@{}: creating a block", canon.block_height);
let (transactions, coinbase_records) = self
.establish_block(canon.block_height as u32 + 1, candidate_transactions)
.await?;
debug!("Miner@{}: generated a coinbase transaction", canon.block_height);
for (index, record) in coinbase_records.iter().enumerate() {
let record_commitment = hex::encode(&to_bytes_le![record.commitment]?);
debug!(
"Miner@{}: Coinbase record {:?} commitment: {:?}",
canon.block_height, index, record_commitment
);
}
let ctx = self.clone();
let txs = transactions.clone();
let header = task::spawn_blocking(move || ctx.find_block(&txs, &canon_header, &terminator)).await??;
debug!("Miner@{}: found a block", canon.block_height);
let block = SerialBlock { header, transactions };
self.consensus.receive_block(block.clone()).await;
let mut records_to_store = vec![];
for record in &coinbase_records {
if !record.is_dummy {
records_to_store.push(record.clone());
}
}
self.consensus.storage.store_records(&records_to_store).await?;
Ok((block, coinbase_records))
}
}