Skip to content

Commit

Permalink
Trickle down block timestamp to EVM (#1980)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jouzo authored May 11, 2023
1 parent 7c327ad commit 69d238e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
11 changes: 4 additions & 7 deletions lib/ain-evm/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use log::debug;
use primitive_types::H256;
use std::error::Error;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};

const GENESIS_STATE_ROOT: &str =
"0xbc36789e7a1e281436464229828f817d6612f7b477d66591ff96a9e064bcc98a";
Expand Down Expand Up @@ -49,7 +48,8 @@ impl Handlers {
context: u64,
update_state: bool,
difficulty: u32,
miner_address: Option<H160>,
beneficiary: H160,
timestamp: u64,
) -> Result<(BlockAny, Vec<String>), Box<dyn Error>> {
let mut all_transactions = Vec::with_capacity(self.evm.tx_queues.len(context));
let mut failed_transactions = Vec::with_capacity(self.evm.tx_queues.len(context));
Expand Down Expand Up @@ -134,7 +134,7 @@ impl Handlers {
let block = Block::new(
PartialHeader {
parent_hash,
beneficiary: miner_address.unwrap_or_default(),
beneficiary,
state_root: if update_state {
backend.commit()
} else {
Expand All @@ -146,10 +146,7 @@ impl Handlers {
number: parent_number + 1,
gas_limit: U256::from(30_000_000),
gas_used: U256::from(gas_used),
timestamp: SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis() as u64,
timestamp,
extra_data: Vec::default(),
mix_hash: H256::default(),
nonce: H64::default(),
Expand Down
15 changes: 10 additions & 5 deletions lib/ain-rs-exports/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ain_evm::transaction::{self, SignedTx};
use ain_grpc::{init_runtime, start_servers, stop_runtime};
use ain_grpc::{init_evm_runtime, start_servers, stop_runtime};

use ain_evm::runtime::RUNTIME;
use log::debug;
Expand Down Expand Up @@ -56,6 +56,7 @@ pub mod ffi {
update_state: bool,
difficulty: u32,
miner_address: [u8; 20],
timestamp: u64,
) -> Result<FinalizeBlockResult>;

unsafe fn init(_argc: i32, _argv: *const *const c_char);
Expand Down Expand Up @@ -181,12 +182,16 @@ fn evm_finalize(
update_state: bool,
difficulty: u32,
miner_address: [u8; 20],
timestamp: u64,
) -> Result<ffi::FinalizeBlockResult, Box<dyn Error>> {
let eth_address = H160::from(miner_address);
let (block, failed_txs) =
RUNTIME
.handlers
.finalize_block(context, update_state, difficulty, Some(eth_address))?;
let (block, failed_txs) = RUNTIME.handlers.finalize_block(
context,
update_state,
difficulty,
eth_address,
timestamp,
)?;
Ok(ffi::FinalizeBlockResult {
block_header: block.header.rlp_bytes().into(),
failed_transactions: failed_txs,
Expand Down
2 changes: 1 addition & 1 deletion src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
std::vector<uint8_t> evmHeader{};
if (IsEVMEnabled(nHeight, mnview)) {
std::array<uint8_t, 20> dummyAddress{};
auto blockResult = evm_finalize(evmContext, false, pos::GetNextWorkRequired(pindexPrev, pblock->nTime, consensus), dummyAddress);
auto blockResult = evm_finalize(evmContext, false, pos::GetNextWorkRequired(pindexPrev, pblock->nTime, consensus), dummyAddress, blockTime);
evmHeader.resize(blockResult.block_header.size());
std::copy(blockResult.block_header.begin(), blockResult.block_header.end(), evmHeader.begin());

Expand Down
2 changes: 1 addition & 1 deletion src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2906,7 +2906,7 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl
std::copy(minter.begin(), minter.end(), minerAddress.begin());
}

const auto blockResult = evm_finalize(evmContext, true, block.nBits, minerAddress);
const auto blockResult = evm_finalize(evmContext, true, block.nBits, minerAddress, block.GetBlockTime());

if (!blockResult.failed_transactions.empty()) {
std::vector<std::string> failedTransactions;
Expand Down

0 comments on commit 69d238e

Please sign in to comment.