Skip to content

Commit

Permalink
move signed extension stuff from prolkadot-core primitives to bridge-…
Browse files Browse the repository at this point in the history
…hub-cumulus-primitives (#1968)
  • Loading branch information
svyatonik committed Mar 17, 2023
1 parent 7481ce6 commit 0add06e
Show file tree
Hide file tree
Showing 13 changed files with 129 additions and 210 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 51 additions & 4 deletions bin/rialto-parachain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));

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

use bridge_runtime_common::messages::source::{XcmBridge, XcmBridgeAdapter};
use bridge_runtime_common::{
generate_bridge_reject_obsolete_headers_and_messages,
messages::source::{XcmBridge, XcmBridgeAdapter},
};
use codec::{Decode, Encode};
use cumulus_pallet_parachain_system::AnyRelayNumber;
use scale_info::TypeInfo;
use sp_api::impl_runtime_apis;
use sp_core::{crypto::KeyTypeId, OpaqueMetadata};
use sp_runtime::{
create_runtime_str, generic, impl_opaque_keys,
traits::{AccountIdLookup, Block as BlockT},
transaction_validity::{TransactionSource, TransactionValidity},
traits::{AccountIdLookup, Block as BlockT, DispatchInfoOf, SignedExtension},
transaction_validity::{TransactionSource, TransactionValidity, TransactionValidityError},
ApplyExtrinsicResult,
};

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

pub mod millau_messages;

// generate signed extension that rejects obsolete bridge transactions
generate_bridge_reject_obsolete_headers_and_messages! {
RuntimeCall, AccountId,
// Grandpa
BridgeMillauGrandpa,
// Messages
BridgeMillauMessages
}

/// Dummy signed extension that does nothing.
///
/// We're using it to have the same set of signed extensions on all parachains with bridge pallets
/// deployed (bridge hubs and rialto parachain).
#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)]
pub struct DummyBridgeRefundMillauMessages;

impl SignedExtension for DummyBridgeRefundMillauMessages {
const IDENTIFIER: &'static str = "DummyBridgeRefundMillauMessages";
type AccountId = AccountId;
type Call = RuntimeCall;
type AdditionalSigned = ();
type Pre = ();

fn additional_signed(&self) -> Result<Self::AdditionalSigned, TransactionValidityError> {
Ok(())
}

fn pre_dispatch(
self,
_who: &Self::AccountId,
_call: &Self::Call,
_info: &DispatchInfoOf<Self::Call>,
_len: usize,
) -> Result<Self::Pre, TransactionValidityError> {
Ok(())
}
}

/// The address format for describing accounts.
pub type Address = MultiAddress<AccountId, ()>;
/// Block type as expected by this runtime.
Expand All @@ -111,6 +154,8 @@ pub type SignedExtra = (
frame_system::CheckNonce<Runtime>,
frame_system::CheckWeight<Runtime>,
pallet_transaction_payment::ChargeTransactionPayment<Runtime>,
BridgeRejectObsoleteHeadersAndMessages,
DummyBridgeRefundMillauMessages,
);
/// Unchecked extrinsic type as expected by this runtime.
pub type UncheckedExtrinsic =
Expand Down Expand Up @@ -909,9 +954,11 @@ mod tests {
frame_system::CheckNonce::from(10),
frame_system::CheckWeight::new(),
pallet_transaction_payment::ChargeTransactionPayment::from(10),
BridgeRejectObsoleteHeadersAndMessages,
DummyBridgeRefundMillauMessages,
);
let indirect_payload = bp_rialto_parachain::SignedExtension::new(
((), (), (), (), Era::Immortal, 10.into(), (), 10.into()),
((), (), (), (), Era::Immortal, 10.into(), (), 10.into(), (), ()),
None,
);
assert_eq!(payload.encode(), indirect_payload.encode());
Expand Down
89 changes: 55 additions & 34 deletions primitives/chain-bridge-hub-cumulus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@

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

use bp_messages::*;
pub use bp_polkadot_core::{
AccountId, AccountInfoStorageMapKeyProvider, AccountPublic, Balance, BlockNumber, Hash, Hasher,
Hashing, Header, Index, Nonce, Perbill, Signature, SignedBlock, UncheckedExtrinsic,
EXTRA_STORAGE_PROOF_SIZE, TX_EXTRA_BYTES,
};

use bp_messages::*;
use bp_runtime::extensions::{
BridgeRejectObsoleteHeadersAndMessages, ChargeTransactionPayment, CheckEra, CheckGenesis,
CheckNonZeroSender, CheckNonce, CheckSpecVersion, CheckTxVersion, CheckWeight,
GenericSignedExtension, RefundBridgedParachainMessagesSchema,
};
use frame_support::{
dispatch::DispatchClass,
parameter_types,
Expand Down Expand Up @@ -94,8 +100,6 @@ pub type AccountSigner = MultiSigner;
/// The address format for describing accounts.
pub type Address = MultiAddress<AccountId, ()>;

pub use bp_polkadot_core::BridgeSignedExtension as SignedExtension;

// Note about selecting values of two following constants:
//
// Normal transactions have limit of 75% of 1/2 second weight for Cumulus parachains. Let's keep
Expand Down Expand Up @@ -125,37 +129,54 @@ pub const MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX: MessageNonce = 1024;
/// Maximal number of unconfirmed messages at inbound lane for Cumulus-based parachains.
pub const MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX: MessageNonce = 4096;

/// Module with rewarding bridge signed extension support
pub mod rewarding_bridge_signed_extension {
use super::*;
use bp_polkadot_core::PolkadotLike;
use bp_runtime::extensions::*;

type RewardingBridgeSignedExtra = (
CheckNonZeroSender,
CheckSpecVersion,
CheckTxVersion,
CheckGenesis<PolkadotLike>,
CheckEra<PolkadotLike>,
CheckNonce<Nonce>,
CheckWeight,
ChargeTransactionPayment<PolkadotLike>,
BridgeRejectObsoleteHeadersAndMessages,
RefundBridgedParachainMessagesSchema,
);
/// Extra signed extension data that is used by all bridge hubs.
pub type SignedExtra = (
CheckNonZeroSender,
CheckSpecVersion,
CheckTxVersion,
CheckGenesis<Hash>,
CheckEra<Hash>,
CheckNonce<Index>,
CheckWeight,
ChargeTransactionPayment<Balance>,
BridgeRejectObsoleteHeadersAndMessages,
RefundBridgedParachainMessagesSchema,
);

/// Signed extension that is used by all bridge hubs.
pub type SignedExtension = GenericSignedExtension<SignedExtra>;

/// Helper trait to define some extra methods on bridge hubs signed extension (and
/// overcome Rust limitations).
pub trait BridgeHubSignedExtension {
/// Create signed extension from its components.
fn from_params(
spec_version: u32,
transaction_version: u32,
era: bp_runtime::TransactionEra<BlockNumber, Hash>,
genesis_hash: Hash,
nonce: Index,
tip: Balance,
) -> Self;

/// The signed extension used by Cumulus and Cumulus-like parachain with bridging and rewarding.
pub type RewardingBridgeSignedExtension = GenericSignedExtension<RewardingBridgeSignedExtra>;
/// Return transaction nonce.
fn nonce(&self) -> Index;

/// Return transaction tip.
fn tip(&self) -> Balance;
}

pub fn from_params(
impl BridgeHubSignedExtension for SignedExtension {
/// Create signed extension from its components.
fn from_params(
spec_version: u32,
transaction_version: u32,
era: bp_runtime::TransactionEraOf<PolkadotLike>,
era: bp_runtime::TransactionEra<BlockNumber, Hash>,
genesis_hash: Hash,
nonce: Nonce,
nonce: Index,
tip: Balance,
) -> RewardingBridgeSignedExtension {
GenericSignedExtension::<RewardingBridgeSignedExtra>::new(
) -> Self {
GenericSignedExtension::new(
(
(), // non-zero sender
(), // spec version
Expand All @@ -166,7 +187,7 @@ pub mod rewarding_bridge_signed_extension {
(), // Check weight
tip.into(), // transaction payment / tip (compact encoding)
(), // bridge reject obsolete headers and msgs
(), // bridge register reward to relayer for message passing
(), // bridge reward to relayer for message passing
),
Some((
(),
Expand All @@ -183,13 +204,13 @@ pub mod rewarding_bridge_signed_extension {
)
}

/// Return signer nonce, used to craft transaction.
pub fn nonce(sign_ext: &RewardingBridgeSignedExtension) -> Nonce {
sign_ext.payload.5.into()
/// Return transaction nonce.
fn nonce(&self) -> Index {
self.payload.5 .0
}

/// Return transaction tip.
pub fn tip(sign_ext: &RewardingBridgeSignedExtension) -> Balance {
sign_ext.payload.7.into()
fn tip(&self) -> Balance {
self.payload.7 .0
}
}
2 changes: 2 additions & 0 deletions primitives/chain-rialto-parachain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ license = "GPL-3.0-or-later WITH Classpath-exception-2.0"

# Bridge Dependencies

bp-bridge-hub-cumulus = { path = "../chain-bridge-hub-cumulus", default-features = false }
bp-messages = { path = "../messages", default-features = false }
bp-polkadot-core = { path = "../polkadot-core", default-features = false }
bp-runtime = { path = "../runtime", default-features = false }
Expand All @@ -26,6 +27,7 @@ sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", d
[features]
default = ["std"]
std = [
"bp-bridge-hub-cumulus/std",
"bp-messages/std",
"bp-runtime/std",
"frame-support/std",
Expand Down
4 changes: 3 additions & 1 deletion primitives/chain-rialto-parachain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ impl Parachain for RialtoParachain {
const PARACHAIN_ID: u32 = RIALTO_PARACHAIN_ID;
}

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

frame_support::parameter_types! {
pub BlockLength: limits::BlockLength =
Expand Down
Loading

0 comments on commit 0add06e

Please sign in to comment.