Skip to content

Commit

Permalink
Token swap pallet benchmarks (paritytech#1174)
Browse files Browse the repository at this point in the history
* token swap benchmarks

* spellcheck
  • Loading branch information
svyatonik authored and serban300 committed Apr 9, 2024
1 parent cf22010 commit d969412
Show file tree
Hide file tree
Showing 14 changed files with 500 additions and 49 deletions.
5 changes: 1 addition & 4 deletions bridges/bin/millau/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ frame-benchmarking-cli = { git = "https://github.com/paritytech/substrate", bran

[features]
default = []

# TODO: https://github.com/paritytech/parity-bridges-common/issues/390
# I've left the feature flag here to test our CI configuration
runtime-benchmarks = [
# "millau-runtime/runtime-benchmarks",
"millau-runtime/runtime-benchmarks",
]
11 changes: 8 additions & 3 deletions bridges/bin/millau/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ repository = "https://github.com/paritytech/parity-bridges-common/"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"

[dependencies]
hex-literal = "0.3"
codec = { package = "parity-scale-codec", version = "2.2.0", default-features = false, features = ["derive"] }
serde = { version = "1.0", optional = true, features = ["derive"] }

Expand All @@ -28,6 +29,7 @@ pallet-shift-session-manager = { path = "../../../modules/shift-session-manager"

# Substrate Dependencies

frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false, optional = true }
frame-executive = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
Expand Down Expand Up @@ -102,6 +104,9 @@ std = [
"sp-trie/std",
"sp-version/std",
]
# TODO: https://github.com/paritytech/parity-bridges-common/issues/390
# I've left the feature flag here to test our CI configuration
runtime-benchmarks = []
runtime-benchmarks = [
"frame-benchmarking",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-bridge-token-swap/runtime-benchmarks",
]
65 changes: 65 additions & 0 deletions bridges/bin/millau/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,10 +425,14 @@ pub type WithRialtoTokenSwapInstance = ();

impl pallet_bridge_token_swap::Config<WithRialtoTokenSwapInstance> for Runtime {
type Event = Event;
type WeightInfo = ();

type BridgedChainId = RialtoChainId;
type OutboundMessageLaneId = TokenSwapMessagesLane;
#[cfg(not(feature = "runtime-benchmarks"))]
type MessagesBridge = pallet_bridge_messages::Pallet<Runtime, WithRialtoMessagesInstance>;
#[cfg(feature = "runtime-benchmarks")]
type MessagesBridge = bp_messages::source_chain::NoopMessagesBridge;
type ThisCurrency = pallet_balances::Pallet<Runtime>;
type FromSwapToThisAccountIdConverter = bp_rialto::AccountIdConverter;

Expand Down Expand Up @@ -704,6 +708,67 @@ impl_runtime_apis! {
BridgeRialtoMessages::inbound_unrewarded_relayers_state(lane)
}
}

#[cfg(feature = "runtime-benchmarks")]
impl frame_benchmarking::Benchmark<Block> for Runtime {
fn benchmark_metadata(extra: bool) -> (
Vec<frame_benchmarking::BenchmarkList>,
Vec<frame_support::traits::StorageInfo>,
) {
use frame_benchmarking::{list_benchmark, Benchmarking, BenchmarkList};
use frame_support::traits::StorageInfoTrait;

let mut list = Vec::<BenchmarkList>::new();

list_benchmark!(list, extra, pallet_bridge_token_swap, BridgeRialtoTokenSwap);

let storage_info = AllPalletsWithSystem::storage_info();

return (list, storage_info)
}

fn dispatch_benchmark(
config: frame_benchmarking::BenchmarkConfig,
) -> Result<Vec<frame_benchmarking::BenchmarkBatch>, sp_runtime::RuntimeString> {
use frame_benchmarking::{Benchmarking, BenchmarkBatch, TrackedStorageKey, add_benchmark};

let whitelist: Vec<TrackedStorageKey> = vec![
// Block Number
hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef702a5c1b19ab7a04f536c519aca4983ac").to_vec().into(),
// Execution Phase
hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef7ff553b5a9862a516939d82b3d3d8661a").to_vec().into(),
// Event Count
hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef70a98fdbe9ce6c55837576c60c7af3850").to_vec().into(),
// System Events
hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef780d41e5e16056765bc8461851072c9d7").to_vec().into(),
// Caller 0 Account
hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da946c154ffd9992e395af90b5b13cc6f295c77033fce8a9045824a6690bbf99c6db269502f0a8d1d2a008542d5690a0749").to_vec().into(),
];

let mut batches = Vec::<BenchmarkBatch>::new();
let params = (&config, &whitelist);

use pallet_bridge_token_swap::benchmarking::Config as TokenSwapConfig;

impl TokenSwapConfig<WithRialtoTokenSwapInstance> for Runtime {
fn initialize_environment() {
let relayers_fund_account = pallet_bridge_messages::relayer_fund_account_id::<
bp_millau::AccountId,
bp_millau::AccountIdConverter,
>();
pallet_balances::Pallet::<Runtime>::make_free_balance_be(
&relayers_fund_account,
Balance::MAX / 100,
);
}
}

add_benchmark!(params, batches, pallet_bridge_token_swap, BridgeRialtoTokenSwap);

if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) }
Ok(batches)
}
}
}

/// Rialto account ownership digest from Millau.
Expand Down
6 changes: 3 additions & 3 deletions bridges/modules/grandpa/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.

//! Autogenerated weights for pallet_bridge_grandpa
//! Autogenerated weights for `pallet_bridge_grandpa`
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 3.0.0
//! DATE: 2021-06-03, STEPS: [50, ], REPEAT: 20
Expand Down Expand Up @@ -46,12 +46,12 @@ use frame_support::{
};
use sp_std::marker::PhantomData;

/// Weight functions needed for pallet_bridge_grandpa.
/// Weight functions needed for `pallet_bridge_grandpa`.
pub trait WeightInfo {
fn submit_finality_proof(p: u32, v: u32) -> Weight;
}

/// Weights for pallet_bridge_grandpa using the Rialto node and recommended hardware.
/// Weights for `pallet_bridge_grandpa` using the Rialto node and recommended hardware.
pub struct RialtoWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for RialtoWeight<T> {
fn submit_finality_proof(p: u32, v: u32) -> Weight {
Expand Down
31 changes: 11 additions & 20 deletions bridges/modules/messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use crate::{
use bp_messages::{
source_chain::{
LaneMessageVerifier, MessageDeliveryAndDispatchPayment, OnDeliveryConfirmed,
OnMessageAccepted, TargetHeaderChain,
OnMessageAccepted, SendMessageArtifacts, TargetHeaderChain,
},
target_chain::{
DispatchMessage, MessageDispatch, ProvedLaneMessages, ProvedMessages, SourceHeaderChain,
Expand Down Expand Up @@ -281,7 +281,10 @@ pub mod pallet {
payload,
delivery_and_dispatch_fee,
)
.map(|sent_message| sent_message.post_dispatch_info)
.map(|sent_message| PostDispatchInfo {
actual_weight: Some(sent_message.weight),
pays_fee: Pays::Yes,
})
}

/// Pay additional fee for the message.
Expand Down Expand Up @@ -853,27 +856,21 @@ where
lane: LaneId,
message: T::OutboundPayload,
delivery_and_dispatch_fee: T::OutboundMessageFee,
) -> Result<MessageNonce, Self::Error> {
) -> Result<SendMessageArtifacts, Self::Error> {
crate::send_message::<T, I>(sender, lane, message, delivery_and_dispatch_fee)
.map(|sent_message| sent_message.nonce)
}
}

/// Message that has been sent.
struct SentMessage {
/// Nonce of the message.
pub nonce: MessageNonce,
/// Post-dispatch call info.
pub post_dispatch_info: PostDispatchInfo,
}

/// Function that actually sends message.
fn send_message<T: Config<I>, I: 'static>(
submitter: bp_messages::source_chain::Sender<T::AccountId>,
lane_id: LaneId,
payload: T::OutboundPayload,
delivery_and_dispatch_fee: T::OutboundMessageFee,
) -> sp_std::result::Result<SentMessage, sp_runtime::DispatchErrorWithPostInfo<PostDispatchInfo>> {
) -> sp_std::result::Result<
SendMessageArtifacts,
sp_runtime::DispatchErrorWithPostInfo<PostDispatchInfo>,
> {
ensure_normal_operating_mode::<T, I>()?;

// initially, actual (post-dispatch) weight is equal to pre-dispatch weight
Expand Down Expand Up @@ -986,13 +983,7 @@ fn send_message<T: Config<I>, I: 'static>(

Pallet::<T, I>::deposit_event(Event::MessageAccepted(lane_id, nonce));

Ok(SentMessage {
nonce,
post_dispatch_info: PostDispatchInfo {
actual_weight: Some(actual_weight),
pays_fee: Pays::Yes,
},
})
Ok(SendMessageArtifacts { nonce, weight: actual_weight })
}

/// Ensure that the origin is either root, or `PalletOwner`.
Expand Down
6 changes: 3 additions & 3 deletions bridges/modules/messages/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.

//! Autogenerated weights for pallet_bridge_messages
//! Autogenerated weights for `pallet_bridge_messages`
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 3.0.0
//! DATE: 2021-06-18, STEPS: [50, ], REPEAT: 20
Expand Down Expand Up @@ -46,7 +46,7 @@ use frame_support::{
};
use sp_std::marker::PhantomData;

/// Weight functions needed for pallet_bridge_messages.
/// Weight functions needed for `pallet_bridge_messages`.
pub trait WeightInfo {
fn send_minimal_message_worst_case() -> Weight;
fn send_1_kb_message_worst_case() -> Weight;
Expand All @@ -71,7 +71,7 @@ pub trait WeightInfo {
fn receive_delivery_proof_for_multiple_messages_by_multiple_relayers(i: u32) -> Weight;
}

/// Weights for pallet_bridge_messages using the Rialto node and recommended hardware.
/// Weights for `pallet_bridge_messages` using the Rialto node and recommended hardware.
pub struct RialtoWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for RialtoWeight<T> {
fn send_minimal_message_worst_case() -> Weight {
Expand Down
6 changes: 6 additions & 0 deletions bridges/modules/token-swap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ bp-messages = { path = "../../primitives/messages", default-features = false }
bp-runtime = { path = "../../primitives/runtime", default-features = false }
bp-token-swap = { path = "../../primitives/token-swap", default-features = false }
pallet-bridge-dispatch = { path = "../dispatch", default-features = false }
pallet-bridge-messages = { path = "../messages", default-features = false }

# Substrate Dependencies

frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false, optional = true }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
Expand All @@ -43,9 +45,13 @@ std = [
"frame-system/std",
"log/std",
"pallet-bridge-dispatch/std",
"pallet-bridge-messages/std",
"serde",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
]
runtime-benchmarks = [
"frame-benchmarking",
]
Loading

0 comments on commit d969412

Please sign in to comment.