Skip to content

Commit

Permalink
Bifrost v0.15.2 (#1536)
Browse files Browse the repository at this point in the history
* Bifrost v0.15.2

* Separate the logic for retrieving the current block for different tok… (#1538)

* Separate the logic for retrieving the current block for different tokens.

* change ReferendumTimeout max number to 100

* Fix vtoken voting (#1540)

* Separate the logic for retrieving the current block for different tokens.

* change ReferendumTimeout max number to 100

* change RelaychainBlockNumber to agent block number

---------

Co-authored-by: Edwin <lark930@gmail.com>

---------

Co-authored-by: Edwin <lark930@gmail.com>
  • Loading branch information
SunTiebing and ark930 authored Dec 5, 2024
1 parent 43d4d1c commit 57f63ee
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 71 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion node/cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bifrost-cli"
version = "0.15.1"
version = "0.15.2"
authors = ["Liebi Technologies <bifrost@liebi.com>"]
description = "Bifrost Parachain Node"
build = "build.rs"
Expand Down
4 changes: 4 additions & 0 deletions pallets/vtoken-voting/src/agents/bifrost_agent/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,8 @@ impl<T: Config> VotingAgent<T> for BifrostAgent<T> {
Ok(<BifrostCall<T> as ConvictionVotingCall<T>>::remove_vote(Some(class), poll_index)
.encode())
}

fn block_number(&self) -> BlockNumberFor<T> {
T::LocalBlockNumberProvider::current_block_number()
}
}
4 changes: 4 additions & 0 deletions pallets/vtoken-voting/src/agents/relaychain_agent/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,8 @@ impl<T: Config> VotingAgent<T> for RelaychainAgent<T> {
)
.encode())
}

fn block_number(&self) -> BlockNumberFor<T> {
BlockNumberFor::<T>::from(T::RelaychainBlockNumberProvider::current_block_number())
}
}
134 changes: 81 additions & 53 deletions pallets/vtoken-voting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub use weights::WeightInfo;
use xcm::v4::{prelude::*, Location, Weight as XcmWeight};

const CONVICTION_VOTING_ID: LockIdentifier = *b"vtvoting";
const SUPPORTED_VTOKENS: &[CurrencyId] = &[VKSM, VDOT, VBNC];

type PollIndex = u32;
type PollClass = u16;
Expand Down Expand Up @@ -156,6 +157,8 @@ pub mod pallet {
type WeightInfo: WeightInfo;

type PalletsOrigin: CallerTrait<Self::AccountId>;

type LocalBlockNumberProvider: BlockNumberProvider<BlockNumber = BlockNumberFor<Self>>;
}

#[pallet::event]
Expand Down Expand Up @@ -417,7 +420,7 @@ pub mod pallet {
_,
Twox64Concat,
BlockNumberFor<T>,
BoundedVec<(CurrencyIdOf<T>, PollIndex), ConstU32<50>>,
BoundedVec<(CurrencyIdOf<T>, PollIndex), ConstU32<100>>,
ValueQuery,
>;

Expand Down Expand Up @@ -459,39 +462,56 @@ pub mod pallet {

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_idle(n: BlockNumberFor<T>, remaining_weight: Weight) -> Weight {
fn on_idle(
bifrost_current_block_number: BlockNumberFor<T>,
remaining_weight: Weight,
) -> Weight {
let db_weight = T::DbWeight::get();
let mut used_weight = db_weight.reads(3);
if remaining_weight.any_lt(used_weight) ||
n % T::ReferendumCheckInterval::get() != Zero::zero()
bifrost_current_block_number % T::ReferendumCheckInterval::get() != Zero::zero()
{
return Weight::zero();
}
let relay_current_block_number =
T::RelaychainBlockNumberProvider::current_block_number();

for relay_block_number in ReferendumTimeout::<T>::iter_keys() {
if relay_current_block_number >= relay_block_number {
let info_list = ReferendumTimeout::<T>::get(relay_block_number);
for block_number in ReferendumTimeout::<T>::iter_keys() {
if relay_current_block_number >= block_number ||
bifrost_current_block_number >= block_number
{
let info_list = ReferendumTimeout::<T>::get(block_number);
let len = info_list.len() as u64;
let temp_weight = db_weight.reads_writes(len, len) + db_weight.writes(1);
if remaining_weight.any_lt(used_weight + temp_weight) {
return used_weight;
}
used_weight += temp_weight;
for (vtoken, poll_index) in info_list.iter() {
let current_block_number = match Self::get_agent_block_number(&vtoken) {
Ok(block_number) => block_number,
Err(e) => {
log::error!(
"Failed to get {:?} current block number: {:?}",
vtoken,
e
);
// exit the current loop
continue;
},
};

ReferendumInfoFor::<T>::mutate(vtoken, poll_index, |maybe_info| {
match maybe_info {
Some(info) =>
if let ReferendumInfo::Ongoing(_) = info {
*info =
ReferendumInfo::Completed(relay_current_block_number);
*info = ReferendumInfo::Completed(current_block_number);
},
None => {},
}
});
}
ReferendumTimeout::<T>::remove(relay_block_number);
ReferendumTimeout::<T>::remove(block_number);
}
}

Expand Down Expand Up @@ -637,10 +657,12 @@ pub mod pallet {
Self::ensure_vtoken(&vtoken)?;
Self::ensure_referendum_completed(vtoken, poll_index)?;

let current_block_number = Self::get_agent_block_number(&vtoken)?;

ReferendumInfoFor::<T>::insert(
vtoken,
poll_index,
ReferendumInfo::Killed(T::RelaychainBlockNumberProvider::current_block_number()),
ReferendumInfo::Killed(current_block_number),
);

Self::deposit_event(Event::<T>::ReferendumKilled { vtoken, poll_index });
Expand Down Expand Up @@ -752,41 +774,7 @@ pub mod pallet {
Self::deposit_event(Event::<T>::VoteNotified { vtoken, poll_index, success });
}

if let Some((vtoken, poll_index)) = PendingReferendumInfo::<T>::get(query_id) {
if success {
ReferendumInfoFor::<T>::try_mutate_exists(
vtoken,
poll_index,
|maybe_info| -> DispatchResult {
if let Some(info) = maybe_info {
if let ReferendumInfo::Ongoing(status) = info {
let relay_current_block_number =
T::RelaychainBlockNumberProvider::current_block_number();
status.submitted = Some(relay_current_block_number);
ReferendumTimeout::<T>::mutate(
relay_current_block_number.saturating_add(
UndecidingTimeout::<T>::get(vtoken)
.ok_or(Error::<T>::NoData)?,
),
|ref_vec| {
ref_vec
.try_push((vtoken, poll_index))
.map_err(|_| Error::<T>::TooMany)
},
)?;
Self::deposit_event(Event::<T>::ReferendumInfoCreated {
vtoken,
poll_index,
info: info.clone(),
});
}
}
Ok(())
},
)?;
} else {
ReferendumInfoFor::<T>::remove(vtoken, poll_index);
}
if let Some((_, _)) = PendingReferendumInfo::<T>::get(query_id) {
PendingReferendumInfo::<T>::remove(query_id);
}

Expand Down Expand Up @@ -862,6 +850,7 @@ pub mod pallet {
if let Some((old_vote, vtoken_balance)) = maybe_old_vote {
Self::try_vote(&who, vtoken, poll_index, old_vote, vtoken_balance)?;
}
ReferendumInfoFor::<T>::remove(vtoken, poll_index);
} else {
if !VoteDelegatorFor::<T>::contains_key((&who, vtoken, poll_index)) {
VoteDelegatorFor::<T>::insert((&who, vtoken, poll_index), derivative_index);
Expand All @@ -876,6 +865,36 @@ pub mod pallet {
}
Ok(())
})?;

ReferendumInfoFor::<T>::try_mutate_exists(
vtoken,
poll_index,
|maybe_info| -> DispatchResult {
if let Some(info) = maybe_info {
if let ReferendumInfo::Ongoing(status) = info {
let current_block_number = Self::get_agent_block_number(&vtoken)?;
status.submitted = Some(current_block_number);
ReferendumTimeout::<T>::mutate(
current_block_number.saturating_add(
UndecidingTimeout::<T>::get(vtoken)
.ok_or(Error::<T>::NoData)?,
),
|ref_vec| {
ref_vec
.try_push((vtoken, poll_index))
.map_err(|_| Error::<T>::TooMany)
},
)?;
Self::deposit_event(Event::<T>::ReferendumInfoCreated {
vtoken,
poll_index,
info: info.clone(),
});
}
}
Ok(())
},
)?;
}

Ok(())
Expand Down Expand Up @@ -1131,7 +1150,8 @@ pub mod pallet {
.ok_or(Error::<T>::NoData)?
.saturating_mul(lock_periods.into()),
);
let now = T::RelaychainBlockNumberProvider::current_block_number();

let now = Self::get_agent_block_number(&vtoken)?;
if now < unlock_at {
ensure!(
matches!(scope, UnvoteScope::Any),
Expand All @@ -1155,8 +1175,9 @@ pub mod pallet {
/// Rejig the lock on an account. It will never get more stringent (since that would
/// indicate a security hole) but may be reduced from what they are currently.
pub(crate) fn update_lock(who: &AccountIdOf<T>, vtoken: CurrencyIdOf<T>) -> DispatchResult {
let current_block = Self::get_agent_block_number(&vtoken)?;
let lock_needed = VotingFor::<T>::mutate(who, |voting| {
voting.rejig(T::RelaychainBlockNumberProvider::current_block_number());
voting.rejig(current_block);
voting.locked_balance()
});

Expand Down Expand Up @@ -1211,7 +1232,7 @@ pub mod pallet {
}

fn ensure_vtoken(vtoken: &CurrencyIdOf<T>) -> Result<(), DispatchError> {
ensure!([VKSM, VDOT, VBNC].contains(vtoken), Error::<T>::VTokenNotSupport);
ensure!(SUPPORTED_VTOKENS.contains(vtoken), Error::<T>::VTokenNotSupport);
Ok(())
}

Expand Down Expand Up @@ -1259,8 +1280,10 @@ pub mod pallet {
(Some(ReferendumInfo::Completed(moment)), Some((lock_periods, _balance))) => {
let locking_period =
VoteLockingPeriod::<T>::get(vtoken).ok_or(Error::<T>::NoData)?;

let current_block = Self::get_agent_block_number(&vtoken)?;
ensure!(
T::RelaychainBlockNumberProvider::current_block_number() >=
current_block >=
moment.saturating_add(
locking_period.saturating_mul(lock_periods.into())
),
Expand All @@ -1269,10 +1292,8 @@ pub mod pallet {
Ok(())
},
(Some(ReferendumInfo::Completed(moment)), None) => {
ensure!(
T::RelaychainBlockNumberProvider::current_block_number() >= moment,
Error::<T>::NotExpired
);
let current_block = Self::get_agent_block_number(&vtoken)?;
ensure!(current_block >= moment, Error::<T>::NotExpired);
Ok(())
},
_ => Err(Error::<T>::NotExpired.into()),
Expand Down Expand Up @@ -1439,6 +1460,13 @@ pub mod pallet {
}
}

pub(crate) fn get_agent_block_number(
currency_id: &CurrencyIdOf<T>,
) -> Result<BlockNumberFor<T>, Error<T>> {
let voting_agent = Self::get_voting_agent(&currency_id)?;
Ok(voting_agent.block_number())
}

pub(crate) fn convert_vtoken_to_dest_location(
vtoken: CurrencyId,
) -> Result<Location, Error<T>> {
Expand Down
1 change: 1 addition & 0 deletions pallets/vtoken-voting/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ impl vtoken_voting::Config for Runtime {
type ReferendumCheckInterval = ReferendumCheckInterval;
type WeightInfo = ();
type PalletsOrigin = OriginCaller;
type LocalBlockNumberProvider = System;
}

pub fn new_test_ext() -> sp_io::TestExternalities {
Expand Down
4 changes: 2 additions & 2 deletions pallets/vtoken-voting/src/tests/common_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ fn notify_vote_success_max_works() {
fn notify_vote_success_exceed_max_fail() {
for &vtoken in TOKENS {
new_test_ext().execute_with(|| {
for poll_index in 0..50 {
for poll_index in 0..100 {
assert_ok!(VtokenVoting::vote(
RuntimeOrigin::signed(ALICE),
vtoken,
Expand All @@ -909,7 +909,7 @@ fn notify_vote_success_exceed_max_fail() {
response_success()
));
}
let poll_index = 50;
let poll_index = 100;
assert_ok!(VtokenVoting::vote(
RuntimeOrigin::signed(ALICE),
vtoken,
Expand Down
Loading

0 comments on commit 57f63ee

Please sign in to comment.