Skip to content

Commit

Permalink
feat: remove unused fee output
Browse files Browse the repository at this point in the history
  • Loading branch information
grumbach authored and joshuef committed Oct 2, 2023
1 parent 6bcc372 commit e7b3fd8
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 98 deletions.
12 changes: 2 additions & 10 deletions sn_transfers/src/cashnotes/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

use super::{
transaction::{Output, Transaction},
CashNote, DerivationIndex, DerivedSecretKey, FeeOutput, Hash, Input, MainPubkey, NanoTokens,
SignedSpend, Spend, UniquePubkey,
CashNote, DerivationIndex, DerivedSecretKey, Hash, Input, MainPubkey, NanoTokens, SignedSpend,
Spend, UniquePubkey,
};

use crate::{Error, Result};
Expand All @@ -25,7 +25,6 @@ pub type InputSrcTx = Transaction;
pub struct TransactionBuilder {
inputs: Vec<Input>,
outputs: Vec<Output>,
fee: FeeOutput,
input_details: BTreeMap<UniquePubkey, (DerivedSecretKey, InputSrcTx)>,
output_details: BTreeMap<UniquePubkey, (MainPubkey, DerivationIndex)>,
}
Expand Down Expand Up @@ -83,18 +82,11 @@ impl TransactionBuilder {
self
}

/// Sets the given fee output.
pub fn set_fee_output(mut self, output: FeeOutput) -> Self {
self.fee = output;
self
}

/// Build the Transaction by signing the inputs. Return a CashNoteBuilder.
pub fn build(self, reason: Hash) -> Result<CashNoteBuilder> {
let spent_tx = Transaction {
inputs: self.inputs.clone(),
outputs: self.outputs.clone(),
fee: self.fee.clone(),
};
let signed_spends: BTreeSet<_> = self
.inputs
Expand Down
9 changes: 2 additions & 7 deletions sn_transfers/src/cashnotes/cashnote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
// permissions and limitations relating to use of the SAFE Network Software.

use super::{
DerivationIndex, DerivedSecretKey, FeeOutput, Hash, MainPubkey, MainSecretKey, NanoTokens,
SignedSpend, Transaction, UniquePubkey,
DerivationIndex, DerivedSecretKey, Hash, MainPubkey, MainSecretKey, NanoTokens, SignedSpend,
Transaction, UniquePubkey,
};

use crate::{Error, Result};
Expand Down Expand Up @@ -99,11 +99,6 @@ impl CashNote {
self.derivation_index
}

/// Return the fee output used in the source transaction
pub fn fee_output(&self) -> &FeeOutput {
&self.src_tx.fee
}

/// Return the reason why this CashNote was spent.
/// Will be the default Hash (empty) if reason is none.
pub fn reason(&self) -> Hash {
Expand Down
54 changes: 0 additions & 54 deletions sn_transfers/src/cashnotes/fee_output.rs

This file was deleted.

12 changes: 0 additions & 12 deletions sn_transfers/src/cashnotes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@
mod address;
mod builder;
mod cashnote;
mod fee_output;
mod nano;
mod reason_hash;
mod signed_spend;
mod transaction;
mod unique_keys;

pub(crate) use builder::TransactionBuilder;
pub(crate) use fee_output::FeeOutput;
pub(crate) use transaction::Input;

pub use address::SpendAddress;
Expand All @@ -44,7 +42,6 @@ pub(crate) mod tests {
let tx = Transaction {
inputs: vec![],
outputs: vec![Output::new(derived_key.unique_pubkey(), amount)],
fee: FeeOutput::new(Hash::default(), 3_500, Hash::default()),
};
let cashnote = CashNote {
id: derived_key.unique_pubkey(),
Expand All @@ -59,9 +56,6 @@ pub(crate) mod tests {
let cashnote = CashNote::from_hex(&hex)?;
assert_eq!(cashnote.value()?.as_nano(), 1_530_000_000);

let fee_amount = cashnote.fee_output().token;
assert_eq!(fee_amount, NanoTokens::from(3_500));

Ok(())
}

Expand All @@ -75,7 +69,6 @@ pub(crate) mod tests {
let tx = Transaction {
inputs: vec![],
outputs: vec![Output::new(derived_key.unique_pubkey(), amount)],
fee: FeeOutput::new(Hash::default(), 2_500, Hash::default()),
};
let cashnote = CashNote {
id: derived_key.unique_pubkey(),
Expand All @@ -90,9 +83,6 @@ pub(crate) mod tests {

assert_eq!(cashnote.value()?, cashnote_from_hex.value()?);

let fee_amount = cashnote.fee_output().token;
assert_eq!(fee_amount, NanoTokens::from(2_500));

Ok(())
}

Expand All @@ -108,7 +98,6 @@ pub(crate) mod tests {
let tx = Transaction {
inputs: vec![],
outputs: vec![Output::new(derived_key.unique_pubkey(), amount)],
fee: FeeOutput::default(),
};

let cashnote = CashNote {
Expand Down Expand Up @@ -140,7 +129,6 @@ pub(crate) mod tests {
let tx = Transaction {
inputs: vec![],
outputs: vec![Output::new(derived_key.unique_pubkey(), amount)],
fee: FeeOutput::default(),
};

let cashnote = CashNote {
Expand Down
7 changes: 1 addition & 6 deletions sn_transfers/src/cashnotes/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// This SAFE Network Software is licensed under the BSD-3-Clause license.
// Please see the LICENSE file for more details.

use super::{FeeOutput, NanoTokens, SignedSpend, UniquePubkey};
use super::{NanoTokens, SignedSpend, UniquePubkey};
use serde::{Deserialize, Serialize};
use std::{cmp::Ordering, collections::BTreeSet};
use tiny_keccak::{Hasher, Sha3};
Expand Down Expand Up @@ -69,7 +69,6 @@ impl Output {
pub struct Transaction {
pub inputs: Vec<Input>,
pub outputs: Vec<Output>,
pub fee: FeeOutput,
}

impl PartialEq for Transaction {
Expand Down Expand Up @@ -97,7 +96,6 @@ impl Transaction {
Self {
inputs: vec![],
outputs: vec![],
fee: FeeOutput::default(),
}
}

Expand All @@ -111,8 +109,6 @@ impl Transaction {
for o in self.outputs.iter() {
v.extend(&o.to_bytes());
}
v.extend("fee".as_bytes());
v.extend(&self.fee.to_bytes());
v.extend("end".as_bytes());
v
}
Expand Down Expand Up @@ -156,7 +152,6 @@ impl Transaction {
.outputs
.iter()
.map(|o| o.amount)
.chain(std::iter::once(self.fee.token))
.try_fold(0, |acc: u64, o| {
acc.checked_add(o.as_nano()).ok_or(Error::NumericOverflow)
})?;
Expand Down
2 changes: 1 addition & 1 deletion sn_transfers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod genesis;
mod transfers;
mod wallet;

pub(crate) use cashnotes::{FeeOutput, Input, TransactionBuilder};
pub(crate) use cashnotes::{Input, TransactionBuilder};

/// Types used in the public API
pub use cashnotes::{
Expand Down
11 changes: 3 additions & 8 deletions sn_transfers/src/transfers/offline_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
// permissions and limitations relating to use of the SAFE Network Software.

use crate::{
rng, CashNote, DerivationIndex, DerivedSecretKey, FeeOutput, Hash, Input, MainPubkey,
NanoTokens, SignedSpend, Transaction, TransactionBuilder, UniquePubkey,
rng, CashNote, DerivationIndex, DerivedSecretKey, Hash, Input, MainPubkey, NanoTokens,
SignedSpend, Transaction, TransactionBuilder, UniquePubkey,
};
use crate::{Error, Result};

Expand Down Expand Up @@ -88,7 +88,7 @@ pub fn create_offline_transfer(
change: (change_amount, change_to),
};

create_offline_transfer_with(selected_inputs, reason_hash, None)
create_offline_transfer_with(selected_inputs, reason_hash)
}

/// Select the necessary number of cash_notes from those that we were passed.
Expand Down Expand Up @@ -161,7 +161,6 @@ fn select_inputs(
fn create_offline_transfer_with(
selected_inputs: TranferInputs,
reason_hash: Hash,
fee: Option<FeeOutput>,
) -> Result<OfflineTransfer> {
let TranferInputs {
change: (change, change_to),
Expand Down Expand Up @@ -190,10 +189,6 @@ fn create_offline_transfer_with(
.add_inputs(inputs)
.add_outputs(selected_inputs.recipients);

if let Some(fee_output) = fee {
tx_builder = tx_builder.set_fee_output(fee_output);
}

let mut rng = rng::thread_rng();
let derivation_index = UniquePubkey::random_derivation_index(&mut rng);
let change_id = change_to.new_unique_pubkey(&derivation_index);
Expand Down

0 comments on commit e7b3fd8

Please sign in to comment.