From fe00ced2f4de9aea0abd7a4ac220c07ac01e0563 Mon Sep 17 00:00:00 2001 From: Vincent Geddes Date: Thu, 14 Dec 2023 22:26:45 +0200 Subject: [PATCH] Add CustomDigestItem type for application-level digest items --- .../runtimes/bridge-hubs/common/Cargo.toml | 2 + .../bridge-hubs/common/src/digest_item.rs | 43 +++++++++++++++++++ .../runtimes/bridge-hubs/common/src/lib.rs | 2 + 3 files changed, 47 insertions(+) create mode 100644 cumulus/parachains/runtimes/bridge-hubs/common/src/digest_item.rs diff --git a/cumulus/parachains/runtimes/bridge-hubs/common/Cargo.toml b/cumulus/parachains/runtimes/bridge-hubs/common/Cargo.toml index 069ca1156075e..92d2653e7bc89 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/common/Cargo.toml +++ b/cumulus/parachains/runtimes/bridge-hubs/common/Cargo.toml @@ -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} @@ -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", diff --git a/cumulus/parachains/runtimes/bridge-hubs/common/src/digest_item.rs b/cumulus/parachains/runtimes/bridge-hubs/common/src/digest_item.rs new file mode 100644 index 0000000000000..8ea1bc05c0f25 --- /dev/null +++ b/cumulus/parachains/runtimes/bridge-hubs/common/src/digest_item.rs @@ -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 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()), + } + } +} diff --git a/cumulus/parachains/runtimes/bridge-hubs/common/src/lib.rs b/cumulus/parachains/runtimes/bridge-hubs/common/src/lib.rs index eb02498448a16..aac6eb036526a 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/common/src/lib.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/common/src/lib.rs @@ -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};