test: update tests to be independent of localnet

remove the requirement that tests must be run after `make test-rpc`. in
fact, these tests must be run independently of those.

test: document raw tx generation
pull/35/head
MaxMustermann2 2 years ago
parent eb322498c4
commit 2135d1050c
No known key found for this signature in database
GPG Key ID: 4F4AB9DB6FF24C94
  1. 3
      pytest.ini
  2. 578
      tests/GenerateRawTransactions.ipynb
  3. 4
      tests/cli-pyhmy/test_cli.py
  4. 4
      tests/numbers-pyhmy/test_numbers.py
  5. 3
      tests/request-pyhmy/test_request.py
  6. 166
      tests/sdk-pyhmy/conftest.py
  7. 26
      tests/sdk-pyhmy/test_account.py
  8. 44
      tests/sdk-pyhmy/test_blockchain.py
  9. 11
      tests/sdk-pyhmy/test_contract.py
  10. 35
      tests/sdk-pyhmy/test_staking.py
  11. 26
      tests/sdk-pyhmy/test_staking_signing.py
  12. 99
      tests/sdk-pyhmy/test_transaction.py
  13. 28
      tests/sdk-pyhmy/test_validator.py

@ -1,3 +1,2 @@
[pytest]
addopts = -v --showlocals
python_paths = .
addopts = -v --showlocals

@ -0,0 +1,578 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 105,
"id": "feee22ef",
"metadata": {},
"outputs": [],
"source": [
"from pyhmy import signing, staking_signing, numbers, transaction, account, validator as validator_module, staking_structures, contract\n",
"from web3 import Web3"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "55b8db60",
"metadata": {},
"outputs": [],
"source": [
"# we need five transactions in conftest\n",
"# simple transfer (from localnet address)\n",
"# contract creation (from second address)\n",
"# cross shard transfer (from second address)\n",
"# validator creation (from localnet address)\n",
"# delegation (from second address)"
]
},
{
"cell_type": "markdown",
"id": "e104724c",
"metadata": {},
"source": [
"### Simple Transfer"
]
},
{
"cell_type": "code",
"execution_count": 144,
"id": "d7fa35f8",
"metadata": {},
"outputs": [],
"source": [
"pk = \"1f84c95ac16e6a50f08d44c7bde7aff8742212fda6e4321fde48bf83bef266dc\"\n",
"tx = {\n",
" 'from': 'one155jp2y76nazx8uw5sa94fr0m4s5aj8e5xm6fu3',\n",
" # 3c86ac59f6b038f584be1c08fced78d7c71bb55d5655f81714f3cddc82144c65\n",
" 'to': 'one1ru3p8ff0wsyl7ncsx3vwd5szuze64qz60upg37',\n",
" 'gasPrice': Web3.toWei( 100, 'gwei' ),\n",
" 'gas': 21000,\n",
" 'chainId': 2, # localnet\n",
" 'value': int( numbers.convert_one_to_atto( 503 ) ),\n",
" 'nonce': 0,\n",
" 'shardID': 0,\n",
" 'toShardID': 0,\n",
"}\n",
"raw_tx = signing.sign_transaction(tx, pk).rawTransaction.hex()\n",
"tx_hash = transaction.send_raw_transaction(raw_tx)"
]
},
{
"cell_type": "code",
"execution_count": 145,
"id": "ed907d4b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0xf86f8085174876e8008252088080941f2213a52f7409ff4f103458e6d202e0b3aa805a891b4486fafde57c00008027a0d7c0b20207dcc9dde376822dc3f5625eac6f59a7526111695cdba3e29553ca17a05d4ca9a421ae16f89cbf6848186eaea7a800da732446dff9952e7c1e91d414e3\n",
"0xc26be5776aa57438bccf196671a2d34f3f22c9c983c0f844c62b2fb90403aa43\n"
]
}
],
"source": [
"print( raw_tx )\n",
"print( tx_hash )"
]
},
{
"cell_type": "markdown",
"id": "1bbee37b",
"metadata": {},
"source": [
"### Contract Creation"
]
},
{
"cell_type": "code",
"execution_count": 147,
"id": "b143507b",
"metadata": {},
"outputs": [],
"source": [
"pk = '3c86ac59f6b038f584be1c08fced78d7c71bb55d5655f81714f3cddc82144c65'\n",
"data = \"0x6080604052348015600f57600080fd5b50607780601d6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80634936cd3614602d575b600080fd5b604080516001815290519081900360200190f3fea2646970667358221220fa3fa0e8d0267831a59f4dd5edf39a513d07e98461cb06660ad28d4beda744cd64736f6c634300080f0033\"\n",
"tx = {\n",
" 'gasPrice': Web3.toWei( 100, 'gwei' ),\n",
" 'gas': 100000,\n",
" 'chainId': 2,\n",
" 'nonce': 0,\n",
" 'shardID': 0,\n",
" 'toShardID': 0,\n",
" 'data': data,\n",
"}\n",
"raw_tx = signing.sign_transaction(tx, pk).rawTransaction.hex()\n",
"tx_hash = transaction.send_raw_transaction(raw_tx)"
]
},
{
"cell_type": "code",
"execution_count": 148,
"id": "53dbcbff",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0xf8e88085174876e800830186a080808080b8946080604052348015600f57600080fd5b50607780601d6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80634936cd3614602d575b600080fd5b604080516001815290519081900360200190f3fea2646970667358221220fa3fa0e8d0267831a59f4dd5edf39a513d07e98461cb06660ad28d4beda744cd64736f6c634300080f003327a08bf26ee0120c296b17af507f62606abdb5c5f09a65642c3d30b349b8bfbb3d69a03ec7be51c615bcbf2f1d63f6eaa56cf8d7be81671717f90239619830a81ebc9f\n",
"0xa605852dd2fa39ed42e101c17aaca9d344d352ba9b24b14b9af94ec9cb58b31f\n"
]
}
],
"source": [
"print( raw_tx )\n",
"print( tx_hash )"
]
},
{
"cell_type": "code",
"execution_count": 130,
"id": "6e66392b",
"metadata": {},
"outputs": [],
"source": [
"contract_address = transaction.get_transaction_receipt( tx_hash ).get( 'contractAddress' )\n",
"deployed = contract.get_code( contract_address, 'latest' )"
]
},
{
"cell_type": "code",
"execution_count": 131,
"id": "ead2f9d4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0x6080604052348015600f57600080fd5b506004361060285760003560e01c80634936cd3614602d575b600080fd5b604080516001815290519081900360200190f3fea2646970667358221220fa3fa0e8d0267831a59f4dd5edf39a513d07e98461cb06660ad28d4beda744cd64736f6c634300080f0033\n",
"0x6080604052348015600f57600080fd5b50607780601d6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80634936cd3614602d575b600080fd5b604080516001815290519081900360200190f3fea2646970667358221220fa3fa0e8d0267831a59f4dd5edf39a513d07e98461cb06660ad28d4beda744cd64736f6c634300080f0033\n"
]
}
],
"source": [
"print( deployed )\n",
"print( data )"
]
},
{
"cell_type": "code",
"execution_count": 132,
"id": "453a34d6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3300f080003436c6f63746dc447adeb4d82da06660bc16489e70d315a93fde5dd4f95a1387620d8e0af3af0221228537660796462aef3f091002063009180915092518100615080406b5df080006b575d2064163dc63943608c10e065300067582060163400605b5df08000675f0065108432504060806x0\n",
"3300f080003436c6f63746dc447adeb4d82da06660bc16489e70d315a93fde5dd4f95a1387620d8e0af3af0221228537660796462aef3f091002063009180915092518100615080406b5df080006b575d2064163dc63943608c10e065300067582060163400605b5df08000675f0065108432504060806ef3f0006930006d10608770605b5df08000675f0065108432504060806x0\n"
]
}
],
"source": [
"print( \"\".join( [ deployed[ len( deployed ) - ( i + 1 ) ] for i in range( len( deployed ) ) ] ) )\n",
"print( \"\".join( [ data[ len( data ) - ( i + 1 ) ] for i in range( len( data ) ) ] ) )"
]
},
{
"cell_type": "code",
"execution_count": 133,
"id": "d251d1bf",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 133,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"0x6080604052348015600f57600080fd5b506004361060285760003560e01c80634936cd3614602d575b600080fd5b604080516001815290519081900360200190f3fea2646970667358221220fa3fa0e8d0267831a59f4dd5edf39a513d07e98461cb06660ad28d4beda744cd64736f6c634300080f0033\" == deployed"
]
},
{
"cell_type": "markdown",
"id": "812e033c",
"metadata": {},
"source": [
"### Cross Shard Transfer"
]
},
{
"cell_type": "code",
"execution_count": 149,
"id": "d7c70614",
"metadata": {},
"outputs": [],
"source": [
"pk = '3c86ac59f6b038f584be1c08fced78d7c71bb55d5655f81714f3cddc82144c65'\n",
"tx = {\n",
" 'from': 'one1ru3p8ff0wsyl7ncsx3vwd5szuze64qz60upg37',\n",
" 'gasPrice': Web3.toWei( 100, 'gwei' ),\n",
" 'gas': 21000,\n",
" 'chainId': 2,\n",
" 'nonce': 1,\n",
" 'shardID': 0,\n",
" 'toShardID': 1,\n",
" 'to': 'one1e8rdglh97t37prtnv7k35ymnh2wazujpzsmzes',\n",
" 'value': Web3.toWei( 100, 'gwei' ),\n",
"}\n",
"raw_tx = signing.sign_transaction(tx, pk).rawTransaction.hex()\n",
"tx_hash = transaction.send_raw_transaction(raw_tx)"
]
},
{
"cell_type": "code",
"execution_count": 150,
"id": "f20990f1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0xf86b0185174876e800825208800194c9c6d47ee5f2e3e08d7367ad1a1373ba9dd1724185174876e8008027a02501c517220e9499f14e97c20b0a88cd3b7ba80637bba43ed295422e69a3f300a079b8e1213c9506184aed6ac2eb0b2cb00594c3f9fcdd6c088937ce17fe47107c\n",
"0xf73ba634cb96fc0e3e2c9d3b4c91379e223741be4a5aa56e6d6caf49c1ae75cf\n"
]
}
],
"source": [
"print( raw_tx )\n",
"print( tx_hash )"
]
},
{
"cell_type": "code",
"execution_count": 153,
"id": "66f024b9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 153,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"account.get_balance( 'one1e8rdglh97t37prtnv7k35ymnh2wazujpzsmzes', 'http://localhost:9502' )"
]
},
{
"cell_type": "markdown",
"id": "2b2446df",
"metadata": {},
"source": [
"### Validator Creation"
]
},
{
"cell_type": "code",
"execution_count": 154,
"id": "c3513c37",
"metadata": {},
"outputs": [],
"source": [
"pk = \"1f84c95ac16e6a50f08d44c7bde7aff8742212fda6e4321fde48bf83bef266dc\"\n",
"address = \"one155jp2y76nazx8uw5sa94fr0m4s5aj8e5xm6fu3\"\n",
"info = {\n",
" \"name\": \"Alice\",\n",
" \"identity\": \"alice\",\n",
" \"website\": \"alice.harmony.one\",\n",
" \"security-contact\": \"Bob\",\n",
" \"details\": \"Are you even reading this?\",\n",
" \"min-self-delegation\": int( numbers.convert_one_to_atto( 10000 ) ),\n",
" \"max-total-delegation\": int( numbers.convert_one_to_atto( 100000 ) ),\n",
" \"rate\": \"0.1\",\n",
" \"max-rate\": \"0.9\",\n",
" \"max-change-rate\": \"0.05\",\n",
" \"bls-public-keys\": [\n",
" # private key is b1f2a5029f5f43c8c933a61ce936ced030b2c9379f8e2478fc888fa670cdbc89b8cd1ebc29b5b00a81d3152bb3aaa3a337404f50bee5e434430ca3693a94a1c102a765cf3b0887b8b0bcf5317d33f4bec60a97feae2498a39ab7a1c2\n",
" # blspass.txt is empty\n",
" \"0xa20e70089664a874b00251c5e85d35a73871531306f3af43e02138339d294e6bb9c4eb82162199c6a852afeaa8d68712\",\n",
" ],\n",
" \"amount\": int( numbers.convert_one_to_atto( 10000 ) ),\n",
" \"bls-key-sigs\": [\n",
" \"0xef2c49a2f31fbbd23c21bc176eaf05cd0bebe6832033075d81fea7cff6f9bc1ab42f3b6895c5493fe645d8379d2eaa1413de55a9d3ce412a4f747cb57d52cc4da4754bfb2583ec9a41fe5dd48287f964f276336699959a5fcef3391dc24df00d\",\n",
" ]\n",
"}\n",
"validator = validator_module.Validator( address )\n",
"validator.load( info )\n",
"raw_tx = validator.sign_create_validator_transaction(\n",
" 1,\n",
" Web3.toWei( 100, 'gwei' ),\n",
" 55000000, # gas limit\n",
" pk,\n",
" 2 # chain id\n",
").rawTransaction.hex()\n",
"tx_hash = transaction.send_raw_staking_transaction(\n",
" raw_tx,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 155,
"id": "9b12f75f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0xf9017c80f9012994a5241513da9f4463f1d4874b548dfbac29d91f34f83d85416c69636585616c69636591616c6963652e6861726d6f6e792e6f6e6583426f629a41726520796f75206576656e2072656164696e6720746869733fddc988016345785d8a0000c9880c7d713b49da0000c887b1a2bc2ec500008a021e19e0c9bab24000008a152d02c7e14af6800000f1b0a20e70089664a874b00251c5e85d35a73871531306f3af43e02138339d294e6bb9c4eb82162199c6a852afeaa8d68712f862b860ef2c49a2f31fbbd23c21bc176eaf05cd0bebe6832033075d81fea7cff6f9bc1ab42f3b6895c5493fe645d8379d2eaa1413de55a9d3ce412a4f747cb57d52cc4da4754bfb2583ec9a41fe5dd48287f964f276336699959a5fcef3391dc24df00d8a021e19e0c9bab24000000185174876e8008403473bc028a08c1146305eaef981aa24c2f17c8519664d10c99ee42acedbc258749930d31a7ca031dadf114ee6ab9bd09933208094c65037b66c796bcfc57a70158106b37357b0\n",
"0x400e9831d358f5daccd153cad5bf53650a0d413bd8682ec0ffad55367d162968\n"
]
}
],
"source": [
"print( raw_tx )\n",
"print( tx_hash )"
]
},
{
"cell_type": "markdown",
"id": "4c2ff645",
"metadata": {},
"source": [
"### Delegation"
]
},
{
"cell_type": "code",
"execution_count": 156,
"id": "458d81b8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0xf88302f4941f2213a52f7409ff4f103458e6d202e0b3aa805a94a5241513da9f4463f1d4874b548dfbac29d91f3489056bc75e2d631000000285174876e80082c35028a02c5e953062dcdfa2de9723639b63bab45705eb6dfbfe7f44536ed266c3c7ca20a0742964e646338e7431874f70715565d99c01c762324355c69db34a9ed9de81d7\n",
"0xc8177ace2049d9f4eb4a45fd6bd6b16f693573d036322c36774cc00d05a3e24f\n"
]
}
],
"source": [
"pk = \"3c86ac59f6b038f584be1c08fced78d7c71bb55d5655f81714f3cddc82144c65\"\n",
"tx = {\n",
" 'directive': staking_structures.Directive.Delegate,\n",
" 'delegatorAddress': 'one1ru3p8ff0wsyl7ncsx3vwd5szuze64qz60upg37',\n",
" 'validatorAddress': 'one155jp2y76nazx8uw5sa94fr0m4s5aj8e5xm6fu3',\n",
" 'amount': Web3.toWei( 100, 'ether' ),\n",
" 'nonce': 2,\n",
" 'gasPrice': Web3.toWei( 100, 'gwei' ),\n",
" 'gasLimit': 50000,\n",
" 'chainId': 2,\n",
"}\n",
"raw_tx = staking_signing.sign_staking_transaction(\n",
" tx,\n",
" pk,\n",
").rawTransaction.hex()\n",
"tx_hash = transaction.send_raw_staking_transaction(\n",
" raw_tx,\n",
")\n",
"print( raw_tx )\n",
"print( tx_hash )"
]
},
{
"cell_type": "markdown",
"id": "8efa5536",
"metadata": {},
"source": [
"### test_transaction.py - transfer 105 ONE to another address"
]
},
{
"cell_type": "code",
"execution_count": 157,
"id": "c3295fee",
"metadata": {},
"outputs": [],
"source": [
"pk = '3c86ac59f6b038f584be1c08fced78d7c71bb55d5655f81714f3cddc82144c65'\n",
"tx = {\n",
" 'from': 'one1ru3p8ff0wsyl7ncsx3vwd5szuze64qz60upg37',\n",
" 'gasPrice': Web3.toWei( 100, 'gwei' ),\n",
" 'gas': 21000,\n",
" 'chainId': 2,\n",
" 'nonce': 3,\n",
" 'shardID': 0,\n",
" 'toShardID': 0,\n",
" 'to': 'one1e8rdglh97t37prtnv7k35ymnh2wazujpzsmzes',\n",
" 'value': Web3.toWei( 105, 'ether' ),\n",
"}\n",
"raw_tx = signing.sign_transaction(tx, pk).rawTransaction.hex()\n",
"tx_hash = transaction.send_raw_transaction(raw_tx)"
]
},
{
"cell_type": "code",
"execution_count": 158,
"id": "af515c7e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0xf86f0385174876e800825208808094c9c6d47ee5f2e3e08d7367ad1a1373ba9dd172418905b12aefafa80400008027a07a4952b90bf38723a9197179a8e6d2e9b3a86fd6da4e66a9cf09fdc59783f757a053910798b311245525bd77d6119332458c2855102e4fb9e564f6a3b710d18bb0\n",
"0x7ccd80f8513f76ec58b357c7a82a12a95e025d88f1444e953f90e3d86e222571\n"
]
}
],
"source": [
"print( raw_tx )\n",
"print( tx_hash )"
]
},
{
"cell_type": "markdown",
"id": "bb546b3e",
"metadata": {},
"source": [
"### test_transaction.py - staking transaction"
]
},
{
"cell_type": "code",
"execution_count": 168,
"id": "c14e2d6d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0xf88302f494c9c6d47ee5f2e3e08d7367ad1a1373ba9dd1724194a5241513da9f4463f1d4874b548dfbac29d91f3489056bc75e2d631000008085174876e80082c35027a0808ea7d27adf3b1f561e8da4676814084bb75ac541b616bece87c6446e6cc54ea02f19f0b14240354bd42ad60b0c7189873c0be87044e13072b0981a837ca76f64\n",
"0xe7d07ef6d9fca595a14ceb0ca917bece7bedb15efe662300e9334a32ac1da629\n"
]
}
],
"source": [
"pk = \"ff9ef6b00a61672b4b7bedd5ac653439b56ac8ee808c99a1bd871cf51b7d60eb\"\n",
"tx = {\n",
" 'directive': staking_structures.Directive.Delegate,\n",
" 'delegatorAddress': 'one1e8rdglh97t37prtnv7k35ymnh2wazujpzsmzes',\n",
" 'validatorAddress': 'one155jp2y76nazx8uw5sa94fr0m4s5aj8e5xm6fu3',\n",
" 'amount': Web3.toWei( 100, 'ether' ),\n",
" 'nonce': 0,\n",
" 'gasPrice': Web3.toWei( 100, 'gwei' ),\n",
" 'gasLimit': 50000,\n",
" 'chainId': 2,\n",
"}\n",
"raw_tx = staking_signing.sign_staking_transaction(\n",
" tx,\n",
" pk,\n",
").rawTransaction.hex()\n",
"tx_hash = transaction.send_raw_staking_transaction(\n",
" raw_tx,\n",
")\n",
"print( raw_tx )\n",
"print( tx_hash )"
]
},
{
"cell_type": "code",
"execution_count": 162,
"id": "ebf296aa",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'blockHash': '0xf55f1fb3c9be76fb74370e8a7d8580327797d2d6082040074783207a171e2de6',\n",
" 'blockNumber': 34,\n",
" 'from': 'one1ru3p8ff0wsyl7ncsx3vwd5szuze64qz60upg37',\n",
" 'hash': '0xf73ba634cb96fc0e3e2c9d3b4c91379e223741be4a5aa56e6d6caf49c1ae75cf',\n",
" 'shardID': 0,\n",
" 'to': 'one1e8rdglh97t37prtnv7k35ymnh2wazujpzsmzes',\n",
" 'toShardID': 1,\n",
" 'value': 100000000000}"
]
},
"execution_count": 162,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"transaction.get_cx_receipt_by_hash( '0xf73ba634cb96fc0e3e2c9d3b4c91379e223741be4a5aa56e6d6caf49c1ae75cf', 'http://localhost:9502' )"
]
},
{
"cell_type": "code",
"execution_count": 166,
"id": "ff0229ce",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'blockHash': '0x0000000000000000000000000000000000000000000000000000000000000000',\n",
" 'blockNumber': None,\n",
" 'from': 'one1e8rdglh97t37prtnv7k35ymnh2wazujpzsmzes',\n",
" 'gas': 50000,\n",
" 'gasPrice': 100000000000,\n",
" 'hash': '0x279935278d20d778cbe4fdfa5d51be9eb1eb184053dc9a7cb88ad3365df73060',\n",
" 'msg': {'amount': 100000000000000000000,\n",
" 'delegatorAddress': 'one1e8rdglh97t37prtnv7k35ymnh2wazujpzsmzes',\n",
" 'validatorAddress': 'one155jp2y76nazx8uw5sa94fr0m4s5aj8e5xm6fu3'},\n",
" 'nonce': 2,\n",
" 'r': '0x8660a63c10af06f2fb3f24b92cf61d4f319044a1f1931c4f4e54ce986ff563c',\n",
" 's': '0x597785559c4283d3ece2df37cbf37077966487a2a2dc0f4cdbbf75a8f20bc1a8',\n",
" 'timestamp': 0,\n",
" 'transactionIndex': 0,\n",
" 'type': 'Delegate',\n",
" 'v': '0x27'}"
]
},
"execution_count": 166,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"transaction.get_staking_transaction_by_hash( \"0x279935278d20d778cbe4fdfa5d51be9eb1eb184053dc9a7cb88ad3365df73060\" )"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

@ -16,14 +16,12 @@ def setup():
os.makedirs(TEMP_DIR, exist_ok=True)
@pytest.mark.run(order=0)
def test_download_cli():
env = cli.download(BINARY_FILE_PATH, replace=False, verbose=False)
cli.environment.update(env)
assert os.path.exists(BINARY_FILE_PATH)
@pytest.mark.run(order=1)
def test_is_valid():
bad_file_path = os.path.realpath(f"{TEMP_DIR}/test_is_valid/bad_hmy")
shutil.rmtree(Path(bad_file_path).parent, ignore_errors=True)
@ -35,7 +33,6 @@ def test_is_valid():
assert not cli.is_valid_binary(bad_file_path)
@pytest.mark.run(order=2)
def test_bad_bin_set():
bad_file_path = os.path.realpath(f"{TEMP_DIR}/test_bad_bin_set/hmy")
shutil.rmtree(Path(bad_file_path).parent, ignore_errors=True)
@ -46,7 +43,6 @@ def test_bad_bin_set():
assert cli.get_binary_path() != bad_file_path
@pytest.mark.run(order=3)
def test_bin_set():
cli.set_binary(BINARY_FILE_PATH)
cli_binary_path = cli.get_binary_path()

@ -1,13 +1,10 @@
from decimal import Decimal
import pytest
from pyhmy import (
numbers
)
@pytest.mark.run(order=1)
def test_convert_atto_to_one():
a = numbers.convert_atto_to_one(1e18)
assert Decimal(1) == a
@ -21,7 +18,6 @@ def test_convert_atto_to_one():
d = numbers.convert_atto_to_one(Decimal(1e18))
assert Decimal(1) == d
@pytest.mark.run(order=2)
def test_convert_one_to_atto():
a = numbers.convert_one_to_atto(1e-18)
assert Decimal(1) == a

@ -33,7 +33,6 @@ def setup():
pytest.skip("can not connect to local blockchain", allow_module_level=True)
@pytest.mark.run(order=1)
def test_request_connection_error():
# Find available port
s = socket.socket()
@ -52,7 +51,6 @@ def test_request_connection_error():
assert bad_request is None
@pytest.mark.run(order=2)
def test_request_rpc_error():
error_request = None
try:
@ -64,7 +62,6 @@ def test_request_rpc_error():
assert error_request is None
@pytest.mark.run(order=3)
def test_rpc_request():
endpoint = 'http://localhost:9500'
timeout = 30

@ -1,73 +1,65 @@
import json
import time
import random
import pytest
import requests
test_validator_address = 'one18tvf56zqjkjnak686lwutcp5mqfnvee35xjnhc'
transfer_raw_transaction = '0xf86f80843b9aca008252080180943ad89a684095a53edb47d7ddc5e034d8133667318a152d02c7e14af68000008027a0ec6c8ad0f70b3c826fa77574c6815a8f73936fafb7b2701a7082ad7d278c95a9a0429f9f166b1c1d385a4ec8f8b86604c26e427c2b0a1c85d9cf4ec6bbd0719508'
tx_hash = '0x1fa20537ea97f162279743139197ecf0eac863278ac1c8ada9a6be5d1e31e633'
create_validator_raw_transaction = '0xf9015680f90105943ad89a684095a53edb47d7ddc5e034d813366731d984746573748474657374847465737484746573748474657374ddc988016345785d8a0000c9880c7d713b49da0000c887b1a2bc2ec500008a022385a827e8155000008b084595161401484a000000f1b0282554f2478661b4844a05a9deb1837aac83931029cb282872f0dcd7239297c499c02ea8da8746d2f08ca2b037e89891f862b86003557e18435c201ecc10b1664d1aea5b4ec59dbfe237233b953dbd9021b86bc9770e116ed3c413fe0334d89562568a10e133d828611f29fee8cdab9719919bbcc1f1bf812c73b9ccd0f89b4f0b9ca7e27e66d58bbb06fcf51c295b1d076cfc878a0228f16f86157860000080843b9aca008351220027a018385211a150ca032c3526cef0aba6a75f99a18cb73f547f67bab746be0c7a64a028be921002c6eb949b3932afd010dfe1de2459ec7fe84403b9d9d8892394a78c'
staking_tx_hash = '0x57ec011aabdeb078a4816502224022f291fa8b07c82bbae8476f514a1d71c730'
contract_tx_hash = '0xa13414dd152173395c69a11e79dea31bf029660f747a42a53744181d05571e70'
contract_raw_transaction = '0xf9025080843b9aca008366916c80808080b901fc608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555061019c806100606000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c8063445df0ac146100465780638da5cb5b14610064578063fdacd576146100ae575b600080fd5b61004e6100dc565b6040518082815260200191505060405180910390f35b61006c6100e2565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6100da600480360360208110156100c457600080fd5b8101908080359060200190929190505050610107565b005b60015481565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16141561016457806001819055505b5056fea265627a7a723158209b80813a158b44af65aee232b44c0ac06472c48f4abbe298852a39f0ff34a9f264736f6c6343000510003227a03a3ad2b7c2934a8325fc04d04daad740d337bb1f589482bbb1d091e1be804d29a00c46772871866a34f254e6197a526bebc2067f75edc53c488b31d84e07c3c685'
# private keys
# 1f84c95ac16e6a50f08d44c7bde7aff8742212fda6e4321fde48bf83bef266dc / one155jp2y76nazx8uw5sa94fr0m4s5aj8e5xm6fu3 (genesis)
# 3c86ac59f6b038f584be1c08fced78d7c71bb55d5655f81714f3cddc82144c65 / one1ru3p8ff0wsyl7ncsx3vwd5szuze64qz60upg37 (transferred 503)
endpoint = 'http://localhost:9500'
endpoint_shard_one = 'http://localhost:9501'
timeout = 30
headers = {
'Content-Type': 'application/json'
}
txs = [
# same shard 503 ONE transfer from one155jp2y76nazx8uw5sa94fr0m4s5aj8e5xm6fu3 to one1ru3p8ff0wsyl7ncsx3vwd5szuze64qz60upg37 (0 nonce)
"0xf86f8085174876e8008252088080941f2213a52f7409ff4f103458e6d202e0b3aa805a891b4486fafde57c00008027a0d7c0b20207dcc9dde376822dc3f5625eac6f59a7526111695cdba3e29553ca17a05d4ca9a421ae16f89cbf6848186eaea7a800da732446dff9952e7c1e91d414e3",
# contract creation by one1ru3p8ff0wsyl7ncsx3vwd5szuze64qz60upg37 (0 nonce)
"0xf8e88085174876e800830186a080808080b8946080604052348015600f57600080fd5b50607780601d6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80634936cd3614602d575b600080fd5b604080516001815290519081900360200190f3fea2646970667358221220fa3fa0e8d0267831a59f4dd5edf39a513d07e98461cb06660ad28d4beda744cd64736f6c634300080f003327a08bf26ee0120c296b17af507f62606abdb5c5f09a65642c3d30b349b8bfbb3d69a03ec7be51c615bcbf2f1d63f6eaa56cf8d7be81671717f90239619830a81ebc9f",
# cross shard transfer by one1ru3p8ff0wsyl7ncsx3vwd5szuze64qz60upg37 (1 nonce)
"0xf86b0185174876e800825208800194c9c6d47ee5f2e3e08d7367ad1a1373ba9dd1724185174876e8008027a02501c517220e9499f14e97c20b0a88cd3b7ba80637bba43ed295422e69a3f300a079b8e1213c9506184aed6ac2eb0b2cb00594c3f9fcdd6c088937ce17fe47107c",
]
stxs = [
# creation of one155jp2y76nazx8uw5sa94fr0m4s5aj8e5xm6fu3 as validator (1 nonce)
"0xf9017c80f9012994a5241513da9f4463f1d4874b548dfbac29d91f34f83d85416c69636585616c69636591616c6963652e6861726d6f6e792e6f6e6583426f629a41726520796f75206576656e2072656164696e6720746869733fddc988016345785d8a0000c9880c7d713b49da0000c887b1a2bc2ec500008a021e19e0c9bab24000008a152d02c7e14af6800000f1b0a20e70089664a874b00251c5e85d35a73871531306f3af43e02138339d294e6bb9c4eb82162199c6a852afeaa8d68712f862b860ef2c49a2f31fbbd23c21bc176eaf05cd0bebe6832033075d81fea7cff6f9bc1ab42f3b6895c5493fe645d8379d2eaa1413de55a9d3ce412a4f747cb57d52cc4da4754bfb2583ec9a41fe5dd48287f964f276336699959a5fcef3391dc24df00d8a021e19e0c9bab24000000185174876e8008403473bc028a08c1146305eaef981aa24c2f17c8519664d10c99ee42acedbc258749930d31a7ca031dadf114ee6ab9bd09933208094c65037b66c796bcfc57a70158106b37357b0",
# delegation by one1ru3p8ff0wsyl7ncsx3vwd5szuze64qz60upg37 to one155jp2y76nazx8uw5sa94fr0m4s5aj8e5xm6fu3 (2 nonce)
"0xf88302f4941f2213a52f7409ff4f103458e6d202e0b3aa805a94a5241513da9f4463f1d4874b548dfbac29d91f3489056bc75e2d631000000285174876e80082c35028a02c5e953062dcdfa2de9723639b63bab45705eb6dfbfe7f44536ed266c3c7ca20a0742964e646338e7431874f70715565d99c01c762324355c69db34a9ed9de81d7",
]
tx_hashes = [
"0xc26be5776aa57438bccf196671a2d34f3f22c9c983c0f844c62b2fb90403aa43",
"0xa605852dd2fa39ed42e101c17aaca9d344d352ba9b24b14b9af94ec9cb58b31f",
"0xf73ba634cb96fc0e3e2c9d3b4c91379e223741be4a5aa56e6d6caf49c1ae75cf",
]
stx_hashes = [
"0x400e9831d358f5daccd153cad5bf53650a0d413bd8682ec0ffad55367d162968",
"0xc8177ace2049d9f4eb4a45fd6bd6b16f693573d036322c36774cc00d05a3e24f",
]
assert len( txs ) == len( tx_hashes ), "Mismatch in tx and tx_hash count"
assert len( stxs ) == len( stx_hashes ), "Mismatch in stx and stx_hash count"
@pytest.fixture(scope="session", autouse=True)
def setup_blockchain():
# return
metadata = _check_connection()
_check_staking_epoch(metadata)
tx_data = _check_funding_transaction()
if not tx_data['result']:
_send_funding_transaction()
time.sleep(20) # Sleep to let cross shard transaction finalize
tx_data = _check_funding_transaction()
if 'error' in tx_data:
pytest.skip(f"Error in hmyv2_getTransactionByHash reply: {tx_data['error']}", allow_module_level=True)
if not tx_data['result']:
pytest.skip(f"Funding transaction failed: {tx_hash}", allow_module_level=True)
stx_data = _check_staking_transaction()
if not stx_data['result']:
_send_staking_transaction()
time.sleep(30) # Sleep to let transaction finalize
stx_data = _check_staking_transaction()
if 'error' in stx_data:
pytest.skip(f"Error in hmyv2_getStakingTransactionByHash reply: {stx_data['error']}", allow_module_level=True)
if not stx_data['result']:
pytest.skip(f"Staking transaction failed: {staking_tx_hash}", allow_module_level=True)
contract_data = _check_contract_transaction()
if not contract_data['result']:
_send_contract_transaction()
times.sleep(30)
contract_data = _check_contract_transaction()
if 'error' in contract_data:
pytest.skip(f"Error in hmyv2_getStakingTransactionByHash reply: {contract_data['error']}", allow_module_level=True)
if not contract_data['result']:
pytest.skip(f"Staking transaction failed: {contract_tx_hash}", allow_module_level=True)
# TODO: Build data object to return data instead of hard coded values in the test files
try:
return int(stx_data['result']['blockNumber'])
except (TypeError, KeyError) as e:
pytest.skip(f"Unexpected reply for hmyv2_getStakingTransactionByHash: {stx_data['result']}", allow_module_level=True)
for i in range( len( txs ) ):
tx = txs[ i ]
tx_hash = tx_hashes[ i ]
_send_transaction( tx, endpoint )
if not _wait_for_transaction_confirmed( tx_hash, endpoint ):
pytest.skip("Could not confirm initial transaction #{} on chain".format( i ), allow_module_level = True)
for i in range( len( stxs ) ):
stx = stxs[ i ]
stx_hash = stx_hashes[ i ]
_send_staking_transaction( stx, endpoint )
if not _wait_for_staking_transaction_confirmed( stx_hash, endpoint ):
pytest.skip("Could not confirm initial staking transaction #{} on chain".format( i ), allow_module_level = True)
def _check_connection():
try:
@ -111,15 +103,15 @@ def _check_staking_epoch(metadata):
if staking_epoch > current_epoch:
pytest.skip(f'Not staking epoch: current {current_epoch}, staking {staking_epoch}', allow_module_level=True)
def _send_funding_transaction():
def _send_transaction(raw_tx, endpoint):
try:
payload = {
"id": "1",
"jsonrpc": "2.0",
"method": 'hmyv2_sendRawTransaction',
"params": [transfer_raw_transaction]
"params": [raw_tx]
}
response = requests.request('POST', endpoint_shard_one, headers=headers,
response = requests.request('POST', endpoint, headers=headers,
data=json.dumps(payload), timeout=timeout, allow_redirects=True)
tx = json.loads(response.content)
if 'error' in tx:
@ -127,7 +119,7 @@ def _send_funding_transaction():
except Exception as e:
pytest.skip('Failed to get hmyv2_sendRawTransaction reply', allow_module_level=True)
def _check_funding_transaction():
def _check_transaction(tx_hash, endpoint):
try:
payload = {
"id": "1",
@ -135,21 +127,6 @@ def _check_funding_transaction():
"method": 'hmyv2_getTransactionByHash',
"params": [tx_hash]
}
response = requests.request('POST', endpoint_shard_one, headers=headers,
data=json.dumps(payload), timeout=timeout, allow_redirects=True)
tx_data = json.loads(response.content)
return tx_data
except Exception as e:
pytest.skip('Failed to get hmyv2_getTransactionByHash reply', allow_module_level=True)
def _check_contract_transaction():
try:
payload = {
"id": "1",
"jsonrpc": "2.0",
"method": 'hmyv2_getTransactionByHash',
"params": [contract_tx_hash]
}
response = requests.request('POST', endpoint, headers=headers,
data=json.dumps(payload), timeout=timeout, allow_redirects=True)
tx_data = json.loads(response.content)
@ -157,29 +134,25 @@ def _check_contract_transaction():
except Exception as e:
pytest.skip('Failed to get hmyv2_getTransactionByHash reply', allow_module_level=True)
def _send_contract_transaction():
try:
payload = {
"id": "1",
"jsonrpc": "2.0",
"method": 'hmyv2_sendRawTransaction',
"params": [contract_raw_transaction]
}
response = requests.request('POST', endpoint, headers=headers,
data=json.dumps(payload), timeout=timeout, allow_redirects=True)
tx_data = json.loads(response.content)
if 'error' in staking_tx:
pytest.skip(f"Error in hmyv2_sendRawTransaction reply: {tx_data['error']}", allow_module_level=True)
except Exception as e:
pytest.skip('Failed to get hmyv2_sendRawTransaction reply', allow_module_level=True)
def _send_staking_transaction():
def _wait_for_transaction_confirmed(tx_hash, endpoint, timeout = 30):
start_time = time.time()
while((time.time() - start_time) <= timeout):
tx_data = _check_transaction(tx_hash, endpoint)
if tx_data is not None:
block_hash = tx_data[ "result" ].get( "blockHash", "0x00" )
unique_chars = "".join( set( list( block_hash[ 2 : ] ) ) )
if unique_chars != "0":
return True
time.sleep(random.uniform(0.2, 0.5))
return False
def _send_staking_transaction(raw_tx, endpoint = endpoint):
try:
payload = {
"id": "1",
"jsonrpc": "2.0",
"method": 'hmyv2_sendRawStakingTransaction',
"params": [create_validator_raw_transaction]
"params": [raw_tx]
}
response = requests.request('POST', endpoint, headers=headers,
data=json.dumps(payload), timeout=timeout, allow_redirects=True)
@ -189,13 +162,13 @@ def _send_staking_transaction():
except Exception as e:
pytest.skip('Failed to get hmyv2_sendRawStakingTransaction reply', allow_module_level=True)
def _check_staking_transaction():
def _check_staking_transaction(stx_hash, endpoint = endpoint):
try:
payload = {
"id": "1",
"jsonrpc": "2.0",
"method": 'hmyv2_getStakingTransactionByHash',
"params": [staking_tx_hash]
"params": [stx_hash]
}
response = requests.request('POST', endpoint, headers=headers,
data=json.dumps(payload), timeout=timeout, allow_redirects=True)
@ -203,3 +176,16 @@ def _check_staking_transaction():
return stx_data
except Exception as e:
pytest.skip('Failed to get hmyv2_getStakingTransactionByHash reply', allow_module_level=True)
def _wait_for_staking_transaction_confirmed(tx_hash, endpoint, timeout = 30):
answer = False
start_time = time.time()
while((time.time() - start_time) <= timeout):
tx_data = _check_staking_transaction(tx_hash, endpoint)
if tx_data is not None:
block_hash = tx_data[ "result" ].get( "blockHash", "0x00" )
unique_chars = "".join( set( list( block_hash[ 2 : ] ) ) )
if unique_chars != "0":
answer = True
time.sleep(random.uniform(0.2, 0.5))
return answer

@ -10,10 +10,10 @@ from pyhmy.rpc import (
)
explorer_endpoint = 'http://localhost:9599'
endpoint_shard_one = 'http://localhost:9501'
local_test_address = 'one1zksj3evekayy90xt4psrz8h6j2v3hla4qwz4ur'
test_validator_address = 'one18tvf56zqjkjnak686lwutcp5mqfnvee35xjnhc'
explorer_endpoint = 'http://localhost:9700'
endpoint_shard_one = 'http://localhost:9502'
local_test_address = 'one155jp2y76nazx8uw5sa94fr0m4s5aj8e5xm6fu3'
test_validator_address = local_test_address
genesis_block_number = 0
test_block_number = 1
fake_shard = 'http://example.com'
@ -31,68 +31,56 @@ def _test_account_rpc(fn, *args, **kwargs):
return response
@pytest.mark.run(order=1)
def test_get_balance(setup_blockchain):
balance = _test_account_rpc(account.get_balance, local_test_address)
assert isinstance(balance, int)
assert balance > 0
@pytest.mark.run(order=2)
def test_get_balance_by_block(setup_blockchain):
balance = _test_account_rpc(account.get_balance_by_block, local_test_address, genesis_block_number)
assert isinstance(balance, int)
assert balance > 0
@pytest.mark.run(order=3)
def test_get_account_nonce(setup_blockchain):
true_nonce = _test_account_rpc(account.get_account_nonce, local_test_address, test_block_number, endpoint=endpoint_shard_one)
assert isinstance(true_nonce, int)
@pytest.mark.run(order=4)
def test_get_transaction_history(setup_blockchain):
tx_history = _test_account_rpc(account.get_transaction_history, local_test_address, endpoint=explorer_endpoint)
assert isinstance(tx_history, list)
assert len(tx_history) >= 0
@pytest.mark.run(order=5)
def test_get_staking_transaction_history(setup_blockchain):
staking_tx_history = _test_account_rpc(account.get_staking_transaction_history, test_validator_address, endpoint=explorer_endpoint)
assert isinstance(staking_tx_history, list)
assert len(staking_tx_history) > 0
@pytest.mark.run(order=6)
def test_get_balance_on_all_shards(setup_blockchain):
balances = _test_account_rpc(account.get_balance_on_all_shards, local_test_address)
assert isinstance(balances, list)
assert len(balances) == 2
@pytest.mark.run(order=7)
def test_get_total_balance(setup_blockchain):
total_balance = _test_account_rpc(account.get_total_balance, local_test_address)
assert isinstance(total_balance, int)
assert total_balance > 0
@pytest.mark.run(order=0)
def test_is_valid_address():
assert account.is_valid_address('one1zksj3evekayy90xt4psrz8h6j2v3hla4qwz4ur')
assert not account.is_valid_address('one1wje75aedczmj4dwjs0812xcg7vx0dy231cajk0')
@pytest.mark.run(order=8)
def test_get_transaction_count(setup_blockchain):
tx_count = _test_account_rpc(account.get_transaction_count, local_test_address, 'latest')
tx_count = _test_account_rpc(account.get_transaction_count, local_test_address, 'latest', explorer_endpoint)
assert isinstance(tx_count, int)
assert tx_count > 0
@pytest.mark.run(order=9)
def test_get_transactions_count(setup_blockchain):
tx_count = _test_account_rpc(account.get_transactions_count, local_test_address, 'ALL')
tx_count = _test_account_rpc(account.get_transactions_count, local_test_address, 'ALL', explorer_endpoint)
@pytest.mark.run(order=10)
def test_get_staking_transactions_count(setup_blockchain):
tx_count = _test_account_rpc(account.get_staking_transactions_count, local_test_address, 'ALL')
tx_count = _test_account_rpc(account.get_staking_transactions_count, local_test_address, 'ALL', explorer_endpoint)
assert isinstance(tx_count, int)
@pytest.mark.run(order=10)
def test_errors():
with pytest.raises(exceptions.RPCError):
account.get_balance('', fake_shard)

@ -15,6 +15,7 @@ genesis_block_number = 0
test_block_number = 1
test_block_hash = None
fake_shard = 'http://example.com'
address = 'one155jp2y76nazx8uw5sa94fr0m4s5aj8e5xm6fu3'
def _test_blockchain_rpc(fn, *args, **kwargs):
if not callable(fn):
@ -28,54 +29,44 @@ def _test_blockchain_rpc(fn, *args, **kwargs):
pytest.fail(f'Unexpected error: {e.__class__} {e}')
return response
@pytest.mark.run(order=1)
def test_get_node_metadata(setup_blockchain):
metadata = _test_blockchain_rpc(blockchain.get_node_metadata)
assert isinstance(metadata, dict)
@pytest.mark.run(order=2)
def test_get_sharding_structure(setup_blockchain):
sharding_structure = _test_blockchain_rpc(blockchain.get_sharding_structure)
assert isinstance(sharding_structure, list)
assert len(sharding_structure) > 0
@pytest.mark.run(order=3)
def test_get_leader_address(setup_blockchain):
leader = _test_blockchain_rpc(blockchain.get_leader_address)
assert isinstance(leader, str)
assert 'one1' in leader
@pytest.mark.run(order=4)
def test_get_block_number(setup_blockchain):
current_block_number = _test_blockchain_rpc(blockchain.get_block_number)
assert isinstance(current_block_number, int)
@pytest.mark.run(order=5)
def test_get_current_epoch(setup_blockchain):
current_epoch = _test_blockchain_rpc(blockchain.get_current_epoch)
assert isinstance(current_epoch, int)
@pytest.mark.run(order=6)
def tset_get_gas_price(setup_blockchain):
gas = _test_blockchain_rpc(blockchain.get_gas_price)
assert isinstance(gas, int)
@pytest.mark.run(order=7)
def test_get_num_peers(setup_blockchain):
peers = _test_blockchain_rpc(blockchain.get_num_peers)
assert isinstance(peers, int)
@pytest.mark.run(order=8)
def test_get_latest_header(setup_blockchain):
header = _test_blockchain_rpc(blockchain.get_latest_header)
assert isinstance(header, dict)
@pytest.mark.run(order=9)
def test_get_latest_chain_headers(setup_blockchain):
header_pair = _test_blockchain_rpc(blockchain.get_latest_chain_headers)
assert isinstance(header_pair, dict)
@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)
@ -83,163 +74,134 @@ def test_get_block_by_number(setup_blockchain):
assert 'hash' in block.keys()
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')
block = _test_blockchain_rpc(blockchain.get_block_by_hash, test_block_hash)
assert isinstance(block, dict)
@pytest.mark.run(order=12)
def test_get_block_transaction_count_by_number(setup_blockchain):
tx_count = _test_blockchain_rpc(blockchain.get_block_transaction_count_by_number, test_block_number)
assert isinstance(tx_count, int)
@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')
tx_count = _test_blockchain_rpc(blockchain.get_block_transaction_count_by_hash, test_block_hash)
assert isinstance(tx_count, int)
@pytest.mark.run(order=14)
def test_get_blocks(setup_blockchain):
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)
@pytest.mark.run(order=15)
def test_get_block_signers(setup_blockchain):
block_signers = _test_blockchain_rpc(blockchain.get_block_signers, test_block_number)
assert isinstance(block_signers, list)
assert len(block_signers) > 0
@pytest.mark.run(order=16)
def test_get_validators(setup_blockchain):
validators = _test_blockchain_rpc(blockchain.get_validators, test_epoch_number)
assert isinstance(validators, dict)
assert 'validators' in validators.keys()
assert len(validators['validators']) > 0
@pytest.mark.run(order=17)
def test_get_shard(setup_blockchain):
shard = _test_blockchain_rpc(blockchain.get_shard)
assert isinstance(shard, int)
assert shard == 0
@pytest.mark.run(order=18)
def test_get_staking_epoch(setup_blockchain):
staking_epoch = _test_blockchain_rpc(blockchain.get_staking_epoch)
assert isinstance(staking_epoch, int)
@pytest.mark.run(order=19)
def test_get_prestaking_epoch(setup_blockchain):
prestaking_epoch = _test_blockchain_rpc(blockchain.get_prestaking_epoch)
assert isinstance(prestaking_epoch, int)
@pytest.mark.run(order=20)
def test_get_bad_blocks(setup_blockchain):
# TODO: Remove skip when RPC is fixed
pytest.skip("Known error with hmyv2_getCurrentBadBlocks")
bad_blocks = _test_blockchain_rpc(blockchain.get_bad_blocks)
assert isinstance(bad_blocks, list)
@pytest.mark.run(order=21)
def test_get_validator_keys(setup_blockchain):
keys = _test_blockchain_rpc(blockchain.get_validator_keys, test_epoch_number)
assert isinstance(keys, list)
assert len(keys) > 0
@pytest.mark.run(order=22)
def test_get_block_signers_keys(setup_blockchain):
keys = _test_blockchain_rpc(blockchain.get_block_signers_keys, test_block_number)
assert isinstance(keys, list)
assert len(keys) > 0
@pytest.mark.run(order=23)
def test_chain_id(setup_blockchain):
chain_id = _test_blockchain_rpc(blockchain.chain_id)
assert isinstance(chain_id, int)
@pytest.mark.run(order=24)
def test_get_peer_info(setup_blockchain):
peer_info = _test_blockchain_rpc(blockchain.get_peer_info)
assert isinstance(peer_info, dict)
@pytest.mark.run(order=25)
def test_protocol_version(setup_blockchain):
protocol_version = _test_blockchain_rpc(blockchain.protocol_version)
assert isinstance(protocol_version, int)
@pytest.mark.run(order=26)
def test_is_last_block(setup_blockchain):
is_last_block = _test_blockchain_rpc(blockchain.is_last_block, 0)
assert isinstance(is_last_block, bool)
assert not is_last_block
@pytest.mark.run(order=27)
def test_epoch_last_block(setup_blockchain):
epoch_last_block = _test_blockchain_rpc(blockchain.epoch_last_block, 0)
assert isinstance(epoch_last_block, int)
@pytest.mark.run(order=28)
def test_get_circulating_supply(setup_blockchain):
circulating_supply = _test_blockchain_rpc(blockchain.get_circulating_supply)
assert isinstance(circulating_supply, str)
@pytest.mark.run(order=29)
def test_get_total_supply(setup_blockchain):
total_supply = _test_blockchain_rpc(blockchain.get_total_supply)
assert isinstance(total_supply, str) or total_supply == None
@pytest.mark.run(order=30)
def test_get_last_cross_links(setup_blockchain):
last_cross_links = _test_blockchain_rpc(blockchain.get_last_cross_links)
assert isinstance(last_cross_links, list)
@pytest.mark.run(order=31)
def test_get_gas_price(setup_blockchain):
gas_price = _test_blockchain_rpc(blockchain.get_gas_price)
assert isinstance(gas_price, int)
@pytest.mark.run(order=32)
def test_get_version(setup_blockchain):
version = _test_blockchain_rpc(blockchain.get_version)
assert isinstance(version, int)
@pytest.mark.run(order=33)
def test_get_header_by_number(setup_blockchain):
header_pair = _test_blockchain_rpc(blockchain.get_header_by_number, 0)
assert isinstance(header_pair, dict)
@pytest.mark.run(order=34)
def test_get_block_staking_transaction_count_by_number(setup_blockchain):
tx_count = _test_blockchain_rpc(blockchain.get_block_staking_transaction_count_by_number, test_block_number)
assert isinstance(tx_count, int)
@pytest.mark.run(order=35)
def test_get_block_staking_transaction_count_by_hash(setup_blockchain):
if not test_block_hash:
pytest.skip('Failed to get reference block hash')
tx_count = _test_blockchain_rpc(blockchain.get_block_staking_transaction_count_by_hash, test_block_hash)
assert isinstance(tx_count, int)
@pytest.mark.run(order=36)
def test_is_block_signer(setup_blockchain):
is_signer = _test_blockchain_rpc(blockchain.is_block_signer, test_block_number, '0x0')
is_signer = _test_blockchain_rpc(blockchain.is_block_signer, test_block_number, address)
assert isinstance(is_signer, bool)
@pytest.mark.run(order=37)
def test_get_signed_blocks(setup_blockchain):
signed_blocks = _test_blockchain_rpc(blockchain.get_signed_blocks, '0x0')
signed_blocks = _test_blockchain_rpc(blockchain.get_signed_blocks, address)
assert isinstance(signed_blocks, int)
@pytest.mark.run(order=38)
def test_in_sync(setup_blockchain):
in_sync = _test_blockchain_rpc(blockchain.in_sync)
assert isinstance(in_sync, bool)
@pytest.mark.run(order=38)
def test_beacon_in_sync(setup_blockchain):
beacon_in_sync = _test_blockchain_rpc(blockchain.beacon_in_sync)
assert isinstance(beacon_in_sync, bool)

@ -9,7 +9,9 @@ from pyhmy.rpc import (
)
explorer_endpoint = 'http://localhost:9599'
contract_tx_hash = '0xa13414dd152173395c69a11e79dea31bf029660f747a42a53744181d05571e70'
contract_tx_hash = '0xa605852dd2fa39ed42e101c17aaca9d344d352ba9b24b14b9af94ec9cb58b31f'
# deployedBytecode from json file
contract_code = '0x6080604052348015600f57600080fd5b506004361060285760003560e01c80634936cd3614602d575b600080fd5b604080516001815290519081900360200190f3fea2646970667358221220fa3fa0e8d0267831a59f4dd5edf39a513d07e98461cb06660ad28d4beda744cd64736f6c634300080f0033'
contract_address = None
fake_shard = 'http://example.com'
@ -27,34 +29,29 @@ def _test_contract_rpc(fn, *args, **kwargs):
pytest.fail(f'Unexpected error: {e.__class__} {e}')
return response
@pytest.mark.run(order=1)
def test_get_contract_address_from_hash(setup_blockchain):
global contract_address
contract_address = _test_contract_rpc(contract.get_contract_address_from_hash, contract_tx_hash)
assert isinstance(contract_address, str)
@pytest.mark.run(order=2)
def test_call(setup_blockchain):
if not contract_address:
pytest.skip('Contract address not loaded yet')
called = _test_contract_rpc(contract.call, contract_address, 'latest')
assert isinstance(called, str) and called.startswith('0x')
@pytest.mark.run(order=3)
def test_estimate_gas(setup_blockchain):
if not contract_address:
pytest.skip('Contract address not loaded yet')
gas = _test_contract_rpc(contract.estimate_gas, contract_address)
assert isinstance(gas, int)
@pytest.mark.run(order=4)
def test_get_code(setup_blockchain):
if not contract_address:
pytest.skip('Contract address not loaded yet')
code = _test_contract_rpc(contract.get_code, contract_address, 'latest')
assert code == '0x608060405234801561001057600080fd5b50600436106100415760003560e01c8063445df0ac146100465780638da5cb5b14610064578063fdacd576146100ae575b600080fd5b61004e6100dc565b6040518082815260200191505060405180910390f35b61006c6100e2565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6100da600480360360208110156100c457600080fd5b8101908080359060200190929190505050610107565b005b60015481565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16141561016457806001819055505b5056fea265627a7a723158209b80813a158b44af65aee232b44c0ac06472c48f4abbe298852a39f0ff34a9f264736f6c63430005100032'
assert code == contract_code
@pytest.mark.run(order=5)
def test_get_storage_at(setup_blockchain):
if not contract_address:
pytest.skip('Contract address not loaded yet')

@ -10,8 +10,8 @@ from pyhmy.rpc import (
)
explorer_endpoint = 'http://localhost:9599'
test_validator_address = 'one18tvf56zqjkjnak686lwutcp5mqfnvee35xjnhc'
explorer_endpoint = 'http://localhost:9700'
test_validator_address = 'one155jp2y76nazx8uw5sa94fr0m4s5aj8e5xm6fu3'
fake_shard = 'http://example.com'
def _test_staking_rpc(fn, *args, **kwargs):
@ -26,126 +26,105 @@ def _test_staking_rpc(fn, *args, **kwargs):
pytest.fail(f'Unexpected error: {e.__class__} {e}')
return response
@pytest.mark.run(order=1)
def test_get_all_validator_addresses(setup_blockchain):
validator_addresses = _test_staking_rpc(staking.get_all_validator_addresses)
assert isinstance(validator_addresses, list)
assert len(validator_addresses) > 0
assert test_validator_address in validator_addresses
@pytest.mark.run(order=2)
def test_get_validator_information(setup_blockchain):
info = _test_staking_rpc(staking.get_validator_information, test_validator_address)
assert isinstance(info, dict)
@pytest.mark.run(order=3)
def test_get_all_validator_information(setup_blockchain):
all_validator_information = _test_staking_rpc(staking.get_all_validator_information)
assert isinstance(all_validator_information, list)
assert len(all_validator_information) > 0
@pytest.mark.run(order=4)
def test_get_delegations_by_delegator(setup_blockchain):
delegations = _test_staking_rpc(staking.get_delegations_by_delegator, test_validator_address)
assert isinstance(delegations, list)
assert len(delegations) > 0
@pytest.mark.run(order=5)
def test_get_delegations_by_validator(setup_blockchain):
delegations = _test_staking_rpc(staking.get_delegations_by_validator, test_validator_address)
assert isinstance(delegations, list)
assert len(delegations) > 0
@pytest.mark.run(order=6)
def test_get_current_utility_metrics(setup_blockchain):
metrics = _test_staking_rpc(staking.get_current_utility_metrics)
assert isinstance(metrics, dict)
@pytest.mark.run(order=7)
def test_get_staking_network_info(setup_blockchain):
info = _test_staking_rpc(staking.get_staking_network_info)
assert isinstance(info, dict)
@pytest.mark.run(order=8)
def test_get_super_committees(setup_blockchain):
committee = _test_staking_rpc(staking.get_super_committees)
assert isinstance(committee, dict)
@pytest.mark.run(order=9)
def test_get_raw_median_stake_snapshot(setup_blockchain):
median_stake = _test_staking_rpc(staking.get_raw_median_stake_snapshot)
assert isinstance(median_stake, dict)
@pytest.mark.run(order=10)
def test_get_validator_information_by_block(setup_blockchain):
# Apparently validator information not created until block after create-validator transaction is accepted, so +1 block
info = _test_staking_rpc(staking.get_validator_information_by_block_number, test_validator_address, setup_blockchain + 1, endpoint=explorer_endpoint)
info = _test_staking_rpc(staking.get_validator_information_by_block_number, test_validator_address, 'latest', endpoint=explorer_endpoint)
assert isinstance(info, dict)
@pytest.mark.run(order=11)
def test_get_validator_information_by_block(setup_blockchain):
# Apparently validator information not created until block after create-validator transaction is accepted, so +1 block
info = _test_staking_rpc(staking.get_all_validator_information_by_block_number, setup_blockchain + 1, endpoint=explorer_endpoint)
info = _test_staking_rpc(staking.get_all_validator_information_by_block_number, 'latest', endpoint=explorer_endpoint)
assert isinstance(info, list)
@pytest.mark.run(order=12)
def test_get_delegations_by_delegator_by_block(setup_blockchain):
delegations = _test_staking_rpc(staking.get_delegations_by_delegator_by_block_number, test_validator_address, setup_blockchain + 1, endpoint=explorer_endpoint)
delegations = _test_staking_rpc(staking.get_delegations_by_delegator_by_block_number, test_validator_address, 'latest', endpoint=explorer_endpoint)
assert isinstance(delegations, list)
@pytest.mark.run(order=13)
def test_get_elected_validator_addresses(setup_blockchain):
validator_addresses = _test_staking_rpc(staking.get_elected_validator_addresses)
assert isinstance(validator_addresses, list)
assert len(validator_addresses) > 0
@pytest.mark.run(order=14)
def test_get_validators(setup_blockchain):
validators = _test_staking_rpc(staking.get_validators, 2)
assert isinstance(validators, dict)
assert len(validators['validators']) > 0
@pytest.mark.run(order=15)
def test_get_validator_keys(setup_blockchain):
validators = _test_staking_rpc(staking.get_validator_keys, 2)
assert isinstance(validators, list)
@pytest.mark.run(order=16)
def test_get_validator_self_delegation(setup_blockchain):
self_delegation = _test_staking_rpc(staking.get_validator_self_delegation, test_validator_address)
assert isinstance(self_delegation, int)
assert self_delegation > 0
@pytest.mark.run(order=17)
def test_get_validator_total_delegation(setup_blockchain):
total_delegation = _test_staking_rpc(staking.get_validator_total_delegation, test_validator_address)
assert isinstance(total_delegation, int)
assert total_delegation > 0
@pytest.mark.run(order=18)
def test_get_all_delegation_information(setup_blockchain):
delegation_information = _test_staking_rpc(staking.get_all_delegation_information, 0)
assert isinstance(delegation_information, list)
assert len(delegation_information) > 0
@pytest.mark.run(order=19)
def test_get_delegation_by_delegator_and_validator(setup_blockchain):
delegation_information = _test_staking_rpc(staking.get_delegation_by_delegator_and_validator, test_validator_address, test_validator_address)
assert isinstance(delegation_information, dict)
@pytest.mark.run(order=20)
def test_get_available_redelegation_balance(setup_blockchain):
redelgation_balance = _test_staking_rpc(staking.get_available_redelegation_balance, test_validator_address)
assert isinstance(redelgation_balance, int)
assert redelgation_balance == 0
@pytest.mark.run(order=21)
def test_get_total_staking(setup_blockchain):
total_staking = _test_staking_rpc(staking.get_total_staking)
assert isinstance(total_staking, int)
assert total_staking > 0
if staking.get_validator_information(test_validator_address, explorer_endpoint)[ 'active-status' ] == 'active':
assert total_staking > 0
@pytest.mark.run(order=22)
def test_errors():
with pytest.raises(exceptions.RPCError):
staking.get_all_validator_addresses(fake_shard)

@ -9,6 +9,7 @@ from pyhmy.numbers import (
# other transactions (create/edit validator) are in test_validator.py
# test_delegate is the same as test_undelegate (except the directive) so it has been omitted
# staking transactions without a chain id have been omitted as well, since the node does not accept them anyway
"""
let stakingTx
@ -27,16 +28,16 @@ const signed = stakingTx.rlpSign('4edef2c24995d15b0e25cbd152fb0e2c05d3b79b9c2afd
console.log( 'Signed transaction' )
console.log(signed)
"""
def test_collect_rewards_no_chain_id():
transaction_dict = {
'directive': staking_structures.Directive.CollectRewards,
'delegatorAddress': 'one1a0x3d6xpmr6f8wsyaxd9v36pytvp48zckswvv9',
'nonce': 2,
'gasPrice': int(convert_one_to_atto(1)),
'gasLimit': 100,
}
signed_tx = staking_signing.sign_staking_transaction(transaction_dict, '4edef2c24995d15b0e25cbd152fb0e2c05d3b79b9c2afd134e6f59f91bf99e48')
assert signed_tx.rawTransaction.hex() == '0xf85a04d594ebcd16e8c1d8f493ba04e99a56474122d81a9c5823a0490e4ceb747563ba40da3e0db8a65133cf6f6ae4c48a24866cd6aa1f0d6c2414a06dbd51a67b35b5685e7b7420cba26e63b0e7d3c696fc6cb69d48e54fcad280e9'
# def test_collect_rewards_no_chain_id():
# transaction_dict = {
# 'directive': staking_structures.Directive.CollectRewards,
# 'delegatorAddress': 'one1a0x3d6xpmr6f8wsyaxd9v36pytvp48zckswvv9',
# 'nonce': 2,
# 'gasPrice': int(convert_one_to_atto(1)),
# 'gasLimit': 100,
# }
# signed_tx = staking_signing.sign_staking_transaction(transaction_dict, '4edef2c24995d15b0e25cbd152fb0e2c05d3b79b9c2afd134e6f59f91bf99e48')
# assert signed_tx.rawTransaction.hex() == '0xf85a04d594ebcd16e8c1d8f493ba04e99a56474122d81a9c5823a0490e4ceb747563ba40da3e0db8a65133cf6f6ae4c48a24866cd6aa1f0d6c2414a06dbd51a67b35b5685e7b7420cba26e63b0e7d3c696fc6cb69d48e54fcad280e9'
"""
let stakingTx
@ -80,7 +81,7 @@ stakingTx = new StakingTransaction(
2, // nonce
numberToHex(new Unit('1').asOne().toWei()), // gasPrice
100, // gasLimit
null, // chainId
2, // chainId
);
const signed = stakingTx.rlpSign('4edef2c24995d15b0e25cbd152fb0e2c05d3b79b9c2afd134e6f59f91bf99e48')
console.log( 'Signed transaction' )
@ -95,6 +96,7 @@ def test_delegate():
'nonce': 2,
'gasPrice': int(convert_one_to_atto(1)),
'gasLimit': 100,
'chainId': 2,
}
signed_tx = staking_signing.sign_staking_transaction(transaction_dict, '4edef2c24995d15b0e25cbd152fb0e2c05d3b79b9c2afd134e6f59f91bf99e48')
assert signed_tx.rawTransaction.hex() == '0xf87002eb94ebcd16e8c1d8f493ba04e99a56474122d81a9c5894ebcd16e8c1d8f493ba04e99a56474122d81a9c580523a0aceff4166ec0ecd0cc664fed865270fe77b35e408138950f802129f1f3d06a74a06f9aca402fb6b4842bff8d65f430d82eefa95645e9046b102195d1044993f9fe'
assert signed_tx.rawTransaction.hex() == '0xf87b02eb94ebcd16e8c1d8f493ba04e99a56474122d81a9c5894ebcd16e8c1d8f493ba04e99a56474122d81a9c580502880de0b6b3a76400006428a0c856fd483a989ca4db4b5257f6996729527828fb21ec13cc65f0bffe6c015ab1a05e9d3c92742e8cb7450bebdfb7ad277ccbfc9fa0719db0b12a715a0a173cadd6'

@ -8,29 +8,30 @@ from pyhmy.rpc import (
exceptions
)
from pyhmy.exceptions import (
TxConfirmationTimedoutError
)
endpoint = 'http://localhost:9500'
endpoint_shard_one = 'http://localhost:9502'
fake_shard = 'http://example.com'
localhost_shard_one = 'http://localhost:9501'
tx_hash = '0x1fa20537ea97f162279743139197ecf0eac863278ac1c8ada9a6be5d1e31e633'
# previously sent txs to get and check
tx_hash = '0xc26be5776aa57438bccf196671a2d34f3f22c9c983c0f844c62b2fb90403aa43'
tx_block_num = None
tx_block_hash = None
cx_hash = '0x1fa20537ea97f162279743139197ecf0eac863278ac1c8ada9a6be5d1e31e633'
stx_hash = '0x57ec011aabdeb078a4816502224022f291fa8b07c82bbae8476f514a1d71c730'
tx_index = None
cx_hash = '0xf73ba634cb96fc0e3e2c9d3b4c91379e223741be4a5aa56e6d6caf49c1ae75cf'
stx_hash = '0xc8177ace2049d9f4eb4a45fd6bd6b16f693573d036322c36774cc00d05a3e24f'
stx_block_num = None
stx_block_hash = None
test_index = 0
fake_shard = 'http://example.com'
stx_index = None
# raw_txt generated via:
# hmy transfer --from one12fuf7x9rgtdgqg7vgq0962c556m3p7afsxgvll --to one12fuf7x9rgtdgqg7vgq0962c556m3p7afsxgvll
# --from-shard 0 --to-shard 1 --amount 0.1 --dry-run
raw_tx = '0xf86d01843b9aca0082520880019452789f18a342da8023cc401e5d2b14a6b710fba988016345785d8a00008028a01095f775386e0e3203446179a7a62e5ce1e765c200b5d885f6bb5b141155bd4da0651350a31e1797035cbf878e4c26069e9895845071d01308573532512cca5820'
raw_tx_hash = '0x86bce2e7765937b776bdcf927339c85421b95c70ddf06ba8e4cc0441142b0f53'
# new txs to send and check
raw_tx = '0xf86f0385174876e800825208808094c9c6d47ee5f2e3e08d7367ad1a1373ba9dd172418905b12aefafa80400008027a07a4952b90bf38723a9197179a8e6d2e9b3a86fd6da4e66a9cf09fdc59783f757a053910798b311245525bd77d6119332458c2855102e4fb9e564f6a3b710d18bb0'
raw_tx_hash = '0x7ccd80f8513f76ec58b357c7a82a12a95e025d88f1444e953f90e3d86e222571'
raw_stx = '0xf9015680f90105943ad89a684095a53edb47d7ddc5e034d813366731d984746573748474657374847465737484746573748474657374ddc988016345785d8a0000c9880c7d713b49da0000c887b1a2bc2ec500008a022385a827e8155000008b084595161401484a000000f1b0282554f2478661b4844a05a9deb1837aac83931029cb282872f0dcd7239297c499c02ea8da8746d2f08ca2b037e89891f862b86003557e18435c201ecc10b1664d1aea5b4ec59dbfe237233b953dbd9021b86bc9770e116ed3c413fe0334d89562568a10e133d828611f29fee8cdab9719919bbcc1f1bf812c73b9ccd0f89b4f0b9ca7e27e66d58bbb06fcf51c295b1d076cfc878a0228f16f86157860000080843b9aca008351220027a018385211a150ca032c3526cef0aba6a75f99a18cb73f547f67bab746be0c7a64a028be921002c6eb949b3932afd010dfe1de2459ec7fe84403b9d9d8892394a78c'
raw_stx = '0xf88302f494c9c6d47ee5f2e3e08d7367ad1a1373ba9dd1724194a5241513da9f4463f1d4874b548dfbac29d91f3489056bc75e2d631000008085174876e80082c35027a0808ea7d27adf3b1f561e8da4676814084bb75ac541b616bece87c6446e6cc54ea02f19f0b14240354bd42ad60b0c7189873c0be87044e13072b0981a837ca76f64'
raw_stx_hash = '0xe7d07ef6d9fca595a14ceb0ca917bece7bedb15efe662300e9334a32ac1da629'
def _test_transaction_rpc(fn, *args, **kwargs):
if not callable(fn):
@ -41,19 +42,15 @@ def _test_transaction_rpc(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)}')
if isinstance(e, TxConfirmationTimedoutError):
pytest.skip(f'{str(e)}')
pytest.fail(f'Unexpected error: {e.__class__} {e}')
return response
@pytest.mark.run(order=1)
def test_get_pending_transactions(setup_blockchain):
pool = _test_transaction_rpc(transaction.get_pending_transactions)
assert isinstance(pool, list)
@pytest.mark.run(order=2)
def test_get_transaction_by_hash(setup_blockchain):
tx = _test_transaction_rpc(transaction.get_transaction_by_hash, tx_hash, endpoint=localhost_shard_one)
tx = _test_transaction_rpc(transaction.get_transaction_by_hash, tx_hash, endpoint=endpoint)
assert tx
assert isinstance(tx, dict)
assert 'blockNumber' in tx.keys()
@ -62,76 +59,55 @@ def test_get_transaction_by_hash(setup_blockchain):
tx_block_num = int(tx['blockNumber'])
global tx_block_hash
tx_block_hash = tx['blockHash']
global tx_index
tx_index = int(tx[ 'transactionIndex' ])
@pytest.mark.run(order=3)
def test_get_transaction_by_block_hash_and_index(setup_blockchain):
if not tx_block_hash:
pytest.skip('Failed to get reference block hash')
tx = _test_transaction_rpc(transaction.get_transaction_by_block_hash_and_index,
tx_block_hash, test_index, endpoint=localhost_shard_one)
tx_block_hash, tx_index, endpoint=endpoint)
assert tx
assert isinstance(tx, dict)
@pytest.mark.run(order=4)
def test_get_transaction_by_block_number_and_index(setup_blockchain):
if not tx_block_num:
pytest.skip('Failed to get reference block num')
tx = _test_transaction_rpc(transaction.get_transaction_by_block_number_and_index, tx_block_num, test_index,
endpoint=localhost_shard_one)
tx = _test_transaction_rpc(transaction.get_transaction_by_block_number_and_index, tx_block_num, tx_index,
endpoint=endpoint)
assert tx
assert isinstance(tx, dict)
@pytest.mark.run(order=5)
def test_get_transaction_receipt(setup_blockchain):
tx_receipt = _test_transaction_rpc(transaction.get_transaction_receipt, tx_hash, endpoint=localhost_shard_one)
tx_receipt = _test_transaction_rpc(transaction.get_transaction_receipt, tx_hash, endpoint=endpoint)
assert tx_receipt
assert isinstance(tx_receipt, dict)
@pytest.mark.run(order=6)
def test_get_transaction_error_sink(setup_blockchain):
errors = _test_transaction_rpc(transaction.get_transaction_error_sink)
assert isinstance(errors, list)
@pytest.mark.run(order=7)
def test_send_and_confirm_raw_transaction(setup_blockchain):
# Note: this test is not yet idempotent since the localnet will reject transactions which were previously finalized.
# Secondly, this is a test that seems to return None values - for example the below curl call has the same null value
# curl --location --request POST 'http://localhost:9501' \
# --header 'Content-Type: application/json' \
# --data-raw '{
# "jsonrpc": "2.0",
# "id": 1,
# "method": "hmyv2_getTransactionByHash",
# "params": [
# "0x86bce2e7765937b776bdcf927339c85421b95c70ddf06ba8e4cc0441142b0f53"
# ]
# }'
# {"jsonrpc":"2.0","id":1,"result":null}
test_tx = _test_transaction_rpc(transaction.send_and_confirm_raw_transaction,
raw_tx) # mining stops by the time this transaction is submitted
# so it never confirms, which is why TxConfirmationTimedoutError
# is in the set up call
raw_tx)
assert isinstance(test_tx, dict)
assert test_tx[ 'hash' ] == raw_tx_hash
@pytest.mark.run(order=8)
def test_get_pending_cx_receipts(setup_blockchain):
pending = _test_transaction_rpc(transaction.get_pending_cx_receipts)
assert isinstance(pending, list)
@pytest.mark.run(order=9)
def test_get_cx_receipt_by_hash(setup_blockchain):
cx = _test_transaction_rpc(transaction.get_cx_receipt_by_hash, cx_hash)
cx = _test_transaction_rpc(transaction.get_cx_receipt_by_hash, cx_hash, endpoint_shard_one)
assert cx
assert isinstance(cx, dict)
@pytest.mark.run(order=10)
def test_resend_cx_receipt(setup_blockchain):
sent = _test_transaction_rpc(transaction.resend_cx_receipt, cx_hash)
assert isinstance(sent, bool)
assert not sent
assert sent
@pytest.mark.run(order=11)
def test_get_staking_transaction_by_hash(setup_blockchain):
staking_tx = _test_transaction_rpc(transaction.get_staking_transaction_by_hash, stx_hash)
assert staking_tx
@ -142,45 +118,40 @@ def test_get_staking_transaction_by_hash(setup_blockchain):
stx_block_num = int(staking_tx['blockNumber'])
global stx_block_hash
stx_block_hash = staking_tx['blockHash']
global stx_index
stx_index = int(staking_tx['transactionIndex'])
@pytest.mark.run(order=12)
def test_get_transaction_by_block_hash_and_index(setup_blockchain):
if not stx_block_hash:
pytest.skip('Failed to get reference block hash')
stx = _test_transaction_rpc(transaction.get_staking_transaction_by_block_hash_and_index, stx_block_hash, test_index)
stx = _test_transaction_rpc(transaction.get_staking_transaction_by_block_hash_and_index, stx_block_hash, stx_index)
assert stx
assert isinstance(stx, dict)
@pytest.mark.run(order=13)
def test_get_transaction_by_block_number_and_index(setup_blockchain):
if not stx_block_num:
pytest.skip('Failed to get reference block num')
stx = _test_transaction_rpc(transaction.get_staking_transaction_by_block_number_and_index, stx_block_num, test_index)
stx = _test_transaction_rpc(transaction.get_staking_transaction_by_block_number_and_index, stx_block_num, stx_index)
assert stx
assert isinstance(stx, dict)
@pytest.mark.run(order=14)
def test_get_staking_transaction_error_sink(setup_blockchain):
errors = _test_transaction_rpc(transaction.get_staking_transaction_error_sink)
assert isinstance(errors, list)
@pytest.mark.run(order=15)
def test_send_raw_staking_transaction(setup_blockchain):
test_stx_hash = _test_transaction_rpc(transaction.send_raw_staking_transaction, raw_stx, endpoint=localhost_shard_one)
assert isinstance(test_stx_hash, str)
assert test_stx_hash == stx_hash
test_stx = _test_transaction_rpc(transaction.send_and_confirm_raw_staking_transaction, raw_stx, endpoint=endpoint)
assert isinstance(test_stx, dict)
assert test_stx[ 'hash' ] == raw_stx_hash
@pytest.mark.run(order=16)
def test_get_pool_stats(setup_blockchain):
test_pool_stats = _test_transaction_rpc(transaction.get_pool_stats, endpoint=localhost_shard_one)
test_pool_stats = _test_transaction_rpc(transaction.get_pool_stats, endpoint=endpoint)
assert isinstance(test_pool_stats, dict)
@pytest.mark.run(order=17)
def test_get_pending_staking_transactions(setup_blockchain):
pending_staking_transactions = _test_transaction_rpc(transaction.get_pending_staking_transactions, endpoint=localhost_shard_one)
pending_staking_transactions = _test_transaction_rpc(transaction.get_pending_staking_transactions, endpoint=endpoint)
assert isinstance(pending_staking_transactions, list)
@pytest.mark.run(order=18)
def test_errors():
with pytest.raises(exceptions.RPCError):
transaction.get_pending_transactions(fake_shard)

@ -1,5 +1,4 @@
import pytest
import requests
from decimal import (
Decimal
)
@ -8,10 +7,6 @@ from pyhmy import (
validator
)
from pyhmy.rpc import (
exceptions
)
from pyhmy.numbers import (
convert_one_to_atto
)
@ -20,21 +15,17 @@ from pyhmy.exceptions import (
InvalidValidatorError
)
import sys
test_epoch_number = 0
genesis_block_number = 0
test_block_number = 1
test_validator_object = None
test_validator_loaded = False
@pytest.mark.run(order=0)
def test_instantiate_validator(setup_blockchain):
global test_validator_object
test_validator_object = validator.Validator('one1a0x3d6xpmr6f8wsyaxd9v36pytvp48zckswvv9')
assert isinstance(test_validator_object, validator.Validator)
@pytest.mark.run(order=1)
def test_load_validator(setup_blockchain):
if not test_validator_object:
pytest.skip('Validator not instantiated yet')
@ -84,17 +75,16 @@ const signed = stakingTx.rlpSign('4edef2c24995d15b0e25cbd152fb0e2c05d3b79b9c2afd
console.log( 'Signed transaction' )
console.log(signed)
"""
@pytest.mark.run(order=2)
def test_create_validator_sign(setup_blockchain):
if not (test_validator_object or test_validator_loaded):
pytest.skip('Validator not ready yet')
pytest.skip('Validator not instantiated yet')
signed_hash = test_validator_object.sign_create_validator_transaction(
2,
int(convert_one_to_atto(1)),
100,
'4edef2c24995d15b0e25cbd152fb0e2c05d3b79b9c2afd134e6f59f91bf99e48',
None).rawTransaction.hex()
assert signed_hash == '0xf9016a80f9012394ebcd16e8c1d8f493ba04e99a56474122d81a9c58f83885416c69636585616c69636591616c6963652e6861726d6f6e792e6f6e6583426f6295446f6e2774206d6573732077697468206d65212121dcc8872386f26fc10000c9880c7d713b49da0000c887b1a2bc2ec500008a021e19e0c9bab24000008a0878678326eac9000000f1b030b2c38b1316da91e068ac3bd8751c0901ef6c02a1d58bc712104918302c6ed03d5894671d0c816dad2b4d303320f202f862b86068f800b6adf657b674903e04708060912b893b7c7b500788808247550ab3e186e56a44ebf3ca488f8ed1a42f6cef3a04bd5d2b2b7eb5a767848d3135b362e668ce6bba42c7b9d5666d8e3a83be707b5708e722c58939fe9b07c170f3b70624148a021e27c1806e59a4000023a0650feed3d7bbcb9ed36575c829bfdc600d53cd7a4fcc0e78ddf26d740f1900a8a027798fc245ee5c66847327b7454670b3b84cb3998527c4fbc0f967b787f56bc2'
2).rawTransaction.hex()
assert signed_hash == '0xf9017580f9012394ebcd16e8c1d8f493ba04e99a56474122d81a9c58f83885416c69636585616c69636591616c6963652e6861726d6f6e792e6f6e6583426f6295446f6e2774206d6573732077697468206d65212121dcc8872386f26fc10000c9880c7d713b49da0000c887b1a2bc2ec500008a021e19e0c9bab24000008a0878678326eac9000000f1b030b2c38b1316da91e068ac3bd8751c0901ef6c02a1d58bc712104918302c6ed03d5894671d0c816dad2b4d303320f202f862b86068f800b6adf657b674903e04708060912b893b7c7b500788808247550ab3e186e56a44ebf3ca488f8ed1a42f6cef3a04bd5d2b2b7eb5a767848d3135b362e668ce6bba42c7b9d5666d8e3a83be707b5708e722c58939fe9b07c170f3b70624148a021e27c1806e59a4000002880de0b6b3a76400006428a0c6c7e62f02331df0afd4699ec514a2fc4548c920d77ad74d98caeec8c924c09aa02b27b999a724b1d341d6bbb0e877611d0047542cb7e380f9a6a272d204b450cd'
"""
Signature matched from TypeScript (is outdated because the JS SDK has not been updated for SlotKeyToAddSig)
@ -137,10 +127,9 @@ const signed = stakingTx.rlpSign('4edef2c24995d15b0e25cbd152fb0e2c05d3b79b9c2afd
console.log( 'Signed transaction' )
console.log(signed)
"""
@pytest.mark.run(order=3)
def test_edit_validator_sign(setup_blockchain):
if not (test_validator_object or test_validator_loaded):
pytest.skip('Validator not ready yet')
pytest.skip('Validator not instantiated yet')
signed_hash = test_validator_object.sign_edit_validator_transaction(
2,
int(convert_one_to_atto(1)),
@ -153,10 +142,9 @@ def test_edit_validator_sign(setup_blockchain):
2).rawTransaction.hex()
assert signed_hash == '0xf9012101f8d094ebcd16e8c1d8f493ba04e99a56474122d81a9c58f83885416c69636585616c69636591616c6963652e6861726d6f6e792e6f6e6583426f6295446f6e2774206d6573732077697468206d65212121c887d529ae9e8600008a021e19e0c9bab24000008a0878678326eac9000000b0b9486167ab9087ab818dc4ce026edb5bf216863364c32e42df2af03c5ced1ad181e7d12f0e6dd5307a73b62247608612b0b9486167ab9087ab818dc4ce026edb5bf216863364c32e42df2af03c5ced1ad181e7d12f0e6dd5307a73b6224760861102880de0b6b3a76400006428a0782ed9f909b5c68eb050a917a274d9187a4c8f13799f21ba351bdcb11290c6c7a00a3b4ec8431290565acbbbb8231cafe32ed7c0b6211e7cf570b459cb733e0995'
@pytest.mark.run(order=4)
def test_invalid_validator(setup_blockchain):
if not (test_validator_object or test_validator_loaded):
pytest.skip('Validator not ready yet')
pytest.skip('Validator not instantiated yet')
with pytest.raises(InvalidValidatorError):
info = {
'name': 'Alice',
@ -185,10 +173,9 @@ def test_invalid_validator(setup_blockchain):
with pytest.raises(InvalidValidatorError):
test_validator_object.set_rate('-2.0')
@pytest.mark.run(order=5)
def test_validator_getters(setup_blockchain):
if not (test_validator_object or test_validator_loaded):
pytest.skip('Validator not ready yet')
pytest.skip('Validator not instantiated yet')
assert test_validator_object.get_address() == 'one1a0x3d6xpmr6f8wsyaxd9v36pytvp48zckswvv9'
assert test_validator_object.add_bls_key('5')
assert test_validator_object.remove_bls_key('5')
@ -205,7 +192,6 @@ def test_validator_getters(setup_blockchain):
assert isinstance(test_validator_object.get_rate(), Decimal)
assert len(test_validator_object.get_bls_keys()) > 0
@pytest.mark.run(order=6)
def test_validator_load_from_blockchain(setup_blockchain):
test_validator_object2 = validator.Validator('one109r0tns7av5sjew7a7fkekg4fs3pw0h76pp45e')
test_validator_object2 = validator.Validator('one155jp2y76nazx8uw5sa94fr0m4s5aj8e5xm6fu3')
test_validator_object2.load_from_blockchain()

Loading…
Cancel
Save