Skip to content

Commit

Permalink
Add integrity check for signed extensions (paritytech#1780)
Browse files Browse the repository at this point in the history
* Add integrity check for signed extensions

* Remove unneeded type specification
  • Loading branch information
serban300 committed Apr 8, 2024
1 parent 86d463f commit 340fff0
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 19 deletions.
3 changes: 3 additions & 0 deletions bridges/bin/rialto-parachain/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ xcm-builder = { git = "https://github.com/paritytech/polkadot", branch = "master
xcm-executor = { git = "https://github.com/paritytech/polkadot", branch = "master", default-features = false }
pallet-xcm = { git = "https://github.com/paritytech/polkadot", branch = "master", default-features = false }

[dev-dependencies]
bridge-runtime-common = { path = "../../runtime-common", features = ["integrity-test"] }

[features]
default = ['std']
runtime-benchmarks = [
Expand Down
26 changes: 25 additions & 1 deletion bridges/bin/rialto-parachain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -848,8 +848,11 @@ mod tests {
LaneId, MessageKey,
};
use bp_runtime::messages::MessageDispatchResult;
use bridge_runtime_common::messages::target::FromBridgedChainMessageDispatch;
use bridge_runtime_common::{
integrity::check_additional_signed, messages::target::FromBridgedChainMessageDispatch,
};
use codec::Encode;
use sp_runtime::generic::Era;

fn new_test_ext() -> sp_io::TestExternalities {
sp_io::TestExternalities::new(
Expand Down Expand Up @@ -909,4 +912,25 @@ mod tests {
);
})
}

#[test]
fn ensure_signed_extension_definition_is_correct() {
let payload: SignedExtra = (
frame_system::CheckNonZeroSender::new(),
frame_system::CheckSpecVersion::new(),
frame_system::CheckTxVersion::new(),
frame_system::CheckGenesis::new(),
frame_system::CheckEra::from(Era::Immortal),
frame_system::CheckNonce::from(10),
frame_system::CheckWeight::new(),
pallet_transaction_payment::ChargeTransactionPayment::from(10),
);
let indirect_payload = bp_rialto_parachain::SignedExtension::new(
((), (), (), (), Era::Immortal, 10.into(), (), 10.into()),
None,
);
assert_eq!(payload.encode(), indirect_payload.encode());

check_additional_signed::<SignedExtra, bp_rialto_parachain::SignedExtension>();
}
}
13 changes: 13 additions & 0 deletions bridges/bin/runtime-common/src/integrity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use bp_runtime::{Chain, ChainId};
use codec::Encode;
use frame_support::{storage::generator::StorageValue, traits::Get};
use frame_system::limits;
use sp_runtime::traits::SignedExtension;

/// Macro that ensures that the runtime configuration and chain primitives crate are sharing
/// the same types (index, block number, hash, hasher, account id and header).
Expand Down Expand Up @@ -319,3 +320,15 @@ pub fn check_message_lane_weights<C: Chain, T: frame_system::Config>(
this_chain_max_unconfirmed_messages,
);
}

/// Check that the `AdditionalSigned` type of a wrapped runtime is the same as the one of the
/// corresponding actual runtime.
///
/// This method doesn't perform any `assert`. If the condition is not true it will generate a
/// compile-time error.
pub fn check_additional_signed<SignedExt, IndirectSignedExt: SignedExtension>()
where
SignedExt: SignedExtension,
IndirectSignedExt: SignedExtension<AdditionalSigned = SignedExt::AdditionalSigned>,
{
}
8 changes: 5 additions & 3 deletions bridges/primitives/chain-bridge-hub-cumulus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

use bp_messages::*;
pub use bp_polkadot_core::{
AccountId, AccountInfoStorageMapKeyProvider, AccountPublic, Balance, BlockNumber,
BridgeSignedExtension, Hash, Hasher, Hashing, Header, Index, Nonce, Perbill,
PolkadotSignedExtension, Signature, SignedBlock, UncheckedExtrinsic, TX_EXTRA_BYTES,
AccountId, AccountInfoStorageMapKeyProvider, AccountPublic, Balance, BlockNumber, Hash, Hasher,
Hashing, Header, Index, Nonce, Perbill, PolkadotSignedExtension, Signature, SignedBlock,
UncheckedExtrinsic, TX_EXTRA_BYTES,
};
use frame_support::{
dispatch::DispatchClass,
Expand Down Expand Up @@ -86,6 +86,8 @@ 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
1 change: 1 addition & 0 deletions bridges/primitives/chain-rialto-parachain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
# Bridge Dependencies

bp-messages = { path = "../messages", default-features = false }
bp-polkadot-core = { path = "../polkadot-core", default-features = false }
bp-runtime = { path = "../runtime", default-features = false }

# Substrate Based Dependencies
Expand Down
2 changes: 2 additions & 0 deletions bridges/primitives/chain-rialto-parachain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ impl Parachain for RialtoParachain {
const PARACHAIN_ID: u32 = RIALTO_PARACHAIN_ID;
}

pub use bp_polkadot_core::DefaultSignedExtension as SignedExtension;

frame_support::parameter_types! {
pub BlockLength: limits::BlockLength =
limits::BlockLength::max_with_normal_ratio(5 * 1024 * 1024, NORMAL_DISPATCH_RATIO);
Expand Down
8 changes: 4 additions & 4 deletions bridges/primitives/polkadot-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl PolkadotSignedExtension for DefaultSignedExtension {
(), // Check weight
tip.into(), // transaction payment / tip (compact encoding)
),
(
Some((
(),
spec_version,
transaction_version,
Expand All @@ -275,7 +275,7 @@ impl PolkadotSignedExtension for DefaultSignedExtension {
(),
(),
(),
),
)),
)
}

Expand Down Expand Up @@ -326,7 +326,7 @@ impl PolkadotSignedExtension for BridgeSignedExtension {
tip.into(), // transaction payment / tip (compact encoding)
(), // bridge reject obsolete headers and msgs
),
(
Some((
(),
spec_version,
transaction_version,
Expand All @@ -336,7 +336,7 @@ impl PolkadotSignedExtension for BridgeSignedExtension {
(),
(),
(),
),
)),
)
}

Expand Down
4 changes: 2 additions & 2 deletions bridges/primitives/runtime/src/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ pub struct GenericSignedExtension<S: SignedExtensionSchema> {
}

impl<S: SignedExtensionSchema> GenericSignedExtension<S> {
pub fn new(payload: S::Payload, additional_signed: S::AdditionalSigned) -> Self {
Self { payload, additional_signed: Some(additional_signed) }
pub fn new(payload: S::Payload, additional_signed: Option<S::AdditionalSigned>) -> Self {
Self { payload, additional_signed }
}
}

Expand Down
2 changes: 1 addition & 1 deletion bridges/relays/client-bridge-hub-rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl ChainWithTransactions for BridgeHubRococo {
) -> Result<Self::SignedTransaction, SubstrateError> {
let raw_payload = SignedPayload::new(
unsigned.call,
bp_bridge_hub_rococo::BridgeSignedExtension::from_params(
bp_bridge_hub_rococo::SignedExtension::from_params(
param.spec_version,
param.transaction_version,
unsigned.era,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
use codec::{Decode, Encode};
use scale_info::TypeInfo;

use bp_bridge_hub_rococo::BridgeSignedExtension;
use bp_bridge_hub_rococo::SignedExtension;
pub use bp_header_chain::BridgeGrandpaCallOf;
pub use bp_parachains::BridgeParachainCall;
pub use bridge_runtime_common::messages::BridgeMessagesCallOf;
pub use relay_substrate_client::calls::SystemCall;

/// Unchecked BridgeHubRococo extrinsic.
pub type UncheckedExtrinsic = bp_bridge_hub_rococo::UncheckedExtrinsic<Call, BridgeSignedExtension>;
pub type UncheckedExtrinsic = bp_bridge_hub_rococo::UncheckedExtrinsic<Call, SignedExtension>;

// The indirect pallet call used to sync `Wococo` GRANDPA finality to `BHRococo`.
pub type BridgeWococoGrandpaCall = BridgeGrandpaCallOf<bp_wococo::Wococo>;
Expand Down
2 changes: 1 addition & 1 deletion bridges/relays/client-bridge-hub-wococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl ChainWithTransactions for BridgeHubWococo {
) -> Result<Self::SignedTransaction, SubstrateError> {
let raw_payload = SignedPayload::new(
unsigned.call,
bp_bridge_hub_wococo::BridgeSignedExtension::from_params(
bp_bridge_hub_wococo::SignedExtension::from_params(
param.spec_version,
param.transaction_version,
unsigned.era,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
use codec::{Decode, Encode};
use scale_info::TypeInfo;

use bp_bridge_hub_wococo::BridgeSignedExtension;
use bp_bridge_hub_wococo::SignedExtension;
pub use bp_header_chain::BridgeGrandpaCallOf;
pub use bp_parachains::BridgeParachainCall;
pub use bridge_runtime_common::messages::BridgeMessagesCallOf;
pub use relay_substrate_client::calls::SystemCall;

/// Unchecked BridgeHubWococo extrinsic.
pub type UncheckedExtrinsic = bp_bridge_hub_wococo::UncheckedExtrinsic<Call, BridgeSignedExtension>;
pub type UncheckedExtrinsic = bp_bridge_hub_wococo::UncheckedExtrinsic<Call, SignedExtension>;

// The indirect pallet call used to sync `Rococo` GRANDPA finality to `BHWococo`.
pub type BridgeRococoGrandpaCall = BridgeGrandpaCallOf<bp_rococo::Rococo>;
Expand Down
6 changes: 3 additions & 3 deletions bridges/relays/client-rialto-parachain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
pub mod runtime_wrapper;

use bp_messages::MessageNonce;
use bp_polkadot_core::{DefaultSignedExtension, PolkadotSignedExtension};
use bp_polkadot_core::PolkadotSignedExtension;
use codec::Encode;
use relay_substrate_client::{
Chain, ChainWithBalances, ChainWithMessages, ChainWithTransactions, Error as SubstrateError,
Expand Down Expand Up @@ -79,15 +79,15 @@ impl ChainWithMessages for RialtoParachain {
impl ChainWithTransactions for RialtoParachain {
type AccountKeyPair = sp_core::sr25519::Pair;
type SignedTransaction =
bp_polkadot_core::UncheckedExtrinsic<Self::Call, DefaultSignedExtension>;
bp_polkadot_core::UncheckedExtrinsic<Self::Call, bp_rialto_parachain::SignedExtension>;

fn sign_transaction(
param: SignParam<Self>,
unsigned: UnsignedTransaction<Self>,
) -> Result<Self::SignedTransaction, SubstrateError> {
let raw_payload = SignedPayload::new(
unsigned.call,
bp_polkadot_core::DefaultSignedExtension::from_params(
bp_rialto_parachain::SignedExtension::from_params(
param.spec_version,
param.transaction_version,
unsigned.era,
Expand Down

0 comments on commit 340fff0

Please sign in to comment.