The home for Hyperlane core contracts, sdk packages, and other infrastructure
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hyperlane-monorepo/rust/chains/hyperlane-ethereum/tests/signer_output.rs

179 lines
5.3 KiB

//! Test functions that output json files
#![cfg(test)]
use std::{fs::OpenOptions, io::Write, str::FromStr};
use hex::FromHex;
use serde_json::{json, Value};
use ethers::signers::Signer;
use hyperlane_core::{
accumulator::{
merkle::{merkle_root_from_branch, MerkleTree},
TREE_DEPTH,
},
test_utils,
utils::domain_hash,
Checkpoint, HyperlaneMessage, HyperlaneSignerExt, H160, H256,
};
use hyperlane_ethereum::Signers;
/// Output proof to /vector/message.json
#[test]
pub fn output_message() {
let hyperlane_message = HyperlaneMessage {
nonce: 0,
version: 0,
origin: 1000,
sender: H256::from(H160::from_str("0x1111111111111111111111111111111111111111").unwrap()),
destination: 2000,
recipient: H256::from(
H160::from_str("0x2222222222222222222222222222222222222222").unwrap(),
),
body: Vec::from_hex("1234").unwrap(),
};
let message_json = json!({
"nonce": hyperlane_message.nonce,
"version": hyperlane_message.version,
"origin": hyperlane_message.origin,
"sender": hyperlane_message.sender,
"destination": hyperlane_message.destination,
"recipient": hyperlane_message.recipient,
"body": hyperlane_message.body,
"id": hyperlane_message.id(),
});
let json = json!([message_json]).to_string();
let mut file = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(test_utils::find_vector("message.json"))
.expect("Failed to open/create file");
file.write_all(json.as_bytes())
.expect("Failed to write to file");
}
/// Output merkle proof test vectors
#[test]
pub fn output_merkle_proof() {
let mut tree = MerkleTree::create(&[], TREE_DEPTH);
let index = 1;
// kludge. these are random message ids
tree.push_leaf(
"0xd89959d277019eee21f1c3c270a125964d63b71876880724d287fbb8b8de55f1"
.parse()
.unwrap(),
TREE_DEPTH,
)
.unwrap();
tree.push_leaf(
"0x5068ac60cb6f9c5202bbe8e7a1babdd972133ea3ad37d7e0e753c7e4ddd7ffbd"
.parse()
.unwrap(),
TREE_DEPTH,
)
.unwrap();
let proof = tree.generate_proof(index, TREE_DEPTH);
let proof_json = json!({ "leaf": proof.0, "path": proof.1, "index": index});
let json = json!({ "proof": proof_json, "root": merkle_root_from_branch(proof.0, &proof.1, 32, index)}).to_string();
let mut file = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(test_utils::find_vector("proof.json"))
.expect("Failed to open/create file");
file.write_all(json.as_bytes())
.expect("Failed to write to file");
}
/// Outputs domain hash test cases in /vector/domainHash.json
#[test]
pub fn output_domain_hashes() {
let mailbox = H256::from(H160::from_str("0x2222222222222222222222222222222222222222").unwrap());
let test_cases: Vec<Value> = (1..=3)
.map(|i| {
json!({
"domain": i,
"mailbox": mailbox,
"expectedDomainHash": domain_hash(mailbox, i as u32)
})
})
.collect();
let json = json!(test_cases).to_string();
let mut file = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(test_utils::find_vector("domainHash.json"))
.expect("Failed to open/create file");
file.write_all(json.as_bytes())
.expect("Failed to write to file");
}
/// Outputs signed checkpoint test cases in /vector/signedCheckpoint.json
#[test]
pub fn output_signed_checkpoints() {
V3 agents rebase (#2746) ### Description It's your favourite PR coming right back... V3 agents! Closes https://github.com/hyperlane-xyz/issues/issues/561 Builds on top of https://github.com/hyperlane-xyz/hyperlane-monorepo/pull/2742 Depends on https://github.com/hyperlane-xyz/hyperlane-monorepo/pull/2681 for e2e testing This PR includes: - [x] Merkle tree hook indexer - [x] Merkle tree builder task - [x] Update submitter to trigger retries if no proof is available yet Slightly more detailed overview of the work here: https://github.com/hyperlane-xyz/hyperlane-monorepo/issues/2720#issuecomment-1724038643 <!-- What's included in this PR? --> ### Drive-by changes <!-- Are there any minor or drive-by changes also included? --> ### Related issues <!-- - Fixes #[issue number here] --> ### Backward compatibility <!-- Are these changes backward compatible? Are there any infrastructure implications, e.g. changes that would prohibit deploying older commits using this infra tooling? Yes/No --> ### Testing <!-- What kind of testing have these changes undergone? None/Manual/Unit Tests --> --------- Co-authored-by: -f <kunalarora1729@gmail.com> Co-authored-by: Trevor Porter <trkporter@ucdavis.edu> Co-authored-by: Kunal Arora <55632507+aroralanuk@users.noreply.github.com> Co-authored-by: Mattie Conover <git@mconover.dev> Co-authored-by: Guillaume Bouvignies <guillaumebouvignies@gmail.com> Co-authored-by: Yorke Rhodes <yorke@hyperlane.xyz> Co-authored-by: Guillaume Bouvignies <guillaume.bouvignies@kurtosistech.com>
1 year ago
let merkle_tree_hook_address =
H256::from(H160::from_str("0x2222222222222222222222222222222222222222").unwrap());
let t = async {
let signer: Signers = "1111111111111111111111111111111111111111111111111111111111111111"
.parse::<ethers::signers::LocalWallet>()
.unwrap()
.into();
let mut test_cases: Vec<Value> = Vec::new();
// test suite
for i in 1..=3 {
let signed_checkpoint = signer
.sign(Checkpoint {
V3 agents rebase (#2746) ### Description It's your favourite PR coming right back... V3 agents! Closes https://github.com/hyperlane-xyz/issues/issues/561 Builds on top of https://github.com/hyperlane-xyz/hyperlane-monorepo/pull/2742 Depends on https://github.com/hyperlane-xyz/hyperlane-monorepo/pull/2681 for e2e testing This PR includes: - [x] Merkle tree hook indexer - [x] Merkle tree builder task - [x] Update submitter to trigger retries if no proof is available yet Slightly more detailed overview of the work here: https://github.com/hyperlane-xyz/hyperlane-monorepo/issues/2720#issuecomment-1724038643 <!-- What's included in this PR? --> ### Drive-by changes <!-- Are there any minor or drive-by changes also included? --> ### Related issues <!-- - Fixes #[issue number here] --> ### Backward compatibility <!-- Are these changes backward compatible? Are there any infrastructure implications, e.g. changes that would prohibit deploying older commits using this infra tooling? Yes/No --> ### Testing <!-- What kind of testing have these changes undergone? None/Manual/Unit Tests --> --------- Co-authored-by: -f <kunalarora1729@gmail.com> Co-authored-by: Trevor Porter <trkporter@ucdavis.edu> Co-authored-by: Kunal Arora <55632507+aroralanuk@users.noreply.github.com> Co-authored-by: Mattie Conover <git@mconover.dev> Co-authored-by: Guillaume Bouvignies <guillaumebouvignies@gmail.com> Co-authored-by: Yorke Rhodes <yorke@hyperlane.xyz> Co-authored-by: Guillaume Bouvignies <guillaume.bouvignies@kurtosistech.com>
1 year ago
merkle_tree_hook_address,
mailbox_domain: 1000,
root: H256::repeat_byte(i + 1),
index: i as u32,
})
.await
.expect("!sign_with");
test_cases.push(json!({
V3 agents rebase (#2746) ### Description It's your favourite PR coming right back... V3 agents! Closes https://github.com/hyperlane-xyz/issues/issues/561 Builds on top of https://github.com/hyperlane-xyz/hyperlane-monorepo/pull/2742 Depends on https://github.com/hyperlane-xyz/hyperlane-monorepo/pull/2681 for e2e testing This PR includes: - [x] Merkle tree hook indexer - [x] Merkle tree builder task - [x] Update submitter to trigger retries if no proof is available yet Slightly more detailed overview of the work here: https://github.com/hyperlane-xyz/hyperlane-monorepo/issues/2720#issuecomment-1724038643 <!-- What's included in this PR? --> ### Drive-by changes <!-- Are there any minor or drive-by changes also included? --> ### Related issues <!-- - Fixes #[issue number here] --> ### Backward compatibility <!-- Are these changes backward compatible? Are there any infrastructure implications, e.g. changes that would prohibit deploying older commits using this infra tooling? Yes/No --> ### Testing <!-- What kind of testing have these changes undergone? None/Manual/Unit Tests --> --------- Co-authored-by: -f <kunalarora1729@gmail.com> Co-authored-by: Trevor Porter <trkporter@ucdavis.edu> Co-authored-by: Kunal Arora <55632507+aroralanuk@users.noreply.github.com> Co-authored-by: Mattie Conover <git@mconover.dev> Co-authored-by: Guillaume Bouvignies <guillaumebouvignies@gmail.com> Co-authored-by: Yorke Rhodes <yorke@hyperlane.xyz> Co-authored-by: Guillaume Bouvignies <guillaume.bouvignies@kurtosistech.com>
1 year ago
"merkle_tree_hook": signed_checkpoint.value.merkle_tree_hook_address,
"domain": signed_checkpoint.value.mailbox_domain,
"root": signed_checkpoint.value.root,
"index": signed_checkpoint.value.index,
"signature": signed_checkpoint.signature,
"signer": signer.address(),
}))
}
let json = json!(test_cases).to_string();
let mut file = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(test_utils::find_vector("signedCheckpoint.json"))
.expect("Failed to open/create file");
file.write_all(json.as_bytes())
.expect("Failed to write to file");
};
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(t)
}