Skip to content

Commit

Permalink
feat(miniprotocols): Add Tx-Mempool-Monitoring mini-Protocol (#150)
Browse files Browse the repository at this point in the history
* implement txmonitor mini-protocol
* update README
* add Protocol Version V11,V12 and function V10_and_above for handshake protocol
  • Loading branch information
Sbcdn authored Jul 12, 2022
1 parent 0b23d7b commit 3b685e8
Show file tree
Hide file tree
Showing 5 changed files with 411 additions and 0 deletions.
1 change: 1 addition & 0 deletions pallas-miniprotocols/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ The following architectural decisions were made for this particular Rust impleme
| handshake | done | planned |
| local-state | done | planned |
| tx-submission | planned | minimal |
| local tx monitor | done | planned |
| local-tx-submission | ongoing | planned |

## Implementation Details
Expand Down
17 changes: 17 additions & 0 deletions pallas-miniprotocols/src/handshake/n2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const PROTOCOL_V7: u64 = 32775;
const PROTOCOL_V8: u64 = 32776;
const PROTOCOL_V9: u64 = 32777;
const PROTOCOL_V10: u64 = 32778;
const PROTOCOL_V11: u64 = 32779;
const PROTOCOL_V12: u64 = 32780;

impl VersionTable {
pub fn v1_and_above(network_magic: u64) -> VersionTable {
Expand All @@ -30,6 +32,8 @@ impl VersionTable {
(PROTOCOL_V8, VersionData(network_magic)),
(PROTOCOL_V9, VersionData(network_magic)),
(PROTOCOL_V10, VersionData(network_magic)),
(PROTOCOL_V11, VersionData(network_magic)),
(PROTOCOL_V12, VersionData(network_magic)),
]
.into_iter()
.collect::<HashMap<u64, VersionData>>();
Expand All @@ -44,6 +48,19 @@ impl VersionTable {

VersionTable { values }
}

pub fn v10_and_above(network_magic: u64) -> VersionTable {
let values = vec![
(PROTOCOL_V10, VersionData(network_magic)),
(PROTOCOL_V11, VersionData(network_magic)),
(PROTOCOL_V12, VersionData(network_magic)),

]
.into_iter()
.collect::<HashMap<u64, VersionData>>();

VersionTable { values }
}
}

#[derive(Debug, Clone)]
Expand Down
1 change: 1 addition & 0 deletions pallas-miniprotocols/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod chainsync;
pub mod handshake;
pub mod localstate;
pub mod txsubmission;
pub mod txmonitor;

pub use common::*;
pub use machines::*;
172 changes: 172 additions & 0 deletions pallas-miniprotocols/src/txmonitor/codec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
use super::{Message, MsgRequest, MsgResponse, MempoolSizeAndCapacity};
use pallas_codec::minicbor::{decode, encode, Decode, Encode, Encoder};

impl Encode<()> for Message
{

fn encode<W: encode::Write>(
&self,
e: &mut Encoder<W>,
ctx: &mut (),
) -> Result<(), encode::Error<W::Error>> {
match self {
Message::MsgDone => {
e.array(1)?.u16(0)?;
},
Message::MsgAcquire => {
e.array(1)?.u16(1)?;

},
Message::MsgAcquired(slot) => {
e.array(2)?.u16(2)?;
e.encode(slot)?;
},
Message::MsgQuery(query) => {
query.encode(e, ctx)?;
},
Message::MsgResponse(response) => {
response.encode(e, ctx)?;
}
}
log::debug!("encode message: {:?}",self);
Ok(())
}
}

impl<'b> Decode<'b,()> for Message {
fn decode(d: &mut pallas_codec::minicbor::Decoder<'b>, _ctx: &mut ()) -> Result<Self, decode::Error> {
d.array()?;
let label = d.u16()?;
log::debug!("decode message: {:?}",label);
match label {
0 => {
Ok(Message::MsgDone)
},
1 => {
Ok(Message::MsgAcquire)
},
2 => {
let slot = d.decode()?;
Ok(Message::MsgAcquired(slot))
},
3 => {
Ok(Message::MsgQuery(MsgRequest::MsgRelease))
},
5 => {
Ok(Message::MsgQuery(MsgRequest::MsgNextTx))
},
6 => {
log::trace!("Decoding 6, 1. Array: {:?}",d);
let de : Result<Option<u64>,pallas_codec::minicbor::decode::Error> = d.array();
log::trace!("Decoding 6, 2. Array: {:?}",de);
let tag : Result<u8,pallas_codec::minicbor::decode::Error> = d.u8();
let mut tx = None;
if let Ok(_) = tag {
log::trace!("Decoding 6, Tag: {:?}",tag);
let det = d.tag();
log::trace!("Decoding 6, Bytes: {:?}",det);
let cbor = d.bytes()?;
tx = Some(hex::encode(cbor));
log::trace!("Decoding 6, Tx: {:?}",tx);

}
Ok(Message::MsgResponse(MsgResponse::MsgReplyNextTx(tx)))
},
7 => {
let txid = d.decode()?;
Ok(Message::MsgQuery(MsgRequest::MsgHasTx(txid)))
}
8 => {
let has = d.decode()?;
Ok(Message::MsgResponse(MsgResponse::MsgReplyHasTx(has)))
}
9 => {
Ok(Message::MsgQuery(MsgRequest::MsgGetSizes))
}
10 => {
d.array()?;
let capacity = d.decode()?;
let size_in_bytes = d.decode()?;
let number_of_tx = d.decode()?;
Ok(
Message::MsgResponse(MsgResponse::MsgReplyGetSizes(MempoolSizeAndCapacity {
capacity_in_bytes : capacity,
size_in_bytes : size_in_bytes,
number_of_txs : number_of_tx,
}))
)
}
_ => Err(decode::Error::message(
"can't decode Message",
))
}
}

fn nil() -> Option<Self> {
None
}
}

impl Encode<()> for MsgRequest {
fn encode<W: encode::Write>(
&self,
e: &mut Encoder<W>,
_ctx: &mut (),
) -> Result<(), encode::Error<W::Error>> {
match self {
MsgRequest::MsgAwaitAcquire => {
e.array(1)?.u16(1)?;
},
MsgRequest::MsgGetSizes => {
e.array(1)?.u16(9)?;
},
MsgRequest::MsgHasTx(tx) => {
e.array(2)?.u16(7)?;
e.encode(tx)?;
},
MsgRequest::MsgNextTx => {
e.array(1)?.u16(5)?;
},
MsgRequest::MsgRelease => {
e.array(1)?.u16(3)?;
},

}
log::debug!("encode message: {:?}",self);
Ok(())
}
}

impl Encode<()> for MsgResponse {
fn encode<W: encode::Write>(
&self,
e: &mut Encoder<W>,
_ctx: &mut (),
) -> Result<(), encode::Error<W::Error>> {
match self {
MsgResponse::MsgReplyGetSizes(sz) => {
e.array(2)?.u16(10)?;
e.array(3)?;
e.encode(sz.capacity_in_bytes)?;
e.encode(sz.size_in_bytes)?;
e.encode(sz.number_of_txs)?;
},
MsgResponse::MsgReplyHasTx(tx) => {
e.array(2)?.u16(8)?;
e.encode(tx)?;
},
MsgResponse::MsgReplyNextTx(None) => {
e.array(1)?.u16(6)?;
},
MsgResponse::MsgReplyNextTx(Some(tx)) => {
e.array(2)?.u16(6)?;
e.encode(tx.to_string())?;
},
}
Ok(())
}

fn is_nil(&self) -> bool {
false
}
}
Loading

0 comments on commit 3b685e8

Please sign in to comment.