mirror of
https://github.com/mimblewimble/grin.git
synced 2025-02-01 17:01:09 +03:00
maintain switch_commit_hashes in the output pmmr (not used yet) (#583)
This commit is contained in:
parent
3515bf748c
commit
33c8e73403
2 changed files with 44 additions and 11 deletions
|
@ -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);
|
||||
|
|
|
@ -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<W: Writer>(&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<SumCommit, ser::Error> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue