Skip to content

Commit

Permalink
feat: rename spends with better names
Browse files Browse the repository at this point in the history
BREAKING CHANGE: renames fields and methods in Spend
  • Loading branch information
grumbach committed May 19, 2023
1 parent 0d65b1c commit 3a608aa
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 75 deletions.
18 changes: 9 additions & 9 deletions benches/reissue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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)]
Expand Down Expand Up @@ -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();

Expand All @@ -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();
}

Expand All @@ -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)]
Expand Down Expand Up @@ -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)?;
}
Expand Down
26 changes: 13 additions & 13 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DbcBuilder> {
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| {
Expand All @@ -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 {
Expand All @@ -150,7 +150,7 @@ impl TransactionBuilder {
.collect();

Ok(DbcBuilder::new(
dst_tx,
spent_tx,
revealed_outputs,
self.output_id_sources,
self.revealed_tx,
Expand All @@ -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<RevealedOutput>,
pub output_id_sources: OutputIdSources,
pub revealed_tx: RevealedTx,
Expand All @@ -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<RevealedOutput>,
output_id_sources: OutputIdSources,
revealed_tx: RevealedTx,
signed_spends: BTreeSet<SignedSpend>,
) -> Self {
Self {
dst_tx,
spent_tx,
revealed_outputs,
output_id_sources,
revealed_tx,
Expand All @@ -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()
}
Expand All @@ -201,7 +201,7 @@ impl DbcBuilder {
pub fn build(self) -> Result<Vec<(Dbc, RevealedAmount)>> {
// 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()
Expand All @@ -223,7 +223,7 @@ impl DbcBuilder {
.collect();

let dbc_id_list: Vec<&DbcIdSource> = self
.dst_tx
.spent_tx
.outputs
.iter()
.map(|output| {
Expand All @@ -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)
Expand All @@ -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(),
};
Expand Down
2 changes: 1 addition & 1 deletion src/dbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?
}
Expand Down
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.")]
Expand Down
2 changes: 1 addition & 1 deletion src/mock/genesis_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down
18 changes: 9 additions & 9 deletions src/mock/mock_spentbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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| {
Expand All @@ -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.
Expand All @@ -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() {
Expand Down
33 changes: 19 additions & 14 deletions src/signed_spend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}

Expand Down Expand Up @@ -106,25 +111,25 @@ 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 {
/// Represent this Spend as bytes.
pub fn to_bytes(&self) -> Vec<u8> {
let mut bytes: Vec<u8> = 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
}

Expand Down

0 comments on commit 3a608aa

Please sign in to comment.