diff --git a/crates/chia-client/src/client.rs b/crates/chia-client/src/client.rs index ca7bda7ba..ec9e1eacb 100644 --- a/crates/chia-client/src/client.rs +++ b/crates/chia-client/src/client.rs @@ -6,9 +6,7 @@ use std::{ time::Duration, }; -use chia_protocol::{ - Handshake, Message, NodeType, ProtocolMessageTypes, RequestPeers, RespondPeers, -}; +use chia_protocol::{Handshake, Message, NodeType, ProtocolMessageTypes, RespondPeers}; use chia_traits::Streamable; use dns_lookup::lookup_host; use futures_util::{stream::FuturesUnordered, StreamExt}; @@ -157,11 +155,8 @@ impl Client { } // Request new peers from the peer. - let Ok(Ok(response)): std::result::Result, _> = timeout( - self.0.options.request_peers_timeout, - peer.request_infallible(RequestPeers::new()), - ) - .await + let Ok(Ok(response)): std::result::Result, _> = + timeout(self.0.options.request_peers_timeout, peer.request_peers()).await else { log::info!("Failed to request peers from {}", peer.ip_addr()); self.remove_peer(peer_id).await; diff --git a/crates/chia-client/src/peer.rs b/crates/chia-client/src/peer.rs index 8750c620f..58db888d0 100644 --- a/crates/chia-client/src/peer.rs +++ b/crates/chia-client/src/peer.rs @@ -1,6 +1,13 @@ use std::{fmt, net::IpAddr, sync::Arc}; -use chia_protocol::{ChiaProtocolMessage, Message}; +use chia_protocol::{ + Bytes32, ChiaProtocolMessage, CoinStateFilters, Message, PuzzleSolutionResponse, + RegisterForCoinUpdates, RegisterForPhUpdates, RejectCoinState, RejectPuzzleSolution, + RejectPuzzleState, RequestCoinState, RequestPeers, RequestPuzzleSolution, RequestPuzzleState, + RequestTransaction, RespondCoinState, RespondPeers, RespondPuzzleSolution, RespondPuzzleState, + RespondToCoinUpdates, RespondToPhUpdates, RespondTransaction, SendTransaction, SpendBundle, + TransactionAck, +}; use chia_traits::Streamable; use futures_util::{ stream::{SplitSink, SplitStream}, @@ -119,6 +126,88 @@ impl Peer { self.0.ip_addr } + pub async fn send_transaction(&self, spend_bundle: SpendBundle) -> Result { + self.request_infallible(SendTransaction::new(spend_bundle)) + .await + } + + pub async fn request_puzzle_state( + &self, + puzzle_hashes: Vec, + previous_height: Option, + header_hash: Bytes32, + filters: CoinStateFilters, + subscribe_when_finished: bool, + ) -> Result> { + self.request_fallible(RequestPuzzleState::new( + puzzle_hashes, + previous_height, + header_hash, + filters, + subscribe_when_finished, + )) + .await + } + + pub async fn request_coin_state( + &self, + coin_ids: Vec, + previous_height: Option, + header_hash: Bytes32, + subscribe: bool, + ) -> Result> { + self.request_fallible(RequestCoinState::new( + coin_ids, + previous_height, + header_hash, + subscribe, + )) + .await + } + + pub async fn register_for_ph_updates( + &self, + puzzle_hashes: Vec, + min_height: u32, + ) -> Result { + self.request_infallible(RegisterForPhUpdates::new(puzzle_hashes, min_height)) + .await + } + + pub async fn register_for_coin_updates( + &self, + coin_ids: Vec, + min_height: u32, + ) -> Result { + self.request_infallible(RegisterForCoinUpdates::new(coin_ids, min_height)) + .await + } + + pub async fn request_transaction(&self, transaction_id: Bytes32) -> Result { + self.request_infallible(RequestTransaction::new(transaction_id)) + .await + } + + pub async fn request_puzzle_and_solution( + &self, + coin_id: Bytes32, + height: u32, + ) -> Result> { + match self + .request_fallible::(RequestPuzzleSolution::new( + coin_id, height, + )) + .await? + { + Response::Success(response) => Ok(Response::Success(response.response)), + Response::Rejection(rejection) => Ok(Response::Rejection(rejection)), + } + } + + pub async fn request_peers(&self) -> Result { + self.request_infallible(RequestPeers::new()).await + } + pub async fn send(&self, body: T) -> Result<()> where T: Streamable + ChiaProtocolMessage,