Skip to content

Commit

Permalink
refactor: consider first logged tx to be genesis
Browse files Browse the repository at this point in the history
* makes genesis KeyImage optional in SpentBookMock.
* if genesis key_image not present, first logged tx becomes genesis
* modifies test cases to be a bit cleaner using this facility
  • Loading branch information
dan-da authored and dirvine committed Feb 17, 2022
1 parent c2bce8b commit 5dea527
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 24 deletions.
9 changes: 4 additions & 5 deletions src/dbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,17 +293,16 @@ mod tests {

let amount = 100;

let spentbook_key_manager =
SimpleKeyManager::new(SimpleSigner::from(crate::bls_dkg_id(&mut rng)));
let mut spentbook = SpentBookMock::from(SimpleKeyManager::new(SimpleSigner::from(
crate::bls_dkg_id(&mut rng),
)));

let (mint_node, genesis) = MintNode::new(SimpleKeyManager::new(SimpleSigner::from(
crate::bls_dkg_id(&mut rng),
)))
.trust_spentbook_public_key(spentbook_key_manager.public_key_set()?.public_key())?
.trust_spentbook_public_key(spentbook.key_manager.public_key_set()?.public_key())?
.issue_genesis_dbc(amount, &mut rng8)?;

let mut spentbook = SpentBookMock::from((spentbook_key_manager, genesis.input_key_image));

let _genesis_spent_proof_share =
spentbook.log_spent(genesis.input_key_image, genesis.transaction.clone())?;

Expand Down
29 changes: 25 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,17 +281,27 @@ mod tests {
pub struct SpentBookMock {
pub key_manager: SimpleKeyManager,
pub transactions: BTreeMap<KeyImage, RingCtTransaction>,
pub genesis_input_key_image: KeyImage,
pub genesis_input_key_image: Option<KeyImage>,
}

impl From<SimpleKeyManager> for SpentBookMock {
fn from(key_manager: SimpleKeyManager) -> Self {
Self {
key_manager,
transactions: Default::default(),
genesis_input_key_image: None,
}
}
}

impl From<(SimpleKeyManager, KeyImage)> for SpentBookMock {
fn from(params: (SimpleKeyManager, KeyImage)) -> Self {
let (key_manager, genesis_input_key_image) = params;
let (key_manager, key_image) = params;

Self {
key_manager,
transactions: Default::default(),
genesis_input_key_image,
genesis_input_key_image: Some(key_image),
}
}
}
Expand All @@ -316,8 +326,15 @@ mod tests {
let spentbook_pks = self.key_manager.public_key_set()?;
let spentbook_sig_share = self.key_manager.sign(&tx_hash)?;

// If this is the very first tx logged and genesis key_image was not
// provided, then it becomes the genesis tx.
let genesis_input_key_image = match self.genesis_input_key_image {
Some(k) => k,
None => key_image,
};

// public_commitments should only be empty for the genesis transaction.
let public_commitments: Vec<G1Affine> = if key_image == self.genesis_input_key_image {
let public_commitments: Vec<G1Affine> = if key_image == genesis_input_key_image {
vec![]
} else {
// Todo: make this cleaner and more efficient.
Expand Down Expand Up @@ -363,6 +380,10 @@ mod tests {
.entry(key_image)
.or_insert_with(|| tx.clone());
if existing_tx.hash() == tx.hash() {
if self.genesis_input_key_image.is_none() {
self.genesis_input_key_image = Some(genesis_input_key_image);
}

Ok(SpentProofShare {
key_image,
spentbook_pks,
Expand Down
27 changes: 12 additions & 15 deletions src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,17 +321,16 @@ mod tests {
let mut rng8 = rand8::rngs::StdRng::from_seed([0u8; 32]);
let mut rng = rand::rngs::StdRng::from_seed([0u8; 32]);

let spentbook_key_manager =
SimpleKeyManager::new(SimpleSigner::from(crate::bls_dkg_id(&mut rng)));
let mut spentbook = SpentBookMock::from(SimpleKeyManager::new(SimpleSigner::from(
crate::bls_dkg_id(&mut rng),
)));

let (mint_node, genesis) = MintNode::new(SimpleKeyManager::new(SimpleSigner::from(
crate::bls_dkg_id(&mut rng),
)))
.trust_spentbook_public_key(spentbook_key_manager.public_key_set()?.public_key())?
.trust_spentbook_public_key(spentbook.key_manager.public_key_set()?.public_key())?
.issue_genesis_dbc(1000, &mut rng8)?;

let mut spentbook = SpentBookMock::from((spentbook_key_manager, genesis.input_key_image));

let mint_sig = mint_node
.key_manager()
.public_key_set()?
Expand Down Expand Up @@ -392,17 +391,16 @@ mod tests {
let n_outputs = output_amounts.len();
let output_amount = output_amounts.iter().sum();

let spentbook_key_manager =
SimpleKeyManager::new(SimpleSigner::from(crate::bls_dkg_id(&mut rng)));
let mut spentbook = SpentBookMock::from(SimpleKeyManager::new(SimpleSigner::from(
crate::bls_dkg_id(&mut rng),
)));

let (mint_node, genesis) = MintNode::new(SimpleKeyManager::new(SimpleSigner::from(
crate::bls_dkg_id(&mut rng),
)))
.trust_spentbook_public_key(spentbook_key_manager.public_key_set()?.public_key())?
.trust_spentbook_public_key(spentbook.key_manager.public_key_set()?.public_key())?
.issue_genesis_dbc(output_amount, &mut rng8)?;

let mut spentbook = SpentBookMock::from((spentbook_key_manager, genesis.input_key_image));

let _genesis_spent_proof_share =
spentbook.log_spent(genesis.input_key_image, genesis.transaction.clone())?;

Expand Down Expand Up @@ -533,17 +531,16 @@ mod tests {
// something like: genesis --> 100 outputs --> x outputs --> y outputs.
let num_decoy_inputs: usize = num_decoy_inputs.coerce::<usize>() % 2;

let spentbook_key_manager =
SimpleKeyManager::new(SimpleSigner::from(crate::bls_dkg_id(&mut rng)));
let mut spentbook = SpentBookMock::from(SimpleKeyManager::new(SimpleSigner::from(
crate::bls_dkg_id(&mut rng),
)));

let (mint_node, genesis) = MintNode::new(SimpleKeyManager::new(SimpleSigner::from(
crate::bls_dkg_id(&mut rng),
)))
.trust_spentbook_public_key(spentbook_key_manager.public_key_set()?.public_key())?
.trust_spentbook_public_key(spentbook.key_manager.public_key_set()?.public_key())?
.issue_genesis_dbc(genesis_amount, &mut rng8)?;

let mut spentbook = SpentBookMock::from((spentbook_key_manager, genesis.input_key_image));

let _genesis_spent_proof_share =
spentbook.log_spent(genesis.input_key_image, genesis.transaction.clone())?;

Expand Down

0 comments on commit 5dea527

Please sign in to comment.