From 3a608aa93da224e604a9a95359b64732a68f202f Mon Sep 17 00:00:00 2001 From: grumbach Date: Mon, 15 May 2023 18:15:47 +0900 Subject: [PATCH] feat: rename spends with better names BREAKING CHANGE: renames fields and methods in Spend --- benches/reissue.rs | 18 +++++++++--------- src/builder.rs | 26 +++++++++++++------------- src/dbc.rs | 2 +- src/error.rs | 4 ++-- src/mock/genesis_builder.rs | 2 +- src/mock/mock_spentbook.rs | 18 +++++++++--------- src/signed_spend.rs | 33 +++++++++++++++++++-------------- src/spentbook.rs | 22 +++++++++++----------- src/verification.rs | 30 +++++++++++++++--------------- 9 files changed, 80 insertions(+), 75 deletions(-) diff --git a/benches/reissue.rs b/benches/reissue.rs index aa38bf9..b8975cd 100644 --- a/benches/reissue.rs +++ b/benches/reissue.rs @@ -39,9 +39,9 @@ fn bench_reissue_1_to_100(c: &mut Criterion) { .build(Hash::default(), &mut rng) .unwrap(); - let dst_tx = &dbc_builder.dst_tx; + let spent_tx = &dbc_builder.spent_tx; for signed_spend in dbc_builder.signed_spends() { - spentbook_node.log_spent(dst_tx, signed_spend).unwrap(); + spentbook_node.log_spent(spent_tx, signed_spend).unwrap(); } let signed_spends: BTreeSet<_> = dbc_builder.signed_spends().into_iter().cloned().collect(); @@ -51,7 +51,7 @@ fn bench_reissue_1_to_100(c: &mut Criterion) { let guard = pprof::ProfilerGuard::new(100).unwrap(); b.iter(|| { - TransactionVerifier::verify(black_box(dst_tx), &signed_spends).unwrap(); + TransactionVerifier::verify(black_box(spent_tx), &signed_spends).unwrap(); }); #[cfg(unix)] @@ -98,9 +98,9 @@ fn bench_reissue_100_to_1(c: &mut Criterion) { .build(Hash::default(), &mut rng) .unwrap(); - let dst_tx = dbc_builder.dst_tx.clone(); + let spent_tx = dbc_builder.spent_tx.clone(); for signed_spend in dbc_builder.signed_spends() { - spentbook_node.log_spent(&dst_tx, signed_spend).unwrap(); + spentbook_node.log_spent(&spent_tx, signed_spend).unwrap(); } let dbcs = dbc_builder.build().unwrap(); @@ -126,10 +126,10 @@ fn bench_reissue_100_to_1(c: &mut Criterion) { .build(Hash::default(), &mut rng) .unwrap(); - let merge_dst_tx = merge_dbc_builder.dst_tx.clone(); + let merge_spent_tx = merge_dbc_builder.spent_tx.clone(); for signed_spend in merge_dbc_builder.signed_spends() { spentbook_node - .log_spent(&merge_dst_tx, signed_spend) + .log_spent(&merge_spent_tx, signed_spend) .unwrap(); } @@ -144,7 +144,7 @@ fn bench_reissue_100_to_1(c: &mut Criterion) { let guard = pprof::ProfilerGuard::new(100).unwrap(); b.iter(|| { - TransactionVerifier::verify(black_box(&merge_dst_tx), &signed_spends).unwrap(); + TransactionVerifier::verify(black_box(&merge_spent_tx), &signed_spends).unwrap(); }); #[cfg(unix)] @@ -186,7 +186,7 @@ fn generate_dbc_of_value( })) .build(Hash::default(), rng)?; - let tx = dbc_builder.dst_tx.clone(); + let tx = dbc_builder.spent_tx.clone(); for signed_spend in dbc_builder.signed_spends() { spentbook_node.log_spent(&tx, signed_spend)?; } diff --git a/src/builder.rs b/src/builder.rs index 70b26e2..dd92ad1 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -122,9 +122,9 @@ impl TransactionBuilder { /// Build the DbcTransaction by signing the inputs, /// and generating the blinded outputs. Return a DbcBuilder. pub fn build(self, reason: Hash, rng: impl RngCore + CryptoRng) -> Result { - let (dst_tx, revealed_outputs) = self.revealed_tx.sign(rng)?; + let (spent_tx, revealed_outputs) = self.revealed_tx.sign(rng)?; - let signed_spends: BTreeSet<_> = dst_tx + let signed_spends: BTreeSet<_> = spent_tx .inputs .iter() .flat_map(|input| { @@ -135,10 +135,10 @@ impl TransactionBuilder { .map(|i| { let spend = crate::Spend { dbc_id: input.dbc_id(), - dst_tx: dst_tx.clone(), + spent_tx: spent_tx.clone(), reason, blinded_amount: input.blinded_amount, - src_tx_hash: i.input_src_tx.hash(), + dbc_creation_tx_hash: i.input_src_tx.hash(), }; let derived_key_sig = i.input.derived_key.sign(&spend.to_bytes()); SignedSpend { @@ -150,7 +150,7 @@ impl TransactionBuilder { .collect(); Ok(DbcBuilder::new( - dst_tx, + spent_tx, revealed_outputs, self.output_id_sources, self.revealed_tx, @@ -163,7 +163,7 @@ impl TransactionBuilder { #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Debug, Clone)] pub struct DbcBuilder { - pub dst_tx: DbcTransaction, + pub spent_tx: DbcTransaction, pub revealed_outputs: Vec, pub output_id_sources: OutputIdSources, pub revealed_tx: RevealedTx, @@ -173,14 +173,14 @@ pub struct DbcBuilder { impl DbcBuilder { /// Create a new DbcBuilder. pub fn new( - dst_tx: DbcTransaction, + spent_tx: DbcTransaction, revealed_outputs: Vec, output_id_sources: OutputIdSources, revealed_tx: RevealedTx, signed_spends: BTreeSet, ) -> Self { Self { - dst_tx, + spent_tx, revealed_outputs, output_id_sources, revealed_tx, @@ -189,7 +189,7 @@ impl DbcBuilder { } /// Return the signed spends. They each already contain the - /// dst_tx, so the inclusion of it in the result is just for convenience. + /// spent_tx, so the inclusion of it in the result is just for convenience. pub fn signed_spends(&self) -> Vec<&SignedSpend> { self.signed_spends.iter().collect() } @@ -201,7 +201,7 @@ impl DbcBuilder { pub fn build(self) -> Result> { // Verify the tx, along with signed spends. // Note that we do this just once for entire tx, not once per output Dbc. - TransactionVerifier::verify(&self.dst_tx, &self.signed_spends)?; + TransactionVerifier::verify(&self.spent_tx, &self.signed_spends)?; // Build output Dbcs. self.build_output_dbcs() @@ -223,7 +223,7 @@ impl DbcBuilder { .collect(); let dbc_id_list: Vec<&DbcIdSource> = self - .dst_tx + .spent_tx .outputs .iter() .map(|output| { @@ -235,7 +235,7 @@ impl DbcBuilder { // Form the final output DBCs let output_dbcs: Vec<(Dbc, RevealedAmount)> = self - .dst_tx + .spent_tx .outputs .iter() .zip(dbc_id_list) @@ -254,7 +254,7 @@ impl DbcBuilder { )); let dbc = Dbc { id: dbc_id_src.dbc_id(), - src_tx: self.dst_tx.clone(), + src_tx: self.spent_tx.clone(), ciphers, signed_spends: self.signed_spends.clone(), }; diff --git a/src/dbc.rs b/src/dbc.rs index eb04d35..515a494 100644 --- a/src/dbc.rs +++ b/src/dbc.rs @@ -453,7 +453,7 @@ pub(crate) mod tests { })) .build(Hash::default(), rng)?; - let tx = &dbc_builder.dst_tx; + let tx = &dbc_builder.spent_tx; for signed_spend in dbc_builder.signed_spends() { spentbook_node.log_spent(tx, signed_spend)? } diff --git a/src/error.rs b/src/error.rs index 9d1f754..1e50df4 100644 --- a/src/error.rs +++ b/src/error.rs @@ -38,10 +38,10 @@ pub enum Error { #[error("Transaction hash does not match the transaction signed by spentbook.")] InvalidTransactionHash, - #[error("Missing a src transaction {src_tx_hash:?} of a signed spend {dbc_id:?}.")] + #[error("Missing a src transaction {dbc_creation_tx_hash:?} of a signed spend {dbc_id:?}.")] MissingSpentSrcTransaction { dbc_id: DbcId, - src_tx_hash: crate::Hash, + dbc_creation_tx_hash: crate::Hash, }, #[error("Dbc ciphers are not present in transaction outputs.")] diff --git a/src/mock/genesis_builder.rs b/src/mock/genesis_builder.rs index b467ca1..ba2ef17 100644 --- a/src/mock/genesis_builder.rs +++ b/src/mock/genesis_builder.rs @@ -58,7 +58,7 @@ impl GenesisBuilder { ) .build(Hash::default(), rng)?; - let tx = &dbc_builder.dst_tx; + let tx = &dbc_builder.spent_tx; for signed_spend in dbc_builder.signed_spends() { for spentbook_node in self.spentbook_nodes.iter_mut() { spentbook_node.log_spent(tx, signed_spend)?; diff --git a/src/mock/mock_spentbook.rs b/src/mock/mock_spentbook.rs index d450885..0b5ca6f 100644 --- a/src/mock/mock_spentbook.rs +++ b/src/mock/mock_spentbook.rs @@ -94,23 +94,23 @@ impl SpentbookNode { #[cfg(test)] pub fn log_spent_and_skip_tx_verification( &mut self, - dst_tx: &DbcTransaction, + spent_tx: &DbcTransaction, signed_spend: &SignedSpend, ) -> Result<()> { - self.log_spent_worker(dst_tx, signed_spend, false) + self.log_spent_worker(spent_tx, signed_spend, false) } fn log_spent_worker( &mut self, - dst_tx: &DbcTransaction, + spent_tx: &DbcTransaction, signed_spend: &SignedSpend, verify_tx: bool, ) -> Result<()> { let input_id = signed_spend.dbc_id(); - let dst_tx_hash = signed_spend.dst_tx_hash(); - let tx_hash = dst_tx.hash(); + let spent_tx_hash = signed_spend.spent_tx_hash(); + let tx_hash = spent_tx.hash(); - if tx_hash != dst_tx_hash { + if tx_hash != spent_tx_hash { return Err(Error::InvalidTransactionHash); } @@ -123,7 +123,7 @@ impl SpentbookNode { if input_id == genesis_dbc_id { vec![(*input_id, *genesis_blinded_amount)] } else { - dst_tx + spent_tx .inputs .iter() .map(|input| { @@ -147,7 +147,7 @@ impl SpentbookNode { if verify_tx { // Do not permit invalid tx to be logged. - dst_tx.verify(&tx_blinded_amounts)?; + spent_tx.verify(&tx_blinded_amounts)?; } // Add dbc_id:tx_hash to dbc_id index. @@ -158,7 +158,7 @@ impl SpentbookNode { let existing_tx = self .transactions .entry(tx_hash) - .or_insert_with(|| dst_tx.clone()); + .or_insert_with(|| spent_tx.clone()); // Add dbc_id:blinded_output to dbc_id index. for output in existing_tx.outputs.iter() { diff --git a/src/signed_spend.rs b/src/signed_spend.rs index dcd0e97..13af623 100644 --- a/src/signed_spend.rs +++ b/src/signed_spend.rs @@ -29,14 +29,19 @@ impl SignedSpend { &self.spend.dbc_id } - /// Get dst transaction hash. - pub fn dst_tx_hash(&self) -> Hash { - self.spend.dst_tx.hash() + /// Get the hash of the transaction this DBC is spent in + pub fn spent_tx_hash(&self) -> Hash { + self.spend.spent_tx.hash() } - /// Get src transaction hash. - pub fn src_tx_hash(&self) -> Hash { - self.spend.src_tx_hash + /// Get the transaction this DBC is spent in + pub fn spent_tx(&self) -> DbcTransaction { + self.spend.spent_tx.clone() + } + + /// Get the hash of the transaction this DBC was created in + pub fn dbc_creation_tx_hash(&self) -> Hash { + self.spend.dbc_creation_tx_hash } /// Get blinded amount. @@ -59,12 +64,12 @@ impl SignedSpend { /// Verify this SignedSpend /// - /// Checks that the provided dst_tx_hash equals the input dst tx hash that was + /// Checks that the provided spent_tx_hash equals the input dst tx hash that was /// signed by the DerivedKey. Also verifies that that signature is /// valid for this SignedSpend. - pub fn verify(&self, dst_tx_hash: Hash) -> Result<()> { - // Verify that input dst_tx_hash matches self.dst_tx_hash which was signed by the DerivedKey of the input. - if dst_tx_hash != self.dst_tx_hash() { + pub fn verify(&self, spent_tx_hash: Hash) -> Result<()> { + // Verify that input spent_tx_hash matches self.spent_tx_hash which was signed by the DerivedKey of the input. + if spent_tx_hash != self.spent_tx_hash() { return Err(Error::InvalidTransactionHash); } @@ -106,14 +111,14 @@ pub struct Spend { pub dbc_id: DbcId, /// The transaction that the input Dbc is being spent in. #[debug(skip)] - pub dst_tx: DbcTransaction, + pub spent_tx: DbcTransaction, /// Reason why this Dbc was spent. pub reason: Hash, /// The amount of the input Dbc. #[debug(skip)] pub blinded_amount: BlindedAmount, /// The hash of the transaction that the input Dbc was created in. - pub src_tx_hash: Hash, + pub dbc_creation_tx_hash: Hash, } impl Spend { @@ -121,10 +126,10 @@ impl Spend { pub fn to_bytes(&self) -> Vec { let mut bytes: Vec = Default::default(); bytes.extend(self.dbc_id.to_bytes()); - bytes.extend(self.dst_tx.hash().as_ref()); + bytes.extend(self.spent_tx.hash().as_ref()); bytes.extend(self.reason.as_ref()); bytes.extend(self.blinded_amount.compress().to_bytes()); - bytes.extend(self.src_tx_hash.as_ref()); + bytes.extend(self.dbc_creation_tx_hash.as_ref()); bytes } diff --git a/src/spentbook.rs b/src/spentbook.rs index 23d7e5a..e7e456f 100644 --- a/src/spentbook.rs +++ b/src/spentbook.rs @@ -86,7 +86,7 @@ mod tests { } }; - let tx = &dbc_builder.dst_tx; + let tx = &dbc_builder.spent_tx; for signed_spend in dbc_builder.signed_spends() { match spentbook_node.log_spent(tx, signed_spend) { Ok(s) => s, @@ -182,7 +182,7 @@ mod tests { } }; - let tx1 = dbc_builder.dst_tx.clone(); + let tx1 = dbc_builder.spent_tx.clone(); for signed_spend in dbc_builder.signed_spends() { // normally spentbook verifies the tx, but here we skip it in order check reissue results. match spentbook_node.log_spent_and_skip_tx_verification(&tx1, signed_spend) { @@ -227,10 +227,10 @@ mod tests { let dbc_output_amounts = first_output_amounts.clone(); let output_total_amount: u64 = dbc_output_amounts.iter().sum(); - assert_eq!(second_inputs_dbcs_len, dbc_builder.dst_tx.inputs.len()); + assert_eq!(second_inputs_dbcs_len, dbc_builder.spent_tx.inputs.len()); assert_eq!(second_inputs_dbcs_len, dbc_builder.signed_spends().len()); - let tx2 = dbc_builder.dst_tx.clone(); + let tx2 = dbc_builder.spent_tx.clone(); // note: we make this a closure because the logic is needed in // a couple places. @@ -274,7 +274,7 @@ mod tests { Ok(()) }; - let tx = &dbc_builder.dst_tx; + let tx = &dbc_builder.spent_tx; for (i, signed_spend) in dbc_builder.signed_spends().into_iter().enumerate() { let is_invalid_signed_spend = invalid_signed_spends.contains(&i); @@ -292,10 +292,10 @@ mod tests { SignedSpend { spend: Spend { dbc_id: *signed_spend.dbc_id(), - dst_tx: signed_spend.spend.dst_tx.clone(), + spent_tx: signed_spend.spend.spent_tx.clone(), reason: Hash::default(), blinded_amount: *signed_spend.blinded_amount(), - src_tx_hash: tx1.hash(), + dbc_creation_tx_hash: tx1.hash(), }, derived_key_sig: SecretKey::random().sign([0u8; 32]), } @@ -405,7 +405,7 @@ mod tests { .add_output(Token::from_nano(b_output_amount), b_output_dbc_id_src) .build(Hash::default(), &mut rng)?; - let tx = &dbc_builder.dst_tx; + let tx = &dbc_builder.spent_tx; for signed_spend in dbc_builder.signed_spends() { spentbook_node.log_spent(tx, signed_spend)?; } @@ -490,7 +490,7 @@ mod tests { // being spent (i.e. is an input now), has a different amount of 2000. // ---------- - let fudged_tx = &dbc_builder_fudged.dst_tx; + let fudged_tx = &dbc_builder_fudged.spent_tx; for signed_spend in dbc_builder_fudged.signed_spends() { match spentbook_node.log_spent(fudged_tx, signed_spend) { @@ -537,7 +537,7 @@ mod tests { .build(Hash::default(), &mut rng)?; let dbc_builder_bad_spend = dbc_builder.clone(); - let bad_tx = &dbc_builder_bad_spend.dst_tx; + let bad_tx = &dbc_builder_bad_spend.spent_tx; for signed_spend in dbc_builder_bad_spend.signed_spends() { let result = spentbook_node.log_spent_and_skip_tx_verification(bad_tx, signed_spend); // The builder should return an error because the SignedSpend does not match the tx. @@ -571,7 +571,7 @@ mod tests { new_spentbook_node.log_spent(&a_dbc.src_tx, a_dbc.signed_spends.first().unwrap())?; new_spentbook_node.log_spent(&b_dbc.src_tx, b_dbc.signed_spends.first().unwrap())?; - let tx = &dbc_builder.dst_tx; + let tx = &dbc_builder.spent_tx; for signed_spend in dbc_builder.signed_spends() { new_spentbook_node.log_spent(tx, signed_spend)?; } diff --git a/src/verification.rs b/src/verification.rs index a13c3fc..9de9b8c 100644 --- a/src/verification.rs +++ b/src/verification.rs @@ -29,32 +29,32 @@ impl TransactionVerifier { /// trustless/verified way. I.e., the caller should not simply obtain a /// spend from a single peer, but must get the same spend from all in the close group. pub fn verify( - dst_tx: &DbcTransaction, + spent_tx: &DbcTransaction, signed_spends: &BTreeSet, ) -> Result<(), Error> { if signed_spends.is_empty() { return Err(transaction::Error::MissingTxInputs)?; } - if signed_spends.len() != dst_tx.inputs.len() { + if signed_spends.len() != spent_tx.inputs.len() { return Err(Error::SignedSpendInputLenMismatch { current: signed_spends.len(), - expected: dst_tx.inputs.len(), + expected: spent_tx.inputs.len(), }); } - let dst_tx_hash = dst_tx.hash(); + let spent_tx_hash = spent_tx.hash(); // Verify that each pubkey is unique in this transaction. let unique_dbc_ids: BTreeSet = - dst_tx.outputs.iter().map(|o| (*o.dbc_id())).collect(); - if unique_dbc_ids.len() != dst_tx.outputs.len() { + spent_tx.outputs.iter().map(|o| (*o.dbc_id())).collect(); + if unique_dbc_ids.len() != spent_tx.outputs.len() { return Err(Error::DbcIdNotUniqueAcrossOutputs); } // Verify that each input has a corresponding signed spend. for signed_spend in signed_spends.iter() { - if !dst_tx + if !spent_tx .inputs .iter() .any(|m| m.dbc_id == *signed_spend.dbc_id()) @@ -65,7 +65,7 @@ impl TransactionVerifier { // Verify that each signed spend is valid for signed_spend in signed_spends.iter() { - signed_spend.verify(dst_tx_hash)?; + signed_spend.verify(spent_tx_hash)?; } // We must get the signed spends into the same order as inputs @@ -74,7 +74,7 @@ impl TransactionVerifier { let mut signed_spends_found: Vec<(usize, &SignedSpend)> = signed_spends .iter() .filter_map(|s| { - dst_tx + spent_tx .inputs .iter() .position(|m| m.dbc_id == *s.dbc_id()) @@ -91,7 +91,7 @@ impl TransactionVerifier { .map(|s| *s.blinded_amount()) .collect(); - dst_tx.verify(&blinded_amounts)?; + spent_tx.verify(&blinded_amounts)?; Ok(()) } @@ -102,29 +102,29 @@ impl TransactionVerifier { /// In the process of doing so, we verify the correct set of signed /// spends and transactions have been provided. pub fn get_blinded_amounts_from_transaction( - dst_tx: &DbcTransaction, + spent_tx: &DbcTransaction, input_signed_spends: &BTreeSet, spent_src_transactions: &BTreeMap, ) -> Result> { // Get txs that are referenced by the signed spends. let mut referenced_spent_txs: Vec<_> = vec![]; for input in input_signed_spends { - if let Some(src_tx) = spent_src_transactions.get(&input.spend.src_tx_hash) { - if src_tx.hash() == input.spend.src_tx_hash { + if let Some(src_tx) = spent_src_transactions.get(&input.spend.dbc_creation_tx_hash) { + if src_tx.hash() == input.spend.dbc_creation_tx_hash { referenced_spent_txs.push(src_tx); continue; } } return Err(Error::MissingSpentSrcTransaction { dbc_id: *input.dbc_id(), - src_tx_hash: input.spend.src_tx_hash, + dbc_creation_tx_hash: input.spend.dbc_creation_tx_hash, }); } // For each input's DbcId, look up the matching // blinded amount in the tx where it was created - its source tx. let mut tx_keys_and_blinded_amounts = Vec::<(DbcId, BlindedAmount)>::new(); - for input in &dst_tx.inputs { + for input in &spent_tx.inputs { let input_dbc_id = input.dbc_id(); let matching_amounts: Vec = referenced_spent_txs