From d92b6ddddb39a912ae3bd0ee7c163e770f75ff19 Mon Sep 17 00:00:00 2001 From: Ignotus Peverell Date: Tue, 12 Dec 2017 23:38:49 +0000 Subject: [PATCH] Fix for disappearing `AlreadySpent` error Fixes buffer misalignment when reading back pruning data. Everything after the buffer length (8000 bytes) was badly read on restart, leading to accepting double spends. --- store/src/sumtree.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/store/src/sumtree.rs b/store/src/sumtree.rs index f1b1f27fb..17454da7c 100644 --- a/store/src/sumtree.rs +++ b/store/src/sumtree.rs @@ -169,7 +169,7 @@ impl RemoveLog { /// Open the remove log file. The content of the file will be read in memory /// for fast checking. fn open(path: String) -> io::Result { - let removed = read_ordered_vec(path.clone())?; + let removed = read_ordered_vec(path.clone(), 12)?; Ok(RemoveLog { path: path, removed: removed, @@ -366,7 +366,7 @@ where let sz = hs_file.size()?; let record_len = 32 + T::sum_len(); let rm_log = RemoveLog::open(format!("{}/{}", data_dir, PMMR_RM_LOG_FILE))?; - let prune_list = read_ordered_vec(format!("{}/{}", data_dir, PMMR_PRUNED_FILE))?; + let prune_list = read_ordered_vec(format!("{}/{}", data_dir, PMMR_PRUNED_FILE), 8)?; Ok(PMMRBackend { data_dir: data_dir, @@ -507,14 +507,14 @@ where } // Read an ordered vector of scalars from a file. -fn read_ordered_vec(path: String) -> io::Result> +fn read_ordered_vec(path: String, elmt_len: usize) -> io::Result> where T: ser::Readable + cmp::Ord, { let file_path = Path::new(&path); let mut ovec = Vec::with_capacity(1000); if file_path.exists() { - let mut file = BufReader::with_capacity(8 * 1000, File::open(path.clone())?); + let mut file = BufReader::with_capacity(elmt_len * 1000, File::open(path.clone())?); loop { // need a block to end mutable borrow before consume let buf_len = {