node: Add track/untrack support

Signed-off-by: Slack Coder <slackcoder@server.ky>
This commit is contained in:
Slack Coder
2022-09-13 10:16:05 +02:00
committed by Alexis Sellier
parent 8fbcb51963
commit d273cd0247
5 changed files with 117 additions and 7 deletions
+19 -6
View File
@@ -1,26 +1,39 @@
use std::sync::{Arc, Mutex};
use crate::client::handle;
use crate::client::handle::traits;
use crate::{client, identity, protocol};
use crate::client::handle::Error;
use crate::identity::ProjId;
use crate::protocol;
#[derive(Default, Clone)]
pub struct Handle {
pub updates: Arc<Mutex<Vec<identity::ProjId>>>,
pub updates: Arc<Mutex<Vec<ProjId>>>,
}
impl traits::Handle for Handle {
fn updated(&self, id: identity::ProjId) -> Result<(), handle::Error> {
fn fetch(&self, _id: ProjId) -> Result<(), Error> {
Ok(())
}
fn track(&self, _id: ProjId) -> Result<bool, Error> {
Ok(true)
}
fn untrack(&self, _id: ProjId) -> Result<bool, Error> {
Ok(true)
}
fn updated(&self, id: ProjId) -> Result<(), Error> {
self.updates.lock().unwrap().push(id);
Ok(())
}
fn command(&self, _cmd: protocol::Command) -> Result<(), handle::Error> {
fn command(&self, _cmd: protocol::Command) -> Result<(), Error> {
Ok(())
}
fn shutdown(self) -> Result<(), client::handle::Error> {
fn shutdown(self) -> Result<(), Error> {
Ok(())
}
}
+35
View File
@@ -1,6 +1,7 @@
use std::io;
use std::sync::Arc;
use crossbeam_channel as chan;
use nakamoto_net as nakamoto;
use nakamoto_net::simulator;
use nakamoto_net::simulator::{Peer as _, Simulation};
@@ -167,6 +168,40 @@ fn test_inventory_sync() {
assert_eq!(a, b);
}
#[test]
fn test_tracking() {
let mut alice = Peer::config(
"alice",
Config {
project_tracking: ProjectTracking::Allowed(HashSet::default()),
..Config::default()
},
[7, 7, 7, 7],
vec![],
MockStorage::empty(),
fastrand::Rng::new(),
);
let proj_id: identity::ProjId = test::arbitrary::gen(1);
let (sender, receiver) = chan::bounded(1);
alice.command(Command::Track(proj_id.clone(), sender));
let policy_change = receiver
.recv()
.map_err(client::handle::Error::from)
.unwrap();
assert!(policy_change);
assert!(alice.config().is_tracking(&proj_id));
let (sender, receiver) = chan::bounded(1);
alice.command(Command::Untrack(proj_id.clone(), sender));
let policy_change = receiver
.recv()
.map_err(client::handle::Error::from)
.unwrap();
assert!(policy_change);
assert!(!alice.config().is_tracking(&proj_id));
}
#[test]
fn test_inventory_relay_bad_timestamp() {
let mut alice = Peer::new("alice", [7, 7, 7, 7], MockStorage::empty());