Skip to content

Commit df3b3b5

Browse files
committed
Replace RocksDB with Redb as backing for EVM database
1 parent 0d6309b commit df3b3b5

File tree

15 files changed

+359
-282
lines changed

15 files changed

+359
-282
lines changed

Cargo.lock

Lines changed: 12 additions & 71 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/portal-bridge/src/bridge/state.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use trin_execution::{
3737
execution::TrinExecution,
3838
storage::{
3939
account_db::AccountDB, evm_db::EvmDB, execution_position::ExecutionPosition,
40-
utils::setup_rocksdb,
40+
utils::setup_redb,
4141
},
4242
subcommands::e2ss::{
4343
import::StateImporter,
@@ -181,8 +181,8 @@ impl StateBridge {
181181
// 1. Download the e2ss file and import the state snapshot
182182
let data_dir = setup_data_dir(APP_NAME, self.data_dir.clone(), false)?;
183183
let next_block_number = {
184-
let rocks_db = Arc::new(setup_rocksdb(&data_dir)?);
185-
let execution_position = ExecutionPosition::initialize_from_db(rocks_db.clone())?;
184+
let red_db = Arc::new(setup_redb(&data_dir)?);
185+
let execution_position = ExecutionPosition::initialize_from_db(red_db.clone())?;
186186
execution_position.next_block_number()
187187
};
188188

@@ -240,15 +240,15 @@ impl StateBridge {
240240

241241
// 2. Start the state bridge
242242

243-
let rocks_db = Arc::new(setup_rocksdb(&data_dir)?);
243+
let redb_db = Arc::new(setup_redb(&data_dir)?);
244244

245-
let execution_position = ExecutionPosition::initialize_from_db(rocks_db.clone())?;
245+
let execution_position = ExecutionPosition::initialize_from_db(redb_db.clone())?;
246246
ensure!(
247247
execution_position.next_block_number() > 0,
248248
"Trin execution not initialized!"
249249
);
250250

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

254254
self.gossip_whole_state_snapshot(&mut evm_db, execution_position)
@@ -424,7 +424,7 @@ impl StateBridge {
424424

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

430430
let storage_walker = TrieWalker::new(account.storage_root, trie, None)?;

bin/trin-execution/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ reqwest = { workspace = true, features = ["stream"] }
3333
revm.workspace = true
3434
revm-inspectors = "0.23"
3535
revm-primitives.workspace = true
36-
rocksdb = "0.23"
36+
redb = "2.5.0"
3737
serde = { workspace = true, features = ["rc"] }
3838
serde_json.workspace = true
3939
thiserror.workspace = true
@@ -44,3 +44,4 @@ trin-utils.workspace = true
4444

4545
[dev-dependencies]
4646
test-log.workspace = true
47+
tempfile = "3"

bin/trin-execution/src/evm/block_executor.rs

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::{
2929
set_int_gauge_vec, start_timer_vec, stop_timer, BLOCK_HEIGHT, BLOCK_PROCESSING_TIMES,
3030
TRANSACTION_PROCESSING_TIMES,
3131
},
32-
storage::evm_db::EvmDB,
32+
storage::evm_db::{EvmDB, ACCOUNTS_TABLE, BLOCK_HASHES_TABLE},
3333
};
3434

3535
pub const BLOCKHASH_SERVE_WINDOW: u64 = 256;
@@ -127,23 +127,25 @@ impl BlockExecutor {
127127
fn process_genesis(&mut self) -> anyhow::Result<()> {
128128
let genesis: GenesisConfig =
129129
serde_json::from_str(include_str!("../../resources/genesis/mainnet.json"))?;
130-
131-
for (address, alloc_balance) in genesis.alloc {
132-
let address_hash = keccak256(address);
133-
let mut account = AccountState::default();
134-
account.balance += alloc_balance.balance;
135-
self.evm
136-
.db()
137-
.database
138-
.trie
139-
.lock()
140-
.insert(address_hash.as_ref(), &alloy::rlp::encode(&account))?;
141-
self.evm
142-
.db()
143-
.database
144-
.db
145-
.put(address_hash, alloy::rlp::encode(account))?;
130+
let db = &self.evm.db().database.db;
131+
let txn = db.begin_write()?;
132+
{
133+
let mut table = txn.open_table(ACCOUNTS_TABLE)?;
134+
for (address, alloc_balance) in genesis.alloc {
135+
let address_hash = keccak256(address);
136+
let mut account = AccountState::default();
137+
account.balance += alloc_balance.balance;
138+
self.evm
139+
.db()
140+
.database
141+
.trie
142+
.lock()
143+
.insert(address_hash.as_ref(), &alloy::rlp::encode(&account))?;
144+
145+
table.insert(address_hash.as_slice(), alloy::rlp::encode(account).as_slice())?;
146+
}
146147
}
148+
txn.commit()?;
147149

148150
Ok(())
149151
}
@@ -240,19 +242,22 @@ impl BlockExecutor {
240242
/// insert block hash into database and remove old one
241243
fn manage_block_hash_serve_window(&mut self, header: &Header) -> anyhow::Result<()> {
242244
let timer = start_timer_vec(&BLOCK_PROCESSING_TIMES, &["insert_blockhash"]);
243-
self.evm.db().database.db.put(
244-
keccak256(B256::from(U256::from(header.number))),
245-
header.hash_slow(),
246-
)?;
247-
if header.number >= BLOCKHASH_SERVE_WINDOW {
248-
self.evm
249-
.db()
250-
.database
251-
.db
252-
.delete(keccak256(B256::from(U256::from(
253-
header.number - BLOCKHASH_SERVE_WINDOW,
254-
))))?;
245+
246+
let db = &self.evm.db().database.db;
247+
let txn = db.begin_write()?;
248+
{
249+
let mut table = txn.open_table(BLOCK_HASHES_TABLE)?;
250+
let key = keccak256(B256::from(U256::from(header.number)));
251+
let value = header.hash_slow();
252+
table.insert(key.as_slice(), value.as_slice())?;
253+
254+
if header.number >= BLOCKHASH_SERVE_WINDOW {
255+
let old_key = keccak256(B256::from(U256::from(header.number - BLOCKHASH_SERVE_WINDOW)));
256+
table.remove(old_key.as_slice())?;
257+
}
255258
}
259+
txn.commit()?;
260+
256261
stop_timer(timer);
257262
Ok(())
258263
}

bin/trin-execution/src/execution.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::{
1818
e2hs::manager::E2HSManager,
1919
evm::block_executor::BlockExecutor,
2020
metrics::{start_timer_vec, stop_timer, BLOCK_PROCESSING_TIMES},
21-
storage::{evm_db::EvmDB, execution_position::ExecutionPosition, utils::setup_rocksdb},
21+
storage::{evm_db::EvmDB, execution_position::ExecutionPosition, utils::setup_redb},
2222
};
2323

2424
pub struct TrinExecution {
@@ -31,7 +31,7 @@ pub struct TrinExecution {
3131

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

3737
let database = EvmDB::new(config.clone(), db, &execution_position)

0 commit comments

Comments
 (0)