Skip to content

Commit

Permalink
Benchmarking: vtoken-voting (#1034)
Browse files Browse the repository at this point in the history
* Fix

* Add trait `DerivativeAccountHandler`

* Impl `DerivativeAccountHandler`

* Fix benchmark

* Update DerivativeAccountHandler implement

* Fix

* Fix benchmark

* Fix benchmark

* Fix benchmark `on_initialize`

* Update weights.rs

* Update release.yml

* Fix weight

* Fix XCM weight

* More tests

* Fix

* Fix
  • Loading branch information
ark930 authored Sep 8, 2023
1 parent 174717b commit 307cf70
Show file tree
Hide file tree
Showing 18 changed files with 892 additions and 263 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ jobs:
${{ github.workspace }}/artifacts/bifrost_polkadot_runtime.compact.compressed.wasm
- uses: 8398a7/action-slack@v3
if: failure()
if: always()
with:
status: ${{ job.status }}
fields: repo,author,eventName,workflow,ref,commit
Expand Down
3 changes: 2 additions & 1 deletion Cargo.lock

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

Original file line number Diff line number Diff line change
Expand Up @@ -67,33 +67,28 @@ fn relaychain_transact_works() {
});

Bifrost::execute_with(|| {
// QueryStatus::Pending { responder: V3(MultiLocation { parents: 1, interior: Here }),
// maybe_match_querier: Some(V3(MultiLocation { parents: 0, interior: Here })),
// maybe_notify: Some((0, 7)), timeout: 100 } let query_id =
let notify_vote_call_weight = notify_vote_call.get_dispatch_info().weight;
let query_id = pallet_xcm::Pallet::<Runtime>::new_notify_query(
MultiLocation::parent(),
notify_vote_call,
100u32.into(),
Here,
);

// QueryResponse { query_id: 0, response: DispatchResult(Success), max_weight: Weight {
// ref_time: 4000000000, proof_size: 0 }, querier: Some(MultiLocation { parents: 0,
// interior: Here }) }
let asset: MultiAsset =
MultiAsset { id: Concrete(MultiLocation::here()), fun: Fungible(4000000000) };
MultiAsset { id: Concrete(MultiLocation::here()), fun: Fungible(517318631) };
let msg = Xcm(vec![
WithdrawAsset(asset.clone().into()),
BuyExecution { fees: asset, weight_limit: Unlimited },
Transact {
origin_kind: OriginKind::SovereignAccount,
require_weight_at_most: Weight::from_parts(4000000000, 100000),
require_weight_at_most: Weight::from_parts(961496000, 83866),
call: vote_call.encode().into(),
},
ReportTransactStatus(QueryResponseInfo {
destination: MultiLocation::from(X1(Parachain(2001))),
query_id,
max_weight: Weight::from_parts(3000000, 0),
max_weight: notify_vote_call_weight,
}),
RefundSurplus,
DepositAsset {
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/bifrost-kusama/src/vtoken_voting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn vote_works() {
RuntimeOrigin::root(),
MultiAddress::Id(ALICE.into()),
VKSM,
10_000_000_000_000u64.into(),
u64::MAX.into(),
Zero::zero(),
));
let token = CurrencyId::to_token(&vtoken).unwrap();
Expand All @@ -86,7 +86,7 @@ fn vote_works() {
delegator_active_staking_maximum: 0u32.into(),
validators_reward_maximum: 0u32,
delegation_amount_minimum: 0u32.into(),
delegators_maximum: 10u16,
delegators_maximum: u16::MAX,
validators_maximum: 0u16,
})
));
Expand Down
1 change: 1 addition & 0 deletions node/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ with-bifrost-runtime = [
with-bifrost-kusama-runtime = []
with-bifrost-polkadot-runtime = []
with-all-runtime = ["with-bifrost-runtime"]
runtime-benchmarks = []
2 changes: 2 additions & 0 deletions node/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ pub type TrieIndex = u32;
/// Distribution Id
pub type DistributionId = u32;

pub type DerivativeIndex = u16;

#[derive(
Encode, Decode, Eq, PartialEq, Copy, Clone, RuntimeDebug, PartialOrd, Ord, scale_info::TypeInfo,
)]
Expand Down
27 changes: 25 additions & 2 deletions node/primitives/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
#![allow(clippy::unnecessary_cast)]

use crate::{
AssetIds, ExtraFeeInfo, LeasePeriod, ParaId, PoolId, RedeemType, TokenId, TokenSymbol,
XcmInterfaceOperation,
AssetIds, DerivativeIndex, ExtraFeeInfo, LeasePeriod, ParaId, PoolId, RedeemType, TokenId,
TokenSymbol, XcmInterfaceOperation,
};
use codec::{Decode, Encode, FullCodec};
use frame_support::{
Expand Down Expand Up @@ -463,3 +463,26 @@ where
pub trait FeeGetter<RuntimeCall> {
fn get_fee_info(call: &RuntimeCall) -> ExtraFeeInfo;
}

pub trait DerivativeAccountHandler<CurrencyId, Balance> {
fn check_derivative_index_exists(token: CurrencyId, derivative_index: DerivativeIndex) -> bool;

fn get_multilocation(
token: CurrencyId,
derivative_index: DerivativeIndex,
) -> Option<xcm::v3::MultiLocation>;

fn get_stake_info(
token: CurrencyId,
derivative_index: DerivativeIndex,
) -> Option<(Balance, Balance)>;

#[cfg(feature = "runtime-benchmarks")]
fn init_minimums_and_maximums(token: CurrencyId);

#[cfg(feature = "runtime-benchmarks")]
fn new_delegator_ledger(token: CurrencyId, who: xcm::v3::MultiLocation);

#[cfg(feature = "runtime-benchmarks")]
fn add_delegator(token: CurrencyId, index: DerivativeIndex, who: xcm::v3::MultiLocation);
}
1 change: 1 addition & 0 deletions pallets/slp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,6 @@ runtime-benchmarks = [
"frame-system/runtime-benchmarks",
"pallet-xcm/runtime-benchmarks",
"xcm-builder/runtime-benchmarks",
"node-primitives/runtime-benchmarks",
]
try-runtime = [ "frame-support/try-runtime" ]
77 changes: 74 additions & 3 deletions pallets/slp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ pub use crate::{
Junctions::X1,
};
use cumulus_primitives_core::{relay_chain::HashT, ParaId};
use frame_support::{pallet_prelude::*, weights::Weight};
use frame_support::{pallet_prelude::*, traits::Contains, weights::Weight};
use frame_system::{
pallet_prelude::{BlockNumberFor, OriginFor},
RawOrigin,
};
use node_primitives::{
currency::{BNC, KSM, MOVR, PHA},
traits::XcmDestWeightAndFeeHandler,
CurrencyId, CurrencyIdExt, SlpOperator, TimeUnit, VtokenMintingOperator,
XcmInterfaceOperation as XcmOperation, ASTR, DOT, FIL, GLMR,
CurrencyId, CurrencyIdExt, DerivativeAccountHandler, DerivativeIndex, SlpOperator, TimeUnit,
VtokenMintingOperator, XcmInterfaceOperation as XcmOperation, ASTR, DOT, FIL, GLMR,
};
use orml_traits::MultiCurrency;
use parachain_staking::ParachainStakingInterface;
Expand Down Expand Up @@ -2383,3 +2383,74 @@ pub mod pallet {
}
}
}

pub struct DerivativeAccountProvider<T, F>(PhantomData<(T, F)>);

impl<T: Config, F: Contains<CurrencyIdOf<T>>>
DerivativeAccountHandler<CurrencyIdOf<T>, BalanceOf<T>> for DerivativeAccountProvider<T, F>
{
fn check_derivative_index_exists(
token: CurrencyIdOf<T>,
derivative_index: DerivativeIndex,
) -> bool {
Pallet::<T>::get_delegator_multilocation_by_index(token, derivative_index).is_some()
}

fn get_multilocation(
token: CurrencyIdOf<T>,
derivative_index: DerivativeIndex,
) -> Option<MultiLocation> {
Pallet::<T>::get_delegator_multilocation_by_index(token, derivative_index)
}

fn get_stake_info(
token: CurrencyIdOf<T>,
derivative_index: DerivativeIndex,
) -> Option<(BalanceOf<T>, BalanceOf<T>)> {
Self::get_multilocation(token, derivative_index).and_then(|location| {
Pallet::<T>::get_delegator_ledger(token, location).and_then(|ledger| match ledger {
Ledger::Substrate(l) if F::contains(&token) => Some((l.total, l.active)),
_ => None,
})
})
}

#[cfg(feature = "runtime-benchmarks")]
fn init_minimums_and_maximums(currency_id: CurrencyIdOf<T>) {
MinimumsAndMaximums::<T>::insert(
currency_id,
MinimumsMaximums {
delegator_bonded_minimum: 0u32.into(),
bond_extra_minimum: 0u32.into(),
unbond_minimum: 0u32.into(),
rebond_minimum: 0u32.into(),
unbond_record_maximum: 0u32,
validators_back_maximum: 0u32,
delegator_active_staking_maximum: 0u32.into(),
validators_reward_maximum: 0u32,
delegation_amount_minimum: 0u32.into(),
delegators_maximum: u16::MAX,
validators_maximum: 0u16,
},
);
}

#[cfg(feature = "runtime-benchmarks")]
fn new_delegator_ledger(currency_id: CurrencyIdOf<T>, who: MultiLocation) {
DelegatorLedgers::<T>::insert(
currency_id,
&who,
Ledger::Substrate(SubstrateLedger {
account: Parent.into(),
total: u32::MAX.into(),
active: u32::MAX.into(),
unlocking: vec![],
}),
);
}

#[cfg(feature = "runtime-benchmarks")]
fn add_delegator(currency_id: CurrencyIdOf<T>, index: DerivativeIndex, who: MultiLocation) {
Pallet::<T>::inner_add_delegator(index, &who, currency_id).unwrap();
}
}
7 changes: 5 additions & 2 deletions pallets/vtoken-voting/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2021"
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
assert_matches = "1.3.0"
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = [
"derive",
] }
Expand All @@ -27,13 +28,12 @@ sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false }
xcm = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.42", default-features = false }
bifrost-slp = { path = "../slp", default-features = false }

[dev-dependencies]
env_logger = "0.10.0"
orml-tokens = "0.4.1-dev"
bifrost-currencies = { workspace = true }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42"}
pallet-xcm = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.42" }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42" }
xcm-executor = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.42" }
xcm-builder = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.42" }
Expand All @@ -54,12 +54,15 @@ std = [
"sp-io/std",
"sp-runtime/std",
"xcm/std",
"bifrost-slp/std",
]
kusama = []
polkadot = []
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"pallet-conviction-voting/runtime-benchmarks",
"pallet-xcm/runtime-benchmarks",
"xcm-builder/runtime-benchmarks",
"bifrost-slp/runtime-benchmarks",
]
try-runtime = ["frame-support/try-runtime"]
Loading

0 comments on commit 307cf70

Please sign in to comment.