diff --git a/pallas-localstate/examples/chainpoint.rs b/pallas-localstate/examples/chainpoint.rs index b69df0a..c939d6b 100644 --- a/pallas-localstate/examples/chainpoint.rs +++ b/pallas-localstate/examples/chainpoint.rs @@ -1,81 +1,12 @@ use minicbor::data::Cbor; -use pallas_localstate::{OneShotClient, Query}; +use pallas_localstate::queries::RequestV10; +use pallas_localstate::{OneShotClient, queries::QueryV10}; use pallas_handshake::n2c::{Client, VersionTable}; use pallas_handshake::{MAINNET_MAGIC}; use pallas_machines::{DecodePayload, EncodePayload, run_agent}; use pallas_multiplexer::Multiplexer; use std::os::unix::net::UnixStream; -#[derive(Debug, Clone)] -struct BlockQuery {} - -#[derive(Debug, Clone)] -enum Request { - BlockQuery(BlockQuery), - GetSystemStart, - GetChainBlockNo, - GetChainPoint, -} - - -impl EncodePayload for Request { - fn encode_payload(&self, e: &mut pallas_machines::PayloadEncoder) -> Result<(), Box> { - match self { - Request::BlockQuery(block_query) => { - e.u16(0)?; - e.array(0)?; - Ok(()) - } - Request::GetSystemStart => { - e.u16(1)?; - Ok(()) - } - Request::GetChainBlockNo => { - e.u16(2)?; - Ok(()) - } - Request::GetChainPoint => { - e.u16(3)?; - Ok(()) - } - } - } -} - -impl DecodePayload for Request { - fn decode_payload(d: &mut pallas_machines::PayloadDecoder) -> Result> { - todo!() - } -} - -#[derive(Debug, Clone)] -enum Response { - Generic(Vec), -} - -impl EncodePayload for Response { - fn encode_payload(&self, e: &mut pallas_machines::PayloadEncoder) -> Result<(), Box> { - todo!() - } -} - -impl DecodePayload for Response { - fn decode_payload(d: &mut pallas_machines::PayloadDecoder) -> Result> { - let cbor: Cbor = d.decode()?; - let slice = cbor.as_ref(); - let vec = slice.to_vec(); - Ok(Response::Generic(vec)) - } -} - -#[derive(Debug, Clone)] -struct ShelleyQuery {} - -impl Query for ShelleyQuery { - type Request = Request; - type Response = Response; -} - fn main() { env_logger::init(); @@ -91,7 +22,8 @@ fn main() { println!("last hanshake state: {:?}", last); let ls_channel = muxer.use_channel(7); - let cs = OneShotClient::::initial(None, Request::GetChainPoint); + + let cs = OneShotClient::::initial(None, RequestV10::GetChainPoint); let cs = run_agent(cs, ls_channel).unwrap(); println!("{:?}", cs); } diff --git a/pallas-localstate/src/lib.rs b/pallas-localstate/src/lib.rs index a5a128a..750a1c8 100644 --- a/pallas-localstate/src/lib.rs +++ b/pallas-localstate/src/lib.rs @@ -1,3 +1,4 @@ +pub mod queries; mod codec; use std::fmt::Debug; diff --git a/pallas-localstate/src/queries.rs b/pallas-localstate/src/queries.rs new file mode 100644 index 0000000..fbb902f --- /dev/null +++ b/pallas-localstate/src/queries.rs @@ -0,0 +1,90 @@ +use minicbor::{Decoder, data::Cbor}; +use pallas_machines::{DecodePayload, EncodePayload, PayloadDecoder, primitives::Point}; + +use super::Query; + +#[derive(Debug, Clone)] +pub struct BlockQuery {} + +#[derive(Debug, Clone)] +pub enum RequestV10 { + BlockQuery(BlockQuery), + GetSystemStart, + GetChainBlockNo, + GetChainPoint, +} + +impl EncodePayload for RequestV10 { + fn encode_payload( + &self, + e: &mut pallas_machines::PayloadEncoder, + ) -> Result<(), Box> { + match self { + Self::BlockQuery(block_query) => { + e.u16(0)?; + e.array(0)?; + Ok(()) + } + Self::GetSystemStart => { + e.u16(1)?; + Ok(()) + } + Self::GetChainBlockNo => { + e.u16(2)?; + Ok(()) + } + Self::GetChainPoint => { + e.u16(3)?; + Ok(()) + } + } + } +} + +impl DecodePayload for RequestV10 { + fn decode_payload( + d: &mut pallas_machines::PayloadDecoder, + ) -> Result> { + todo!() + } +} + +#[derive(Debug, Clone)] +pub struct GenericResponse(Vec); + +impl EncodePayload for GenericResponse { + fn encode_payload( + &self, + e: &mut pallas_machines::PayloadEncoder, + ) -> Result<(), Box> { + todo!() + } +} + +impl DecodePayload for GenericResponse { + fn decode_payload( + d: &mut pallas_machines::PayloadDecoder, + ) -> Result> { + let cbor: Cbor = d.decode()?; + let slice = cbor.as_ref(); + let vec = slice.to_vec(); + Ok(GenericResponse(vec)) + } +} + +impl TryInto for GenericResponse { + type Error = Box; + + fn try_into(self) -> Result { + let mut d = PayloadDecoder(Decoder::new(self.0.as_slice())); + Point::decode_payload(&mut d) + } +} + +#[derive(Debug, Clone)] +pub struct QueryV10 {} + +impl Query for QueryV10 { + type Request = RequestV10; + type Response = GenericResponse; +} diff --git a/pallas-machines/src/payloads.rs b/pallas-machines/src/payloads.rs index ac84035..daac63d 100644 --- a/pallas-machines/src/payloads.rs +++ b/pallas-machines/src/payloads.rs @@ -27,7 +27,7 @@ impl<'a> PayloadEncoder<'a> { } } -pub struct PayloadDecoder<'a>(Decoder<'a>); +pub struct PayloadDecoder<'a>(pub Decoder<'a>); impl<'a> Deref for PayloadDecoder<'a> { type Target = Decoder<'a>; diff --git a/pallas-multiplexer/src/lib.rs b/pallas-multiplexer/src/lib.rs index fda5ab0..16399dc 100644 --- a/pallas-multiplexer/src/lib.rs +++ b/pallas-multiplexer/src/lib.rs @@ -1,14 +1,8 @@ mod bearers; -use std::{ - collections::HashMap, - io::{Read, Write}, - sync::mpsc::{self, Receiver, Sender, TryRecvError}, - thread::{self, JoinHandle}, - time::{Duration, Instant}, -}; +use std::{collections::HashMap, io::{Read, Write}, sync::mpsc::{self, Receiver, Sender, TryRecvError}, thread::{self, JoinHandle}, time::{Duration, Instant}}; -use log::{debug, error, warn}; +use log::{debug, error, trace, warn}; pub trait Bearer: Read + Write + Send + Sync + Sized { fn read_segment(&mut self) -> Result<(u16, u32, Payload), std::io::Error>; @@ -27,9 +21,6 @@ const MAX_SEGMENT_PAYLOAD_LENGTH: usize = 65535; pub type Payload = Vec; -#[derive(Debug)] -pub struct Error {} - fn tx_round( bearer: &mut TBearer, ingress: &MuxIngress, @@ -52,7 +43,7 @@ where } Err(TryRecvError::Disconnected) => { //TODO: remove handle from list - warn!("protocol handle disconnected"); + trace!("protocol handle {} disconnected", id); } Err(TryRecvError::Empty) => (), }; @@ -124,7 +115,7 @@ pub struct Multiplexer { } impl Multiplexer { - pub fn setup(bearer: TBearer, protocols: &[u16]) -> Result + pub fn setup(bearer: TBearer, protocols: &[u16]) -> Result> where TBearer: Bearer + 'static, {