Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,6 @@ codegen-units = 16
inherits = "release"
lto = "fat"
codegen-units = 1

[dev-dependencies]
uuid = { version = "1", features = ["v4"] }
12 changes: 6 additions & 6 deletions src/node/evm/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ where
Spec: EthChainSpec,
{
/// Reference to the specification object.
spec: Spec,
pub(super) spec: Spec,
/// Inner EVM.
evm: EVM,
pub(super) evm: EVM,
/// Gas used in the block.
gas_used: u64,
/// Receipts of executed transactions.
Expand All @@ -61,7 +61,7 @@ where
/// Receipt builder.
receipt_builder: R,
/// System contracts used to trigger fork specific logic.
system_contracts: SystemContract<Spec>,
pub(super) system_contracts: SystemContract<Spec>,
/// Hertz patch manager for mainnet compatibility
/// TODO: refine later.
#[allow(dead_code)]
Expand Down Expand Up @@ -192,7 +192,7 @@ where
tx: &TransactionSigned,
sender: Address,
) -> Result<(), BlockExecutionError> {
trace!("⚙️ [BSC] transact_system_tx: sender={:?}, tx_hash={:?}, to={:?}, value={}, gas_limit={}",
trace!("Start to transact_system_tx: sender={:?}, tx_hash={:?}, to={:?}, value={}, gas_limit={}",
sender, tx.hash(), tx.to(), tx.value(), tx.gas_limit());

// TODO: Consensus handle reverting slashing system txs (they shouldnt be in the block)
Expand All @@ -205,7 +205,7 @@ where
.map_err(BlockExecutionError::other)?
.unwrap_or_default();

trace!("⚙️ [BSC] transact_system_tx: sender account balance={}, nonce={}", account.balance, account.nonce);
trace!("transact_system_tx: sender account balance={}, nonce={}", account.balance, account.nonce);

let tx_env = BscTxEnv {
base: TxEnv {
Expand Down Expand Up @@ -234,7 +234,7 @@ where
is_system_transaction: true,
};

trace!("⚙️ [BSC] transact_system_tx: TxEnv gas_price={}, gas_limit={}, is_system_transaction={}",
trace!("transact_system_tx: TxEnv gas_price={}, gas_limit={}, is_system_transaction={}",
tx_env.base.gas_price, tx_env.base.gas_limit, tx_env.is_system_transaction);

let result_and_state = self.evm.transact(tx_env).map_err(BlockExecutionError::other)?;
Expand Down
94 changes: 92 additions & 2 deletions src/node/evm/pre_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ use reth_chainspec::{EthChainSpec, EthereumHardforks, Hardforks};
use reth_evm::{eth::receipt_builder::ReceiptBuilder, execute::BlockExecutionError, Database, Evm, FromRecoveredTx, FromTxWithEncoded, IntoTxEnv};
use reth_primitives::TransactionSigned;
use reth_revm::State;
use revm::context::BlockEnv;
use revm::{
context::{BlockEnv, TxEnv},
primitives::{Address, Bytes, TxKind, U256},
};
use alloy_consensus::TxReceipt;
//use alloy_primitives::Address;
use crate::consensus::parlia::VoteAddress;
use crate::system_contracts::feynman_fork::ValidatorElectionInfo;
// use consensus trait object for cascading validation

impl<'a, DB, EVM, Spec, R: ReceiptBuilder> BscBlockExecutor<'a, EVM, Spec, R>
Expand Down Expand Up @@ -59,6 +65,7 @@ where
.unwrap()
.verify_cascading_fields(&header, &parent_header, None, &snap);

// TODO: remove this part, just for debug.
if let Err(err) = verify_res {
let proposer = header.beneficiary;
let is_inturn = snap.is_inturn(proposer);
Expand Down Expand Up @@ -89,8 +96,91 @@ where
return Err(err);
}

// TODO: query finalise input from parlia consensus object.
// TODO: query validator-related info from system contract.
let (validator_set, vote_address) = self.get_current_validators(block_number)?;
tracing::info!("validator_set: {:?}, vote_address: {:?}", validator_set, vote_address);

// TODO: query election info from system contract.
if self.spec.is_feynman_active_at_timestamp(header.timestamp) &&
!self.spec.is_feynman_transition_at_timestamp(header.timestamp, parent_header.timestamp)
{
let (to, data) = self.system_contracts.get_max_elected_validators();
let bz = self.eth_call(to, data)?;
let max_elected_validators = self.system_contracts.unpack_data_into_max_elected_validators(bz.as_ref());
tracing::info!("max_elected_validators: {:?}", max_elected_validators);

let (to, data) = self.system_contracts.get_validator_election_info();
let bz = self.eth_call(to, data)?;

let (validators, voting_powers, vote_addrs, total_length) =
self.system_contracts.unpack_data_into_validator_election_info(bz.as_ref());

let total_length = total_length.to::<u64>() as usize;
if validators.len() != total_length ||
voting_powers.len() != total_length ||
vote_addrs.len() != total_length
{
return Err(BlockExecutionError::msg("Failed to get top validators"));
}

let validator_election_info: Vec<ValidatorElectionInfo> = validators
.into_iter()
.zip(voting_powers)
.zip(vote_addrs)
.map(|((validator, voting_power), vote_addr)| ValidatorElectionInfo {
address: validator,
voting_power,
vote_address: vote_addr,
})
.collect();
tracing::info!("validator_election_info: {:?}", validator_election_info);
}

Ok(())
}

fn get_current_validators(&mut self, block_number: u64) -> Result<(Vec<Address>, Vec<VoteAddress>), BlockExecutionError> {
if self.spec.is_luban_active_at_block(block_number) {
let (to, data) = self.system_contracts.get_current_validators();
let output = self.eth_call(to, data)?;
Ok(self.system_contracts.unpack_data_into_validator_set(&output))
} else {
let (to, data) = self.system_contracts.get_current_validators_before_luban(block_number);
let output = self.eth_call(to, data)?;
let validator_set = self.system_contracts.unpack_data_into_validator_set_before_luban(&output);
Ok((validator_set, Vec::new()))
}
}

fn eth_call(&mut self, to: Address, data: Bytes) -> Result<Bytes, BlockExecutionError> {
let tx_env = BscTxEnv {
base: TxEnv {
caller: Address::default(),
kind: TxKind::Call(to),
nonce: 0,
gas_limit: self.evm.block().gas_limit,
value: U256::ZERO,
data: data.clone(),
gas_price: 0,
chain_id: Some(self.spec.chain().id()),
gas_priority_fee: None,
access_list: Default::default(),
blob_hashes: Vec::new(),
max_fee_per_blob_gas: 0,
tx_type: 0,
authorization_list: Default::default(),
},
is_system_transaction: false,
};

let result_and_state = self.evm.transact(tx_env).map_err(|err| BlockExecutionError::other(err))?;
if !result_and_state.result.is_success() {
tracing::error!("Failed to eth call, to: {:?}, data: {:?}", to, data);
return Err(BlockExecutionError::msg("ETH call failed"));
}
let output = result_and_state.result.output().ok_or(BlockExecutionError::msg("ETH call output is None"))?;
Ok(output.clone())
}


}
Loading