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
buddies-main-deployment
Luke Tchang 4 years ago committed by GitHub
parent befde049a6
commit 5ce1430ae0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      rust/optics-core/bin/utils_test_output.rs
  2. 5
      rust/optics-core/src/lib.rs
  3. 33
      rust/optics-core/src/test_output.rs
  4. 10
      rust/optics-core/src/utils.rs
  5. 6
      rust/optics-ethereum/src/home.rs
  6. 3
      rust/optics-ethereum/src/lib.rs
  7. 41
      rust/optics-ethereum/src/utils.rs

@ -5,5 +5,6 @@ fn main() {
#[cfg(feature = "output")]
{
output_home_domain_hashes();
output_destination_and_sequences();
}
}

@ -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};

@ -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<Value> = (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())

@ -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
}

@ -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<Option<RawCommittedMessage>, ChainCommunicationError> {
let dest_and_seq = destination_and_sequence(destination, sequence);
let dest_and_seq = utils::destination_and_sequence(destination, sequence);
let events = self
.contract

@ -16,9 +16,6 @@ mod replica;
#[cfg(not(doctest))]
mod xapp;
/// Base trait for an agent
mod utils;
/// Configuration structs
pub mod settings;

@ -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<Value> = (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");
}
}
Loading…
Cancel
Save