Skip to content

Commit

Permalink
Add CustomDigestItem type for application-level digest items
Browse files Browse the repository at this point in the history
  • Loading branch information
vgeddes committed Dec 14, 2023
1 parent 69cf0b1 commit fe00ced
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cumulus/parachains/runtimes/bridge-hubs/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ codec = { package = "parity-scale-codec", version = "3.0.0", default-features =
scale-info = { version = "2.10.0", default-features = false, features = ["derive"] }
frame-support = { path = "../../../../../substrate/frame/support", default-features = false }
sp-std = { path = "../../../../../substrate/primitives/std", default-features = false }
sp-core = { path = "../../../../../substrate/primitives/core", default-features = false }
sp-runtime = { path = "../../../../../substrate/primitives/runtime", default-features = false }
cumulus-primitives-core = { path = "../../../../primitives/core", default-features = false }
xcm = { package = "staging-xcm", path = "../../../../../polkadot/xcm", default-features = false}
Expand All @@ -24,6 +25,7 @@ std = [
"scale-info/std",
"frame-support/std",
"sp-std/std",
"sp-core/std",
"sp-runtime/std",
"cumulus-primitives-core/std",
"xcm/std",
Expand Down
43 changes: 43 additions & 0 deletions cumulus/parachains/runtimes/bridge-hubs/common/src/digest_item.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Custom digest items

use codec::{Decode, Encode};
use sp_core::{RuntimeDebug, H256};
use sp_runtime::generic::DigestItem;

/// Custom header digest items, inserted as DigestItem::Other
#[derive(Encode, Decode, Copy, Clone, Eq, PartialEq, RuntimeDebug)]
pub enum CustomDigestItem {
/// Merkle root of outbound Snowbridge messages.
Snowbridge(H256),
}

/// Convert custom application digest item into a concrete digest item
impl Into<DigestItem> for CustomDigestItem {
fn into(self) -> DigestItem {
match self {
// For snowbridge, we sidestep SCALE-encoding of `CustomDigestItem`, and insert the
// merkle root directly into the DigestItem::Other payload. This reduces complexity
// and gas costs on the Ethereum side.
//
// Other light clients can discriminate between custom digest items by checking the
// length of the encoded payload. If the length is greater than 32, then its a digest
// item inserted by some application other than Snowbridge.
CustomDigestItem::Snowbridge(merkle_root) =>
DigestItem::Other(merkle_root.to_fixed_bytes().into()),
}
}
}
2 changes: 2 additions & 0 deletions cumulus/parachains/runtimes/bridge-hubs/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// limitations under the License.
#![cfg_attr(not(feature = "std"), no_std)]

pub mod digest_item;
pub mod message_queue;

pub use digest_item::CustomDigestItem;
pub use message_queue::{AggregateMessageOrigin, BridgeHubMessageRouter};

0 comments on commit fe00ced

Please sign in to comment.