chia-blockchain/tests/rl_wallet/test_rl_rpc.py

187 lines
7.3 KiB
Python
Raw Normal View History

2020-07-21 10:18:50 +03:00
import asyncio
import pytest
2020-08-25 10:03:00 +03:00
2020-07-21 10:18:50 +03:00
from src.rpc.wallet_rpc_api import WalletRpcApi
from src.simulator.simulator_protocol import FarmNewBlockProtocol
from src.types.coin import Coin
2020-07-21 10:18:50 +03:00
from src.types.peer_info import PeerInfo
2020-08-11 04:35:12 +03:00
from src.util.chech32 import encode_puzzle_hash
2020-08-27 08:10:50 +03:00
from src.util.ints import uint16
2020-08-11 04:35:12 +03:00
from src.wallet.util.wallet_types import WalletType
2020-07-21 10:18:50 +03:00
from tests.setup_nodes import setup_simulators_and_wallets
2020-08-08 02:07:12 +03:00
from tests.time_out_assert import time_out_assert
2020-09-14 12:29:03 +03:00
from src.types.sized_bytes import bytes32
from src.types.mempool_inclusion_status import MempoolInclusionStatus
2020-07-21 10:18:50 +03:00
@pytest.fixture(scope="module")
def event_loop():
loop = asyncio.get_event_loop()
yield loop
2020-09-15 21:47:11 +03:00
class TestRLWallet:
2020-07-21 10:18:50 +03:00
@pytest.fixture(scope="function")
async def three_wallet_nodes(self):
async for _ in setup_simulators_and_wallets(
2020-08-11 03:09:45 +03:00
1, 3, {"COINBASE_FREEZE_PERIOD": 0}
2020-07-21 10:18:50 +03:00
):
yield _
@pytest.mark.asyncio
async def test_create_rl_coin(self, three_wallet_nodes):
num_blocks = 4
full_nodes, wallets = three_wallet_nodes
2020-08-25 10:03:00 +03:00
full_node, server_1 = full_nodes[0]
2020-07-21 10:18:50 +03:00
wallet_node, server_2 = wallets[0]
wallet_node_1, wallet_server_1 = wallets[1]
2020-08-08 01:40:02 +03:00
wallet_node_2, wallet_server_2 = wallets[2]
2020-07-21 10:18:50 +03:00
wallet = wallet_node.wallet_state_manager.main_wallet
ph = await wallet.get_new_puzzlehash()
await server_2.start_client(PeerInfo("localhost", uint16(server_1._port)), None)
await wallet_server_1.start_client(
PeerInfo("localhost", uint16(server_1._port)), None
)
2020-08-08 02:07:12 +03:00
await wallet_server_2.start_client(
PeerInfo("localhost", uint16(server_1._port)), None
)
2020-08-25 10:03:00 +03:00
await full_node.farm_new_block(FarmNewBlockProtocol(ph))
2020-08-27 08:10:50 +03:00
for i in range(0, num_blocks + 1):
2020-08-25 10:03:00 +03:00
await full_node.farm_new_block(FarmNewBlockProtocol(32 * b"\0"))
2020-09-22 05:03:50 +03:00
fund_owners_initial_balance = await wallet.get_confirmed_balance()
2020-07-21 10:18:50 +03:00
api_user = WalletRpcApi(wallet_node_1)
2020-08-11 03:09:45 +03:00
val = await api_user.create_new_wallet(
2020-09-04 06:54:37 +03:00
{"wallet_type": "rl_wallet", "rl_type": "user", "host": "127.0.0.1:5000"}
2020-08-11 03:09:45 +03:00
)
2020-07-21 10:18:50 +03:00
assert isinstance(val, dict)
2020-09-14 12:29:03 +03:00
if "success" in val:
assert val["success"]
2020-08-11 03:09:45 +03:00
assert val["id"]
2020-08-11 04:35:12 +03:00
assert val["type"] == WalletType.RATE_LIMITED.value
2020-08-18 18:42:42 +03:00
user_wallet_id = val["id"]
2020-08-11 03:09:45 +03:00
pubkey = val["pubkey"]
2020-07-21 10:18:50 +03:00
api_admin = WalletRpcApi(wallet_node)
2020-08-11 03:09:45 +03:00
val = await api_admin.create_new_wallet(
{
"wallet_type": "rl_wallet",
"rl_type": "admin",
"interval": 2,
2020-09-15 21:47:11 +03:00
"limit": 10,
2020-08-11 03:09:45 +03:00
"pubkey": pubkey,
"amount": 100,
2020-09-24 19:53:12 +03:00
"fee": 1,
2020-09-05 07:06:15 +03:00
"host": "127.0.0.1:5000",
2020-08-11 03:09:45 +03:00
}
)
2020-07-21 10:18:50 +03:00
assert isinstance(val, dict)
2020-09-14 12:29:03 +03:00
if "success" in val:
assert val["success"]
2020-08-11 03:09:45 +03:00
assert val["id"]
2020-08-11 04:35:12 +03:00
assert val["type"] == WalletType.RATE_LIMITED.value
2020-08-11 03:09:45 +03:00
assert val["origin"]
assert val["pubkey"]
admin_wallet_id = val["id"]
2020-08-11 03:09:45 +03:00
admin_pubkey = val["pubkey"]
origin: Coin = val["origin"]
2020-09-14 12:29:03 +03:00
await api_user.rl_set_user_info(
2020-08-11 03:09:45 +03:00
{
2020-08-18 18:42:42 +03:00
"wallet_id": user_wallet_id,
2020-08-11 03:09:45 +03:00
"interval": 2,
2020-09-15 21:47:11 +03:00
"limit": 10,
2020-08-11 03:09:45 +03:00
"origin": {
"parent_coin_info": origin.parent_coin_info.hex(),
"puzzle_hash": origin.puzzle_hash.hex(),
"amount": origin.amount,
},
"admin_pubkey": admin_pubkey,
}
)
assert (await api_user.get_wallet_balance({"wallet_id": user_wallet_id}))[
"wallet_balance"
]["confirmed_wallet_balance"] == 0
2020-08-11 03:09:45 +03:00
for i in range(0, 2 * num_blocks):
2020-08-25 10:03:00 +03:00
await full_node.farm_new_block(FarmNewBlockProtocol(32 * b"\0"))
2020-08-27 08:10:50 +03:00
2020-09-24 19:53:12 +03:00
assert await wallet.get_confirmed_balance() == fund_owners_initial_balance - 101
2020-08-08 02:07:12 +03:00
async def check_balance(api, wallet_id):
2020-08-11 03:09:45 +03:00
balance_response = await api.get_wallet_balance({"wallet_id": wallet_id})
2020-08-11 04:35:12 +03:00
balance = balance_response["wallet_balance"]["confirmed_wallet_balance"]
2020-08-08 02:07:12 +03:00
return balance
2020-08-18 18:42:42 +03:00
await time_out_assert(15, check_balance, 100, api_user, user_wallet_id)
2020-07-21 10:18:50 +03:00
receiving_wallet = wallet_node_2.wallet_state_manager.main_wallet
2020-09-14 18:42:34 +03:00
address = encode_puzzle_hash(await receiving_wallet.get_new_puzzlehash())
assert await receiving_wallet.get_spendable_balance() == 0
2020-08-11 03:09:45 +03:00
val = await api_user.send_transaction(
{"wallet_id": user_wallet_id, "amount": 3, "fee": 2, "address": address}
2020-08-11 03:09:45 +03:00
)
2020-09-14 12:29:03 +03:00
assert "transaction_id" in val
async def is_transaction_in_mempool(api, tx_id: bytes32) -> bool:
try:
val = await api.get_transaction(
{"wallet_id": user_wallet_id, "transaction_id": tx_id.hex()}
)
except ValueError:
return False
for _, mis, _ in val["transaction"].sent_to:
if (
MempoolInclusionStatus(mis) == MempoolInclusionStatus.SUCCESS
or MempoolInclusionStatus(mis) == MempoolInclusionStatus.PENDING
):
return True
return False
await time_out_assert(
15, is_transaction_in_mempool, True, api_user, val["transaction_id"]
)
2020-08-08 02:07:12 +03:00
2020-08-18 18:42:42 +03:00
for i in range(0, num_blocks):
2020-08-25 10:03:00 +03:00
await full_node.farm_new_block(FarmNewBlockProtocol(32 * b"\0"))
await time_out_assert(15, check_balance, 95, api_user, user_wallet_id)
2020-08-08 02:07:12 +03:00
await time_out_assert(15, receiving_wallet.get_spendable_balance, 3)
2020-09-22 05:43:22 +03:00
val = await api_admin.add_rate_limited_funds(
2020-09-29 01:43:28 +03:00
{"wallet_id": admin_wallet_id, "amount": 100, "fee": 7}
2020-09-22 05:43:22 +03:00
)
2020-09-15 20:42:28 +03:00
assert val["status"] == "SUCCESS"
2020-09-15 21:47:11 +03:00
for i in range(0, 50):
2020-09-15 20:42:28 +03:00
await full_node.farm_new_block(FarmNewBlockProtocol(32 * b"\0"))
await time_out_assert(15, check_balance, 195, api_user, user_wallet_id)
2020-08-18 18:42:42 +03:00
2020-09-22 00:48:30 +03:00
# test spending
2020-09-22 05:03:50 +03:00
puzzle_hash = encode_puzzle_hash(await receiving_wallet.get_new_puzzlehash())
2020-09-15 20:42:28 +03:00
val = await api_user.send_transaction(
{
"wallet_id": user_wallet_id,
2020-09-22 05:03:50 +03:00
"amount": 105,
2020-09-15 20:42:28 +03:00
"fee": 0,
2020-09-22 05:03:50 +03:00
"address": puzzle_hash,
2020-09-15 20:42:28 +03:00
}
)
2020-09-22 05:03:50 +03:00
await time_out_assert(
15, is_transaction_in_mempool, True, api_user, val["transaction_id"]
)
for i in range(0, num_blocks):
await full_node.farm_new_block(FarmNewBlockProtocol(32 * b"\0"))
await time_out_assert(15, check_balance, 90, api_user, user_wallet_id)
2020-09-22 05:03:50 +03:00
await time_out_assert(15, receiving_wallet.get_spendable_balance, 108)
2020-09-14 12:29:03 +03:00
2020-09-29 02:07:44 +03:00
val = await api_admin.send_clawback_transaction(
{"wallet_id": admin_wallet_id, "fee": 11}
)
2020-09-14 12:29:03 +03:00
await time_out_assert(
15, is_transaction_in_mempool, True, api_admin, val["transaction_id"]
)
2020-08-18 18:42:42 +03:00
for i in range(0, num_blocks):
2020-08-25 10:03:00 +03:00
await full_node.farm_new_block(FarmNewBlockProtocol(32 * b"\0"))
await time_out_assert(15, check_balance, 0, api_user, user_wallet_id)
2020-09-22 05:03:50 +03:00
await time_out_assert(15, check_balance, 0, api_admin, user_wallet_id)
final_balance = await wallet.get_confirmed_balance()
2020-09-29 02:07:44 +03:00
assert final_balance == fund_owners_initial_balance - 129