Skip to content

Commit 7414842

Browse files
svyatonikbkchr
authored andcommitted
override conversion rate in estimate-message-fee RPC (#1189)
1 parent 22b1e45 commit 7414842

File tree

16 files changed

+85
-71
lines changed

16 files changed

+85
-71
lines changed

bridges/bin/millau/runtime/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ use sp_runtime::{
5252
create_runtime_str, generic, impl_opaque_keys,
5353
traits::{Block as BlockT, IdentityLookup, Keccak256, NumberFor, OpaqueKeys},
5454
transaction_validity::{TransactionSource, TransactionValidity},
55-
ApplyExtrinsicResult, FixedPointNumber, MultiSignature, MultiSigner, Perquintill,
55+
ApplyExtrinsicResult, FixedPointNumber, FixedU128, MultiSignature, MultiSigner, Perquintill,
5656
};
5757
use sp_std::prelude::*;
5858
#[cfg(feature = "std")]
@@ -744,10 +744,12 @@ impl_runtime_apis! {
744744
fn estimate_message_delivery_and_dispatch_fee(
745745
_lane_id: bp_messages::LaneId,
746746
payload: ToRialtoMessagePayload,
747+
rialto_to_this_conversion_rate: Option<FixedU128>,
747748
) -> Option<Balance> {
748749
estimate_message_dispatch_and_delivery_fee::<WithRialtoMessageBridge>(
749750
&payload,
750751
WithRialtoMessageBridge::RELAYER_FEE_PERCENT,
752+
rialto_to_this_conversion_rate,
751753
).ok()
752754
}
753755

bridges/bin/millau/runtime/src/rialto_messages.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,14 @@ impl MessageBridge for WithRialtoMessageBridge {
9191
type ThisChain = Millau;
9292
type BridgedChain = Rialto;
9393

94-
fn bridged_balance_to_this_balance(bridged_balance: bp_rialto::Balance) -> bp_millau::Balance {
95-
bp_millau::Balance::try_from(
96-
RialtoToMillauConversionRate::get().saturating_mul_int(bridged_balance),
97-
)
98-
.unwrap_or(bp_millau::Balance::MAX)
94+
fn bridged_balance_to_this_balance(
95+
bridged_balance: bp_rialto::Balance,
96+
bridged_to_this_conversion_rate_override: Option<FixedU128>,
97+
) -> bp_millau::Balance {
98+
let conversion_rate = bridged_to_this_conversion_rate_override
99+
.unwrap_or_else(|| RialtoToMillauConversionRate::get());
100+
bp_millau::Balance::try_from(conversion_rate.saturating_mul_int(bridged_balance))
101+
.unwrap_or(bp_millau::Balance::MAX)
99102
}
100103
}
101104

bridges/bin/rialto/runtime/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use sp_runtime::{
5353
create_runtime_str, generic, impl_opaque_keys,
5454
traits::{AccountIdLookup, Block as BlockT, Keccak256, NumberFor, OpaqueKeys},
5555
transaction_validity::{TransactionSource, TransactionValidity},
56-
ApplyExtrinsicResult, FixedPointNumber, MultiSignature, MultiSigner, Perquintill,
56+
ApplyExtrinsicResult, FixedPointNumber, FixedU128, MultiSignature, MultiSigner, Perquintill,
5757
};
5858
use sp_std::{collections::btree_map::BTreeMap, prelude::*};
5959
#[cfg(feature = "std")]
@@ -892,10 +892,12 @@ impl_runtime_apis! {
892892
fn estimate_message_delivery_and_dispatch_fee(
893893
_lane_id: bp_messages::LaneId,
894894
payload: ToMillauMessagePayload,
895+
millau_to_this_conversion_rate: Option<FixedU128>,
895896
) -> Option<Balance> {
896897
estimate_message_dispatch_and_delivery_fee::<WithMillauMessageBridge>(
897898
&payload,
898899
WithMillauMessageBridge::RELAYER_FEE_PERCENT,
900+
millau_to_this_conversion_rate,
899901
).ok()
900902
}
901903

bridges/bin/rialto/runtime/src/millau_messages.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,14 @@ impl MessageBridge for WithMillauMessageBridge {
9191
type ThisChain = Rialto;
9292
type BridgedChain = Millau;
9393

94-
fn bridged_balance_to_this_balance(bridged_balance: bp_millau::Balance) -> bp_rialto::Balance {
95-
bp_rialto::Balance::try_from(
96-
MillauToRialtoConversionRate::get().saturating_mul_int(bridged_balance),
97-
)
98-
.unwrap_or(bp_rialto::Balance::MAX)
94+
fn bridged_balance_to_this_balance(
95+
bridged_balance: bp_millau::Balance,
96+
bridged_to_this_conversion_rate_override: Option<FixedU128>,
97+
) -> bp_rialto::Balance {
98+
let conversion_rate = bridged_to_this_conversion_rate_override
99+
.unwrap_or_else(|| MillauToRialtoConversionRate::get());
100+
bp_rialto::Balance::try_from(conversion_rate.saturating_mul_int(bridged_balance))
101+
.unwrap_or(bp_rialto::Balance::MAX)
99102
}
100103
}
101104

bridges/bin/runtime-common/src/messages.rs

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ pub trait MessageBridge {
7070
/// Convert Bridged chain balance into This chain balance.
7171
fn bridged_balance_to_this_balance(
7272
bridged_balance: BalanceOf<BridgedChain<Self>>,
73+
bridged_to_this_conversion_rate_override: Option<FixedU128>,
7374
) -> BalanceOf<ThisChain<Self>>;
7475
}
7576

@@ -316,8 +317,11 @@ pub mod source {
316317
pallet_bridge_dispatch::verify_message_origin(submitter, payload)
317318
.map_err(|_| BAD_ORIGIN)?;
318319

319-
let minimal_fee_in_this_tokens =
320-
estimate_message_dispatch_and_delivery_fee::<B>(payload, B::RELAYER_FEE_PERCENT)?;
320+
let minimal_fee_in_this_tokens = estimate_message_dispatch_and_delivery_fee::<B>(
321+
payload,
322+
B::RELAYER_FEE_PERCENT,
323+
None,
324+
)?;
321325

322326
// compare with actual fee paid
323327
if *delivery_and_dispatch_fee < minimal_fee_in_this_tokens {
@@ -371,6 +375,7 @@ pub mod source {
371375
pub fn estimate_message_dispatch_and_delivery_fee<B: MessageBridge>(
372376
payload: &FromThisChainMessagePayload<B>,
373377
relayer_fee_percent: u32,
378+
bridged_to_this_conversion_rate: Option<FixedU128>,
374379
) -> Result<BalanceOf<ThisChain<B>>, &'static str> {
375380
// the fee (in Bridged tokens) of all transactions that are made on the Bridged chain
376381
//
@@ -391,8 +396,11 @@ pub mod source {
391396
ThisChain::<B>::transaction_payment(confirmation_transaction);
392397

393398
// minimal fee (in This tokens) is a sum of all required fees
394-
let minimal_fee = B::bridged_balance_to_this_balance(delivery_transaction_fee)
395-
.checked_add(&confirmation_transaction_fee);
399+
let minimal_fee = B::bridged_balance_to_this_balance(
400+
delivery_transaction_fee,
401+
bridged_to_this_conversion_rate,
402+
)
403+
.checked_add(&confirmation_transaction_fee);
396404

397405
// before returning, add extra fee that is paid to the relayer (relayer interest)
398406
minimal_fee
@@ -798,8 +806,12 @@ mod tests {
798806

799807
fn bridged_balance_to_this_balance(
800808
bridged_balance: BridgedChainBalance,
809+
bridged_to_this_conversion_rate_override: Option<FixedU128>,
801810
) -> ThisChainBalance {
802-
ThisChainBalance(bridged_balance.0 * BRIDGED_CHAIN_TO_THIS_CHAIN_BALANCE_RATE as u32)
811+
let conversion_rate = bridged_to_this_conversion_rate_override
812+
.map(|r| r.to_float() as u32)
813+
.unwrap_or(BRIDGED_CHAIN_TO_THIS_CHAIN_BALANCE_RATE);
814+
ThisChainBalance(bridged_balance.0 * conversion_rate)
803815
}
804816
}
805817

@@ -817,7 +829,10 @@ mod tests {
817829
type ThisChain = BridgedChain;
818830
type BridgedChain = ThisChain;
819831

820-
fn bridged_balance_to_this_balance(_this_balance: ThisChainBalance) -> BridgedChainBalance {
832+
fn bridged_balance_to_this_balance(
833+
_this_balance: ThisChainBalance,
834+
_bridged_to_this_conversion_rate_override: Option<FixedU128>,
835+
) -> BridgedChainBalance {
821836
unreachable!()
822837
}
823838
}
@@ -1095,6 +1110,7 @@ mod tests {
10951110
source::estimate_message_dispatch_and_delivery_fee::<OnThisChainBridge>(
10961111
&payload,
10971112
OnThisChainBridge::RELAYER_FEE_PERCENT,
1113+
None,
10981114
),
10991115
Ok(ThisChainBalance(EXPECTED_MINIMAL_FEE)),
11001116
);
@@ -1106,6 +1122,7 @@ mod tests {
11061122
source::estimate_message_dispatch_and_delivery_fee::<OnThisChainBridge>(
11071123
&payload_with_pay_on_target,
11081124
OnThisChainBridge::RELAYER_FEE_PERCENT,
1125+
None,
11091126
)
11101127
.expect(
11111128
"estimate_message_dispatch_and_delivery_fee failed for pay-at-target-chain message",
@@ -1572,4 +1589,21 @@ mod tests {
15721589
100 + 50 * 10 + 777,
15731590
);
15741591
}
1592+
1593+
#[test]
1594+
fn conversion_rate_override_works() {
1595+
let payload = regular_outbound_message_payload();
1596+
let regular_fee = source::estimate_message_dispatch_and_delivery_fee::<OnThisChainBridge>(
1597+
&payload,
1598+
OnThisChainBridge::RELAYER_FEE_PERCENT,
1599+
None,
1600+
);
1601+
let overrided_fee = source::estimate_message_dispatch_and_delivery_fee::<OnThisChainBridge>(
1602+
&payload,
1603+
OnThisChainBridge::RELAYER_FEE_PERCENT,
1604+
Some(FixedU128::from_float((BRIDGED_CHAIN_TO_THIS_CHAIN_BALANCE_RATE * 2) as f64)),
1605+
);
1606+
1607+
assert!(regular_fee < overrided_fee);
1608+
}
15751609
}

bridges/primitives/chain-kusama/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ bp-runtime = { path = "../runtime", default-features = false }
1919

2020
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
2121
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
22+
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
2223
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
2324
sp-version = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
2425

@@ -30,6 +31,7 @@ std = [
3031
"bp-runtime/std",
3132
"frame-support/std",
3233
"sp-api/std",
34+
"sp-runtime/std",
3335
"sp-std/std",
3436
"sp-version/std",
3537
]

bridges/primitives/chain-kusama/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use bp_messages::{LaneId, MessageDetails, MessageNonce, UnrewardedRelayersState}
2424
use frame_support::weights::{
2525
WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial,
2626
};
27+
use sp_runtime::FixedU128;
2728
use sp_std::prelude::*;
2829
use sp_version::RuntimeVersion;
2930

@@ -141,6 +142,7 @@ sp_api::decl_runtime_apis! {
141142
fn estimate_message_delivery_and_dispatch_fee(
142143
lane_id: LaneId,
143144
payload: OutboundPayload,
145+
kusama_to_this_conversion_rate: Option<FixedU128>,
144146
) -> Option<OutboundMessageFee>;
145147
/// Returns dispatch weight, encoded payload size and delivery+dispatch fee of all
146148
/// messages in given inclusive range.

bridges/primitives/chain-millau/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use scale_info::TypeInfo;
3333
use sp_core::Hasher as HasherT;
3434
use sp_runtime::{
3535
traits::{Convert, IdentifyAccount, Verify},
36-
MultiSignature, MultiSigner, Perbill,
36+
FixedU128, MultiSignature, MultiSigner, Perbill,
3737
};
3838
use sp_std::prelude::*;
3939
use sp_trie::{trie_types::Layout, TrieConfiguration};
@@ -319,6 +319,7 @@ sp_api::decl_runtime_apis! {
319319
fn estimate_message_delivery_and_dispatch_fee(
320320
lane_id: LaneId,
321321
payload: OutboundPayload,
322+
millau_to_this_conversion_rate: Option<FixedU128>,
322323
) -> Option<OutboundMessageFee>;
323324
/// Returns dispatch weight, encoded payload size and delivery+dispatch fee of all
324325
/// messages in given inclusive range.

bridges/primitives/chain-polkadot/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ bp-runtime = { path = "../runtime", default-features = false }
1919

2020
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
2121
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
22+
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
2223
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
2324
sp-version = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
2425

@@ -30,6 +31,7 @@ std = [
3031
"bp-runtime/std",
3132
"frame-support/std",
3233
"sp-api/std",
34+
"sp-runtime/std",
3335
"sp-std/std",
3436
"sp-version/std",
3537
]

bridges/primitives/chain-polkadot/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use bp_messages::{LaneId, MessageDetails, MessageNonce, UnrewardedRelayersState}
2424
use frame_support::weights::{
2525
WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial,
2626
};
27+
use sp_runtime::FixedU128;
2728
use sp_std::prelude::*;
2829
use sp_version::RuntimeVersion;
2930

@@ -141,6 +142,7 @@ sp_api::decl_runtime_apis! {
141142
fn estimate_message_delivery_and_dispatch_fee(
142143
lane_id: LaneId,
143144
payload: OutboundPayload,
145+
polkadot_to_this_conversion_rate: Option<FixedU128>,
144146
) -> Option<OutboundMessageFee>;
145147
/// Returns dispatch weight, encoded payload size and delivery+dispatch fee of all
146148
/// messages in given inclusive range.

0 commit comments

Comments
 (0)