Skip to content

Commit

Permalink
Improve vtoken-voting (#1101)
Browse files Browse the repository at this point in the history
* Fix tests compile errors

* Add method Utility::BatchAll

* Add function set_vote_cap_ratio

* Add function vote_cap

* Add function vote_to_capital

* Add function compute_delegator_total_vote

* Fix import

* Add function allocate_delegator_votes

* Fix function ensure_referendum_expired

* Fix test `notify_remove_delegator_vote_fail_works`

* Remove tests `split_voting_works` and `abstain_voting_works`

* Remove delegator_vote_roles in GenesisConfig

* Remove `set_delegator_role` and add `add_delegator`

* Fix `ensure_referendum_expired`

* Fix integration-tests vtoken_voting::vote_works

* Fix compiles

* Add function account_vote

* Optimize compute_delegator_total_vote

* Optimize allocate_delegator_votes

* Remove try_select_derivative_index, try_add_delegator_vote, try_sub_delegator_vote

* Minor fix

* Allow vote by any conviction

* Test `add_delegator_works`

* Fix test `successful_but_zero_conviction_vote_balance_can_be_unlocked`

* Fix integration-tests/bifrost-kusama/src/vtoken_voting.rs

* Allow vote by any conviction

* Fix

* Fix tests

* Fix copyright

* Fix benchmarking, clippy and copyright

* Fix
  • Loading branch information
ark930 authored Dec 13, 2023
1 parent fcb27f3 commit 9cd2ca6
Show file tree
Hide file tree
Showing 17 changed files with 559 additions and 407 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2019-2022 Liebi Technologies (UK) Ltd.
# Copyright (C) Liebi Technologies PTE. LTD.
# This file is part of Bifrost.

# Bifrost is free software: you can redistribute it and/or modify
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/bifrost-kusama/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ parachains-common = { workspace = true }
xcm-emulator = { workspace = true }

# Local
integration-tests-common = { path = "../common", default-features = false}
integration-tests-common = { path = "../common", default-features = false }
bifrost-slp = { workspace = true }
bifrost-vtoken-voting = { workspace = true }
bifrost-primitives = { workspace = true }
Expand Down
13 changes: 7 additions & 6 deletions integration-tests/bifrost-kusama/src/vtoken_voting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ use bifrost_primitives::{
KSM,
};
use bifrost_slp::{Ledger, MinimumsMaximums, SubstrateLedger};
use bifrost_vtoken_voting::{AccountVote, TallyOf, VoteRole};
use bifrost_vtoken_voting::{AccountVote, TallyOf};
use frame_support::{
assert_ok,
dispatch::RawOrigin,
traits::{schedule::DispatchTime, StorePreimage},
weights::Weight,
};
use integration_tests_common::{BifrostKusama, BifrostKusamaAlice, Kusama, KusamaAlice};
use pallet_conviction_voting::{Conviction, Vote};
use pallet_conviction_voting::Vote;
use sp_runtime::Perbill;
use xcm::v3::Parent;
use xcm_emulator::{Parachain, RelayChain, TestExt};

Expand Down Expand Up @@ -138,12 +139,12 @@ fn vote_works() {
})))
));

assert_ok!(VtokenVoting::set_delegator_role(
assert_ok!(VtokenVoting::set_vote_cap_ratio(
RuntimeOrigin::root(),
vtoken,
5,
VoteRole::Standard { aye: true, conviction: Conviction::Locked5x },
Perbill::from_percent(90)
));
assert_ok!(VtokenVoting::add_delegator(RuntimeOrigin::root(), vtoken, 5));
assert_ok!(VtokenVoting::set_vote_locking_period(RuntimeOrigin::root(), vtoken, 0));
assert_ok!(VtokenVoting::set_undeciding_timeout(RuntimeOrigin::root(), vtoken, 100));

Expand All @@ -165,7 +166,7 @@ fn vote_works() {
who: _,
vtoken: VKSM,
poll_index: 0,
new_vote: _,
token_vote: _,
delegator_vote: _,
})
)));
Expand Down
2 changes: 1 addition & 1 deletion pallets/flexible-fee/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub(crate) const VTOKENVOTING_VOTE_CALL: <Test as frame_system::Config>::Runtime
RuntimeCall::VtokenVoting(bifrost_vtoken_voting::Call::vote {
vtoken: VKSM,
poll_index: 1u32,
vote: AccountVote::Split { aye: 1, nay: 1 },
vtoken_vote: AccountVote::Split { aye: 1, nay: 1 },
});

impl bifrost_asset_registry::Config for Test {
Expand Down
2 changes: 1 addition & 1 deletion pallets/leverage-staking/src/weights.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is part of Bifrost.

// Copyright (C) 2019-2022 Liebi Technologies (UK) Ltd.
// Copyright (C) Liebi Technologies PTE. LTD.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
Expand Down
1 change: 1 addition & 0 deletions pallets/vtoken-voting/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ std = [
"sp-io/std",
"sp-runtime/std",
"xcm/std",
"xcm-builder/std",
]
kusama = []
polkadot = []
Expand Down
38 changes: 24 additions & 14 deletions pallets/vtoken-voting/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,8 @@ fn init_vote<T: Config>(vtoken: CurrencyIdOf<T>) -> Result<(), BenchmarkError> {
T::DerivativeAccount::add_delegator(token, derivative_index, Parent.into());
T::DerivativeAccount::new_delegator_ledger(token, Parent.into());
Pallet::<T>::set_undeciding_timeout(RawOrigin::Root.into(), vtoken, Zero::zero())?;
Pallet::<T>::set_delegator_role(
RawOrigin::Root.into(),
vtoken,
derivative_index,
VoteRole::Standard { aye: true, conviction: Conviction::Locked1x },
)?;
Pallet::<T>::add_delegator(RawOrigin::Root.into(), vtoken, derivative_index)?;
Pallet::<T>::set_vote_cap_ratio(RawOrigin::Root.into(), vtoken, Perbill::from_percent(10))?;

Ok(())
}
Expand Down Expand Up @@ -190,6 +186,13 @@ mod benchmarks {

init_vote::<T>(vtoken)?;
Pallet::<T>::vote(origin.clone().into(), vtoken, poll_index, vote)?;

let notify_origin =
T::ResponseOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
let query_id = 0u64;
let response = Response::DispatchResult(MaybeErrorCode::Success);
Pallet::<T>::notify_vote(notify_origin, query_id, response)?;

Pallet::<T>::set_referendum_status(
RawOrigin::Root.into(),
vtoken,
Expand Down Expand Up @@ -237,12 +240,11 @@ mod benchmarks {
}

#[benchmark]
pub fn set_delegator_role() -> Result<(), BenchmarkError> {
pub fn add_delegator() -> Result<(), BenchmarkError> {
let origin =
T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
let vtoken = VKSM;
let derivative_index = 10;
let vote_role = VoteRole::SplitAbstain;

init_vote::<T>(vtoken)?;
T::DerivativeAccount::add_delegator(
Expand All @@ -252,12 +254,7 @@ mod benchmarks {
);

#[extrinsic_call]
_(
origin as <T as frame_system::Config>::RuntimeOrigin,
vtoken,
derivative_index,
vote_role,
);
_(origin as <T as frame_system::Config>::RuntimeOrigin, vtoken, derivative_index);

Ok(())
}
Expand Down Expand Up @@ -353,6 +350,19 @@ mod benchmarks {
Ok(())
}

#[benchmark]
pub fn set_vote_cap_ratio() -> Result<(), BenchmarkError> {
let origin =
T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
let vtoken = VKSM;
let vote_cap_ratio = Perbill::from_percent(10);

#[extrinsic_call]
_(origin as <T as frame_system::Config>::RuntimeOrigin, vtoken, vote_cap_ratio);

Ok(())
}

// This line generates test cases for benchmarking, and could be run by:
// `cargo test -p pallet-example-basic --all-features`, you will see one line per case:
// `test benchmarking::bench_sort_vector ... ok`
Expand Down
10 changes: 9 additions & 1 deletion pallets/vtoken-voting/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use crate::{AccountVote, BalanceOf, Config, DerivativeIndex, PollIndex};
use parity_scale_codec::{Decode, Encode};
use sp_runtime::{traits::StaticLookup, RuntimeDebug};
use sp_std::boxed::Box;
use sp_std::prelude::*;

#[cfg(feature = "kusama")]
pub use kusama::*;
Expand Down Expand Up @@ -83,14 +83,22 @@ impl<T: Config> ConvictionVotingCall<T> for RelayCall<T> {
pub enum Utility<Call> {
#[codec(index = 1)]
AsDerivative(DerivativeIndex, Box<Call>),
#[codec(index = 2)]
BatchAll(Vec<Call>),
}

pub trait UtilityCall<Call> {
fn as_derivative(derivative_index: DerivativeIndex, call: Call) -> Call;

fn batch_all(calls: Vec<Call>) -> Call;
}

impl<T: Config> UtilityCall<RelayCall<T>> for RelayCall<T> {
fn as_derivative(derivative_index: DerivativeIndex, call: Self) -> Self {
Self::Utility(Utility::AsDerivative(derivative_index, Box::new(call)))
}

fn batch_all(calls: Vec<Self>) -> Self {
Self::Utility(Utility::BatchAll(calls))
}
}
Loading

0 comments on commit 9cd2ca6

Please sign in to comment.