maintain switch_commit_hashes in the output pmmr (not used yet) (#583)

This commit is contained in:
AntiochP 2018-01-06 22:18:12 -05:00 committed by GitHub
parent 3515bf748c
commit 33c8e73403
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 11 deletions

View file

@ -100,7 +100,9 @@ impl SumTrees {
); );
if let Some(hs) = output_pmmr.get(pos) { if let Some(hs) = output_pmmr.get(pos) {
let hashsum = HashSum::from_summable( let hashsum = HashSum::from_summable(
pos, &SumCommit{commit: commit.clone()}); pos,
&SumCommit::from_commit(&commit),
);
Ok(hs.hash == hashsum.hash) Ok(hs.hash == hashsum.hash)
} else { } else {
Ok(false) Ok(false)
@ -264,6 +266,12 @@ impl<'a> Extension<'a> {
for out in &b.outputs { for out in &b.outputs {
let commit = out.commitment(); 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) { if let Ok(pos) = self.get_output_pos(&commit) {
// we need to check whether the commitment is in the current MMR view // 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 // 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 // note that this doesn't show the commitment *never* existed, just
// that this is not an existing unspent commitment right now // that this is not an existing unspent commitment right now
if let Some(c) = self.output_pmmr.get(pos) { 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 // processing a new fork so we may get a position on the old
// fork that exists but matches a different node // fork that exists but matches a different node
// filtering that case out // 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 // 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(sum_commit)
SumCommit {
commit: out.commitment(),
},
)
.map_err(&Error::SumTreeErr)?; .map_err(&Error::SumTreeErr)?;
self.new_output_commits.insert(out.commitment(), pos); self.new_output_commits.insert(out.commitment(), pos);

View file

@ -467,6 +467,11 @@ impl SwitchCommitHash {
} }
SwitchCommitHash { hash: h } 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 /// Output for a transaction, defining the new ownership of coins that are being
@ -571,6 +576,19 @@ impl Output {
pub struct SumCommit { pub struct SumCommit {
/// Output commitment /// Output commitment
pub commit: 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. /// Outputs get summed through their commitments.
@ -578,17 +596,23 @@ impl Summable for SumCommit {
type Sum = SumCommit; type Sum = SumCommit;
fn sum(&self) -> 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 { fn sum_len() -> usize {
secp::constants::PEDERSEN_COMMITMENT_SIZE secp::constants::PEDERSEN_COMMITMENT_SIZE + SWITCH_COMMIT_HASH_SIZE
} }
} }
impl Writeable for SumCommit { impl Writeable for SumCommit {
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> { fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
self.commit.write(writer)?; self.commit.write(writer)?;
if writer.serialization_mode() == ser::SerializationMode::Full {
self.switch_commit_hash.write(writer)?;
}
Ok(()) Ok(())
} }
} }
@ -596,8 +620,12 @@ impl Writeable for SumCommit {
impl Readable for SumCommit { impl Readable for SumCommit {
fn read(reader: &mut Reader) -> Result<SumCommit, ser::Error> { fn read(reader: &mut Reader) -> Result<SumCommit, ser::Error> {
let commit = Commitment::read(reader)?; 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, Ok(s) => s,
Err(_) => Commitment::from_vec(vec![1; 33]), Err(_) => Commitment::from_vec(vec![1; 33]),
}; };
SumCommit { commit: sum } SumCommit::from_commit(&sum)
} }
} }