diff --git a/assembler/src/test_data_generator.rs b/assembler/src/test_data_generator.rs index 967b8524..1ba3b424 100644 --- a/assembler/src/test_data_generator.rs +++ b/assembler/src/test_data_generator.rs @@ -4,6 +4,7 @@ mod tests { use crate::relocate::{asm_relocate, AsmBundle}; use log::LevelFilter; use std::fs; + use std::path::Path; #[test] fn generate_fib() { @@ -227,6 +228,13 @@ mod tests { generate_from_file( "system/ContractDeployer_asm.json".to_string(), "ContractDeployer.json".to_string(), + ) + } + #[test] + fn generate_sys() { + generate_all_dir( + "test_data/asm/sys".to_string(), + "test_data/bin/sys".to_string(), ); } @@ -253,4 +261,34 @@ mod tests { fs::write(output_path, pretty).unwrap(); println!("{}", program.bytecode); } + + fn generate_all_dir(input_dir: String, output_dir: String) { + let out_dir = Path::new(&output_dir); + let _ = fs::create_dir_all(out_dir).unwrap(); + let in_dir = Path::new(input_dir.as_str()); + + for entry in fs::read_dir(in_dir).unwrap() { + let entry = entry.unwrap(); + let path = entry.path(); + if path.is_file() { + let json_str = std::fs::read_to_string(&path.as_path()).unwrap(); + let bundle: AsmBundle = serde_json::from_str(json_str.as_str()).unwrap(); + let relocated = asm_relocate(bundle).unwrap(); + let program = encode_to_binary(relocated).unwrap(); + let out_file_name = path + .file_name() + .unwrap() + .to_str() + .unwrap() + .replace("_asm", ""); + let output_path = format!("{}/{}", out_dir.display(), out_file_name); + let pretty = serde_json::to_string(&program).unwrap(); + fs::write(output_path, pretty).unwrap(); + println!( + "finish compile {}", + path.file_name().unwrap().to_str().unwrap() + ); + } + } + } } diff --git a/core/src/program/binary_program.rs b/core/src/program/binary_program.rs index 1a0c376b..b81156ae 100644 --- a/core/src/program/binary_program.rs +++ b/core/src/program/binary_program.rs @@ -60,7 +60,13 @@ impl BinaryProgram { pub fn bytecode_u64_array(&self) -> Result, ParseIntError> { let bytecodes: Vec<&str> = self.bytecode.split('\n').collect(); - bytecodes.iter().map(|&c| c.parse::()).collect() + bytecodes + .iter() + .map(|&c| { + let instruction_without_prefix = c.trim_start_matches("0x"); + u64::from_str_radix(instruction_without_prefix, 16) + }) + .collect() } } diff --git a/core/src/state/mod.rs b/core/src/state/mod.rs index 9ac90269..4ccc569b 100644 --- a/core/src/state/mod.rs +++ b/core/src/state/mod.rs @@ -11,6 +11,7 @@ use plonky2::field::goldilocks_field::GoldilocksField; pub mod contracts; pub mod error; pub mod state_storage; +pub mod utils; #[derive(Debug)] pub struct NodeState { diff --git a/core/src/state/state_storage.rs b/core/src/state/state_storage.rs index a1fa0eaa..42201050 100644 --- a/core/src/state/state_storage.rs +++ b/core/src/state/state_storage.rs @@ -5,6 +5,8 @@ use crate::types::storage::{field_arr_to_u8_arr, u8_arr_to_field_arr}; use plonky2::field::goldilocks_field::GoldilocksField; use rocksdb::WriteBatch; +use super::utils::get_prog_hash_cf_key_from_contract_addr; + #[derive(Debug)] pub struct StateStorage { pub db: RocksDB, @@ -132,8 +134,8 @@ impl StateStorage { pub fn get_contract_map(&self, contract_addr: &TreeValue) -> Result, StateError> { let cf = self .db - .cf_sequencer_handle(SequencerColumnFamily::ContractMap); - let addr_key = tree_key_to_u8_arr(contract_addr); + .cf_sequencer_handle(SequencerColumnFamily::State); + let addr_key = get_prog_hash_cf_key_from_contract_addr(contract_addr); let res = self.db.get_cf(cf, addr_key); if let Ok(code) = res { diff --git a/core/src/state/utils.rs b/core/src/state/utils.rs new file mode 100644 index 00000000..4186ad68 --- /dev/null +++ b/core/src/state/utils.rs @@ -0,0 +1,26 @@ +use plonky2::{field::types::PrimeField64, hash::utils::poseidon_hash_bytes}; +use web3::types::{H256, U256}; + +use crate::types::merkle_tree::TreeKey; + +pub fn get_prog_hash_cf_key_from_contract_addr(contract_addr: &TreeKey) -> [u8; 32] { + let address = H256([ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x80, 0x02, + ]); + + let slot: [u8; 32] = contract_addr + .iter() + .flat_map(|&field| field.to_canonical_u64().to_be_bytes().to_vec()) + .collect::>() + .try_into() + .unwrap(); + let key = [[0; 32], slot].concat(); + let key = H256(poseidon_hash_bytes(&key)); + + let mut bytes = [0_u8; 64]; + bytes[0..32].copy_from_slice(&address.0); + U256::from(key.to_fixed_bytes()).to_big_endian(&mut bytes[32..64]); + poseidon_hash_bytes(&bytes) +}