Skip to content

Commit

Permalink
DEL: tx_idx
Browse files Browse the repository at this point in the history
hongyuanyang-uu committed Dec 13, 2023
1 parent dcddeb3 commit ee04692
Showing 10 changed files with 34 additions and 77 deletions.
31 changes: 31 additions & 0 deletions core/src/storage/db.rs
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@ pub struct RocksDB {
pub enum Database {
MerkleTree,
StateKeeper,
Sequencer,
}

#[derive(Debug)]
@@ -43,6 +44,13 @@ pub enum StateKeeperColumnFamily {
FactoryDeps,
}

#[derive(Debug, Clone, Copy)]
enum SequencerColumnFamily {
State,
Contracts,
FactoryDeps,
}

impl MerkleTreeColumnFamily {
fn all() -> &'static [Self] {
&[Self::Tree, Self::LeafIndices]
@@ -62,6 +70,12 @@ impl StateKeeperColumnFamily {
}
}

impl SequencerColumnFamily {
fn all() -> &'static [Self] {
&[Self::State, Self::Contracts, Self::FactoryDeps]
}
}

impl std::fmt::Display for MerkleTreeColumnFamily {
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
let value = match self {
@@ -86,6 +100,17 @@ impl std::fmt::Display for StateKeeperColumnFamily {
}
}

impl std::fmt::Display for SequencerColumnFamily {
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
let value = match self {
Self::State => "state",
Self::Contracts => "contracts",
Self::FactoryDeps => "factory_deps",
};
write!(formatter, "{}", value)
}
}

impl RocksDB {
pub fn new<P: AsRef<Path>>(database: Database, path: P, tune_options: bool) -> Self {
let options = Self::rocksdb_options(tune_options);
@@ -102,6 +127,12 @@ impl RocksDB {
});
DB::open_cf_descriptors(&options, path, cfs).expect("failed to init rocksdb")
}
Database::Sequencer => {
let cfs = SequencerColumnFamily::all().iter().map(|cf| {
ColumnFamilyDescriptor::new(cf.to_string(), Self::rocksdb_options(tune_options))
});
DB::open_cf_descriptors(&options, path, cfs).expect("failed to init rocksdb")
}
};

Self {
14 changes: 0 additions & 14 deletions core/src/trace/trace.rs
Original file line number Diff line number Diff line change
@@ -65,7 +65,6 @@ pub enum ComparisonOperation {

#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub struct MemoryTraceCell {
pub tx_idx: GoldilocksField,
pub env_idx: GoldilocksField,
pub addr: GoldilocksField,
pub clk: GoldilocksField,
@@ -108,7 +107,6 @@ pub struct BuiltinSelector {

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Step {
pub tx_idx: GoldilocksField,
pub env_idx: GoldilocksField,
pub call_sc_cnt: GoldilocksField,
pub clk: u32,
@@ -180,7 +178,6 @@ pub struct CmpRow {

#[derive(Debug, Copy, Clone, Default, Serialize, Deserialize)]
pub struct PoseidonChunkRow {
pub tx_idx: GoldilocksField,
pub env_idx: GoldilocksField,
pub clk: u32,
pub opcode: GoldilocksField,
@@ -270,7 +267,6 @@ pub struct HashTrace(

#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub struct StorageRow {
pub tx_idx: GoldilocksField,
pub env_idx: GoldilocksField,
pub clk: u32,
pub diff_clk: u32,
@@ -300,7 +296,6 @@ pub struct StorageHashRow {

#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub struct TapeRow {
pub tx_idx: GoldilocksField,
pub is_init: bool,
pub opcode: GoldilocksField,
pub addr: GoldilocksField,
@@ -310,7 +305,6 @@ pub struct TapeRow {

#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub struct SCCallRow {
pub tx_idx: GoldilocksField,
pub caller_env_idx: GoldilocksField,
pub addr_storage: Address,
pub addr_code: Address,
@@ -446,7 +440,6 @@ impl Trace {
ext_cnt: GoldilocksField,
filter_tape_looking: GoldilocksField,
addr_code: Address,
tx_idx: GoldilocksField,
env_idx: GoldilocksField,
call_sc_cnt: GoldilocksField,
storage_access_idx: GoldilocksField,
@@ -466,7 +459,6 @@ impl Trace {
ext_cnt,
filter_tape_looking,
addr_code,
tx_idx,
env_idx,
call_sc_cnt,
storage_access_idx,
@@ -482,7 +474,6 @@ impl Trace {
root: [GoldilocksField; 4],
addr: [GoldilocksField; 4],
value: [GoldilocksField; 4],
tx_idx: GoldilocksField,
env_idx: GoldilocksField,
) {
self.builtin_storage.push(StorageRow {
@@ -492,14 +483,12 @@ impl Trace {
root,
addr,
value,
tx_idx,
env_idx,
});
}

pub fn insert_sccall(
&mut self,
tx_idx: GoldilocksField,
caller_env_idx: GoldilocksField,
addr_storage: Address,
addr_code: Address,
@@ -511,7 +500,6 @@ impl Trace {
clk_callee_end: GoldilocksField,
) {
self.sc_call.push(SCCallRow {
tx_idx,
caller_env_idx,
addr_storage,
addr_code,
@@ -526,7 +514,6 @@ impl Trace {

pub fn insert_poseidon_chunk(
&mut self,
tx_idx: GoldilocksField,
env_idx: GoldilocksField,
clk: u32,
opcode: GoldilocksField,
@@ -540,7 +527,6 @@ impl Trace {
is_ext_line: GoldilocksField,
) {
self.builtin_poseidon_chunk.push(PoseidonChunkRow {
tx_idx,
env_idx,
clk,
opcode,
5 changes: 0 additions & 5 deletions core/src/vm/memory.rs
Original file line number Diff line number Diff line change
@@ -11,7 +11,6 @@ pub const HP_START_ADDR: u64 = GoldilocksField::ORDER - 2 * MEM_SPAN_SIZE;

#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub struct MemoryCell {
pub tx_idx: GoldilocksField,
pub env_idx: GoldilocksField,
pub clk: u32,
pub is_rw: GoldilocksField,
@@ -41,7 +40,6 @@ impl MemoryTree {
filter_looked_for_main: GoldilocksField,
region_prophet: GoldilocksField,
region_heap: GoldilocksField,
tx_idx: GoldilocksField,
env_idx: GoldilocksField,
) -> Result<GoldilocksField, ProcessorError> {
// look up the previous value in the appropriate address trace and add (clk,
@@ -52,7 +50,6 @@ impl MemoryTree {
if let Some(mem_data) = read_mem_res {
let last_value = mem_data.last().expect("empty address trace").value;
let new_value = MemoryCell {
tx_idx,
env_idx,
is_rw,
clk,
@@ -81,7 +78,6 @@ impl MemoryTree {
region_prophet: GoldilocksField,
region_heap: GoldilocksField,
value: GoldilocksField,
tx_idx: GoldilocksField,
env_idx: GoldilocksField,
) {
// add a memory access to the appropriate address trace; if this is the first
@@ -95,7 +91,6 @@ impl MemoryTree {
region_prophet,
region_heap,
value,
tx_idx,
env_idx,
};
self.trace
24 changes: 0 additions & 24 deletions executor/src/lib.rs
Original file line number Diff line number Diff line change
@@ -99,7 +99,6 @@ macro_rules! memory_op {
GoldilocksField::from_canonical_u64(FilterLockForMain::True as u64),
region_prophet,
region_heap,
$v.tx_idx,
$v.env_idx,
)?;
};
@@ -118,7 +117,6 @@ macro_rules! memory_op {
region_prophet,
region_heap,
$value,
$v.tx_idx,
$v.env_idx,
);
};
@@ -142,7 +140,6 @@ macro_rules! aux_insert {
ext_cnt: $ext_cnt,
filter_tape_looking: $filter_tape_looking,
addr_code: $ctx_code_regs_status,
tx_idx: $v.tx_idx,
env_idx: $v.env_idx,
call_sc_cnt: $v.call_sc_cnt,
storage_access_idx: $v.storage_access_idx,
@@ -193,7 +190,6 @@ enum MemRangeType {
}
#[derive(Debug)]
pub struct Process {
pub tx_idx: GoldilocksField,
pub env_idx: GoldilocksField,
pub call_sc_cnt: GoldilocksField,
pub clk: u32,
@@ -221,7 +217,6 @@ pub struct Process {
impl Process {
pub fn new() -> Self {
Self {
tx_idx: Default::default(),
env_idx: Default::default(),
call_sc_cnt: Default::default(),
clk: 0,
@@ -311,7 +306,6 @@ impl Process {
GoldilocksField::from_canonical_u64(FilterLockForMain::False as u64),
GoldilocksField::ZERO,
GoldilocksField::ZERO,
self.tx_idx,
self.env_idx,
)?
.to_canonical_u64();
@@ -329,7 +323,6 @@ impl Process {
GoldilocksField::from_canonical_u64(FilterLockForMain::False as u64),
GoldilocksField::ZERO,
GoldilocksField::ZERO,
self.tx_idx,
self.env_idx,
)?
.to_canonical_u64();
@@ -389,7 +382,6 @@ impl Process {
GoldilocksField::from_canonical_u64(1_u64),
GoldilocksField::from_canonical_u64(0_u64),
GoldilocksField(value.get_number() as u64),
self.tx_idx,
self.env_idx,
);
self.psp += GoldilocksField::ONE;
@@ -1104,7 +1096,6 @@ impl Process {
GoldilocksField::ZERO,
GoldilocksField::ZERO,
ctx_code_regs_status.clone(),
self.tx_idx,
self.env_idx,
self.call_sc_cnt,
self.storage_access_idx,
@@ -1115,7 +1106,6 @@ impl Process {
self.register_selector.aux1 = GoldilocksField::from_canonical_u64(self.clk as u64);
let register_selector = self.register_selector.clone();
end_step = Some(Step {
tx_idx: self.tx_idx,
env_idx: GoldilocksField::default(),
call_sc_cnt: self.call_sc_cnt,
tp: self.tp,
@@ -1194,7 +1184,6 @@ impl Process {
tree_key,
store_value,
tree_key_default(),
self.tx_idx,
self.env_idx,
);
self.storage_access_idx += GoldilocksField::ONE;
@@ -1326,7 +1315,6 @@ impl Process {
tree_key,
tree_key_default(),
read_value,
self.tx_idx,
self.env_idx,
);

@@ -1419,7 +1407,6 @@ impl Process {
let mut hash_input_value = [GoldilocksField::ZERO; POSEIDON_INPUT_VALUE_LEN];
if !program.pre_exe_flag {
program.trace.insert_poseidon_chunk(
self.tx_idx,
self.env_idx,
self.clk,
self.opcode,
@@ -1453,7 +1440,6 @@ impl Process {
hash_input_value.clone_from_slice(&input[0..POSEIDON_INPUT_VALUE_LEN]);
hash_cap.clone_from_slice(&hash_pre[POSEIDON_INPUT_VALUE_LEN..]);
program.trace.insert_poseidon_chunk(
self.tx_idx,
self.env_idx,
self.clk,
self.opcode,
@@ -1495,7 +1481,6 @@ impl Process {
hash_input_value.clone_from_slice(&input[0..POSEIDON_INPUT_VALUE_LEN]);
hash_cap.clone_from_slice(&hash_pre[POSEIDON_INPUT_VALUE_LEN..]);
program.trace.insert_poseidon_chunk(
self.tx_idx,
self.env_idx,
self.clk,
self.opcode,
@@ -1587,7 +1572,6 @@ impl Process {
let registers_status = registers_status.clone();
tape_copy!(self,
let value = self.tape.read(
self.tx_idx,
tape_addr,
self.clk,
GoldilocksField::from_canonical_u64(1 << Opcode::TLOAD as u64),
@@ -1603,7 +1587,6 @@ impl Process {
region_prophet,
region_heap,
value,
self.tx_idx,
self.env_idx
), ctx_regs_status, ctx_code_regs_status, registers_status, zone_length, mem_base_addr, tape_base_addr, aux_steps,
mem_addr, tape_addr, is_rw, region_prophet, region_heap, value);
@@ -1663,11 +1646,9 @@ impl Process {
GoldilocksField::from_canonical_u64(FilterLockForMain::True as u64),
region_prophet,
region_heap,
self.tx_idx,
self.env_idx
)?,
self.tape.write(
self.tx_idx,
tape_addr,
self.clk,
GoldilocksField::from_canonical_u64(1 << Opcode::TSTORE as u64),
@@ -1743,7 +1724,6 @@ impl Process {

if !program.pre_exe_flag {
program.trace.insert_sccall(
self.tx_idx,
self.env_idx,
self.addr_storage,
self.addr_code,
@@ -1770,7 +1750,6 @@ impl Process {
GoldilocksField::ZERO,
GoldilocksField::ZERO,
ctx_code_regs_status.clone(),
self.tx_idx,
self.env_idx,
self.call_sc_cnt,
self.storage_access_idx,
@@ -1796,7 +1775,6 @@ impl Process {
GoldilocksField::ONE,
GoldilocksField::ZERO,
self.addr_code,
self.tx_idx,
self.env_idx,
self.call_sc_cnt,
self.storage_access_idx,
@@ -1876,7 +1854,6 @@ impl Process {
GoldilocksField::from_canonical_u64(0_u64),
GoldilocksField::from_canonical_u64(1_u64),
GoldilocksField(HP_START_ADDR + 1),
self.tx_idx,
self.env_idx,
);

@@ -2028,7 +2005,6 @@ impl Process {
GoldilocksField::ZERO,
GoldilocksField::ZERO,
ctx_code_regs_status,
self.tx_idx,
self.env_idx,
self.call_sc_cnt,
storage_acc_id_status,
2 changes: 0 additions & 2 deletions executor/src/load_tx.rs
Original file line number Diff line number Diff line change
@@ -10,7 +10,6 @@ use serde_derive::{Deserialize, Serialize};
pub fn load_tx_calldata(process: &mut Process, calldate: &Vec<GoldilocksField>) {
for data in calldate {
process.tape.write(
process.tx_idx,
process.tp.to_canonical_u64(),
0,
GoldilocksField::from_canonical_u64(0),
@@ -52,7 +51,6 @@ macro_rules! load_ctx_to_tape {
.read_u64::<LittleEndian>()
.expect("failed to deserialize value");
process.tape.write(
process.tx_idx,
tp + addr as u64,
0,
GoldilocksField::from_canonical_u64(0),
15 changes: 2 additions & 13 deletions executor/src/storage.rs
Original file line number Diff line number Diff line change
@@ -10,7 +10,6 @@ use serde::{Deserialize, Serialize};

#[derive(Debug, Copy, Clone, Serialize, Deserialize, Eq, PartialEq)]
pub struct StorageCell {
pub tx_idx: GoldilocksField,
pub env_idx: GoldilocksField,
pub clk: u32,
pub op: GoldilocksField,
@@ -21,16 +20,11 @@ pub struct StorageCell {

impl Ord for StorageCell {
fn cmp(&self, other: &Self) -> Ordering {
let mut order = self.tx_idx.0.cmp(&other.tx_idx.0);
let mut order = self.env_idx.0.cmp(&other.env_idx.0);
if order.is_ne() {
return order;
} else {
order = self.env_idx.0.cmp(&other.env_idx.0);
if order.is_ne() {
return order;
}
order = self.clk.cmp(&other.clk);
}
order = self.clk.cmp(&other.clk);
return order;
}

@@ -95,7 +89,6 @@ impl StorageTree {
addr: TreeKey,
root: ZkHash,
value: TreeValue,
tx_idx: GoldilocksField,
env_idx: GoldilocksField,
) -> TreeValue {
// look up the previous value in the appropriate address trace and add (clk,
@@ -112,7 +105,6 @@ impl StorageTree {
addr,
root,
value: last_value,
tx_idx,
env_idx,
};
addr_trace.push(new_value);
@@ -124,7 +116,6 @@ impl StorageTree {
addr,
root,
value,
tx_idx,
env_idx,
};
vec![new_value]
@@ -141,7 +132,6 @@ impl StorageTree {
addr: TreeKey,
value: TreeValue,
root: ZkHash,
tx_idx: GoldilocksField,
env_idx: GoldilocksField,
) {
// add a memory access to the appropriate address trace; if this is the first
@@ -152,7 +142,6 @@ impl StorageTree {
addr,
value,
root,
tx_idx,
env_idx,
};
self.trace
5 changes: 0 additions & 5 deletions executor/src/tape.rs
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@ use std::collections::BTreeMap;

#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub struct TapeCell {
pub tx_idx: GoldilocksField,
pub clk: u32,
pub is_init: GoldilocksField,
pub op: GoldilocksField,
@@ -24,7 +23,6 @@ pub struct TapeTree {
impl TapeTree {
pub fn read(
&mut self,
tx_idx: GoldilocksField,
addr: u64,
clk: u32,
op: GoldilocksField,
@@ -38,7 +36,6 @@ impl TapeTree {
if let Some(tape_data) = read_res {
let last_value = tape_data.last().expect("empty address trace").value;
let new_value = TapeCell {
tx_idx,
clk,
op,
is_init: tape_data.last().expect("empty address trace").is_init,
@@ -68,7 +65,6 @@ impl TapeTree {

pub fn write(
&mut self,
tx_idx: GoldilocksField,
addr: u64,
clk: u32,
op: GoldilocksField,
@@ -79,7 +75,6 @@ impl TapeTree {
// add a memory access to the appropriate address trace; if this is the first
// time we access this address, initialize address trace.
let new_cell = TapeCell {
tx_idx,
clk,
op,
is_init,
5 changes: 0 additions & 5 deletions executor/src/trace.rs
Original file line number Diff line number Diff line change
@@ -62,7 +62,6 @@ pub fn gen_memory_table(
if first_row_flag {
let rc_value = GoldilocksField::ZERO;
let trace_cell = MemoryTraceCell {
tx_idx: cell.tx_idx,
env_idx: cell.env_idx,
addr: GoldilocksField::from_canonical_u64(canonical_addr),
clk: GoldilocksField::from_canonical_u64(cell.clk as u64),
@@ -115,7 +114,6 @@ pub fn gen_memory_table(
}
diff_clk = GoldilocksField::ZERO;
let trace_cell = MemoryTraceCell {
tx_idx: cell.tx_idx,
env_idx: cell.env_idx,
addr: GoldilocksField::from_canonical_u64(canonical_addr),
clk: GoldilocksField::from_canonical_u64(cell.clk as u64),
@@ -156,7 +154,6 @@ pub fn gen_memory_table(
}

let trace_cell = MemoryTraceCell {
tx_idx: cell.tx_idx,
env_idx: cell.env_idx,
addr: GoldilocksField::from_canonical_u64(canonical_addr),
clk: GoldilocksField::from_canonical_u64(cell.clk as u64),
@@ -377,7 +374,6 @@ pub fn gen_storage_table(
root,
item.1.addr,
item.1.value,
item.1.tx_idx,
item.1.env_idx,
);

@@ -399,7 +395,6 @@ pub fn gen_tape_table(process: &mut Process, program: &mut Program) -> Result<()
for (addr, cells) in process.tape.trace.iter() {
for tape_row in cells {
program.trace.tape.push(TapeRow {
tx_idx: tape_row.tx_idx,
is_init: tape_row.is_init.is_one(),
opcode: tape_row.op,
addr: GoldilocksField::from_canonical_u64(*addr),
3 changes: 0 additions & 3 deletions zk-vm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -219,15 +219,13 @@ impl OlaVM {

pub fn execute_tx(
&mut self,
tx_idx: GoldilocksField,
caller_addr: TreeValue,
code_exe_addr: TreeValue,
calldata: Vec<GoldilocksField>,
) -> Result<(), StateError> {
let mut env_idx = 0;
let mut sc_cnt = 0;
let mut process = Arc::new(Mutex::new(Process::new()));
mutex_data!(process).tx_idx = tx_idx;
mutex_data!(process).env_idx = GoldilocksField::from_canonical_u64(env_idx);
mutex_data!(process).call_sc_cnt = GoldilocksField::from_canonical_u64(sc_cnt);
mutex_data!(process).addr_storage = caller_addr;
@@ -275,7 +273,6 @@ impl OlaVM {
process = Arc::new(Mutex::new(Process::new()));
mutex_data!(process).tape = tape_tree;
mutex_data!(process).tp = tp.clone();
mutex_data!(process).tx_idx = tx_idx;
mutex_data!(process).env_idx = GoldilocksField::from_canonical_u64(sc_cnt);
mutex_data!(process).call_sc_cnt = GoldilocksField::from_canonical_u64(sc_cnt);

7 changes: 1 addition & 6 deletions zk-vm/src/test.rs
Original file line number Diff line number Diff line change
@@ -72,12 +72,7 @@ pub mod tests {
GoldilocksField::from_canonical_u64(3965482278),
];

let res = node.execute_tx(
GoldilocksField::from_canonical_u64(5),
caller_address,
caller_exe_address,
calldata,
);
let res = node.execute_tx(caller_address, caller_exe_address, calldata);

if res.is_ok() {
println!("run tx success:{:?}", res);

0 comments on commit ee04692

Please sign in to comment.