Skip to content

Commit

Permalink
chore(node): use proper SpendReason enum
Browse files Browse the repository at this point in the history
  • Loading branch information
maqi committed May 10, 2024
1 parent de1f7c1 commit dfa136d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
7 changes: 1 addition & 6 deletions sn_node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ use libp2p::kad::{Quorum, Record};
use sn_networking::PutRecordCfg;
#[cfg(feature = "reward-forward")]
use sn_protocol::storage::{try_serialize_record, RecordKind, SpendAddress};
#[cfg(feature = "reward-forward")]
use sn_transfers::Hash;

/// Interval to trigger replication of all records to all peers.
/// This is the max time it should take. Minimum interval at any ndoe will be half this
Expand Down Expand Up @@ -776,10 +774,7 @@ impl Node {
*NETWORK_ROYALTIES_PK,
)];

spend_requests.extend(
wallet
.prepare_forward_signed_spend(payee, Some(Hash::hash(&owner.into_bytes())))?,
);
spend_requests.extend(wallet.prepare_forward_signed_spend(payee, owner)?);
}

let record_kind = RecordKind::Spend;
Expand Down
7 changes: 7 additions & 0 deletions sn_transfers/src/cashnotes/spend_reason.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ impl SpendReason {
Self::BetaRewardTracking(cypher) => Hash::hash(&cypher.cipher),
}
}

pub fn create_reward_tracking_reason(input_str: &str) -> Result<Self> {
let input_pk = crate::NETWORK_ROYALTIES_PK.public_key();
Ok(Self::BetaRewardTracking(DiscordNameCipher::create(
input_str, input_pk,
)?))
}
}

lazy_static! {
Expand Down
26 changes: 21 additions & 5 deletions sn_transfers/src/wallet/hot_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,15 +376,23 @@ impl HotWallet {
pub fn prepare_forward_signed_spend(
&mut self,
to: Vec<TransactionPayeeDetails>,
reason_hash: Option<Hash>,
owner: String,
) -> Result<Vec<SignedSpend>> {
let (available_cash_notes, exclusive_access) = self.available_cash_notes()?;
debug!(
"Available CashNotes for local send: {:#?}",
available_cash_notes
);

let reason_hash = reason_hash.unwrap_or_default();
let spend_reason = match SpendReason::create_reward_tracking_reason(&owner) {
Ok(spend_reason) => spend_reason,
Err(err) => {
error!("Failed to generate spend_reason {err:?}");
return Err(Error::CouldNotSendMoney(format!(
"Failed to generate spend_reason {err:?}"
)));
}
};

// create a unique key for each output
let mut rng = &mut rand::rngs::OsRng;
Expand All @@ -399,7 +407,7 @@ impl HotWallet {
available_cash_notes,
to_unique_keys,
self.address(),
reason_hash,
spend_reason,
)?;

let signed_spends = transfer.all_spend_requests.clone();
Expand Down Expand Up @@ -476,13 +484,21 @@ impl HotWallet {
start.elapsed()
);
debug!("Available CashNotes: {:#?}", available_cash_notes);
let input_reason_hash = SpendReason::default();
let spend_reason = match SpendReason::create_reward_tracking_reason("STORAGE") {
Ok(spend_reason) => spend_reason,
Err(err) => {
error!("Failed to generate spend_reason for local_send {err:?}");
return Err(Error::CouldNotSendMoney(format!(
"Failed to generate spend_reason {err:?}"
)));
}
};
let start = Instant::now();
let offline_transfer = OfflineTransfer::new(
available_cash_notes,
recipients,
self.address(),
input_reason_hash,
spend_reason,
)?;
trace!(
"local_send_storage_payment created offline_transfer with {} cashnotes in {:?}",
Expand Down

0 comments on commit dfa136d

Please sign in to comment.