2021-04-04 06:55:26 +03:00
|
|
|
from chia.protocols import full_node_protocol, introducer_protocol, wallet_protocol
|
|
|
|
from chia.server.outbound_message import NodeType
|
|
|
|
from chia.server.ws_connection import WSChiaConnection
|
|
|
|
from chia.types.mempool_inclusion_status import MempoolInclusionStatus
|
2021-04-29 10:13:00 +03:00
|
|
|
from chia.util.api_decorators import api_request, peer_required, execute_task
|
2021-04-04 06:55:26 +03:00
|
|
|
from chia.util.errors import Err
|
|
|
|
from chia.wallet.wallet_node import WalletNode
|
2020-10-16 04:03:46 +03:00
|
|
|
|
|
|
|
|
|
|
|
class WalletNodeAPI:
|
|
|
|
wallet_node: WalletNode
|
|
|
|
|
2021-04-02 20:37:01 +03:00
|
|
|
def __init__(self, wallet_node) -> None:
|
2020-10-16 04:03:46 +03:00
|
|
|
self.wallet_node = wallet_node
|
|
|
|
|
2021-03-10 02:32:33 +03:00
|
|
|
@property
|
|
|
|
def log(self):
|
|
|
|
return self.wallet_node.log
|
|
|
|
|
2021-04-06 03:22:40 +03:00
|
|
|
@property
|
|
|
|
def api_ready(self):
|
|
|
|
return self.wallet_node.logged_in
|
|
|
|
|
2020-10-24 02:52:00 +03:00
|
|
|
@peer_required
|
2020-10-16 04:03:46 +03:00
|
|
|
@api_request
|
2020-12-10 08:40:12 +03:00
|
|
|
async def respond_removals(self, response: wallet_protocol.RespondRemovals, peer: WSChiaConnection):
|
2020-12-11 03:25:35 +03:00
|
|
|
pass
|
2020-10-16 04:03:46 +03:00
|
|
|
|
2020-12-10 08:40:12 +03:00
|
|
|
async def reject_removals_request(self, response: wallet_protocol.RejectRemovalsRequest, peer: WSChiaConnection):
|
2020-10-16 04:03:46 +03:00
|
|
|
"""
|
|
|
|
The full node has rejected our request for removals.
|
|
|
|
"""
|
2020-12-11 03:25:35 +03:00
|
|
|
pass
|
2020-10-16 04:03:46 +03:00
|
|
|
|
|
|
|
@api_request
|
2020-12-10 08:40:12 +03:00
|
|
|
async def reject_additions_request(self, response: wallet_protocol.RejectAdditionsRequest):
|
2020-10-16 04:03:46 +03:00
|
|
|
"""
|
|
|
|
The full node has rejected our request for additions.
|
|
|
|
"""
|
2020-12-11 03:25:35 +03:00
|
|
|
pass
|
2020-10-16 04:03:46 +03:00
|
|
|
|
2021-04-29 10:13:00 +03:00
|
|
|
@execute_task
|
2020-12-07 08:23:42 +03:00
|
|
|
@peer_required
|
|
|
|
@api_request
|
2021-02-09 10:14:37 +03:00
|
|
|
async def new_peak_wallet(self, peak: wallet_protocol.NewPeakWallet, peer: WSChiaConnection):
|
2020-12-07 08:23:42 +03:00
|
|
|
"""
|
|
|
|
The full node sent as a new peak
|
|
|
|
"""
|
2021-02-09 10:14:37 +03:00
|
|
|
await self.wallet_node.new_peak_wallet(peak, peer)
|
2020-12-07 08:23:42 +03:00
|
|
|
|
2020-10-16 04:03:46 +03:00
|
|
|
@api_request
|
2021-02-12 23:24:33 +03:00
|
|
|
async def reject_block_header(self, response: wallet_protocol.RejectHeaderRequest):
|
2020-10-16 04:03:46 +03:00
|
|
|
"""
|
|
|
|
The full node has rejected our request for a header.
|
|
|
|
"""
|
2020-12-11 03:25:35 +03:00
|
|
|
pass
|
2020-10-16 04:03:46 +03:00
|
|
|
|
2020-12-09 08:16:05 +03:00
|
|
|
@api_request
|
2021-02-12 23:24:33 +03:00
|
|
|
async def respond_block_header(self, response: wallet_protocol.RespondBlockHeader):
|
2020-12-09 08:16:05 +03:00
|
|
|
pass
|
|
|
|
|
|
|
|
@peer_required
|
|
|
|
@api_request
|
2020-12-10 08:40:12 +03:00
|
|
|
async def respond_additions(self, response: wallet_protocol.RespondAdditions, peer: WSChiaConnection):
|
2020-12-09 08:16:05 +03:00
|
|
|
pass
|
|
|
|
|
2020-12-11 03:25:35 +03:00
|
|
|
@api_request
|
|
|
|
async def respond_proof_of_weight(self, response: full_node_protocol.RespondProofOfWeight):
|
|
|
|
pass
|
2020-10-16 04:03:46 +03:00
|
|
|
|
2020-10-24 02:52:00 +03:00
|
|
|
@peer_required
|
2020-10-16 04:03:46 +03:00
|
|
|
@api_request
|
2020-12-10 08:40:12 +03:00
|
|
|
async def transaction_ack(self, ack: wallet_protocol.TransactionAck, peer: WSChiaConnection):
|
2020-10-16 04:03:46 +03:00
|
|
|
"""
|
|
|
|
This is an ack for our previous SendTransaction call. This removes the transaction from
|
|
|
|
the send queue if we have sent it to enough nodes.
|
|
|
|
"""
|
|
|
|
assert peer.peer_node_id is not None
|
|
|
|
name = peer.peer_node_id.hex()
|
2021-02-09 10:14:37 +03:00
|
|
|
status = MempoolInclusionStatus(ack.status)
|
2022-01-13 23:08:32 +03:00
|
|
|
if self.wallet_node.wallet_state_manager is None:
|
2021-05-11 08:05:45 +03:00
|
|
|
return None
|
2021-02-09 10:14:37 +03:00
|
|
|
if status == MempoolInclusionStatus.SUCCESS:
|
2020-12-10 08:40:12 +03:00
|
|
|
self.wallet_node.log.info(f"SpendBundle has been received and accepted to mempool by the FullNode. {ack}")
|
2021-02-09 10:14:37 +03:00
|
|
|
elif status == MempoolInclusionStatus.PENDING:
|
2020-12-10 08:40:12 +03:00
|
|
|
self.wallet_node.log.info(f"SpendBundle has been received (and is pending) by the FullNode. {ack}")
|
2020-10-16 04:03:46 +03:00
|
|
|
else:
|
2022-01-13 23:08:32 +03:00
|
|
|
if not self.wallet_node.is_trusted(peer) and ack.error == Err.NO_TRANSACTIONS_WHILE_SYNCING.name:
|
|
|
|
self.wallet_node.log.info(f"Peer {peer.get_peer_info()} is not synced, closing connection")
|
|
|
|
await peer.close()
|
|
|
|
return
|
2020-12-10 08:40:12 +03:00
|
|
|
self.wallet_node.log.warning(f"SpendBundle has been rejected by the FullNode. {ack}")
|
2020-10-16 04:03:46 +03:00
|
|
|
if ack.error is not None:
|
2021-02-09 10:14:37 +03:00
|
|
|
await self.wallet_node.wallet_state_manager.remove_from_queue(ack.txid, name, status, Err[ack.error])
|
2020-10-16 04:03:46 +03:00
|
|
|
else:
|
2021-02-09 10:14:37 +03:00
|
|
|
await self.wallet_node.wallet_state_manager.remove_from_queue(ack.txid, name, status, None)
|
2020-10-16 04:03:46 +03:00
|
|
|
|
2020-12-11 03:25:35 +03:00
|
|
|
@peer_required
|
|
|
|
@api_request
|
2021-02-09 10:14:37 +03:00
|
|
|
async def respond_peers_introducer(
|
|
|
|
self, request: introducer_protocol.RespondPeersIntroducer, peer: WSChiaConnection
|
|
|
|
):
|
2022-01-13 23:08:32 +03:00
|
|
|
if self.wallet_node.wallet_peers is not None:
|
2022-01-29 06:16:47 +03:00
|
|
|
await self.wallet_node.wallet_peers.respond_peers(request, peer.get_peer_info(), False)
|
2020-12-11 03:25:35 +03:00
|
|
|
|
|
|
|
if peer is not None and peer.connection_type is NodeType.INTRODUCER:
|
|
|
|
await peer.close()
|
2021-01-02 06:13:08 +03:00
|
|
|
|
2021-03-10 02:32:33 +03:00
|
|
|
@peer_required
|
|
|
|
@api_request
|
|
|
|
async def respond_peers(self, request: full_node_protocol.RespondPeers, peer: WSChiaConnection):
|
2022-01-13 23:08:32 +03:00
|
|
|
if self.wallet_node.wallet_peers is None:
|
|
|
|
return None
|
2022-01-29 06:16:47 +03:00
|
|
|
|
|
|
|
self.log.info(f"Wallet received {len(request.peer_list)} peers.")
|
|
|
|
await self.wallet_node.wallet_peers.respond_peers(request, peer.get_peer_info(), True)
|
|
|
|
|
2021-03-10 02:32:33 +03:00
|
|
|
return None
|
|
|
|
|
2021-01-02 06:13:08 +03:00
|
|
|
@api_request
|
|
|
|
async def respond_puzzle_solution(self, request: wallet_protocol.RespondPuzzleSolution):
|
2022-01-13 23:08:32 +03:00
|
|
|
if self.wallet_node.wallet_state_manager is None:
|
2021-05-11 08:05:45 +03:00
|
|
|
return None
|
2021-01-02 06:13:08 +03:00
|
|
|
await self.wallet_node.wallet_state_manager.puzzle_solution_received(request)
|
|
|
|
|
|
|
|
@api_request
|
2021-05-19 22:51:52 +03:00
|
|
|
async def reject_puzzle_solution(self, request: wallet_protocol.RejectPuzzleSolution):
|
|
|
|
self.log.warning(f"Reject puzzle solution: {request}")
|
2021-01-11 08:30:42 +03:00
|
|
|
|
|
|
|
@api_request
|
|
|
|
async def respond_header_blocks(self, request: wallet_protocol.RespondHeaderBlocks):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@api_request
|
2021-02-09 10:14:37 +03:00
|
|
|
async def reject_header_blocks(self, request: wallet_protocol.RejectHeaderBlocks):
|
2021-05-19 22:51:52 +03:00
|
|
|
self.log.warning(f"Reject header blocks: {request}")
|
2022-01-13 23:08:32 +03:00
|
|
|
|
|
|
|
@peer_required
|
|
|
|
@api_request
|
|
|
|
async def coin_state_update(self, request: wallet_protocol.CoinStateUpdate, peer: WSChiaConnection):
|
|
|
|
await self.wallet_node.state_update_received(request, peer)
|
|
|
|
|
|
|
|
@api_request
|
|
|
|
async def respond_to_ph_update(self, request: wallet_protocol.RespondToPhUpdates):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@api_request
|
|
|
|
async def respond_to_coin_update(self, request: wallet_protocol.RespondToCoinUpdates):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@api_request
|
|
|
|
async def respond_children(self, request: wallet_protocol.RespondChildren):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@api_request
|
|
|
|
async def respond_ses_hashes(self, request: wallet_protocol.RespondSESInfo):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@api_request
|
|
|
|
async def respond_blocks(self, request: full_node_protocol.RespondBlocks) -> None:
|
|
|
|
pass
|