Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore!: refactor spend struct #1930

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion .github/workflows/merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -855,13 +855,43 @@ jobs:
- name: Create and fund a wallet first time
run: |
~/faucet --log-output-dest=data-dir send 100000000 $(~/safe --log-output-dest=data-dir wallet address | tail -n 1) | tail -n 1 1>first.txt
address=$(~/safe --log-output-dest=data-dir wallet address | tail -n 1)
echo "-----------------------------------"
~/faucet --log-output-dest=data-dir send 100000000 $address > first_faucet.txt
echo "-----------------------------------"
cat first_faucet.txt
echo "==================================="
cat first_faucet.txt | tail -n 1 1>first.txt
echo "----------"
cat first.txt
env:
SN_LOG: "all"
timeout-minutes: 5

- name: Move faucet log to the working folder
run: |
echo "SAFE_DATA_PATH has: "
ls -l $SAFE_DATA_PATH
echo "test_faucet foder has: "
ls -l $SAFE_DATA_PATH/test_faucet
echo "logs folder has: "
ls -l $SAFE_DATA_PATH/test_faucet/logs
mv $SAFE_DATA_PATH/test_faucet/logs/faucet.log ./faucet_log.log
env:
SN_LOG: "all"
SAFE_DATA_PATH: /home/runner/.local/share/safe
continue-on-error: true
if: always()
timeout-minutes: 1

- name: Upload faucet log
uses: actions/upload-artifact@main
with:
name: faucet_test_first_faucet_log
path: faucet_log.log
continue-on-error: true
if: always()

- name: Create and fund a wallet second time
run: |
ls -l /home/runner/.local/share
Expand Down
11 changes: 6 additions & 5 deletions sn_auditor/src/dag_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,11 @@ 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
let user_name_hash = match spend.reason().get_sender_hash(sk) {
let user_name_hash = match spend.reason().decrypt_discord_cypher(sk) {
Some(n) => n,
None => {
return;
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 All @@ -359,8 +359,9 @@ impl SpendDagDb {
.insert((addr, amount));
} else {
// check with default key
if let Some(default_user_name_hash) =
spend.reason().get_sender_hash(&DEFAULT_PAYMENT_FORWARD_SK)
if let Some(default_user_name_hash) = spend
.reason()
.decrypt_discord_cypher(&DEFAULT_PAYMENT_FORWARD_SK)
{
if let Some(user_name) = beta_participants_read.get(&default_user_name_hash) {
warn!("With default key, got forwarded reward {amount} from {user_name} of {amount} at {addr:?}");
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
Loading