def test_ethereum_transactions_with_offline_relayer( basic_transfer_request: EthereumToSifchainTransferRequest, smart_contracts_dir, source_ethereum_address, bridgebank_address, integration_dir, ): logging.debug( "need one transaction to make sure ebrelayer writes out relaydb") basic_transfer_request.ethereum_address = source_ethereum_address generate_minimal_test_account(base_transfer_request=basic_transfer_request, target_ceth_balance=100) logging.info("shut down ebrelayer") time.sleep(10) test_utilities.get_shell_output(f"pkill -9 ebrelayer || true") logging.info("prepare transactions to be sent while ebrelayer is offline") amount = 9000 new_addresses = list(map(lambda x: create_new_sifaddr(), range(3))) logging.debug(f"new_addresses: {new_addresses}") request: EthereumToSifchainTransferRequest = copy.deepcopy( basic_transfer_request) requests = list( map( lambda addr: { "amount": amount, "symbol": test_utilities.NULL_ADDRESS, "sifchain_address": addr }, new_addresses)) json_requests = json.dumps(requests) logging.info("send ethereum transactions while ebrelayer is offline") yarn_result = test_utilities.run_yarn_command(" ".join([ f"yarn --cwd {smart_contracts_dir}", "integrationtest:sendBulkLockTx", f"--amount {amount}", f"--symbol eth", f"--json_path {request.solidity_json_path}", f"--sifchain_address {new_addresses[0]}", f"--transactions \'{json_requests}\'", f"--ethereum_address {source_ethereum_address}", f"--bridgebank_address {bridgebank_address}" ])) logging.debug(f"bulk result: {yarn_result}") logging.info( "restart ebrelayer with outstanding locks on the ethereum side") test_utilities.get_shell_output( f"{integration_dir}/sifchain_start_ebrelayer.sh") time.sleep(5) for _ in new_addresses: # ebrelayer only reads blocks if there are new blocks generated test_utilities.advance_n_ethereum_blocks(test_utilities.n_wait_blocks, request.smart_contracts_dir) for a in new_addresses: test_utilities.wait_for_sif_account( a, basic_transfer_request.sifnoded_node, 90) test_utilities.wait_for_sifchain_addr_balance( a, "ceth", amount, basic_transfer_request.sifnoded_node, 90)
def test_bulk_transfers( basic_transfer_request: EthereumToSifchainTransferRequest, smart_contracts_dir, source_ethereum_address, bridgebank_address, integration_dir, ): logging.info("shut down ebrelayer") test_utilities.get_shell_output(f"pkill -9 ebrelayer || true") logging.info("prepare transactions to be sent while ebrelayer is offline") amount = 9000 new_addresses = list(map(lambda x: create_new_sifaddr(), range(3))) logging.debug(f"new_addresses: {new_addresses}") request: EthereumToSifchainTransferRequest = copy.deepcopy( basic_transfer_request) requests = list( map( lambda addr: { "amount": amount, "symbol": test_utilities.NULL_ADDRESS, "sifchain_address": addr }, new_addresses)) json_requests = json.dumps(requests) logging.info("send ethereum transactions while ebrelayer is offline") yarn_result = test_utilities.run_yarn_command(" ".join([ f"yarn --cwd {smart_contracts_dir}", "integrationtest:sendBulkLockTx", f"--amount {amount}", f"--symbol eth", f"--sifchain_address {new_addresses[0]}", f"--transactions \'{json_requests}\'", f"--ethereum_address {source_ethereum_address}", f"--bridgebank_address {bridgebank_address}" ])) logging.info(f"bulk result: {yarn_result}") logging.info("restart ebrelayer") test_utilities.get_shell_output( f"{integration_dir}/sifchain_start_ebrelayer.sh") test_utilities.advance_n_ethereum_blocks(test_utilities.n_wait_blocks, request.smart_contracts_dir) for a in new_addresses: test_utilities.wait_for_sif_account( a, basic_transfer_request.sifnodecli_node, 90) test_utilities.wait_for_sifchain_addr_balance( a, "ceth", amount, basic_transfer_request.sifnodecli_node, 90)
def transfer_sifchain_to_sifchain( transfer_request: EthereumToSifchainTransferRequest, credentials: SifchaincliCredentials, max_seconds: int = 30 ): logging.debug(f"transfer_sifchain_to_sifchain: {transfer_request.as_json()}") try: sifchain_starting_balance = get_sifchain_addr_balance( transfer_request.sifchain_destination_address, transfer_request.sifnodecli_node, transfer_request.sifchain_symbol ) except Exception as e: # this is a new account, so the balance is 0 sifchain_starting_balance = 0 status = { "action": "transfer_sifchain_to_sifchain", "sifchain_starting_balance": sifchain_starting_balance, } logging.info(status) send_from_sifchain_to_sifchain( transfer_request, credentials ) target_balance = transfer_request.amount + sifchain_starting_balance wait_for_sif_account( sif_addr=transfer_request.sifchain_destination_address, sifchaincli_node=transfer_request.sifnodecli_node, max_seconds=max_seconds ) wait_for_sifchain_addr_balance( sifchain_address=transfer_request.sifchain_destination_address, symbol=transfer_request.sifchain_symbol, target_balance=target_balance, sifchaincli_node=transfer_request.sifnodecli_node, max_seconds=max_seconds, debug_prefix=f"transfer_sifchain_to_sifchain {transfer_request}" ) return { **status, "sifchain_ending_balance": target_balance, }
def test_bulk_transfers( basic_transfer_request: EthereumToSifchainTransferRequest, smart_contracts_dir, source_ethereum_address, bridgebank_address, integration_dir, ganache_timed_blocks, ): n_transfers = int(test_utilities.get_optional_env_var("NTRANSFERS", 1)) ganache_delay = test_utilities.get_optional_env_var("GANACHE_DELAY", 1) # test_utilities.get_shell_output(f"{integration_dir}/ganache_start.sh {ganache_delay}") amount = 9000 new_addresses = list( map(lambda x: create_new_sifaddr(), range(n_transfers))) logging.debug(f"new_addresses: {new_addresses}") request: EthereumToSifchainTransferRequest = copy.deepcopy( basic_transfer_request) requests = list( map( lambda addr: { "amount": amount, "symbol": test_utilities.NULL_ADDRESS, "sifchain_address": addr }, new_addresses)) json_requests = json.dumps(requests) yarn_result = test_utilities.run_yarn_command(" ".join([ f"yarn --cwd {smart_contracts_dir}", "integrationtest:sendBulkLockTx", f"--amount {amount}", f"--symbol eth", f"--sifchain_address {new_addresses[0]}", f"--transactions \'{json_requests}\'", f"--ethereum_address {source_ethereum_address}", f"--bridgebank_address {bridgebank_address}" ])) logging.info(f"bulk result: {yarn_result}") test_utilities.wait_for_ethereum_block_number( yarn_result["blockNumber"] + test_utilities.n_wait_blocks, basic_transfer_request) for a in new_addresses: test_utilities.wait_for_sif_account( a, basic_transfer_request.sifnodecli_node, 90) test_utilities.wait_for_sifchain_addr_balance( a, "ceth", amount, basic_transfer_request.sifnodecli_node, 90)
def transfer_ethereum_to_sifchain( transfer_request: EthereumToSifchainTransferRequest, max_seconds: int = default_timeout_for_ganache): logging.debug( f"transfer_ethereum_to_sifchain {transfer_request.as_json()}") assert transfer_request.ethereum_address assert transfer_request.sifchain_address # it's possible that this is the first transfer to the address, so there's # no balance to retrieve. Catch that exception. original_log_level = decrease_log_level() try: sifchain_starting_balance = get_sifchain_addr_balance( transfer_request.sifchain_address, transfer_request.sifnodecli_node, transfer_request.sifchain_symbol) except: logging.debug( f"transfer_ethereum_to_sifchain failed to get starting balance, this is probably a new account" ) sifchain_starting_balance = 0 status = { "action": "transfer_ethereum_to_sifchain", "sifchain_starting_balance": sifchain_starting_balance, "transfer_request": transfer_request.__dict__, } logging.debug( f"transfer_ethereum_to_sifchain_json: {json.dumps(status)}", ) force_log_level(original_log_level) starting_block = send_from_ethereum_to_sifchain(transfer_request) logging.debug( f"send_from_ethereum_to_sifchain ethereum block number: {starting_block}" ) original_log_level = decrease_log_level() half_n_wait_blocks = n_wait_blocks / 2 logging.debug("wait half the blocks, transfer should not complete") if transfer_request.manual_block_advance: advance_n_ethereum_blocks(half_n_wait_blocks, transfer_request.smart_contracts_dir) time.sleep(5) else: wait_for_ethereum_block_number(block_number=starting_block + half_n_wait_blocks, transfer_request=transfer_request) # we still may not have an account try: sifchain_balance_before_required_elapsed_blocks = get_sifchain_addr_balance( transfer_request.sifchain_address, transfer_request.sifnodecli_node, transfer_request.sifchain_symbol) except: sifchain_balance_before_required_elapsed_blocks = 0 if transfer_request.check_wait_blocks and sifchain_balance_before_required_elapsed_blocks != sifchain_starting_balance: print_error_message( f"balance should not have changed yet. Starting balance {sifchain_starting_balance}," f" current balance {sifchain_balance_before_required_elapsed_blocks}" ) if transfer_request.manual_block_advance: advance_n_ethereum_blocks(half_n_wait_blocks, transfer_request.smart_contracts_dir) else: wait_for_ethereum_block_number(block_number=starting_block + n_wait_blocks, transfer_request=transfer_request) target_balance = sifchain_starting_balance + transfer_request.amount # You can't get the balance of an account that doesn't exist yet, # so wait for the account to be there before asking for the balance logging.debug(f"wait for account {transfer_request.sifchain_address}") wait_for_sif_account(sif_addr=transfer_request.sifchain_address, sifchaincli_node=transfer_request.sifnodecli_node, max_seconds=max_seconds) wait_for_sifchain_addr_balance( sifchain_address=transfer_request.sifchain_address, symbol=transfer_request.sifchain_symbol, sifchaincli_node=transfer_request.sifnodecli_node, target_balance=target_balance, max_seconds=max_seconds, debug_prefix= f"transfer_ethereum_to_sifchain waiting for balance {transfer_request}" ) force_log_level(original_log_level) result = { **status, "sifchain_ending_balance": target_balance, } logging.debug(f"transfer_ethereum_to_sifchain completed {result}") return result
def test_bulk_transfers_from_sifchain( basic_transfer_request: EthereumToSifchainTransferRequest, rowan_source_integrationtest_env_credentials: SifchaincliCredentials, rowan_source_integrationtest_env_transfer_request: EthereumToSifchainTransferRequest, smart_contracts_dir, source_ethereum_address, rowan_source, rowan_source_key, bridgebank_address, bridgetoken_address, ethereum_network, ): basic_transfer_request.ethereum_address = source_ethereum_address logging.info(f"transfer_request: {basic_transfer_request}") # account_with_ceth, credentials_for_account_with_ceth = generate_test_account( # base_transfer_request=basic_transfer_request, # rowan_source_integrationtest_env_transfer_request=rowan_source_integrationtest_env_transfer_request, # rowan_source_integrationtest_env_credentials=rowan_source_integrationtest_env_credentials, # target_ceth_balance=5 * 10 ** 18, # target_rowan_balance=50 * 10 ** 18 # ) n_transfers = int(test_utilities.get_optional_env_var("NTRANSFERS", 2)) amount = "{:d}".format(5 * test_utilities.highest_gas_cost) new_addresses_and_keys = list(map(lambda x: create_new_sifaddr_and_key(), range(n_transfers))) logging.info(f"aandk: {new_addresses_and_keys}") new_addresses = list(map(lambda a: a[0], new_addresses_and_keys)) logging.debug(f"new_addresses: {new_addresses}") new_eth_addrs = test_utilities.create_ethereum_addresses(smart_contracts_dir, basic_transfer_request.ethereum_network, len(new_addresses)) logging.info(f"new eth addrs: {new_eth_addrs}") request: EthereumToSifchainTransferRequest = copy.deepcopy(basic_transfer_request) requests = list(map(lambda addr: { "amount": amount, "symbol": test_utilities.NULL_ADDRESS, "sifchain_address": addr }, new_addresses)) request.amount = 5 * test_utilities.highest_gas_cost credentials_for_account_with_ceth = SifchaincliCredentials(from_key=rowan_source_key) for r in requests: request.ethereum_address = source_ethereum_address request.sifchain_address = rowan_source request.sifchain_destination_address = r["sifchain_address"] request.sifchain_symbol = "ceth" request.ethereum_symbol = "eth" logging.warning(f"requestis: {request}") test_utilities.send_from_sifchain_to_sifchain(request, credentials_for_account_with_ceth) time.sleep(3) request.sifchain_symbol = "rowan" request.ethereum_symbol = bridgetoken_address test_utilities.send_from_sifchain_to_sifchain(request, credentials_for_account_with_ceth) time.sleep(3) for a in new_addresses: test_utilities.wait_for_sif_account(a, basic_transfer_request.sifnodecli_node, 90) test_utilities.wait_for_sifchain_addr_balance(a, "ceth", amount, basic_transfer_request.sifnodecli_node, 180) test_utilities.wait_for_sifchain_addr_balance(a, "rowan", amount, basic_transfer_request.sifnodecli_node, 180) text_file = open("pfile.cmds", "w") simple_credentials = SifchaincliCredentials( keyring_passphrase=None, keyring_backend="test", from_key=None, sifnodecli_homedir=None ) logging.info(f"all accounts are on sifchain and have the correct balance") for sifaddr, ethaddr in zip(new_addresses_and_keys, new_eth_addrs): r = copy.deepcopy(basic_transfer_request) r.sifchain_address = sifaddr[0] r.ethereum_address = ethaddr["address"] r.amount = 100 simple_credentials.from_key = sifaddr[1] c = test_utilities.send_from_sifchain_to_ethereum_cmd(r, simple_credentials) text_file.write(f"{c}\n") text_file.close() test_utilities.get_shell_output("cat pfile.cmds | parallel --trim lr -v {}") test_utilities.advance_n_ethereum_blocks(test_utilities.n_wait_blocks, smart_contracts_dir) # test_utilities.get_shell_output("bash -x pfile.cmds") for sifaddr, ethaddr in zip(new_addresses_and_keys, new_eth_addrs): r = copy.deepcopy(basic_transfer_request) r.ethereum_address = ethaddr["address"] r.amount = 100 test_utilities.wait_for_eth_balance(r, 100, 6000 * (n_transfers + 1))
import sys from test_utilities import wait_for_sif_account wait_for_sif_account(sys.argv[2], "")
def test_bulk_transfers( basic_transfer_request: EthereumToSifchainTransferRequest, smart_contracts_dir, source_ethereum_address, bridgebank_address, bridgetoken_address, ethereum_network, ): n_transfers = int(test_utilities.get_optional_env_var("NTRANSFERS", 2)) ganache_delay = test_utilities.get_optional_env_var("GANACHE_DELAY", 1) # test_utilities.get_shell_output(f"{integration_dir}/ganache_start.sh {ganache_delay}") amount = "{:d}".format(5 * test_utilities.highest_gas_cost) new_addresses_and_keys = list( map(lambda x: create_new_sifaddr_and_key(), range(n_transfers))) logging.info(f"aandk: {new_addresses_and_keys}") new_addresses = list(map(lambda a: a[0], new_addresses_and_keys)) logging.debug(f"new_addresses: {new_addresses}") new_eth_addrs = test_utilities.create_ethereum_addresses( smart_contracts_dir, basic_transfer_request.ethereum_network, len(new_addresses)) logging.info(f"new eth addrs: {new_eth_addrs}") request: EthereumToSifchainTransferRequest = copy.deepcopy( basic_transfer_request) requests = list( map( lambda addr: { "amount": amount, "symbol": test_utilities.NULL_ADDRESS, "sifchain_address": addr }, new_addresses)) json_requests = json.dumps(requests) test_utilities.run_yarn_command(" ".join([ f"yarn --cwd {smart_contracts_dir}", "integrationtest:sendBulkLockTx", f"--amount {amount}", f"--symbol eth", f"--json_path {request.solidity_json_path}", f"--sifchain_address {new_addresses[0]}", f"--transactions \'{json_requests}\'", f"--ethereum_address {source_ethereum_address}", f"--bridgebank_address {bridgebank_address}", f"--ethereum_network {ethereum_network}", ])) requests = list( map( lambda addr: { "amount": amount, "symbol": bridgetoken_address, "sifchain_address": addr }, new_addresses)) json_requests = json.dumps(requests) yarn_result = test_utilities.run_yarn_command(" ".join([ f"yarn --cwd {smart_contracts_dir}", "integrationtest:sendBulkLockTx", f"--amount {amount}", "--lock_or_burn burn", f"--symbol {bridgetoken_address}", f"--json_path {request.solidity_json_path}", f"--sifchain_address {new_addresses[0]}", f"--transactions \'{json_requests}\'", f"--ethereum_address {source_ethereum_address}", f"--bridgebank_address {bridgebank_address}", f"--ethereum_network {ethereum_network}", ])) logging.info(f"bulk result: {yarn_result}") manual_advance = False if manual_advance: test_utilities.advance_n_ethereum_blocks(test_utilities.n_wait_blocks, smart_contracts_dir) test_utilities.wait_for_ethereum_block_number( yarn_result["blockNumber"] + test_utilities.n_wait_blocks, basic_transfer_request) for a in new_addresses: test_utilities.wait_for_sif_account( a, basic_transfer_request.sifnodecli_node, 90) test_utilities.wait_for_sifchain_addr_balance( a, "ceth", amount, basic_transfer_request.sifnodecli_node, 180) test_utilities.wait_for_sifchain_addr_balance( a, "rowan", amount, basic_transfer_request.sifnodecli_node, 180) text_file = open("pfile.cmds", "w") simple_credentials = SifchaincliCredentials(keyring_passphrase=None, keyring_backend="test", from_key=None, sifnodecli_homedir=None) logging.info(f"all accounts are on sifchain and have the correct balance") for sifaddr, ethaddr in zip(new_addresses_and_keys, new_eth_addrs): r = copy.deepcopy(basic_transfer_request) r.sifchain_address = sifaddr[0] r.ethereum_address = ethaddr["address"] r.amount = 100 simple_credentials.from_key = sifaddr[1] c = test_utilities.send_from_sifchain_to_ethereum_cmd( r, simple_credentials) text_file.write(f"{c}\n") text_file.close() # test_utilities.get_shell_output("cat pfile.cmds | parallel --trim lr -v {}") test_utilities.get_shell_output("bash -x pfile.cmds") for sifaddr, ethaddr in zip(new_addresses_and_keys, new_eth_addrs): r = copy.deepcopy(basic_transfer_request) r.ethereum_address = ethaddr["address"] r.amount = 100 test_utilities.wait_for_eth_balance(r, 100, 300)