Move local state queries to its own module

This commit is contained in:
Santiago Carmuega 2021-11-29 08:23:58 -03:00
parent c2ffc7aa3b
commit afd1d9e7c5
5 changed files with 100 additions and 86 deletions

View file

@ -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<dyn std::error::Error>> {
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<Self, Box<dyn std::error::Error>> {
todo!()
}
}
#[derive(Debug, Clone)]
enum Response {
Generic(Vec<u8>),
}
impl EncodePayload for Response {
fn encode_payload(&self, e: &mut pallas_machines::PayloadEncoder) -> Result<(), Box<dyn std::error::Error>> {
todo!()
}
}
impl DecodePayload for Response {
fn decode_payload(d: &mut pallas_machines::PayloadDecoder) -> Result<Self, Box<dyn std::error::Error>> {
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::<ShelleyQuery>::initial(None, Request::GetChainPoint);
let cs = OneShotClient::<QueryV10>::initial(None, RequestV10::GetChainPoint);
let cs = run_agent(cs, ls_channel).unwrap();
println!("{:?}", cs);
}

View file

@ -1,3 +1,4 @@
pub mod queries;
mod codec;
use std::fmt::Debug;

View file

@ -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<dyn std::error::Error>> {
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<Self, Box<dyn std::error::Error>> {
todo!()
}
}
#[derive(Debug, Clone)]
pub struct GenericResponse(Vec<u8>);
impl EncodePayload for GenericResponse {
fn encode_payload(
&self,
e: &mut pallas_machines::PayloadEncoder,
) -> Result<(), Box<dyn std::error::Error>> {
todo!()
}
}
impl DecodePayload for GenericResponse {
fn decode_payload(
d: &mut pallas_machines::PayloadDecoder,
) -> Result<Self, Box<dyn std::error::Error>> {
let cbor: Cbor = d.decode()?;
let slice = cbor.as_ref();
let vec = slice.to_vec();
Ok(GenericResponse(vec))
}
}
impl TryInto<Point> for GenericResponse {
type Error = Box<dyn std::error::Error>;
fn try_into(self) -> Result<Point, Self::Error> {
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;
}

View file

@ -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>;

View file

@ -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<u8>;
#[derive(Debug)]
pub struct Error {}
fn tx_round<TBearer>(
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<TBearer>(bearer: TBearer, protocols: &[u16]) -> Result<Multiplexer, Error>
pub fn setup<TBearer>(bearer: TBearer, protocols: &[u16]) -> Result<Multiplexer, Box<dyn std::error::Error>>
where
TBearer: Bearer + 'static,
{