def test_client_init(): c = libra.Client.new("localhost","8080", "libra/consensus_peers.config.toml") assert c.host == "localhost" assert c.port == 8080 assert hasattr(c, "faucet_host") == False assert c.verbose == True assert c.faucet_account is not None assert len(c.validator_verifier.validators) > 0 address, key = c.validator_verifier.validators.popitem() assert len(address) == libra.account_address.ADDRESS_LENGTH assert len(key._key) == 32 c2 = libra.Client("testnet") assert c2.host == "ac.testnet.libra.org" assert c2.port == 8000 assert c2.faucet_host == "faucet.testnet.libra.org" assert c2.verbose == True assert c2.faucet_account is None assert len(c2.validator_verifier.validators) > 0 with pytest.raises(libra.LibraNetError): libra.Client("xnet") with pytest.raises(libra.LibraNetError): libra.Client("mainnet") with pytest.raises(TypeError): libra.Client.new("localhost", 8000) with pytest.raises(FileNotFoundError): libra.Client.new("localhost", 8000, "non_exsits_file")
def test_get_transaction(): c = libra.Client("testnet") txn = c.get_transaction(1) assert len(txn.signed_txn) > 0 stx = SignedTransaction.deserialize(txn.signed_txn) assert bytes( stx.raw_txn.sender).hex() == libra.AccountConfig.association_address() assert stx.raw_txn.sequence_number == 1 assert stx.raw_txn.payload.index == 2 assert stx.raw_txn.payload.Script == True assert stx.raw_txn.payload.value.code == RawTransaction.get_script_bytecode( "mint") assert stx.raw_txn.payload.value.args[0].index == 1 assert stx.raw_txn.payload.value.args[0].Address == True assert stx.raw_txn.payload.value.args[1].index == 0 assert stx.raw_txn.payload.value.args[1].U64 == True assert stx.raw_txn.payload.value.args[1].value == 999999000000 assert stx.raw_txn.max_gas_amount == 140000 assert stx.raw_txn.gas_unit_price == 0 assert stx.raw_txn.expiration_time > 1_568_000_000 assert stx.raw_txn.expiration_time < 11_568_000_000 assert len(stx.public_key) == 32 assert len(stx.signature) == 64 raw_txn_bytes = stx.raw_txn.serialize() raw_txn_hash = libra.Client.raw_tx_hash(raw_txn_bytes) libra.Client.verify_transaction(raw_txn_hash, stx.public_key, stx.signature)
def test_merkle_tree_accumulator_invariants(): c = libra.Client("testnet") request, resp = c._get_txs(1, 2, False) version = resp.ledger_info_with_sigs.ledger_info.version txn_list = resp.response_items[ 0].get_transactions_response.txn_list_with_proof first = txn_list.proof_of_first_transaction.non_default_siblings last = txn_list.proof_of_last_transaction.non_default_siblings assert len(first) == len(last) assert 2**len(first) > version assert 2**(len(first) - 1) < version common = 0 for v1, v2 in zip(first, last): if v1 == v2: common += 1 else: break assert len(first) - common == 2 hash1 = TransactionInfo.from_proto(txn_list.infos[0]).hash() hash0 = first[-1] hasher = TransactionAccumulatorHasher() hash01 = MerkleTreeInternalNode(hash0, hash1, hasher).hash() assert hash01 == last[-2] hash2 = TransactionInfo.from_proto(txn_list.infos[1]).hash() hash3 = last[-1] hasher = TransactionAccumulatorHasher() hash23 = MerkleTreeInternalNode(hash2, hash3, hasher).hash() assert hash23 == first[-2] root_hash = hash01 for item in reversed(first[0:-1]): hasher = TransactionAccumulatorHasher() root_hash = MerkleTreeInternalNode(root_hash, item, hasher).hash() assert root_hash == resp.ledger_info_with_sigs.ledger_info.transaction_accumulator_hash
def test_get_tx_latest(): c = libra.Client("testnet") ver = c.get_latest_transaction_version() transactions, events_for_versions = c.get_transactions_proto( ver - 2, 2, True) assert len(transactions) == 2 assert len(events_for_versions.events_for_version) == 2
def test_get_tx_from_zero(): c = libra.Client("testnet") transactions, events_for_versions = c.get_transactions_proto(0, 2, True) assert len(transactions) == 2 assert len(events_for_versions.events_for_version) == 2 transactions, events_for_versions = c.get_transactions_proto(0, 1, True) assert len(transactions) == 1 assert len(events_for_versions.events_for_version) == 1 assert len(events_for_versions.events_for_version[0].events) == 0
def test_wait_for_transaction_timeout(): wallet = libra.WalletLibrary.recover('test/test.wallet') a0 = wallet.accounts[0] a1 = wallet.accounts[1] c = libra.Client("testnet") diff = c._get_time_diff() if diff < 0: with pytest.raises(TransactionTimeoutError): c.transfer_coin(a0, a1.address, 1, unit_price=0, is_blocking=True, txn_expiration=0)
def test_get_account_transaction(): address = libra.AccountConfig.association_address() c = libra.Client("testnet") txn = c.get_account_transaction(address, 1, True) assert txn.events.events[0].sequence_number == 1 assert len(txn.signed_transaction.signed_txn) > 0 assert txn.version > 0 assert txn.proof.HasField("ledger_info_to_transaction_info_proof") assert txn.proof.HasField("transaction_info")
def test_mint(): address = "7af57a0c206fbcc846532f75f373b5d1db9333308dbc4673c5befbca5db60e20" c = libra.Client("testnet") try: balance = c.get_balance(address) except libra.client.AccountError: balance = 0 c.mint_coins_with_faucet_service(address, 12345, True) balance2 = c.get_balance(address) assert (balance2 - balance) % 12345 == 0 # tolerate parallel mint
def test_get_account_transaction_proto(): address = libra.AccountConfig.association_address() c = libra.Client("testnet") txn, usecs = c.get_account_transaction_proto(address, 1, True) len(str(usecs)) == 16 assert usecs // 1000_000 > 1570_000_000 assert txn.events.events[0].sequence_number == 1 assert len(txn.signed_transaction.signed_txn) > 0 assert txn.version > 0 assert txn.proof.HasField("ledger_info_to_transaction_info_proof") assert txn.proof.HasField("transaction_info")
def test_execute_script_on_testnet(capsys): c = libra.Client("testnet") wallet = libra.WalletLibrary.recover('test/test.wallet') assert wallet.child_count == 2 a0 = wallet.accounts[0] balance = c.get_balance(a0.address_hex) if balance < 1: c.mint_coins_with_faucet_service(a0.address_hex, 9999999, True) addr1 = wallet.accounts[1].address_hex output = exec_input(f"dev e 0 transaction_scripts/peer_to_peer_transfer.mv {addr1} 1", capsys) assert 'Compiling program' in output
def test_gax_too_large(): wallet = libra.WalletLibrary.recover('test/test.wallet') a0 = wallet.accounts[0] a1 = wallet.accounts[1] c = libra.Client("testnet") balance0 = c.get_balance(a0.address) with pytest.raises(TransactionError): c.transfer_coin(a0, a1.address, 1, unit_price=balance0) with pytest.raises(TransactionError): c.transfer_coin(a0, a1.address, 1, max_gas=1_000_001) with pytest.raises(TransactionError): c.transfer_coin(a0, a1.address, 1, max_gas=balance0+1, unit_price=10000)
def test_amount_illegal(): wallet = libra.WalletLibrary.recover('test/test.wallet') a0 = wallet.accounts[0] a1 = wallet.accounts[1] c = libra.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) c.transfer_coin(a0, a1.address, balance0+1, is_blocking=False) assert False == c.wait_for_transaction(a0.address, sequence_number) #no events emitted
def test_ledger_info(): c = libra.Client("testnet") info = c.get_latest_ledger_info() assert info.version > 0 assert len(info.transaction_accumulator_hash) == 32 assert len(info.consensus_data_hash) == 32 assert len(info.consensus_block_id) == 32 assert info.timestamp_usecs > 1570_000_000_000_000 secs = info.timestamp_usecs / 1000_000 localtime = datetime.now().timestamp() diff = localtime - secs if diff != 0: print(f"localtime {localtime}, ledger_info time{secs}, diff:{diff}") assert abs(diff) < 5
def test_transfer_coin(): wallet = libra.WalletLibrary.new() a0 = wallet.new_account() a1 = wallet.new_account() c = libra.Client("testnet") c.mint_coins_with_faucet_service(a0.address.hex(), 1234, True) balance0 = c.get_balance(a0.address) try: balance1 = c.get_balance(a1.address) except libra.client.AccountError: balance1 = 0 ret = c.transfer_coin(a0, a1.address, 1234, unit_price=0, is_blocking=True) assert ret.ac_status.code == libra.proto.admission_control_pb2.AdmissionControlStatusCode.Accepted assert c.get_balance(a0.address) == balance0 - 1234 assert c.get_balance(a1.address) == balance1 + 1234
def test_transfer_coin(): wallet = libra.WalletLibrary.new() a0 = wallet.new_account() a1 = wallet.new_account() c = libra.Client("testnet") c.mint_coins_with_faucet_service(a0.address.hex(), 1234, True) balance0 = c.get_balance(a0.address) try: balance1 = c.get_balance(a1.address) except libra.client.AccountError: balance1 = 0 ret = c.transfer_coin(a0, a1.address, 1234, unit_price=0, 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 - 1234 assert c.get_balance(a1.address) == balance1 + 1234
def test_get_account_resource(): address = libra.AccountConfig.association_address() c = libra.Client("testnet") ret = c.get_account_resource(address) assert len(ret.authentication_key) == 32 assert ret.balance > 0 assert ret.delegated_key_rotation_capability == False assert ret.delegated_withdrawal_capability == False assert ret.received_events.count > 0 assert len(ret.received_events.key) == 32 assert ret.sent_events.count > 0 assert len(ret.sent_events.key) == 32 assert ret.sequence_number > 0 addr = b'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\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.received_events.count >= ret.received_events.count assert ret2.sent_events.count >= ret.sent_events.count assert ret2.sequence_number >= ret.sequence_number
def test_get_transaction(): c = libra.Client("testnet") stx = c.get_transaction(1) assert bytes(stx.raw_txn.sender).hex() == libra.AccountConfig.association_address() assert stx.raw_txn.sequence_number == 1 assert stx.raw_txn.payload.index == 2 assert stx.raw_txn.payload.Script == True assert stx.raw_txn.payload.value.code == Script.get_script_bytecode("mint") assert stx.raw_txn.payload.value.args[0].index == 1 assert stx.raw_txn.payload.value.args[0].Address == True assert stx.raw_txn.payload.value.args[1].index == 0 assert stx.raw_txn.payload.value.args[1].U64 == True assert stx.raw_txn.payload.value.args[1].value == 100000000 assert stx.raw_txn.max_gas_amount == 140000 assert stx.raw_txn.gas_unit_price == 0 assert stx.raw_txn.expiration_time > 1_568_000_000 assert stx.raw_txn.expiration_time < 11_568_000_000 assert len(stx.public_key) == 32 assert len(stx.signature) == 64 stx.check_signature stx.__str__()
def bytes_to_int_list(bytes_str): tp = struct.unpack("<{}B".format(len(bytes_str)), bytes_str) return list(tp) def hex_to_int_list(hex_str): return bytes_to_int_list(bytes.fromhex(hex_str)) # import sys # for line in sys.stdin: # nextTxnId = int(line) c = libra.Client("testnet") # mint txn and blockmetadata # nextTxnId = 62880 # nextTxnId = 384945 # mint # nextTxnId = 68943 # p2p # nextTxnId = 234834 # nextTxnId = 242402 # nextTxnId = 8209738 # nextTxnId = 8230240 # nextTxnId = 9295591
def test_get_balance(): address = libra.AccountConfig.association_address() c = libra.Client("testnet") balance = c.get_balance(address) assert balance > 0
def test_get_latest_transaction_version(): c = libra.Client("testnet") ver = c.get_latest_transaction_version() assert ver > 0
def test_get_tx_invalid(): c = libra.Client("testnet") with pytest.raises(ValueError): c.get_transactions_proto(1, -1, True)
def test_events(): address = libra.AccountConfig.association_address() c = libra.Client("testnet") events = c.get_latest_events_sent(address, 2) #pdb.set_trace() assert len(events) == 2
def test_get_tx_zero(): c = libra.Client("testnet") transactions, events_for_versions = c.get_transactions_proto(1, 0, True) assert len(transactions) == 0
def test_get_tx_with_events(): c = libra.Client("testnet") transactions, events_for_versions = c.get_transactions_proto(1, 2, True) assert len(transactions) == 2 assert len(events_for_versions.events_for_version) == 2
def test_account_not_exsits(): address = "7af57a0c206fbcc846532f75f373b5d1db9333308dbc4673c5befbca5db60e21" c = libra.Client("testnet") with pytest.raises(libra.client.AccountError): balance = c.get_balance(address)