From 33c8e73403bbd7bd0aea38f87fd6d96382b23a5c Mon Sep 17 00:00:00 2001 From: AntiochP <30642645+antiochp@users.noreply.github.com> Date: Sat, 6 Jan 2018 22:18:12 -0500 Subject: [PATCH] maintain switch_commit_hashes in the output pmmr (not used yet) (#583) --- chain/src/sumtree.rs | 19 ++++++++++++------- core/src/core/transaction.rs | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/chain/src/sumtree.rs b/chain/src/sumtree.rs index 50477b787..fa16fb5c4 100644 --- a/chain/src/sumtree.rs +++ b/chain/src/sumtree.rs @@ -100,7 +100,9 @@ impl SumTrees { ); if let Some(hs) = output_pmmr.get(pos) { let hashsum = HashSum::from_summable( - pos, &SumCommit{commit: commit.clone()}); + pos, + &SumCommit::from_commit(&commit), + ); Ok(hs.hash == hashsum.hash) } else { Ok(false) @@ -264,6 +266,12 @@ impl<'a> Extension<'a> { for out in &b.outputs { let commit = out.commitment(); + let switch_commit_hash = out.switch_commit_hash(); + let sum_commit = SumCommit { + commit, + switch_commit_hash, + }; + if let Ok(pos) = self.get_output_pos(&commit) { // we need to check whether the commitment is in the current MMR view // as well as the index doesn't support rewind and is non-authoritative @@ -271,7 +279,8 @@ impl<'a> Extension<'a> { // note that this doesn't show the commitment *never* existed, just // that this is not an existing unspent commitment right now if let Some(c) = self.output_pmmr.get(pos) { - let hashsum = HashSum::from_summable(pos, &SumCommit{commit}); + let hashsum = HashSum::from_summable(pos, &sum_commit); + // processing a new fork so we may get a position on the old // fork that exists but matches a different node // filtering that case out @@ -282,11 +291,7 @@ impl<'a> Extension<'a> { } // push new outputs commitments in their MMR and save them in the index let pos = self.output_pmmr - .push( - SumCommit { - commit: out.commitment(), - }, - ) + .push(sum_commit) .map_err(&Error::SumTreeErr)?; self.new_output_commits.insert(out.commitment(), pos); diff --git a/core/src/core/transaction.rs b/core/src/core/transaction.rs index 6c3777a34..dcc69ec54 100644 --- a/core/src/core/transaction.rs +++ b/core/src/core/transaction.rs @@ -467,6 +467,11 @@ impl SwitchCommitHash { } SwitchCommitHash { hash: h } } + + /// Build an "zero" switch commitment hash + pub fn zero() -> SwitchCommitHash { + SwitchCommitHash { hash: [0; SWITCH_COMMIT_HASH_SIZE] } + } } /// Output for a transaction, defining the new ownership of coins that are being @@ -571,6 +576,19 @@ impl Output { pub struct SumCommit { /// Output commitment pub commit: Commitment, + /// The corresponding "switch commit hash" + pub switch_commit_hash: SwitchCommitHash, +} + +impl SumCommit { + /// For when we do not care about the switch_commit_hash + /// for example when comparing sum_commit hashes + pub fn from_commit(commit: &Commitment) -> SumCommit { + SumCommit { + commit: commit.clone(), + switch_commit_hash: SwitchCommitHash::zero(), + } + } } /// Outputs get summed through their commitments. @@ -578,17 +596,23 @@ impl Summable for SumCommit { type Sum = SumCommit; fn sum(&self) -> SumCommit { - SumCommit { commit: self.commit.clone() } + SumCommit { + commit: self.commit.clone(), + switch_commit_hash: self.switch_commit_hash.clone(), + } } fn sum_len() -> usize { - secp::constants::PEDERSEN_COMMITMENT_SIZE + secp::constants::PEDERSEN_COMMITMENT_SIZE + SWITCH_COMMIT_HASH_SIZE } } impl Writeable for SumCommit { fn write(&self, writer: &mut W) -> Result<(), ser::Error> { self.commit.write(writer)?; + if writer.serialization_mode() == ser::SerializationMode::Full { + self.switch_commit_hash.write(writer)?; + } Ok(()) } } @@ -596,8 +620,12 @@ impl Writeable for SumCommit { impl Readable for SumCommit { fn read(reader: &mut Reader) -> Result { let commit = Commitment::read(reader)?; + let switch_commit_hash = SwitchCommitHash::read(reader)?; - Ok(SumCommit { commit: commit }) + Ok(SumCommit { + commit: commit, + switch_commit_hash: switch_commit_hash, + }) } } @@ -616,7 +644,7 @@ impl ops::Add for SumCommit { Ok(s) => s, Err(_) => Commitment::from_vec(vec![1; 33]), }; - SumCommit { commit: sum } + SumCommit::from_commit(&sum) } }