Skip to content

Commit 90dd688

Browse files
committed
Update process_event to return internal err
We can get rid of the hacky `ProtocolError` variant off `InternalReplayError` if we seperate event processing events from replay errors.
1 parent cad6943 commit 90dd688

5 files changed

Lines changed: 38 additions & 30 deletions

File tree

payjoin/src/core/error.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ impl<SessionState: Debug, SessionEvent: Debug, SessionHistory: Debug> std::fmt::
6868
},
6969
Expired(time, _) => write!(f, "Session expired at {time:?}"),
7070
PersistenceFailure(e) => write!(f, "Persistence failure: {e}"),
71-
ProtocolError() => write!(f, "Protocol error"),
7271
TerminalFailure(_) => write!(f, "Terminal failure"),
7372
}
7473
}
@@ -100,9 +99,12 @@ pub(crate) enum InternalReplayError<SessionState, SessionEvent, SessionHistory>
10099
Expired(crate::time::Time, SessionHistory),
101100
/// Application storage error
102101
PersistenceFailure(ImplementationError),
103-
/// Protocol error
104-
// TODO: should this include a deserialize / string representation of the error?
105-
ProtocolError(),
106102
/// Terminal failure with session history
107103
TerminalFailure(SessionHistory),
108104
}
105+
106+
#[cfg(feature = "v2")]
107+
pub(crate) enum InternalProcessEventError<SessionState, SessionEvent> {
108+
InvalidEvent(Box<SessionEvent>, Option<Box<SessionState>>),
109+
ProtocolError(),
110+
}

payjoin/src/core/receive/v2/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use super::error::{Error, InputContributionError};
4141
use super::{
4242
common, InternalPayloadError, JsonReply, OutputSubstitutionError, ProtocolError, SelectionError,
4343
};
44-
use crate::error::{InternalReplayError, ReplayError};
44+
use crate::error::InternalProcessEventError;
4545
use crate::hpke::{decrypt_message_a, encrypt_message_b, HpkeKeyPair, HpkePublicKey};
4646
use crate::ohttp::{
4747
ohttp_encapsulate, process_get_res, process_post_res, OhttpEncapsulationError, OhttpKeys,
@@ -149,7 +149,7 @@ impl ReceiveSession {
149149
fn process_event(
150150
self,
151151
event: SessionEvent,
152-
) -> Result<ReceiveSession, ReplayError<Self, SessionEvent, SessionHistory>> {
152+
) -> Result<ReceiveSession, InternalProcessEventError<Self, SessionEvent>> {
153153
match (self, event) {
154154
(
155155
ReceiveSession::Initialized(state),
@@ -206,13 +206,12 @@ impl ReceiveSession {
206206
},
207207
})),
208208
(_, SessionEvent::Closed(SessionOutcome::Failure)) =>
209-
Err(InternalReplayError::ProtocolError().into()),
209+
Err(InternalProcessEventError::ProtocolError()),
210210
(current_state, SessionEvent::Closed(_)) => Ok(current_state),
211-
(current_state, event) => Err(InternalReplayError::InvalidEvent(
211+
(current_state, event) => Err(InternalProcessEventError::InvalidEvent(
212212
Box::new(event),
213213
Some(Box::new(current_state)),
214-
)
215-
.into()),
214+
)),
216215
}
217216
}
218217
}

payjoin/src/core/receive/v2/session.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use serde::{Deserialize, Serialize};
22

33
use super::{ReceiveSession, SessionContext};
4-
use crate::error::{InternalReplayError, ReplayError};
4+
use crate::error::{InternalProcessEventError, InternalReplayError, ReplayError};
55
use crate::output_substitution::OutputSubstitution;
66
use crate::persist::SessionPersister;
77
use crate::receive::{InputPair, JsonReply, OriginalPayload, PsbtContext};
@@ -40,13 +40,17 @@ where
4040
persister.close().map_err(|e| {
4141
InternalReplayError::PersistenceFailure(ImplementationError::new(e))
4242
})?;
43-
if let InternalReplayError::ProtocolError() = e.0 {
44-
return Err(InternalReplayError::TerminalFailure(SessionHistory::new(
45-
session_events,
46-
))
47-
.into());
43+
match e {
44+
InternalProcessEventError::ProtocolError() => {
45+
return Err(InternalReplayError::TerminalFailure(SessionHistory::new(
46+
session_events,
47+
))
48+
.into());
49+
}
50+
InternalProcessEventError::InvalidEvent(event, session) => {
51+
return Err(InternalReplayError::InvalidEvent(event, session).into());
52+
}
4853
}
49-
break;
5054
}
5155
}
5256
}

payjoin/src/core/send/v2/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use url::Url;
3939

4040
use super::error::BuildSenderError;
4141
use super::*;
42-
use crate::error::{InternalReplayError, ReplayError};
42+
use crate::error::InternalProcessEventError;
4343
use crate::hpke::{decrypt_message_b, encrypt_message_a, HpkeSecretKey};
4444
use crate::ohttp::{ohttp_encapsulate, process_get_res, process_post_res};
4545
use crate::persist::{
@@ -223,7 +223,7 @@ impl SendSession {
223223
fn process_event(
224224
self,
225225
event: SessionEvent,
226-
) -> Result<SendSession, ReplayError<Self, SessionEvent, SessionHistory>> {
226+
) -> Result<SendSession, InternalProcessEventError<Self, SessionEvent>> {
227227
match (self, event) {
228228
(SendSession::WithReplyKey(state), SessionEvent::PollingForProposal()) =>
229229
Ok(state.apply_polling_for_proposal()),
@@ -232,13 +232,12 @@ impl SendSession {
232232
SessionEvent::ReceivedProposalPsbt(proposal),
233233
) => Ok(SendSession::ProposalReceived(proposal)),
234234
(_, SessionEvent::Closed(SessionOutcome::Failure)) =>
235-
Err(InternalReplayError::ProtocolError().into()),
235+
Err(InternalProcessEventError::ProtocolError()),
236236
(current_state, SessionEvent::Closed(_)) => Ok(current_state),
237-
(current_state, event) => Err(InternalReplayError::InvalidEvent(
237+
(current_state, event) => Err(InternalProcessEventError::InvalidEvent(
238238
Box::new(event),
239239
Some(Box::new(current_state)),
240-
)
241-
.into()),
240+
)),
242241
}
243242
}
244243
}

payjoin/src/core/send/v2/session.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::WithReplyKey;
2-
use crate::error::{InternalReplayError, ReplayError};
2+
use crate::error::{InternalProcessEventError, InternalReplayError, ReplayError};
33
use crate::persist::SessionPersister;
44
use crate::send::v2::SendSession;
55
use crate::uri::v2::PjParam;
@@ -32,13 +32,17 @@ where
3232
persister.close().map_err(|e| {
3333
InternalReplayError::PersistenceFailure(ImplementationError::new(e))
3434
})?;
35-
if let InternalReplayError::ProtocolError() = e.0 {
36-
return Err(InternalReplayError::TerminalFailure(SessionHistory::new(
37-
session_events,
38-
))
39-
.into());
35+
match e {
36+
InternalProcessEventError::ProtocolError() => {
37+
return Err(InternalReplayError::TerminalFailure(SessionHistory::new(
38+
session_events,
39+
))
40+
.into());
41+
}
42+
InternalProcessEventError::InvalidEvent(event, session) => {
43+
return Err(InternalReplayError::InvalidEvent(event, session).into());
44+
}
4045
}
41-
break;
4246
}
4347
}
4448
}

0 commit comments

Comments
 (0)