fix(network): use correct client state transition for n2n txsub (#348)

This commit is contained in:
Harper 2023-12-15 16:28:36 +00:00 committed by GitHub
parent 6175a53fec
commit 4c651c0c3a
4 changed files with 301 additions and 15 deletions

View file

@ -10,7 +10,7 @@ use tokio::net::UnixListener;
use crate::miniprotocols::handshake::{n2c, n2n, Confirmation, VersionNumber, VersionTable};
use crate::miniprotocols::PROTOCOL_N2N_HANDSHAKE;
use crate::miniprotocols::{txsubmission, PROTOCOL_N2N_HANDSHAKE, PROTOCOL_N2N_TX_SUBMISSION};
use crate::{
miniprotocols::{
blockfetch, chainsync, handshake, localstate, PROTOCOL_N2C_CHAIN_SYNC,
@ -38,6 +38,7 @@ pub struct PeerClient {
pub handshake: handshake::Confirmation<handshake::n2n::VersionData>,
pub chainsync: chainsync::N2NClient,
pub blockfetch: blockfetch::Client,
pub txsubmission: txsubmission::Client,
}
impl PeerClient {
@ -49,14 +50,15 @@ impl PeerClient {
let mut plexer = multiplexer::Plexer::new(bearer);
let channel0 = plexer.subscribe_client(0);
let channel2 = plexer.subscribe_client(2);
let channel3 = plexer.subscribe_client(3);
let hs_channel = plexer.subscribe_client(PROTOCOL_N2N_HANDSHAKE);
let cs_channel = plexer.subscribe_client(PROTOCOL_N2N_CHAIN_SYNC);
let bf_channel = plexer.subscribe_client(PROTOCOL_N2N_BLOCK_FETCH);
let txsub_channel = plexer.subscribe_client(PROTOCOL_N2N_TX_SUBMISSION);
let plexer_handle = tokio::spawn(async move { plexer.run().await });
let versions = handshake::n2n::VersionTable::v7_and_above(magic);
let mut client = handshake::Client::new(channel0);
let mut client = handshake::Client::new(hs_channel);
let handshake = client
.handshake(versions)
@ -71,8 +73,9 @@ impl PeerClient {
Ok(Self {
plexer_handle,
handshake,
chainsync: chainsync::Client::new(channel2),
blockfetch: blockfetch::Client::new(channel3),
chainsync: chainsync::Client::new(cs_channel),
blockfetch: blockfetch::Client::new(bf_channel),
txsubmission: txsubmission::Client::new(txsub_channel),
})
}
@ -84,6 +87,10 @@ impl PeerClient {
&mut self.blockfetch
}
pub fn txsubmission(&mut self) -> &mut txsubmission::Client {
&mut self.txsubmission
}
pub fn abort(&mut self) {
self.plexer_handle.abort();
}
@ -95,6 +102,7 @@ pub struct PeerServer {
pub version: (VersionNumber, n2n::VersionData),
pub chainsync: chainsync::N2NServer,
pub blockfetch: blockfetch::Server,
pub txsubmission: txsubmission::Server,
}
impl PeerServer {
@ -108,10 +116,12 @@ impl PeerServer {
let hs_channel = server_plexer.subscribe_server(PROTOCOL_N2N_HANDSHAKE);
let cs_channel = server_plexer.subscribe_server(PROTOCOL_N2N_CHAIN_SYNC);
let bf_channel = server_plexer.subscribe_server(PROTOCOL_N2N_BLOCK_FETCH);
let txsub_channel = server_plexer.subscribe_server(PROTOCOL_N2N_TX_SUBMISSION);
let mut server_hs: handshake::Server<n2n::VersionData> = handshake::Server::new(hs_channel);
let server_cs = chainsync::N2NServer::new(cs_channel);
let server_bf = blockfetch::Server::new(bf_channel);
let server_txsub = txsubmission::Server::new(txsub_channel);
let plexer_handle = tokio::spawn(async move { server_plexer.run().await });
@ -126,6 +136,7 @@ impl PeerServer {
version: ver,
chainsync: server_cs,
blockfetch: server_bf,
txsubmission: server_txsub,
})
} else {
plexer_handle.abort();
@ -141,6 +152,10 @@ impl PeerServer {
&mut self.blockfetch
}
pub fn txsubmission(&mut self) -> &mut txsubmission::Server {
&mut self.txsubmission
}
pub fn abort(&mut self) {
self.plexer_handle.abort();
}

View file

@ -132,14 +132,16 @@ where
pub async fn next_request(&mut self) -> Result<Request<TxId>, Error> {
match self.recv_message().await? {
Message::RequestTxIds(blocking, ack, req) => {
self.0 = State::TxIdsBlocking;
match blocking {
true => Ok(Request::TxIds(ack, req)),
false => Ok(Request::TxIdsNonBlocking(ack, req)),
Message::RequestTxIds(blocking, ack, req) => match blocking {
true => {
self.0 = State::TxIdsBlocking;
Ok(Request::TxIds(ack, req))
}
}
false => {
self.0 = State::TxIdsNonBlocking;
Ok(Request::TxIdsNonBlocking(ack, req))
}
},
Message::RequestTxs(x) => {
self.0 = State::Txs;
Ok(Request::Txs(x))