Skip to content
Open
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
83 changes: 12 additions & 71 deletions Cargo.lock

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

14 changes: 7 additions & 7 deletions bin/portal-bridge/src/bridge/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use trin_execution::{
execution::TrinExecution,
storage::{
account_db::AccountDB, evm_db::EvmDB, execution_position::ExecutionPosition,
utils::setup_rocksdb,
utils::setup_redb,
},
subcommands::e2ss::{
import::StateImporter,
Expand Down Expand Up @@ -181,8 +181,8 @@ impl StateBridge {
// 1. Download the e2ss file and import the state snapshot
let data_dir = setup_data_dir(APP_NAME, self.data_dir.clone(), false)?;
let next_block_number = {
let rocks_db = Arc::new(setup_rocksdb(&data_dir)?);
let execution_position = ExecutionPosition::initialize_from_db(rocks_db.clone())?;
let red_db = Arc::new(setup_redb(&data_dir)?);
let execution_position = ExecutionPosition::initialize_from_db(red_db.clone())?;
execution_position.next_block_number()
};

Expand Down Expand Up @@ -240,15 +240,15 @@ impl StateBridge {

// 2. Start the state bridge

let rocks_db = Arc::new(setup_rocksdb(&data_dir)?);
let redb_db = Arc::new(setup_redb(&data_dir)?);

let execution_position = ExecutionPosition::initialize_from_db(rocks_db.clone())?;
let execution_position = ExecutionPosition::initialize_from_db(redb_db.clone())?;
ensure!(
execution_position.next_block_number() > 0,
"Trin execution not initialized!"
);

let mut evm_db = EvmDB::new(StateConfig::default(), rocks_db, &execution_position)
let mut evm_db = EvmDB::new(StateConfig::default(), redb_db, &execution_position)
.expect("Failed to create EVM database");

self.gossip_whole_state_snapshot(&mut evm_db, execution_position)
Expand Down Expand Up @@ -424,7 +424,7 @@ impl StateBridge {

// check contract storage content key/value
if account.storage_root != EMPTY_ROOT_HASH {
let account_db = AccountDB::new(address_hash, evm_db.db.clone());
let account_db = AccountDB::new(address_hash, evm_db.db.clone())?;
let trie = EthTrie::from(Arc::new(account_db), account.storage_root)?.db;

let storage_walker = TrieWalker::new(account.storage_root, trie, None)?;
Expand Down
3 changes: 2 additions & 1 deletion bin/trin-execution/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ reqwest = { workspace = true, features = ["stream"] }
revm.workspace = true
revm-inspectors = "0.23"
revm-primitives.workspace = true
rocksdb = "0.23"
redb = "2.5.0"
serde = { workspace = true, features = ["rc"] }
serde_json.workspace = true
thiserror.workspace = true
Expand All @@ -44,3 +44,4 @@ trin-utils.workspace = true

[dev-dependencies]
test-log.workspace = true
tempfile = "3"
66 changes: 38 additions & 28 deletions bin/trin-execution/src/evm/block_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::{
set_int_gauge_vec, start_timer_vec, stop_timer, BLOCK_HEIGHT, BLOCK_PROCESSING_TIMES,
TRANSACTION_PROCESSING_TIMES,
},
storage::evm_db::EvmDB,
storage::evm_db::{EvmDB, ACCOUNTS_TABLE, BLOCK_HASHES_TABLE},
};

pub const BLOCKHASH_SERVE_WINDOW: u64 = 256;
Expand Down Expand Up @@ -127,23 +127,28 @@ impl BlockExecutor {
fn process_genesis(&mut self) -> anyhow::Result<()> {
let genesis: GenesisConfig =
serde_json::from_str(include_str!("../../resources/genesis/mainnet.json"))?;

for (address, alloc_balance) in genesis.alloc {
let address_hash = keccak256(address);
let mut account = AccountState::default();
account.balance += alloc_balance.balance;
self.evm
.db()
.database
.trie
.lock()
.insert(address_hash.as_ref(), &alloy::rlp::encode(&account))?;
self.evm
.db()
.database
.db
.put(address_hash, alloy::rlp::encode(account))?;
let db = &self.evm.db().database.db;
let txn = db.begin_write()?;
{
let mut table = txn.open_table(ACCOUNTS_TABLE)?;
for (address, alloc_balance) in genesis.alloc {
let address_hash = keccak256(address);
let mut account = AccountState::default();
account.balance += alloc_balance.balance;
self.evm
.db()
.database
.trie
.lock()
.insert(address_hash.as_ref(), &alloy::rlp::encode(&account))?;

table.insert(
address_hash.as_slice(),
alloy::rlp::encode(account).as_slice(),
)?;
}
}
txn.commit()?;

Ok(())
}
Expand Down Expand Up @@ -240,19 +245,24 @@ impl BlockExecutor {
/// insert block hash into database and remove old one
fn manage_block_hash_serve_window(&mut self, header: &Header) -> anyhow::Result<()> {
let timer = start_timer_vec(&BLOCK_PROCESSING_TIMES, &["insert_blockhash"]);
self.evm.db().database.db.put(
keccak256(B256::from(U256::from(header.number))),
header.hash_slow(),
)?;
if header.number >= BLOCKHASH_SERVE_WINDOW {
self.evm
.db()
.database
.db
.delete(keccak256(B256::from(U256::from(

let db = &self.evm.db().database.db;
let txn = db.begin_write()?;
{
let mut table = txn.open_table(BLOCK_HASHES_TABLE)?;
let key = keccak256(B256::from(U256::from(header.number)));
let value = header.hash_slow();
table.insert(key.as_slice(), value.as_slice())?;

if header.number >= BLOCKHASH_SERVE_WINDOW {
let old_key = keccak256(B256::from(U256::from(
header.number - BLOCKHASH_SERVE_WINDOW,
))))?;
)));
table.remove(old_key.as_slice())?;
}
}
txn.commit()?;

stop_timer(timer);
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions bin/trin-execution/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
e2hs::manager::E2HSManager,
evm::block_executor::BlockExecutor,
metrics::{start_timer_vec, stop_timer, BLOCK_PROCESSING_TIMES},
storage::{evm_db::EvmDB, execution_position::ExecutionPosition, utils::setup_rocksdb},
storage::{evm_db::EvmDB, execution_position::ExecutionPosition, utils::setup_redb},
};

pub struct TrinExecution {
Expand All @@ -31,7 +31,7 @@ pub struct TrinExecution {

impl TrinExecution {
pub async fn new(data_dir: &Path, config: StateConfig) -> anyhow::Result<Self> {
let db = Arc::new(setup_rocksdb(data_dir)?);
let db = Arc::new(setup_redb(data_dir)?);
let execution_position = ExecutionPosition::initialize_from_db(db.clone())?;

let database = EvmDB::new(config.clone(), db, &execution_position)
Expand Down
Loading