mirror of
https://github.com/mimblewimble/grin.git
synced 2025-02-08 12:21:09 +03:00
Fix sumtree checks ignoring total size
Duplicate commitment and ducplicate kernel errors, as well as `is_unspent` did not check the total PMMR size, leading to wrong detection of duplicates or unspent.
This commit is contained in:
parent
7573f6a556
commit
5807a6b270
1 changed files with 16 additions and 6 deletions
|
@ -1,4 +1,4 @@
|
||||||
// Copyright 2016 The Grin Developers
|
|
||||||
//
|
//
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
// you may not use this file except in compliance with the License.
|
// you may not use this file except in compliance with the License.
|
||||||
|
@ -93,7 +93,13 @@ impl SumTrees {
|
||||||
pub fn is_unspent(&self, commit: &Commitment) -> Result<bool, Error> {
|
pub fn is_unspent(&self, commit: &Commitment) -> Result<bool, Error> {
|
||||||
let rpos = self.commit_index.get_output_pos(commit);
|
let rpos = self.commit_index.get_output_pos(commit);
|
||||||
match rpos {
|
match rpos {
|
||||||
Ok(pos) => Ok(self.output_pmmr_h.backend.get(pos).is_some()),
|
Ok(pos) => {
|
||||||
|
if pos > self.output_pmmr_h.last_pos {
|
||||||
|
Ok(false)
|
||||||
|
} else {
|
||||||
|
Ok(self.output_pmmr_h.backend.get(pos).is_some())
|
||||||
|
}
|
||||||
|
}
|
||||||
Err(grin_store::Error::NotFoundErr) => Ok(false),
|
Err(grin_store::Error::NotFoundErr) => Ok(false),
|
||||||
Err(e) => Err(Error::StoreErr(e, "sumtree unspent check".to_owned())),
|
Err(e) => Err(Error::StoreErr(e, "sumtree unspent check".to_owned())),
|
||||||
}
|
}
|
||||||
|
@ -252,9 +258,11 @@ impl<'a> Extension<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
for out in &b.outputs {
|
for out in &b.outputs {
|
||||||
if let Ok(_) = self.commit_index.get_output_pos(&out.commitment()) {
|
if let Ok(pos) = self.commit_index.get_output_pos(&out.commitment()) {
|
||||||
|
if pos <= self.output_pmmr.unpruned_size() {
|
||||||
return Err(Error::DuplicateCommitment(out.commitment()));
|
return Err(Error::DuplicateCommitment(out.commitment()));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// push new outputs commitments in their MMR and save them in the index
|
// push new outputs commitments in their MMR and save them in the index
|
||||||
let pos = self.output_pmmr
|
let pos = self.output_pmmr
|
||||||
.push(
|
.push(
|
||||||
|
@ -274,9 +282,11 @@ impl<'a> Extension<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
for kernel in &b.kernels {
|
for kernel in &b.kernels {
|
||||||
if let Ok(_) = self.commit_index.get_kernel_pos(&kernel.excess) {
|
if let Ok(pos) = self.commit_index.get_kernel_pos(&kernel.excess) {
|
||||||
|
if pos <= self.kernel_pmmr.unpruned_size() {
|
||||||
return Err(Error::DuplicateKernel(kernel.excess.clone()));
|
return Err(Error::DuplicateKernel(kernel.excess.clone()));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// push kernels in their MMR
|
// push kernels in their MMR
|
||||||
let pos = self.kernel_pmmr
|
let pos = self.kernel_pmmr
|
||||||
.push(NoSum(kernel.clone()), None::<RangeProof>)
|
.push(NoSum(kernel.clone()), None::<RangeProof>)
|
||||||
|
|
Loading…
Reference in a new issue