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

feat: deep clean sn_transfers, reduce exposition, remove dead code #773

Merged
merged 6 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sn_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use sn_client::Client;
#[cfg(feature = "metrics")]
use sn_logging::{init_logging, metrics::init_metrics, LogFormat};
use sn_peers_acquisition::parse_peers_args;
use sn_transfers::wallet::bls_secret_from_hex;
use sn_transfers::bls_secret_from_hex;
use std::path::PathBuf;
use tracing::Level;

Expand Down
2 changes: 1 addition & 1 deletion sn_cli/src/subcommands/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use clap::Subcommand;
use color_eyre::{eyre::WrapErr, Result, Section};
use sn_client::{Client, ClientRegister, Error as ClientError, WalletClient};
use sn_protocol::storage::RegisterAddress;
use sn_transfers::wallet::LocalWallet;
use sn_transfers::LocalWallet;
use std::path::Path;
use xor_name::XorName;

Expand Down
2 changes: 1 addition & 1 deletion sn_cli/src/subcommands/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use clap::Parser;
use color_eyre::{eyre::eyre, Result};
use sn_client::{Client, Files, WalletClient};
use sn_transfers::wallet::{parse_main_pubkey, LocalWallet};
use sn_transfers::NanoTokens;
use sn_transfers::{parse_main_pubkey, LocalWallet};
use std::{
io::Read,
path::{Path, PathBuf},
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 @@ -31,7 +31,7 @@ use sn_protocol::{
use sn_transfers::{MainPubkey, NanoTokens, SignedSpend, UniquePubkey};

use sn_registers::SignedRegister;
use sn_transfers::{transfers::SpendRequest, wallet::Transfer};
use sn_transfers::{SpendRequest, Transfer};
use std::time::Duration;
use tokio::{sync::OwnedSemaphorePermit, task::spawn};
use tracing::trace;
Expand Down
4 changes: 2 additions & 2 deletions sn_client/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use thiserror::Error;
#[allow(missing_docs)]
pub enum Error {
#[error("Genesis error {0}")]
GenesisError(#[from] sn_transfers::genesis::Error),
GenesisError(#[from] sn_transfers::GenesisError),

/// Could not acquire a Semaphore permit.
#[error("Could not acquire a Semaphore permit.")]
Expand All @@ -29,7 +29,7 @@ pub enum Error {
NoNetworkConcurrencyLimiterFound,

#[error("Transfer Error {0}.")]
Transfers(#[from] sn_transfers::wallet::Error),
Transfers(#[from] sn_transfers::WalletError),

#[error("Network Error {0}.")]
Network(#[from] sn_networking::Error),
Expand Down
4 changes: 2 additions & 2 deletions sn_client/src/faucet/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::{wallet::send, Result};
use crate::Client;

use sn_transfers::genesis::{create_faucet_wallet, load_genesis_wallet};
use sn_transfers::wallet::LocalWallet;
use sn_transfers::LocalWallet;
use sn_transfers::{create_faucet_wallet, load_genesis_wallet};
use sn_transfers::{CashNote, MainPubkey, NanoTokens};

/// Returns a cash_note with the requested number of tokens, for use by E2E test instances.
Expand Down
2 changes: 1 addition & 1 deletion sn_client/src/file_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use sn_protocol::{
storage::{Chunk, ChunkAddress},
NetworkAddress, PrettyPrintRecordKey,
};
use sn_transfers::wallet::LocalWallet;
use sn_transfers::LocalWallet;

use std::{
fs::{self, create_dir_all, File},
Expand Down
2 changes: 1 addition & 1 deletion sn_client/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use sn_protocol::{
use sn_registers::{
Entry, EntryHash, Permissions, Register, RegisterAddress, SignedRegister, User,
};
use sn_transfers::wallet::Transfer;
use sn_transfers::Transfer;

use std::collections::{BTreeSet, LinkedList};
use xor_name::XorName;
Expand Down
48 changes: 22 additions & 26 deletions sn_client/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use sn_transfers::{transfers::SpendRequest, wallet::Transfer};
use sn_transfers::{SpendRequest, Transfer};
use xor_name::XorName;

use super::Client;

use sn_protocol::NetworkAddress;
use sn_transfers::{
transfers::TransferOutputs,
wallet::{Error, LocalWallet, Result},
};
use sn_transfers::{CashNote, MainPubkey, NanoTokens};
use sn_transfers::{LocalWallet, OfflineTransfer, WalletError, WalletResult};

use futures::future::join_all;
use std::{
Expand All @@ -40,7 +37,7 @@ impl WalletClient {
}

/// Stores the wallet to disk.
pub fn store_local_wallet(&self) -> Result<()> {
pub fn store_local_wallet(&self) -> WalletResult<()> {
self.wallet.store()
}

Expand All @@ -59,13 +56,13 @@ impl WalletClient {
}

/// Get the payment transfers for a given network address
pub fn get_payment_transfers(&self, address: &NetworkAddress) -> Result<Vec<Transfer>> {
pub fn get_payment_transfers(&self, address: &NetworkAddress) -> WalletResult<Vec<Transfer>> {
match &address.as_xorname() {
Some(xorname) => {
let cash_notes = self.wallet.get_payment_cash_notes(xorname);
Transfer::transfers_from_cash_notes(cash_notes)
}
None => Err(Error::InvalidAddressType),
None => Err(WalletError::InvalidAddressType),
}
}

Expand All @@ -76,7 +73,7 @@ impl WalletClient {
amount: NanoTokens,
to: MainPubkey,
verify_store: bool,
) -> Result<CashNote> {
) -> WalletResult<CashNote> {
let created_cash_notes = self.wallet.local_send(vec![(amount, to)], None)?;

// send to network
Expand All @@ -94,7 +91,7 @@ impl WalletClient {
// return created CashNotes even if network part failed???
match &created_cash_notes[..] {
[info, ..] => Ok(info.clone()),
[] => Err(Error::CouldNotSendMoney(
[] => Err(WalletError::CouldNotSendMoney(
"No CashNotes were returned from the wallet.".into(),
)),
}
Expand All @@ -104,13 +101,12 @@ impl WalletClient {
/// Returns a Vec of proofs
pub async fn get_store_cost_at_address(
&self,

address: &NetworkAddress,
) -> Result<Vec<(MainPubkey, NanoTokens)>> {
) -> WalletResult<Vec<(MainPubkey, NanoTokens)>> {
self.client
.get_store_costs_at_address(address)
.await
.map_err(|error| Error::CouldNotSendMoney(error.to_string()))
.map_err(|error| WalletError::CouldNotSendMoney(error.to_string()))
}

/// Send tokens to nodes closest to the data we want to make storage payment for.
Expand All @@ -122,7 +118,7 @@ impl WalletClient {
&mut self,
content_addrs: impl Iterator<Item = NetworkAddress>,
verify_store: bool,
) -> Result<NanoTokens> {
) -> WalletResult<NanoTokens> {
let mut total_cost = NanoTokens::zero();

let mut payment_map = BTreeMap::default();
Expand All @@ -135,7 +131,7 @@ impl WalletClient {
let costs = client
.get_store_costs_at_address(&content_addr)
.await
.map_err(|error| Error::CouldNotSendMoney(error.to_string()));
.map_err(|error| WalletError::CouldNotSendMoney(error.to_string()));
(content_addr, costs)
});
}
Expand Down Expand Up @@ -186,7 +182,7 @@ impl WalletClient {
&mut self,
all_data_payments: BTreeMap<XorName, Vec<(MainPubkey, NanoTokens)>>,
verify_store: bool,
) -> Result<NanoTokens> {
) -> WalletResult<NanoTokens> {
// TODO:
// Check for any existing payment CashNotes, and use them if they exist, only topping up if needs be
let num_of_payments = all_data_payments.len();
Expand All @@ -198,7 +194,7 @@ impl WalletClient {
if let Some(new_cost) = total_cost.checked_add(*cost) {
total_cost = new_cost;
} else {
return Err(Error::TotalPriceTooHigh);
return Err(WalletError::TotalPriceTooHigh);
}
}
}
Expand Down Expand Up @@ -263,7 +259,7 @@ impl Client {
&self,
spend_requests: &BTreeSet<SpendRequest>,
verify_store: bool,
) -> Result<()> {
) -> WalletResult<()> {
let mut tasks = Vec::new();

for spend_request in spend_requests {
Expand All @@ -275,15 +271,15 @@ impl Client {
}

for spend_attempt_result in join_all(tasks).await {
spend_attempt_result.map_err(|err| Error::CouldNotSendMoney(err.to_string()))?;
spend_attempt_result.map_err(|err| WalletError::CouldNotSendMoney(err.to_string()))?;
}

Ok(())
}

/// Send a spend request to the network.
/// This does _not_ verify the spend has been put to the network correctly
pub async fn send_without_verify(&self, transfer: TransferOutputs) -> Result<()> {
pub async fn send_without_verify(&self, transfer: OfflineTransfer) -> WalletResult<()> {
let mut tasks = Vec::new();
for spend_request in &transfer.all_spend_requests {
trace!(
Expand All @@ -294,13 +290,13 @@ impl Client {
}

for spend_attempt_result in join_all(tasks).await {
spend_attempt_result.map_err(|err| Error::CouldNotSendMoney(err.to_string()))?;
spend_attempt_result.map_err(|err| WalletError::CouldNotSendMoney(err.to_string()))?;
}

Ok(())
}

pub async fn verify(&self, cash_note: &CashNote) -> Result<()> {
pub async fn verify(&self, cash_note: &CashNote) -> WalletResult<()> {
// We need to get all the spends in the cash_note from the network,
// and compare them to the spends in the cash_note, to know if the
// transfer is considered valid in the network.
Expand All @@ -312,7 +308,7 @@ impl Client {
let mut received_spends = std::collections::BTreeSet::new();
for result in join_all(tasks).await {
let network_valid_spend =
result.map_err(|err| Error::CouldNotVerifyTransfer(err.to_string()))?;
result.map_err(|err| WalletError::CouldNotVerifyTransfer(err.to_string()))?;
let _ = received_spends.insert(network_valid_spend);
}

Expand All @@ -321,7 +317,7 @@ impl Client {
if received_spends == cash_note.signed_spends {
return Ok(());
}
Err(Error::CouldNotVerifyTransfer(
Err(WalletError::CouldNotVerifyTransfer(
"The spends in network were not the same as the ones in the CashNote. The parents of this CashNote are probably double spends.".into(),
))
}
Expand All @@ -335,7 +331,7 @@ pub async fn send(
to: MainPubkey,
client: &Client,
verify_store: bool,
) -> Result<CashNote> {
) -> WalletResult<CashNote> {
if amount.as_nano() == 0 {
panic!("Amount must be more than zero.");
}
Expand Down Expand Up @@ -373,7 +369,7 @@ pub async fn send(
.expect("Created cash_note shall be successfully stored.");

if did_error {
return Err(Error::UnconfirmedTxAfterRetries);
return Err(WalletError::UnconfirmedTxAfterRetries);
}

Ok(new_cash_note)
Expand Down
2 changes: 1 addition & 1 deletion sn_networking/src/transfers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use sn_protocol::{
storage::{try_deserialize_record, RecordHeader, RecordKind, SpendAddress},
NetworkAddress, PrettyPrintRecordKey,
};
use sn_transfers::wallet::{LocalWallet, Transfer};
use sn_transfers::{CashNote, DerivationIndex, SignedSpend, Transaction, UniquePubkey};
use sn_transfers::{LocalWallet, Transfer};
use tokio::task::JoinSet;

impl Network {
Expand Down
2 changes: 1 addition & 1 deletion sn_node/examples/registers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use sn_client::{Client, Error, WalletClient};
use sn_registers::RegisterAddress;
use sn_transfers::wallet::LocalWallet;
use sn_transfers::LocalWallet;
use xor_name::XorName;

use bls::SecretKey;
Expand Down
2 changes: 1 addition & 1 deletion sn_node/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use sn_protocol::{
messages::{Cmd, CmdResponse, Query, QueryResponse, Request, Response},
NetworkAddress, PrettyPrintRecordKey,
};
use sn_transfers::wallet::LocalWallet;
use sn_transfers::LocalWallet;
use sn_transfers::MainSecretKey;
use std::{
collections::HashSet,
Expand Down
2 changes: 1 addition & 1 deletion sn_node/src/bin/faucet/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use faucet_server::run_faucet_server;
use sn_client::{get_tokens_from_faucet, load_faucet_wallet_from_genesis_wallet, Client};
use sn_logging::{init_logging, LogFormat, LogOutputDest};
use sn_peers_acquisition::{parse_peers_args, PeersArgs};
use sn_transfers::{wallet::parse_main_pubkey, NanoTokens};
use sn_transfers::{parse_main_pubkey, NanoTokens};
use std::path::PathBuf;
use tracing::info;
use tracing_core::Level;
Expand Down
4 changes: 2 additions & 2 deletions sn_node/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use sn_networking::Error as NetworkError;
use sn_protocol::error::Error as ProtocolError;
use sn_transfers::wallet::Error as RewardsWalletError;
use sn_transfers::WalletError;
use thiserror::Error;

pub(super) type Result<T, E = Error> = std::result::Result<T, E>;
Expand All @@ -27,5 +27,5 @@ pub enum Error {
NodeEventParsingFailed,

#[error("Node's rewards wallet error {0}")]
RewardsWallet(#[from] RewardsWalletError),
RewardsWallet(#[from] WalletError),
}
5 changes: 1 addition & 4 deletions sn_node/src/put_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ use sn_protocol::{
NetworkAddress, PrettyPrintRecordKey,
};
use sn_registers::SignedRegister;
use sn_transfers::{
genesis::{is_genesis_parent_tx, GENESIS_CASHNOTE},
wallet::{LocalWallet, Transfer},
};
use sn_transfers::{is_genesis_parent_tx, LocalWallet, Transfer, GENESIS_CASHNOTE};
use sn_transfers::{CashNote, NanoTokens, SignedSpend, UniquePubkey};
use std::collections::{BTreeSet, HashSet};

Expand Down
2 changes: 1 addition & 1 deletion sn_node/src/spends.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use sn_protocol::{
error::{Error, Result},
storage::SpendAddress,
};
use sn_transfers::genesis::{is_genesis_parent_tx, GENESIS_CASHNOTE};
use sn_transfers::{is_genesis_parent_tx, GENESIS_CASHNOTE};
use sn_transfers::{SignedSpend, UniquePubkey};
use std::{
collections::{BTreeSet, HashSet},
Expand Down
2 changes: 1 addition & 1 deletion sn_node/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use self_encryption::MIN_ENCRYPTABLE_BYTES;
use sn_client::{load_faucet_wallet_from_genesis_wallet, send, Client, Files};
use sn_peers_acquisition::parse_peer_addr;
use sn_protocol::storage::ChunkAddress;
use sn_transfers::wallet::LocalWallet;
use sn_transfers::LocalWallet;

use bytes::Bytes;
use eyre::{eyre, Result};
Expand Down
2 changes: 1 addition & 1 deletion sn_node/tests/data_with_churn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use sn_protocol::{
storage::{Chunk, ChunkAddress, RegisterAddress, SpendAddress},
NetworkAddress,
};
use sn_transfers::wallet::LocalWallet;
use sn_transfers::LocalWallet;
use sn_transfers::{CashNote, MainSecretKey, NanoTokens};
use std::{
collections::{BTreeMap, VecDeque},
Expand Down
2 changes: 1 addition & 1 deletion sn_node/tests/nodes_rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use sn_protocol::{
storage::{ChunkAddress, RegisterAddress},
NetworkAddress,
};
use sn_transfers::wallet::LocalWallet;
use sn_transfers::LocalWallet;
use sn_transfers::NanoTokens;
use xor_name::XorName;

Expand Down
2 changes: 1 addition & 1 deletion sn_node/tests/sequential_transfers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use common::{get_client_and_wallet, get_wallet, init_logging};

use sn_client::send;

use sn_transfers::transfers::create_offline_transfer;
use sn_transfers::create_offline_transfer;
use sn_transfers::{rng, Hash, NanoTokens, UniquePubkey};

use assert_fs::TempDir;
Expand Down