From 5ce1430ae08dc99b5341364818ce4b8723f08dd9 Mon Sep 17 00:00:00 2001 From: Luke Tchang Date: Tue, 11 May 2021 18:56:37 -0400 Subject: [PATCH] refactor: move destinationAndSequence to optics-core and run output in precommit (#326) * refactor: moves destinationAndSequence to optics-core and adds output function to precommit * fix: domainHash output function writes to homeDomainHashTestCases.json --- rust/optics-core/bin/utils_test_output.rs | 1 + rust/optics-core/src/lib.rs | 5 +-- rust/optics-core/src/test_output.rs | 33 ++++++++++++++++-- rust/optics-core/src/utils.rs | 10 +++++- rust/optics-ethereum/src/home.rs | 6 ++-- rust/optics-ethereum/src/lib.rs | 3 -- rust/optics-ethereum/src/utils.rs | 41 ----------------------- 7 files changed, 46 insertions(+), 53 deletions(-) delete mode 100644 rust/optics-ethereum/src/utils.rs diff --git a/rust/optics-core/bin/utils_test_output.rs b/rust/optics-core/bin/utils_test_output.rs index 4dabbad49..87583fe3c 100644 --- a/rust/optics-core/bin/utils_test_output.rs +++ b/rust/optics-core/bin/utils_test_output.rs @@ -5,5 +5,6 @@ fn main() { #[cfg(feature = "output")] { output_home_domain_hashes(); + output_destination_and_sequences(); } } diff --git a/rust/optics-core/src/lib.rs b/rust/optics-core/src/lib.rs index d88571355..7ab4af540 100644 --- a/rust/optics-core/src/lib.rs +++ b/rust/optics-core/src/lib.rs @@ -24,6 +24,9 @@ pub mod encode; /// 20-byte ids (e.g ethereum addresses) pub mod identifiers; +/// Utilities to match contract values +pub mod utils; + /// Testing utilities pub mod test_utils; @@ -31,8 +34,6 @@ pub mod test_utils; #[cfg(feature = "output")] pub mod test_output; -mod utils; - use std::convert::Infallible; pub use encode::{Decode, Encode}; diff --git a/rust/optics-core/src/test_output.rs b/rust/optics-core/src/test_output.rs index 8901579d0..cac2f842e 100644 --- a/rust/optics-core/src/test_output.rs +++ b/rust/optics-core/src/test_output.rs @@ -1,4 +1,7 @@ -use crate::{utils::home_domain_hash, FailureNotification, Update}; +use crate::{ + utils::{destination_and_sequence, home_domain_hash}, + FailureNotification, Update, +}; use ethers::core::types::H256; use serde_json::{json, Value}; @@ -26,7 +29,33 @@ pub mod output_functions { .write(true) .create(true) .truncate(true) - .open("../../vectors/domainHashTestCases.json") + .open("../../vectors/homeDomainHashTestCases.json") + .expect("Failed to open/create file"); + + file.write_all(json.as_bytes()) + .expect("Failed to write to file"); + } + + /// Outputs combined destination and sequence test cases in /vector/ + /// destinationSequenceTestCases.json + pub fn output_destination_and_sequences() { + let test_cases: Vec = (1..=5) + .map(|i| { + json!({ + "destination": i, + "sequence": i + 1, + "expectedDestinationAndSequence": destination_and_sequence(i, i + 1) + }) + }) + .collect(); + + let json = json!({ "testCases": test_cases }).to_string(); + + let mut file = OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open("../../vectors/destinationSequenceTestCases.json") .expect("Failed to open/create file"); file.write_all(json.as_bytes()) diff --git a/rust/optics-core/src/utils.rs b/rust/optics-core/src/utils.rs index 2bbcaf0ff..7e4092c3b 100644 --- a/rust/optics-core/src/utils.rs +++ b/rust/optics-core/src/utils.rs @@ -2,7 +2,7 @@ use ethers::core::types::H256; use sha3::{Digest, Keccak256}; /// Computes hash of home domain concatenated with "OPTICS" -pub(crate) fn home_domain_hash(home_domain: u32) -> H256 { +pub fn home_domain_hash(home_domain: u32) -> H256 { H256::from_slice( Keccak256::new() .chain(home_domain.to_be_bytes()) @@ -11,3 +11,11 @@ pub(crate) fn home_domain_hash(home_domain: u32) -> H256 { .as_slice(), ) } + +/// Destination and destination-specific sequence combined in single field ( +/// (destination << 32) & sequence) +pub fn destination_and_sequence(destination: u32, sequence: u32) -> u64 { + assert!(destination < u32::MAX); + assert!(sequence < u32::MAX); + ((destination as u64) << 32) | sequence as u64 +} diff --git a/rust/optics-ethereum/src/home.rs b/rust/optics-ethereum/src/home.rs index 0779b7980..b844a3a9b 100644 --- a/rust/optics-ethereum/src/home.rs +++ b/rust/optics-ethereum/src/home.rs @@ -5,11 +5,9 @@ use optics_core::{ traits::{ ChainCommunicationError, Common, DoubleUpdate, Home, RawCommittedMessage, State, TxOutcome, }, - Message, SignedUpdate, Update, + utils, Message, SignedUpdate, Update, }; -use crate::utils::*; - use std::{convert::TryFrom, error::Error as StdError, sync::Arc}; #[allow(missing_docs)] @@ -189,7 +187,7 @@ where destination: u32, sequence: u32, ) -> Result, ChainCommunicationError> { - let dest_and_seq = destination_and_sequence(destination, sequence); + let dest_and_seq = utils::destination_and_sequence(destination, sequence); let events = self .contract diff --git a/rust/optics-ethereum/src/lib.rs b/rust/optics-ethereum/src/lib.rs index 4662cefc4..05f7b3359 100644 --- a/rust/optics-ethereum/src/lib.rs +++ b/rust/optics-ethereum/src/lib.rs @@ -16,9 +16,6 @@ mod replica; #[cfg(not(doctest))] mod xapp; -/// Base trait for an agent -mod utils; - /// Configuration structs pub mod settings; diff --git a/rust/optics-ethereum/src/utils.rs b/rust/optics-ethereum/src/utils.rs deleted file mode 100644 index 23f5bb94f..000000000 --- a/rust/optics-ethereum/src/utils.rs +++ /dev/null @@ -1,41 +0,0 @@ -// Return destination and sequence -pub(crate) fn destination_and_sequence(destination: u32, sequence: u32) -> u64 { - assert!(destination < u32::MAX); - assert!(sequence < u32::MAX); - ((destination as u64) << 32) | sequence as u64 -} - -#[cfg(test)] -mod test { - use serde_json::{json, Value}; - - use super::*; - use std::{fs::OpenOptions, io::Write}; - - // Outputs combined destination and sequence test cases in /vector/ - // destinationSequenceTestCases.json - #[allow(dead_code)] - fn output_destination_and_sequences() { - let test_cases: Vec = (1..=5) - .map(|i| { - json!({ - "destination": i, - "sequence": i + 1, - "expectedDestinationAndSequence": destination_and_sequence(i, i + 1) - }) - }) - .collect(); - - let json = json!({ "testCases": test_cases }).to_string(); - - let mut file = OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open("../../vectors/destinationSequenceTestCases.json") - .expect("Failed to open/create file"); - - file.write_all(json.as_bytes()) - .expect("Failed to write to file"); - } -}