Skip to content

Commit

Permalink
Removed all switch commitment usages, including restore (#841)
Browse files Browse the repository at this point in the history
* Removed all switch commitment usages, including restore
* Fixed pool tests
* Fix keychain tests
* Get rid of the switch key in keychain
  • Loading branch information
ignopeverell authored Mar 22, 2018
1 parent ff4d68d commit ca8447f
Show file tree
Hide file tree
Showing 18 changed files with 55 additions and 921 deletions.
23 changes: 0 additions & 23 deletions api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use std::sync::Arc;
use core::{core, ser};
use core::core::hash::Hashed;
use core::core::pmmr::MerkleProof;
use core::core::SwitchCommitHash;
use chain;
use p2p;
use util;
Expand Down Expand Up @@ -228,8 +227,6 @@ pub struct OutputPrintable {
/// The homomorphic commitment representing the output's amount
/// (as hex string)
pub commit: pedersen::Commitment,
/// switch commit hash
pub switch_commit_hash: SwitchCommitHash,
/// Whether the output has been spent
pub spent: bool,
/// Rangeproof (as hex string)
Expand Down Expand Up @@ -280,19 +277,13 @@ impl OutputPrintable {
OutputPrintable {
output_type,
commit: output.commit,
switch_commit_hash: output.switch_commit_hash,
spent,
proof,
proof_hash: util::to_hex(output.proof.hash().to_vec()),
merkle_proof,
}
}

// Convert the hex string back into a switch_commit_hash instance
pub fn switch_commit_hash(&self) -> Result<core::SwitchCommitHash, ser::Error> {
Ok(self.switch_commit_hash.clone())
}

pub fn commit(&self) -> Result<pedersen::Commitment, ser::Error> {
Ok(self.commit.clone())
}
Expand All @@ -312,7 +303,6 @@ impl serde::ser::Serialize for OutputPrintable {
let mut state = serializer.serialize_struct("OutputPrintable", 7)?;
state.serialize_field("output_type", &self.output_type)?;
state.serialize_field("commit", &util::to_hex(self.commit.0.to_vec()))?;
state.serialize_field("switch_commit_hash", &self.switch_commit_hash.to_hex())?;
state.serialize_field("spent", &self.spent)?;
state.serialize_field("proof", &self.proof)?;
state.serialize_field("proof_hash", &self.proof_hash)?;
Expand All @@ -334,7 +324,6 @@ impl<'de> serde::de::Deserialize<'de> for OutputPrintable {
enum Field {
OutputType,
Commit,
SwitchCommitHash,
Spent,
Proof,
ProofHash,
Expand All @@ -356,7 +345,6 @@ impl<'de> serde::de::Deserialize<'de> for OutputPrintable {
{
let mut output_type = None;
let mut commit = None;
let mut switch_commit_hash = None;
let mut spent = None;
let mut proof = None;
let mut proof_hash = None;
Expand All @@ -376,14 +364,6 @@ impl<'de> serde::de::Deserialize<'de> for OutputPrintable {
util::from_hex(val.clone()).map_err(serde::de::Error::custom)?;
commit = Some(pedersen::Commitment::from_vec(vec));
}
Field::SwitchCommitHash => {
no_dup!(switch_commit_hash);

let val: String = map.next_value()?;
let hash = core::SwitchCommitHash::from_hex(&val.clone())
.map_err(serde::de::Error::custom)?;
switch_commit_hash = Some(hash)
}
Field::Spent => {
no_dup!(spent);
spent = Some(map.next_value()?)
Expand Down Expand Up @@ -427,7 +407,6 @@ impl<'de> serde::de::Deserialize<'de> for OutputPrintable {
Ok(OutputPrintable {
output_type: output_type.unwrap(),
commit: commit.unwrap(),
switch_commit_hash: switch_commit_hash.unwrap(),
spent: spent.unwrap(),
proof: proof,
proof_hash: proof_hash.unwrap(),
Expand All @@ -439,7 +418,6 @@ impl<'de> serde::de::Deserialize<'de> for OutputPrintable {
const FIELDS: &'static [&'static str] = &[
"output_type",
"commit",
"switch_commit_hash",
"spent",
"proof",
"proof_hash",
Expand Down Expand Up @@ -645,7 +623,6 @@ mod test {
"{\
\"output_type\":\"Coinbase\",\
\"commit\":\"083eafae5d61a85ab07b12e1a51b3918d8e6de11fc6cde641d54af53608aa77b9f\",\
\"switch_commit_hash\":\"85daaf11011dc11e52af84ebe78e2f2d19cbdc76000000000000000000000000\",\
\"spent\":false,\
\"proof\":null,\
\"proof_hash\":\"ed6ba96009b86173bade6a9227ed60422916593fa32dd6d78b25b7a4eeef4946\",\
Expand Down
5 changes: 2 additions & 3 deletions chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ use std::fs::File;
use std::sync::{Arc, Mutex, RwLock};
use std::time::{Duration, Instant};

use core::core::{Block, BlockHeader, Input, OutputFeatures, OutputIdentifier, OutputStoreable,
TxKernel};
use core::core::{Block, BlockHeader, Input, OutputFeatures, OutputIdentifier, TxKernel};
use core::core::hash::{Hash, Hashed};
use core::core::pmmr::MerkleProof;
use core::core::target::Difficulty;
Expand Down Expand Up @@ -625,7 +624,7 @@ impl Chain {
}

/// returns the last n nodes inserted into the output sum tree
pub fn get_last_n_output(&self, distance: u64) -> Vec<(Hash, Option<OutputStoreable>)> {
pub fn get_last_n_output(&self, distance: u64) -> Vec<(Hash, Option<OutputIdentifier>)> {
let mut txhashset = self.txhashset.write().unwrap();
txhashset.last_n_output(distance)
}
Expand Down
19 changes: 9 additions & 10 deletions chain/src/txhashset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ use util::static_secp_instance;
use util::secp::pedersen::{Commitment, RangeProof};

use core::consensus::REWARD;
use core::core::{Block, BlockHeader, Input, Output, OutputFeatures, OutputIdentifier,
OutputStoreable, TxKernel};
use core::core::{Block, BlockHeader, Input, Output, OutputFeatures, OutputIdentifier, TxKernel};
use core::core::pmmr::{self, MerkleProof, PMMR};
use core::global;
use core::core::hash::{Hash, Hashed};
Expand Down Expand Up @@ -90,7 +89,7 @@ where
/// pruning enabled.

pub struct TxHashSet {
output_pmmr_h: PMMRHandle<OutputStoreable>,
output_pmmr_h: PMMRHandle<OutputIdentifier>,
rproof_pmmr_h: PMMRHandle<RangeProof>,
kernel_pmmr_h: PMMRHandle<TxKernel>,

Expand Down Expand Up @@ -144,7 +143,7 @@ impl TxHashSet {
pub fn is_unspent(&mut self, output_id: &OutputIdentifier) -> Result<Hash, Error> {
match self.commit_index.get_output_pos(&output_id.commit) {
Ok(pos) => {
let output_pmmr: PMMR<OutputStoreable, _> =
let output_pmmr: PMMR<OutputIdentifier, _> =
PMMR::at(&mut self.output_pmmr_h.backend, self.output_pmmr_h.last_pos);
if let Some((hash, _)) = output_pmmr.get(pos, false) {
if hash == output_id.hash_with_index(pos) {
Expand All @@ -164,8 +163,8 @@ impl TxHashSet {
/// returns the last N nodes inserted into the tree (i.e. the 'bottom'
/// nodes at level 0
/// TODO: These need to return the actual data from the flat-files instead of hashes now
pub fn last_n_output(&mut self, distance: u64) -> Vec<(Hash, Option<OutputStoreable>)> {
let output_pmmr: PMMR<OutputStoreable, _> =
pub fn last_n_output(&mut self, distance: u64) -> Vec<(Hash, Option<OutputIdentifier>)> {
let output_pmmr: PMMR<OutputIdentifier, _> =
PMMR::at(&mut self.output_pmmr_h.backend, self.output_pmmr_h.last_pos);
output_pmmr.get_last_n_insertions(distance)
}
Expand Down Expand Up @@ -201,7 +200,7 @@ impl TxHashSet {
/// Get sum tree roots
/// TODO: Return data instead of hashes
pub fn roots(&mut self) -> (Hash, Hash, Hash) {
let output_pmmr: PMMR<OutputStoreable, _> =
let output_pmmr: PMMR<OutputIdentifier, _> =
PMMR::at(&mut self.output_pmmr_h.backend, self.output_pmmr_h.last_pos);
let rproof_pmmr: PMMR<RangeProof, _> =
PMMR::at(&mut self.rproof_pmmr_h.backend, self.rproof_pmmr_h.last_pos);
Expand Down Expand Up @@ -298,7 +297,7 @@ where
/// reversible manner within a unit of work provided by the `extending`
/// function.
pub struct Extension<'a> {
output_pmmr: PMMR<'a, OutputStoreable, PMMRBackend<OutputStoreable>>,
output_pmmr: PMMR<'a, OutputIdentifier, PMMRBackend<OutputIdentifier>>,
rproof_pmmr: PMMR<'a, RangeProof, PMMRBackend<RangeProof>>,
kernel_pmmr: PMMR<'a, TxKernel, PMMRBackend<TxKernel>>,

Expand Down Expand Up @@ -440,14 +439,14 @@ impl<'a> Extension<'a> {
// 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
if hash == OutputStoreable::from_output(out).hash() {
if hash == OutputIdentifier::from_output(out).hash() {
return Err(Error::DuplicateCommitment(commit));
}
}
}
// push new outputs in their MMR and save them in the index
let pos = self.output_pmmr
.push(OutputStoreable::from_output(out))
.push(OutputIdentifier::from_output(out))
.map_err(&Error::TxHashSetErr)?;
self.new_output_commits.insert(out.commitment(), pos);

Expand Down
4 changes: 4 additions & 0 deletions chain/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,14 @@ pub trait ChainStore: Send + Sync {
/// Deletes the MMR position of an output.
fn delete_output_pos(&self, commit: &[u8]) -> Result<(), store::Error>;

/// Saves a marker associated with a block recording the MMR positions of its
/// last elements.
fn save_block_marker(&self, bh: &Hash, marker: &(u64, u64)) -> Result<(), store::Error>;

/// Retrieves a block marker from a block hash.
fn get_block_marker(&self, bh: &Hash) -> Result<(u64, u64), store::Error>;

/// Deletes a block marker associated with the provided hash
fn delete_block_marker(&self, bh: &Hash) -> Result<(), store::Error>;

/// Saves information about the last written PMMR file positions for each
Expand Down
38 changes: 12 additions & 26 deletions core/src/core/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use time;
use rand::{thread_rng, Rng};
use std::collections::HashSet;

use core::{Committed, Input, KernelFeatures, Output, OutputFeatures, Proof, ProofMessageElements,
ShortId, SwitchCommitHash, Transaction, TxKernel};
use core::{Committed, Input, KernelFeatures, Output, OutputFeatures, Proof,
ShortId, Transaction, TxKernel};
use consensus;
use consensus::{exceeds_weight, reward, VerifySortOrder, REWARD};
use core::hash::{Hash, HashWriter, Hashed, ZERO_HASH};
Expand Down Expand Up @@ -785,37 +785,23 @@ impl Block {
height: u64,
) -> Result<(Output, TxKernel), keychain::Error> {
let commit = keychain.commit(reward(fees), key_id)?;
let switch_commit = keychain.switch_commit(key_id)?;
let switch_commit_hash =
SwitchCommitHash::from_switch_commit(switch_commit, keychain, key_id);

trace!(
LOGGER,
"Block reward - Pedersen Commit is: {:?}, Switch Commit is: {:?}",
"Block reward - Pedersen Commit is: {:?}",
commit,
switch_commit
);
trace!(
LOGGER,
"Block reward - Switch Commit Hash is: {:?}",
switch_commit_hash
);

let value = reward(fees);
let msg = (ProofMessageElements { value: value }).to_proof_message();

let rproof = keychain.range_proof(
value,
reward(fees),
key_id,
commit,
Some(switch_commit_hash.as_ref().to_vec()),
msg,
None,
)?;

let output = Output {
features: OutputFeatures::COINBASE_OUTPUT,
commit: commit,
switch_commit_hash: switch_commit_hash,
proof: rproof,
};

Expand Down Expand Up @@ -1059,7 +1045,7 @@ mod test {
let b = new_block(vec![], &keychain, &prev);
let mut vec = Vec::new();
ser::serialize(&mut vec, &b).expect("serialization failed");
let target_len = 1_248;
let target_len = 1_216;
assert_eq!(vec.len(), target_len,);
}

Expand All @@ -1071,8 +1057,8 @@ mod test {
let b = new_block(vec![&tx1], &keychain, &prev);
let mut vec = Vec::new();
ser::serialize(&mut vec, &b).expect("serialization failed");
let target_len = 2_892;
assert_eq!(vec.len(), target_len,);
let target_len = 2_796;
assert_eq!(vec.len(), target_len);
}

#[test]
Expand All @@ -1082,7 +1068,7 @@ mod test {
let b = new_block(vec![], &keychain, &prev);
let mut vec = Vec::new();
ser::serialize(&mut vec, &b.as_compact_block()).expect("serialization failed");
let target_len = 1_256;
let target_len = 1_224;
assert_eq!(vec.len(), target_len,);
}

Expand All @@ -1094,7 +1080,7 @@ mod test {
let b = new_block(vec![&tx1], &keychain, &prev);
let mut vec = Vec::new();
ser::serialize(&mut vec, &b.as_compact_block()).expect("serialization failed");
let target_len = 1_262;
let target_len = 1_230;
assert_eq!(vec.len(), target_len,);
}

Expand All @@ -1111,7 +1097,7 @@ mod test {
let b = new_block(txs.iter().collect(), &keychain, &prev);
let mut vec = Vec::new();
ser::serialize(&mut vec, &b).expect("serialization failed");
let target_len = 17_688;
let target_len = 17_016;
assert_eq!(vec.len(), target_len,);
}

Expand All @@ -1128,7 +1114,7 @@ mod test {
let b = new_block(txs.iter().collect(), &keychain, &prev);
let mut vec = Vec::new();
ser::serialize(&mut vec, &b.as_compact_block()).expect("serialization failed");
let target_len = 1_316;
let target_len = 1_284;
assert_eq!(vec.len(), target_len,);
}

Expand Down
20 changes: 3 additions & 17 deletions core/src/core/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@

use util::{kernel_sig_msg, secp};

use core::{Input, Output, OutputFeatures, ProofMessageElements, SwitchCommitHash, Transaction,
TxKernel};
use core::{Input, Output, OutputFeatures, Transaction, TxKernel};
use core::hash::Hash;
use core::pmmr::MerkleProof;
use keychain;
Expand Down Expand Up @@ -102,39 +101,26 @@ pub fn output(value: u64, key_id: Identifier) -> Box<Append> {
debug!(LOGGER, "Building an output: {}, {}", value, key_id,);

let commit = build.keychain.commit(value, &key_id).unwrap();
let switch_commit = build.keychain.switch_commit(&key_id).unwrap();
let switch_commit_hash =
SwitchCommitHash::from_switch_commit(switch_commit, build.keychain, &key_id);
trace!(
LOGGER,
"Builder - Pedersen Commit is: {:?}, Switch Commit is: {:?}",
"Builder - Pedersen Commit is: {:?}",
commit,
switch_commit,
);
trace!(
LOGGER,
"Builder - Switch Commit Hash is: {:?}",
switch_commit_hash
);

let msg = (ProofMessageElements { value: value }).to_proof_message();

let rproof = build
.keychain
.range_proof(
value,
&key_id,
commit,
Some(switch_commit_hash.as_ref().to_vec()),
msg,
None,
)
.unwrap();

(
tx.with_output(Output {
features: OutputFeatures::DEFAULT_OUTPUT,
commit: commit,
switch_commit_hash: switch_commit_hash,
proof: rproof,
}),
kern,
Expand Down
2 changes: 1 addition & 1 deletion core/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ mod test {
let tx = tx2i1o();
let mut vec = Vec::new();
ser::serialize(&mut vec, &tx).expect("serialization failed");
let target_len = 986;
let target_len = 954;
assert_eq!(vec.len(), target_len,);
}

Expand Down
1 change: 1 addition & 0 deletions core/src/core/pmmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ where
}
}

/// Prints PMMR statistics to the logs, used for debugging.
pub fn dump_stats(&self) {
debug!(LOGGER, "pmmr: unpruned - {}", self.unpruned_size());
self.backend.dump_stats();
Expand Down
Loading

0 comments on commit ca8447f

Please sign in to comment.