diff --git a/Cargo.lock b/Cargo.lock index 6e2559994c044b30c296cf7be4df1ce2808e242a..582ce61c7d1bb2da5971ab36f4e0cfe6f02b1a1a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5849,6 +5849,7 @@ dependencies = [ "frame-system", "pallet-authority-members", "pallet-authorship", + "pallet-balances", "pallet-identity", "pallet-membership", "pallet-session", @@ -5857,6 +5858,8 @@ dependencies = [ "sp-core", "sp-distance", "sp-inherents", + "sp-io", + "sp-keystore", "sp-runtime", "sp-std 5.0.0", ] diff --git a/pallets/distance/Cargo.toml b/pallets/distance/Cargo.toml index b1910ffba55fb1d69ad8907e3cb9f6b577a7a3ce..5a04dab80072c91c3d51f535ba443d4581341a23 100644 --- a/pallets/distance/Cargo.toml +++ b/pallets/distance/Cargo.toml @@ -11,7 +11,9 @@ version = '1.0.0' [features] default = ['std'] -runtime-benchmarks = ['frame-benchmarking/runtime-benchmarks'] +runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks", +"pallet-balances", +] std = [ 'codec/std', 'frame-support/std', @@ -39,11 +41,7 @@ scale-info = { version = "2.1.1", default-features = false, features = [ "derive", ] } -[dependencies.frame-benchmarking] -default-features = false -git = 'https://github.com/duniter/substrate' -optional = true -branch = 'duniter-substrate-v0.9.42' +pallet-balances = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42', optional = true, default-features = false } [dependencies.codec] default-features = false @@ -51,6 +49,12 @@ features = ['derive'] package = 'parity-scale-codec' version = '3.1.5' +[dependencies.frame-benchmarking] +default-features = false +git = 'https://github.com/duniter/substrate' +optional = true +branch = 'duniter-substrate-v0.9.42' + [dependencies.frame-support] default-features = false git = 'https://github.com/duniter/substrate' @@ -95,3 +99,8 @@ branch = 'duniter-substrate-v0.9.42' [package.metadata.docs.rs] targets = ['x86_64-unknown-linux-gnu'] + +[dev-dependencies] +pallet-balances = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42' } +sp-io = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42' } +sp-keystore = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42' } diff --git a/pallets/distance/src/benchmarking.rs b/pallets/distance/src/benchmarking.rs new file mode 100644 index 0000000000000000000000000000000000000000..9c400ad451c23e74a10791485cd19f93ef906eaf --- /dev/null +++ b/pallets/distance/src/benchmarking.rs @@ -0,0 +1,74 @@ +// Copyright 2021-2023 Axiom-Team +// +// This file is part of Duniter-v2S. +// +// Duniter-v2S is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, version 3 of the License. +// +// Duniter-v2S is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with Duniter-v2S. If not, see <https://www.gnu.org/licenses/>. + +#![cfg(feature = "runtime-benchmarks")] + +use super::*; + +//use codec::Encode; +use codec::Encode; +use frame_benchmarking::{account, benchmarks, Vec}; +use frame_support::pallet_prelude::IsType; +use frame_support::traits::Currency; +use frame_support::traits::OnInitialize; +use frame_support::ConsensusEngineId; +use frame_system::RawOrigin; +use pallet_balances::Pallet as Balances; +use pallet_identity::Identities; +use sp_runtime::traits::{Bounded, One}; +use sp_runtime::DigestItem; +use sp_runtime::Perbill; + +use crate::Pallet; + +/*fn assert_has_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) { + frame_system::Pallet::<T>::assert_has_event(generic_event.into()); +}*/ + +benchmarks! { + where_clause { + where + T: pallet_balances::Config, T::Balance: From<u64>, + T::BlockNumber: From<u32>, + } + request_distance_evaluation { + let idty = T::IdtyIndex::one(); + let caller: T::AccountId = pallet_identity::Identities::<T>::get(idty).unwrap().owner_key; + let caller_origin: <T as frame_system::Config>::RuntimeOrigin = RawOrigin::Signed(caller.clone()).into(); + let _ = <Balances<T> as Currency<_>>::make_free_balance_be(&caller, T::Balance::max_value()); + }: _<T::RuntimeOrigin>(caller_origin.clone()) + verify { + assert!(IdentityDistanceStatus::<T>::get(&idty) == Some((caller, DistanceStatus::Pending)), "Request not added"); + } + update_evaluation { + let idty = T::IdtyIndex::one(); + let caller: T::AccountId = pallet_identity::Identities::<T>::get(idty).unwrap().owner_key; + let caller_origin: <T as frame_system::Config>::RuntimeOrigin = RawOrigin::Signed(caller.clone()).into(); + //let _ = <Balances<T> as Currency<_>>::make_free_balance_be(&caller, T::Balance::max_value()); + let computation_result = ComputationResult{distances: Vec::<Perbill>::default()}; + + /// TODO: set the author of the block. + /*let author = 1; + let mut digest:<frame_system::Pallet<T>>::digest = <frame_system::Pallet<T>>::digest(); + const TEST_ID: ConsensusEngineId = [1, 2, 3, 4]; + digest.logs.push(DigestItem::PreRuntime(TEST_ID, author.encode())); + //digest.logs.push(DigestItem::Seal(TEST_ID, author.encode())); + //digest.pop(); + frame_system::Pallet::<T>::reset_events(); + frame_system::Pallet::<T>::initialize(&0.into(), &Default::default(), &digest); + pallet_authorship::Pallet::<T>::author().unwrap();*/ + }: _<T::RuntimeOrigin>(RawOrigin::None.into(), computation_result) +} diff --git a/runtime/gdev/Cargo.toml b/runtime/gdev/Cargo.toml index c7a84e4994097901c544a920207e75ab31806e3f..58faf3aecbf7124d737e1b7fdf92672b59a91d78 100644 --- a/runtime/gdev/Cargo.toml +++ b/runtime/gdev/Cargo.toml @@ -27,6 +27,7 @@ runtime-benchmarks = [ 'pallet-balances/runtime-benchmarks', 'pallet-certification/runtime-benchmarks', 'pallet-collective/runtime-benchmarks', + 'pallet-distance/runtime-benchmarks', 'pallet-duniter-test-parameters/runtime-benchmarks', 'pallet-duniter-account/runtime-benchmarks', 'pallet-duniter-wot/runtime-benchmarks', diff --git a/runtime/gdev/src/lib.rs b/runtime/gdev/src/lib.rs index 5c4491398802f43fae43c87551fbad5483c5ca92..ac89084e6a7b3d012c36a0cac7c2fcd6b7c8e5f9 100644 --- a/runtime/gdev/src/lib.rs +++ b/runtime/gdev/src/lib.rs @@ -143,6 +143,7 @@ mod benches { define_benchmarks!( [pallet_certification, Cert] [pallet_certification, SmithCert] + [pallet_distance, Distance] [pallet_oneshot_account, OneshotAccount] [pallet_universal_dividend, UniversalDividend] [pallet_provide_randomness, ProvideRandomness]