fix: pad tx based on the on-chain contract result. (#224)

This commit is contained in:
peilun-conflux 2024-10-09 15:20:42 +08:00 committed by GitHub
parent b76fa7be9b
commit 4e2c5fa8a4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 14 deletions

View File

@ -113,12 +113,16 @@ impl Transaction {
1 << (depth - 1) 1 << (depth - 1)
} }
pub fn num_entries(&self) -> usize { pub fn num_entries_of_list(merkle_nodes: &[(usize, DataRoot)]) -> usize {
self.merkle_nodes.iter().fold(0, |size, &(depth, _)| { merkle_nodes.iter().fold(0, |size, &(depth, _)| {
size + Transaction::num_entries_of_node(depth) size + Transaction::num_entries_of_node(depth)
}) })
} }
pub fn num_entries(&self) -> usize {
Self::num_entries_of_list(&self.merkle_nodes)
}
pub fn hash(&self) -> H256 { pub fn hash(&self) -> H256 {
let bytes = self.as_ssz_bytes(); let bytes = self.as_ssz_bytes();
let mut h = Keccak::v256(); let mut h = Keccak::v256();

View File

@ -257,7 +257,7 @@ impl LogStoreWrite for LogManager {
} }
let maybe_same_data_tx_seq = self.tx_store.put_tx(tx.clone())?.first().cloned(); let maybe_same_data_tx_seq = self.tx_store.put_tx(tx.clone())?.first().cloned();
// TODO(zz): Should we validate received tx? // TODO(zz): Should we validate received tx?
self.append_subtree_list(tx.merkle_nodes.clone(), &mut merkle)?; self.append_subtree_list(tx.start_entry_index, tx.merkle_nodes.clone(), &mut merkle)?;
merkle.commit_merkle(tx.seq)?; merkle.commit_merkle(tx.seq)?;
debug!( debug!(
"commit flow root: root={:?}", "commit flow root: root={:?}",
@ -868,6 +868,7 @@ impl LogManager {
#[instrument(skip(self, merkle))] #[instrument(skip(self, merkle))]
fn append_subtree_list( fn append_subtree_list(
&self, &self,
tx_start_index: u64,
merkle_list: Vec<(usize, DataRoot)>, merkle_list: Vec<(usize, DataRoot)>,
merkle: &mut MerkleManager, merkle: &mut MerkleManager,
) -> Result<()> { ) -> Result<()> {
@ -875,7 +876,7 @@ impl LogManager {
return Ok(()); return Ok(());
} }
self.pad_tx(1 << (merkle_list[0].0 - 1), &mut *merkle)?; self.pad_tx(tx_start_index, &mut *merkle)?;
let mut batch_root_map = BTreeMap::new(); let mut batch_root_map = BTreeMap::new();
for (subtree_depth, subtree_root) in merkle_list { for (subtree_depth, subtree_root) in merkle_list {
@ -923,18 +924,18 @@ impl LogManager {
} }
#[instrument(skip(self, merkle))] #[instrument(skip(self, merkle))]
fn pad_tx(&self, first_subtree_size: u64, merkle: &mut MerkleManager) -> Result<()> { fn pad_tx(&self, tx_start_index: u64, merkle: &mut MerkleManager) -> Result<()> {
// Check if we need to pad the flow. // Check if we need to pad the flow.
let mut tx_start_flow_index = let mut tx_start_flow_index =
merkle.last_chunk_start_index() + merkle.last_chunk_merkle.leaves() as u64; merkle.last_chunk_start_index() + merkle.last_chunk_merkle.leaves() as u64;
let extra = tx_start_flow_index % first_subtree_size; let pad_size = tx_start_index - tx_start_flow_index;
trace!( trace!(
"before pad_tx {} {}", "before pad_tx {} {}",
merkle.pora_chunks_merkle.leaves(), merkle.pora_chunks_merkle.leaves(),
merkle.last_chunk_merkle.leaves() merkle.last_chunk_merkle.leaves()
); );
if extra != 0 { if pad_size != 0 {
for pad_data in Self::padding((first_subtree_size - extra) as usize) { for pad_data in Self::padding(pad_size as usize) {
let mut is_full_empty = true; let mut is_full_empty = true;
let mut root_map = BTreeMap::new(); let mut root_map = BTreeMap::new();
@ -997,12 +998,10 @@ impl LogManager {
// Update the flow database. // Update the flow database.
// This should be called before `complete_last_chunk_merkle` so that we do not save // This should be called before `complete_last_chunk_merkle` so that we do not save
// subtrees with data known. // subtrees with data known.
self.flow_store self.flow_store.append_entries(ChunkArray {
.append_entries(ChunkArray {
data: pad_data.to_vec(), data: pad_data.to_vec(),
start_index: tx_start_flow_index, start_index: tx_start_flow_index,
}) })?;
.unwrap();
} }
tx_start_flow_index += data_size as u64; tx_start_flow_index += data_size as u64;