Skip to content

Commit

Permalink
Add CustomDigestItem type (#1053)
Browse files Browse the repository at this point in the history
Co-authored-by: ron <[email protected]>
  • Loading branch information
vgeddes and yrong authored Dec 16, 2023
1 parent 09a9253 commit 4c175e4
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 14 deletions.
10 changes: 8 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 Enum variant ID for CustomDigestItem::Snowbridge
bytes1 public constant DIGEST_ITEM_OTHER_SNOWBRIDGE = 0x00;

/// @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 @@ -132,8 +135,11 @@ library Verification {
returns (bool)
{
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)) {
if (
header.digestItems[i].kind == DIGEST_ITEM_OTHER && header.digestItems[i].data.length == 33
&& header.digestItems[i].data[0] == DIGEST_ITEM_OTHER_SNOWBRIDGE
&& commitment == bytes32(header.digestItems[i].data[1:])
) {
return true;
}
}
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 @@ -237,3 +237,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

0 comments on commit 4c175e4

Please sign in to comment.