def test_client_error(): with pytest.raises(libra_client.client.LibraNetError): libra_client.Client("xnet") with pytest.raises(libra_client.client.LibraNetError): libra_client.Client("mainnet") with pytest.raises(FileNotFoundError): libra_client.Client.new("localhost:8000", "non_exsits_file")
def test_get_transaction(): c = libra_client.Client("testnet") stx = c.get_transaction(1, True) assert stx.transaction.type == 'blockmetadata' assert stx.vm_status == 4001 assert stx.success is True assert stx.gas_used == 600
def test_create_account_and_rotate_key(): if not TESTNET_LOCAL: return wallet = libra_client.WalletLibrary.recover('test/test.wallet') assert wallet.child_count == 2 a0 = wallet.accounts[0] c = libra_client.Client("testnet") wallet2 = libra_client.WalletLibrary.new() account = wallet2.new_account() address = account.address with pytest.raises(libra_client.client.AccountError): c.get_account_state(address) c.create_account(a0, address) account_resource = c.get_account_resource(address) assert account_resource.balance == 0 assert account_resource.sequence_number == 0 assert account_resource.sent_events.count == 0 assert account_resource.received_events.count == 0 assert Address.equal_address(account_resource.authentication_key, address) c.rotate_authentication_key(account, a0.public_key) account_resource = c.get_account_resource(address) assert account_resource.balance == 0 assert account_resource.sequence_number == 1 assert account_resource.sent_events.count == 0 assert account_resource.received_events.count == 0 assert bytes(account_resource.authentication_key) == a0.public_key
def test_client_testnet(): c2 = libra_client.Client("testnet") if 'TESTNET_LOCAL' in os.environ: return assert c2.url == "https://client.testnet.libra.org" assert c2.verbose == True assert c2.faucet_account is None
def test_get_tx_latest(): c = libra_client.Client("testnet") ver = c.get_latest_transaction_version() if ver == 1: return transactions = c.get_transactions(ver - 2, 2, True) assert len(transactions) == 2
def test_get_account_transaction(): address = libra.AccountConfig.association_address() c = libra_client.Client("testnet") txn = c.get_account_transaction(address, 0, True) if txn is None: return usecs = txn.transaction.expiration_time assert usecs // 1000_000 > 1570_000_000
def test_events_received(): address = libra.AccountConfig.association_address() c = libra_client.Client("testnet") events = c.get_events_received(address, 0, limit=1) if len(events) == 0: return assert len(events) == 1 assert events[0].transaction_version >= 0
def test_ledger_time(capsys): # output = exec_input("ledger time", capsys) from libra_client.cli.ledger_cmds import LedgerCmdTime client = libra_client.Client("testnet") LedgerCmdTime().execute(client, {}) output = capsys.readouterr().out assert 'start_time' in output assert 'latest_time' in output
def test_get_transactions3(): c = libra_client.Client("testnet") start_version = 0 txs = c.get_transactions(start_version, limit=3, include_events=True) assert len(txs) == 3 for i, tx in enumerate(txs): assert i == tx.version assert tx.gas_used >= 0
def test_get_transaction_invalid(): client = libra_client.Client("testnet") with pytest.raises(LibraError): client.get_transaction(-1) tx = client.get_transaction(Uint64.max_value) assert tx is None with pytest.raises(LibraError): client.get_transaction(Uint64.max_value + 1)
def test_timeout(): c = libra_client.Client("testnet") c.timeout = 0.001 with pytest.raises(Exception) as excinfo: stx = c.get_transaction(1, True) error = excinfo.value assert "timeout" in error.__str__().lower( ) or "ConnectionError" in error.__str__()
def test_amount_zero(): wallet = libra_client.WalletLibrary.recover('test/test.wallet') a0 = wallet.accounts[0] a1 = wallet.accounts[1] c = libra_client.Client("testnet") try: ret = c.transfer_coin(a0, a1.address, 0, is_blocking=True) except LibraError as ae: pass
def test_query(): c = libra_client.Client("testnet") with pytest.raises(LibraError): txs = c.get_transactions(1, "1") with pytest.raises(LibraError): txs = c.get_transactions("1", 1) with pytest.raises(LibraError): txs = c.get_transactions(1, "1", False) with pytest.raises(LibraError): c.get_transactions(1, True)
def test_transfer_to_self(): return wallet = libra_client.WalletLibrary.recover('test/test.wallet') a0 = wallet.accounts[0] c = libra_client.Client("testnet") balance = c.get_balance(a0.address) if balance == 0: c.mint_coins(a0.address, 1000000, is_blocking=True) ret = c.transfer_coin(a0, a0.address, 1, is_blocking=True) stx = c.get_account_transaction(ret.raw_txn.sender, ret.raw_txn.sequence_number, True) assert proto.version > 1 assert len(proto.events.events) == 2 assert proto.proof.transaction_info.major_status == 4001 assert balance == c.get_balance(a0.address)
def test_amount_illegal(): wallet = libra_client.WalletLibrary.recover('test/test.wallet') a0 = wallet.accounts[0] a1 = wallet.accounts[1] c = libra_client.Client("testnet") sequence_number = c.get_sequence_number(a0.address) balance0 = c.get_balance(a0.address) with pytest.raises(Exception): c.transfer_coin(a0, a1.address, -1) with pytest.raises(Exception): c.transfer_coin(a0, a1.address, 0.1) try: c.transfer_coin(a0, a1.address, balance0 + 99999999, is_blocking=False) c.wait_for_transaction(a0.address, sequence_number) #no events emitted except LibraError as mpe: pass
def test_get_account_resource(): address = libra.AccountConfig.association_address() c = libra_client.Client("testnet") ret = c.get_account_resource(address) assert len(ret.authentication_key) == 32 * 2 #hex assert ret.delegated_key_rotation_capability == False assert ret.delegated_withdrawal_capability == False assert len(ret.received_events_key) == libra.event.EVENT_KEY_LENGTH * 2 assert len(ret.sent_events_key) == libra.event.EVENT_KEY_LENGTH * 2 assert ret.sequence_number > 0 balance = c.get_balance(address) assert balance >= 0 addr = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\nU\x0c\x18' assert addr == bytes.fromhex(address) ret2 = c.get_account_resource(addr) assert ret.delegated_withdrawal_capability == ret2.delegated_withdrawal_capability assert ret2.sequence_number >= ret.sequence_number
def test_transfer_coin(): wallet = libra_client.WalletLibrary.new() a0 = wallet.new_account() a1 = wallet.new_account() c = libra_client.Client("testnet") c.mint_coins(a0.address, a0.auth_key_prefix, 1234_000, is_blocking=True) balance0 = c.get_balance(a0.address, retry=True) c.create_account(a0, a1.address, a1.auth_key_prefix, is_blocking=True) balance1 = c.get_balance(a1.address, retry=True) ret = c.transfer_coin(a0, a1.address, 123, gas_unit_price=1, is_blocking=True) assert bytes(ret.raw_txn.sender) == a0.address assert ret.raw_txn.sequence_number == 0 assert c.get_balance(a0.address) <= balance0 - 123 assert c.get_balance(a1.address) == balance1 + 123
def test_invalid_signature(): wallet = libra_client.WalletLibrary.recover('test/test.wallet') a0 = wallet.accounts[0] a1 = wallet.accounts[1] raw_tx = RawTransaction._gen_transfer_transaction(a0.address, 0, a1.address, 123) stx = SignedTransaction.gen_from_raw_txn(raw_tx, a0) stx.signature = b'\0' * 64 client = libra_client.Client("testnet") with pytest.raises(Exception) as excinfo: client.submit_signed_txn(stx) #libra_client.error.VMError: (3, 'SEQUENCE_NUMBER_TOO_OLD') return #TODO: This maybe libra's bug, should return INVALID_SIGNATURE error. with pytest.raises(libra_client.VMError) as excinfo: client.submit_signed_txn(stx) vm_error = excinfo.value assert vm_error.args == (1, 'INVALID_SIGNATURE') assert vm_error.error_code == 1 assert vm_error.error_msg == 'INVALID_SIGNATURE'
def test_gax_too_large(): wallet = libra_client.WalletLibrary.recover('test/test.wallet') a0 = wallet.accounts[0] a1 = wallet.accounts[1] c = libra_client.Client("testnet") try: balance0 = c.get_balance(a0.address) except AccountError: balance0 = 0 with pytest.raises(LibraError): c.transfer_coin(a0, a1.address, 1, gas_unit_price=balance0) with pytest.raises(LibraError): c.transfer_coin(a0, a1.address, 1, max_gas_amount=1_000_000_001) with pytest.raises(LibraError): c.transfer_coin(a0, a1.address, 1, max_gas_amount=balance0 + 1, gas_unit_price=10000)
def test_transfer_with_metadata(): wallet = libra_client.WalletLibrary.recover('test/test.wallet') a0 = wallet.accounts[0] a1 = wallet.accounts[1] client = libra_client.Client("testnet") balance = client.get_balance(a0.address) if balance == 0: client.mint_coins(a0.address, a0.auth_key_prefix, 1000000, is_blocking=True) try: client.create_account(a0, a1.address, a1.auth_key_prefix, is_blocking=True) except libra_client.error.VMError as err: if err.error_code == 4016: pass else: raise except Exception: if not 'TESTNET_LOCAL' in os.environ: return else: raise ret = client.transfer_coin(a0, a1.address, 1, metadata=bytes([3, 4, 5]), is_blocking=True) script = ret.raw_txn.payload.value assert script.code == Script.get_script_bytecode( "peer_to_peer_with_metadata") assert bytes(script.args[0].value) == a1.address assert script.args[1].value == 1 assert script.args[2].value == bytes([3, 4, 5]) tx = client.get_account_transaction(ret.raw_txn.sender, ret.raw_txn.sequence_number, True) assert tx.version > 1
def test_execute_script_on_testnet(capsys): if TESTNET_LOCAL: #TODO: why should sleep some seconds to avoid MempoolError import time time.sleep(1) client = libra_client.Client("testnet") wallet = libra_client.WalletLibrary.recover('test/test.wallet') assert wallet.child_count == 2 a0 = wallet.accounts[0] balance = client.get_balance(a0.address_hex) if balance <= 1: try: client.mint_coins(a0.address_hex, a0.auth_key_prefix, 9999999, True) except Exception: params = { "receiver_account_address": a0.address_hex, "number_of_micro_libra": 9 } import requests requests.post("http://apitest.moveonlibra.com/v1/transactions/mint_mol", data=params) addr1 = wallet.accounts[1].address.hex() addr1_prefix = 'b"'+wallet.accounts[1].auth_key_prefix.hex()+'"' output = exec_input(f"dev e 0 test/peer_to_peer.mv {addr1} {addr1_prefix} 1", capsys) assert 'Compiling program' in output return #TODO: peer_to_peer need type parameter which is not support in libra shell. if TESTNET_LOCAL: if "MempoolError" in output: pass # assert "Failed to update gas price to 0" in output else: assert "Successfully finished execution" in output else: seq = client.get_sequence_number(a0.address_hex) #should get seq by submited transaction, above will get error seq in concurrent env. client.wait_for_transaction(a0.address_hex, seq-1) balance2 = client.get_balance(addr1) assert balance2 >= 0
def test_get_balance(): address = libra.AccountConfig.association_address() c = libra_client.Client("testnet") balance = c.get_balance(address) assert balance >= 0
def test_invalid_param(): c = libra_client.Client("testnet") with pytest.raises(libra_client.LibraError) as excinfo: c.json_rpc("get_transactions", []) err = excinfo.value assert err.args[0]['error']['code']
def test_account_not_exsits(): address = "7af57fff206fbcc846532f75f373b5d1db9333308dbc4673c5befbca5db60e21"[: 32] c = libra_client.Client("testnet") with pytest.raises(libra_client.client.AccountError): balance = c.get_account_state(address)
def test_get_transaction_without_events(): c = libra_client.Client("testnet") transactions = c.get_transactions(1, 1, False) assert len(transactions) == 1
def test_get_tx_from_zero(): c = libra_client.Client("testnet") transactions = c.get_transactions(0, 2, True) assert len(transactions) == 2
def test_no_blob_of_non_exsits_address(): if not TESTNET_LOCAL: return with pytest.raises(libra_client.client.AccountError): libra_client.Client("testnet").get_account_state(Address.random())
def test_get_tx_invalid(): c = libra_client.Client("testnet") with pytest.raises(LibraError): c.get_transactions(1, -1, True)
def test_get_latest_transaction_version(): c = libra_client.Client("testnet") ver = c.get_latest_transaction_version() assert ver > 0
def test_get_account_transaction_non_exists(): address = libra.AccountConfig.association_address() c = libra_client.Client("testnet") txn = c.get_account_transaction(address, Uint64.max_value, True) assert txn is None