From 69b71fc0b2dcb22d99536e59be03ba5a16ab9ccb Mon Sep 17 00:00:00 2001 From: peilun-conflux <48905552+peilun-conflux@users.noreply.github.com> Date: Fri, 27 Sep 2024 10:02:47 +0800 Subject: [PATCH] Fix issue in reverting the last incomplete tx. (#215) --- node/storage/src/log_store/load_chunk/chunk_data.rs | 11 ++--------- node/storage/src/log_store/load_chunk/mod.rs | 8 ++++++++ node/storage/src/log_store/log_manager.rs | 1 + 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/node/storage/src/log_store/load_chunk/chunk_data.rs b/node/storage/src/log_store/load_chunk/chunk_data.rs index 1a499bf..e816d4d 100644 --- a/node/storage/src/log_store/load_chunk/chunk_data.rs +++ b/node/storage/src/log_store/load_chunk/chunk_data.rs @@ -189,15 +189,8 @@ impl EntryBatchData { } pub fn insert_data(&mut self, start_byte: usize, mut data: Vec) -> Result> { - assert!(start_byte % BYTES_PER_SECTOR == 0); - assert!(data.len() % BYTES_PER_SECTOR == 0); - - if data.is_empty() || self.get(start_byte, data.len()) == Some(&data) { - // TODO(zz): This assumes the caller has processed chain reorg (truncate flow) before - // inserting new data, and the data of the same file are always inserted with the - // same pattern. - return Ok(vec![]); - } + assert_eq!(start_byte % BYTES_PER_SECTOR, 0); + assert_eq!(data.len() % BYTES_PER_SECTOR, 0); // Check if the entry is completed let (list, subtree_list) = if let EntryBatchData::Incomplete(x) = self { diff --git a/node/storage/src/log_store/load_chunk/mod.rs b/node/storage/src/log_store/load_chunk/mod.rs index f62fe7c..3daff69 100644 --- a/node/storage/src/log_store/load_chunk/mod.rs +++ b/node/storage/src/log_store/load_chunk/mod.rs @@ -127,6 +127,14 @@ impl EntryBatch { /// Return `Error` if the new data overlaps with old data. /// Convert `Incomplete` to `Completed` if the chunk is completed after the insertion. pub fn insert_data(&mut self, offset: usize, data: Vec) -> Result> { + if data.is_empty() + || self + .get_unsealed_data(offset, data.len() / BYTES_PER_SECTOR) + .as_ref() + == Some(&data) + { + return Ok(vec![]); + } self.data.insert_data(offset * BYTES_PER_SECTOR, data) } diff --git a/node/storage/src/log_store/log_manager.rs b/node/storage/src/log_store/log_manager.rs index 026ca8b..1642c6d 100644 --- a/node/storage/src/log_store/log_manager.rs +++ b/node/storage/src/log_store/log_manager.rs @@ -708,6 +708,7 @@ impl LogManager { }; if let Some(tx) = last_tx_to_insert { + log_manager.revert_to(tx.seq - 1)?; log_manager.put_tx(tx)?; let mut merkle = log_manager.merkle.write(); for (index, h) in extra_leaves {