Skip to content

Commit

Permalink
Merge branch 'v3' into nambrot/repro-only-merkle-tree
Browse files Browse the repository at this point in the history
  • Loading branch information
aroralanuk committed Nov 2, 2023
2 parents 9751ce5 + c44fd29 commit b5d05cb
Show file tree
Hide file tree
Showing 107 changed files with 5,722 additions and 2,870 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ jobs:
# needs: [yarn-build]
# strategy:
# matrix:
# environment: [testnet4, mainnet2]
# environment: [testnet4, mainnet3]
# module: [ism, core, igp, ica, helloworld]

# steps:
Expand Down
6 changes: 1 addition & 5 deletions rust/agents/relayer/src/msg/metadata/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ use tracing::{debug, info, instrument, warn};
use crate::{
merkle_tree::builder::MerkleTreeBuilder,
msg::metadata::{
multisig::{
LegacyMultisigMetadataBuilder, MerkleRootMultisigMetadataBuilder,
MessageIdMultisigMetadataBuilder,
},
multisig::{MerkleRootMultisigMetadataBuilder, MessageIdMultisigMetadataBuilder},
AggregationIsmMetadataBuilder, CcipReadIsmMetadataBuilder, NullMetadataBuilder,
RoutingIsmMetadataBuilder,
},
Expand Down Expand Up @@ -87,7 +84,6 @@ impl MetadataBuilder for BaseMetadataBuilder {
let base = self.clone_with_incremented_depth()?;

let metadata_builder: Box<dyn MetadataBuilder> = match module_type {
ModuleType::LegacyMultisig => Box::new(LegacyMultisigMetadataBuilder::new(base)),
ModuleType::MerkleRootMultisig => {
Box::new(MerkleRootMultisigMetadataBuilder::new(base))
}
Expand Down
87 changes: 19 additions & 68 deletions rust/agents/relayer/src/msg/metadata/multisig/base.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
use std::collections::HashMap;
use std::fmt::Debug;

use async_trait::async_trait;
use derive_more::{AsRef, Deref};
use derive_new::new;
use ethers::abi::Token;

use eyre::{Context, Result};
use hyperlane_base::MultisigCheckpointSyncer;
use hyperlane_core::accumulator::merkle::Proof;
use hyperlane_core::{Checkpoint, HyperlaneMessage, SignatureWithSigner, H256};
use hyperlane_core::{HyperlaneMessage, MultisigSignedCheckpoint, H256};
use strum::Display;
use tracing::{debug, info};

use crate::msg::metadata::BaseMetadataBuilder;
use crate::msg::metadata::MetadataBuilder;

#[derive(new)]
#[derive(new, AsRef, Deref)]
pub struct MultisigMetadata {
checkpoint: Checkpoint,
signatures: Vec<SignatureWithSigner>,
merkle_leaf_index: Option<u32>,
message_id: Option<H256>,
#[deref]
quorum_checkpoint: MultisigSignedCheckpoint,
merkle_leaf_index: u32,
// optional because it's only used for MerkleRootMultisig
proof: Option<Proof>,
}

Expand All @@ -32,9 +32,7 @@ pub enum MetadataToken {
MessageId,
MerkleProof,
MessageMerkleLeafIndex,
Threshold,
Signatures,
Validators,
}

#[async_trait]
Expand All @@ -49,22 +47,15 @@ pub trait MultisigIsmMetadataBuilder: AsRef<BaseMetadataBuilder> + Send + Sync {

fn token_layout(&self) -> Vec<MetadataToken>;

fn format_metadata(
&self,
validators: &[H256],
threshold: u8,
metadata: MultisigMetadata,
) -> Result<Vec<u8>> {
fn format_metadata(&self, metadata: MultisigMetadata) -> Result<Vec<u8>> {
let build_token = |token: &MetadataToken| -> Result<Vec<u8>> {
match token {
MetadataToken::CheckpointMerkleRoot => {
Ok(metadata.checkpoint.root.to_fixed_bytes().into())
}
MetadataToken::MessageMerkleLeafIndex => Ok(metadata
.merkle_leaf_index
.ok_or(eyre::eyre!("Failed to fetch metadata"))?
.to_be_bytes()
.into()),
MetadataToken::MessageMerkleLeafIndex => {
Ok(metadata.merkle_leaf_index.to_be_bytes().into())
}
MetadataToken::CheckpointIndex => {
Ok(metadata.checkpoint.index.to_be_bytes().into())
}
Expand All @@ -74,9 +65,8 @@ pub trait MultisigIsmMetadataBuilder: AsRef<BaseMetadataBuilder> + Send + Sync {
.to_fixed_bytes()
.into()),
MetadataToken::MessageId => {
Ok(metadata.message_id.unwrap().to_fixed_bytes().into())
Ok(metadata.checkpoint.message_id.to_fixed_bytes().into())
}
MetadataToken::Threshold => Ok(Vec::from([threshold])),
MetadataToken::MerkleProof => {
let proof_tokens: Vec<Token> = metadata
.proof
Expand All @@ -87,18 +77,12 @@ pub trait MultisigIsmMetadataBuilder: AsRef<BaseMetadataBuilder> + Send + Sync {
.collect();
Ok(ethers::abi::encode(&proof_tokens))
}
MetadataToken::Validators => {
let validator_tokens: Vec<Token> = validators
.iter()
.map(|x| Token::FixedBytes(x.to_fixed_bytes().into()))
.collect();
Ok(ethers::abi::encode(&[Token::FixedArray(validator_tokens)]))
}
MetadataToken::Signatures => {
let ordered_signatures = order_signatures(validators, &metadata.signatures);
let threshold_signatures = &ordered_signatures[..threshold as usize];
Ok(threshold_signatures.concat())
}
MetadataToken::Signatures => Ok(metadata
.signatures
.iter()
.map(|x| x.to_vec())
.collect::<Vec<_>>()
.concat()),
}
};
let metas: Result<Vec<Vec<u8>>> = self.token_layout().iter().map(build_token).collect();
Expand Down Expand Up @@ -143,11 +127,7 @@ impl<T: MultisigIsmMetadataBuilder> MetadataBuilder for T {
.context(CTX)?
{
debug!(?message, ?metadata.checkpoint, "Found checkpoint with quorum");
Ok(Some(self.format_metadata(
&validators,
threshold,
metadata,
)?))
Ok(Some(self.format_metadata(metadata)?))
} else {
println!(
"pacquiao: validator, threshold, message_id: {:?}, {:?}, {:?}",
Expand All @@ -163,32 +143,3 @@ impl<T: MultisigIsmMetadataBuilder> MetadataBuilder for T {
}
}
}

/// Orders `signatures` by the signers according to the `desired_order`.
/// Returns a Vec of the signature raw bytes in the correct order.
/// Panics if any signers in `signatures` are not present in `desired_order`
fn order_signatures(desired_order: &[H256], signatures: &[SignatureWithSigner]) -> Vec<Vec<u8>> {
// Signer address => index to sort by
let ordering_map: HashMap<H256, usize> = desired_order
.iter()
.enumerate()
.map(|(index, a)| (*a, index))
.collect();

// Create a tuple of (SignatureWithSigner, index to sort by)
let mut ordered_signatures = signatures
.iter()
.cloned()
.map(|s| {
let order_index = ordering_map.get(&H256::from(s.signer)).unwrap();
(s, *order_index)
})
.collect::<Vec<_>>();
// Sort by the index
ordered_signatures.sort_by_key(|s| s.1);
// Now collect only the raw signature bytes
ordered_signatures
.into_iter()
.map(|s| s.0.signature.to_vec())
.collect()
}
67 changes: 0 additions & 67 deletions rust/agents/relayer/src/msg/metadata/multisig/legacy_multisig.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,8 @@ impl MultisigIsmMetadataBuilder for MerkleRootMultisigMetadataBuilder {
debug!(leaf_index, checkpoint=?quorum_checkpoint, "Couldn't get proof")
);
Ok(Some(MultisigMetadata::new(
quorum_checkpoint.checkpoint.checkpoint,
quorum_checkpoint.signatures,
Some(leaf_index),
Some(quorum_checkpoint.checkpoint.message_id),
quorum_checkpoint,
leaf_index,
Some(proof),
)))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl MultisigIsmMetadataBuilder for MessageIdMultisigMetadataBuilder {
vec![
MetadataToken::CheckpointMerkleTreeHook,
MetadataToken::CheckpointMerkleRoot,
MetadataToken::MessageMerkleLeafIndex,
MetadataToken::CheckpointIndex,
MetadataToken::Signatures,
]
}
Expand All @@ -34,10 +34,12 @@ impl MultisigIsmMetadataBuilder for MessageIdMultisigMetadataBuilder {
message: &HyperlaneMessage,
checkpoint_syncer: &MultisigCheckpointSyncer,
) -> Result<Option<MultisigMetadata>> {
let message_id = message.id();

const CTX: &str = "When fetching MessageIdMultisig metadata";
unwrap_or_none_result!(
leaf_index,
self.get_merkle_leaf_id_by_message_id(message.id())
self.get_merkle_leaf_id_by_message_id(message_id)
.await
.context(CTX)?,
debug!(
Expand All @@ -54,27 +56,23 @@ impl MultisigIsmMetadataBuilder for MessageIdMultisigMetadataBuilder {
trace!("No quorum checkpoint found")
);

if quorum_checkpoint.checkpoint.message_id != message.id() {
if quorum_checkpoint.checkpoint.message_id != message_id {
warn!(
"Quorum checkpoint message id {} does not match message id {}",
quorum_checkpoint.checkpoint.message_id,
message.id()
quorum_checkpoint.checkpoint.message_id, message_id
);
if quorum_checkpoint.checkpoint.index != leaf_index {
warn!(
"Quorum checkpoint index {} does not match leaf index {}",
quorum_checkpoint.checkpoint.index, leaf_index
);
}
return Ok(None);
}

unwrap_or_none_result!(
merkle_leaf_id,
self.get_merkle_leaf_id_by_message_id(message.id())
.await
.context(CTX)?
);

Ok(Some(MultisigMetadata::new(
quorum_checkpoint.checkpoint.checkpoint,
quorum_checkpoint.signatures,
Some(merkle_leaf_id),
None,
quorum_checkpoint,
leaf_index,
None,
)))
}
Expand Down
2 changes: 0 additions & 2 deletions rust/agents/relayer/src/msg/metadata/multisig/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
mod base;
mod legacy_multisig;
mod merkle_root_multisig;
mod message_id_multisig;

pub use base::{MetadataToken, MultisigIsmMetadataBuilder, MultisigMetadata};

pub use legacy_multisig::LegacyMultisigMetadataBuilder;
pub use merkle_root_multisig::MerkleRootMultisigMetadataBuilder;
pub use message_id_multisig::MessageIdMultisigMetadataBuilder;
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,30 @@ const DOMAINS: &[RawDomain] = &[
is_test_net: true,
is_deprecated: false,
},
RawDomain {
name: "polygonzkevm",
token: "ETH",
domain: 1101,
chain_id: 1101,
is_test_net: false,
is_deprecated: false,
},
RawDomain {
name: "base",
token: "ETH",
domain: 8453,
chain_id: 8453,
is_test_net: false,
is_deprecated: false,
},
RawDomain {
name: "scroll",
token: "ETH",
domain: 534352,
chain_id: 534352,
is_test_net: false,
is_deprecated: false,
},
RawDomain {
name: "test1",
token: "ETH",
Expand Down
Loading

0 comments on commit b5d05cb

Please sign in to comment.