Skip to content

Commit 2f56528

Browse files
authored
Update outbound channel interval (#73)
1 parent 2afec11 commit 2f56528

File tree

7 files changed

+383
-393
lines changed

7 files changed

+383
-393
lines changed

pallets/substrate-channel/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ runtime-benchmarks = [
6262
"frame-benchmarking",
6363
"frame-support/runtime-benchmarks",
6464
"frame-system/runtime-benchmarks",
65+
"pallet-timestamp/runtime-benchmarks",
6566
"hex-literal",
6667
"rlp",
6768
]

pallets/substrate-channel/src/outbound/benchmarking.rs

+14-53
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use frame_benchmarking::benchmarks;
3737
use frame_support::traits::OnInitialize;
3838
use frame_system::EventRecord;
3939
use frame_system::RawOrigin;
40+
use sp_runtime::traits::One;
4041
use sp_std::prelude::*;
4142

4243
const BASE_NETWORK_ID: SubNetworkId = SubNetworkId::Mainnet;
@@ -56,57 +57,6 @@ benchmarks! {
5657
where crate::outbound::Event::<T>: Into<<T as frame_system::Config>::RuntimeEvent>
5758
}
5859

59-
// Benchmark `on_initialize` under worst case conditions, i.e. messages
60-
// in queue are committed.
61-
on_initialize {
62-
let m in 1 .. T::MaxMessagesPerCommit::get();
63-
let p in 0 .. T::MaxMessagePayloadSize::get();
64-
65-
for _ in 0 .. m {
66-
let payload: Vec<u8> = (0..).take(p as usize).collect();
67-
MessageQueues::<T>::try_append(
68-
BASE_NETWORK_ID, BridgeMessage {
69-
payload: payload.try_into().unwrap(),
70-
timepoint: Default::default(),
71-
}).unwrap();
72-
}
73-
74-
let block_number = 0u32.into();
75-
76-
}: { BridgeOutboundChannel::<T>::on_initialize(block_number) }
77-
verify {
78-
assert_eq!(<MessageQueues<T>>::get(BASE_NETWORK_ID).len(), 0);
79-
}
80-
81-
// Benchmark 'on_initialize` for the best case, i.e. nothing is done
82-
// because it's not a commitment interval.
83-
on_initialize_non_interval {
84-
MessageQueues::<T>::take(BASE_NETWORK_ID);
85-
let payload: Vec<u8> = (0..).take(10).collect();
86-
MessageQueues::<T>::try_append(
87-
BASE_NETWORK_ID, BridgeMessage {
88-
payload: payload.try_into().unwrap(),
89-
timepoint: Default::default(),
90-
}).unwrap();
91-
92-
let interval: T::BlockNumber = 10u32.into();
93-
Interval::<T>::put(interval);
94-
let block_number: T::BlockNumber = 12u32.into();
95-
96-
}: { BridgeOutboundChannel::<T>::on_initialize(block_number) }
97-
verify {
98-
assert_eq!(<MessageQueues<T>>::get(BASE_NETWORK_ID).len(), 1);
99-
}
100-
101-
// Benchmark 'on_initialize` for the case where it is a commitment interval
102-
// but there are no messages in the queue.
103-
on_initialize_no_messages {
104-
MessageQueues::<T>::take(BASE_NETWORK_ID);
105-
106-
let block_number = Interval::<T>::get();
107-
108-
}: { BridgeOutboundChannel::<T>::on_initialize(block_number) }
109-
11060
submit {
11161

11262
}: {
@@ -120,9 +70,20 @@ benchmarks! {
12070
}.into());
12171
}
12272

73+
update_interval {
74+
75+
}: {
76+
BridgeOutboundChannel::<T>::update_interval(RawOrigin::Root.into(), One::one()).unwrap()
77+
}
78+
verify {
79+
assert_last_event::<T>(crate::outbound::Event::<T>::IntervalUpdated {
80+
interval: One::one()
81+
}.into());
82+
}
83+
12384
impl_benchmark_test_suite!(
12485
BridgeOutboundChannel,
125-
crate::outbound::test::new_tester(),
126-
crate::outbound::test::Test,
86+
crate::outbound::mock::new_tester(),
87+
crate::outbound::mock::Test,
12788
);
12889
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
use super::*;
2+
use bridge_types::GenericNetworkId;
3+
use codec::{Decode, Encode, MaxEncodedLen};
4+
use currencies::BasicCurrencyAdapter;
5+
6+
use bridge_types::traits::TimepointProvider;
7+
use frame_support::traits::{Everything, GenesisBuild};
8+
use frame_support::{parameter_types, Deserialize, Serialize};
9+
use scale_info::TypeInfo;
10+
use sp_core::H256;
11+
use sp_keyring::AccountKeyring as Keyring;
12+
use sp_runtime::testing::Header;
13+
use sp_runtime::traits::{BlakeTwo256, IdentifyAccount, IdentityLookup, Verify};
14+
use sp_runtime::{AccountId32, MultiSignature};
15+
use sp_std::convert::From;
16+
use traits::parameter_type_with_key;
17+
18+
use crate::outbound as bridge_outbound_channel;
19+
20+
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
21+
type Block = frame_system::mocking::MockBlock<Test>;
22+
23+
pub const BASE_NETWORK_ID: SubNetworkId = SubNetworkId::Mainnet;
24+
25+
frame_support::construct_runtime!(
26+
pub enum Test where
27+
Block = Block,
28+
NodeBlock = Block,
29+
UncheckedExtrinsic = UncheckedExtrinsic,
30+
{
31+
System: frame_system::{Pallet, Call, Storage, Event<T>},
32+
Timestamp: pallet_timestamp::{Pallet, Call, Storage},
33+
Tokens: tokens::{Pallet, Call, Config<T>, Storage, Event<T>},
34+
Currencies: currencies::{Pallet, Call, Storage},
35+
Balances: pallet_balances::{Pallet, Call, Storage, Event<T>},
36+
BridgeOutboundChannel: bridge_outbound_channel::{Pallet, Config<T>, Storage, Event<T>},
37+
}
38+
);
39+
40+
pub type Signature = MultiSignature;
41+
pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;
42+
43+
#[derive(
44+
Encode,
45+
Decode,
46+
PartialEq,
47+
Eq,
48+
Debug,
49+
Clone,
50+
Copy,
51+
MaxEncodedLen,
52+
TypeInfo,
53+
PartialOrd,
54+
Ord,
55+
Serialize,
56+
Deserialize,
57+
)]
58+
pub enum AssetId {
59+
XOR,
60+
ETH,
61+
DAI,
62+
}
63+
64+
pub type Balance = u128;
65+
pub type Amount = i128;
66+
67+
parameter_types! {
68+
pub const BlockHashCount: u64 = 250;
69+
}
70+
71+
impl frame_system::Config for Test {
72+
type BaseCallFilter = Everything;
73+
type BlockWeights = ();
74+
type BlockLength = ();
75+
type RuntimeOrigin = RuntimeOrigin;
76+
type RuntimeCall = RuntimeCall;
77+
type Index = u64;
78+
type BlockNumber = u64;
79+
type Hash = H256;
80+
type Hashing = BlakeTwo256;
81+
type AccountId = AccountId;
82+
type Lookup = IdentityLookup<Self::AccountId>;
83+
type Header = Header;
84+
type RuntimeEvent = RuntimeEvent;
85+
type BlockHashCount = BlockHashCount;
86+
type DbWeight = ();
87+
type Version = ();
88+
type PalletInfo = PalletInfo;
89+
type AccountData = pallet_balances::AccountData<Balance>;
90+
type OnNewAccount = ();
91+
type OnKilledAccount = ();
92+
type SystemWeightInfo = ();
93+
type SS58Prefix = ();
94+
type OnSetCode = ();
95+
type MaxConsumers = frame_support::traits::ConstU32<65536>;
96+
}
97+
98+
parameter_types! {
99+
pub const ExistentialDeposit: u128 = 0;
100+
}
101+
102+
parameter_type_with_key! {
103+
pub ExistentialDeposits: |_currency_id: AssetId| -> Balance {
104+
0
105+
};
106+
}
107+
108+
impl pallet_balances::Config for Test {
109+
type Balance = Balance;
110+
type RuntimeEvent = RuntimeEvent;
111+
type DustRemoval = ();
112+
type ExistentialDeposit = ExistentialDeposit;
113+
type AccountStore = System;
114+
type WeightInfo = ();
115+
type MaxLocks = ();
116+
type MaxReserves = ();
117+
type ReserveIdentifier = ();
118+
}
119+
120+
impl tokens::Config for Test {
121+
type RuntimeEvent = RuntimeEvent;
122+
type Balance = Balance;
123+
type Amount = Amount;
124+
type CurrencyId = AssetId;
125+
type WeightInfo = ();
126+
type ExistentialDeposits = ExistentialDeposits;
127+
type CurrencyHooks = ();
128+
type MaxLocks = ();
129+
type MaxReserves = ();
130+
type ReserveIdentifier = ();
131+
type DustRemovalWhitelist = Everything;
132+
}
133+
134+
impl currencies::Config for Test {
135+
type MultiCurrency = Tokens;
136+
type NativeCurrency = BasicCurrencyAdapter<Test, Balances, Amount, u64>;
137+
type GetNativeCurrencyId = GetBaseAssetId;
138+
type WeightInfo = ();
139+
}
140+
parameter_types! {
141+
pub const GetBaseAssetId: AssetId = AssetId::XOR;
142+
pub GetTeamReservesAccountId: AccountId = AccountId32::from([0; 32]);
143+
pub GetFeeAccountId: AccountId = AccountId32::from([1; 32]);
144+
pub GetTreasuryAccountId: AccountId = AccountId32::from([2; 32]);
145+
}
146+
147+
parameter_types! {
148+
pub const MaxMessagePayloadSize: u32 = 128;
149+
pub const MaxMessagesPerCommit: u32 = 5;
150+
pub const ThisNetworkId: GenericNetworkId = GenericNetworkId::Sub(SubNetworkId::Mainnet);
151+
}
152+
153+
pub struct GenericTimepointProvider;
154+
155+
impl TimepointProvider for GenericTimepointProvider {
156+
fn get_timepoint() -> bridge_types::GenericTimepoint {
157+
bridge_types::GenericTimepoint::Sora(System::block_number() as u32)
158+
}
159+
}
160+
161+
impl bridge_outbound_channel::Config for Test {
162+
type RuntimeEvent = RuntimeEvent;
163+
type MaxMessagePayloadSize = MaxMessagePayloadSize;
164+
type MaxMessagesPerCommit = MaxMessagesPerCommit;
165+
type MessageStatusNotifier = ();
166+
type AuxiliaryDigestHandler = ();
167+
type AssetId = ();
168+
type Balance = u128;
169+
type WeightInfo = ();
170+
type TimepointProvider = GenericTimepointProvider;
171+
type ThisNetworkId = ThisNetworkId;
172+
}
173+
174+
impl pallet_timestamp::Config for Test {
175+
type Moment = u64;
176+
type OnTimestampSet = ();
177+
type MinimumPeriod = ();
178+
type WeightInfo = ();
179+
}
180+
181+
pub fn new_tester() -> sp_io::TestExternalities {
182+
let mut storage = frame_system::GenesisConfig::default()
183+
.build_storage::<Test>()
184+
.unwrap();
185+
186+
let config: bridge_outbound_channel::GenesisConfig<Test> =
187+
bridge_outbound_channel::GenesisConfig {
188+
interval: 10u32.into(),
189+
};
190+
config.assimilate_storage(&mut storage).unwrap();
191+
192+
let bob: AccountId = Keyring::Bob.into();
193+
194+
pallet_balances::GenesisConfig::<Test> {
195+
balances: vec![(bob, 1u32.into())],
196+
}
197+
.assimilate_storage(&mut storage)
198+
.unwrap();
199+
200+
let mut ext: sp_io::TestExternalities = storage.into();
201+
202+
ext.execute_with(|| System::set_block_number(1));
203+
ext
204+
}

0 commit comments

Comments
 (0)