Skip to content

Commit

Permalink
Fix truncated hex in anvil dump_state (#8216)
Browse files Browse the repository at this point in the history
* Fix truncated hex in anvil dump_state

* Change type of key

* fixs

* rustfmt

* fix

---------

Co-authored-by: unknown <[email protected]>
Co-authored-by: Matthias Seitz <[email protected]>
Co-authored-by: Yash Atreya <[email protected]>
  • Loading branch information
4 people authored Sep 13, 2024
1 parent 6c0a15b commit 84e63fe
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
8 changes: 4 additions & 4 deletions crates/anvil/src/eth/backend/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub trait Db:
}

/// Sets the balance of the given address
fn set_storage_at(&mut self, address: Address, slot: U256, val: U256) -> DatabaseResult<()>;
fn set_storage_at(&mut self, address: Address, slot: B256, val: B256) -> DatabaseResult<()>;

/// inserts a blockhash for the given number
fn insert_block_hash(&mut self, number: U256, hash: B256);
Expand Down Expand Up @@ -184,8 +184,8 @@ impl<T: DatabaseRef<Error = DatabaseError> + Send + Sync + Clone + fmt::Debug> D
self.insert_account_info(address, account)
}

fn set_storage_at(&mut self, address: Address, slot: U256, val: U256) -> DatabaseResult<()> {
self.insert_account_storage(address, slot, val)
fn set_storage_at(&mut self, address: Address, slot: B256, val: B256) -> DatabaseResult<()> {
self.insert_account_storage(address, slot.into(), val.into())
}

fn insert_block_hash(&mut self, number: U256, hash: B256) {
Expand Down Expand Up @@ -356,7 +356,7 @@ pub struct SerializableAccountRecord {
pub nonce: u64,
pub balance: U256,
pub code: Bytes,
pub storage: BTreeMap<U256, U256>,
pub storage: BTreeMap<B256, B256>,
}

/// Defines a backwards-compatible enum for transactions.
Expand Down
2 changes: 1 addition & 1 deletion crates/anvil/src/eth/backend/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl GenesisConfig {
db.insert_account(addr, self.genesis_to_account_info(&acc));
// insert all storage values
for (k, v) in storage.unwrap_or_default().iter() {
db.set_storage_at(addr, U256::from_be_bytes(k.0), U256::from_be_bytes(v.0))?;
db.set_storage_at(addr, *k, *v)?;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/anvil/src/eth/backend/mem/fork_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Db for ForkedDatabase {
self.database_mut().insert_account(address, account)
}

fn set_storage_at(&mut self, address: Address, slot: U256, val: U256) -> DatabaseResult<()> {
fn set_storage_at(&mut self, address: Address, slot: B256, val: B256) -> DatabaseResult<()> {
// this ensures the account is loaded first
let _ = Database::basic(self, address)?;
self.database_mut().set_storage_at(address, slot, val)
Expand Down Expand Up @@ -57,7 +57,7 @@ impl Db for ForkedDatabase {
nonce: v.info.nonce,
balance: v.info.balance,
code: code.original_bytes(),
storage: v.storage.into_iter().collect(),
storage: v.storage.into_iter().map(|(k, v)| (k.into(), v.into())).collect(),
},
))
})
Expand Down
16 changes: 9 additions & 7 deletions crates/anvil/src/eth/backend/mem/in_memory_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ impl Db for MemDb {
self.inner.insert_account_info(address, account)
}

fn set_storage_at(&mut self, address: Address, slot: U256, val: U256) -> DatabaseResult<()> {
self.inner.insert_account_storage(address, slot, val)
fn set_storage_at(&mut self, address: Address, slot: B256, val: B256) -> DatabaseResult<()> {
self.inner.insert_account_storage(address, slot.into(), val.into())
}

fn insert_block_hash(&mut self, number: U256, hash: B256) {
Expand Down Expand Up @@ -56,7 +56,7 @@ impl Db for MemDb {
nonce: v.info.nonce,
balance: v.info.balance,
code: code.original_bytes(),
storage: v.storage.into_iter().collect(),
storage: v.storage.into_iter().map(|(k, v)| (k.into(), v.into())).collect(),
},
))
})
Expand Down Expand Up @@ -159,7 +159,9 @@ mod tests {
nonce: 1234,
},
);
dump_db.set_storage_at(test_addr, U256::from(1234567), U256::from(1)).unwrap();
dump_db
.set_storage_at(test_addr, U256::from(1234567).into(), U256::from(1).into())
.unwrap();

// blocks dumping/loading tested in storage.rs
let state = dump_db
Expand Down Expand Up @@ -202,8 +204,8 @@ mod tests {
},
);

db.set_storage_at(test_addr, U256::from(1234567), U256::from(1)).unwrap();
db.set_storage_at(test_addr, U256::from(1234568), U256::from(2)).unwrap();
db.set_storage_at(test_addr, U256::from(1234567).into(), U256::from(1).into()).unwrap();
db.set_storage_at(test_addr, U256::from(1234568).into(), U256::from(2).into()).unwrap();

let mut new_state = SerializableState::default();

Expand All @@ -218,7 +220,7 @@ mod tests {
);

let mut new_storage = BTreeMap::default();
new_storage.insert(U256::from(1234568), U256::from(5));
new_storage.insert(U256::from(1234568).into(), U256::from(5).into());

new_state.accounts.insert(
test_addr,
Expand Down
4 changes: 2 additions & 2 deletions crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ impl Backend {
slot: U256,
val: B256,
) -> DatabaseResult<()> {
self.db.write().await.set_storage_at(address, slot, U256::from_be_bytes(val.0))
self.db.write().await.set_storage_at(address, slot.into(), val)
}

/// Returns the configured specid
Expand Down Expand Up @@ -2498,7 +2498,7 @@ impl Backend {
self.db.write().await.clear();
for (address, acc) in common_state {
for (key, value) in acc.storage {
self.db.write().await.set_storage_at(address, key, value)?;
self.db.write().await.set_storage_at(address, key.into(), value.into())?;
}
self.db.write().await.insert_account(address, acc.info);
}
Expand Down

0 comments on commit 84e63fe

Please sign in to comment.