Skip to content

Commit

Permalink
Build EVM Environment from payload
Browse files Browse the repository at this point in the history
  • Loading branch information
saibatizoku committed Oct 26, 2021
1 parent 85c71ec commit 4a20866
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
3 changes: 3 additions & 0 deletions chain-evm/src/machine.rs
Expand Up @@ -62,6 +62,9 @@ pub type BlockGasLimit = U256;
/// A block's origin
pub type Origin = H160;

/// A block's coinbase
pub type BlockCoinBase = H160;

/// Gas quantity integer for EVM operations.
pub type Gas = U256;

Expand Down
36 changes: 34 additions & 2 deletions chain-impl-mockchain/src/config.rs
Expand Up @@ -14,7 +14,7 @@ use chain_core::packer::Codec;
use chain_core::property;
use chain_crypto::PublicKey;
#[cfg(feature = "evm")]
use chain_evm::machine::{Config, Environment};
use chain_evm::machine::{BlockCoinBase, BlockHash, Config, Environment, Origin};
#[cfg(feature = "evm")]
use std::convert::TryInto;
use std::{
Expand Down Expand Up @@ -993,8 +993,40 @@ impl ConfigParamVariant for EvmConfigParams {
has_ext_code_hash,
estimate,
};

// Read Enviroment
todo!();
let gas_price = rb.get_slice(32)?.into();
let origin = Origin::from_slice(rb.get_slice(20)?);
let chain_id = rb.get_slice(32)?.into();

let block_hashes = match rb.get_u64()? {
0 => Vec::new(),
n => (0..n)
.map(|_| BlockHash::from_slice(rb.get_slice(32).unwrap()))
.collect(),
};
let block_number = rb.get_slice(32)?.into();
let block_coinbase = BlockCoinBase::from_slice(rb.get_slice(20)?);
let block_timestamp = rb.get_slice(32)?.into();
let block_difficulty = rb.get_slice(32)?.into();
let block_gas_limit = rb.get_slice(32)?.into();

let environment = Environment {
gas_price,
origin,
chain_id,
block_hashes,
block_number,
block_coinbase,
block_timestamp,
block_difficulty,
block_gas_limit,
};

Ok(EvmConfigParams {
config: Box::new(config),
environment,
})
}
}

Expand Down
6 changes: 3 additions & 3 deletions chain-impl-mockchain/src/testing/ledger.rs
Expand Up @@ -34,8 +34,8 @@ use chain_crypto::*;
#[cfg(feature = "evm")]
use chain_evm::{
machine::{
BlockDifficulty, BlockHashes, BlockNumber, BlockTimestamp, ChainId, Config, Environment,
GasLimit, GasPrice, Origin,
BlockCoinBase, BlockDifficulty, BlockHashes, BlockNumber, BlockTimestamp, ChainId, Config,
Environment, GasLimit, GasPrice, Origin,
},
state::AccountAddress,
};
Expand Down Expand Up @@ -122,7 +122,7 @@ impl ConfigBuilder {
chain_id: ChainId::zero(),
block_hashes: BlockHashes::new(),
block_number: BlockNumber::zero(),
block_coinbase: AccountAddress::zero(),
block_coinbase: BlockCoinBase::zero(),
block_timestamp: BlockTimestamp::now(),
block_difficulty: BlockDifficulty::from(131_072),
// FIXME: need to set a real limit
Expand Down

0 comments on commit 4a20866

Please sign in to comment.