diff --git a/node/shared_types/src/lib.rs b/node/shared_types/src/lib.rs index aac0805..04e46b7 100644 --- a/node/shared_types/src/lib.rs +++ b/node/shared_types/src/lib.rs @@ -113,12 +113,16 @@ impl Transaction { 1 << (depth - 1) } - pub fn num_entries(&self) -> usize { - self.merkle_nodes.iter().fold(0, |size, &(depth, _)| { + pub fn num_entries_of_list(merkle_nodes: &[(usize, DataRoot)]) -> usize { + merkle_nodes.iter().fold(0, |size, &(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 { let bytes = self.as_ssz_bytes(); let mut h = Keccak::v256(); diff --git a/node/storage/src/log_store/log_manager.rs b/node/storage/src/log_store/log_manager.rs index 20b72ec..4df19b5 100644 --- a/node/storage/src/log_store/log_manager.rs +++ b/node/storage/src/log_store/log_manager.rs @@ -257,7 +257,7 @@ impl LogStoreWrite for LogManager { } let maybe_same_data_tx_seq = self.tx_store.put_tx(tx.clone())?.first().cloned(); // 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)?; debug!( "commit flow root: root={:?}", @@ -868,6 +868,7 @@ impl LogManager { #[instrument(skip(self, merkle))] fn append_subtree_list( &self, + tx_start_index: u64, merkle_list: Vec<(usize, DataRoot)>, merkle: &mut MerkleManager, ) -> Result<()> { @@ -875,7 +876,7 @@ impl LogManager { 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(); for (subtree_depth, subtree_root) in merkle_list { @@ -923,18 +924,18 @@ impl LogManager { } #[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. let mut tx_start_flow_index = 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!( "before pad_tx {} {}", merkle.pora_chunks_merkle.leaves(), merkle.last_chunk_merkle.leaves() ); - if extra != 0 { - for pad_data in Self::padding((first_subtree_size - extra) as usize) { + if pad_size != 0 { + for pad_data in Self::padding(pad_size as usize) { let mut is_full_empty = true; let mut root_map = BTreeMap::new(); @@ -997,12 +998,10 @@ impl LogManager { // Update the flow database. // This should be called before `complete_last_chunk_merkle` so that we do not save // subtrees with data known. - self.flow_store - .append_entries(ChunkArray { - data: pad_data.to_vec(), - start_index: tx_start_flow_index, - }) - .unwrap(); + self.flow_store.append_entries(ChunkArray { + data: pad_data.to_vec(), + start_index: tx_start_flow_index, + })?; } tx_start_flow_index += data_size as u64;