Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(watchtower): move watchtower tests to new testnet #1846

Merged
merged 25 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
cc220f1
move watchtower tests to the new testnet
caglaryucekaya May 29, 2023
b1e666d
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into mo…
caglaryucekaya May 29, 2023
6200c63
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into mo…
caglaryucekaya May 30, 2023
60fb1be
increase timeout for some watchtower tests
caglaryucekaya May 30, 2023
dcf9191
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into mo…
caglaryucekaya Jun 8, 2023
94d6867
fix test_withdraw_and_send test
caglaryucekaya Jun 9, 2023
abcb7b4
reduce timeouts for watchtower tests
caglaryucekaya Jun 9, 2023
f3ad1fd
add debug log to ethereum transport layer
caglaryucekaya Jun 9, 2023
a604432
fix formatting
caglaryucekaya Jun 9, 2023
98e05bb
increase sleep duration for test_withdraw_and_send
caglaryucekaya Jun 10, 2023
d6cfb62
add test for watcher rewards
caglaryucekaya Jun 10, 2023
48f2d7b
use random addresses for send_and_refund_eth_payment and send_and_ref…
caglaryucekaya Jun 11, 2023
0e43982
use random address for test_nonce_lock
caglaryucekaya Jun 11, 2023
ad33e98
fix the usage of assert statements in test_watcher_reward
caglaryucekaya Jun 11, 2023
0329d1c
use static distributor for eth and jst in eth_tests
caglaryucekaya Jun 11, 2023
d5cf22a
fix jst distributor
caglaryucekaya Jun 11, 2023
64772aa
use ALICE_PASSPHRASE and BOB_PASSPHRASE for eth and jst distributors
caglaryucekaya Jun 11, 2023
aeec737
increase sleep duration in test_withdraw_and_send
caglaryucekaya Jun 11, 2023
0307dd5
increase sleep duration for send_and_refund_erc20_payment test
caglaryucekaya Jun 12, 2023
42ff05d
wait randomly before certain tests
caglaryucekaya Jun 12, 2023
e1eba31
fix formatting
caglaryucekaya Jun 12, 2023
0a1b360
remove random waiting before tests
caglaryucekaya Jun 12, 2023
0014888
Merge branch 'dev' of github.com:KomodoPlatform/atomicDEX-API into mo…
caglaryucekaya Jun 12, 2023
375aad6
remove logging in ethereum transport layer
caglaryucekaya Jun 12, 2023
e04fb10
add log to test_withdraw_and_send
caglaryucekaya Jun 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions mm2src/coins/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1390,7 +1390,7 @@ impl SwapOps for EthCoin {
}

fn is_supported_by_watchers(&self) -> bool {
false
std::env::var("USE_WATCHER_REWARD").is_ok()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use coins configuration instead of re-fetching the environment?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this to enable watcher rewards in certain tests and disable in others temporarily. Soon I will implement watcher rewards as an option for orders and there won't be need for this anymore.

//self.contract_supports_watchers
}
}
Expand Down Expand Up @@ -1797,7 +1797,7 @@ impl WatcherOps for EthCoin {
},
};

let send_contract_reward_on_spend = true;
let send_contract_reward_on_spend = other_coin.is_eth();

Ok(Some(WatcherReward {
amount,
Expand Down
122 changes: 103 additions & 19 deletions mm2src/coins/eth/eth_tests.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use super::*;
use crate::IguanaPrivKey;
use common::{block_on, now_sec_u32};
use common::{block_on, now_sec_u32, wait_until_sec};
use crypto::privkey::key_pair_from_seed;
use ethkey::{Generator, Random};
use mm2_core::mm_ctx::{MmArc, MmCtxBuilder};
use mm2_test_helpers::for_tests::{eth_jst_testnet_conf, eth_testnet_conf, ETH_DEV_NODE, ETH_DEV_SWAP_CONTRACT,
ETH_DEV_TOKEN_CONTRACT, ETH_MAINNET_NODE, ETH_MAINNET_SWAP_CONTRACT};
use mm2_test_helpers::{for_tests::{eth_jst_testnet_conf, eth_testnet_conf, ETH_DEV_NODE, ETH_DEV_NODES,
ETH_DEV_SWAP_CONTRACT, ETH_DEV_TOKEN_CONTRACT, ETH_MAINNET_NODE,
ETH_MAINNET_SWAP_CONTRACT},
get_passphrase};
use mocktopus::mocking::*;

/// The gas price for the tests
Expand All @@ -17,11 +21,62 @@ const GAS_PRICE_APPROXIMATION_ON_TRADE_PREIMAGE: u64 = 53_500_000_000;

const TAKER_PAYMENT_SPEND_SEARCH_INTERVAL: f64 = 1.;

lazy_static! {
static ref ETH_DISTRIBUTOR: EthCoin = eth_distributor();
static ref JST_DISTRIBUTOR: EthCoin = jst_distributor();
static ref MM_CTX: MmArc = MmCtxBuilder::new().into_mm_arc();
}

fn check_sum(addr: &str, expected: &str) {
let actual = checksum_address(addr);
assert_eq!(expected, actual);
}

pub fn eth_distributor() -> EthCoin {
let req = json!({
"method": "enable",
"coin": "ETH",
"urls": ETH_DEV_NODES,
"swap_contract_address": ETH_DEV_SWAP_CONTRACT,
});
let seed = get_passphrase!(".env.client", "ALICE_PASSPHRASE").unwrap();
let keypair = key_pair_from_seed(&seed).unwrap();
let priv_key_policy = PrivKeyBuildPolicy::IguanaPrivKey(keypair.private().secret);
block_on(eth_coin_from_conf_and_request(
&MM_CTX,
"ETH",
&eth_testnet_conf(),
&req,
CoinProtocol::ETH,
priv_key_policy,
))
.unwrap()
}

pub fn jst_distributor() -> EthCoin {
let req = json!({
"method": "enable",
"coin": "ETH",
"urls": ETH_DEV_NODES,
"swap_contract_address": ETH_DEV_SWAP_CONTRACT,
});
let seed = get_passphrase!(".env.client", "BOB_PASSPHRASE").unwrap();
let keypair = key_pair_from_seed(&seed).unwrap();
let priv_key_policy = PrivKeyBuildPolicy::IguanaPrivKey(keypair.private().secret);
block_on(eth_coin_from_conf_and_request(
&MM_CTX,
"ETH",
&eth_testnet_conf(),
&req,
CoinProtocol::ERC20 {
platform: "ETH".to_string(),
contract_address: ETH_DEV_TOKEN_CONTRACT.to_string(),
},
priv_key_policy,
))
.unwrap()
}

fn eth_coin_for_test(
coin_type: EthCoinType,
urls: &[&str],
Expand All @@ -31,7 +86,25 @@ fn eth_coin_for_test(
&hex::decode("809465b17d0a4ddb3e4c69e8f23c2cabad868f51f8bed5c765ad1d6516c3306f").unwrap(),
)
.unwrap();
eth_coin_from_keypair(coin_type, urls, fallback_swap_contract, key_pair)
}

fn random_eth_coin_for_test(
coin_type: EthCoinType,
urls: &[&str],
fallback_swap_contract: Option<Address>,
) -> (MmArc, EthCoin) {
let key_pair = Random.generate().unwrap();
fill_eth(key_pair.address(), 0.001);
eth_coin_from_keypair(coin_type, urls, fallback_swap_contract, key_pair)
}

fn eth_coin_from_keypair(
coin_type: EthCoinType,
urls: &[&str],
fallback_swap_contract: Option<Address>,
key_pair: KeyPair,
) -> (MmArc, EthCoin) {
let mut nodes = vec![];
for url in urls.iter() {
nodes.push(HttpTransportNode {
Expand Down Expand Up @@ -85,6 +158,24 @@ fn eth_coin_for_test(
(ctx, eth_coin)
}

pub fn fill_eth(to_addr: Address, amount: f64) {
let wei_per_eth: u64 = 1_000_000_000_000_000_000;
let amount_in_wei = (amount * wei_per_eth as f64) as u64;
ETH_DISTRIBUTOR
.send_to_address(to_addr, amount_in_wei.into())
.wait()
.unwrap();
}

pub fn fill_jst(to_addr: Address, amount: f64) {
let wei_per_jst: u64 = 1_000_000_000_000_000_000;
let amount_in_wei = (amount * wei_per_jst as f64) as u64;
JST_DISTRIBUTOR
.send_to_address(to_addr, amount_in_wei.into())
.wait()
.unwrap();
}

#[test]
/// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md#test-cases
fn test_check_sum_address() {
Expand Down Expand Up @@ -214,10 +305,9 @@ fn test_wei_from_big_decimal() {

#[test]
fn send_and_refund_erc20_payment() {
let key_pair = KeyPair::from_secret_slice(
&hex::decode("d27bfdbb5f89d30f8b7a1d0dcd4e181e54b8a2347836cb59c6570834f820f9a4").unwrap(),
)
.unwrap();
let key_pair = Random.generate().unwrap();
fill_eth(key_pair.address(), 0.001);
fill_jst(key_pair.address(), 0.0001);

let transport = Web3Transport::single_node(ETH_DEV_NODE, false);
let web3 = Web3::new(transport);
Expand Down Expand Up @@ -260,16 +350,15 @@ fn send_and_refund_erc20_payment() {
time_lock,
other_pubkey: &DEX_FEE_ADDR_RAW_PUBKEY,
secret_hash,
amount: "0.001".parse().unwrap(),
amount: "0.0001".parse().unwrap(),
swap_contract_address: &coin.swap_contract_address(),
swap_unique_data: &[],
payment_instructions: &None,
watcher_reward: None,
wait_for_confirmation_until: 0,
wait_for_confirmation_until: wait_until_sec(15),
};
let payment = coin.send_maker_payment(maker_payment_args).wait().unwrap();
log!("{:?}", payment);
block_on(Timer::sleep(5.));

let swap_id = coin.etomic_swap_id(time_lock, secret_hash);
let status = block_on(
Expand All @@ -296,7 +385,6 @@ fn send_and_refund_erc20_payment() {
.wait()
.unwrap();
log!("{:?}", refund);
block_on(Timer::sleep(5.));

let status = block_on(
coin.payment_status(
Expand All @@ -311,10 +399,8 @@ fn send_and_refund_erc20_payment() {

#[test]
fn send_and_refund_eth_payment() {
let key_pair = KeyPair::from_secret_slice(
&hex::decode("0b6d7b4b1b9454f3f0bc76b2988cd672439213a1de23d20a83467131366ba41c").unwrap(),
)
.unwrap();
let key_pair = Random.generate().unwrap();
fill_eth(key_pair.address(), 0.001);
let transport = Web3Transport::single_node(ETH_DEV_NODE, false);
let web3 = Web3::new(transport);
let ctx = MmCtxBuilder::new().into_mm_arc();
Expand Down Expand Up @@ -353,7 +439,7 @@ fn send_and_refund_eth_payment() {
time_lock,
other_pubkey: &DEX_FEE_ADDR_RAW_PUBKEY,
secret_hash,
amount: "0.001".parse().unwrap(),
amount: "0.0001".parse().unwrap(),
swap_contract_address: &coin.swap_contract_address(),
swap_unique_data: &[],
payment_instructions: &None,
Expand All @@ -363,7 +449,6 @@ fn send_and_refund_eth_payment() {
let payment = coin.send_maker_payment(send_maker_payment_args).wait().unwrap();

log!("{:?}", payment);
block_on(Timer::sleep(5.));

let swap_id = coin.etomic_swap_id(time_lock, secret_hash);
let status = block_on(
Expand Down Expand Up @@ -391,7 +476,6 @@ fn send_and_refund_eth_payment() {
.unwrap();

log!("{:?}", refund);
block_on(Timer::sleep(5.));

let status = block_on(
coin.payment_status(
Expand Down Expand Up @@ -820,7 +904,7 @@ fn test_nonce_lock() {

// send several transactions concurrently to check that they are not using same nonce
// using real ETH dev node
let (ctx, coin) = eth_coin_for_test(EthCoinType::Eth, ETH_DEV_NODES, None);
let (ctx, coin) = random_eth_coin_for_test(EthCoinType::Eth, ETH_DEV_NODES, None);
let mut futures = vec![];
for _ in 0..5 {
futures.push(
Expand Down
2 changes: 1 addition & 1 deletion mm2src/mm2_main/src/lp_swap/maker_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ impl MakerSwap {
};

// This will be done during order match
self.w().watcher_reward = false;
self.w().watcher_reward = std::env::var("USE_WATCHER_REWARD").is_ok();

Ok((Some(MakerSwapCommand::Negotiate), vec![MakerSwapEvent::Started(data)]))
}
Expand Down
2 changes: 1 addition & 1 deletion mm2src/mm2_main/src/lp_swap/taker_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,7 @@ impl TakerSwap {
};

// This will be done during order match
self.w().watcher_reward = false;
self.w().watcher_reward = std::env::var("USE_WATCHER_REWARD").is_ok();

Ok((Some(TakerSwapCommand::Negotiate), vec![TakerSwapEvent::Started(data)]))
}
Expand Down
25 changes: 12 additions & 13 deletions mm2src/mm2_main/tests/docker_tests/docker_tests_common.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
pub use common::{block_on, now_ms, now_sec, wait_until_ms, wait_until_sec};
pub use mm2_number::MmNumber;
pub use mm2_test_helpers::for_tests::{check_my_swap_status, check_recent_swaps, check_stats_swap_status,
enable_native_bch, eth_sepolia_conf, jst_sepolia_conf, mm_dump, MarketMakerIt,
MAKER_ERROR_EVENTS, MAKER_SUCCESS_EVENTS, TAKER_ERROR_EVENTS,
TAKER_SUCCESS_EVENTS};
use mm2_test_helpers::for_tests::{ETH_SEPOLIA_NODE, ETH_SEPOLIA_SWAP_CONTRACT, ETH_SEPOLIA_TOKEN_CONTRACT};
enable_native, enable_native_bch, eth_jst_testnet_conf, eth_sepolia_conf,
eth_testnet_conf, jst_sepolia_conf, mm_dump, MarketMakerIt, ETH_DEV_NODES,
ETH_DEV_SWAP_CONTRACT, ETH_DEV_TOKEN_CONTRACT, MAKER_ERROR_EVENTS,
MAKER_SUCCESS_EVENTS, TAKER_ERROR_EVENTS, TAKER_SUCCESS_EVENTS};
pub use secp256k1::{PublicKey, SecretKey};
pub use std::env;
pub use std::thread;
Expand All @@ -31,7 +31,6 @@ use http::StatusCode;
use keys::{Address, AddressHashEnum, KeyPair, NetworkPrefix as CashAddrPrefix};
use mm2_core::mm_ctx::{MmArc, MmCtxBuilder};
use mm2_number::BigDecimal;
use mm2_test_helpers::for_tests::{enable_native, eth_testnet_conf, ETH_DEV_NODES, ETH_DEV_SWAP_CONTRACT};
use mm2_test_helpers::structs::{MyBalanceResponse, TransactionDetails};
use primitives::hash::{H160, H256};
use script::Builder;
Expand Down Expand Up @@ -165,19 +164,19 @@ pub fn _fill_eth(to_addr: &str) {
}

// Generates an ethereum coin in the sepolia network with the given seed
pub fn generate_eth_coin_with_seed(seed: &str) -> EthCoin {
pub fn _generate_eth_coin_with_seed(seed: &str) -> EthCoin {
let req = json!({
"method": "enable",
"coin": "ETH",
"urls": ETH_SEPOLIA_NODE,
"swap_contract_address": ETH_SEPOLIA_SWAP_CONTRACT,
"urls": ETH_DEV_NODES,
"swap_contract_address": ETH_DEV_SWAP_CONTRACT,
});
let keypair = key_pair_from_seed(seed).unwrap();
let priv_key_policy = PrivKeyBuildPolicy::IguanaPrivKey(keypair.private().secret);
block_on(eth_coin_from_conf_and_request(
&MM_CTX,
"ETH",
&eth_sepolia_conf(),
&eth_testnet_conf(),
&req,
CoinProtocol::ETH,
priv_key_policy,
Expand All @@ -189,20 +188,20 @@ pub fn generate_jst_with_seed(seed: &str) -> EthCoin {
let req = json!({
"method": "enable",
"coin": "JST",
"urls": ETH_SEPOLIA_NODE,
"swap_contract_address": ETH_SEPOLIA_SWAP_CONTRACT,
"urls": ETH_DEV_NODES,
"swap_contract_address": ETH_DEV_SWAP_CONTRACT,
});

let keypair = key_pair_from_seed(seed).unwrap();
let priv_key_policy = PrivKeyBuildPolicy::IguanaPrivKey(keypair.private().secret);
block_on(eth_coin_from_conf_and_request(
&MM_CTX,
"JST",
&jst_sepolia_conf(),
&eth_jst_testnet_conf(),
&req,
CoinProtocol::ERC20 {
platform: "ETH".into(),
contract_address: String::from(ETH_SEPOLIA_TOKEN_CONTRACT),
contract_address: String::from(ETH_DEV_TOKEN_CONTRACT),
},
priv_key_policy,
))
Expand Down
Loading