Skip to content

Commit

Permalink
Merge branch 'main' into dev/pierre
Browse files Browse the repository at this point in the history
  • Loading branch information
web3Softcloud committed Jan 10, 2024
2 parents 45ece78 + 7af81e6 commit f119612
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 3 deletions.
38 changes: 38 additions & 0 deletions assembler/src/test_data_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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(),
);
}

Expand All @@ -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()
);
}
}
}
}
8 changes: 7 additions & 1 deletion core/src/program/binary_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,13 @@ impl BinaryProgram {

pub fn bytecode_u64_array(&self) -> Result<Vec<u64>, ParseIntError> {
let bytecodes: Vec<&str> = self.bytecode.split('\n').collect();
bytecodes.iter().map(|&c| c.parse::<u64>()).collect()
bytecodes
.iter()
.map(|&c| {
let instruction_without_prefix = c.trim_start_matches("0x");
u64::from_str_radix(instruction_without_prefix, 16)
})
.collect()
}
}

Expand Down
1 change: 1 addition & 0 deletions core/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<H> {
Expand Down
6 changes: 4 additions & 2 deletions core/src/state/state_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -132,8 +134,8 @@ impl StateStorage {
pub fn get_contract_map(&self, contract_addr: &TreeValue) -> Result<Vec<u8>, 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 {
Expand Down
26 changes: 26 additions & 0 deletions core/src/state/utils.rs
Original file line number Diff line number Diff line change
@@ -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::<Vec<u8>>()
.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)
}

0 comments on commit f119612

Please sign in to comment.