Skip to content
Merged
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
45 changes: 20 additions & 25 deletions crates/staged-sync/src/utils/init.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use reth_db::{
cursor::{DbCursorRO, DbCursorRW},
cursor::DbCursorRO,
database::{Database, DatabaseGAT},
mdbx::{Env, WriteMap},
tables,
transaction::{DbTx, DbTxMut},
};
use reth_primitives::{keccak256, Account, Bytecode, ChainSpec, StorageEntry, H256};
use reth_provider::{Transaction, TransactionError};
use reth_primitives::{Account, Bytecode, ChainSpec, H256, U256};
use reth_provider::{PostState, Transaction, TransactionError};
use reth_stages::StageKind;
use std::{path::Path, sync::Arc};
use tracing::debug;
Expand Down Expand Up @@ -65,7 +65,6 @@ pub fn init_genesis<DB: Database>(
drop(tx);
debug!("Writing genesis block.");
let tx = db.tx_mut()?;
insert_genesis_state::<DB>(&tx, genesis)?;

// use transaction to insert genesis header
let transaction = Transaction::new_raw(&db, tx);
Expand All @@ -79,6 +78,8 @@ pub fn init_genesis<DB: Database>(
tx.put::<tables::HeaderTD>(0, header.difficulty.into())?;
tx.put::<tables::Headers>(0, header)?;

insert_genesis_state::<DB>(&tx, genesis)?;

// insert sync stage
for stage in StageKind::ALL.iter() {
tx.put::<tables::SyncStage>(stage.to_string(), 0)?;
Expand All @@ -93,38 +94,32 @@ pub fn insert_genesis_state<DB: Database>(
tx: &<DB as DatabaseGAT<'_>>::TXMut,
genesis: &reth_primitives::Genesis,
) -> Result<(), InitDatabaseError> {
let mut account_cursor = tx.cursor_write::<tables::PlainAccountState>()?;
let mut storage_cursor = tx.cursor_write::<tables::PlainStorageState>()?;
let mut bytecode_cursor = tx.cursor_write::<tables::Bytecodes>()?;
let mut state = PostState::default();

// Insert account state
for (address, account) in &genesis.alloc {
let mut bytecode_hash = None;
// insert bytecode hash
if let Some(code) = &account.code {
let hash = keccak256(code.as_ref());
bytecode_cursor.upsert(hash, Bytecode::new_raw_with_hash(code.0.clone(), hash))?;
bytecode_hash = Some(hash);
let bytecode = Bytecode::new_raw(code.0.clone());
// FIXME: Can bytecode_hash be Some(Bytes::new()) here?
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, if they have bytecode the hash must match since that is how we load bytecode from the database. otherwise the account would have no bytecode associated with it

Suggested change
// FIXME: Can bytecode_hash be Some(Bytes::new()) here?

bytecode_hash = Some(bytecode.hash);
state.add_bytecode(bytecode.hash, bytecode);
}
// insert plain account.
account_cursor.upsert(
state.create_account(
0,
*address,
Account {
nonce: account.nonce.unwrap_or_default(),
balance: account.balance,
bytecode_hash,
},
)?;
// insert plain storages
Account { nonce: account.nonce.unwrap_or(0), balance: account.balance, bytecode_hash },
);
if let Some(storage) = &account.storage {
let mut storage_changes = reth_provider::post_state::StorageChangeset::new();
for (&key, &value) in storage {
if value.is_zero() {
continue
}
storage_cursor.upsert(*address, StorageEntry { key, value: value.into() })?
storage_changes
.insert(U256::from_be_bytes(key.0), (U256::ZERO, U256::from_be_bytes(value.0)));
}
state.change_storage(0, *address, storage_changes);
}
}
state.write_to_db(tx)?;

Ok(())
}

Expand Down