From 9c74fdc280a938c35410e22931dc50c884678f6e Mon Sep 17 00:00:00 2001 From: Zhang Zhuo Date: Tue, 11 Jul 2023 17:29:02 +0800 Subject: [PATCH 1/3] capacity checking works well --- zktrie/src/state.rs | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/zktrie/src/state.rs b/zktrie/src/state.rs index 49db52d92c..3d05a71cc2 100644 --- a/zktrie/src/state.rs +++ b/zktrie/src/state.rs @@ -89,12 +89,16 @@ impl ZktrieState { ) } - /// .. + /// `overwrite` should be false when we use capacity checking API. + /// Whe we use capacity checking API, the storage proof is given tx by tx, + /// but they are all storage proofs generated for the state **before** this block. + /// So the local statedb is more accurate. pub fn update_statedb_from_proofs<'d, BYTES>( &mut self, account_proofs: impl Iterator + Clone, storage_proofs: impl Iterator + Clone, _additional_proofs: impl Iterator + Clone, + overwrite: bool, ) -> Result<(), Error> where BYTES: IntoIterator, @@ -109,13 +113,22 @@ impl ZktrieState { let acc_data = acc_proof.data; let (exists, acc) = self.sdb.get_account(addr); if exists { - log::trace!( - "overwrite trace account in sdb: addr {:?}, new {:?}, replace old: {:?}", - addr, - acc_data, - acc - ); - //continue; + if overwrite { + log::trace!( + "overwrite trace account in sdb: addr {:?}, new {:?}, replace old: {:?}", + addr, + acc_data, + acc + ); + } else { + log::trace!( + "skip trace account into sdb: addr {:?}, new {:?}, keep old: {:?}", + addr, + acc_data, + acc + ); + continue; + } } if acc_proof.key.is_some() { log::trace!("trace account into sdb: {:?} => {:?}", addr, acc_data); @@ -139,11 +152,13 @@ impl ZktrieState { } for (addr, key, bytes) in storage_proofs { - let (_exists, old_value) = self.sdb.get_storage(addr, key); + let (exists, old_value) = self.sdb.get_storage(addr, key); let old_value = *old_value; - //if exists { - // continue; - //} + if exists { + if !overwrite { + continue; + } + } let (_, acc) = self.sdb.get_account_mut(addr); let mut key_buf = [0u8; 32]; key.to_big_endian(key_buf.as_mut_slice()); @@ -197,6 +212,7 @@ impl ZktrieState { account_proofs.clone(), storage_proofs.clone(), additional_proofs.clone(), + true, )?; self.update_nodes_from_proofs(account_proofs, storage_proofs, additional_proofs)?; Ok(()) @@ -238,6 +254,7 @@ impl ZktrieState { account_proofs.clone(), storage_proofs.clone(), additional_proofs.clone(), + true, )?; state.update_nodes_from_proofs(account_proofs, storage_proofs, additional_proofs)?; Ok(state) From 6dbb39d5480a06764abfda02e5842110c2361d9a Mon Sep 17 00:00:00 2001 From: Zhang Zhuo Date: Tue, 11 Jul 2023 18:16:15 +0800 Subject: [PATCH 2/3] lint --- zktrie/src/state.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/zktrie/src/state.rs b/zktrie/src/state.rs index 3d05a71cc2..35d7a3ac83 100644 --- a/zktrie/src/state.rs +++ b/zktrie/src/state.rs @@ -154,10 +154,8 @@ impl ZktrieState { for (addr, key, bytes) in storage_proofs { let (exists, old_value) = self.sdb.get_storage(addr, key); let old_value = *old_value; - if exists { - if !overwrite { - continue; - } + if exists && !overwrite { + continue; } let (_, acc) = self.sdb.get_account_mut(addr); let mut key_buf = [0u8; 32]; From 3190591eade99553620305ea00eaa221492d6d41 Mon Sep 17 00:00:00 2001 From: Zhang Zhuo Date: Tue, 18 Jul 2023 13:30:02 +0800 Subject: [PATCH 3/3] fix state wit --- zktrie/src/state.rs | 33 +++++++++------------------------ 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/zktrie/src/state.rs b/zktrie/src/state.rs index 35d7a3ac83..2e801eb64a 100644 --- a/zktrie/src/state.rs +++ b/zktrie/src/state.rs @@ -89,16 +89,12 @@ impl ZktrieState { ) } - /// `overwrite` should be false when we use capacity checking API. - /// Whe we use capacity checking API, the storage proof is given tx by tx, - /// but they are all storage proofs generated for the state **before** this block. - /// So the local statedb is more accurate. + /// .. pub fn update_statedb_from_proofs<'d, BYTES>( &mut self, account_proofs: impl Iterator + Clone, storage_proofs: impl Iterator + Clone, _additional_proofs: impl Iterator + Clone, - overwrite: bool, ) -> Result<(), Error> where BYTES: IntoIterator, @@ -113,22 +109,13 @@ impl ZktrieState { let acc_data = acc_proof.data; let (exists, acc) = self.sdb.get_account(addr); if exists { - if overwrite { - log::trace!( - "overwrite trace account in sdb: addr {:?}, new {:?}, replace old: {:?}", - addr, - acc_data, - acc - ); - } else { - log::trace!( - "skip trace account into sdb: addr {:?}, new {:?}, keep old: {:?}", - addr, - acc_data, - acc - ); - continue; - } + log::trace!( + "skip trace account into sdb: addr {:?}, new {:?}, keep old: {:?}", + addr, + acc_data, + acc + ); + continue; } if acc_proof.key.is_some() { log::trace!("trace account into sdb: {:?} => {:?}", addr, acc_data); @@ -154,7 +141,7 @@ impl ZktrieState { for (addr, key, bytes) in storage_proofs { let (exists, old_value) = self.sdb.get_storage(addr, key); let old_value = *old_value; - if exists && !overwrite { + if exists { continue; } let (_, acc) = self.sdb.get_account_mut(addr); @@ -210,7 +197,6 @@ impl ZktrieState { account_proofs.clone(), storage_proofs.clone(), additional_proofs.clone(), - true, )?; self.update_nodes_from_proofs(account_proofs, storage_proofs, additional_proofs)?; Ok(()) @@ -252,7 +238,6 @@ impl ZktrieState { account_proofs.clone(), storage_proofs.clone(), additional_proofs.clone(), - true, )?; state.update_nodes_from_proofs(account_proofs, storage_proofs, additional_proofs)?; Ok(state)