From 1adc9a31d66c0ecbad03a5fecd2252ff3e35f0f5 Mon Sep 17 00:00:00 2001 From: teebaumcrypto <72811287+teebaumcrypto@users.noreply.github.com> Date: Sat, 29 Jun 2024 20:00:08 +0200 Subject: [PATCH] refactor(network): don't treat rejected txs as submit protocol errors (#306) --- .../miniprotocols/localtxsubmission/client.rs | 35 ++++---- .../miniprotocols/localtxsubmission/codec.rs | 82 ++++++++++++++++++- 2 files changed, 100 insertions(+), 17 deletions(-) diff --git a/pallas-network/src/miniprotocols/localtxsubmission/client.rs b/pallas-network/src/miniprotocols/localtxsubmission/client.rs index c6e5e15..ccb8f57 100644 --- a/pallas-network/src/miniprotocols/localtxsubmission/client.rs +++ b/pallas-network/src/miniprotocols/localtxsubmission/client.rs @@ -46,7 +46,7 @@ where /// # Errors /// Returns an error if the agency is not ours or if the outbound state is /// invalid. - pub async fn submit_tx(&mut self, tx: Tx) -> Result<(), Error> { + pub async fn submit_tx(&mut self, tx: Tx) -> Result, Error> { self.send_submit_tx(tx).await?; self.recv_submit_tx_response().await } @@ -56,7 +56,7 @@ where /// # Errors /// Returns an error if the agency is not ours or if the outbound state is /// invalid. - pub async fn terminate_gracefully(&mut self) -> Result<(), Error> { + pub async fn terminate_gracefully(&mut self) -> Result<(), Error> { let msg = Message::Done; self.send_message(&msg).await?; self.state = State::Done; @@ -77,7 +77,7 @@ where } } - fn assert_agency_is_ours(&self) -> Result<(), Error> { + fn assert_agency_is_ours(&self) -> Result<(), Error> { if !self.has_agency() { Err(Error::AgencyIsTheirs) } else { @@ -85,7 +85,7 @@ where } } - fn assert_agency_is_theirs(&self) -> Result<(), Error> { + fn assert_agency_is_theirs(&self) -> Result<(), Error> { if self.has_agency() { Err(Error::AgencyIsOurs) } else { @@ -93,14 +93,14 @@ where } } - fn assert_outbound_state(&self, msg: &Message) -> Result<(), Error> { + fn assert_outbound_state(&self, msg: &Message) -> Result<(), Error> { match (&self.state, msg) { (State::Idle, Message::SubmitTx(_) | Message::Done) => Ok(()), _ => Err(Error::InvalidOutbound), } } - fn assert_inbound_state(&self, msg: &Message) -> Result<(), Error> { + fn assert_inbound_state(&self, msg: &Message) -> Result<(), Error> { match (&self.state, msg) { (State::Busy, Message::AcceptTx | Message::RejectTx(_)) => Ok(()), _ => Err(Error::InvalidInbound), @@ -116,7 +116,7 @@ where /// # Errors /// Returns an error if the agency is not ours or if the outbound state is /// invalid. - async fn send_message(&mut self, msg: &Message) -> Result<(), Error> { + async fn send_message(&mut self, msg: &Message) -> Result<(), Error> { self.assert_agency_is_ours()?; self.assert_outbound_state(msg)?; @@ -133,7 +133,7 @@ where /// # Errors /// Returns an error if the agency is not theirs or if the inbound state is /// invalid. - async fn recv_message(&mut self) -> Result, Error> { + async fn recv_message(&mut self) -> Result, Error> { self.assert_agency_is_theirs()?; let msg = self @@ -155,7 +155,7 @@ where /// # Errors /// Returns an error if the agency is not ours or if the outbound state is /// invalid. - async fn send_submit_tx(&mut self, tx: Tx) -> Result<(), Error> { + async fn send_submit_tx(&mut self, tx: Tx) -> Result<(), Error> { let msg = Message::SubmitTx(tx); self.send_message(&msg).await?; self.state = State::Busy; @@ -169,17 +169,17 @@ where /// /// # Errors /// Returns an error if the inbound message is invalid. - async fn recv_submit_tx_response(&mut self) -> Result<(), Error> { + async fn recv_submit_tx_response(&mut self) -> Result, Error> { debug!("waiting for SubmitTx response"); match self.recv_message().await? { Message::AcceptTx => { self.state = State::Idle; - Ok(()) + Ok(Response::Accepted) } Message::RejectTx(rejection) => { self.state = State::Idle; - Err(Error::TxRejected(rejection)) + Ok(Response::Rejected(rejection)) } _ => Err(Error::InvalidInbound), } @@ -187,7 +187,7 @@ where } #[derive(Error, Debug)] -pub enum Error { +pub enum Error { #[error("attempted to receive message while agency is ours")] AgencyIsOurs, @@ -202,7 +202,10 @@ pub enum Error { #[error("error while sending or receiving data through the channel")] ChannelError(multiplexer::Error), - - #[error("tx was rejected by the server")] - TxRejected(Reject), } + +#[derive(Debug)] +pub enum Response { + Accepted, + Rejected(Reject), +} \ No newline at end of file diff --git a/pallas-network/src/miniprotocols/localtxsubmission/codec.rs b/pallas-network/src/miniprotocols/localtxsubmission/codec.rs index ce54a8a..353bb2d 100644 --- a/pallas-network/src/miniprotocols/localtxsubmission/codec.rs +++ b/pallas-network/src/miniprotocols/localtxsubmission/codec.rs @@ -38,7 +38,14 @@ where impl<'b, Tx: Decode<'b, ()>, Reject: Decode<'b, ()>> Decode<'b, ()> for Message { fn decode(d: &mut Decoder<'b>, _ctx: &mut ()) -> Result { - d.array()?; + if let Err(_) = d.array() { + // if the first element isn't an array, it's a plutus error + // the node sends string data + let rejection = d.decode()?; + // skip this data via setting the decoder position, because it doesn't recognize it with rejection decode + let _ = d.set_position(d.input().len()); + return Ok(Message::RejectTx(rejection)); + } let label = d.u16()?; match label { 0 => { @@ -48,6 +55,8 @@ impl<'b, Tx: Decode<'b, ()>, Reject: Decode<'b, ()>> Decode<'b, ()> for Message< 1 => Ok(Message::AcceptTx), 2 => { let rejection = d.decode()?; + // skip this data via setting the decoder position, because it doesn't recognize it with rejection decode + let _ = d.set_position(d.input().len()); Ok(Message::RejectTx(rejection)) } 3 => Ok(Message::Done), @@ -134,6 +143,14 @@ mod tests { } } + #[test] + fn decode_reject_string_message() { + let mut bytes = hex::decode(RAW_REJECT_REPONSE_ERROR_STRING).unwrap(); + let msg_res = try_decode_message::>(&mut bytes); + println!("result {:?}", msg_res); + assert!(msg_res.is_ok()) + } + const RAW_REJECT_RESPONSE: &str = "82028182059f820082018200820a81581c3b890fb5449baedf5342a48ee9c9ec6acbc995641be92ad21f08c686\ 8200820183038158202628ce6ff8cc7ff0922072d930e4a693c17f991748dedece0be64819a2f9ef7782582031d\ @@ -149,4 +166,67 @@ mod tests { cb33fb9906b097980a83f2b8ef40b51c4ef52eccd402825820efc267ad2c15c34a117535eecc877241ed836eb3e\ 643ec90de21ca1b12fd79c20282008202820181148200820283023a000f0f6d1a004944ce820082028201830d3a\ 000f0f6d1a00106253820082028201830182811a02409e10811a024138c01a0255e528ff"; + + const RAW_REJECT_REPONSE_ERROR_STRING: &str = + "6867475972786f4141794e6847514d734151455a412b675a416a734141526b4436426c65635151424751506f47\ + 4341614141484b64686b6f3677515a576467595a426c5a3242686b47566e594747515a576467595a426c5a32426\ + 86b47566e59474751595a42686b47566e594747515a5446455949426f4141717a364743415a7456454547674144\ + 5978555a4166384141526f4141567731474341614141655864526b3239415143476741432f35516141416271654\ + 26a63414145424751506f47572f324241496141414f3943426f414130374647443442476741514c67385a4d536f\ + 42476741444c6f415a4161554247674143326e675a412b675a7a775942476741424f6a515949426d6f385267674\ + 751506f47434161414145367241455a34554d454751506f43686f414177495a474a77424767414441686b596e41\ + 456141414d6766426b423251456141414d7741426b422f77455a7a504d5949426e395142676747662f564743415\ + a5742345949426c4173786767476741424b74385949426f4141762b5547674147366e67593341414241526f4141\ + 512b534753326e4141455a3672735949426f4141762b5547674147366e67593341414241526f4141762b5547674\ + 147366e67593341414241526f4145624973476741462f64344141686f414446424f4758635342426f4148577232\ + 47674142516c73454767414544475941424141614141465071786767476741444932455a4179774241526d67336\ + 86767476741445058595949426c353942676747582b344743415a7156305949426c3939786767475a5771474341\ + 6141694f737a416f61413354326b786c4b48776f61416c466568426d417377714347674149466c41614364577a5\ + 1466b452f466b452b514541414449794d6a49794d6a49794d6a49794d6a49794d6a49794d6a49794d69496c4d7a\ + 41554d6a49794d6a49794d6a49794d6a49794d6a49794d6a49794d6a49794d6a49794d6a49794d6c4d7a41774d3\ + 3447041424142435a47526b706d59475a6d3464544d774d7a4e773575744d4451774e5144306741425341414649\ + 414a494141564d774a444e77356d425341555947674469514151715a6753475a455a75764e30356763414247366\ + 3774f414154413041454d44514167564d774a444e77356d59475245536d5a675941416941454a6d41475a754141\ + 435341434d44674146494141424e494151564d774a444e784a75744d4451774e5146674168557a416b4142457a4\ + d7949694d33456d62677a4e77526d34497a4174414f4144414341424d33414762676a4d433041344152494e4150\ + 4d3342414241416d626741426b67416a413041654d4451423033576d426f417362725441304162457a496a4d6a4\ + 131496c4d7a41794142464b41715a6d42775a75764d446b4145414d556f69594152676441416d366b4145414933\ + 5747426f5a4742735947786762474273594777414a6761674b473634774e4147464d794d774d77415253695a475\ + 26b706d424d5a75504e31786762474275414562726a41324d44634145544e783575754d44594149335847427341\ + 435947344535676241416d426d41344a6d5a6d52455247526b5a754a4d33416d6267544170414e4e316f414a6d3\ + 44d7a6345414b4149414759464943616d5a676347526b706d42575a75504e317867646d4234414562726a41374d\ + 44774145544e783575754d447341493358474232414359486746686764674443627141425241424e31435141426\ + 75a674f6d3630774d77465141546461594759444a75744d444d4268544d774d5449794d6c4d774a544e78357575\ + 4d4455774e67416a646359477067624141695a75504e317867616742473634774e5141544132416d4d445541457\ + 74d6747784d33426d6267674154646159475143356d344533576d426b4175627254417941594541457a63435a67\ + 54414647426941305a6754414347426941304c47426d4145594651414a75714d4334774c77475464575a4742635\ + 94635675941416d4261594677414a675841416d59453575744d43734163416f33566d5267566d425959466f414a\ + 67564742574143594659414a6d4249627254416f414641484d7949794d6a4a544d774b7a49794d6c4d7a41754d3\ + 3447041424142435a47526b706d59474a6d34644941414149556f435a75764e30344168756e41424d4451414977\ + 4b7741546455414f4a6b5a47536d5a67596d626830674167416853674a6d3638335467434736634145774e41416\ + a417241424e3151413567596742474251414362716741524143466a49794d6a4979557a4d43387a634f6b414141\ + 454a6b5a47526b706d59475a6d346449414141495449794d6a4a544d774e7a4e77365141514151734a6d3656494\ + 1414145774f67416a417841424e3151414a676141416978676241424742614143627167415441774142457a644b\ + 6b414542555947514152675567416d366f4145774c4441744d43344145774b7a417441464e315a6b5a47536d5a6\ + 75747626830674167416859564d7a41734d33486d3634774c51415142684d4330774c6a41764148466a41764143\ + 4d43594145335641416d526756474259414359464a675667426d3634774a77437a416e414b4d77496a646159457\ + 741494168675441416d424b414359456f434275734d434941493357474243414559454a675167416d4243594434\ + 4252675067416d4138414359446f414a674f41416d41324143594451414a674d67416d417741435944414168674\ + c6741696b7773536d5a67466741696b41414a6d59434a6d3638774454415341424e3149417875744d425577456a\ + 6457594370674a414170414145526d59434941514149415970514d33537041414759414a757041434d774154645\ + 341454151726f45695141694d6a4d774241417a6463594277414a75754d413477447741544150414249694d7a4d\ + 4151414a4941416a4d7a414641435341416461627177415141794d4149335567416b52455a674645536d5a67446\ + 74169414b4b6d5a67476d62727a414a4d4134414541595441454d424577446741524d414977447741514156567a\ + 3658726756584f6b536d5a67436d626941416b67414259544d414d41494145774153496c4d7a41464d334467424\ + a41414359417741496d59415a6d3445414a49414977427741534d6a4143497a414341434142497741694d774167\ + 416741566330726f56644552674247366f4146566338474432486d6632486d665145442f32486d6657427950377\ + 9303042345a5a53547a68596162482b3653316176373668545570616c644439705748524546425245482f32486d\ + 66574279694c7235587846304c3437704c363870616e5568337443312f32484c7a313042425436456b544546425\ + 245466651555242583035475650385a412b5562414256704b5a4c365955776241574e466546324b414142594848\ + 6b6743687a624c72495933354279415a653538786c3365776836586b464d693035332b4b2f59655a3959484b303\ + 465644c505031447a4441647969454d6e77445879736a4d4769693351475346574e62722f476773764b4d416141\ + 58764a4d502f59655a3842414145412f3968356e3968356e352f59655a2f59655a2f59655a39594942364f54504\ + 845657a426d5249524448705765462b4d69394961367935426b564665434675786155714d522f77442f32486d66\ + 32486d66324871665742776d474f6c4d32775a354c7757756d78374869774978394c66304956736254505575593\ + 04c652f39683667502b68514b4641476774734d63445965352f59655a2f59"; }