Skip to content

Commit

Permalink
feat: pass SpecId into BlockSTM & VM
Browse files Browse the repository at this point in the history
  • Loading branch information
hai-rise committed Apr 26, 2024
1 parent d394329 commit 3b1683a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/block_stm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
thread,
};

use revm::primitives::{BlockEnv, ResultAndState, TxEnv};
use revm::primitives::{BlockEnv, ResultAndState, SpecId, TxEnv};

use crate::{
mv_memory::{MvMemory, ReadMemoryResult},
Expand All @@ -23,6 +23,7 @@ impl BlockSTM {
/// TODO: Better concurrency control
pub fn run(
storage: Storage,
spec_id: SpecId,
block_env: BlockEnv,
txs: Vec<TxEnv>,
concurrency_level: NonZeroUsize,
Expand All @@ -32,7 +33,7 @@ impl BlockSTM {
let mv_memory = Arc::new(MvMemory::new(block_size));
// TODO: Better error handling
let mut beneficiary_account_info = storage.basic(block_env.coinbase).unwrap();
let vm = Vm::new(storage, block_env.clone(), txs, mv_memory.clone());
let vm = Vm::new(storage, spec_id, block_env.clone(), txs, mv_memory.clone());
// TODO: Should we move this to `Vm`?
let execution_results = (0..block_size).map(|_| Mutex::new(None)).collect();
// TODO: Better thread handling
Expand Down
10 changes: 7 additions & 3 deletions src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::{cell::RefCell, sync::Arc, thread};

use revm::{
primitives::{
AccountInfo, Address, BlockEnv, Bytecode, CancunSpec, EVMError, ResultAndState, TxEnv,
B256, U256,
AccountInfo, Address, BlockEnv, Bytecode, CancunSpec, EVMError, ResultAndState, SpecId,
TxEnv, B256, U256,
},
Database, Evm, Handler,
};
Expand Down Expand Up @@ -194,6 +194,7 @@ impl Database for VmDb {
// `Vm` can be shared among threads.
pub(crate) struct Vm {
storage: Arc<Storage>,
spec_id: SpecId,
block_env: BlockEnv,
txs: Arc<Vec<TxEnv>>,
mv_memory: Arc<MvMemory>,
Expand All @@ -202,13 +203,15 @@ pub(crate) struct Vm {
impl Vm {
pub(crate) fn new(
storage: Storage,
spec_id: SpecId,
block_env: BlockEnv,
txs: Vec<TxEnv>,
// TODO: Make `Vm` own `MvMemory` away from `BlockSTM::run`?
mv_memory: Arc<MvMemory>,
) -> Self {
Self {
storage: Arc::new(storage),
spec_id,
block_env,
txs: Arc::new(txs),
mv_memory,
Expand Down Expand Up @@ -258,9 +261,10 @@ impl Vm {

let mut evm = Evm::builder()
.with_db(&mut db)
.with_handler(handler)
.with_spec_id(self.spec_id)
.with_block_env(self.block_env.clone())
.with_tx_env(self.txs.get(tx_idx).unwrap().clone())
.with_handler(handler)
.build();

let evm_result = evm.transact();
Expand Down
8 changes: 6 additions & 2 deletions tests/beneficiary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
// TODO: Add more test scenarios around the beneficiary account's activities in the block.

use rand::random;
use revm::primitives::{alloy_primitives::U160, env::TxEnv, Address, BlockEnv, TransactTo, U256};
use revm::primitives::{
alloy_primitives::U160, env::TxEnv, Address, BlockEnv, SpecId, TransactTo, U256,
};

mod common;

#[test]
fn beneficiary() {
let block_size = 100_000; // number of transactions
let spec_id = SpecId::LATEST;
let block_env = BlockEnv::default();
let block_size = 100_000; // number of transactions

common::test_txs(
spec_id,
block_env,
// Mock `block_size` transactions sending some tokens to itself.
// Skipping `Address::ZERO` as the beneficiary account.
Expand Down
9 changes: 6 additions & 3 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{num::NonZeroUsize, thread};
use block_stm_revm::{BlockSTM, Storage};
use revm::{
primitives::{
alloy_primitives::U160, AccountInfo, Address, BlockEnv, ResultAndState, TxEnv, U256,
alloy_primitives::U160, AccountInfo, Address, BlockEnv, ResultAndState, SpecId, TxEnv, U256,
},
DatabaseCommit, Evm, InMemoryDB,
};
Expand Down Expand Up @@ -31,13 +31,15 @@ fn mock_dbs(num_prefilled_accounts: usize) -> (InMemoryDB, Storage) {
// The source-of-truth sequential execution result that BlockSTM must match.
fn execute_sequential(
mut db: InMemoryDB,
spec_id: SpecId,
block_env: BlockEnv,
txs: &[TxEnv],
) -> Vec<ResultAndState> {
txs.iter()
.map(|tx| {
let result_and_state = Evm::builder()
.with_ref_db(&mut db)
.with_spec_id(spec_id)
.with_block_env(block_env.clone())
.with_tx_env(tx.clone())
.build()
Expand All @@ -52,12 +54,13 @@ fn execute_sequential(

// Execute a list of transactions sequentially & with BlockSTM and assert that
// the execution results match.
pub(crate) fn test_txs(block_env: BlockEnv, txs: Vec<TxEnv>) {
pub(crate) fn test_txs(spec_id: SpecId, block_env: BlockEnv, txs: Vec<TxEnv>) {
// TODO: Decouple the (number of) prefilled accounts with the number of transactions.
let (sequential_db, block_stm_storage) = mock_dbs(txs.len());
let result_sequential = execute_sequential(sequential_db, block_env.clone(), &txs);
let result_sequential = execute_sequential(sequential_db, spec_id, block_env.clone(), &txs);
let result_block_stm = BlockSTM::run(
block_stm_storage,
spec_id,
block_env,
txs,
thread::available_parallelism().unwrap_or(NonZeroUsize::MIN),
Expand Down
8 changes: 6 additions & 2 deletions tests/raw_transfers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@
// Currently, we only have a no-state-conflict test of user account sending to themselves.
// TODO: Add more tests of accounts cross-transferring to create state depdendencies.

use revm::primitives::{alloy_primitives::U160, env::TxEnv, Address, BlockEnv, TransactTo, U256};
use revm::primitives::{
alloy_primitives::U160, env::TxEnv, Address, BlockEnv, SpecId, TransactTo, U256,
};

mod common;

#[test]
fn raw_transfers() {
let block_size = 100_000; // number of transactions
let spec_id = SpecId::LATEST;
let block_env = BlockEnv::default();
let block_size = 100_000; // number of transactions

common::test_txs(
spec_id,
block_env,
// Mock `block_size` transactions sending some tokens to itself.
// Skipping `Address::ZERO` as the beneficiary account.
Expand Down

0 comments on commit 3b1683a

Please sign in to comment.