Skip to content

Commit

Permalink
chore: keeping spent transactions within Dbc in a set rather than a vec
Browse files Browse the repository at this point in the history
BREAKING CHANGE: changes in the API due to Dbc struct change, but also due
to moving GenesisMaterial to the mock/exmaple mod as it's not needed in public API.
  • Loading branch information
bochaco committed Jun 29, 2022
1 parent 7db1874 commit 861288a
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mock = [ ]
[dependencies]
bincode = "1.3.3"
blsttc = "6.0.0"
bls_ringct = "0.2.0"
bls_ringct = "0.2.1"
hex = "0.4.3"
thiserror = "1.0.24"

Expand Down
2 changes: 1 addition & 1 deletion benches/reissue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ fn generate_dbc_of_value(
let (mut spentbook_node, genesis_dbc, _genesis_material, _amount_secrets) =
mock::GenesisBuilder::init_genesis_single(rng)?;

let output_amounts = vec![amount, sn_dbc::GenesisMaterial::GENESIS_AMOUNT - amount];
let output_amounts = vec![amount, mock::GenesisMaterial::GENESIS_AMOUNT - amount];

let mut dbc_builder = sn_dbc::TransactionBuilder::default()
.set_require_all_decoys(false) // no decoys!
Expand Down
14 changes: 8 additions & 6 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ pub struct DbcBuilder {
pub ringct_material: RingCtMaterial,

pub spent_proof_shares: BTreeMap<KeyImage, HashSet<SpentProofShare>>,
pub spent_transactions: Vec<RingCtTransaction>,
pub spent_transactions: BTreeMap<Hash, RingCtTransaction>,
}

impl DbcBuilder {
Expand All @@ -352,7 +352,7 @@ impl DbcBuilder {
output_owner_map,
ringct_material,
spent_proof_shares: Default::default(),
spent_transactions: Vec::default(),
spent_transactions: Default::default(),
}
}

Expand Down Expand Up @@ -389,7 +389,10 @@ impl DbcBuilder {

/// Add a transaction which spent one of the inputs
pub fn add_spent_transaction(mut self, spent_tx: RingCtTransaction) -> Self {
self.spent_transactions.push(spent_tx);
let tx_hash = Hash::from(spent_tx.hash());
self.spent_transactions
.entry(tx_hash)
.or_insert_with(|| spent_tx);
self
}

Expand All @@ -410,8 +413,7 @@ impl DbcBuilder {
// verify there is a maching spent transaction for each spent_proof
if !spent_proofs.iter().all(|proof| {
self.spent_transactions
.iter()
.any(|tx| Hash::from(tx.hash()) == proof.transaction_hash())
.contains_key(&proof.transaction_hash())
}) {
return Err(Error::MissingSpentTransaction);
}
Expand Down Expand Up @@ -471,7 +473,7 @@ impl DbcBuilder {
)),
transaction: self.transaction.clone(),
spent_proofs: spent_proofs.clone(),
spent_transactions: self.spent_transactions.clone(),
spent_transactions: self.spent_transactions.values().cloned().collect(),
};
(dbc, owner_once.clone(), amount_secrets_list[0].clone())
})
Expand Down
9 changes: 4 additions & 5 deletions src/dbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub struct Dbc {
pub content: DbcContent,
pub transaction: RingCtTransaction,
pub spent_proofs: BTreeSet<SpentProof>,
pub spent_transactions: Vec<RingCtTransaction>,
pub spent_transactions: BTreeSet<RingCtTransaction>,
}

impl Dbc {
Expand Down Expand Up @@ -321,8 +321,7 @@ pub(crate) mod tests {
use crate::{
mock,
rand::{CryptoRng, RngCore},
Amount, AmountSecrets, DbcBuilder, GenesisMaterial, Hash, Owner, OwnerOnce,
SpentProofContent,
Amount, AmountSecrets, DbcBuilder, Hash, Owner, OwnerOnce, SpentProofContent,
};
use bls_ringct::{bls_bulletproofs::PedersenGens, ringct::RingCtMaterial, Output};
use blsttc::PublicKey;
Expand Down Expand Up @@ -702,7 +701,7 @@ pub(crate) mod tests {
}
}

let spent_transactions = dbc_builder.spent_transactions.clone();
let spent_transactions = dbc_builder.spent_transactions.values().cloned().collect();
let dbcs = dbc_builder.build(&spentbook_node.key_manager)?;
let (dbc_valid, ..) = &dbcs[0];

Expand Down Expand Up @@ -814,7 +813,7 @@ pub(crate) mod tests {
let (mut spentbook_node, genesis_dbc, _genesis_material, _amount_secrets) =
mock::GenesisBuilder::init_genesis_single(rng)?;

let output_amounts = vec![amount, GenesisMaterial::GENESIS_AMOUNT - amount];
let output_amounts = vec![amount, mock::GenesisMaterial::GENESIS_AMOUNT - amount];

let mut dbc_builder = crate::TransactionBuilder::default()
.set_require_all_decoys(false)
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ mod builder;
mod dbc;
mod dbc_content;
mod error;
mod genesis;
mod key_manager;
mod mint;
mod owner;
Expand Down Expand Up @@ -45,7 +44,6 @@ pub use crate::{
dbc::Dbc,
dbc_content::DbcContent,
error::{Error, Result},
genesis::GenesisMaterial,
key_manager::{IndexedSignatureShare, KeyManager, PublicKey, PublicKeySet, Signature},
owner::{DerivationIndex, Owner, OwnerOnce},
spent_proof::{SpentProof, SpentProofContent, SpentProofShare},
Expand Down
16 changes: 8 additions & 8 deletions src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ mod tests {
use std::iter::FromIterator;

use crate::{
mock, Amount, AmountSecrets, Dbc, DbcContent, Error, GenesisMaterial,
IndexedSignatureShare, KeyImage, Owner, OwnerOnce, Result, SpentProofContent,
SpentProofShare, TransactionBuilder,
mock, Amount, AmountSecrets, Dbc, DbcContent, Error, IndexedSignatureShare, KeyImage,
Owner, OwnerOnce, Result, SpentProofContent, SpentProofShare, TransactionBuilder,
};

#[test]
Expand All @@ -44,7 +43,7 @@ mod tests {
let mut output_amounts =
Vec::from_iter(output_amounts.into_iter().map(TinyInt::coerce::<Amount>));
output_amounts
.push(GenesisMaterial::GENESIS_AMOUNT - output_amounts.iter().sum::<Amount>());
.push(mock::GenesisMaterial::GENESIS_AMOUNT - output_amounts.iter().sum::<Amount>());

let n_outputs = output_amounts.len();
let output_amount = output_amounts.iter().sum();
Expand Down Expand Up @@ -146,12 +145,13 @@ mod tests {

let mut input_amounts =
Vec::from_iter(input_amounts.into_iter().map(TinyInt::coerce::<Amount>));
input_amounts.push(GenesisMaterial::GENESIS_AMOUNT - input_amounts.iter().sum::<Amount>());
input_amounts
.push(mock::GenesisMaterial::GENESIS_AMOUNT - input_amounts.iter().sum::<Amount>());

let mut output_amounts =
Vec::from_iter(output_amounts.into_iter().map(TinyInt::coerce::<Amount>));
output_amounts
.push(GenesisMaterial::GENESIS_AMOUNT - output_amounts.iter().sum::<Amount>());
.push(mock::GenesisMaterial::GENESIS_AMOUNT - output_amounts.iter().sum::<Amount>());

let invalid_spent_proofs = BTreeSet::from_iter(
invalid_spent_proofs
Expand Down Expand Up @@ -253,7 +253,7 @@ mod tests {
Error::RingCt(
bls_ringct::Error::InputPseudoCommitmentsDoNotSumToOutputCommitments,
) => {
if GenesisMaterial::GENESIS_AMOUNT == output_total_amount {
if mock::GenesisMaterial::GENESIS_AMOUNT == output_total_amount {
// This can correctly occur if there are 0 outputs and inputs sum to zero.
//
// The error occurs because there is no output with a commitment
Expand Down Expand Up @@ -333,7 +333,7 @@ mod tests {

match many_to_many_result {
Ok(output_dbcs) => {
assert_eq!(GenesisMaterial::GENESIS_AMOUNT, output_total_amount);
assert_eq!(mock::GenesisMaterial::GENESIS_AMOUNT, output_total_amount);
assert!(invalid_spent_proofs.iter().all(|i| i >= &tx2.mlsags.len()));

// The output amounts (from params) should correspond to the actual output_amounts
Expand Down
3 changes: 2 additions & 1 deletion src/mock/genesis_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use super::GenesisMaterial;
use crate::{
mock,
rand::{CryptoRng, RngCore},
Amount, AmountSecrets, Dbc, GenesisMaterial, KeyManager, Result, TransactionBuilder,
Amount, AmountSecrets, Dbc, KeyManager, Result, TransactionBuilder,
};
use blsttc::SecretKeySet;

Expand Down
29 changes: 14 additions & 15 deletions src/genesis.rs → src/mock/genesis_material.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
// Copyright 2022 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use crate::{Amount, KeyImage, Owner, OwnerOnce};
use bls_ringct::{
blstrs::Scalar,
Expand All @@ -17,31 +25,23 @@ pub struct GenesisMaterial {

impl GenesisMaterial {
/// The Genesis DBC will mint all possible tokens.
pub const GENESIS_AMOUNT: Amount = 18446744073709551615; // aka 2^64 aka Amount::MAX
pub const GENESIS_AMOUNT: Amount = Amount::MAX; // aka 2^64
}

impl Default for GenesisMaterial {
/// generate the GenesisMaterial.
///
/// It is allowed to pass in an amount for local testing purposes.
/// However, to participate on a public network (mainnet, testnet)
/// one must use GenesisMaterial::STD_GENESIS_AMOUNT
///
/// todo: implement Network enum {Mainnet, Testnet, ...}
/// It uses GenesisMaterial::GENESIS_AMOUNT by default
fn default() -> Self {
// Make a secret key for the input of Genesis Tx. (fictional Dbc)
// note that this represents the one-time-use key.
// (we have no need for the base key)
// The seed is an homage to bitcoin. block 0 timestamp (utc).
let input_sk_seed: u64 = 1231006505;
let input_sk_seed: u64 = 1234567890;
let input_sk = blsttc::SecretKey::from_mut(&mut input_sk_seed.into_fr());

// Make a secret key for the output of Genesis Tx. (The Genesis Dbc)
// note that this represents the base key, from which one-time-use key is derived.
// The seed is an homage to monero. block 1 timestamp (utc).
// We do not use the block 0 timestamp because it is 0 (1970) which is boring!
let output_sk_seed: u64 = 1397843393;
let output_sk = blsttc::SecretKey::from_mut(&mut output_sk_seed.into_fr());
let output_sk = blsttc::SecretKey::random();

// OwnerOnce ties together the base key and one-time-use key.
let output_owner_once = OwnerOnce {
Expand All @@ -58,7 +58,7 @@ impl Default for GenesisMaterial {
input_sk,
RevealedCommitment {
value: Self::GENESIS_AMOUNT,
blinding: 1776.into(), // freedom baby!
blinding: 1000.into(), // just a random number
},
);

Expand All @@ -75,7 +75,7 @@ impl Default for GenesisMaterial {
r: vec![(Scalar::default(), Scalar::default())],
};

// onward to RingCtMaterial
// build the genesis RingCtMaterial
let ringct_material = RingCtMaterial {
inputs: vec![mlsag_material],
outputs: vec![Output::new(
Expand All @@ -84,7 +84,6 @@ impl Default for GenesisMaterial {
)],
};

// Voila!
Self {
ringct_material,
owner_once: output_owner_once,
Expand Down
2 changes: 2 additions & 0 deletions src/mock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@

mod error;
mod genesis_builder;
mod genesis_material;
mod key_manager;
mod spentbook;

pub use self::{
error::Error,
genesis_builder::GenesisBuilder,
genesis_material::GenesisMaterial,
key_manager::{KeyManager, Signer},
spentbook::SpentBookNode,
};
4 changes: 2 additions & 2 deletions src/mock/spentbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ use bls_ringct::{
use blsttc::PublicKey;
use std::collections::{BTreeMap, HashMap};

use super::GenesisMaterial;
use crate::{
mock,
rand::{prelude::IteratorRandom, RngCore},
Commitment, GenesisMaterial, Hash, KeyImage, KeyManager, Result, SpentProofContent,
SpentProofShare,
Commitment, Hash, KeyImage, KeyManager, Result, SpentProofContent, SpentProofShare,
};

/// This is a mock SpentBook used for our test cases. A proper implementation
Expand Down

0 comments on commit 861288a

Please sign in to comment.