Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trickle down block timestamp to EVM #1980

Merged
merged 1 commit into from
May 11, 2023
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
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