Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct wrong endianness #82

Merged
merged 4 commits into from
Apr 15, 2024
Merged
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
25 changes: 13 additions & 12 deletions src/processor/snapshot/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,30 @@ impl SnapshotDB {
Ok(Self(db))
}

pub fn process_value(&self, key: U256, value: PackingType) -> H256 {
pub fn process_value(&self, key: U256, value: PackingType) -> Result<H256> {
let processed_value = match value {
PackingType::NoCompression(v) | PackingType::Transform(v) => v,
PackingType::Add(_) | PackingType::Sub(_) => {
let mut buffer = [0; 32];
key.to_little_endian(&mut buffer);
if let Ok(Some(log)) = self.get_storage_log(&buffer) {
let existing_value = U256::from(log.value.to_fixed_bytes());
// NOTE: We're explicitly allowing over-/underflow as per the spec.
match value {
PackingType::Add(v) => existing_value.overflowing_add(v).0,
PackingType::Sub(v) => existing_value.overflowing_sub(v).0,
_ => unreachable!(),
}
key.to_big_endian(&mut buffer);
let existing_value = if let Some(log) = self.get_storage_log(&buffer)? {
U256::from(log.value.to_fixed_bytes())
} else {
panic!("no key found for version")
U256::from(0)
};

// NOTE: We're explicitly allowing over-/underflow as per the spec.
match value {
PackingType::Add(v) => existing_value.overflowing_add(v).0,
PackingType::Sub(v) => existing_value.overflowing_sub(v).0,
_ => unreachable!(),
}
}
};

let mut buffer = [0; 32];
processed_value.to_big_endian(&mut buffer);
H256::from(buffer)
Ok(H256::from(buffer))
}

pub fn new_read_only(db_path: PathBuf) -> Result<Self> {
Expand Down
71 changes: 68 additions & 3 deletions src/processor/snapshot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,16 @@ impl Processor for SnapshotBuilder {
while let Some(block) = rx.recv().await {
// Initial calldata.
for (key, value) in &block.initial_storage_changes {
let key = U256::from_little_endian(key);
let value = self
.database
.process_value(key, *value)
.expect("failed to get key from database");

self.database
.insert_storage_log(&mut SnapshotStorageLog {
key: U256::from_little_endian(key),
value: self.database.process_value(U256::from(key), *value),
key,
value,
miniblock_number_of_initial_write: U64::from(0),
l1_batch_number_of_initial_write: U64::from(
block.l1_block_number.unwrap_or(0),
Expand All @@ -86,7 +92,10 @@ impl Processor for SnapshotBuilder {
.database
.get_key_from_index(index as u64)
.expect("missing key");
let value = self.database.process_value(U256::from(&key[0..32]), *value);
let value = self
.database
.process_value(U256::from_big_endian(&key[0..32]), *value)
.expect("failed to get key from database");

if self
.database
Expand Down Expand Up @@ -230,3 +239,59 @@ fn derive_final_address_for_params(address: &Address, key: &U256) -> [u8; 32] {

result
}

#[cfg(test)]
mod tests {
use std::fs;

use indexmap::IndexMap;
use state_reconstruct_fetcher::types::PackingType;

use super::*;

#[tokio::test]
async fn simple() {
let db_dir = "./test_db".to_string();
let _ = fs::remove_dir_all(db_dir.clone());

{
let builder = SnapshotBuilder::new(Some(db_dir.clone()));
let (tx, rx) = mpsc::channel::<CommitBlock>(5);

tokio::spawn(async move {
let key = U256::from_dec_str("1234").unwrap();
let mut key1 = [0u8; 32];
key.to_little_endian(&mut key1);
let val1 = U256::from_dec_str("5678").unwrap();
let mut initial_storage_changes = IndexMap::new();
initial_storage_changes.insert(key1, PackingType::NoCompression(val1));
let repeated_storage_changes = IndexMap::new();
let cb = CommitBlock {
l1_block_number: Some(1),
l2_block_number: 2,
index_repeated_storage_changes: 0,
new_state_root: Vec::new(),
initial_storage_changes,
repeated_storage_changes,
factory_deps: Vec::new(),
};
tx.send(cb).await.unwrap();
});

builder.run(rx).await;
}

let db = SnapshotDB::new(PathBuf::from(db_dir.clone())).unwrap();

let key = U256::from_dec_str("1234").unwrap();
let mut key1 = [0u8; 32];
key.to_big_endian(&mut key1);
let Some(sl1) = db.get_storage_log(&key1).unwrap() else {
panic!("key1 not found")
};

assert_eq!(sl1.key, key1.into());

fs::remove_dir_all(db_dir).unwrap();
}
}