Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removed MintReward and added PayLaneRewardFromAccount #1693

Merged
merged 3 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion bin/millau/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,8 @@ impl pallet_session::Config for Runtime {
impl pallet_bridge_relayers::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Reward = Balance;
type PaymentProcedure = bp_relayers::MintReward<pallet_balances::Pallet<Runtime>, AccountId>;
type PaymentProcedure =
bp_relayers::PayLaneRewardFromAccount<pallet_balances::Pallet<Runtime>, AccountId>;
type WeightInfo = ();
}

Expand Down
3 changes: 2 additions & 1 deletion bin/rialto-parachain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,8 @@ impl pallet_aura::Config for Runtime {
impl pallet_bridge_relayers::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Reward = Balance;
type PaymentProcedure = bp_relayers::MintReward<pallet_balances::Pallet<Runtime>, AccountId>;
type PaymentProcedure =
bp_relayers::PayLaneRewardFromAccount<pallet_balances::Pallet<Runtime>, AccountId>;
type WeightInfo = ();
}

Expand Down
3 changes: 2 additions & 1 deletion bin/rialto/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ impl pallet_authority_discovery::Config for Runtime {
impl pallet_bridge_relayers::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Reward = Balance;
type PaymentProcedure = bp_relayers::MintReward<pallet_balances::Pallet<Runtime>, AccountId>;
type PaymentProcedure =
bp_relayers::PayLaneRewardFromAccount<pallet_balances::Pallet<Runtime>, AccountId>;
type WeightInfo = ();
}

Expand Down
30 changes: 24 additions & 6 deletions modules/relayers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ mod tests {
use mock::{RuntimeEvent as TestEvent, *};

use crate::Event::RewardPaid;
use frame_support::{assert_noop, assert_ok, traits::fungible::Inspect};
use frame_support::{
assert_noop, assert_ok,
traits::fungible::{Inspect, Mutate},
};
use frame_system::{EventRecord, Pallet as System, Phase};
use sp_runtime::DispatchError;

Expand Down Expand Up @@ -232,16 +235,31 @@ mod tests {
}

#[test]
fn mint_reward_payment_procedure_actually_mints_tokens() {
fn pay_lane_reward_from_account_actually_pays_reward() {
type Balances = pallet_balances::Pallet<TestRuntime>;
type PayLaneRewardFromAccount = bp_relayers::PayLaneRewardFromAccount<Balances, AccountId>;

run_test(|| {
let lane0_rewards_account =
PayLaneRewardFromAccount::lane_rewards_account([0, 0, 0, 0]);
let lane1_rewards_account =
PayLaneRewardFromAccount::lane_rewards_account([0, 0, 0, 1]);

Balances::mint_into(&lane0_rewards_account, 100).unwrap();
Balances::mint_into(&lane1_rewards_account, 100).unwrap();
assert_eq!(Balances::balance(&lane0_rewards_account), 100);
assert_eq!(Balances::balance(&lane1_rewards_account), 100);
assert_eq!(Balances::balance(&1), 0);
assert_eq!(Balances::total_issuance(), 0);
bp_relayers::MintReward::<Balances, AccountId>::pay_reward(&1, TEST_LANE_ID, 100)
.unwrap();

PayLaneRewardFromAccount::pay_reward(&1, [0, 0, 0, 0], 100).unwrap();
assert_eq!(Balances::balance(&lane0_rewards_account), 0);
assert_eq!(Balances::balance(&lane1_rewards_account), 100);
assert_eq!(Balances::balance(&1), 100);
assert_eq!(Balances::total_issuance(), 100);

PayLaneRewardFromAccount::pay_reward(&1, [0, 0, 0, 1], 100).unwrap();
assert_eq!(Balances::balance(&lane0_rewards_account), 0);
assert_eq!(Balances::balance(&lane1_rewards_account), 0);
assert_eq!(Balances::balance(&1), 200);
});
}
}
11 changes: 11 additions & 0 deletions primitives/messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use bp_runtime::{BasicOperatingMode, OperatingMode};
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::RuntimeDebug;
use scale_info::TypeInfo;
use sp_core::TypeId;
use sp_std::{collections::vec_deque::VecDeque, prelude::*};

pub mod source_chain;
Expand Down Expand Up @@ -68,6 +69,16 @@ impl OperatingMode for MessagesOperatingMode {
/// Lane identifier.
pub type LaneId = [u8; 4];

/// Lane id which implements `TypeId`.
// TODO (https://github.com/paritytech/parity-bridges-common/issues/1694):
// `LaneId` shall be replaced with this across all codebase (codec-compatible)
#[derive(Decode, Encode, RuntimeDebug)]
pub struct TypedLaneId(pub [u8; 4]);

impl TypeId for TypedLaneId {
const TYPE_ID: [u8; 4] = *b"blan";
}

/// Message nonce. Valid messages will never have 0 nonce.
pub type MessageNonce = u64;

Expand Down
1 change: 1 addition & 0 deletions primitives/relayers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }

[dev-dependencies]
bp-rialto = { path = "../chain-rialto" }
hex = "0.4"
hex-literal = "0.3"

Expand Down
54 changes: 47 additions & 7 deletions primitives/relayers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
#![warn(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]

use bp_messages::LaneId;
use bp_messages::{LaneId, TypedLaneId};
use sp_runtime::{
codec::{Decode, Encode},
traits::AccountIdConversion,
};
use sp_std::{fmt::Debug, marker::PhantomData};

/// Reward payment procedure.
Expand All @@ -31,20 +35,56 @@ pub trait PaymentProcedure<Relayer, Reward> {
fn pay_reward(relayer: &Relayer, lane_id: LaneId, reward: Reward) -> Result<(), Self::Error>;
}

/// Reward payment procedure that is simply minting given amount of tokens.
pub struct MintReward<T, Relayer>(PhantomData<(T, Relayer)>);
/// Reward payment procedure that does `balances::transfer` call from the account, derived from
/// given lane.
pub struct PayLaneRewardFromAccount<T, Relayer>(PhantomData<(T, Relayer)>);

impl<T, Relayer> PaymentProcedure<Relayer, T::Balance> for MintReward<T, Relayer>
impl<T, Relayer> PayLaneRewardFromAccount<T, Relayer>
where
T: frame_support::traits::fungible::Mutate<Relayer>,
Relayer: Decode + Encode,
{
/// Return account that pay rewards for serving given lane.
pub fn lane_rewards_account(lane_id: LaneId) -> Relayer {
TypedLaneId(lane_id).into_sub_account_truncating(b"bridge-lane")
}
}

impl<T, Relayer> PaymentProcedure<Relayer, T::Balance> for PayLaneRewardFromAccount<T, Relayer>
where
T: frame_support::traits::fungible::Transfer<Relayer>,
Relayer: Decode + Encode,
{
type Error = sp_runtime::DispatchError;

fn pay_reward(
relayer: &Relayer,
_lane_id: LaneId,
lane_id: LaneId,
reward: T::Balance,
) -> Result<(), Self::Error> {
T::mint_into(relayer, reward)
T::transfer(&Self::lane_rewards_account(lane_id), relayer, reward, false).map(drop)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn lanes_are_using_different_accounts() {
assert_eq!(
PayLaneRewardFromAccount::<(), bp_rialto::AccountId>::lane_rewards_account([
0, 0, 0, 0
]),
hex_literal::hex!("626c616e000000006272696467652d6c616e6500000000000000000000000000")
.into(),
);

assert_eq!(
PayLaneRewardFromAccount::<(), bp_rialto::AccountId>::lane_rewards_account([
0, 0, 0, 1
]),
hex_literal::hex!("626c616e000000016272696467652d6c616e6500000000000000000000000000")
.into(),
);
}
}