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
13 changes: 13 additions & 0 deletions crates/validator-core/src/data_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ impl PlainKey {
}
}

/// Encodes an account address into a stack-allocated 20-byte array.
pub fn encode_account(addr: Address) -> [u8; ACCOUNT_ADDRESS_LEN] {
addr.into_array()
}

/// Encodes an address + storage slot into a stack-allocated 52-byte array.
pub fn encode_storage(addr: Address, slot: B256) -> [u8; STORAGE_SLOT_KEY_LEN] {
let mut buf = [0u8; STORAGE_SLOT_KEY_LEN];
buf[..ACCOUNT_ADDRESS_LEN].copy_from_slice(addr.as_slice());
buf[ACCOUNT_ADDRESS_LEN..].copy_from_slice(slot.as_slice());
buf
}

/// Decodes a byte slice into a PlainKey.
///
/// Returns `PlainKey::Unknown` if the buffer length is neither 20 (account)
Expand Down
8 changes: 4 additions & 4 deletions crates/validator-core/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ where
fn basic_ref(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
trace!(?address, "basic_ref");

let raw_value = self.plain_value(&PlainKey::Account(address).encode())?;
let raw_value = self.plain_value(&PlainKey::encode_account(address))?;

match raw_value.and_then(|v| match PlainValue::decode(&v) {
PlainValue::Account(acc) => Some(acc),
Expand Down Expand Up @@ -130,7 +130,7 @@ where
fn storage_ref(&self, address: Address, index: U256) -> Result<U256, Self::Error> {
trace!(?address, index = %format_args!("{:#x}", index), "storage_ref");

let raw_value = self.plain_value(&PlainKey::Storage(address, index.into()).encode())?;
let raw_value = self.plain_value(&PlainKey::encode_storage(address, index.into()))?;

Ok(raw_value
.and_then(|v| match PlainValue::decode(&v) {
Expand Down Expand Up @@ -292,11 +292,11 @@ impl SaltEnv for WitnessExternalEnv {
}

fn bucket_id_for_account(account: Address) -> BucketId {
hasher::bucket_id(&PlainKey::Account(account).encode())
hasher::bucket_id(&PlainKey::encode_account(account))
}

fn bucket_id_for_slot(address: Address, key: U256) -> BucketId {
hasher::bucket_id(&PlainKey::Storage(address, key.into()).encode())
hasher::bucket_id(&PlainKey::encode_storage(address, key.into()))
}
}

Expand Down