This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
forked from paradigmxyz/reth
-
Notifications
You must be signed in to change notification settings - Fork 3
/
lib.rs
380 lines (329 loc) · 12.8 KB
/
lib.rs
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#![cfg_attr(docsrs, feature(doc_cfg))]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/paradigmxyz/reth/main/assets/reth-docs.png",
html_favicon_url = "https://avatars0.githubusercontent.com/u/97369466?s=256",
issue_tracker_base_url = "https://github.com/paradigmxzy/reth/issues/"
)]
#![warn(missing_docs, unreachable_pub, unused_crate_dependencies)]
#![deny(unused_must_use, rust_2018_idioms)]
#![doc(test(
no_crate_inject,
attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_variables))
))]
//! A [Consensus] implementation for local testing purposes
//! that automatically seals blocks.
//!
//! The Mining task polls a [MiningMode], and will return a list of transactions that are ready to
//! be mined.
//!
//! These downloaders poll the miner, assemble the block, and return transactions that are ready to
//! be mined.
use reth_beacon_consensus::BeaconEngineMessage;
use reth_interfaces::{
consensus::{Consensus, ConsensusError},
executor::{BlockExecutionError, BlockValidationError},
};
use reth_primitives::{
constants::{EMPTY_RECEIPTS, EMPTY_TRANSACTIONS, ETHEREUM_BLOCK_GAS_LIMIT},
proofs, Address, Block, BlockBody, BlockHash, BlockHashOrNumber, BlockNumber, Bloom, ChainSpec,
Header, ReceiptWithBloom, SealedBlock, SealedHeader, TransactionSigned, EMPTY_OMMER_ROOT, H256,
U256,
};
use reth_provider::{
BlockExecutor, BlockReaderIdExt, CanonStateNotificationSender, PostState, StateProvider,
};
use reth_revm::executor::Executor;
use reth_transaction_pool::TransactionPool;
use std::{
collections::HashMap,
sync::Arc,
time::{SystemTime, UNIX_EPOCH},
};
use tokio::sync::{mpsc::UnboundedSender, RwLock, RwLockReadGuard, RwLockWriteGuard};
use tracing::{trace, warn};
mod client;
mod mode;
mod task;
pub use crate::client::AutoSealClient;
pub use mode::{FixedBlockTimeMiner, MiningMode, ReadyTransactionMiner};
pub use task::MiningTask;
/// A consensus implementation intended for local development and testing purposes.
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct AutoSealConsensus {
/// Configuration
chain_spec: Arc<ChainSpec>,
}
impl AutoSealConsensus {
/// Create a new instance of [AutoSealConsensus]
pub fn new(chain_spec: Arc<ChainSpec>) -> Self {
Self { chain_spec }
}
}
impl Consensus for AutoSealConsensus {
fn validate_header(&self, _header: &SealedHeader) -> Result<(), ConsensusError> {
Ok(())
}
fn validate_header_against_parent(
&self,
_header: &SealedHeader,
_parent: &SealedHeader,
) -> Result<(), ConsensusError> {
Ok(())
}
fn validate_header_with_total_difficulty(
&self,
_header: &Header,
_total_difficulty: U256,
) -> Result<(), ConsensusError> {
Ok(())
}
fn validate_block(&self, _block: &SealedBlock) -> Result<(), ConsensusError> {
Ok(())
}
}
/// Builder type for configuring the setup
pub struct AutoSealBuilder<Client, Pool> {
client: Client,
consensus: AutoSealConsensus,
pool: Pool,
mode: MiningMode,
storage: Storage,
to_engine: UnboundedSender<BeaconEngineMessage>,
canon_state_notification: CanonStateNotificationSender,
}
// === impl AutoSealBuilder ===
impl<Client, Pool: TransactionPool> AutoSealBuilder<Client, Pool>
where
Client: BlockReaderIdExt,
{
/// Creates a new builder instance to configure all parts.
pub fn new(
chain_spec: Arc<ChainSpec>,
client: Client,
pool: Pool,
to_engine: UnboundedSender<BeaconEngineMessage>,
canon_state_notification: CanonStateNotificationSender,
mode: MiningMode,
) -> Self {
let latest_header = client
.latest_header()
.ok()
.flatten()
.unwrap_or_else(|| chain_spec.sealed_genesis_header());
Self {
storage: Storage::new(latest_header),
client,
consensus: AutoSealConsensus::new(chain_spec),
pool,
mode,
to_engine,
canon_state_notification,
}
}
/// Sets the [MiningMode] it operates in, default is [MiningMode::Auto]
pub fn mode(mut self, mode: MiningMode) -> Self {
self.mode = mode;
self
}
/// Consumes the type and returns all components
#[track_caller]
pub fn build(self) -> (AutoSealConsensus, AutoSealClient, MiningTask<Client, Pool>) {
let Self { client, consensus, pool, mode, storage, to_engine, canon_state_notification } =
self;
let auto_client = AutoSealClient::new(storage.clone());
let task = MiningTask::new(
Arc::clone(&consensus.chain_spec),
mode,
to_engine,
canon_state_notification,
storage,
client,
pool,
);
(consensus, auto_client, task)
}
}
/// In memory storage
#[derive(Debug, Clone, Default)]
pub(crate) struct Storage {
inner: Arc<RwLock<StorageInner>>,
}
// == impl Storage ===
impl Storage {
fn new(header: SealedHeader) -> Self {
let (header, best_hash) = header.split();
let mut storage = StorageInner {
best_hash,
total_difficulty: header.difficulty,
best_block: header.number,
..Default::default()
};
storage.headers.insert(0, header);
storage.bodies.insert(best_hash, BlockBody::default());
Self { inner: Arc::new(RwLock::new(storage)) }
}
/// Returns the write lock of the storage
pub(crate) async fn write(&self) -> RwLockWriteGuard<'_, StorageInner> {
self.inner.write().await
}
/// Returns the read lock of the storage
pub(crate) async fn read(&self) -> RwLockReadGuard<'_, StorageInner> {
self.inner.read().await
}
}
/// In-memory storage for the chain the auto seal engine is building.
#[derive(Default, Debug)]
pub(crate) struct StorageInner {
/// Headers buffered for download.
pub(crate) headers: HashMap<BlockNumber, Header>,
/// A mapping between block hash and number.
pub(crate) hash_to_number: HashMap<BlockHash, BlockNumber>,
/// Bodies buffered for download.
pub(crate) bodies: HashMap<BlockHash, BlockBody>,
/// Tracks best block
pub(crate) best_block: u64,
/// Tracks hash of best block
pub(crate) best_hash: H256,
/// The total difficulty of the chain until this block
pub(crate) total_difficulty: U256,
}
// === impl StorageInner ===
impl StorageInner {
/// Returns the block hash for the given block number if it exists.
pub(crate) fn block_hash(&self, num: u64) -> Option<BlockHash> {
self.hash_to_number.iter().find_map(|(k, v)| num.eq(v).then_some(*k))
}
/// Returns the matching header if it exists.
pub(crate) fn header_by_hash_or_number(
&self,
hash_or_num: BlockHashOrNumber,
) -> Option<Header> {
let num = match hash_or_num {
BlockHashOrNumber::Hash(hash) => self.hash_to_number.get(&hash).copied()?,
BlockHashOrNumber::Number(num) => num,
};
self.headers.get(&num).cloned()
}
/// Inserts a new header+body pair
pub(crate) fn insert_new_block(&mut self, mut header: Header, body: BlockBody) {
header.number = self.best_block + 1;
header.parent_hash = self.best_hash;
self.best_hash = header.hash_slow();
self.best_block = header.number;
self.total_difficulty += header.difficulty;
trace!(target: "consensus::auto", num=self.best_block, hash=?self.best_hash, "inserting new block");
self.headers.insert(header.number, header);
self.bodies.insert(self.best_hash, body);
self.hash_to_number.insert(self.best_hash, self.best_block);
}
/// Fills in pre-execution header fields based on the current best block and given
/// transactions.
pub(crate) fn build_header_template(
&self,
transactions: &Vec<TransactionSigned>,
chain_spec: Arc<ChainSpec>,
) -> Header {
// check previous block for base fee
let base_fee_per_gas = self
.headers
.get(&self.best_block)
.and_then(|parent| parent.next_block_base_fee(chain_spec.base_fee_params));
let mut header = Header {
parent_hash: self.best_hash,
ommers_hash: EMPTY_OMMER_ROOT,
beneficiary: Default::default(),
state_root: Default::default(),
transactions_root: Default::default(),
receipts_root: Default::default(),
withdrawals_root: None,
logs_bloom: Default::default(),
difficulty: U256::from(2),
number: self.best_block + 1,
gas_limit: ETHEREUM_BLOCK_GAS_LIMIT,
gas_used: 0,
timestamp: SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_secs(),
mix_hash: Default::default(),
nonce: 0,
base_fee_per_gas,
blob_gas_used: None,
excess_blob_gas: None,
extra_data: Default::default(),
};
header.transactions_root = if transactions.is_empty() {
EMPTY_TRANSACTIONS
} else {
proofs::calculate_transaction_root(transactions)
};
header
}
/// Executes the block with the given block and senders, on the provided [Executor].
///
/// This returns the poststate from execution and post-block changes, as well as the gas used.
pub(crate) fn execute<DB: StateProvider>(
&mut self,
block: &Block,
executor: &mut Executor<DB>,
senders: Vec<Address>,
) -> Result<(PostState, u64), BlockExecutionError> {
trace!(target: "consensus::auto", transactions=?&block.body, "executing transactions");
let (post_state, gas_used) =
executor.execute_transactions(block, U256::ZERO, Some(senders))?;
// apply post block changes
let post_state = executor.apply_post_block_changes(block, U256::ZERO, post_state)?;
Ok((post_state, gas_used))
}
/// Fills in the post-execution header fields based on the given PostState and gas used.
/// In doing this, the state root is calculated and the final header is returned.
pub(crate) fn complete_header<DB: StateProvider>(
&self,
mut header: Header,
post_state: &PostState,
executor: &mut Executor<DB>,
gas_used: u64,
) -> Header {
let receipts = post_state.receipts(header.number);
header.receipts_root = if receipts.is_empty() {
EMPTY_RECEIPTS
} else {
let receipts_with_bloom =
receipts.iter().map(|r| r.clone().into()).collect::<Vec<ReceiptWithBloom>>();
header.logs_bloom =
receipts_with_bloom.iter().fold(Bloom::zero(), |bloom, r| bloom | r.bloom);
proofs::calculate_receipt_root(&receipts_with_bloom)
};
header.gas_used = gas_used;
// calculate the state root
let state_root = executor.db().db.0.state_root(post_state.clone()).unwrap();
header.state_root = state_root;
header
}
/// Builds and executes a new block with the given transactions, on the provided [Executor].
///
/// This returns the header of the executed block, as well as the poststate from execution.
pub(crate) fn build_and_execute<DB: StateProvider>(
&mut self,
transactions: Vec<TransactionSigned>,
executor: &mut Executor<DB>,
chain_spec: Arc<ChainSpec>,
) -> Result<(SealedHeader, PostState), BlockExecutionError> {
let header = self.build_header_template(&transactions, chain_spec);
let block = Block { header, body: transactions, ommers: vec![], withdrawals: None };
let senders = TransactionSigned::recover_signers(&block.body, block.body.len())
.ok_or(BlockExecutionError::Validation(BlockValidationError::SenderRecoveryError))?;
trace!(target: "consensus::auto", transactions=?&block.body, "executing transactions");
// now execute the block
let (post_state, gas_used) = self.execute(&block, executor, senders)?;
let Block { header, body, .. } = block;
let body = BlockBody { transactions: body, ommers: vec![], withdrawals: None };
trace!(target: "consensus::auto", ?post_state, ?header, ?body, "executed block, calculating state root and completing header");
// fill in the rest of the fields
let header = self.complete_header(header, &post_state, executor, gas_used);
trace!(target: "consensus::auto", root=?header.state_root, ?body, "calculated root");
// finally insert into storage
self.insert_new_block(header.clone(), body);
// set new header with hash that should have been updated by insert_new_block
let new_header = header.seal(self.best_hash);
Ok((new_header, post_state))
}
}