More test
This commit is contained in:
parent
def5f6a8db
commit
9ed6949099
|
@ -17,6 +17,38 @@ class WalletRpcClient(RpcClient):
|
|||
async def get_wallet_balance(self, wallet_id: str) -> Dict:
|
||||
return await self.fetch("get_wallet_balance", {"wallet_id": wallet_id})
|
||||
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
async def send_transaction(
|
||||
self, wallet_id: str, amount: uint64, address: str, fee: uint64 = uint64(0)
|
||||
) -> TransactionRecord:
|
||||
|
||||
response = await self.fetch(
|
||||
"send_transaction",
|
||||
{
|
||||
"wallet_id": wallet_id,
|
||||
"amount": amount,
|
||||
"puzzle_hash": address,
|
||||
"fee": fee,
|
||||
},
|
||||
)
|
||||
if response["success"]:
|
||||
return TransactionRecord.from_json_dict(response["transaction"])
|
||||
raise Exception(response["reason"])
|
||||
|
||||
async def get_transaction(
|
||||
self, wallet_id: str, transaction_id: bytes32
|
||||
) -> Optional[TransactionRecord]:
|
||||
|
||||
response = await self.fetch(
|
||||
"get_transaction",
|
||||
{"walled_id": wallet_id, "transaction_id": transaction_id.hex()},
|
||||
)
|
||||
if response["success"]:
|
||||
return TransactionRecord.from_json_dict(response["transaction"])
|
||||
return None
|
||||
|
||||
>>>>>>> f1c565a8... More test
|
||||
async def log_in(self, fingerprint) -> Dict:
|
||||
return await self.fetch(
|
||||
"log_in",
|
||||
|
|
|
@ -49,7 +49,9 @@ def dataclass_from_dict(klass, d):
|
|||
if not d:
|
||||
return None
|
||||
return dataclass_from_dict(klass.__args__[0], d)
|
||||
if dataclasses.is_dataclass(klass):
|
||||
elif is_type_Tuple(klass):
|
||||
return tuple(dataclass_from_dict(klass.__args__[0], item) for item in d)
|
||||
elif dataclasses.is_dataclass(klass):
|
||||
# Type is a dataclass, data is a dictionary
|
||||
fieldtypes = {f.name: f.type for f in dataclasses.fields(klass)}
|
||||
return klass(**{f: dataclass_from_dict(fieldtypes[f], d[f]) for f in d})
|
||||
|
|
|
@ -7,6 +7,7 @@ from src.types.sized_bytes import bytes32
|
|||
from src.util.hash import std_hash
|
||||
from src.util.streamable import Streamable, streamable
|
||||
from src.util.ints import uint32, uint64, uint8
|
||||
from src.types.mempool_inclusion_status import MempoolInclusionStatus
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
|
@ -42,3 +43,11 @@ class TransactionRecord(Streamable):
|
|||
+ bytes(self.created_at_time)
|
||||
+ bytes(self.amount)
|
||||
)
|
||||
|
||||
def is_in_mempool(self) -> bool:
|
||||
# If one of the nodes we sent it to responded with success, we set it to success
|
||||
for (_, mis, _) in self.sent_to:
|
||||
if MempoolInclusionStatus(mis) == MempoolInclusionStatus.SUCCESS:
|
||||
return True
|
||||
# Note, transactions pending inclusion (pending) return false
|
||||
return False
|
||||
|
|
|
@ -0,0 +1,137 @@
|
|||
import asyncio
|
||||
from secrets import token_bytes
|
||||
|
||||
import pytest
|
||||
|
||||
from src.protocols import full_node_protocol
|
||||
from src.simulator.simulator_protocol import FarmNewBlockProtocol, ReorgProtocol
|
||||
from src.types.peer_info import PeerInfo
|
||||
from src.util.ints import uint16, uint32, uint64
|
||||
from tests.setup_nodes import setup_simulators_and_wallets, bt
|
||||
from src.consensus.block_rewards import calculate_base_fee, calculate_block_reward
|
||||
from tests.time_out_assert import time_out_assert, time_out_assert_not_None
|
||||
from src.util.chech32 import encode_puzzle_hash
|
||||
from src.rpc.wallet_rpc_client import WalletRpcClient
|
||||
from src.rpc.wallet_rpc_api import WalletRpcApi
|
||||
from src.util.config import load_config
|
||||
from src.rpc.rpc_server import start_rpc_server
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def event_loop():
|
||||
loop = asyncio.get_event_loop()
|
||||
yield loop
|
||||
|
||||
|
||||
class TestWalletRpc:
|
||||
@pytest.fixture(scope="function")
|
||||
async def two_wallet_nodes(self):
|
||||
async for _ in setup_simulators_and_wallets(
|
||||
1, 2, {"COINBASE_FREEZE_PERIOD": 0}
|
||||
):
|
||||
yield _
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_wallet_make_transaction(self, two_wallet_nodes):
|
||||
test_rpc_port = uint16(21529)
|
||||
num_blocks = 5
|
||||
full_nodes, wallets = two_wallet_nodes
|
||||
full_node_1, server_1 = full_nodes[0]
|
||||
wallet_node, server_2 = wallets[0]
|
||||
wallet_node_2, server_3 = wallets[1]
|
||||
wallet = wallet_node.wallet_state_manager.main_wallet
|
||||
ph = await wallet.get_new_puzzlehash()
|
||||
ph_2 = await wallet.get_new_puzzlehash()
|
||||
|
||||
await server_2.start_client(PeerInfo("localhost", uint16(server_1._port)), None)
|
||||
|
||||
for i in range(0, num_blocks):
|
||||
await full_node_1.farm_new_block(FarmNewBlockProtocol(ph))
|
||||
|
||||
initial_funds = sum(
|
||||
[
|
||||
calculate_base_fee(uint32(i)) + calculate_block_reward(uint32(i))
|
||||
for i in range(1, num_blocks - 1)
|
||||
]
|
||||
)
|
||||
|
||||
wallet_rpc_api = WalletRpcApi(wallet_node)
|
||||
|
||||
config = load_config(bt.root_path, "config.yaml")
|
||||
hostname = config["self_hostname"]
|
||||
daemon_port = config["daemon_port"]
|
||||
|
||||
def stop_node_cb():
|
||||
pass
|
||||
|
||||
rpc_cleanup = await start_rpc_server(
|
||||
wallet_rpc_api,
|
||||
hostname,
|
||||
daemon_port,
|
||||
test_rpc_port,
|
||||
stop_node_cb,
|
||||
connect_to_daemon=False,
|
||||
)
|
||||
|
||||
await time_out_assert(5, wallet.get_confirmed_balance, initial_funds)
|
||||
await time_out_assert(5, wallet.get_unconfirmed_balance, initial_funds)
|
||||
|
||||
client = await WalletRpcClient.create("localhost", test_rpc_port)
|
||||
try:
|
||||
addr = encode_puzzle_hash(
|
||||
await wallet_node_2.wallet_state_manager.main_wallet.get_new_puzzlehash()
|
||||
)
|
||||
tx = await client.send_transaction("1", 1, addr)
|
||||
assert tx is not None
|
||||
transaction_id = tx.name()
|
||||
|
||||
async def tx_in_mempool():
|
||||
tx = await client.get_transaction("1", transaction_id)
|
||||
return tx.is_in_mempool()
|
||||
|
||||
await time_out_assert(5, tx_in_mempool, True)
|
||||
await time_out_assert(5, wallet.get_unconfirmed_balance, initial_funds - 1)
|
||||
assert (await client.get_wallet_balance("1"))["wallet_balance"]["unconfirmed_wallet_balance"] == initial_funds - 1
|
||||
assert (await client.get_wallet_balance("1"))["wallet_balance"]["confirmed_wallet_balance"] == initial_funds
|
||||
|
||||
for i in range(0, num_blocks * 5):
|
||||
await full_node_1.farm_new_block(FarmNewBlockProtocol(ph_2))
|
||||
|
||||
assert (await client.get_wallet_balance("1"))["wallet_balance"]["confirmed_wallet_balance"] == initial_funds - 1
|
||||
|
||||
|
||||
|
||||
print(await client.get_wallet_balance("1"))
|
||||
|
||||
except Exception:
|
||||
# Checks that the RPC manages to stop the node
|
||||
client.close()
|
||||
await client.await_closed()
|
||||
await rpc_cleanup()
|
||||
raise
|
||||
|
||||
client.close()
|
||||
await client.await_closed()
|
||||
await rpc_cleanup()
|
||||
|
||||
# tx = await wallet.generate_signed_transaction(
|
||||
# 10,
|
||||
# await wallet_node_2.wallet_state_manager.main_wallet.get_new_puzzlehash(),
|
||||
# 0,
|
||||
# )
|
||||
# await wallet.push_transaction(tx)
|
||||
|
||||
# await time_out_assert(5, wallet.get_confirmed_balance, funds)
|
||||
# await time_out_assert(5, wallet.get_unconfirmed_balance, funds - 10)
|
||||
|
||||
# for i in range(0, num_blocks):
|
||||
# await full_node_1.farm_new_block(FarmNewBlockProtocol(ph))
|
||||
|
||||
# new_funds = sum(
|
||||
# [
|
||||
# calculate_base_fee(uint32(i)) + calculate_block_reward(uint32(i))
|
||||
# for i in range(1, (2 * num_blocks) - 1)
|
||||
# ]
|
||||
# )
|
||||
# await time_out_assert(5, wallet.get_confirmed_balance, new_funds - 10)
|
||||
# await time_out_assert(5, wallet.get_unconfirmed_balance, new_funds - 10)
|
Loading…
Reference in New Issue