Skip to content

Commit 8f654a1

Browse files
committed
Fix remaining issues before rebase
1 parent a2a5e0b commit 8f654a1

File tree

9 files changed

+73
-41
lines changed

9 files changed

+73
-41
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ impl BlockExecutor {
129129
serde_json::from_str(include_str!("../../resources/genesis/mainnet.json"))?;
130130
let db = &self.evm.db().database.db;
131131
let txn = db.begin_write()?;
132-
{
133-
let mut table = txn.open_table(ACCOUNTS_TABLE)?;
134-
for (address, alloc_balance) in genesis.alloc {
132+
{
133+
let mut table = txn.open_table(ACCOUNTS_TABLE)?;
134+
for (address, alloc_balance) in genesis.alloc {
135135
let address_hash = keccak256(address);
136136
let mut account = AccountState::default();
137137
account.balance += alloc_balance.balance;
@@ -142,7 +142,10 @@ impl BlockExecutor {
142142
.lock()
143143
.insert(address_hash.as_ref(), &alloy::rlp::encode(&account))?;
144144

145-
table.insert(address_hash.as_slice(), alloy::rlp::encode(account).as_slice())?;
145+
table.insert(
146+
address_hash.as_slice(),
147+
alloy::rlp::encode(account).as_slice(),
148+
)?;
146149
}
147150
}
148151
txn.commit()?;
@@ -252,7 +255,9 @@ impl BlockExecutor {
252255
table.insert(key.as_slice(), value.as_slice())?;
253256

254257
if header.number >= BLOCKHASH_SERVE_WINDOW {
255-
let old_key = keccak256(B256::from(U256::from(header.number - BLOCKHASH_SERVE_WINDOW)));
258+
let old_key = keccak256(B256::from(U256::from(
259+
header.number - BLOCKHASH_SERVE_WINDOW,
260+
)));
256261
table.remove(old_key.as_slice())?;
257262
}
258263
}

bin/trin-execution/src/storage/account_db.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ const ACCOUNT_TABLE: TableDefinition<&[u8], &[u8]> = TableDefinition::new("accou
1313
#[derive(Debug, Clone)]
1414
pub struct AccountDB {
1515
pub address_hash: B256,
16-
pub db: Arc<ReDB>
16+
pub db: Arc<ReDB>,
17+
}
18+
19+
impl From<redb::Error> for EVMError {
20+
fn from(e: redb::Error) -> Self {
21+
EVMError::DB(Box::new(e))
22+
}
1723
}
1824

1925
impl AccountDB {
@@ -22,7 +28,7 @@ impl AccountDB {
2228
txn.open_table(ACCOUNT_TABLE).map_err(EVMError::from)?;
2329
txn.commit().map_err(EVMError::from)?;
2430

25-
Ok(Self {address_hash, db})
31+
Ok(Self { address_hash, db })
2632
}
2733

2834
pub fn get_db_key(&self, key: &[u8]) -> Vec<u8> {
@@ -44,7 +50,7 @@ impl DB for AccountDB {
4450

4551
match table.get(db_key.as_slice()).map_err(EVMError::from)? {
4652
Some(access_guard) => Ok(Some(access_guard.value().to_vec())),
47-
None => Ok(None)
53+
None => Ok(None),
4854
}
4955
}
5056

@@ -57,7 +63,9 @@ impl DB for AccountDB {
5763
{
5864
let mut table = txn.open_table(ACCOUNT_TABLE).map_err(EVMError::from)?;
5965
let db_key = self.get_db_key(key);
60-
table.insert(db_key.as_slice(), value.as_slice()).map_err(EVMError::from)?;
66+
table
67+
.insert(db_key.as_slice(), value.as_slice())
68+
.map_err(EVMError::from)?;
6169
}
6270
txn.commit().map_err(EVMError::from)?;
6371
Ok(())
@@ -81,4 +89,4 @@ impl DB for AccountDB {
8189
fn flush(&self) -> Result<(), Self::Error> {
8290
Ok(())
8391
}
84-
}
92+
}

bin/trin-execution/src/storage/error.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub enum EVMError {
1010
RLP(#[from] alloy::rlp::Error),
1111

1212
#[error("redb error {0}")]
13-
DB(#[from] redb::Error),
13+
DB(#[from] Box<redb::Error>),
1414

1515
#[error("ethportal error {0}")]
1616
ANYHOW(#[from] anyhow::Error),
@@ -24,7 +24,6 @@ pub enum EVMError {
2424

2525
impl DBErrorMarker for EVMError {}
2626

27-
2827
impl From<redb::DatabaseError> for EVMError {
2928
fn from(err: redb::DatabaseError) -> Self {
3029
EVMError::ANYHOW(anyhow::anyhow!("redb database error: {}", err))

bin/trin-execution/src/storage/evm_db.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ use ethportal_api::types::state_trie::account_state::AccountState;
1010
use hashbrown::{HashMap as BrownHashMap, HashSet};
1111
use parking_lot::Mutex;
1212
use prometheus_exporter::prometheus::HistogramTimer;
13+
use redb::{Database as ReDB, Table, TableDefinition};
1314
use revm::{
1415
database::{states::PlainStorageChangeset, BundleState, OriginalValuesKnown},
1516
state::{AccountInfo, Bytecode},
1617
Database, DatabaseRef,
1718
};
1819
use revm_primitives::KECCAK_EMPTY;
19-
use redb::{Database as ReDB, Table, TableDefinition};
2020
use tracing::info;
2121

2222
use super::{account_db::AccountDB, execution_position::ExecutionPosition, trie_db::TrieReDB};
@@ -53,15 +53,15 @@ pub struct EvmDB {
5353
pub db: Arc<ReDB>,
5454
/// To get proofs and to verify trie state.
5555
pub trie: Arc<Mutex<EthTrie<TrieReDB>>>,
56-
}
56+
}
5757

5858
impl EvmDB {
5959
pub fn new(
6060
config: StateConfig,
6161
db: Arc<ReDB>,
6262
execution_position: &ExecutionPosition,
6363
) -> anyhow::Result<Self> {
64-
// Initialize empty byte code in the database
64+
// Initialize empty byte code in the database
6565
let txn = db.begin_write()?;
6666
{
6767
let mut contracts_table = txn.open_table(CONTRACTS_TABLE)?;
@@ -70,7 +70,7 @@ impl EvmDB {
7070
contracts_table.insert(B256::ZERO.as_slice(), empty_bytecode.as_ref())?;
7171
}
7272
txn.commit()?;
73-
73+
7474
// db.put(KECCAK_EMPTY, Bytecode::new().bytes().as_ref())?;
7575
// db.put(B256::ZERO, Bytecode::new().bytes().as_ref())?;
7676

@@ -100,7 +100,9 @@ impl EvmDB {
100100
let mut trie_diff = BrownHashMap::new();
101101

102102
let txn = self.db.begin_read().expect("Redb read transaction failed");
103-
let storage_table = txn.open_table(STORAGE_TABLE).expect("Failed to open Redb storage table");
103+
let storage_table = txn
104+
.open_table(STORAGE_TABLE)
105+
.expect("Failed to open Redb storage table");
104106

105107
for key in self
106108
.storage_cache
@@ -213,7 +215,10 @@ impl EvmDB {
213215
let txn = self.db.begin_write()?;
214216
{
215217
let mut table = txn.open_table(ACCOUNTS_TABLE)?;
216-
table.insert(address_hash.as_slice(), alloy::rlp::encode(&account_state).as_slice())?;
218+
table.insert(
219+
address_hash.as_slice(),
220+
alloy::rlp::encode(&account_state).as_slice(),
221+
)?;
217222
}
218223
txn.commit()?;
219224

@@ -291,7 +296,10 @@ impl EvmDB {
291296
let txn = self.db.begin_write()?;
292297
{
293298
let mut table = txn.open_table(ACCOUNTS_TABLE)?;
294-
table.insert(address_hash.as_slice(), alloy::rlp::encode(&account_state).as_slice())?;
299+
table.insert(
300+
address_hash.as_slice(),
301+
alloy::rlp::encode(&account_state).as_slice(),
302+
)?;
295303
}
296304
txn.commit()?;
297305
stop_timer(timer);
@@ -355,10 +363,12 @@ impl EvmDB {
355363
let txn = self.db.begin_write()?;
356364
{
357365
let mut table = txn.open_table(CONTRACTS_TABLE)?;
358-
table.insert(hash.as_slice(), bytecode.original_bytes().as_ref()).expect("Inserting contract code should never fail");
366+
table
367+
.insert(hash.as_slice(), bytecode.original_bytes().as_ref())
368+
.expect("Inserting contract code should never fail");
359369
}
360370
txn.commit()?;
361-
371+
362372
stop_timer(timer);
363373
}
364374
stop_timer(timer);
@@ -431,7 +441,7 @@ impl DatabaseRef for EvmDB {
431441
let result = match table.get(code_hash.as_slice()) {
432442
Ok(Some(value)) => Ok(Bytecode::new_raw(value.value().to_vec().into())),
433443
Ok(None) => Err(Self::Error::NotFound("code_by_hash".to_string())),
434-
Err(e) => Err(Self::Error::DB(e.into())),
444+
Err(e) => Err(Self::Error::DB(Box::new(e.into()))),
435445
};
436446
stop_timer(timer);
437447
result
@@ -470,7 +480,7 @@ impl DatabaseRef for EvmDB {
470480
let result = match table.get(key.as_slice()) {
471481
Ok(Some(value)) => Ok(B256::from_slice(value.value())),
472482
Ok(None) => Err(Self::Error::NotFound("block_hash".to_string())),
473-
Err(e) => Err(Self::Error::DB(e.into())),
483+
Err(e) => Err(Self::Error::DB(Box::new(e.into()))),
474484
};
475485

476486
stop_timer(timer);

bin/trin-execution/src/storage/execution_position.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use alloy::{
44
consensus::{Header, EMPTY_ROOT_HASH},
55
rlp::{Decodable, RlpDecodable, RlpEncodable},
66
};
7-
use revm_primitives::B256;
87
use redb::{Database as ReDB, TableDefinition};
8+
use revm_primitives::B256;
99
use serde::{Deserialize, Serialize};
1010

1111
const TABLE: TableDefinition<&[u8], &[u8]> = TableDefinition::new("execution");
@@ -29,7 +29,7 @@ impl ExecutionPosition {
2929
let table = txn.open_table(TABLE)?;
3030
match table.get(EXECUTION_POSITION_DB_KEY.as_slice())? {
3131
Some(value) => Ok(Decodable::decode(&mut value.value().as_ref())?),
32-
None => Ok(Self::default())
32+
None => Ok(Self::default()),
3333
}
3434
}
3535

@@ -53,14 +53,13 @@ impl ExecutionPosition {
5353
{
5454
let mut table = txn.open_table(TABLE)?;
5555
table.insert(
56-
EXECUTION_POSITION_DB_KEY.as_slice(),
57-
&alloy::rlp::encode(self)[..]
56+
EXECUTION_POSITION_DB_KEY.as_slice(),
57+
&alloy::rlp::encode(self)[..],
5858
)?;
5959
}
6060
txn.commit()?;
6161
Ok(())
6262
}
63-
6463
}
6564

6665
impl Default for ExecutionPosition {
@@ -71,4 +70,4 @@ impl Default for ExecutionPosition {
7170
state_root: EMPTY_ROOT_HASH,
7271
}
7372
}
74-
}
73+
}

bin/trin-execution/src/storage/trie_db.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl DB for TrieReDB {
2626
fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Self::Error> {
2727
let txn = self.storage.begin_read()?;
2828
let table = txn.open_table(TABLE)?;
29-
Ok(table.get(key)?.map(|val| val.value().to_vec()))
29+
Ok(table.get(key)?.map(|val| val.value().to_vec()))
3030
}
3131

3232
fn insert(&self, key: &[u8], value: Vec<u8>) -> Result<(), Self::Error> {
@@ -54,4 +54,4 @@ impl DB for TrieReDB {
5454
fn flush(&self) -> Result<(), Self::Error> {
5555
Ok(())
5656
}
57-
}
57+
}

bin/trin-execution/src/storage/utils.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::path::Path;
33
use redb::{Database as ReDB, Error};
44
use tracing::info;
55

6-
76
/// Helper function for opening a ReDB database at the specified path.
87
pub fn setup_redb(path: &Path) -> Result<ReDB, Error> {
98
let redb_path = path.join("redb");
@@ -16,4 +15,4 @@ pub fn setup_redb(path: &Path) -> Result<ReDB, Error> {
1615
};
1716

1817
Ok(db)
19-
}
18+
}

bin/trin-execution/src/subcommands/e2ss/export.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::{
2323
e2hs::manager::E2HSManager,
2424
storage::{
2525
account_db::AccountDB, evm_db::EvmDB, execution_position::ExecutionPosition,
26-
utils::setup_redb
26+
utils::setup_redb,
2727
},
2828
subcommands::e2ss::utils::percentage_from_address_hash,
2929
};
@@ -86,7 +86,8 @@ impl StateExporter {
8686
let bytecode = if account_state.code_hash != KECCAK_EMPTY {
8787
let txn = self.evm_db.db.begin_read()?;
8888
let table = txn.open_table(CONTRACTS_TABLE)?;
89-
table.get(account_state.code_hash.as_slice())?
89+
table
90+
.get(account_state.code_hash.as_slice())?
9091
.map(|val| val.value().to_vec())
9192
.expect("If code hash is not empty, code must be present")
9293
} else {

bin/trin-execution/src/subcommands/e2ss/import.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl StateImporter {
102102
account_state.code_hash == keccak256(&bytecode),
103103
"Code hash mismatch, .e2ss import failed"
104104
);
105-
105+
106106
let db = &self.evm_db.db;
107107
let txn = db.begin_write()?;
108108

@@ -111,14 +111,17 @@ impl StateImporter {
111111
let mut contracts = txn.open_table(CONTRACTS_TABLE)?;
112112

113113
if !bytecode.is_empty() {
114-
contracts.insert(keccak256(&bytecode).as_slice(), bytecode.as_slice())?;
114+
contracts.insert(keccak256(&bytecode).as_slice(), bytecode.as_slice())?;
115115
}
116116

117117
// Insert account into accounts table
118-
accounts.insert(address_hash.as_slice(), alloy::rlp::encode(&account_state).as_slice(),)?;
118+
accounts.insert(
119+
address_hash.as_slice(),
120+
alloy::rlp::encode(&account_state).as_slice(),
121+
)?;
119122
}
120123
txn.commit()?;
121-
124+
122125
self.evm_db
123126
.trie
124127
.lock()
@@ -127,7 +130,12 @@ impl StateImporter {
127130
let txn = self.evm_db.db.begin_write()?;
128131
{
129132
let mut accounts = txn.open_table(ACCOUNTS_TABLE)?;
130-
accounts.insert(address_hash.as_slice(), alloy::rlp::encode(account_state).as_slice()).expect("Inserting account should never fail");
133+
accounts
134+
.insert(
135+
address_hash.as_slice(),
136+
alloy::rlp::encode(account_state).as_slice(),
137+
)
138+
.expect("Inserting account should never fail");
131139
}
132140
txn.commit()?;
133141

@@ -172,7 +180,10 @@ impl StateImporter {
172180

173181
while era_manager.next_block_number() <= block_number {
174182
let block = era_manager.get_next_block().await?;
175-
table.insert(keccak256(B256::from(U256::from(block.header.number))).as_slice(), block.header.hash_slow().as_slice())?;
183+
table.insert(
184+
keccak256(B256::from(U256::from(block.header.number))).as_slice(),
185+
block.header.hash_slow().as_slice(),
186+
)?;
176187
}
177188
>>>>>>> 7c448cc7 (Replace RocksDB with Redb as backing for EVM database)
178189
}

0 commit comments

Comments
 (0)