You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
145 lines
4.9 KiB
145 lines
4.9 KiB
5 years ago
|
import pytest
|
||
|
import requests
|
||
|
|
||
|
from pyhmy import (
|
||
|
blockchain
|
||
|
)
|
||
|
|
||
|
from pyhmy.rpc import (
|
||
|
exceptions
|
||
|
)
|
||
|
|
||
|
|
||
|
test_epoch_number = 0
|
||
|
genesis_block_number = 0
|
||
|
test_block_number = 1
|
||
|
test_block_hash = None
|
||
|
|
||
|
def _test_blockchain_rpc(fn, *args, **kwargs):
|
||
|
if not callable(fn):
|
||
|
pytest.fail(f'Invalid function: {fn}')
|
||
|
|
||
|
try:
|
||
|
response = fn(*args, **kwargs)
|
||
|
except Exception as e:
|
||
|
if isinstance(e, exceptions.RPCError) and 'does not exist/is not available' in str(e):
|
||
|
pytest.skip(f'{str(e)}')
|
||
|
pytest.fail(f'Unexpected error: {e.__class__} {e}')
|
||
|
return response
|
||
|
|
||
|
@pytest.mark.run(order=1)
|
||
|
def test_get_node_metadata(setup_blockchain):
|
||
5 years ago
|
metadata = _test_blockchain_rpc(blockchain.get_node_metadata)
|
||
|
assert isinstance(metadata, dict)
|
||
5 years ago
|
|
||
|
@pytest.mark.run(order=2)
|
||
|
def test_get_sharding_structure(setup_blockchain):
|
||
5 years ago
|
sharding_structure = _test_blockchain_rpc(blockchain.get_sharding_structure)
|
||
|
assert isinstance(sharding_structure, list)
|
||
|
assert len(sharding_structure) > 0
|
||
5 years ago
|
|
||
|
@pytest.mark.run(order=3)
|
||
|
def test_get_leader_address(setup_blockchain):
|
||
5 years ago
|
leader = _test_blockchain_rpc(blockchain.get_leader_address)
|
||
|
assert isinstance(leader, str)
|
||
|
assert 'one1' in leader
|
||
5 years ago
|
|
||
|
@pytest.mark.run(order=4)
|
||
|
def test_get_block_number(setup_blockchain):
|
||
5 years ago
|
current_block_number = _test_blockchain_rpc(blockchain.get_block_number)
|
||
|
assert isinstance(current_block_number, int)
|
||
5 years ago
|
|
||
|
@pytest.mark.run(order=5)
|
||
|
def test_get_current_epoch(setup_blockchain):
|
||
5 years ago
|
current_epoch = _test_blockchain_rpc(blockchain.get_current_epoch)
|
||
|
assert isinstance(current_epoch, int)
|
||
5 years ago
|
|
||
|
@pytest.mark.run(order=6)
|
||
|
def tset_get_gas_price(setup_blockchain):
|
||
5 years ago
|
gas = _test_blockchain_rpc(blockchain.get_gas_price)
|
||
|
assert isinstance(gas, int)
|
||
5 years ago
|
|
||
|
@pytest.mark.run(order=7)
|
||
|
def test_get_num_peers(setup_blockchain):
|
||
5 years ago
|
peers = _test_blockchain_rpc(blockchain.get_num_peers)
|
||
|
assert isinstance(peers, int)
|
||
5 years ago
|
|
||
|
@pytest.mark.run(order=8)
|
||
|
def test_get_latest_header(setup_blockchain):
|
||
5 years ago
|
header = _test_blockchain_rpc(blockchain.get_latest_header)
|
||
|
assert isinstance(header, dict)
|
||
5 years ago
|
|
||
|
@pytest.mark.run(order=9)
|
||
|
def test_get_latest_headers(setup_blockchain):
|
||
5 years ago
|
header_pair = _test_blockchain_rpc(blockchain.get_latest_headers)
|
||
|
assert isinstance(header_pair, dict)
|
||
5 years ago
|
|
||
|
@pytest.mark.run(order=10)
|
||
|
def test_get_block_by_number(setup_blockchain):
|
||
|
global test_block_hash
|
||
|
block = _test_blockchain_rpc(blockchain.get_block_by_number, test_block_number)
|
||
5 years ago
|
assert isinstance(block, dict)
|
||
|
assert 'hash' in block.keys()
|
||
5 years ago
|
test_block_hash = block['hash']
|
||
|
|
||
|
@pytest.mark.run(order=11)
|
||
|
def test_get_block_by_hash(setup_blockchain):
|
||
|
if not test_block_hash:
|
||
|
pytest.skip('Failed to get reference block hash')
|
||
5 years ago
|
block = _test_blockchain_rpc(blockchain.get_block_by_hash, test_block_hash)
|
||
|
assert isinstance(block, dict)
|
||
5 years ago
|
|
||
|
@pytest.mark.run(order=12)
|
||
|
def test_get_block_transaction_count_by_number(setup_blockchain):
|
||
5 years ago
|
tx_count = _test_blockchain_rpc(blockchain.get_block_transaction_count_by_number, test_block_number)
|
||
|
assert isinstance(tx_count, int)
|
||
5 years ago
|
|
||
|
@pytest.mark.run(order=13)
|
||
|
def test_get_block_transaction_count_by_hash(setup_blockchain):
|
||
|
if not test_block_hash:
|
||
|
pytest.skip('Failed to get reference block hash')
|
||
5 years ago
|
tx_count = _test_blockchain_rpc(blockchain.get_block_transaction_count_by_hash, test_block_hash)
|
||
|
assert isinstance(tx_count, int)
|
||
5 years ago
|
|
||
|
@pytest.mark.run(order=14)
|
||
|
def test_get_blocks(setup_blockchain):
|
||
5 years ago
|
blocks = _test_blockchain_rpc(blockchain.get_blocks, genesis_block_number, test_block_number)
|
||
|
assert isinstance(blocks, list)
|
||
|
assert len(blocks) == (test_block_number - genesis_block_number + 1)
|
||
5 years ago
|
|
||
|
@pytest.mark.run(order=15)
|
||
|
def test_get_block_signers(setup_blockchain):
|
||
5 years ago
|
block_signers = _test_blockchain_rpc(blockchain.get_block_signers, test_block_number)
|
||
|
assert isinstance(block_signers, list)
|
||
|
assert len(block_signers) > 0
|
||
5 years ago
|
|
||
|
@pytest.mark.run(order=16)
|
||
|
def test_get_validators(setup_blockchain):
|
||
5 years ago
|
validators = _test_blockchain_rpc(blockchain.get_validators, test_epoch_number)
|
||
|
assert isinstance(validators, dict)
|
||
|
assert 'validators' in validators.keys()
|
||
|
assert len(validators['validators']) > 0
|
||
5 years ago
|
|
||
|
@pytest.mark.run(order=17)
|
||
|
def test_get_shard(setup_blockchain):
|
||
|
shard = _test_blockchain_rpc(blockchain.get_shard)
|
||
5 years ago
|
assert isinstance(shard, int)
|
||
5 years ago
|
assert shard == 0
|
||
|
|
||
|
@pytest.mark.run(order=18)
|
||
|
def test_get_staking_epoch(setup_blockchain):
|
||
5 years ago
|
staking_epoch = _test_blockchain_rpc(blockchain.get_staking_epoch)
|
||
|
assert isinstance(staking_epoch, int)
|
||
5 years ago
|
|
||
|
@pytest.mark.run(order=19)
|
||
|
def test_get_prestaking_epoch(setup_blockchain):
|
||
5 years ago
|
prestaking_epoch = _test_blockchain_rpc(blockchain.get_prestaking_epoch)
|
||
|
assert isinstance(prestaking_epoch, int)
|
||
5 years ago
|
|
||
|
@pytest.mark.run(order=20)
|
||
|
def test_get_bad_blocks(setup_blockchain):
|
||
|
# TODO: Remove skip when RPC is fixed
|
||
|
pytest.skip("Known error with hmy_getCurrentBadBlocks")
|
||
5 years ago
|
bad_blocks = _test_blockchain_rpc(blockchain.get_bad_blocks)
|
||
|
assert isinstance(bad_blocks, list)
|