Skip to content

Commit

Permalink
feat(eos-on-int): <- adds test for prev block submissions
Browse files Browse the repository at this point in the history
  • Loading branch information
gskapka committed Jan 31, 2024
1 parent a6a4879 commit 8939566
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 4 deletions.
105 changes: 103 additions & 2 deletions v2_bridges/eos_on_int/src/eos/submit_eos_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,14 @@ pub fn submit_eos_block_to_core<D: DatabaseInterface>(db: &D, block_json: &str)
.and_then(get_eos_output)
}

#[cfg(all(test, feature = "non-validating"))] // NOTE: The test uses TELOS blocks, whose headers fail validation.
// NOTE: The test uses TELOS blocks, whose headers fail validation.
#[cfg(all(test, feature = "non-validating"))]
mod tests {
use std::str::FromStr;

use common::test_utils::get_test_database;
use common_chain_ids::EthChainId;
use common_eos::{initialize_eos_core_inner, EosPrivateKey, ProcessedGlobalSequences};
use common_eos::{initialize_eos_core_inner, EosPrivateKey, EosSubmissionMaterial, ProcessedGlobalSequences};
use common_eth::{
initialize_eth_core_with_router_contract_and_return_state,
EthDbUtils,
Expand All @@ -101,6 +102,12 @@ mod tests {
get_sample_int_address,
get_sample_int_private_key,
get_sample_router_address,
multi_incremerkle_submission::{
get_incremekle_update_block,
get_init_block,
get_sample_dictionary as get_sample_dictionary_for_incremerkle_test,
get_submission_block,
},
},
};

Expand Down Expand Up @@ -226,4 +233,98 @@ mod tests {
let processed_glob_sequences_after = ProcessedGlobalSequences::get_from_db(&db).unwrap();
assert!(processed_glob_sequences_after.contains(&9854285413));
}

#[test]
fn should_submit_eos_material_with_proof_tied_to_block_behind_chain_tip() {
let db = get_test_database();
let router_address = get_sample_router_address();

// NOTE: Initialize the EOS mainnet core...
let eos_chain_id = "aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906";
let maybe_eos_account_name = Some("x.ptokens");
let maybe_eos_token_symbol = None;
let eos_init_block = get_init_block();
let eos_init_block_num = 351734756;
initialize_eos_core_inner(
&db,
eos_chain_id,
maybe_eos_account_name,
maybe_eos_token_symbol,
&eos_init_block,
true,
)
.unwrap();

// NOTE: Overwrite the EOS private key since it's generated randomly above...
let eos_pk = get_sample_eos_private_key();
eos_pk.write_to_db(&db).unwrap();
assert_eq!(EosPrivateKey::get_from_db(&db).unwrap(), eos_pk);

// NOTE: Initialize the INT side of the core...
let int_confirmations = 0;
let int_gas_price = 20_000_000_000;
let contiguous_int_block_json_strs = get_contiguous_int_block_json_strs();
let int_init_block = contiguous_int_block_json_strs[0].clone();
initialize_eth_core_with_router_contract_and_return_state(
&int_init_block,
&EthChainId::Ropsten,
int_gas_price,
int_confirmations,
IntState::init(&db),
&router_address,
false,
)
.unwrap();

// NOTE: Overwrite the INT address & private key since it's generated randomly above...
let int_address = get_sample_int_address();
let int_private_key = get_sample_int_private_key();
let int_db_utils = EthDbUtils::new(&db);
int_db_utils
.put_eth_address_in_db(&int_db_utils.get_eth_address_key(), &int_address)
.unwrap();
int_db_utils.put_eth_private_key_in_db(&int_private_key).unwrap();
assert_eq!(int_db_utils.get_public_eth_address_from_db().unwrap(), int_address);
assert_eq!(int_db_utils.get_eth_private_key_from_db().unwrap(), int_private_key);

// NOTE: Add the token dictionary to the db...
let dictionary = get_sample_dictionary_for_incremerkle_test();
dictionary.save_to_db(&db).unwrap();

// NOTE: Assert that there are no processed global sequences in the db...
let processed_glob_sequences_before = ProcessedGlobalSequences::get_from_db(&db).unwrap();
assert!(processed_glob_sequences_before.is_empty());

let mut incremerkles = Incremerkles::get_from_db(&common_eos::EosDbUtils::new(&db)).unwrap();
assert_eq!(incremerkles.len(), 1);
assert_eq!(incremerkles.latest_block_num(), eos_init_block_num);

// NOTE: Now lets update the incremerkle...
let incremerkle_update_block = get_incremekle_update_block();
let incremerkle_update_block_num = EosSubmissionMaterial::from_str(&incremerkle_update_block)
.unwrap()
.block_num;
submit_eos_block_to_core(&db, &incremerkle_update_block).unwrap();

incremerkles = Incremerkles::get_from_db(&common_eos::EosDbUtils::new(&db)).unwrap();
assert_eq!(incremerkles.len(), 2);
assert_eq!(incremerkles.latest_block_num(), incremerkle_update_block_num);

let submission_block_json = get_submission_block();
let submission_block_num = EosSubmissionMaterial::from_str(&submission_block_json)
.unwrap()
.block_num;
assert_eq!(submission_block_num, 351734979);

// NOTE: Let's assert that the core's latest block number is indeed _past_ the submission
// material block num
let latest_block_num = incremerkles.latest_block_num();
assert_eq!(latest_block_num, 351735116);
assert!(latest_block_num > submission_block_num);

// NOTE: Now we can submit the block with the peg in in it...
let output = EosOutput::from_str(&submit_eos_block_to_core(&db, &submission_block_json).unwrap()).unwrap();
// NOTE: Asserting a tx is outputted successfully is sufficient for this test.
assert_eq!(output.int_signed_transactions.len(), 1);
}
}
12 changes: 10 additions & 2 deletions v2_bridges/eos_on_int/src/test_utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
#![cfg(all(test, feature = "non-validating"))] // NOTE: The test uses TELOS blocks, whose headers fail validation.
#![cfg(test)]
// NOTE: Some tests use telos blocks which require `non-validating` feature flag.
// Helpers for those tests would flag up in the linter as unused if the feature is
// not used. This solves that.
#![allow(unused)]

#[cfg(feature = "non-validating")]
pub(crate) mod multi_incremerkle_submission;

use std::fs::read_to_string;

use common::{
Expand All @@ -15,6 +23,7 @@ macro_rules! write_int_paths_and_getter_fxn {
paste! {
$(const [<SAMPLE_INT_BLOCK_ $num>]: &str = $path;)*

//#[cfg(feature = "non-validating")] // NOTE: The test uses TELOS blocks, whose headers fail validation.
fn get_int_path_n(n: usize) -> Result<String> {
match n {
$($num => Ok([<SAMPLE_INT_BLOCK_ $num>].to_string()),)*
Expand Down Expand Up @@ -96,7 +105,6 @@ pub fn get_sample_int_address() -> EthAddress {
convert_hex_to_eth_address("0x49B9d619E3402de8867A8113C7bc204653F5DB4c").unwrap()
}

#[cfg(feature = "non-validating")]
pub fn get_sample_eos_submission_material_string() -> String {
read_to_string(get_eos_path_n(1).unwrap()).unwrap()
}

Large diffs are not rendered by default.

Loading

0 comments on commit 8939566

Please sign in to comment.