Skip to content

Commit

Permalink
feat: make SpendBook a trait so that implementer can decide how to st…
Browse files Browse the repository at this point in the history
…ore it
  • Loading branch information
dan-da committed Jun 24, 2021
1 parent 43eec72 commit 287a341
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 25 deletions.
12 changes: 9 additions & 3 deletions benches/reissue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ use std::iter::FromIterator;

use sn_dbc::{
bls_dkg_id, Dbc, DbcContent, Mint, ReissueRequest, ReissueTransaction, SimpleKeyManager,
SimpleSigner,
SimpleSigner, SimpleSpendBook,
};

use criterion::{black_box, criterion_group, criterion_main, Criterion};

fn genesis(amount: u64) -> (Mint<SimpleKeyManager>, bls_dkg::outcome::Outcome, Dbc) {
fn genesis(
amount: u64,
) -> (
Mint<SimpleKeyManager, SimpleSpendBook>,
bls_dkg::outcome::Outcome,
Dbc,
) {
let genesis_owner = bls_dkg_id();

let key_manager = SimpleKeyManager::new(
Expand All @@ -20,7 +26,7 @@ fn genesis(amount: u64) -> (Mint<SimpleKeyManager>, bls_dkg::outcome::Outcome, D
),
genesis_owner.public_key_set.public_key(),
);
let mut genesis_node = Mint::new(key_manager);
let mut genesis_node = Mint::new(key_manager, SimpleSpendBook::new());

let (content, transaction, (mint_key_set, mint_sig_share)) =
genesis_node.issue_genesis_dbc(amount).unwrap();
Expand Down
14 changes: 9 additions & 5 deletions examples/mint-repl/mint-repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use serde::{Deserialize, Serialize};
use sn_dbc::{
BlindedOwner, Dbc, DbcContent, DbcTransaction, Hash, Mint, MintSignatures, NodeSignature,
ReissueRequest, ReissueTransaction, SimpleKeyManager as KeyManager, SimpleSigner as Signer,
SimpleSpendBook as SpendBook,
};
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::iter::FromIterator;
Expand All @@ -29,15 +30,15 @@ use threshold_crypto::{
/// Holds information about the Mint, which may be comprised
/// of 1 or more nodes.
struct MintInfo {
mintnodes: Vec<Mint<KeyManager>>,
mintnodes: Vec<Mint<KeyManager, SpendBook>>,
genesis: DbcUnblinded,
secret_key_set: SecretKeySet,
poly: Poly,
}

impl MintInfo {
// returns the first mint node.
fn mintnode(&self) -> Result<&Mint<KeyManager>> {
fn mintnode(&self) -> Result<&Mint<KeyManager, SpendBook>> {
self.mintnodes
.get(0)
.ok_or_else(|| anyhow!("Mint not yet created"))
Expand Down Expand Up @@ -91,7 +92,10 @@ fn main() -> Result<()> {
continue;
};
let result = match cmd {
"newmint" => newmint().map(|_| ()),
"newmint" => {
mintinfo = newmint()?;
Ok(())
}
"mintinfo" => print_mintinfo_human(&mintinfo),
"prepare_tx" => prepare_tx(),
"sign_tx" => sign_tx(),
Expand Down Expand Up @@ -185,7 +189,7 @@ fn mk_new_random_mint(threshold: usize, amount: u64) -> Result<MintInfo> {
/// creates a new mint from an existing SecretKeySet that was seeded by poly.
fn mk_new_mint(secret_key_set: SecretKeySet, poly: Poly, amount: u64) -> Result<MintInfo> {
let genesis_pubkey = secret_key_set.public_keys().public_key();
let mut mints: Vec<Mint<KeyManager>> = Default::default();
let mut mints: Vec<Mint<KeyManager, SpendBook>> = Default::default();

// Generate each Mint node, and corresponding NodeSignature. (Index + SignatureShare)
let mut genesis_set: Vec<(DbcContent, DbcTransaction, (PublicKeySet, NodeSignature))> =
Expand All @@ -198,7 +202,7 @@ fn mk_new_mint(secret_key_set: SecretKeySet, poly: Poly, amount: u64) -> Result<
),
genesis_pubkey,
);
let mut mint = Mint::new(key_manager);
let mut mint = Mint::new(key_manager, SpendBook::new());
genesis_set.push(mint.issue_genesis_dbc(amount)?);
mints.push(mint);
}
Expand Down
3 changes: 2 additions & 1 deletion src/dbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ mod tests {
use crate::tests::{NonZeroTinyInt, TinyInt};
use crate::{
KeyManager, Mint, ReissueRequest, ReissueTransaction, SimpleKeyManager, SimpleSigner,
SimpleSpendBook,
};

fn divide(amount: u64, n_ways: u8) -> impl Iterator<Item = u64> {
Expand Down Expand Up @@ -173,7 +174,7 @@ mod tests {
),
genesis_owner.public_key_set.public_key(),
);
let mut genesis_node = Mint::new(key_manager);
let mut genesis_node = Mint::new(key_manager, SimpleSpendBook::new());

let (gen_dbc_content, gen_dbc_trans, (gen_key_set, gen_node_sig)) =
genesis_node.issue_genesis_dbc(amount).unwrap();
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ pub use crate::{
KeyManager, NodeSignature, PublicKey, PublicKeySet, Signature, SimpleKeyManager,
SimpleSigner,
},
mint::{Mint, MintSignatures, ReissueRequest, ReissueTransaction, GENESIS_DBC_INPUT},
mint::{
Mint, MintSignatures, ReissueRequest, ReissueTransaction, SimpleSpendBook, SpendBook,
GENESIS_DBC_INPUT,
},
};

impl From<[u8; 32]> for Hash {
Expand Down
53 changes: 38 additions & 15 deletions src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ pub type MintSignatures = BTreeMap<DbcContentHash, (PublicKeySet, NodeSignature)

pub const GENESIS_DBC_INPUT: Hash = Hash([0u8; 32]);

pub trait SpendBook: std::fmt::Debug + Clone + IntoIterator {
fn lookup(&self, dbc_hash: &DbcContentHash) -> Option<&DbcTransaction>;
fn log(&mut self, dbc_hash: DbcContentHash, transaction: DbcTransaction);
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct SpendBook {
pub struct SimpleSpendBook {
pub transactions: BTreeMap<DbcContentHash, DbcTransaction>,
}

impl SpendBook {
impl SpendBook for SimpleSpendBook {
fn lookup(&self, dbc_hash: &DbcContentHash) -> Option<&DbcTransaction> {
self.transactions.get(dbc_hash)
}
Expand All @@ -42,6 +47,23 @@ impl SpendBook {
}
}

impl IntoIterator for SimpleSpendBook {
type Item = (DbcContentHash, DbcTransaction);
type IntoIter = std::collections::btree_map::IntoIter<DbcContentHash, DbcTransaction>;

fn into_iter(self) -> Self::IntoIter {
self.transactions.into_iter()
}
}

impl SimpleSpendBook {
pub fn new() -> Self {
Self {
transactions: Default::default(),
}
}
}

#[derive(Eq, PartialEq, Debug, Clone, Deserialize, Serialize)]
pub struct ReissueTransaction {
pub inputs: HashSet<Dbc>,
Expand Down Expand Up @@ -122,20 +144,21 @@ pub struct ReissueRequest {
HashMap<DbcContentHash, (threshold_crypto::PublicKey, threshold_crypto::Signature)>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Mint<K>
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Mint<K, S>
where
K: KeyManager,
S: SpendBook,
{
pub(crate) key_manager: K,
pub spendbook: SpendBook,
pub spendbook: S,
}

impl<K: KeyManager> Mint<K> {
pub fn new(key_manager: K) -> Self {
impl<K: KeyManager, S: SpendBook> Mint<K, S> {
pub fn new(key_manager: K, spendbook: S) -> Self {
Self {
key_manager,
spendbook: Default::default(),
spendbook,
}
}

Expand Down Expand Up @@ -261,12 +284,12 @@ impl<K: KeyManager> Mint<K> {
}

// Used in testing / benchmarking
pub fn snapshot_spendbook(&self) -> SpendBook {
pub fn snapshot_spendbook(&self) -> S {
self.spendbook.clone()
}

// Used in testing / benchmarking
pub fn reset_spendbook(&mut self, spendbook: SpendBook) {
pub fn reset_spendbook(&mut self, spendbook: S) {
self.spendbook = spendbook
}
}
Expand All @@ -293,7 +316,7 @@ mod tests {
),
genesis_owner.public_key_set.public_key(),
);
let mut genesis_node = Mint::new(key_manager);
let mut genesis_node = Mint::new(key_manager, SimpleSpendBook::new());

let (gen_dbc_content, gen_dbc_trans, (gen_key_set, gen_node_sig)) =
genesis_node.issue_genesis_dbc(1000).unwrap();
Expand Down Expand Up @@ -330,7 +353,7 @@ mod tests {
),
genesis_owner.public_key_set.public_key(),
);
let mut genesis_node = Mint::new(key_manager.clone());
let mut genesis_node = Mint::new(key_manager.clone(), SimpleSpendBook::new());

let (gen_dbc_content, gen_dbc_trans, (gen_key_set, gen_node_sig)) =
genesis_node.issue_genesis_dbc(output_amount).unwrap();
Expand Down Expand Up @@ -437,7 +460,7 @@ mod tests {
),
genesis_owner.public_key_set.public_key(),
);
let mut genesis_node = Mint::new(key_manager);
let mut genesis_node = Mint::new(key_manager, SimpleSpendBook::new());

let (gen_dbc_content, gen_dbc_trans, (gen_key_set, gen_node_sig)) =
genesis_node.issue_genesis_dbc(1000).unwrap();
Expand Down Expand Up @@ -571,7 +594,7 @@ mod tests {
),
genesis_owner.public_key_set.public_key(),
);
let mut genesis_node = Mint::new(key_manager);
let mut genesis_node = Mint::new(key_manager, SimpleSpendBook::new());

let genesis_amount: u64 = input_amounts.iter().sum();
let (gen_dbc_content, gen_dbc_trans, (_gen_key, gen_node_sig)) =
Expand Down Expand Up @@ -855,7 +878,7 @@ mod tests {
),
genesis_owner.public_key_set.public_key(),
);
let mut genesis_node = Mint::new(key_manager);
let mut genesis_node = Mint::new(key_manager, SimpleSpendBook::new());

let input_owner = crate::bls_dkg_id();
let input_content = DbcContent::new(
Expand Down

0 comments on commit 287a341

Please sign in to comment.