Skip to content

Commit

Permalink
Fix prover tests
Browse files Browse the repository at this point in the history
  • Loading branch information
karim-en committed Nov 15, 2022
1 parent 49f7579 commit d654df3
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 285 deletions.
1 change: 1 addition & 0 deletions contracts/near/eth-client/src/data/12965004.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions contracts/near/eth-client/src/data/12965005.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion contracts/near/eth-prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ hex = "0.4.2"
indicatif = "0.14"
lazy_static = "*"
near-crypto = "0.14.0"
near-primitives = "0.14.0"
near-primitives = "0.15.0"
workspaces = "0.6.0"
tokio = { version = "1.18.1", features = ["full"] }

[features]
default = ["eip1559"]
Expand Down
4 changes: 2 additions & 2 deletions contracts/near/eth-prover/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use eth_types::*;
use near_plugins::{Ownable, Pausable};
use near_plugins_derive::pause;
use eth_types::*;
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::{env, ext_contract, near_bindgen, Gas, PanicOnDefault, PromiseOrValue};
use rlp::Rlp;

Expand Down
33 changes: 18 additions & 15 deletions contracts/near/eth-prover/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
mod tests {
use crate::EthProver;
use hex::{FromHex, ToHex};
use near_sdk::borsh;
use near_sdk::PromiseOrValue;
use rlp::RlpStream;
use serde::{Deserialize, Deserializer};
use near_sdk::borsh;

#[derive(Debug)]
struct Hex(pub Vec<u8>);
Expand All @@ -30,15 +30,14 @@ mod tests {

// TESTS

use near_sdk::MockedBlockchain;
use near_sdk::{testing_env, VMContext};

fn get_context(input: Vec<u8>, is_view: bool) -> VMContext {
fn get_context(input: Vec<u8>) -> VMContext {
VMContext {
current_account_id: "alice.near".to_string(),
signer_account_id: "bob.near".to_string(),
signer_account_pk: vec![0, 1, 2],
predecessor_account_id: "carol.near".to_string(),
current_account_id: "alice.near".parse().unwrap(),
signer_account_id: "bob.near".parse().unwrap(),
signer_account_pk: vec![0u8; 33].try_into().unwrap(),
predecessor_account_id: "alice.near".parse().unwrap(),
input,
block_index: 0,
block_timestamp: 0,
Expand All @@ -47,16 +46,17 @@ mod tests {
account_locked_balance: 0,
storage_usage: 0,
attached_deposit: 0,
prepaid_gas: 10u64.pow(18),
random_seed: vec![0, 1, 2],
is_view,
prepaid_gas: near_sdk::Gas(10u64.pow(18)),
random_seed: vec![1; 32].try_into().unwrap(),
view_config: None,
output_data_receivers: vec![],
}
}

#[test]
#[cfg_attr(feature = "eip1559", ignore)]
fn simple_tx_res() {
testing_env!(get_context(vec![], false));
testing_env!(get_context(vec![]));

let contract = EthProver::init("ethbridge".to_string());

Expand Down Expand Up @@ -97,8 +97,9 @@ mod tests {
}

#[test]
#[cfg_attr(feature = "eip1559", ignore)]
fn complex_tx_res() {
testing_env!(get_context(vec![], false));
testing_env!(get_context(vec![]));

let contract = EthProver::init("ethbridge".to_string());

Expand Down Expand Up @@ -215,6 +216,7 @@ mod tests {
}

#[test]
#[cfg_attr(feature = "eip1559", ignore)]
fn complex_test2() {
let log_index = 0;
let receipt_index = 1;
Expand Down Expand Up @@ -267,7 +269,7 @@ mod tests {
Vec::from_hex("f901a70183012d99b9010000000000000000000000000000000000000001000000000000000000000000000000000000000000000002000000010000000000000000000000000000000000000000000008000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000400000000000000000000000000000000000000000000000000100000000000000000000004000080000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f89df89b94dac17f958d2ee523a2206206994597c13d831ec7f863a0ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa0000000000000000000000000adb2b42f6bd96f5c65920b9ac88619dce4166f94a0000000000000000000000000658a36d8f840f73207af8df717d12046b2c75969a000000000000000000000000000000000000000000000000000000000042c1d80").unwrap(),
],
];
testing_env!(get_context(vec![], false));
testing_env!(get_context(vec![]));

let contract = EthProver::init("ethbridge".to_string());

Expand Down Expand Up @@ -298,6 +300,7 @@ mod tests {
}

#[test]
#[cfg_attr(feature = "eip1559", ignore)]
fn complex_test3() {
let log_index = 0;
let receipt_index = 0;
Expand Down Expand Up @@ -366,7 +369,7 @@ mod tests {
assert_eq!(borsh_proof, actual_borsh_proof);
assert_eq!(borsh_skip_bridge_call, actual_borsh_skip_bridge_call);

testing_env!(get_context(vec![], false));
testing_env!(get_context(vec![]));

let contract = EthProver::init("ethbridge".to_string());

Expand Down Expand Up @@ -460,7 +463,7 @@ mod tests {
#[test]
pub fn test_verify_proof() {
let vm_config = VMConfig::free();
testing_env!(get_context(vec![], false), vm_config, Default::default());
testing_env!(get_context(vec![]), vm_config);

let expected_value = "f902a60183af4adfb9010000000000000000000000000000000000000000000000000000000000000000000800010000000000000002000100000000000000000000000000000000000000000000000000000008000008000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000010000000040000000000000000000000000000000200000000010000000000000000000000000000000000200080000000202000000000000000000000000000004000000000000002000000000000000000000000000000000000080000000000000000000000000000000000000000000200000004000000000000000000000000000000f9019bf89b94a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48f863a0ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa0000000000000000000000000c22df065a81f6e0107e214991b9d7fb179d401b3a000000000000000000000000023ddd3e3692d1861ed57ede224608875809e127fa00000000000000000000000000000000000000000000000000000000005f5e100f8fc9423ddd3e3692d1861ed57ede224608875809e127ff863a0dd85dc56b5b4da387bf69c28ec19b1d66e793e0d51b567882fa31dc50bbd32c5a0000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48a0000000000000000000000000c22df065a81f6e0107e214991b9d7fb179d401b3b8800000000000000000000000000000000000000000000000000000000005f5e1000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000a6d616b6b652e6e65617200000000000000000000000000000000000000000000";
let expected_root = "73733f420161b4189ea48140489bfada55d485fd580ab7e6f0f4b6de229f5177";
Expand Down
3 changes: 2 additions & 1 deletion contracts/near/eth-prover/test.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env bash

RUST_BACKTRACE=1 cargo test --jobs 8 --package eth-prover -- --nocapture
cargo test --no-default-features --jobs 8 --package eth-prover
cargo test --jobs 8 --package eth-prover
139 changes: 66 additions & 73 deletions contracts/near/eth-prover/tests/spec.rs
Original file line number Diff line number Diff line change
@@ -1,109 +1,104 @@
mod utils;
use borsh::BorshSerialize;
use eth_types::*;
use near_primitives::transaction::ExecutionStatus;
use near_sdk::{testing_env, MockedBlockchain};
use near_sdk::borsh::BorshSerialize;
use near_sdk::testing_env;
use utils::{
get_context, new_root, ntoy, read_block, AddBlockHeaderArgs, AssertEthbridgeHashArgs,
ExternalUser, RuntimeStandalone,
get_context, init_eth_client, init_eth_prover, read_block, AddBlockHeaderArgs,
AssertEthbridgeHashArgs,
};
use workspaces::{Account, Contract};

fn setup_factory() -> (RuntimeStandalone, ExternalUser) {
let (mut r, near) = new_root("near".into());
near.init_eth_client(&mut r, "eth-client".to_string(), true)
.unwrap();
near.init_eth_prover(&mut r, "eth-prover".to_string(), "eth-client".to_string())
.unwrap();
(r, near)
async fn setup_factory() -> (Account, Contract, Contract) {
let worker = workspaces::sandbox().await.unwrap();
let client = init_eth_client(&worker, true).await;
let prover = init_eth_prover(&worker, client.id().clone()).await;
(worker.root_account().unwrap(), client, prover)
}

#[test]
fn block_hash_safe_from_eth_client() {
let (mut r, near) = setup_factory();
#[tokio::test]
#[cfg(feature = "eip1559")]
async fn block_hash_safe_from_eth_client() {
let (alice, client, prover) = setup_factory().await;

let safe_block = read_block("../eth-client/src/data/10234001.json".to_string());
let unsafe_block1 = read_block("../eth-client/src/data/10234002.json".to_string());
let unsafe_block2 = read_block("../eth-client/src/data/10234003.json".to_string());
for i in 10234002..10234012 {
let safe_block = read_block("../eth-client/src/data/12965000.json".to_string());
let unsafe_block1 = read_block("../eth-client/src/data/12965001.json".to_string());
let unsafe_block2 = read_block("../eth-client/src/data/12965002.json".to_string());
for i in 12965001..12965006 {
let block = read_block(format!("../eth-client/src/data/{}.json", i));
let add_block_header_args = AddBlockHeaderArgs {
block_header: block.header(),
dag_nodes: block.to_double_node_with_merkle_proof_vec(),
};
near.function_call(
&mut r,
"eth-client",
"add_block_header",
&add_block_header_args.try_to_vec().unwrap(),
ntoy(0),
)
.unwrap();

let _result = alice
.call(client.id(), "add_block_header")
.args(add_block_header_args.try_to_vec().unwrap())
.max_gas()
.transact()
.await
.unwrap();
}

testing_env!(get_context(vec![], false)); // For use rlp::decode, eth_types::near_keccak256
testing_env!(get_context(vec![])); // For use rlp::decode, eth_types::near_keccak256
let header: BlockHeader = rlp::decode(safe_block.header().as_slice()).unwrap();
let assert_ethclient_hash_args = AssertEthbridgeHashArgs {
block_number: header.number,
expected_block_hash: header.hash.unwrap(),
};
let res = near
.function_call(
&mut r,
"eth-prover",
"assert_ethclient_hash",
&assert_ethclient_hash_args.try_to_vec().unwrap(),
ntoy(0),
)

let result = alice
.call(prover.id(), "assert_ethclient_hash")
.args(assert_ethclient_hash_args.try_to_vec().unwrap())
.max_gas()
.transact()
.await
.unwrap();
assert_eq!(res.status, ExecutionStatus::SuccessValue(b"\x01".to_vec())); // true

assert_eq!(result.raw_bytes().unwrap(), b"\x01".to_vec()); // true

let header: BlockHeader = rlp::decode(unsafe_block1.header().as_slice()).unwrap();
let assert_ethclient_hash_args = AssertEthbridgeHashArgs {
block_number: header.number,
expected_block_hash: header.hash.unwrap(),
};
let res = near
.function_call(
&mut r,
"eth-prover",
"assert_ethclient_hash",
&assert_ethclient_hash_args.try_to_vec().unwrap(),
ntoy(0),
)

let result = alice
.call(prover.id(), "assert_ethclient_hash")
.args(assert_ethclient_hash_args.try_to_vec().unwrap())
.max_gas()
.transact()
.await
.unwrap();
assert_eq!(res.status, ExecutionStatus::SuccessValue(b"\x00".to_vec())); // false since block is not 10 block away
assert_eq!(result.raw_bytes().unwrap(), b"\x00".to_vec()); // false since block is not 10 block away

let header: BlockHeader = rlp::decode(unsafe_block2.header().as_slice()).unwrap();
let assert_ethclient_hash_args = AssertEthbridgeHashArgs {
block_number: header.number,
expected_block_hash: header.hash.unwrap(),
};
let res = near
.function_call(
&mut r,
"eth-prover",
"assert_ethclient_hash",
&assert_ethclient_hash_args.try_to_vec().unwrap(),
ntoy(0),
)

let result = alice
.call(prover.id(), "assert_ethclient_hash")
.args(assert_ethclient_hash_args.try_to_vec().unwrap())
.max_gas()
.transact()
.await
.unwrap();
assert_eq!(res.status, ExecutionStatus::SuccessValue(b"\x00".to_vec())); // false since block is not 10 block away
assert_eq!(result.raw_bytes().unwrap(), b"\x00".to_vec()); // false since block is not 10 block away

let header: BlockHeader = rlp::decode(safe_block.header().as_slice()).unwrap();
let assert_ethclient_hash_args = AssertEthbridgeHashArgs {
block_number: header.number - 1,
expected_block_hash: header.hash.unwrap(),
};
let res = near
.function_call(
&mut r,
"eth-prover",
"assert_ethclient_hash",
&assert_ethclient_hash_args.try_to_vec().unwrap(),
ntoy(0),
)
let result = alice
.call(prover.id(), "assert_ethclient_hash")
.args(assert_ethclient_hash_args.try_to_vec().unwrap())
.max_gas()
.transact()
.await
.unwrap();
assert_eq!(res.status, ExecutionStatus::SuccessValue(b"\x00".to_vec())); // false since block number is incorrect
assert_eq!(result.raw_bytes().unwrap(), b"\x00".to_vec()); // false since block number is incorrect

let header: BlockHeader = rlp::decode(safe_block.header().as_slice()).unwrap();
let block_number = header.number;
Expand All @@ -112,14 +107,12 @@ fn block_hash_safe_from_eth_client() {
block_number: block_number,
expected_block_hash: header.hash.unwrap(),
};
let res = near
.function_call(
&mut r,
"eth-prover",
"assert_ethclient_hash",
&assert_ethclient_hash_args.try_to_vec().unwrap(),
ntoy(0),
)
let result = alice
.call(prover.id(), "assert_ethclient_hash")
.args(assert_ethclient_hash_args.try_to_vec().unwrap())
.max_gas()
.transact()
.await
.unwrap();
assert_eq!(res.status, ExecutionStatus::SuccessValue(b"\x00".to_vec())); // false since block hash is incorrect
assert_eq!(result.raw_bytes().unwrap(), b"\x00".to_vec()); // false since block hash is incorrect
}
Loading

0 comments on commit d654df3

Please sign in to comment.