From 424a9db1a43dabcc98fdcdc4c9d19746653b87b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20Eng=C3=A9libert?= <tuxmain@zettascript.org> Date: Wed, 10 Jan 2024 16:54:32 +0100 Subject: [PATCH] distance: move max-depth to runtime constant (nodes/rust/duniter-v2s!226) * resolve metadata conflicts * feat(distance): move max-depth to runtime constant --- distance-oracle/src/api.rs | 7 +++++++ distance-oracle/src/lib.rs | 10 ++++------ distance-oracle/src/main.rs | 4 ---- distance-oracle/src/mock.rs | 4 ++++ distance-oracle/src/tests.rs | 16 ++++------------ docs/user/distance.md | 10 ++-------- end2end-tests/tests/common/distance.rs | 1 - node/src/cli.rs | 3 --- node/src/command.rs | 1 - pallets/distance/src/lib.rs | 4 ++++ pallets/distance/src/mock.rs | 1 + resources/metadata.scale | Bin 132114 -> 132253 bytes runtime/common/src/pallets_config.rs | 1 + 13 files changed, 27 insertions(+), 35 deletions(-) diff --git a/distance-oracle/src/api.rs b/distance-oracle/src/api.rs index 0ddcaba1d..14e67b710 100644 --- a/distance-oracle/src/api.rs +++ b/distance-oracle/src/api.rs @@ -81,6 +81,13 @@ pub async fn evaluation_block(client: &Client, parent_hash: H256) -> H256 { .expect("No evaluation block") } +pub async fn max_referee_distance(client: &Client) -> u32 { + client + .constants() + .at(&runtime::constants().distance().max_referee_distance()) + .expect("Cannot fetch referee distance") +} + pub async fn member_iter(client: &Client, evaluation_block: H256) -> MemberIter { MemberIter( client diff --git a/distance-oracle/src/lib.rs b/distance-oracle/src/lib.rs index bc8c432ba..daff57e90 100644 --- a/distance-oracle/src/lib.rs +++ b/distance-oracle/src/lib.rs @@ -72,7 +72,6 @@ impl From<u64> for Tip { pub struct Settings { pub evaluation_result_dir: PathBuf, - pub max_depth: u32, pub rpc_url: String, } @@ -80,7 +79,6 @@ impl Default for Settings { fn default() -> Self { Self { evaluation_result_dir: PathBuf::from("/tmp/duniter/chains/gdev/distance"), - max_depth: 5, rpc_url: String::from("ws://127.0.0.1:9944"), } } @@ -151,6 +149,8 @@ pub async fn run( ) -> Option<(Vec<sp_runtime::Perbill>, u32, PathBuf)> { let parent_hash = api::parent_hash(client).await; + let max_depth = api::max_referee_distance(client).await; + let current_session = api::current_session(client, parent_hash).await; // Fetch the pending identities @@ -196,9 +196,7 @@ pub async fn run( members.insert(member_idty, 0); } - let min_certs_for_referee = (members.len() as f32) - .powf(1. / (settings.max_depth as f32)) - .ceil() as u32; + let min_certs_for_referee = (members.len() as f32).powf(1. / (max_depth as f32)).ceil() as u32; // idty -> received certs let mut received_certs = FnvHashMap::<IdtyIndex, Vec<IdtyIndex>>::default(); @@ -236,7 +234,7 @@ pub async fn run( .0 .as_slice() .par_iter() - .map(|(idty, _)| distance_rule(&received_certs, &referees, settings.max_depth, *idty)) + .map(|(idty, _)| distance_rule(&received_certs, &referees, max_depth, *idty)) .collect(); Some((evaluation, current_session, evaluation_result_path)) diff --git a/distance-oracle/src/main.rs b/distance-oracle/src/main.rs index d60668e87..9e3644a8b 100644 --- a/distance-oracle/src/main.rs +++ b/distance-oracle/src/main.rs @@ -20,9 +20,6 @@ use clap::Parser; struct Cli { #[clap(short = 'd', long, default_value = "/tmp/duniter/chains/gdev/distance")] evaluation_result_dir: String, - /// Maximum depth to explore the WoT graph for referees - #[clap(short = 'D', long, default_value = "5")] - max_depth: u32, #[clap(short = 'u', long, default_value = "ws://127.0.0.1:9944")] rpc_url: String, /// Log level (off, error, warn, info, debug, trace) @@ -43,7 +40,6 @@ async fn main() { &distance_oracle::api::client(cli.rpc_url.clone()).await, distance_oracle::Settings { evaluation_result_dir: cli.evaluation_result_dir.into(), - max_depth: cli.max_depth, rpc_url: cli.rpc_url, }, ) diff --git a/distance-oracle/src/mock.rs b/distance-oracle/src/mock.rs index 1ecc7a74c..805ac607b 100644 --- a/distance-oracle/src/mock.rs +++ b/distance-oracle/src/mock.rs @@ -76,6 +76,10 @@ pub async fn evaluation_block(_client: &Client, _parent_hash: H256) -> H256 { Default::default() } +pub async fn max_referee_distance(_client: &Client) -> u32 { + 5 +} + pub async fn member_iter(client: &Client, _evaluation_block: H256) -> MemberIter { MemberIter(client.wot.get_enabled().into_iter()) } diff --git a/distance-oracle/src/tests.rs b/distance-oracle/src/tests.rs index f2f6d77ed..a1c197fa8 100644 --- a/distance-oracle/src/tests.rs +++ b/distance-oracle/src/tests.rs @@ -26,7 +26,6 @@ use std::{fs::File, io::Read}; async fn test_distance_against_v1() { let wot = wot_from_v1_file(); let n = wot.size(); - let max_depth = 5; let min_certs_for_referee = (wot.get_enabled().len() as f32).powf(1. / 5.).ceil() as u32; // Reference implementation @@ -44,7 +43,7 @@ async fn test_distance_against_v1() { dubp_wot::operations::distance::WotDistanceParameters { node: i, sentry_requirement: min_certs_for_referee, - step_max: max_depth, + step_max: 5, x_percent: 0.8, }, ) @@ -59,16 +58,9 @@ async fn test_distance_against_v1() { client.pool_len = n; let t_a = std::time::Instant::now(); - let results = crate::run( - &client, - &crate::Settings { - max_depth, - ..Default::default() - }, - false, - ) - .await - .unwrap(); + let results = crate::run(&client, &Default::default(), false) + .await + .unwrap(); println!("new time: {}", t_a.elapsed().as_millis()); assert_eq!(results.0.len(), n); diff --git a/docs/user/distance.md b/docs/user/distance.md index 3dea9edf2..1f578f250 100644 --- a/docs/user/distance.md +++ b/docs/user/distance.md @@ -10,15 +10,9 @@ Any smith member authoring blocks can run a distance evaluation oracle. It is be The simplest way is to run the oracle on the same machine as Duniter. -Build the oracle: - - cargo build --release -p distance-oracle - -It will be available at `./target/release/distance-oracle`. Move it to somewhere appropriate. - Add this line to your cron with the command `crontab -e`: (add option `-u <user>` to edit another user's cron) - 4,24,44 * * * * nice -n 2 /absolute/path/to/distance-oracle + 4,24,44 * * * * nice -n 2 /absolute/path/to/duniter distance-oracle The precise hours don't matter so you can pick random values, but it should run at least one time per hour, and running it more often decreases the risk of problem in case of missing blocks or temporary network failure. @@ -28,4 +22,4 @@ The `nice -n 2` lowers the oracle's priority, so that Duniter has the priority e ### Additional Duniter configuration -Duniter should keep states at least one session old, that it 600 blocks (while 256 is the default). Use the option `--state-pruning 600` if your node is not already an archive (`--state-pruning archive`). +Duniter should keep states at least one session old, that is 600 blocks (while 256 is the default). Use the option `--state-pruning 600` if your node is not already an archive (`--state-pruning archive`). diff --git a/end2end-tests/tests/common/distance.rs b/end2end-tests/tests/common/distance.rs index e7ff6d6d4..c06b52919 100644 --- a/end2end-tests/tests/common/distance.rs +++ b/end2end-tests/tests/common/distance.rs @@ -49,7 +49,6 @@ pub async fn run_oracle(client: &Client, origin: AccountKeyring, rpc_url: String &distance_oracle::api::client(rpc_url.clone()).await, &distance_oracle::Settings { evaluation_result_dir: PathBuf::default(), - max_depth: 5, rpc_url, }, false, diff --git a/node/src/cli.rs b/node/src/cli.rs index a6f6a25e9..effac655d 100644 --- a/node/src/cli.rs +++ b/node/src/cli.rs @@ -141,9 +141,6 @@ pub struct Completion { pub struct DistanceOracle { #[clap(short = 'd', long, default_value = "/tmp/duniter/chains/gdev/distance")] pub evaluation_result_dir: String, - /// Maximum depth to explore the WoT graph for referees - #[clap(short = 'D', long, default_value = "5")] - pub max_depth: u32, #[clap(short = 'u', long, default_value = "ws://127.0.0.1:9944")] pub rpc_url: String, } diff --git a/node/src/command.rs b/node/src/command.rs index 60aa193aa..e120906e7 100644 --- a/node/src/command.rs +++ b/node/src/command.rs @@ -310,7 +310,6 @@ pub fn run() -> sc_cli::Result<()> { &distance_oracle::api::client(cmd.rpc_url.clone()).await, distance_oracle::Settings { evaluation_result_dir: cmd.evaluation_result_dir.clone().into(), - max_depth: cmd.max_depth, rpc_url: cmd.rpc_url.clone(), }, ) diff --git a/pallets/distance/src/lib.rs b/pallets/distance/src/lib.rs index 0a13a751c..d7d131cd8 100644 --- a/pallets/distance/src/lib.rs +++ b/pallets/distance/src/lib.rs @@ -72,6 +72,10 @@ pub mod pallet { type EvaluationPrice: Get< <Self::Currency as frame_support::traits::Currency<Self::AccountId>>::Balance, >; + /// Maximum distance used to define referee's accessibility + /// Unused by runtime but needed by client distance oracle + #[pallet::constant] + type MaxRefereeDistance: Get<u32>; /// Minimum ratio of accessible referees #[pallet::constant] type MinAccessibleReferees: Get<Perbill>; diff --git a/pallets/distance/src/mock.rs b/pallets/distance/src/mock.rs index a05d58577..bcdbd095d 100644 --- a/pallets/distance/src/mock.rs +++ b/pallets/distance/src/mock.rs @@ -253,6 +253,7 @@ parameter_types! { impl pallet_distance::Config for Test { type Currency = Balances; type EvaluationPrice = frame_support::traits::ConstU64<1000>; + type MaxRefereeDistance = frame_support::traits::ConstU32<5>; type MinAccessibleReferees = MinAccessibleReferees; type ResultExpiration = frame_support::traits::ConstU32<720>; type RuntimeEvent = RuntimeEvent; diff --git a/resources/metadata.scale b/resources/metadata.scale index 7f579695264b0823a205907a6cfb8014e4f750cd..86dc5fa76adcc7628a8dd62903866e06603f0719 100644 GIT binary patch delta 165 zcmbQ#!7;a!qhSl<`L~Qb(=WVbw5#{<O{@q?O-n6GO?AmEE=kNwP8ASfWnf_7c%T53 z%FHdzRY-wKD3lhbrYMx;E2N~RW#*+S6hZW<7b_$tC#Mz{XC`IlWR_IkQ3%Zgt4yj? xC@Re>$;?evNGdH+$V*L40qO^eCg)_P=9PfV<pJBMkYALToRhkp?;RtH7y#*CJfQ#p delta 24 gcmbQ+$uX&eqhSl<`L~Q5(=WVbwA()O9V3ev0EJZwHvj+t diff --git a/runtime/common/src/pallets_config.rs b/runtime/common/src/pallets_config.rs index a9eedceef..e61c1996d 100644 --- a/runtime/common/src/pallets_config.rs +++ b/runtime/common/src/pallets_config.rs @@ -518,6 +518,7 @@ macro_rules! pallets_config { impl pallet_distance::Config for Runtime { type Currency = Balances; type EvaluationPrice = frame_support::traits::ConstU64<1000>; + type MaxRefereeDistance = frame_support::traits::ConstU32<5>; type MinAccessibleReferees = MinAccessibleReferees; type ResultExpiration = frame_support::traits::ConstU32<720>; type RuntimeEvent = RuntimeEvent; -- GitLab