fix(autoseal): use higher difficulty and fix root calc (#3044)

This commit is contained in:
Dan Cline
2023-06-07 06:31:38 -04:00
committed by GitHub
parent 8ffcb3124d
commit 1a965b7969
3 changed files with 34 additions and 16 deletions

View File

@ -140,7 +140,7 @@ where
receipts_root: Default::default(),
withdrawals_root: None,
logs_bloom: Default::default(),
difficulty: U256::from(1),
difficulty: U256::from(2),
number: storage.best_block + 1,
gas_limit: 30_000_000,
gas_used: 0,
@ -182,6 +182,11 @@ where
match executor.execute_transactions(&block, U256::ZERO, Some(senders.clone())) {
Ok((post_state, gas_used)) => {
// apply post block changes
let post_state = executor
.apply_post_block_changes(&block, U256::ZERO, post_state)
.unwrap();
let Block { mut header, body, .. } = block;
// clear all transactions from pool
@ -202,11 +207,15 @@ where
BlockBody { transactions: body, ommers: vec![], withdrawals: None };
header.gas_used = gas_used;
trace!(target: "consensus::auto", ?post_state, ?header, ?body, "executed block, calculating root");
// calculate the state root
let state_root =
executor.db().db.0.state_root(post_state.clone()).unwrap();
header.state_root = state_root;
trace!(target: "consensus::auto", root=?header.state_root, ?body, "calculated root");
storage.insert_new_block(header.clone(), body);
let new_hash = storage.best_hash;

View File

@ -271,6 +271,27 @@ where
Ok((post_state, cumulative_gas_used))
}
/// Applies the post-block changes, assuming the poststate is generated after executing
/// tranactions
pub fn apply_post_block_changes(
&mut self,
block: &Block,
total_difficulty: U256,
mut post_state: PostState,
) -> Result<PostState, BlockExecutionError> {
// Add block rewards
let balance_increments = self.post_block_balance_increments(block, total_difficulty);
for (address, increment) in balance_increments.into_iter() {
self.increment_account_balance(block.number, address, increment, &mut post_state)?;
}
// Perform DAO irregular state change
if self.chain_spec.fork(Hardfork::Dao).transitions_at_block(block.number) {
self.apply_dao_fork_changes(block.number, &mut post_state)?;
}
Ok(post_state)
}
}
impl<DB> BlockExecutor<DB> for Executor<DB>
@ -283,7 +304,7 @@ where
total_difficulty: U256,
senders: Option<Vec<Address>>,
) -> Result<PostState, BlockExecutionError> {
let (mut post_state, cumulative_gas_used) =
let (post_state, cumulative_gas_used) =
self.execute_transactions(block, total_difficulty, senders)?;
// Check if gas used matches the value set in header.
@ -295,17 +316,7 @@ where
.into())
}
// Add block rewards
let balance_increments = self.post_block_balance_increments(block, total_difficulty);
for (address, increment) in balance_increments.into_iter() {
self.increment_account_balance(block.number, address, increment, &mut post_state)?;
}
// Perform DAO irregular state change
if self.chain_spec.fork(Hardfork::Dao).transitions_at_block(block.number) {
self.apply_dao_fork_changes(block.number, &mut post_state)?;
}
Ok(post_state)
self.apply_post_block_changes(block, total_difficulty, post_state)
}
fn execute_and_verify_receipt(