Skip to content

Commit a183bd2

Browse files
committed
Rename SenderTypeState to SendSession
It contains every possible state of a session including Sender types. Include the module `Send` name to disambiguate the public type used in payjoin-ffi where namespacing is not yet available.
1 parent 8539f85 commit a183bd2

File tree

5 files changed

+38
-36
lines changed

5 files changed

+38
-36
lines changed

payjoin-cli/src/app/v2/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use payjoin::receive::v2::{
1010
Receiver, SessionHistory, UncheckedProposal, WantsInputs, WantsOutputs,
1111
};
1212
use payjoin::send::v2::{
13-
replay_event_log as replay_sender_event_log, Sender, SenderBuilder, SenderTypeState,
14-
V2GetContext, WithReplyKey,
13+
replay_event_log as replay_sender_event_log, SendSession, Sender, SenderBuilder, V2GetContext,
14+
WithReplyKey,
1515
};
1616
use payjoin::Uri;
1717
use tokio::sync::watch;
@@ -81,7 +81,7 @@ impl AppTrait for App {
8181
.build_recommended(fee_rate)
8282
.save(&persister)?;
8383

84-
(SenderTypeState::WithReplyKey(sender), persister)
84+
(SendSession::WithReplyKey(sender), persister)
8585
}
8686
};
8787
let mut interrupt = self.interrupt.clone();
@@ -173,11 +173,11 @@ impl AppTrait for App {
173173
impl App {
174174
async fn process_sender_session(
175175
&self,
176-
session: SenderTypeState,
176+
session: SendSession,
177177
persister: &SenderPersister,
178178
) -> Result<()> {
179179
match session {
180-
SenderTypeState::WithReplyKey(context) => {
180+
SendSession::WithReplyKey(context) => {
181181
// TODO: can we handle the fall back case in `post_original_proposal`. That way we don't have to clone here
182182
match self.post_original_proposal(context.clone(), persister).await {
183183
Ok(()) => (),
@@ -192,9 +192,9 @@ impl App {
192192
}
193193
return Ok(());
194194
}
195-
SenderTypeState::V2GetContext(context) =>
195+
SendSession::V2GetContext(context) =>
196196
self.get_proposed_payjoin_psbt(context, persister).await?,
197-
SenderTypeState::ProposalReceived(proposal) => {
197+
SendSession::ProposalReceived(proposal) => {
198198
self.process_pj_response(proposal.clone())?;
199199
return Ok(());
200200
}

payjoin-ffi/src/send/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ impl From<payjoin::send::v2::SessionEvent> for SessionEvent {
2727
}
2828

2929
#[derive(Debug, Clone)]
30-
pub struct SenderTypeState(payjoin::send::v2::SenderTypeState);
30+
pub struct SendSession(payjoin::send::v2::SendSession);
3131

32-
impl From<payjoin::send::v2::SenderTypeState> for SenderTypeState {
33-
fn from(value: payjoin::send::v2::SenderTypeState) -> Self { Self(value) }
32+
impl From<payjoin::send::v2::SendSession> for SendSession {
33+
fn from(value: payjoin::send::v2::SendSession) -> Self { Self(value) }
3434
}
3535

36-
pub fn replay_event_log<P>(persister: &P) -> Result<(SenderTypeState, SessionHistory), SenderReplayError>
36+
pub fn replay_event_log<P>(persister: &P) -> Result<(SendSession, SessionHistory), SenderReplayError>
3737
where
3838
P: SessionPersister + Clone,
3939
P::SessionEvent: Into<payjoin::send::v2::SessionEvent> + Clone,

payjoin-ffi/src/send/uni.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,17 @@ impl SenderSessionEvent {
3434
}
3535

3636
#[derive(Clone, uniffi::Enum)]
37-
pub enum SenderTypeState {
37+
pub enum SendSession {
3838
Uninitialized,
3939
WithReplyKey { inner: Arc<WithReplyKey> },
4040
V2GetContext { inner: Arc<V2GetContext> },
4141
ProposalReceived { inner: Arc<Psbt> },
4242
TerminalFailure,
4343
}
4444

45-
impl From<super::SenderTypeState> for SenderTypeState {
46-
fn from(value: super::SenderTypeState) -> Self {
47-
use payjoin::send::v2::SenderTypeState::*;
45+
impl From<super::SendSession> for SendSession {
46+
fn from(value: super::SendSession) -> Self {
47+
use payjoin::send::v2::SendSession::*;
4848
match value.0 {
4949
Uninitialized => Self::Uninitialized,
5050
WithReplyKey(inner) =>
@@ -82,13 +82,13 @@ impl SenderSessionHistory {
8282

8383
#[derive(uniffi::Object)]
8484
pub struct SenderReplayResult {
85-
state: SenderTypeState,
85+
state: SendSession,
8686
session_history: SenderSessionHistory,
8787
}
8888

8989
#[uniffi::export]
9090
impl SenderReplayResult {
91-
pub fn state(&self) -> SenderTypeState { self.state.clone() }
91+
pub fn state(&self) -> SendSession { self.state.clone() }
9292

9393
pub fn session_history(&self) -> SenderSessionHistory { self.session_history.clone() }
9494
}

payjoin/src/send/v2/mod.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -161,27 +161,29 @@ impl<State> core::ops::DerefMut for Sender<State> {
161161
fn deref_mut(&mut self) -> &mut Self::Target { &mut self.state }
162162
}
163163

164+
/// Represents the various states of a Payjoin send session during the protocol flow.
165+
///
166+
/// This provides type erasure for the send session state, allowing the session to be replayed
167+
/// and the state to be updated with the next event over a uniform interface.
164168
#[derive(Debug, Clone, PartialEq, Eq)]
165-
pub enum SenderTypeState {
169+
pub enum SendSession {
166170
Uninitialized,
167171
WithReplyKey(Sender<WithReplyKey>),
168172
V2GetContext(Sender<V2GetContext>),
169173
ProposalReceived(Psbt),
170174
TerminalFailure,
171175
}
172176

173-
impl SenderTypeState {
174-
fn process_event(self, event: SessionEvent) -> Result<SenderTypeState, ReplayError> {
177+
impl SendSession {
178+
fn process_event(self, event: SessionEvent) -> Result<SendSession, ReplayError> {
175179
match (self, event) {
176-
(
177-
SenderTypeState::Uninitialized,
178-
SessionEvent::CreatedReplyKey(sender_with_reply_key),
179-
) => Ok(SenderTypeState::WithReplyKey(Sender { state: sender_with_reply_key })),
180-
(SenderTypeState::WithReplyKey(state), SessionEvent::V2GetContext(v2_get_context)) =>
180+
(SendSession::Uninitialized, SessionEvent::CreatedReplyKey(sender_with_reply_key)) =>
181+
Ok(SendSession::WithReplyKey(Sender { state: sender_with_reply_key })),
182+
(SendSession::WithReplyKey(state), SessionEvent::V2GetContext(v2_get_context)) =>
181183
Ok(state.apply_v2_get_context(v2_get_context)),
182-
(SenderTypeState::V2GetContext(_state), SessionEvent::ProposalReceived(proposal)) =>
183-
Ok(SenderTypeState::ProposalReceived(proposal)),
184-
(_, SessionEvent::SessionInvalid(_)) => Ok(SenderTypeState::TerminalFailure),
184+
(SendSession::V2GetContext(_state), SessionEvent::ProposalReceived(proposal)) =>
185+
Ok(SendSession::ProposalReceived(proposal)),
186+
(_, SessionEvent::SessionInvalid(_)) => Ok(SendSession::TerminalFailure),
185187
(current_state, event) => Err(InternalReplayError::InvalidStateAndEvent(
186188
Box::new(current_state),
187189
Box::new(event),
@@ -303,8 +305,8 @@ impl Sender<WithReplyKey> {
303305
/// The endpoint in the Payjoin URI
304306
pub fn endpoint(&self) -> &Url { self.v1.endpoint() }
305307

306-
pub(crate) fn apply_v2_get_context(self, v2_get_context: V2GetContext) -> SenderTypeState {
307-
SenderTypeState::V2GetContext(Sender { state: v2_get_context })
308+
pub(crate) fn apply_v2_get_context(self, v2_get_context: V2GetContext) -> SendSession {
309+
SendSession::V2GetContext(Sender { state: v2_get_context })
308310
}
309311
}
310312

payjoin/src/send/v2/session.rs

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

33
use super::WithReplyKey;
44
use crate::persist::SessionPersister;
5-
use crate::send::v2::{SenderTypeState, V2GetContext};
5+
use crate::send::v2::{SendSession, V2GetContext};
66
use crate::ImplementationError;
77
/// Errors that can occur when replaying a sender event log
88
#[derive(Debug)]
@@ -29,12 +29,12 @@ impl From<InternalReplayError> for ReplayError {
2929
#[derive(Debug)]
3030
pub(crate) enum InternalReplayError {
3131
/// Invalid combination of state and event
32-
InvalidStateAndEvent(Box<SenderTypeState>, Box<SessionEvent>),
32+
InvalidStateAndEvent(Box<SendSession>, Box<SessionEvent>),
3333
/// Application storage error
3434
PersistenceFailure(ImplementationError),
3535
}
3636

37-
pub fn replay_event_log<P>(persister: &P) -> Result<(SenderTypeState, SessionHistory), ReplayError>
37+
pub fn replay_event_log<P>(persister: &P) -> Result<(SendSession, SessionHistory), ReplayError>
3838
where
3939
P: SessionPersister + Clone,
4040
P::SessionEvent: Into<SessionEvent> + Clone,
@@ -43,7 +43,7 @@ where
4343
.load()
4444
.map_err(|e| InternalReplayError::PersistenceFailure(Box::new(e).into()))?;
4545

46-
let mut sender = SenderTypeState::Uninitialized;
46+
let mut sender = SendSession::Uninitialized;
4747
let mut history = SessionHistory::default();
4848
for log in logs {
4949
history.events.push(log.clone().into());
@@ -163,7 +163,7 @@ mod tests {
163163
struct SessionHistoryTest {
164164
events: Vec<SessionEvent>,
165165
expected_session_history: SessionHistoryExpectedOutcome,
166-
expected_sender_state: SenderTypeState,
166+
expected_sender_state: SendSession,
167167
}
168168

169169
fn run_session_history_test(test: SessionHistoryTest) {
@@ -203,7 +203,7 @@ mod tests {
203203
fallback_tx: Some(fallback_tx),
204204
endpoint: Some(endpoint),
205205
},
206-
expected_sender_state: SenderTypeState::WithReplyKey(sender),
206+
expected_sender_state: SendSession::WithReplyKey(sender),
207207
};
208208
run_session_history_test(test);
209209
}

0 commit comments

Comments
 (0)