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

Evm integration step 2: working Node #2144

Merged
merged 3 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 6 additions & 3 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions sn_networking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ sn_build_info = { path="../sn_build_info", version = "0.1.13" }
sn_protocol = { path = "../sn_protocol", version = "0.17.9" }
sn_transfers = { path = "../sn_transfers", version = "0.19.1" }
sn_registers = { path = "../sn_registers", version = "0.3.19" }
sn_evm = { path = "../sn_evm", version = "0.1" }
sysinfo = { version = "0.30.8", default-features = false, optional = true }
thiserror = "1.0.23"
tiny-keccak = { version = "~2.0.2", features = ["sha3"] }
Expand Down
17 changes: 9 additions & 8 deletions sn_networking/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ use libp2p::{
},
Multiaddr, PeerId,
};
use sn_evm::{AttoTokens, PaymentQuote, QuotingMetrics};
use sn_protocol::{
messages::{Cmd, Request, Response},
storage::{RecordHeader, RecordKind, RecordType},
NetworkAddress, PrettyPrintRecordKey,
};
use sn_transfers::{NanoTokens, PaymentQuote, QuotingMetrics};
use std::{
collections::{BTreeMap, HashMap},
fmt::Debug,
Expand Down Expand Up @@ -91,12 +91,12 @@ pub enum LocalSwarmCmd {
/// GetLocalStoreCost for this node
GetLocalStoreCost {
key: RecordKey,
sender: oneshot::Sender<(NanoTokens, QuotingMetrics)>,
sender: oneshot::Sender<(AttoTokens, QuotingMetrics)>,
},
/// Notify the node received a payment.
PaymentReceived,
/// Put record to the local RecordStore
PutVerifiedLocalRecord {
PutLocalRecord {
record: Record,
},
/// Remove a local record from the RecordStore
Expand Down Expand Up @@ -194,7 +194,7 @@ pub enum NetworkSwarmCmd {
impl Debug for LocalSwarmCmd {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LocalSwarmCmd::PutVerifiedLocalRecord { record } => {
LocalSwarmCmd::PutLocalRecord { record } => {
write!(
f,
"LocalSwarmCmd::PutLocalRecord {{ key: {:?} }}",
Expand Down Expand Up @@ -561,7 +561,7 @@ impl SwarmDriver {
.store_cost(&key);

self.record_metrics(Marker::StoreCost {
cost: cost.as_nano(),
cost: cost.as_atto(),
quoting_metrics: &quoting_metrics,
});

Expand All @@ -587,8 +587,8 @@ impl SwarmDriver {
let _ = sender.send(record);
}

LocalSwarmCmd::PutVerifiedLocalRecord { record } => {
cmd_string = "PutVerifiedLocalRecord";
LocalSwarmCmd::PutLocalRecord { record } => {
cmd_string = "PutLocalRecord";
let key = record.key.clone();
let record_key = PrettyPrintRecordKey::from(&key);

Expand Down Expand Up @@ -719,6 +719,7 @@ impl SwarmDriver {
}
LocalSwarmCmd::GetAllLocalRecordAddresses { sender } => {
cmd_string = "GetAllLocalRecordAddresses";
#[allow(clippy::mutable_key_type)] // for the Bytes in NetworkAddress
let addresses = self
.swarm
.behaviour_mut()
Expand All @@ -735,7 +736,7 @@ impl SwarmDriver {
if let Some(distance) = range.0.ilog2() {
let peers_in_kbucket = kbucket
.iter()
.map(|peer_entry| (*peer_entry.node.key).into_preimage())
.map(|peer_entry| peer_entry.node.key.into_preimage())
.collect::<Vec<PeerId>>();
let _ = ilog2_kbuckets.insert(distance, peers_in_kbucket);
} else {
Expand Down
2 changes: 1 addition & 1 deletion sn_networking/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ use libp2p::{
};
#[cfg(feature = "open-metrics")]
use prometheus_client::{metrics::info::Info, registry::Registry};
use sn_evm::PaymentQuote;
use sn_protocol::{
messages::{ChunkProof, Nonce, Request, Response},
storage::{try_deserialize_record, RetryStrategy},
Expand All @@ -57,7 +58,6 @@ use sn_protocol::{
NetworkAddress, PrettyPrintKBucketKey, PrettyPrintRecordKey,
};
use sn_registers::SignedRegister;
use sn_transfers::PaymentQuote;
use std::{
collections::{btree_map::Entry, BTreeMap, HashMap, HashSet},
fmt::Debug,
Expand Down
2 changes: 2 additions & 0 deletions sn_networking/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ pub enum NetworkError {
Wallet(#[from] sn_transfers::WalletError),
#[error("Transfer Error {0}")]
Transfer(#[from] sn_transfers::TransferError),
#[error("Evm payment Error {0}")]
EvmPaymemt(#[from] sn_evm::EvmError),

#[error("Failed to sign the message with the PeerId keypair")]
SigningFailed(#[from] libp2p::identity::SigningError),
Expand Down
2 changes: 1 addition & 1 deletion sn_networking/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ use libp2p::{
Multiaddr, PeerId,
};

use sn_evm::PaymentQuote;
use sn_protocol::{
messages::{Query, Request, Response},
NetworkAddress, PrettyPrintRecordKey,
};
use sn_transfers::PaymentQuote;
use std::{
collections::BTreeSet,
fmt::{Debug, Formatter},
Expand Down
1 change: 1 addition & 0 deletions sn_networking/src/event/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@
self.send_event(NetworkEvent::NewListenAddr(address.clone()));

info!("Local node is listening {listener_id:?} on {address:?}");
println!("Local node is listening on {address:?}"); // TODO: make it print only once

Check notice

Code scanning / devskim

A "TODO" or similar was left in source code, possibly indicating incomplete functionality Note

Suspicious comment
}
SwarmEvent::ListenerClosed {
listener_id,
Expand Down
34 changes: 17 additions & 17 deletions sn_networking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ use libp2p::{
Multiaddr, PeerId,
};
use rand::Rng;
use sn_evm::{AttoTokens, PaymentQuote, QuotingMetrics, RewardsAddress};
use sn_protocol::{
error::Error as ProtocolError,
messages::{ChunkProof, Cmd, Nonce, Query, QueryResponse, Request, Response},
storage::{RecordType, RetryStrategy},
NetworkAddress, PrettyPrintKBucketKey, PrettyPrintRecordKey, CLOSE_GROUP_SIZE,
};
use sn_transfers::{MainPubkey, NanoTokens, PaymentQuote, QuotingMetrics};
use std::{
collections::{BTreeMap, BTreeSet, HashMap},
net::IpAddr,
Expand All @@ -79,7 +79,7 @@ use tokio::sync::{
use tokio::time::Duration;

/// The type of quote for a selected payee.
pub type PayeeQuote = (PeerId, MainPubkey, PaymentQuote);
pub type PayeeQuote = (PeerId, RewardsAddress, PaymentQuote);

/// The count of peers that will be considered as close to a record target,
/// that a replication of the record shall be sent/accepted to/by the peer.
Expand Down Expand Up @@ -378,8 +378,8 @@ impl Network {
peer_address,
}) => {
// Check the quote itself is valid.
if quote.cost.as_nano()
!= calculate_cost_for_records(quote.quoting_metrics.close_records_stored)
if quote.cost
!= AttoTokens::from_u64(calculate_cost_for_records(quote.quoting_metrics.close_records_stored))
{
warn!("Received invalid quote from {peer_address:?}, {quote:?}");
continue;
Expand Down Expand Up @@ -589,7 +589,7 @@ impl Network {
pub async fn get_local_storecost(
&self,
key: RecordKey,
) -> Result<(NanoTokens, QuotingMetrics)> {
) -> Result<(AttoTokens, QuotingMetrics)> {
let (sender, receiver) = oneshot::channel();
self.send_local_swarm_cmd(LocalSwarmCmd::GetLocalStoreCost { key, sender });

Expand Down Expand Up @@ -751,7 +751,7 @@ impl Network {
PrettyPrintRecordKey::from(&record.key),
record.value.len()
);
self.send_local_swarm_cmd(LocalSwarmCmd::PutVerifiedLocalRecord { record })
self.send_local_swarm_cmd(LocalSwarmCmd::PutLocalRecord { record })
}

/// Returns true if a RecordKey is present locally in the RecordStore
Expand Down Expand Up @@ -961,7 +961,7 @@ impl Network {
/// Given `all_costs` it will return the closest / lowest cost
/// Closest requiring it to be within CLOSE_GROUP nodes
fn get_fees_from_store_cost_responses(
all_costs: Vec<(NetworkAddress, MainPubkey, PaymentQuote)>,
all_costs: Vec<(NetworkAddress, RewardsAddress, PaymentQuote)>,
) -> Result<PayeeQuote> {
// Find the minimum cost using a linear scan with random tie break
let mut rng = rand::thread_rng();
Expand Down Expand Up @@ -1114,26 +1114,26 @@ mod tests {
use eyre::bail;

use super::*;
use sn_transfers::PaymentQuote;
use sn_evm::PaymentQuote;

#[test]
fn test_get_fee_from_store_cost_responses() -> Result<()> {
// for a vec of different costs of CLOSE_GROUP size
// ensure we return the CLOSE_GROUP / 2 indexed price
let mut costs = vec![];
for i in 1..CLOSE_GROUP_SIZE {
let addr = MainPubkey::new(bls::SecretKey::random().public_key());
let addr = sn_evm::utils::dummy_address();
costs.push((
NetworkAddress::from_peer(PeerId::random()),
addr,
PaymentQuote::test_dummy(Default::default(), NanoTokens::from(i as u64)),
PaymentQuote::test_dummy(Default::default(), AttoTokens::from_u64(i as u64)),
));
}
let expected_price = costs[0].2.cost.as_nano();
let expected_price = costs[0].2.cost.as_atto();
let (_peer_id, _key, price) = get_fees_from_store_cost_responses(costs)?;

assert_eq!(
price.cost.as_nano(),
price.cost.as_atto(),
expected_price,
"price should be {expected_price}"
);
Expand All @@ -1148,26 +1148,26 @@ mod tests {
let responses_count = CLOSE_GROUP_SIZE as u64 - 1;
let mut costs = vec![];
for i in 1..responses_count {
// push random MainPubkey and Nano
let addr = MainPubkey::new(bls::SecretKey::random().public_key());
// push random addr and Nano
let addr = sn_evm::utils::dummy_address();
costs.push((
NetworkAddress::from_peer(PeerId::random()),
addr,
PaymentQuote::test_dummy(Default::default(), NanoTokens::from(i)),
PaymentQuote::test_dummy(Default::default(), AttoTokens::from_u64(i)),
));
println!("price added {i}");
}

// this should be the lowest price
let expected_price = costs[0].2.cost.as_nano();
let expected_price = costs[0].2.cost.as_atto();

let (_peer_id, _key, price) = match get_fees_from_store_cost_responses(costs) {
Err(_) => bail!("Should not have errored as we have enough responses"),
Ok(cost) => cost,
};

assert_eq!(
price.cost.as_nano(),
price.cost.as_atto(),
expected_price,
"price should be {expected_price}"
);
Expand Down
4 changes: 2 additions & 2 deletions sn_networking/src/log_markers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// permissions and limitations relating to use of the SAFE Network Software.

use libp2p::PeerId;
use sn_transfers::QuotingMetrics;
use sn_evm::{Amount, QuotingMetrics};
// this gets us to_string easily enough
use strum::Display;

Expand All @@ -22,7 +22,7 @@ pub enum Marker<'a> {
/// Store cost
StoreCost {
/// Cost
cost: u64,
cost: Amount,
quoting_metrics: &'a QuotingMetrics,
},
/// The peer has been considered as bad
Expand Down
8 changes: 7 additions & 1 deletion sn_networking/src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,13 @@ impl NetworkMetricsRecorder {
cost,
quoting_metrics,
} => {
let _ = self.store_cost.set(cost as i64);
let _ = self.store_cost.set(cost.try_into().unwrap_or(i64::MAX));
let _ = self.relevant_records.set(
quoting_metrics
.close_records_stored
.try_into()
.unwrap_or(i64::MAX),
);
let _ = self
.relevant_records
.set(quoting_metrics.close_records_stored as i64);
Expand Down
Loading
Loading