Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CustomDigestItem type #1053

Merged
merged 10 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions contracts/src/Verification.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ library Verification {
uint256 public constant DIGEST_ITEM_PRERUNTIME = 6;
uint256 public constant DIGEST_ITEM_RUNTIME_ENVIRONMENT_UPDATED = 8;

/// @dev Prefix of enum variants of DigestItem(0 is reserved for snowbridge)
bytes1 public constant DIGEST_ITEM_PREFIX = 0;

/// @dev Verify the message commitment by applying several proofs
///
/// 1. First check that the commitment is included in the digest items of the parachain header
Expand Down Expand Up @@ -133,8 +136,17 @@ library Verification {
{
for (uint256 i = 0; i < header.digestItems.length; i++) {
DigestItem memory item = header.digestItems[i];
if (item.kind == DIGEST_ITEM_OTHER && item.data.length == 32 && commitment == bytes32(item.data)) {
return true;
if (item.kind == DIGEST_ITEM_OTHER && item.data.length == 33 && item.data[0] == DIGEST_ITEM_PREFIX) {
bytes memory commitment_in_header = new bytes(32);
for (uint256 j = 1; j < 33;) {
commitment_in_header[j - 1] = item.data[j];
unchecked {
++j;
}
}
if (commitment == bytes32(commitment_in_header)) {
return true;
}
}
}
return false;
Expand Down
2 changes: 1 addition & 1 deletion contracts/test/Verification.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ contract VerificationTest is Test {
digestItems[2] = Verification.DigestItem({
kind: 0,
consensusEngineID: 0x00000000,
data: hex"b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c"
data: hex"00b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c"
});
digestItems[3] = Verification.DigestItem({
kind: 5,
Expand Down
1 change: 1 addition & 0 deletions parachain/Cargo.lock

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

13 changes: 8 additions & 5 deletions parachain/pallets/outbound-queue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ mod mock;
#[cfg(test)]
mod test;

use bridge_hub_common::AggregateMessageOrigin;
use bridge_hub_common::{AggregateMessageOrigin, CustomDigestItem};
use codec::Decode;
use frame_support::{
storage::StorageStreamIter,
Expand All @@ -104,7 +104,10 @@ use snowbridge_core::{
use snowbridge_outbound_queue_merkle_tree::merkle_root;
pub use snowbridge_outbound_queue_merkle_tree::MerkleProof;
use sp_core::{H256, U256};
use sp_runtime::traits::{CheckedDiv, Hash};
use sp_runtime::{
traits::{CheckedDiv, Hash},
DigestItem,
};
use sp_std::prelude::*;
pub use types::{CommittedMessage, FeeConfigRecord, ProcessMessageOriginOf};
pub use weights::WeightInfo;
Expand Down Expand Up @@ -277,10 +280,10 @@ pub mod pallet {
// Create merkle root of messages
let root = merkle_root::<<T as Config>::Hashing, _>(MessageLeaves::<T>::stream_iter());

let digest_item: DigestItem = CustomDigestItem::Snowbridge(root).into();

// Insert merkle root into the header digest
<frame_system::Pallet<T>>::deposit_log(sp_runtime::DigestItem::Other(
root.to_fixed_bytes().into(),
));
<frame_system::Pallet<T>>::deposit_log(digest_item);

Self::deposit_event(Event::MessagesCommitted { root, count });
}
Expand Down
12 changes: 12 additions & 0 deletions parachain/pallets/outbound-queue/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,15 @@ fn convert_local_currency() {
assert_eq!(fee, fee2);
});
}

#[test]
fn encode_digest_item() {
new_tester().execute_with(|| {
let digest_item: DigestItem = CustomDigestItem::Snowbridge(H256::default()).into();
let enum_prefix = match digest_item {
DigestItem::Other(data) => data[0],
_ => u8::MAX,
};
assert_eq!(enum_prefix, 0);
});
}
14 changes: 9 additions & 5 deletions relayer/relays/parachain/digest_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import (
func ExtractCommitmentFromDigest(digest types.Digest) (*types.H256, error) {
for _, digestItem := range digest {
if digestItem.IsOther {
var commitment types.H256
err := types.DecodeFromBytes(digestItem.AsOther, &commitment)
if err != nil {
return nil, err
digestItemRawBytes := digestItem.AsOther
// Prefix 0 reserved for snowbridge
if digestItemRawBytes[0] == 0 {
var commitment types.H256
err := types.DecodeFromBytes(digestItemRawBytes[1:], &commitment)
if err != nil {
return nil, err
}
return &commitment, nil
}
return &commitment, nil
}
}
return nil, nil
Expand Down
Loading