Skip to content

Commit

Permalink
verify messages delivery proofs (paritytech#482)
Browse files Browse the repository at this point in the history
  • Loading branch information
svyatonik authored and serban300 committed Apr 9, 2024
1 parent 8990856 commit b3f8b99
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 13 deletions.
17 changes: 11 additions & 6 deletions bridges/bin/millau/runtime/src/rialto_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use frame_support::{
RuntimeDebug,
};
use sp_core::storage::StorageKey;
use sp_trie::StorageProof;

/// Storage key of the Millau -> Rialto message in the runtime storage.
pub fn message_key(lane: &LaneId, nonce: MessageNonce) -> StorageKey {
Expand Down Expand Up @@ -63,6 +62,12 @@ pub type ToRialtoMessageVerifier = messages::source::FromThisChainMessageVerifie
/// Message payload for Rialto -> Millau messages.
pub type FromRialtoMessagePayload = messages::target::FromBridgedChainMessagePayload<WithRialtoMessageBridge>;

/// Messages proof for Rialto -> Millau messages.
type FromRialtoMessagesProof = messages::target::FromBridgedChainMessagesProof<WithRialtoMessageBridge>;

/// Messages delivery proof for Millau -> Rialto messages.
type ToRialtoMessagesDeliveryProof = messages::source::FromBridgedChainMessagesDeliveryProof<WithRialtoMessageBridge>;

/// Call-dispatch based message dispatch for Rialto -> Millau messages.
pub type FromRialtoMessageDispatch = messages::target::FromBridgedChainMessageDispatch<
WithRialtoMessageBridge,
Expand Down Expand Up @@ -152,7 +157,7 @@ impl TargetHeaderChain<ToRialtoMessagePayload, bp_rialto::AccountId> for Rialto
// - hash of the header this proof has been created with;
// - the storage proof or one or several keys;
// - id of the lane we prove state of.
type MessagesDeliveryProof = (bp_rialto::Hash, StorageProof, LaneId);
type MessagesDeliveryProof = ToRialtoMessagesDeliveryProof;

fn verify_message(payload: &ToRialtoMessagePayload) -> Result<(), Self::Error> {
if payload.weight > WithRialtoMessageBridge::maximal_dispatch_weight_of_message_on_bridged_chain() {
Expand All @@ -163,9 +168,9 @@ impl TargetHeaderChain<ToRialtoMessagePayload, bp_rialto::AccountId> for Rialto
}

fn verify_messages_delivery_proof(
_proof: Self::MessagesDeliveryProof,
) -> Result<(LaneId, InboundLaneData<bp_rialto::AccountId>), Self::Error> {
unimplemented!("https://github.com/paritytech/parity-bridges-common/issues/397")
proof: Self::MessagesDeliveryProof,
) -> Result<(LaneId, InboundLaneData<bp_millau::AccountId>), Self::Error> {
messages::source::verify_messages_delivery_proof::<WithRialtoMessageBridge, Runtime>(proof)
}
}

Expand All @@ -176,7 +181,7 @@ impl SourceHeaderChain<bp_rialto::Balance> for Rialto {
// - the storage proof or one or several keys;
// - id of the lane we prove messages for;
// - inclusive range of messages nonces that are proved.
type MessagesProof = (bp_rialto::Hash, StorageProof, LaneId, MessageNonce, MessageNonce);
type MessagesProof = FromRialtoMessagesProof;

fn verify_messages_proof(
_proof: Self::MessagesProof,
Expand Down
12 changes: 7 additions & 5 deletions bridges/bin/rialto/runtime/src/millau_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use frame_support::{
RuntimeDebug,
};
use sp_core::storage::StorageKey;
use sp_trie::StorageProof;

/// Storage key of the Rialto -> Millau message in the runtime storage.
pub fn message_key(lane: &LaneId, nonce: MessageNonce) -> StorageKey {
Expand Down Expand Up @@ -73,6 +72,9 @@ pub type FromMillauMessageDispatch = messages::target::FromBridgedChainMessageDi
/// Messages proof for Millau -> Rialto messages.
type FromMillauMessagesProof = messages::target::FromBridgedChainMessagesProof<WithMillauMessageBridge>;

/// Messages delivery proof for Rialto -> Millau messages.
type ToMillauMessagesDeliveryProof = messages::source::FromBridgedChainMessagesDeliveryProof<WithMillauMessageBridge>;

/// Millau <-> Rialto message bridge.
#[derive(RuntimeDebug, Clone, Copy)]
pub struct WithMillauMessageBridge;
Expand Down Expand Up @@ -155,7 +157,7 @@ impl TargetHeaderChain<ToMillauMessagePayload, bp_millau::AccountId> for Millau
// - hash of the header this proof has been created with;
// - the storage proof of one or several keys;
// - id of the lane we prove state of.
type MessagesDeliveryProof = (bp_millau::Hash, StorageProof, LaneId);
type MessagesDeliveryProof = ToMillauMessagesDeliveryProof;

fn verify_message(payload: &ToMillauMessagePayload) -> Result<(), Self::Error> {
if payload.weight > WithMillauMessageBridge::maximal_dispatch_weight_of_message_on_bridged_chain() {
Expand All @@ -166,9 +168,9 @@ impl TargetHeaderChain<ToMillauMessagePayload, bp_millau::AccountId> for Millau
}

fn verify_messages_delivery_proof(
_proof: Self::MessagesDeliveryProof,
) -> Result<(LaneId, InboundLaneData<bp_millau::AccountId>), Self::Error> {
unimplemented!("https://github.com/paritytech/parity-bridges-common/issues/397")
proof: Self::MessagesDeliveryProof,
) -> Result<(LaneId, InboundLaneData<bp_rialto::AccountId>), Self::Error> {
messages::source::verify_messages_delivery_proof::<WithMillauMessageBridge, Runtime>(proof)
}
}

Expand Down
2 changes: 2 additions & 0 deletions bridges/bin/runtime-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ bp-message-lane = { path = "../../primitives/message-lane", default-features = f
bp-runtime = { path = "../../primitives/runtime", default-features = false }
pallet-bridge-call-dispatch = { path = "../../modules/call-dispatch", default-features = false }
pallet-message-lane = { path = "../../modules/message-lane", default-features = false }
pallet-substrate-bridge = { path = "../../modules/substrate", default-features = false }

# Substrate dependencies

Expand All @@ -35,6 +36,7 @@ std = [
"frame-support/std",
"pallet-bridge-call-dispatch/std",
"pallet-message-lane/std",
"pallet-substrate-bridge/std",
"sp-runtime/std",
"sp-std/std",
"sp-trie/std",
Expand Down
49 changes: 47 additions & 2 deletions bridges/bin/runtime-common/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use bp_message_dispatch::MessageDispatch as _;
use bp_message_lane::{
source_chain::LaneMessageVerifier,
target_chain::{DispatchMessage, MessageDispatch},
LaneId, MessageNonce,
InboundLaneData, LaneId, MessageNonce,
};
use bp_runtime::InstanceId;
use codec::{Compact, Decode, Input};
Expand Down Expand Up @@ -77,7 +77,7 @@ pub trait ChainWithMessageLanes {
/// Hash used in the chain.
type Hash: Decode;
/// Accound id on the chain.
type AccountId;
type AccountId: Decode;
/// Public key of the chain account that may be used to verify signatures.
type Signer: Decode;
/// Signature type used on the chain.
Expand Down Expand Up @@ -105,6 +105,7 @@ pub(crate) type SignatureOf<C> = <C as ChainWithMessageLanes>::Signature;
pub(crate) type WeightOf<C> = <C as ChainWithMessageLanes>::Weight;
pub(crate) type BalanceOf<C> = <C as ChainWithMessageLanes>::Balance;
pub(crate) type CallOf<C> = <C as ChainWithMessageLanes>::Call;
pub(crate) type MessageLaneInstanceOf<C> = <C as ChainWithMessageLanes>::MessageLaneInstance;

/// Sub-module that is declaring types required for processing This -> Bridged chain messages.
pub mod source {
Expand All @@ -121,6 +122,16 @@ pub mod source {
BridgedChainOpaqueCall,
>;

/// Messages delivery proof from bridged chain:
///
/// - hash of finalized header;
/// - storage proof of inbound lane state;
/// - lane id.
pub type FromBridgedChainMessagesDeliveryProof<B> = (HashOf<BridgedChain<B>>, StorageProof, LaneId);

/// 'Parsed' message delivery proof - inbound lane id and its state.
pub type ParsedMessagesDeliveryProofFromBridgedChain<B> = (LaneId, InboundLaneData<AccountIdOf<ThisChain<B>>>);

/// Message verifier that requires submitter to pay minimal delivery and dispatch fee.
#[derive(RuntimeDebug)]
pub struct FromThisChainMessageVerifier<B>(PhantomData<B>);
Expand Down Expand Up @@ -188,6 +199,40 @@ pub mod source {
.and_then(|interest| fee.checked_add(&interest)))
.ok_or("Overflow when computing minimal required message delivery and dispatch fee")
}

/// Verify proof of This -> Bridged chain messages delivery.
pub fn verify_messages_delivery_proof<B: MessageBridge, ThisRuntime>(
proof: FromBridgedChainMessagesDeliveryProof<B>,
) -> Result<ParsedMessagesDeliveryProofFromBridgedChain<B>, &'static str>
where
ThisRuntime: pallet_substrate_bridge::Trait,
ThisRuntime: pallet_message_lane::Trait<MessageLaneInstanceOf<BridgedChain<B>>>,
HashOf<BridgedChain<B>>:
Into<bp_runtime::HashOf<<ThisRuntime as pallet_substrate_bridge::Trait>::BridgedChain>>,
{
let (bridged_header_hash, bridged_storage_proof, lane) = proof;
pallet_substrate_bridge::Module::<ThisRuntime>::parse_finalized_storage_proof(
bridged_header_hash.into(),
bridged_storage_proof,
|storage| {
// Messages delivery proof is just proof of single storage key read => any error
// is fatal.
let storage_inbound_lane_data_key = pallet_message_lane::storage_keys::inbound_lane_data_key::<
ThisRuntime,
MessageLaneInstanceOf<BridgedChain<B>>,
>(&lane);
let raw_inbound_lane_data = storage
.read_value(storage_inbound_lane_data_key.0.as_ref())
.map_err(|_| "Failed to read inbound lane state from storage proof")?
.ok_or("Inbound lane state is missing from the messages proof")?;
let inbound_lane_data = InboundLaneData::decode(&mut &raw_inbound_lane_data[..])
.map_err(|_| "Failed to decode inbound lane state from the proof")?;

Ok((lane, inbound_lane_data))
},
)
.map_err(<&'static str>::from)?
}
}

/// Sub-module that is declaring types required for processing Bridged -> This chain messages.
Expand Down

0 comments on commit b3f8b99

Please sign in to comment.