Skip to content

Commit

Permalink
Merge branch 'feature/argo-bridge' into fix/petra-ci-checks
Browse files Browse the repository at this point in the history
  • Loading branch information
ignazio-bovo committed May 31, 2024
2 parents bfe1832 + 8f3c830 commit a8890f2
Show file tree
Hide file tree
Showing 11 changed files with 384 additions and 76 deletions.
6 changes: 3 additions & 3 deletions bin/node/src/chain_spec/argo_bridge_config.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use node_runtime::{argo_bridge::types::BridgeStatus, ArgoBridgeConfig};
use node_runtime::{argo_bridge::types::BridgeStatus, ArgoBridgeConfig, DefaultBridgingFee};

pub fn production_config() -> ArgoBridgeConfig {
ArgoBridgeConfig {
status: BridgeStatus::Paused,
mint_allowance: 0,
bridging_fee: 0,
bridging_fee: DefaultBridgingFee::get(),
thawn_duration: 1,
..Default::default()
}
Expand All @@ -14,7 +14,7 @@ pub fn testing_config() -> ArgoBridgeConfig {
ArgoBridgeConfig {
status: BridgeStatus::Paused,
mint_allowance: 0,
bridging_fee: 0,
bridging_fee: DefaultBridgingFee::get(),
thawn_duration: 1,
..Default::default()
}
Expand Down
225 changes: 214 additions & 11 deletions runtime-modules/argo-bridge/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use frame_support::{assert_err, assert_ok};

use super::*;
use crate::types::*;
use crate::vec::Vec;
use crate::Module as ArgoBridge;
use balances::Pallet as Balances;
use core::convert::TryFrom;
use frame_benchmarking::v1::{account, benchmarks};
use frame_system::Pallet as System;
use frame_system::{EventRecord, RawOrigin};
use sp_runtime::traits::One;
use sp_runtime::traits::{One, StaticLookup};
use sp_std::convert::TryInto;
use sp_std::vec;

use crate::{BridgeConstraints, BridgeStatus, RemoteAccount, RemoteTransfer};

Expand Down Expand Up @@ -65,36 +65,239 @@ benchmarks! {
T::AccountId: CreateAccountId
}

// Worst case scenario:
// - max number of remote chains being use
// - using the last chain
request_outbound_transfer{
let fee: BalanceOf<T> = 10u32.into();
let remote_chains = BoundedVec::try_from(vec![1u32]).unwrap();
let remote_chains: Vec<u32> = (0..MAX_REMOTE_CHAINS).collect();
let parameters = BridgeConstraints {
operator_account: Some(T::AccountId::create_account_id(1u32.into())),
pauser_accounts: Some(vec![T::AccountId::create_account_id(2u32.into())]),
pauser_accounts: None,
bridging_fee: Some(fee),
thawn_duration: None::<T::BlockNumber>,
remote_chains: Some(remote_chains)
remote_chains: Some(BoundedVec::try_from(remote_chains).unwrap())
};

assert_ok!(ArgoBridge::<T>::update_bridge_constrains(
RawOrigin::Root.into(),
parameters
));

let initial_balance: u32 = 1020u32.into();
let sender = T::AccountId::create_account_id(1u32.into());
let lookup_sender = T::Lookup::unlookup(sender.clone());
Balances::<T>::set_balance(RawOrigin::Root.into(), lookup_sender,
initial_balance.into(), 0u32.into()).unwrap();

let dest_account = RemoteAccount {
account: [0; 32],
chain_id: 1,
chain_id: MAX_REMOTE_CHAINS - 1,
};
let sender = T::AccountId::create_account_id(1u32.into());
let origin = RawOrigin::Signed(sender);
let transfer_id = ArgoBridge::<T>::next_transfer_id();
let amount = 100u32.into();
}: _(origin, dest_account, amount, fee)
let transfer_amount = 1000u32.into();
}: _(origin, dest_account, transfer_amount, fee)
verify {
let sender = T::AccountId::create_account_id(1u32.into());
assert_last_event::<T>(
RawEvent::OutboundTransferRequested(transfer_id, sender, dest_account, amount, fee).into());
RawEvent::OutboundTransferRequested(transfer_id, sender, dest_account, transfer_amount, fee).into());
}


// Worst case scenario:
// - max number of remote chains being use
// - using the last chain
finalize_inbound_transfer{
let fee: BalanceOf<T> = 10u32.into();
let remote_chains: Vec<u32> = (0..MAX_REMOTE_CHAINS).collect();
let parameters = BridgeConstraints {
operator_account: Some(T::AccountId::create_account_id(1u32.into())),
pauser_accounts: None,
bridging_fee: Some(fee),
thawn_duration: None::<T::BlockNumber>,
remote_chains: Some(BoundedVec::try_from(remote_chains).unwrap())
};

assert_ok!(ArgoBridge::<T>::update_bridge_constrains(
RawOrigin::Root.into(),
parameters
));

let sender = T::AccountId::create_account_id(1u32.into());
let dest_account = T::AccountId::create_account_id(1u32.into());

let transfer_amount = 100u32.into();
let remote_transfer = RemoteTransfer { id: 0, chain_id: MAX_REMOTE_CHAINS - 1 };
}: _(RawOrigin::Signed(sender), remote_transfer.clone(), dest_account.clone(), transfer_amount)
verify {
assert_last_event::<T>(
RawEvent::InboundTransferFinalized(remote_transfer, dest_account, transfer_amount).into());
}

// Worst case scenario:
// - max number of pauser accounts being use
// - using the last pauser account
pause_bridge{
let mut pauser_accounts :Vec<T::AccountId> = Vec::new();
for i in 0u32..T::MaxPauserAccounts::get() {
pauser_accounts.push(T::AccountId::create_account_id(i));
}
let parameters = BridgeConstraints {
operator_account: None,
pauser_accounts: Some(pauser_accounts),
bridging_fee: None,
thawn_duration: None::<T::BlockNumber>,
remote_chains: None
};

assert_ok!(ArgoBridge::<T>::update_bridge_constrains(
RawOrigin::Root.into(),
parameters
));

let pauser_acount = T::AccountId::create_account_id((T::MaxPauserAccounts::get() - 1).into());
}: _(RawOrigin::Signed(pauser_acount.clone()))
verify {
assert_eq!(ArgoBridge::<T>::status(), BridgeStatus::Paused);
assert_last_event::<T>(
RawEvent::BridgePaused(pauser_acount.clone()).into());
}

// Worst case scenario:
// - max number of pauser accounts being use
// - using the last pauser account
init_unpause_bridge{
let pauser_accounts: Vec<T::AccountId> = (0..T::MaxPauserAccounts::get())
.map(|i| T::AccountId::create_account_id(i))
.collect();
let parameters = BridgeConstraints {
operator_account: None,
pauser_accounts: Some(pauser_accounts),
bridging_fee: None,
thawn_duration: Some(1u32.into()),
remote_chains: None
};

assert_ok!(ArgoBridge::<T>::update_bridge_constrains(
RawOrigin::Root.into(),
parameters
));

let pauser_acount = T::AccountId::create_account_id((T::MaxPauserAccounts::get() - 1) .into());
ArgoBridge::<T>::pause_bridge(RawOrigin::Signed(pauser_acount.clone()).into()).unwrap();
}: _(RawOrigin::Signed(pauser_acount.clone()))
verify {
let current_block = System::<T>::block_number();
assert_last_event::<T>(
RawEvent::BridgeThawnStarted(pauser_acount, current_block + 1u32.into()).into());
}

// Worst case scenario:
// - max number of pauser accounts being use
// - using the last pauser account
finish_unpause_bridge{
let pauser_accounts: Vec<T::AccountId> = (0..T::MaxPauserAccounts::get())
.map(|i| T::AccountId::create_account_id(i))
.collect();
let operator_account = T::AccountId::create_account_id(1u32.into());
let parameters = BridgeConstraints {
operator_account: Some(operator_account.clone().into()),
pauser_accounts: Some(pauser_accounts),
bridging_fee: None,
thawn_duration: Some(1u32.into()),
remote_chains: None
};

assert_ok!(ArgoBridge::<T>::update_bridge_constrains(
RawOrigin::Root.into(),
parameters
));

let pauser_acount = T::AccountId::create_account_id((T::MaxPauserAccounts::get() - 1) .into());
let origin = RawOrigin::Signed(pauser_acount.clone());
ArgoBridge::<T>::pause_bridge(origin.clone().into()).unwrap();
ArgoBridge::<T>::init_unpause_bridge(origin.into()).unwrap();
System::<T>::set_block_number(3u32.into());
}: _(RawOrigin::Signed(operator_account))
verify {
let current_block = System::<T>::block_number();
assert_last_event::<T>(
RawEvent::BridgeThawnFinished().into());
}

// Worst case scenario:
// - update all parameters
update_bridge_constrains{
let fee: BalanceOf<T> = 10u32.into();
let pauser_accounts: Vec<T::AccountId> = (0..T::MaxPauserAccounts::get())
.map(|i| T::AccountId::create_account_id(i))
.collect();
let remote_chains: Vec<u32> = (0..MAX_REMOTE_CHAINS).collect();

let parameters = BridgeConstraints {
operator_account: Some(T::AccountId::create_account_id(1u32.into())),
pauser_accounts: Some(pauser_accounts),
bridging_fee: Some(fee),
thawn_duration: Some(1u32.into()),
remote_chains: Some(BoundedVec::try_from(remote_chains).unwrap())
};

}: _(RawOrigin::Root, parameters.clone())
verify {
assert_last_event::<T>(
RawEvent::BridgeConfigUpdated(parameters).into());
}
}

#[cfg(test)]
mod tests {}
mod tests {
use crate::tests::mock::{
with_test_externalities, with_test_externalities_custom_mint_allowance, Test,
};
use frame_support::assert_ok;

type ArgoBridge = crate::Module<Test>;

#[test]
fn test_request_outbound_transfer() {
with_test_externalities(|| {
assert_ok!(ArgoBridge::test_benchmark_request_outbound_transfer());
});
}

#[test]
fn test_finalize_inbound_transfer() {
with_test_externalities_custom_mint_allowance(1000u32.into(), || {
assert_ok!(ArgoBridge::test_benchmark_finalize_inbound_transfer());
});
}

#[test]
fn test_pause_bridge() {
with_test_externalities(|| {
assert_ok!(ArgoBridge::test_benchmark_pause_bridge());
});
}

#[test]
fn test_init_unpause_bridge() {
with_test_externalities(|| {
assert_ok!(ArgoBridge::test_benchmark_init_unpause_bridge());
});
}

#[test]
fn test_finish_unpause_bridge() {
with_test_externalities(|| {
assert_ok!(ArgoBridge::test_benchmark_finish_unpause_bridge());
});
}

#[test]
fn test_init_update_bridge_constrains() {
with_test_externalities(|| {
assert_ok!(ArgoBridge::test_benchmark_update_bridge_constrains());
});
}
}
7 changes: 5 additions & 2 deletions runtime-modules/argo-bridge/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@ decl_error! {
FeeDifferentThanExpected,

/// Not enough mint allowance for transaction
InsufficienBridgMintAllowance,
InsufficientBridgeMintAllowance,

/// Operator account required
NotOperatorAccount,

/// Operator was not yet set
OperatorAccountNotSet,

/// Pauser account required
NotPauserAccount,

/// Number of pauser accounts over the maximum allowed
InvalidNumberOfPauserAccounts,

/// Current block is lower than thawn_started_at + thawn_duration
/// Current block is lower than thawn_ends_at
ThawnNotFinished,

/// ChainId is not on the list of the supported chains
Expand Down
3 changes: 2 additions & 1 deletion runtime-modules/argo-bridge/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ decl_event!(
AccountId = <T as frame_system::Config>::AccountId,
Balance = BalanceOf<T>,
BridgeConstraints = BridgeConstraintsOf<T>,
BlockNumber = <T as frame_system::Config>::BlockNumber,
{
OutboundTransferRequested(TransferId, AccountId, RemoteAccount, Balance, Balance),
InboundTransferFinalized(RemoteTransfer, AccountId, Balance),
BridgePaused(AccountId),
BridgeThawnStarted(AccountId),
BridgeThawnStarted(AccountId, BlockNumber),
BridgeThawnFinished(),
BridgeConfigUpdated(BridgeConstraints),
}
Expand Down
Loading

0 comments on commit a8890f2

Please sign in to comment.