def no_whitelisted_validators(integration_dir): """restart sifchain with no whitelisted validators, execute test, then restart with validators""" yield test_utilities.get_shell_output( f"ADD_VALIDATOR_TO_WHITELIST= bash {integration_dir}/setup_sifchain.sh" ) test_utilities.get_shell_output( f". {integration_dir}/vagrantenv.sh; ADD_VALIDATOR_TO_WHITELIST=true bash {integration_dir}/setup_sifchain.sh" )
def ganache_timed_blocks(integration_dir): "restart ganache with timed blocks (keeps existing database)" logging.info("restart ganache with timed blocks (keeps existing database)") yield test_utilities.get_shell_output( f"{integration_dir}/ganache_start.sh 2") logging.info( "restart ganache with instant mining (keeps existing database)") test_utilities.get_shell_output(f"{integration_dir}/ganache_start.sh")
def ensure_relayer_restart(integration_dir, smart_contracts_dir): """restarts relayer after the test function completes. Used by tests that need to stop the relayer.""" yield None logging.info("restart ebrelayer after advancing wait blocks - avoids any interaction with replaying blocks") original_log_level = decrease_log_level(new_level=logging.WARNING) test_utilities.advance_n_ethereum_blocks(test_utilities.n_wait_blocks + 1, smart_contracts_dir) test_utilities.get_shell_output(f"{integration_dir}/sifchain_start_ebrelayer.sh") force_log_level(original_log_level)
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_sifchain_transactions_with_offline_relayer( basic_transfer_request: EthereumToSifchainTransferRequest, rowan_source_integrationtest_env_credentials: test_utilities. SifchaincliCredentials, rowan_source_integrationtest_env_transfer_request: EthereumToSifchainTransferRequest, rowan_source, smart_contracts_dir, source_ethereum_address, integration_dir, ): basic_transfer_request.ethereum_address = source_ethereum_address request, credentials = generate_test_account( basic_transfer_request, rowan_source_integrationtest_env_transfer_request, rowan_source_integrationtest_env_credentials, target_ceth_balance=10**19, target_rowan_balance=10**19, ) 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_eth_addrs = test_utilities.create_ethereum_addresses( smart_contracts_dir, basic_transfer_request.ethereum_network, 2) request.amount = amount request.sifchain_symbol = "ceth" request.ethereum_symbol = "eth" logging.info("send transactions while ebrelayer is offline") for a in new_eth_addrs: request.ethereum_address = a["address"] sifchain_balance = test_utilities.get_sifchain_addr_balance( request.sifchain_address, request.sifnoded_node, "ceth") logging.info( f"sifchain balance is {sifchain_balance}, request is {request}") test_utilities.send_from_sifchain_to_ethereum(transfer_request=request, credentials=credentials) time.sleep(5) logging.info("restart ebrelayer") test_utilities.get_shell_output( f"{integration_dir}/sifchain_start_ebrelayer.sh") time.sleep(15) test_utilities.advance_n_ethereum_blocks(test_utilities.n_wait_blocks * 2, request.smart_contracts_dir) for a in new_eth_addrs: request.ethereum_address = a["address"] test_utilities.wait_for_eth_balance(request, amount, 600)
def generate_minimal_test_account( base_transfer_request: EthereumToSifchainTransferRequest, target_ceth_balance: int = 10**18, ) -> (EthereumToSifchainTransferRequest, SifchaincliCredentials): """Creates a test account with ceth and rowan. The address for the new account is in request.sifchain_address""" new_account_key = get_shell_output("uuidgen") credentials = sifchain_cli_credentials_for_test(new_account_key) new_addr = burn_lock_functions.create_new_sifaddr(credentials=credentials, keyname=new_account_key) new_sifaddr = new_addr["address"] credentials.from_key = new_addr["name"] request: EthereumToSifchainTransferRequest = copy.deepcopy( base_transfer_request) request.sifchain_address = new_sifaddr request.amount = target_ceth_balance request.sifchain_symbol = "ceth" request.ethereum_symbol = "eth" logging.debug( f"transfer {target_ceth_balance} eth to {new_sifaddr} from {base_transfer_request.ethereum_address}" ) burn_lock_functions.transfer_ethereum_to_sifchain(request) logging.info( f"created sifchain addr {new_sifaddr} with {test_utilities.display_currency_value(target_ceth_balance)} ceth" ) return request, credentials
def create_new_sifaddr_and_credentials( ) -> (str, SifchaincliCredentials): new_account_key = get_shell_output("uuidgen") credentials = sifchain_cli_credentials_for_test(new_account_key) new_addr = burn_lock_functions.create_new_sifaddr(credentials=credentials, keyname=new_account_key) credentials.from_key = new_addr["name"] return new_addr["address"], credentials,
def test_two_currencies_with_different_capitalization_should_not_interfere_with_each_other( smart_contracts_dir, bridgebank_address, solidity_json_path, ): new_account_key = ( "a" + get_shell_output("uuidgen").replace("-", "").lower())[:5] amount = amount_in_wei(9) new_currency = create_new_currency(amount, new_account_key, smart_contracts_dir, bridgebank_address, solidity_json_path) (request1, _) = build_request_for_new_sifchain_address(new_currency, amount) burn_lock_functions.transfer_ethereum_to_sifchain(request1, 10) balance_1 = get_sifchain_addr_balance(request1.sifchain_address, request1.sifnodecli_node, request1.sifchain_symbol) assert (balance_1 == request1.amount) new_currency = create_new_currency(amount, new_account_key.upper(), smart_contracts_dir, bridgebank_address, solidity_json_path) (request2, _) = build_request_for_new_sifchain_address(new_currency, amount + 70000) burn_lock_functions.transfer_ethereum_to_sifchain(request2, 10) balance_1_again = get_sifchain_addr_balance(request1.sifchain_address, request1.sifnodecli_node, request1.sifchain_symbol) assert (balance_1 == balance_1_again)
def test_can_create_a_new_token_and_peg_it( token_length: int, basic_transfer_request: EthereumToSifchainTransferRequest, smart_contracts_dir, bridgebank_address, solidity_json_path, operator_address, ethereum_network, source_ethereum_address, ): new_account_key = ( "a" + get_shell_output("uuidgen").replace("-", ""))[:token_length] token_name = new_account_key amount = amount_in_wei(9) new_currency = create_new_currency(amount=amount, symbol=new_account_key, token_name=token_name, decimals=18, smart_contracts_dir=smart_contracts_dir, bridgebank_address=bridgebank_address, solidity_json_path=solidity_json_path, operator_address=operator_address, ethereum_network=ethereum_network) request = build_request_for_new_sifchain_address(basic_transfer_request, source_ethereum_address, new_currency, amount / 10) burn_lock_functions.transfer_ethereum_to_sifchain(request)
def create_new_sifaddr_and_key(): new_account_key = test_utilities.get_shell_output("uuidgen").replace( "-", "") credentials = sifchain_cli_credentials_for_test(new_account_key) new_addr = burn_lock_functions.create_new_sifaddr(credentials=credentials, keyname=new_account_key) return new_addr["address"], new_addr["name"]
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 test_transfer_eth_to_ceth_using_replay_blocks(integration_dir, smart_contracts_dir, ensure_relayer_restart): starting_block = test_utilities.current_ethereum_block_number( smart_contracts_dir) logging.info("stopping ebrelayer") test_utilities.get_shell_output("pkill -9 ebrelayer || true") request, credentials = build_request() logging.info("(no transactions should happen without a relayer)") # test_utilities.whitelist_token(request.ethereum_symbol, request.smart_contracts_dir, True) logging.info( f"send {request.amount / 10**18} eth ({request.amount} wei) to {request.sifchain_address}" ) test_utilities.send_from_ethereum_to_sifchain(request) # test_utilities.get_shell_output(f"{integration_dir}/sifchain_start_ebrelayer.sh") logging.info("replay blocks using ebrelayer replayEthereum") ews = test_utilities.get_required_env_var("ETHEREUM_WEBSOCKET_ADDRESS") bra = test_utilities.get_required_env_var("BRIDGE_REGISTRY_ADDRESS") mon = test_utilities.get_required_env_var("MONIKER") mn = test_utilities.get_required_env_var("MNEMONIC") cn = test_utilities.get_required_env_var("CHAINNET") ending_block = test_utilities.current_ethereum_block_number( smart_contracts_dir) + 1 cmd = f"""ebrelayer replayEthereum tcp://0.0.0.0:26657 {ews} {bra} {mon} '{mn}' {starting_block} {ending_block} 1 2 --chain-id {cn} --gas 5000000000000 \ --gas-prices 0.5rowan""" test_utilities.get_shell_output(cmd) time.sleep(15) logging.info( f"check the ending balance of {request.sifchain_address} after replaying blocks" ) ending_balance = test_utilities.get_sifchain_addr_balance( request.sifchain_address, request.sifnodecli_node, request.sifchain_symbol) assert (ending_balance == request.amount) # now do it again test_utilities.get_shell_output(cmd) time.sleep(5) ending_balance2 = test_utilities.get_sifchain_addr_balance( request.sifchain_address, request.sifnodecli_node, request.sifchain_symbol) assert (ending_balance2 == request.amount) # now start ebrelayer and do another transfer test_utilities.advance_n_ethereum_blocks(test_utilities.n_wait_blocks + 1, smart_contracts_dir) test_utilities.get_shell_output( f"{integration_dir}/sifchain_start_ebrelayer.sh") burn_lock_functions.transfer_ethereum_to_sifchain(request, 15) ending_balance3 = test_utilities.get_sifchain_addr_balance( request.sifchain_address, request.sifnodecli_node, request.sifchain_symbol) assert (ending_balance3 == request.amount * 2)
def generate_test_account( base_transfer_request: EthereumToSifchainTransferRequest, rowan_source_integrationtest_env_transfer_request: EthereumToSifchainTransferRequest, rowan_source_integrationtest_env_credentials: SifchaincliCredentials, target_ceth_balance: int = 10**18, target_rowan_balance: int = 10**18 ) -> (EthereumToSifchainTransferRequest, SifchaincliCredentials): """Creates a test account with ceth and rowan""" new_account_key = get_shell_output("uuidgen") credentials = sifchain_cli_credentials_for_test(new_account_key) new_addr = burn_lock_functions.create_new_sifaddr(credentials=credentials, keyname=new_account_key) new_sifaddr = new_addr["address"] credentials.from_key = new_addr["name"] initial_rowan = test_utilities.get_sifchain_addr_balance( rowan_source_integrationtest_env_transfer_request.sifchain_address, rowan_source_integrationtest_env_transfer_request.sifnodecli_node, "rowan") if target_rowan_balance > 0: rowan_request: EthereumToSifchainTransferRequest = copy.deepcopy( rowan_source_integrationtest_env_transfer_request) rowan_request.sifchain_destination_address = new_sifaddr rowan_request.amount = target_rowan_balance logging.debug( f"transfer {target_rowan_balance} to {new_sifaddr} from {rowan_request.sifchain_address}" ) test_utilities.send_from_sifchain_to_sifchain( rowan_request, rowan_source_integrationtest_env_credentials) request: EthereumToSifchainTransferRequest = copy.deepcopy( base_transfer_request) request.sifchain_address = new_sifaddr request.amount = target_ceth_balance request.sifchain_symbol = "ceth" request.ethereum_symbol = "eth" logging.debug( f"transfer {target_ceth_balance} eth to {new_sifaddr} from {base_transfer_request.ethereum_address}" ) burn_lock_functions.transfer_ethereum_to_sifchain(request) ending_rowan = test_utilities.get_sifchain_addr_balance( rowan_source_integrationtest_env_transfer_request.sifchain_address, rowan_source_integrationtest_env_transfer_request.sifnodecli_node, "rowan") rowan_used = initial_rowan - ending_rowan rowan_fees = rowan_used - target_rowan_balance # logging.info(f"rowan fees from source is {rowan_fees}") logging.info( f"created sifchain addr {new_sifaddr} with {test_utilities.display_currency_value(target_ceth_balance)} ceth and {test_utilities.display_currency_value(target_rowan_balance)} rowan" ) return request, credentials
def test_ebrelayer_restart( basic_transfer_request: EthereumToSifchainTransferRequest, source_ethereum_address: str, integration_dir, ): basic_transfer_request.ethereum_address = source_ethereum_address request, credentials = generate_minimal_test_account( base_transfer_request=basic_transfer_request, target_ceth_balance=10**15) balance = test_utilities.get_sifchain_addr_balance( request.sifchain_address, request.sifnodecli_node, "ceth") logging.info( "restart ebrelayer normally, leaving the last block db in place") 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) time.sleep(5) assert balance == test_utilities.get_sifchain_addr_balance( request.sifchain_address, request.sifnodecli_node, "ceth")
def test_cannot_create_two_currencies_with_the_same_name( smart_contracts_dir, bridgebank_address, solidity_json_path, ): new_account_key = get_shell_output("uuidgen").replace("-", "") new_currency = create_new_currency(amount_in_wei(10), new_account_key, smart_contracts_dir, bridgebank_address, solidity_json_path) with pytest.raises(Exception): new_currency = create_new_currency(amount_in_wei(10), new_account_key, smart_contracts_dir, bridgebank_address, solidity_json_path)
def test_cannot_create_two_currencies_that_only_differ_in_capitalization( smart_contracts_dir, bridgebank_address, solidity_json_path, ): new_account_key = get_shell_output("uuidgen").replace("-", "").lower() new_currency = create_new_currency(10**18, new_account_key, smart_contracts_dir, bridgebank_address, solidity_json_path) with pytest.raises(Exception): new_currency = create_new_currency(10**18, new_account_key, smart_contracts_dir, bridgebank_address, solidity_json_path)
def build_request( ) -> (EthereumToSifchainTransferRequest, SifchaincliCredentials): new_account_key = get_shell_output("uuidgen") credentials = sifchain_cli_credentials_for_test(new_account_key) new_addr = burn_lock_functions.create_new_sifaddr(credentials=credentials, keyname=new_account_key) credentials.from_key = new_addr["name"] request = EthereumToSifchainTransferRequest( sifchain_address=new_addr["address"], smart_contracts_dir=smart_contracts_dir, ethereum_address=ethereum_address, ethereum_private_key_env_var="ETHEREUM_PRIVATE_KEY", bridgebank_address=get_required_env_var("BRIDGE_BANK_ADDRESS"), ethereum_network=(os.environ.get("ETHEREUM_NETWORK") or ""), amount=9 * 10**18, ceth_amount=2 * (10**16)) return request, credentials
def request_faucet_coins(transfer_request: EthereumToSifchainTransferRequest, credentials: SifchaincliCredentials): logging.debug(f"request_faucet_coins {transfer_request}") yes_entry = f"yes {credentials.keyring_passphrase} | " if credentials.keyring_passphrase else "" keyring_backend_entry = f"--keyring-backend {credentials.keyring_backend}" if credentials.keyring_backend else "" chain_id_entry = f"--chain-id {transfer_request.chain_id}" if transfer_request.chain_id else "" node = f"--node {transfer_request.sifnodecli_node}" if transfer_request.sifnodecli_node else "" sifchain_fees_entry = f"--fees {transfer_request.sifchain_fees}" if transfer_request.sifchain_fees else "" cmd = " ".join([ yes_entry, "sifnodecli tx faucet request-coins", f"{transfer_request.amount}{transfer_request.sifchain_symbol}", f"--from {transfer_request.sifchain_address}", keyring_backend_entry, chain_id_entry, node, sifchain_fees_entry, f"--home {credentials.sifnodecli_homedir} ", "-y" ]) result = get_shell_output(cmd) detect_errors_in_sifnodecli_output(result) return result
def test_transfer_eth_to_ceth_using_replay_blocks( integration_dir, smart_contracts_dir, solidity_json_path, source_ethereum_address, validator_address, ensure_relayer_restart): starting_block = test_utilities.current_ethereum_block_number( smart_contracts_dir) logging.info("stopping ebrelayer") test_utilities.get_shell_output("pkill -9 ebrelayer || true") request, credentials = build_request(smart_contracts_dir, source_ethereum_address, solidity_json_path) logging.info("(no transactions should happen without a relayer)") logging.info( f"send {request.amount / 10 ** 18} eth ({request.amount} wei) to {request.sifchain_address}" ) test_utilities.send_from_ethereum_to_sifchain(request) logging.info("make sure no balances changed while the relayer was offline") test_utilities.advance_n_ethereum_blocks(test_utilities.n_wait_blocks, smart_contracts_dir) time.sleep(5) balance_with_no_relayer = test_utilities.get_sifchain_addr_balance( request.sifchain_address, request.sifnoded_node, request.sifchain_symbol) assert (balance_with_no_relayer == 0) logging.info("replay blocks using ebrelayer replayEthereum") ews = test_utilities.get_required_env_var("ETHEREUM_WEBSOCKET_ADDRESS") bra = test_utilities.get_required_env_var("BRIDGE_REGISTRY_ADDRESS") mon = test_utilities.get_required_env_var("MONIKER") mn = test_utilities.get_required_env_var("MNEMONIC") cn = test_utilities.get_required_env_var("CHAINNET") ending_block = test_utilities.current_ethereum_block_number( smart_contracts_dir) + 1 cmd = f"""yes | ebrelayer replayEthereum tcp://0.0.0.0:26657 {ews} {bra} {mon} '{mn}' {starting_block} {ending_block} 1 2 --chain-id {cn} --gas 5000000000000 \ --keyring-backend test --node tcp://0.0.0.0:26657 --from {mon}""" test_utilities.get_shell_output(cmd) time.sleep(5) logging.info( f"check the ending balance of {request.sifchain_address} after replaying blocks" ) ending_balance = test_utilities.get_sifchain_addr_balance( request.sifchain_address, request.sifnoded_node, request.sifchain_symbol) assert (ending_balance == request.amount) # now do it again test_utilities.get_shell_output(cmd) time.sleep(5) ending_balance2 = test_utilities.get_sifchain_addr_balance( request.sifchain_address, request.sifnoded_node, request.sifchain_symbol) assert (ending_balance2 == request.amount)
def test_three_letter_currency_with_capitals_in_name( basic_transfer_request: EthereumToSifchainTransferRequest, source_ethereum_address: str, rowan_source_integrationtest_env_credentials: SifchaincliCredentials, rowan_source_integrationtest_env_transfer_request: EthereumToSifchainTransferRequest, ethereum_network, solidity_json_path, ): new_currency_symbol = ("F" + get_shell_output("uuidgen").replace("-", ""))[:3] do_currency_test(new_currency_symbol, basic_transfer_request, source_ethereum_address, rowan_source_integrationtest_env_credentials, rowan_source_integrationtest_env_transfer_request, ethereum_network, solidity_json_path=solidity_json_path)
def build_request(new_currency, amount): accounts = ganache_accounts(smart_contracts_dir=smart_contracts_dir) new_account_key = get_shell_output("uuidgen") credentials = sifchain_cli_credentials_for_test(new_account_key) new_addr = burn_lock_functions.create_new_sifaddr(credentials=credentials, keyname=new_account_key) credentials.from_key = new_addr["name"] request = EthereumToSifchainTransferRequest( ethereum_symbol=new_currency["newtoken_address"], sifchain_symbol="c" + new_currency["newtoken_symbol"], sifchain_address=get_required_env_var("OWNER_ADDR"), smart_contracts_dir=smart_contracts_dir, ethereum_address=accounts["accounts"][0], ethereum_private_key_env_var="ETHEREUM_PRIVATE_KEY", bridgebank_address=bridgebank_address, bridgetoken_address=bridgetoken_address, ethereum_network=(os.environ.get("ETHEREUM_NETWORK") or ""), amount=amount, ceth_amount=2 * (10**16)) return (request, credentials)
def test_transfer_eth_to_ceth_without_a_validator_should_throw_exception(): new_account_key = get_shell_output("uuidgen") credentials = sifchain_cli_credentials_for_test(new_account_key) new_addr = burn_lock_functions.create_new_sifaddr(credentials=credentials, keyname=new_account_key) credentials.from_key = new_addr["name"] request = EthereumToSifchainTransferRequest( sifchain_address=new_addr["address"], smart_contracts_dir=get_required_env_var("SMART_CONTRACTS_DIR"), ethereum_address=get_required_env_var("ETHEREUM_ADDRESS"), ethereum_private_key_env_var="ETHEREUM_PRIVATE_KEY", bridgebank_address=get_required_env_var("BRIDGE_BANK_ADDRESS"), ethereum_network=(os.environ.get("ETHEREUM_NETWORK") or ""), amount=90000) logging.info( "try to transfer, but expect a failure since there are no whitelisted validators" ) with pytest.raises(Exception): # use a small number for max_retries - on a local system, it shouldn't # take more than a second or two for ebrelayer to act burn_lock_functions.transfer_ethereum_to_sifchain(request, 3)
def build_request( sifnodecli_node, source_ethereum_address, chain_id, smart_contracts_dir ) -> (EthereumToSifchainTransferRequest, SifchaincliCredentials): new_account_key = get_shell_output("uuidgen") credentials = sifchain_cli_credentials_for_test(new_account_key) new_addr = burn_lock_functions.create_new_sifaddr(credentials=credentials, keyname=new_account_key) credentials.from_key = new_addr["name"] ceth_fee = 2 * (10**16) request = EthereumToSifchainTransferRequest( sifchain_address=new_addr["address"], smart_contracts_dir=smart_contracts_dir, ethereum_private_key_env_var="ETHEREUM_PRIVATE_KEY", bridgebank_address=bridgebank_address, ethereum_network=ethereum_network, amount=amount + ceth_fee, ceth_amount=ceth_fee, sifnodecli_node=sifnodecli_node, manual_block_advance=False, chain_id=chain_id, sifchain_fees="100000rowan", ) return request, credentials
new_account_key = 'user1' credentials = sifchain_cli_credentials_for_test(new_account_key) new_addr = burn_lock_functions.create_new_sifaddr(credentials=credentials, keyname=new_account_key) credentials.from_key = new_addr["name"] request = EthereumToSifchainTransferRequest( sifchain_address=new_addr["address"], smart_contracts_dir=smart_contracts_dir, ethereum_address=ethereum_address, ethereum_private_key_env_var="ETHEREUM_PRIVATE_KEY", bridgebank_address=get_required_env_var("BRIDGE_BANK_ADDRESS"), ethereum_network=(os.environ.get("ETHEREUM_NETWORK") or ""), amount=9 * 10**18, ceth_amount=2 * (10**16)) return request, credentials # if there's an existing user1 key, just remove it. Otherwise, adding a duplicate key will just hang try: test_utilities.get_shell_output( f"sifnoded keys delete user1 --home /home/vagrant/.sifnoded --keyring-backend test -o json" ) except: logging.debug("no key to delete, this is normal in a fresh environment") request, credentials = build_request() # test_utilities.set_lock_burn_limit(smart_contracts_dir, request.ethereum_symbol, request.amount) burn_lock_functions.transfer_ethereum_to_sifchain(request) test_utilities.get_sifchain_addr_balance(request.sifchain_address, request.sifnoded_node, "ceth") logging.info(f"created account for key {credentials.from_key}")
def test_rollback_chain(source_ethereum_address): new_account_key = get_shell_output("uuidgen") credentials = sifchain_cli_credentials_for_test(new_account_key) new_account = burn_lock_functions.create_new_sifaddr( credentials=credentials, keyname=new_account_key) credentials.from_key = new_account["name"] # Any amount will work amount = 11000 request = EthereumToSifchainTransferRequest( sifchain_address=new_account["address"], smart_contracts_dir=get_required_env_var("SMART_CONTRACTS_DIR"), ethereum_address=source_ethereum_address, ethereum_private_key_env_var="ETHEREUM_PRIVATE_KEY", bridgebank_address=get_required_env_var("BRIDGE_BANK_ADDRESS"), ethereum_network=(os.environ.get("ETHEREUM_NETWORK") or ""), amount=amount) logging.info(f"create account with a balance of {request.amount}") burn_lock_functions.transfer_ethereum_to_sifchain(request, 50) new_addr = new_account["address"] snapshot = get_shell_output( f"{test_integration_dir}/snapshot_ganache_chain.sh") logging.info(f"created new account, took ganache snapshot {snapshot}") initial_user_balance = get_sifchain_addr_balance(new_addr, "", request.sifchain_symbol) logging.info(f"initial_user_balance {initial_user_balance}") transfer_1 = send_from_ethereum_to_sifchain(transfer_request=request) logging.info(f"transfer started but it will never complete (by design)") logging.info("advance less than wait blocks") advance_n_ethereum_blocks(n_wait_blocks / 2, request.smart_contracts_dir) # the transaction should not have happened on the sifchain side yet # since we haven't waited for the right number of blocks. # roll back ganache to the snapshot and try another transfer that # should succeed. logging.info( f"apply snapshot {snapshot} - this eliminates transfer_1 (block {transfer_1})" ) get_shell_output( f"{test_integration_dir}/apply_ganache_snapshot.sh {snapshot} 2>&1") logging.info("advance past block wait") advance_n_ethereum_blocks(n_wait_blocks * 2, request.smart_contracts_dir) time.sleep(5) second_user_balance = get_sifchain_addr_balance(new_addr, "", request.sifchain_symbol) if second_user_balance == initial_user_balance: logging.info( f"got expected outcome of no balance change @ {initial_user_balance}" ) else: raise Exception( f"balance should be the same after applying snapshot and rolling forward n_wait_blocks * 2. initial_user_balance: {initial_user_balance} second_user_balance: {second_user_balance}" ) request.amount = 10000 logging.info(f"sending more eth: {request.amount} to {new_addr}") burn_lock_functions.transfer_ethereum_to_sifchain(request) # We want to know that ebrelayer will never do a second transaction. # We can't know that, so just delay a reasonable amount of time. logging.info("delay to give ebrelayer time to make a mistake") time.sleep(10) balance_after_sleep = get_sifchain_addr_balance(new_addr, "", request.sifchain_symbol) logging.info( f"get_sifchain_addr_balance after sleep is {balance_after_sleep} for {new_addr}" ) expected_balance = initial_user_balance + request.amount logging.info(f"look for a balance of {expected_balance}") wait_for_sifchain_addr_balance(new_addr, request.sifchain_symbol, expected_balance, "")
def test_pools( basic_transfer_request: EthereumToSifchainTransferRequest, source_ethereum_address: str, rowan_source_integrationtest_env_credentials: SifchaincliCredentials, rowan_source_integrationtest_env_transfer_request: EthereumToSifchainTransferRequest, solidity_json_path): # max symbol length in clp is 10 new_currency_symbol = ("a" + get_shell_output("uuidgen").replace("-", ""))[:8] target_new_currency_balance = 5 * 10**18 logging.info(f"create new currency") new_currency = test_utilities.create_new_currency( 1000 * target_new_currency_balance, new_currency_symbol, smart_contracts_dir=smart_contracts_dir, bridgebank_address=bridgebank_address, solidity_json_path=solidity_json_path) sifchain_symbol = ("c" + new_currency["newtoken_symbol"]).lower() basic_transfer_request.ethereum_address = source_ethereum_address basic_transfer_request.check_wait_blocks = True target_rowan_balance = 5 * 10**18 target_ceth_balance = 10**18 request, credentials = generate_test_account( basic_transfer_request, rowan_source_integrationtest_env_transfer_request, rowan_source_integrationtest_env_credentials, target_ceth_balance=target_ceth_balance, target_rowan_balance=target_rowan_balance) logging.info( f"transfer some of the new currency {new_currency_symbol} to the test sifchain address" ) request.ethereum_symbol = new_currency["newtoken_address"] request.sifchain_symbol = sifchain_symbol request.amount = target_new_currency_balance burn_lock_functions.transfer_ethereum_to_sifchain(request) sifaddress = request.sifchain_address sifchain_fees = 100000 # Should probably make this a constant #from_key = credentials.from_key # wait for balance test_utilities.wait_for_sifchain_addr_balance( sifaddress, "rowan", target_rowan_balance, basic_transfer_request.sifnodecli_node) test_utilities.wait_for_sifchain_addr_balance( sifaddress, sifchain_symbol, target_new_currency_balance, basic_transfer_request.sifnodecli_node) pools = get_pools(basic_transfer_request.sifnodecli_node) basic_transfer_request.sifchain_symbol = sifchain_symbol basic_transfer_request.sifchain_address = sifaddress current_coin_balance = target_new_currency_balance current_rowan_balance = target_rowan_balance change_amount = 10**19 basic_transfer_request.amount = change_amount # Fail if amount is greater than user has txn = create_pool(basic_transfer_request, credentials) assert (txn["code"] == 12) current_rowan_balance = current_rowan_balance - sifchain_fees assert (test_utilities.get_sifchain_addr_balance( sifaddress, basic_transfer_request.sifnodecli_node, "rowan") == current_rowan_balance) change_amount = 10**17 basic_transfer_request.amount = change_amount # Fail if amount is less than or equal to minimum txn = create_pool(basic_transfer_request, credentials) assert (txn["code"] == 7) current_rowan_balance = current_rowan_balance - sifchain_fees assert (test_utilities.get_sifchain_addr_balance( sifaddress, basic_transfer_request.sifnodecli_node, "rowan") == current_rowan_balance) change_amount = 10**18 basic_transfer_request.amount = change_amount # Only works the first time, fails later. txn = create_pool(basic_transfer_request, credentials) assert (txn.get("code", 0) == 0) get_pools(basic_transfer_request.sifnodecli_node) current_coin_balance = current_coin_balance - change_amount current_rowan_balance = current_rowan_balance - change_amount - sifchain_fees assert (test_utilities.get_sifchain_addr_balance( sifaddress, basic_transfer_request.sifnodecli_node, "rowan") == current_rowan_balance) assert (test_utilities.get_sifchain_addr_balance( sifaddress, basic_transfer_request.sifnodecli_node, sifchain_symbol) == current_coin_balance) # check for failure if we try to create a pool twice txn = create_pool(basic_transfer_request, credentials) assert (txn["code"] == 14) get_pools(basic_transfer_request.sifnodecli_node) current_rowan_balance = current_rowan_balance - sifchain_fees assert (test_utilities.get_sifchain_addr_balance( sifaddress, basic_transfer_request.sifnodecli_node, "rowan") == current_rowan_balance) assert (test_utilities.get_sifchain_addr_balance( sifaddress, basic_transfer_request.sifnodecli_node, sifchain_symbol) == current_coin_balance) # ensure we can add liquidity, money gets transferred txn = add_pool_liquidity(basic_transfer_request, credentials) assert (txn.get("code", 0) == 0) get_pools(basic_transfer_request.sifnodecli_node) current_coin_balance = current_coin_balance - change_amount current_rowan_balance = current_rowan_balance - change_amount - sifchain_fees assert (test_utilities.get_sifchain_addr_balance( sifaddress, basic_transfer_request.sifnodecli_node, "rowan") == current_rowan_balance) assert (test_utilities.get_sifchain_addr_balance( sifaddress, basic_transfer_request.sifnodecli_node, sifchain_symbol) == current_coin_balance) # check for failure if we try to remove all liquidity txn = remove_pool_liquidity(basic_transfer_request, credentials, 10000) assert (txn["code"] == 26) get_pools(basic_transfer_request.sifnodecli_node) current_rowan_balance = current_rowan_balance - sifchain_fees assert (test_utilities.get_sifchain_addr_balance( sifaddress, basic_transfer_request.sifnodecli_node, "rowan") == current_rowan_balance) assert (test_utilities.get_sifchain_addr_balance( sifaddress, basic_transfer_request.sifnodecli_node, sifchain_symbol) == current_coin_balance) # ensure we can remove liquidity, money gets transferred txn = remove_pool_liquidity(basic_transfer_request, credentials, 5000) assert (txn.get("code", 0) == 0) get_pools(basic_transfer_request.sifnodecli_node) current_coin_balance = current_coin_balance + change_amount current_rowan_balance = current_rowan_balance + change_amount - sifchain_fees #assert(test_utilities.get_sifchain_addr_balance(sifaddress, basic_transfer_request.sifnodecli_node, "rowan") == current_rowan_balance) #assert(test_utilities.get_sifchain_addr_balance(sifaddress, basic_transfer_request.sifnodecli_node, sifchain_symbol) == current_ceth_balance) # no slippage if pool is perfectly balanced. # TODO: compute this precisely? slip_pct = 0.01 balance = test_utilities.get_sifchain_addr_balance( sifaddress, basic_transfer_request.sifnodecli_node, "rowan") slip_cost = (slip_pct * current_rowan_balance) assert (balance >= current_rowan_balance - slip_cost and balance <= current_rowan_balance + slip_cost) current_rowan_balance = balance balance = test_utilities.get_sifchain_addr_balance( sifaddress, basic_transfer_request.sifnodecli_node, sifchain_symbol) slip_cost = (slip_pct * current_coin_balance) assert (balance >= current_coin_balance - slip_cost and balance <= current_coin_balance + slip_cost) current_coin_balance = balance # check for failure if we try to add too much liquidity basic_transfer_request.amount = 10**19 txn = add_pool_liquidity(basic_transfer_request, credentials) assert (txn["code"] == 25) get_pools(basic_transfer_request.sifnodecli_node) current_rowan_balance = current_rowan_balance - sifchain_fees assert (test_utilities.get_sifchain_addr_balance( sifaddress, basic_transfer_request.sifnodecli_node, "rowan") == current_rowan_balance) assert (test_utilities.get_sifchain_addr_balance( sifaddress, basic_transfer_request.sifnodecli_node, sifchain_symbol) == current_coin_balance) # check for failure if we try to remove too much liquidity, only occurs now if not 100% liquidity provider """ txn = remove_pool_liquidity(basic_transfer_request, credentials, 10000) assert(txn["code"] == 3) get_pools(basic_transfer_request.sifnodecli_node) current_rowan_balance = current_rowan_balance - sifchain_fees assert(test_utilities.get_sifchain_addr_balance(sifaddress, basic_transfer_request.sifnodecli_node, "rowan") == current_rowan_balance) assert(test_utilities.get_sifchain_addr_balance(sifaddress, basic_transfer_request.sifnodecli_node, sifchain_symbol) == current_coin_balance) """ # check for failure if we try to swap too much for user txn = swap_pool(basic_transfer_request, "rowan", sifchain_symbol, credentials) assert (txn["code"] == 27) get_pools(basic_transfer_request.sifnodecli_node) current_rowan_balance = current_rowan_balance - sifchain_fees assert (test_utilities.get_sifchain_addr_balance( sifaddress, basic_transfer_request.sifnodecli_node, "rowan") == current_rowan_balance) assert (test_utilities.get_sifchain_addr_balance( sifaddress, basic_transfer_request.sifnodecli_node, sifchain_symbol) == current_coin_balance) # check for failure if we try to swap too much for pool basic_transfer_request.amount = 5 * 10**17 txn = swap_pool(basic_transfer_request, "rowan", sifchain_symbol, credentials) assert (txn["code"] == 31) get_pools(basic_transfer_request.sifnodecli_node) current_rowan_balance = current_rowan_balance - sifchain_fees assert (test_utilities.get_sifchain_addr_balance( sifaddress, basic_transfer_request.sifnodecli_node, "rowan") == current_rowan_balance) assert (test_utilities.get_sifchain_addr_balance( sifaddress, basic_transfer_request.sifnodecli_node, sifchain_symbol) == current_coin_balance) # now try to do a swap that works change_amount = 10**15 basic_transfer_request.amount = change_amount txn = swap_pool(basic_transfer_request, "rowan", sifchain_symbol, credentials) # TODO: compute this precisely? slip_pct = 0.01 balance = test_utilities.get_sifchain_addr_balance( sifaddress, basic_transfer_request.sifnodecli_node, "rowan") assert (balance < current_rowan_balance) current_rowan_balance = balance balance = test_utilities.get_sifchain_addr_balance( sifaddress, basic_transfer_request.sifnodecli_node, sifchain_symbol) assert (balance > current_coin_balance) current_coin_balance = balance
def test_can_create_a_new_token_with_a_7_char_name_and_peg_it(): new_account_key = ("a" + get_shell_output("uuidgen").replace("-", ""))[:7] amount = amount_in_wei(9) new_currency = create_new_currency(amount, new_account_key) (request1, _) = build_request(new_currency, amount) burn_lock_functions.transfer_ethereum_to_sifchain(request1, 10)
def test_cannot_create_two_currencies_with_the_same_name(): new_account_key = get_shell_output("uuidgen").replace("-", "") create_new_currency(amount_in_wei(10), new_account_key) with pytest.raises(Exception): create_new_currency(amount_in_wei(10), new_account_key)
def test_cannot_create_two_currencies_that_only_differ_in_capitalization(): new_account_key = get_shell_output("uuidgen").replace("-", "").lower() create_new_currency(amount_in_wei(10), new_account_key) with pytest.raises(Exception): create_new_currency(amount_in_wei(10), new_account_key.upper())
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))