Skip to content

Commit

Permalink
chore!: refactor spend struct
Browse files Browse the repository at this point in the history
BREAKING CHANGE
  • Loading branch information
maqi committed Jul 7, 2024
1 parent 45ca228 commit a370dd4
Show file tree
Hide file tree
Showing 22 changed files with 581 additions and 645 deletions.
4 changes: 2 additions & 2 deletions sn_auditor/src/dag_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ impl SpendDagDb {
) {
let mut beta_tracking = self.beta_tracking.write().await;
beta_tracking.processed_spends += 1;
beta_tracking.total_accumulated_utxo += spend.spend.spent_tx.outputs.len() as u64;
beta_tracking.total_accumulated_utxo += spend.spend.descendants.len() as u64;
beta_tracking.total_on_track_utxo += utxos_for_further_track;

// check for beta rewards reason
Expand All @@ -347,7 +347,7 @@ impl SpendDagDb {

// add to local rewards
let addr = spend.address();
let amount = spend.spend.amount;
let amount = spend.spend.amount();
let beta_participants_read = self.beta_participants.read().await;

if let Some(user_name) = beta_participants_read.get(&user_name_hash) {
Expand Down
27 changes: 15 additions & 12 deletions sn_cli/src/bin/subcommands/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,22 @@ impl WalletApiHelper {
spend.spend.unique_pubkey,
address.to_hex()
);
println!("reason {:?}, amount {}, inputs: {}, outputs: {}, royalties: {}, {:?} - {:?}",
spend.spend.reason, spend.spend.amount, spend.spend.spent_tx.inputs.len(), spend.spend.spent_tx.outputs.len(),
spend.spend.network_royalties.len(), spend.spend.spent_tx.inputs, spend.spend.spent_tx.outputs);
println!(
"reason {:?}, amount {}, inputs: {}, outputs: {}",
spend.spend.reason,
spend.spend.amount(),
spend.spend.ancestors.len(),
spend.spend.descendants.len()
);
println!("Inputs in hex str:");
for input in spend.spend.spent_tx.inputs.iter() {
let address = SpendAddress::from_unique_pubkey(&input.unique_pubkey);
for input in spend.spend.ancestors.iter() {
let address = SpendAddress::from_unique_pubkey(input);
println!("Input spend {}", address.to_hex());
}
println!("parent_tx inputs in hex str:");
for input in spend.spend.parent_tx.inputs.iter() {
let address = SpendAddress::from_unique_pubkey(&input.unique_pubkey);
println!("parent_tx input spend {}", address.to_hex());
println!("Outputs in hex str:");
for (output, (amount, purpose)) in spend.spend.descendants.iter() {
let address = SpendAddress::from_unique_pubkey(output);
println!("Output {} with {amount} and {purpose:?}", address.to_hex());
}
}
println!("Available cash notes are:");
Expand All @@ -91,10 +95,9 @@ impl WalletApiHelper {
let cash_notes = vec![cash_note.clone()];

let spent_unique_pubkeys: BTreeSet<_> = cash_note
.parent_tx
.inputs
.parent_spends
.iter()
.map(|input| input.unique_pubkey())
.map(|spend| spend.unique_pubkey())
.collect();

match self {
Expand Down
23 changes: 5 additions & 18 deletions sn_cli/src/bin/subcommands/wallet/hot_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,28 +312,15 @@ fn sign_transaction(tx: &str, root_dir: &Path, force: bool) -> Result<()> {
let unsigned_transfer: UnsignedTransfer = rmp_serde::from_slice(&hex::decode(tx)?)?;

println!("The unsigned transaction has been successfully decoded:");
let mut spent_tx = None;
for (i, (spend, _)) in unsigned_transfer.spends.iter().enumerate() {
println!("\nSpending input #{i}:");
println!("\tKey: {}", spend.unique_pubkey.to_hex());
println!("\tAmount: {}", spend.amount);
if let Some(ref tx) = spent_tx {
if tx != &spend.spent_tx {
bail!("Transaction seems corrupted, not all Spends (inputs) refer to the same transaction");
}
} else {
spent_tx = Some(spend.spent_tx.clone());
}
}
println!("\tAmount: {}", spend.amount());

if let Some(ref tx) = spent_tx {
for (i, output) in tx.outputs.iter().enumerate() {
println!("\nOutput #{i}:");
println!("\tKey: {}", output.unique_pubkey.to_hex());
println!("\tAmount: {}", output.amount);
for (descendant, (amount, _purpose)) in spend.descendants.iter() {
println!("\tOutput Key: {}", descendant.to_hex());
println!("\tAmount: {amount}");
}
} else {
bail!("Transaction is corrupted, no transaction information found.");
}

if !force {
Expand All @@ -352,7 +339,7 @@ fn sign_transaction(tx: &str, root_dir: &Path, force: bool) -> Result<()> {
let signed_spends = wallet.sign(unsigned_transfer.spends);

for signed_spend in signed_spends.iter() {
if let Err(err) = signed_spend.verify(signed_spend.spent_tx_hash()) {
if let Err(err) = signed_spend.verify() {
bail!("Signature or transaction generated is invalid: {err:?}");
}
}
Expand Down
40 changes: 14 additions & 26 deletions sn_cli/src/bin/subcommands/wallet/wo_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ use std::{
};
use walkdir::WalkDir;

type SignedTx = (
BTreeSet<SignedSpend>,
BTreeMap<UniquePubkey, (MainPubkey, DerivationIndex, NanoTokens)>,
UniquePubkey,
);

// Please do not remove the blank lines in these doc comments.
// They are used for inserting line breaks when the help menu is rendered in the UI.
#[derive(Parser, Debug)]
Expand Down Expand Up @@ -249,42 +255,24 @@ async fn broadcast_signed_spends(
verify_store: bool,
force: bool,
) -> Result<()> {
let (signed_spends, output_details, change_id): (
BTreeSet<SignedSpend>,
BTreeMap<UniquePubkey, (MainPubkey, DerivationIndex)>,
UniquePubkey,
) = rmp_serde::from_slice(&hex::decode(signed_tx)?)?;
let (signed_spends, output_details, change_id): SignedTx =
rmp_serde::from_slice(&hex::decode(signed_tx)?)?;

println!("The signed transaction has been successfully decoded:");
let mut transaction = None;
for (i, signed_spend) in signed_spends.iter().enumerate() {
println!("\nSpending input #{i}:");
println!("\tKey: {}", signed_spend.unique_pubkey().to_hex());
println!("\tAmount: {}", signed_spend.token());
let linked_tx = signed_spend.spent_tx();
if let Some(ref tx) = transaction {
if tx != &linked_tx {
bail!("Transaction seems corrupted, not all Spends (inputs) refer to the same transaction");
}
} else {
transaction = Some(linked_tx);
}

if let Err(err) = signed_spend.verify(signed_spend.spent_tx_hash()) {
if let Err(err) = signed_spend.verify() {
bail!("Transaction is invalid: {err:?}");
}
}

let tx = if let Some(tx) = transaction {
for (i, output) in tx.outputs.iter().enumerate() {
println!("\nOutput #{i}:");
println!("\tKey: {}", output.unique_pubkey.to_hex());
println!("\tAmount: {}", output.amount);
for (descendant, (amount, _purpose)) in signed_spend.spend.descendants.iter() {
println!("\tOutput Key: {}", descendant.to_hex());
println!("\tAmount: {amount}");
}
tx
} else {
bail!("Transaction is corrupted, no transaction information found.");
};
}

if !force {
println!(
Expand All @@ -301,7 +289,7 @@ async fn broadcast_signed_spends(
}

println!("Broadcasting the transaction to the network...");
let transfer = OfflineTransfer::from_transaction(signed_spends, tx, change_id, output_details)?;
let transfer = OfflineTransfer::from_transaction(signed_spends, change_id, output_details)?;

// return the first CashNote (assuming there is only one because we only sent to one recipient)
let cash_note = match &transfer.cash_notes_for_recipient[..] {
Expand Down
2 changes: 1 addition & 1 deletion sn_client/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@ impl Client {
}

// check spend
match signed_spend.verify(signed_spend.spent_tx_hash()) {
match signed_spend.verify() {
Ok(()) => {
trace!("Verified signed spend got from network for {address:?}");
Ok(signed_spend.clone())
Expand Down
Loading

0 comments on commit a370dd4

Please sign in to comment.