Skip to content

Commit e5700e2

Browse files
authored
move signed extension stuff from prolkadot-core primitives to bridge-hub-cumulus-primitives (paritytech#1968)
1 parent 9fde0a9 commit e5700e2

File tree

12 files changed

+127
-210
lines changed

12 files changed

+127
-210
lines changed

bin/rialto-parachain/runtime/src/lib.rs

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,19 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
2828

2929
use crate::millau_messages::{WithMillauMessageBridge, XCM_LANE};
3030

31-
use bridge_runtime_common::messages::source::{XcmBridge, XcmBridgeAdapter};
31+
use bridge_runtime_common::{
32+
generate_bridge_reject_obsolete_headers_and_messages,
33+
messages::source::{XcmBridge, XcmBridgeAdapter},
34+
};
35+
use codec::{Decode, Encode};
3236
use cumulus_pallet_parachain_system::AnyRelayNumber;
37+
use scale_info::TypeInfo;
3338
use sp_api::impl_runtime_apis;
3439
use sp_core::{crypto::KeyTypeId, OpaqueMetadata};
3540
use sp_runtime::{
3641
create_runtime_str, generic, impl_opaque_keys,
37-
traits::{AccountIdLookup, Block as BlockT},
38-
transaction_validity::{TransactionSource, TransactionValidity},
42+
traits::{AccountIdLookup, Block as BlockT, DispatchInfoOf, SignedExtension},
43+
transaction_validity::{TransactionSource, TransactionValidity, TransactionValidityError},
3944
ApplyExtrinsicResult,
4045
};
4146

@@ -93,6 +98,44 @@ use xcm_executor::{Config, XcmExecutor};
9398

9499
pub mod millau_messages;
95100

101+
// generate signed extension that rejects obsolete bridge transactions
102+
generate_bridge_reject_obsolete_headers_and_messages! {
103+
RuntimeCall, AccountId,
104+
// Grandpa
105+
BridgeMillauGrandpa,
106+
// Messages
107+
BridgeMillauMessages
108+
}
109+
110+
/// Dummy signed extension that does nothing.
111+
///
112+
/// We're using it to have the same set of signed extensions on all parachains with bridge pallets
113+
/// deployed (bridge hubs and rialto parachain).
114+
#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)]
115+
pub struct DummyBridgeRefundMillauMessages;
116+
117+
impl SignedExtension for DummyBridgeRefundMillauMessages {
118+
const IDENTIFIER: &'static str = "DummyBridgeRefundMillauMessages";
119+
type AccountId = AccountId;
120+
type Call = RuntimeCall;
121+
type AdditionalSigned = ();
122+
type Pre = ();
123+
124+
fn additional_signed(&self) -> Result<Self::AdditionalSigned, TransactionValidityError> {
125+
Ok(())
126+
}
127+
128+
fn pre_dispatch(
129+
self,
130+
_who: &Self::AccountId,
131+
_call: &Self::Call,
132+
_info: &DispatchInfoOf<Self::Call>,
133+
_len: usize,
134+
) -> Result<Self::Pre, TransactionValidityError> {
135+
Ok(())
136+
}
137+
}
138+
96139
/// The address format for describing accounts.
97140
pub type Address = MultiAddress<AccountId, ()>;
98141
/// Block type as expected by this runtime.
@@ -111,6 +154,8 @@ pub type SignedExtra = (
111154
frame_system::CheckNonce<Runtime>,
112155
frame_system::CheckWeight<Runtime>,
113156
pallet_transaction_payment::ChargeTransactionPayment<Runtime>,
157+
BridgeRejectObsoleteHeadersAndMessages,
158+
DummyBridgeRefundMillauMessages,
114159
);
115160
/// Unchecked extrinsic type as expected by this runtime.
116161
pub type UncheckedExtrinsic =
@@ -909,9 +954,11 @@ mod tests {
909954
frame_system::CheckNonce::from(10),
910955
frame_system::CheckWeight::new(),
911956
pallet_transaction_payment::ChargeTransactionPayment::from(10),
957+
BridgeRejectObsoleteHeadersAndMessages,
958+
DummyBridgeRefundMillauMessages,
912959
);
913960
let indirect_payload = bp_rialto_parachain::SignedExtension::new(
914-
((), (), (), (), Era::Immortal, 10.into(), (), 10.into()),
961+
((), (), (), (), Era::Immortal, 10.into(), (), 10.into(), (), ()),
915962
None,
916963
);
917964
assert_eq!(payload.encode(), indirect_payload.encode());

primitives/chain-bridge-hub-cumulus/src/lib.rs

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@
1616

1717
#![cfg_attr(not(feature = "std"), no_std)]
1818

19-
use bp_messages::*;
2019
pub use bp_polkadot_core::{
2120
AccountId, AccountInfoStorageMapKeyProvider, AccountPublic, Balance, BlockNumber, Hash, Hasher,
2221
Hashing, Header, Index, Nonce, Perbill, Signature, SignedBlock, UncheckedExtrinsic,
2322
EXTRA_STORAGE_PROOF_SIZE, TX_EXTRA_BYTES,
2423
};
24+
25+
use bp_messages::*;
26+
use bp_runtime::extensions::{
27+
BridgeRejectObsoleteHeadersAndMessages, ChargeTransactionPayment, CheckEra, CheckGenesis,
28+
CheckNonZeroSender, CheckNonce, CheckSpecVersion, CheckTxVersion, CheckWeight,
29+
GenericSignedExtension, RefundBridgedParachainMessagesSchema,
30+
};
2531
use frame_support::{
2632
dispatch::DispatchClass,
2733
parameter_types,
@@ -94,8 +100,6 @@ pub type AccountSigner = MultiSigner;
94100
/// The address format for describing accounts.
95101
pub type Address = MultiAddress<AccountId, ()>;
96102

97-
pub use bp_polkadot_core::BridgeSignedExtension as SignedExtension;
98-
99103
// Note about selecting values of two following constants:
100104
//
101105
// Normal transactions have limit of 75% of 1/2 second weight for Cumulus parachains. Let's keep
@@ -125,37 +129,54 @@ pub const MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX: MessageNonce = 1024;
125129
/// Maximal number of unconfirmed messages at inbound lane for Cumulus-based parachains.
126130
pub const MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX: MessageNonce = 4096;
127131

128-
/// Module with rewarding bridge signed extension support
129-
pub mod rewarding_bridge_signed_extension {
130-
use super::*;
131-
use bp_polkadot_core::PolkadotLike;
132-
use bp_runtime::extensions::*;
133-
134-
type RewardingBridgeSignedExtra = (
135-
CheckNonZeroSender,
136-
CheckSpecVersion,
137-
CheckTxVersion,
138-
CheckGenesis<PolkadotLike>,
139-
CheckEra<PolkadotLike>,
140-
CheckNonce<Nonce>,
141-
CheckWeight,
142-
ChargeTransactionPayment<PolkadotLike>,
143-
BridgeRejectObsoleteHeadersAndMessages,
144-
RefundBridgedParachainMessagesSchema,
145-
);
132+
/// Extra signed extension data that is used by all bridge hubs.
133+
pub type SignedExtra = (
134+
CheckNonZeroSender,
135+
CheckSpecVersion,
136+
CheckTxVersion,
137+
CheckGenesis<Hash>,
138+
CheckEra<Hash>,
139+
CheckNonce<Index>,
140+
CheckWeight,
141+
ChargeTransactionPayment<Balance>,
142+
BridgeRejectObsoleteHeadersAndMessages,
143+
RefundBridgedParachainMessagesSchema,
144+
);
145+
146+
/// Signed extension that is used by all bridge hubs.
147+
pub type SignedExtension = GenericSignedExtension<SignedExtra>;
148+
149+
/// Helper trait to define some extra methods on bridge hubs signed extension (and
150+
/// overcome Rust limitations).
151+
pub trait BridgeHubSignedExtension {
152+
/// Create signed extension from its components.
153+
fn from_params(
154+
spec_version: u32,
155+
transaction_version: u32,
156+
era: bp_runtime::TransactionEra<BlockNumber, Hash>,
157+
genesis_hash: Hash,
158+
nonce: Index,
159+
tip: Balance,
160+
) -> Self;
146161

147-
/// The signed extension used by Cumulus and Cumulus-like parachain with bridging and rewarding.
148-
pub type RewardingBridgeSignedExtension = GenericSignedExtension<RewardingBridgeSignedExtra>;
162+
/// Return transaction nonce.
163+
fn nonce(&self) -> Index;
164+
165+
/// Return transaction tip.
166+
fn tip(&self) -> Balance;
167+
}
149168

150-
pub fn from_params(
169+
impl BridgeHubSignedExtension for SignedExtension {
170+
/// Create signed extension from its components.
171+
fn from_params(
151172
spec_version: u32,
152173
transaction_version: u32,
153-
era: bp_runtime::TransactionEraOf<PolkadotLike>,
174+
era: bp_runtime::TransactionEra<BlockNumber, Hash>,
154175
genesis_hash: Hash,
155-
nonce: Nonce,
176+
nonce: Index,
156177
tip: Balance,
157-
) -> RewardingBridgeSignedExtension {
158-
GenericSignedExtension::<RewardingBridgeSignedExtra>::new(
178+
) -> Self {
179+
GenericSignedExtension::new(
159180
(
160181
(), // non-zero sender
161182
(), // spec version
@@ -166,7 +187,7 @@ pub mod rewarding_bridge_signed_extension {
166187
(), // Check weight
167188
tip.into(), // transaction payment / tip (compact encoding)
168189
(), // bridge reject obsolete headers and msgs
169-
(), // bridge register reward to relayer for message passing
190+
(), // bridge reward to relayer for message passing
170191
),
171192
Some((
172193
(),
@@ -183,13 +204,13 @@ pub mod rewarding_bridge_signed_extension {
183204
)
184205
}
185206

186-
/// Return signer nonce, used to craft transaction.
187-
pub fn nonce(sign_ext: &RewardingBridgeSignedExtension) -> Nonce {
188-
sign_ext.payload.5.into()
207+
/// Return transaction nonce.
208+
fn nonce(&self) -> Index {
209+
self.payload.5 .0
189210
}
190211

191212
/// Return transaction tip.
192-
pub fn tip(sign_ext: &RewardingBridgeSignedExtension) -> Balance {
193-
sign_ext.payload.7.into()
213+
fn tip(&self) -> Balance {
214+
self.payload.7 .0
194215
}
195216
}

primitives/chain-rialto-parachain/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
1010

1111
# Bridge Dependencies
1212

13+
bp-bridge-hub-cumulus = { path = "../chain-bridge-hub-cumulus", default-features = false }
1314
bp-messages = { path = "../messages", default-features = false }
1415
bp-polkadot-core = { path = "../polkadot-core", default-features = false }
1516
bp-runtime = { path = "../runtime", default-features = false }
@@ -26,6 +27,7 @@ sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", d
2627
[features]
2728
default = ["std"]
2829
std = [
30+
"bp-bridge-hub-cumulus/std",
2931
"bp-messages/std",
3032
"bp-runtime/std",
3133
"frame-support/std",

primitives/chain-rialto-parachain/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ impl Parachain for RialtoParachain {
133133
const PARACHAIN_ID: u32 = RIALTO_PARACHAIN_ID;
134134
}
135135

136-
pub use bp_polkadot_core::DefaultSignedExtension as SignedExtension;
136+
// Technically this is incorrect, because rialto-parachain isn't a bridge hub, but we're
137+
// trying to keep it close to the bridge hubs code (at least in this aspect).
138+
pub use bp_bridge_hub_cumulus::SignedExtension;
137139

138140
frame_support::parameter_types! {
139141
pub BlockLength: limits::BlockLength =

0 commit comments

Comments
 (0)