Skip to content

Commit

Permalink
Add beneficiary, logs_bloom and gas_used to block (#1945)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jouzo authored Apr 28, 2023
1 parent 1f569b0 commit 28a410b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
2 changes: 2 additions & 0 deletions lib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions lib/ain-evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ bincode = "1.3.3"
rand = "0.8.5"
keccak-hash = "0.10.0"
serde = { version = "1.0", features = ["derive"] }
ethbloom = "0.13.0"
ethereum-types = "0.14.1"

# Runtime dependencies
lazy_static = "1.4"
Expand Down
36 changes: 28 additions & 8 deletions lib/ain-evm/src/handler.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::block::BlockHandler;
use crate::evm::{get_vicinity, EVMHandler};
use crate::executor::AinExecutor;
use crate::executor::{AinExecutor, TxResponse};
use crate::receipt::ReceiptHandler;
use crate::storage::Storage;
use crate::traits::Executor;

use ethereum::{Block, BlockAny, PartialHeader, TransactionV2};
use ethereum::{Block, BlockAny, Log, PartialHeader, TransactionV2};
use ethereum_types::{Bloom, BloomInput};
use evm::backend::MemoryBackend;
use primitive_types::{H160, H256, U256};
use std::error::Error;
Expand Down Expand Up @@ -39,22 +40,32 @@ impl Handlers {
context: u64,
update_state: bool,
difficulty: u32,
_miner_address: Option<H160>,
miner_address: Option<H160>,
) -> Result<(BlockAny, Vec<TransactionV2>), Box<dyn Error>> {
let mut successful_transactions = Vec::with_capacity(self.evm.tx_queues.len(context));
let mut failed_transactions = Vec::with_capacity(self.evm.tx_queues.len(context));
let mut gas_used = 0u64;
let mut logs_bloom: Bloom = Default::default();

let vicinity = get_vicinity(None, None);
let state = self.evm.tx_queues.state(context).expect("Wrong context");
let backend = MemoryBackend::new(&vicinity, state);
let mut executor = AinExecutor::new(backend);

for signed_tx in self.evm.tx_queues.drain_all(context) {
let tx_response = executor.exec(&signed_tx);
if tx_response.exit_reason.is_succeed() {
let TxResponse {
exit_reason,
logs,
used_gas,
..
} = executor.exec(&signed_tx);
if exit_reason.is_succeed() {
successful_transactions.push(signed_tx);
} else {
failed_transactions.push(signed_tx)
}
gas_used += used_gas;
Self::logs_bloom(logs, &mut logs_bloom);
}

let mut all_transactions = successful_transactions
Expand Down Expand Up @@ -82,14 +93,14 @@ impl Handlers {
let mut block = Block::new(
PartialHeader {
parent_hash,
beneficiary: Default::default(),
beneficiary: miner_address.unwrap_or_default(),
state_root: Default::default(),
receipts_root: Default::default(),
logs_bloom: Default::default(),
logs_bloom,
difficulty: U256::from(difficulty),
number,
gas_limit: U256::from(30000000),
gas_used: Default::default(),
gas_used: U256::from(gas_used),
timestamp: SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
Expand Down Expand Up @@ -128,4 +139,13 @@ impl Handlers {
.collect(),
))
}

fn logs_bloom(logs: Vec<Log>, bloom: &mut Bloom) {
for log in logs {
bloom.accrue(BloomInput::Raw(&log.address[..]));
for topic in log.topics {
bloom.accrue(BloomInput::Raw(&topic[..]));
}
}
}
}

0 comments on commit 28a410b

Please sign in to comment.