@@ -83,10 +83,13 @@ fn subdir_path_from_pubkey(pubkey: &HpkePublicKey) -> ShortId {
8383}
8484
8585/// Represents the various states of a Payjoin receiver session during the protocol flow.
86- /// Each variant wraps a `Receiver` with a specific state type, except for [`ReceiverTypeState::TerminalFailure`] which
87- /// indicates the session has ended or is invalid.
86+ /// Each variant wraps a `Receiver` with a specific state type, except for [`ReceiveSession::Uninitialized`] which
87+ /// has no context yet and [`ReceiveSession::TerminalFailure`] which indicates the session has ended or is invalid.
88+ ///
89+ /// This provides type erasure for the receive session state, allowing for the session to be replayed
90+ /// and the state to be updated with the next event over a uniform interface.
8891#[ derive( Debug , Clone , PartialEq ) ]
89- pub enum ReceiverTypeState {
92+ pub enum ReceiveSession {
9093 Uninitialized ( Receiver < UninitializedReceiver > ) ,
9194 Initialized ( Receiver < Initialized > ) ,
9295 UncheckedProposal ( Receiver < UncheckedProposal > ) ,
@@ -100,50 +103,46 @@ pub enum ReceiverTypeState {
100103 TerminalFailure ,
101104}
102105
103- impl ReceiverTypeState {
104- fn process_event ( self , event : SessionEvent ) -> Result < ReceiverTypeState , ReplayError > {
106+ impl ReceiveSession {
107+ fn process_event ( self , event : SessionEvent ) -> Result < ReceiveSession , ReplayError > {
105108 match ( self , event) {
106- ( ReceiverTypeState :: Uninitialized ( _) , SessionEvent :: Created ( context) ) =>
107- Ok ( ReceiverTypeState :: Initialized ( Receiver { state : Initialized { context } } ) ) ,
109+ ( ReceiveSession :: Uninitialized ( _) , SessionEvent :: Created ( context) ) =>
110+ Ok ( ReceiveSession :: Initialized ( Receiver { state : Initialized { context } } ) ) ,
108111
109112 (
110- ReceiverTypeState :: Initialized ( state) ,
113+ ReceiveSession :: Initialized ( state) ,
111114 SessionEvent :: UncheckedProposal ( ( proposal, reply_key) ) ,
112115 ) => Ok ( state. apply_unchecked_from_payload ( proposal, reply_key) ?) ,
113116
114- (
115- ReceiverTypeState :: UncheckedProposal ( state) ,
116- SessionEvent :: MaybeInputsOwned ( inputs) ,
117- ) => Ok ( state. apply_maybe_inputs_owned ( inputs) ) ,
117+ ( ReceiveSession :: UncheckedProposal ( state) , SessionEvent :: MaybeInputsOwned ( inputs) ) =>
118+ Ok ( state. apply_maybe_inputs_owned ( inputs) ) ,
118119
119120 (
120- ReceiverTypeState :: MaybeInputsOwned ( state) ,
121+ ReceiveSession :: MaybeInputsOwned ( state) ,
121122 SessionEvent :: MaybeInputsSeen ( maybe_inputs_seen) ,
122123 ) => Ok ( state. apply_maybe_inputs_seen ( maybe_inputs_seen) ) ,
123124
124125 (
125- ReceiverTypeState :: MaybeInputsSeen ( state) ,
126+ ReceiveSession :: MaybeInputsSeen ( state) ,
126127 SessionEvent :: OutputsUnknown ( outputs_unknown) ,
127128 ) => Ok ( state. apply_outputs_unknown ( outputs_unknown) ) ,
128129
129- (
130- ReceiverTypeState :: OutputsUnknown ( state) ,
131- SessionEvent :: WantsOutputs ( wants_outputs) ,
132- ) => Ok ( state. apply_wants_outputs ( wants_outputs) ) ,
130+ ( ReceiveSession :: OutputsUnknown ( state) , SessionEvent :: WantsOutputs ( wants_outputs) ) =>
131+ Ok ( state. apply_wants_outputs ( wants_outputs) ) ,
133132
134- ( ReceiverTypeState :: WantsOutputs ( state) , SessionEvent :: WantsInputs ( wants_inputs) ) =>
133+ ( ReceiveSession :: WantsOutputs ( state) , SessionEvent :: WantsInputs ( wants_inputs) ) =>
135134 Ok ( state. apply_wants_inputs ( wants_inputs) ) ,
136135
137136 (
138- ReceiverTypeState :: WantsInputs ( state) ,
137+ ReceiveSession :: WantsInputs ( state) ,
139138 SessionEvent :: ProvisionalProposal ( provisional_proposal) ,
140139 ) => Ok ( state. apply_provisional_proposal ( provisional_proposal) ) ,
141140
142141 (
143- ReceiverTypeState :: ProvisionalProposal ( state) ,
142+ ReceiveSession :: ProvisionalProposal ( state) ,
144143 SessionEvent :: PayjoinProposal ( payjoin_proposal) ,
145144 ) => Ok ( state. apply_payjoin_proposal ( payjoin_proposal) ) ,
146- ( _, SessionEvent :: SessionInvalid ( _, _) ) => Ok ( ReceiverTypeState :: TerminalFailure ) ,
145+ ( _, SessionEvent :: SessionInvalid ( _, _) ) => Ok ( ReceiveSession :: TerminalFailure ) ,
147146 ( current_state, event) => Err ( InternalReplayError :: InvalidStateAndEvent (
148147 Box :: new ( current_state) ,
149148 Box :: new ( event) ,
@@ -404,7 +403,7 @@ impl Receiver<Initialized> {
404403 self ,
405404 event : v1:: UncheckedProposal ,
406405 reply_key : Option < HpkePublicKey > ,
407- ) -> Result < ReceiverTypeState , InternalReplayError > {
406+ ) -> Result < ReceiveSession , InternalReplayError > {
408407 if self . state . context . expiry < SystemTime :: now ( ) {
409408 // Session is expired, close the session
410409 return Err ( InternalReplayError :: SessionExpired ( self . state . context . expiry ) ) ;
@@ -417,7 +416,7 @@ impl Receiver<Initialized> {
417416 } ,
418417 } ;
419418
420- Ok ( ReceiverTypeState :: UncheckedProposal ( new_state) )
419+ Ok ( ReceiveSession :: UncheckedProposal ( new_state) )
421420 }
422421}
423422
@@ -485,10 +484,10 @@ impl Receiver<UncheckedProposal> {
485484 )
486485 }
487486
488- pub ( crate ) fn apply_maybe_inputs_owned ( self , v1 : v1:: MaybeInputsOwned ) -> ReceiverTypeState {
487+ pub ( crate ) fn apply_maybe_inputs_owned ( self , v1 : v1:: MaybeInputsOwned ) -> ReceiveSession {
489488 let new_state =
490489 Receiver { state : MaybeInputsOwned { v1, context : self . state . context . clone ( ) } } ;
491- ReceiverTypeState :: MaybeInputsOwned ( new_state)
490+ ReceiveSession :: MaybeInputsOwned ( new_state)
492491 }
493492}
494493
@@ -538,10 +537,10 @@ impl Receiver<MaybeInputsOwned> {
538537 )
539538 }
540539
541- pub ( crate ) fn apply_maybe_inputs_seen ( self , v1 : v1:: MaybeInputsSeen ) -> ReceiverTypeState {
540+ pub ( crate ) fn apply_maybe_inputs_seen ( self , v1 : v1:: MaybeInputsSeen ) -> ReceiveSession {
542541 let new_state =
543542 Receiver { state : MaybeInputsSeen { v1, context : self . state . context . clone ( ) } } ;
544- ReceiverTypeState :: MaybeInputsSeen ( new_state)
543+ ReceiveSession :: MaybeInputsSeen ( new_state)
545544 }
546545}
547546
@@ -584,10 +583,10 @@ impl Receiver<MaybeInputsSeen> {
584583 )
585584 }
586585
587- pub ( crate ) fn apply_outputs_unknown ( self , inner : v1:: OutputsUnknown ) -> ReceiverTypeState {
586+ pub ( crate ) fn apply_outputs_unknown ( self , inner : v1:: OutputsUnknown ) -> ReceiveSession {
588587 let new_state =
589588 Receiver { state : OutputsUnknown { inner, context : self . state . context . clone ( ) } } ;
590- ReceiverTypeState :: OutputsUnknown ( new_state)
589+ ReceiveSession :: OutputsUnknown ( new_state)
591590 }
592591}
593592
@@ -629,10 +628,10 @@ impl Receiver<OutputsUnknown> {
629628 )
630629 }
631630
632- pub ( crate ) fn apply_wants_outputs ( self , v1 : v1:: WantsOutputs ) -> ReceiverTypeState {
631+ pub ( crate ) fn apply_wants_outputs ( self , v1 : v1:: WantsOutputs ) -> ReceiveSession {
633632 let new_state =
634633 Receiver { state : WantsOutputs { v1, context : self . state . context . clone ( ) } } ;
635- ReceiverTypeState :: WantsOutputs ( new_state)
634+ ReceiveSession :: WantsOutputs ( new_state)
636635 }
637636}
638637
@@ -684,9 +683,9 @@ impl Receiver<WantsOutputs> {
684683 )
685684 }
686685
687- pub ( crate ) fn apply_wants_inputs ( self , v1 : v1:: WantsInputs ) -> ReceiverTypeState {
686+ pub ( crate ) fn apply_wants_inputs ( self , v1 : v1:: WantsInputs ) -> ReceiveSession {
688687 let new_state = Receiver { state : WantsInputs { v1, context : self . state . context . clone ( ) } } ;
689- ReceiverTypeState :: WantsInputs ( new_state)
688+ ReceiveSession :: WantsInputs ( new_state)
690689 }
691690}
692691
@@ -742,13 +741,10 @@ impl Receiver<WantsInputs> {
742741 )
743742 }
744743
745- pub ( crate ) fn apply_provisional_proposal (
746- self ,
747- v1 : v1:: ProvisionalProposal ,
748- ) -> ReceiverTypeState {
744+ pub ( crate ) fn apply_provisional_proposal ( self , v1 : v1:: ProvisionalProposal ) -> ReceiveSession {
749745 let new_state =
750746 Receiver { state : ProvisionalProposal { v1, context : self . state . context . clone ( ) } } ;
751- ReceiverTypeState :: ProvisionalProposal ( new_state)
747+ ReceiveSession :: ProvisionalProposal ( new_state)
752748 }
753749}
754750
@@ -795,10 +791,10 @@ impl Receiver<ProvisionalProposal> {
795791 )
796792 }
797793
798- pub ( crate ) fn apply_payjoin_proposal ( self , v1 : v1:: PayjoinProposal ) -> ReceiverTypeState {
794+ pub ( crate ) fn apply_payjoin_proposal ( self , v1 : v1:: PayjoinProposal ) -> ReceiveSession {
799795 let new_state =
800796 Receiver { state : PayjoinProposal { v1, context : self . state . context . clone ( ) } } ;
801- ReceiverTypeState :: PayjoinProposal ( new_state)
797+ ReceiveSession :: PayjoinProposal ( new_state)
802798 }
803799}
804800
0 commit comments