Skip to content

Commit

Permalink
chore: store owner info inside node instead of network
Browse files Browse the repository at this point in the history
  • Loading branch information
maqi authored and RolandSherwin committed Apr 30, 2024
1 parent e8d592b commit a3a2044
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 47 deletions.
14 changes: 2 additions & 12 deletions sn_client/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,10 @@ impl Client {
// TODO: shall client bearing owner's discord user name, to be reflected in the cash_notes?

#[cfg(not(feature = "open-metrics"))]
let network_builder = NetworkBuilder::new(
Keypair::generate_ed25519(),
local,
root_dir,
"maidsafe_client".to_string(),
);
let network_builder = NetworkBuilder::new(Keypair::generate_ed25519(), local, root_dir);

#[cfg(feature = "open-metrics")]
let mut network_builder = NetworkBuilder::new(
Keypair::generate_ed25519(),
local,
root_dir,
"maidsafe_client".to_string(),
);
let mut network_builder = NetworkBuilder::new(Keypair::generate_ed25519(), local, root_dir);
#[cfg(feature = "open-metrics")]
network_builder.metrics_registry(Registry::default());

Expand Down
7 changes: 1 addition & 6 deletions sn_client/src/uploader/tests/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,12 +425,7 @@ pub fn start_uploading_with_steps(
// Build a very simple client struct for testing. This does not connect to any network.
// The UploaderInterface eliminates the need for direct networking in tests.
pub fn build_unconnected_client(root_dir: PathBuf) -> Result<Client> {
let network_builder = NetworkBuilder::new(
Keypair::generate_ed25519(),
true,
root_dir,
"maidsafe_client".to_string(),
);
let network_builder = NetworkBuilder::new(Keypair::generate_ed25519(), true, root_dir);
let (network, ..) = network_builder.build_client()?;
let client = Client {
network: network.clone(),
Expand Down
11 changes: 1 addition & 10 deletions sn_networking/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,10 @@ pub struct NetworkBuilder {
metrics_registry: Option<Registry>,
#[cfg(feature = "open-metrics")]
metrics_server_port: u16,
owner: String,
}

impl NetworkBuilder {
pub fn new(keypair: Keypair, local: bool, root_dir: PathBuf, owner: String) -> Self {
pub fn new(keypair: Keypair, local: bool, root_dir: PathBuf) -> Self {
Self {
is_behind_home_network: false,
keypair,
Expand All @@ -224,7 +223,6 @@ impl NetworkBuilder {
metrics_registry: None,
#[cfg(feature = "open-metrics")]
metrics_server_port: 0,
owner,
}
}

Expand Down Expand Up @@ -317,15 +315,13 @@ impl NetworkBuilder {
};

let listen_addr = self.listen_addr;
let owner = self.owner.clone();

let (network, events_receiver, mut swarm_driver) = self.build(
kad_cfg,
Some(store_cfg),
false,
ProtocolSupport::Full,
IDENTIFY_NODE_VERSION_STR.to_string(),
owner,
)?;

// Listen on the provided address
Expand Down Expand Up @@ -371,15 +367,12 @@ impl NetworkBuilder {
.ok_or_else(|| NetworkError::InvalidCloseGroupSize)?,
);

let owner = self.owner.clone();

let (network, net_event_recv, driver) = self.build(
kad_cfg,
None,
true,
ProtocolSupport::Outbound,
IDENTIFY_CLIENT_VERSION_STR.to_string(),
owner,
)?;

Ok((network, net_event_recv, driver))
Expand All @@ -393,7 +386,6 @@ impl NetworkBuilder {
is_client: bool,
req_res_protocol: ProtocolSupport,
identify_version: String,
owner: String,
) -> Result<(Network, mpsc::Receiver<NetworkEvent>, SwarmDriver)> {
let peer_id = PeerId::from(self.keypair.public());
// vdash metric (if modified please notify at https://github.com/happybeing/vdash/issues):
Expand Down Expand Up @@ -587,7 +579,6 @@ impl NetworkBuilder {
peer_id: Arc::new(peer_id),
root_dir_path: Arc::new(self.root_dir),
keypair: Arc::new(self.keypair),
owner,
},
network_event_receiver,
swarm_driver,
Expand Down
17 changes: 3 additions & 14 deletions sn_networking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,9 @@ pub struct Network {
pub peer_id: Arc<PeerId>,
pub root_dir_path: Arc<PathBuf>,
keypair: Arc<Keypair>,
/// node owner's discord username, in readable format
owner: String,
}

impl Network {
/// Returns of the discord user_name of the node's owner
pub fn owner(&self) -> String {
self.owner.clone()
}

/// Signs the given data with the node's keypair.
pub fn sign(&self, msg: &[u8]) -> Result<Vec<u8>> {
self.keypair.sign(msg).map_err(NetworkError::from)
Expand Down Expand Up @@ -1053,13 +1046,9 @@ mod tests {

#[test]
fn test_network_sign_verify() -> eyre::Result<()> {
let (network, _, _) = NetworkBuilder::new(
Keypair::generate_ed25519(),
false,
std::env::temp_dir(),
"maidsafe_test".to_string(),
)
.build_client()?;
let (network, _, _) =
NetworkBuilder::new(Keypair::generate_ed25519(), false, std::env::temp_dir())
.build_client()?;
let msg = b"test message";
let sig = network.sign(msg)?;
assert!(network.verify(msg, &sig));
Expand Down
11 changes: 8 additions & 3 deletions sn_node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ impl NodeBuilder {
(metrics_registry, node_metrics)
};

let mut network_builder =
NetworkBuilder::new(self.keypair, self.local, self.root_dir, self.owner.clone());
let mut network_builder = NetworkBuilder::new(self.keypair, self.local, self.root_dir);

network_builder.listen_addr(self.addr);
#[cfg(feature = "open-metrics")]
Expand All @@ -156,6 +155,7 @@ impl NodeBuilder {
reward_address: Arc::new(reward_address),
#[cfg(feature = "open-metrics")]
node_metrics,
owner: self.owner.clone(),
};
let running_node = RunningNode {
network,
Expand Down Expand Up @@ -188,6 +188,8 @@ pub(crate) struct Node {
reward_address: Arc<MainPubkey>,
#[cfg(feature = "open-metrics")]
pub(crate) node_metrics: NodeMetrics,
/// node owner's discord username, in readable format
owner: String,
}

impl Node {
Expand Down Expand Up @@ -389,9 +391,10 @@ impl Node {
event_header = "QueryRequestReceived";
let network = self.network.clone();
let payment_address = *self.reward_address;
let owner = self.owner.clone();

let _handle = spawn(async move {
let res = Self::handle_query(&network, query, payment_address).await;
let res = Self::handle_query(&network, query, payment_address, owner).await;
trace!("Sending response {res:?}");

network.send_response(res, channel);
Expand Down Expand Up @@ -564,6 +567,7 @@ impl Node {
network: &Network,
query: Query,
payment_address: MainPubkey,
owner: String,
) -> Response {
let resp: QueryResponse = match query {
Query::GetStoreCost(address) => {
Expand All @@ -590,6 +594,7 @@ impl Node {
cost,
&address,
&quoting_metrics,
owner,
),
payment_address,
peer_address: NetworkAddress::from_peer(self_id),
Expand Down
5 changes: 3 additions & 2 deletions sn_node/src/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ impl Node {
cost: NanoTokens,
address: &NetworkAddress,
quoting_metrics: &QuotingMetrics,
owner: String,
) -> Result<PaymentQuote, ProtocolError> {
let content = address.as_xorname().unwrap_or_default();
let timestamp = std::time::SystemTime::now();
Expand All @@ -27,7 +28,7 @@ impl Node {
cost,
timestamp,
quoting_metrics,
network.owner(),
owner.clone(),
);

let Ok(signature) = network.sign(&bytes) else {
Expand All @@ -39,7 +40,7 @@ impl Node {
cost,
timestamp,
quoting_metrics: quoting_metrics.clone(),
reason: network.owner(),
reason: owner,
pub_key: network.get_pub_key(),
signature,
};
Expand Down

0 comments on commit a3a2044

Please sign in to comment.