2929var (
3030 Deposited = fsm .StateType ("Deposited" )
3131
32+ Withdrawing = fsm .StateType ("Withdrawing" )
33+
34+ Withdrawn = fsm .StateType ("Withdrawn" )
35+
3236 PublishExpiredDeposit = fsm .StateType ("PublishExpiredDeposit" )
3337
3438 WaitForExpirySweep = fsm .StateType ("WaitForExpirySweep" )
@@ -40,11 +44,13 @@ var (
4044
4145// Events.
4246var (
43- OnStart = fsm .EventType ("OnStart" )
44- OnExpiry = fsm .EventType ("OnExpiry" )
45- OnExpiryPublished = fsm .EventType ("OnExpiryPublished" )
46- OnExpirySwept = fsm .EventType ("OnExpirySwept" )
47- OnRecover = fsm .EventType ("OnRecover" )
47+ OnStart = fsm .EventType ("OnStart" )
48+ OnWithdrawInitiated = fsm .EventType ("OnWithdrawInitiated" )
49+ OnWithdrawn = fsm .EventType ("OnWithdrawn" )
50+ OnExpiry = fsm .EventType ("OnExpiry" )
51+ OnExpiryPublished = fsm .EventType ("OnExpiryPublished" )
52+ OnExpirySwept = fsm .EventType ("OnExpirySwept" )
53+ OnRecover = fsm .EventType ("OnRecover" )
4854)
4955
5056// FSM is the state machine that handles the instant out.
@@ -118,13 +124,7 @@ func NewFSM(ctx context.Context, deposit *Deposit, cfg *ManagerConfig,
118124 for {
119125 select {
120126 case currentHeight := <- depoFsm .blockNtfnChan :
121- err := depoFsm .handleBlockNotification (
122- currentHeight ,
123- )
124- if err != nil {
125- log .Errorf ("error handling block " +
126- "notification: %v" , err )
127- }
127+ depoFsm .handleBlockNotification (currentHeight )
128128
129129 case <- ctx .Done ():
130130 return
@@ -138,15 +138,10 @@ func NewFSM(ctx context.Context, deposit *Deposit, cfg *ManagerConfig,
138138// handleBlockNotification inspects the current block height and sends the
139139// OnExpiry event to publish the expiry sweep transaction if the deposit timed
140140// out, or it republishes the expiry sweep transaction if it was not yet swept.
141- func (f * FSM ) handleBlockNotification (currentHeight uint32 ) error {
142- params , err := f .cfg .AddressManager .GetStaticAddressParameters (f .ctx )
143- if err != nil {
144- return err
145- }
146-
141+ func (f * FSM ) handleBlockNotification (currentHeight uint32 ) {
147142 // If the deposit is expired but not yet sufficiently confirmed, we
148143 // republish the expiry sweep transaction.
149- if f .deposit .IsExpired (currentHeight , params .Expiry ) {
144+ if f .deposit .IsExpired (currentHeight , f . params .Expiry ) {
150145 if f .deposit .IsInState (WaitForExpirySweep ) {
151146 f .PublishDepositExpirySweepAction (nil )
152147 } else {
@@ -159,8 +154,6 @@ func (f *FSM) handleBlockNotification(currentHeight uint32) error {
159154 }()
160155 }
161156 }
162-
163- return nil
164157}
165158
166159// DepositStatesV0 returns the states a deposit can be in.
@@ -174,8 +167,9 @@ func (f *FSM) DepositStatesV0() fsm.States {
174167 },
175168 Deposited : fsm.State {
176169 Transitions : fsm.Transitions {
177- OnExpiry : PublishExpiredDeposit ,
178- OnRecover : Deposited ,
170+ OnExpiry : PublishExpiredDeposit ,
171+ OnWithdrawInitiated : Withdrawing ,
172+ OnRecover : Deposited ,
179173 },
180174 Action : fsm .NoOpAction ,
181175 },
@@ -210,6 +204,36 @@ func (f *FSM) DepositStatesV0() fsm.States {
210204 },
211205 Action : f .SweptExpiredDepositAction ,
212206 },
207+ Withdrawing : fsm.State {
208+ Transitions : fsm.Transitions {
209+ OnWithdrawn : Withdrawn ,
210+ // Upon recovery, we go back to the Deposited
211+ // state. The deposit by then has a withdrawal
212+ // address stamped to it which will cause it to
213+ // transition into the Withdrawing state again.
214+ OnRecover : Deposited ,
215+
216+ // A precondition for the Withdrawing state is
217+ // that the withdrawal transaction has been
218+ // broadcast. If the deposit expires while the
219+ // withdrawal isn't confirmed, we can ignore the
220+ // expiry.
221+ OnExpiry : Withdrawing ,
222+
223+ // If the withdrawal failed we go back to
224+ // Deposited, hoping that another withdrawal
225+ // attempt will be successful. Alternatively,
226+ // the client can wait for the timeout sweep.
227+ fsm .OnError : Deposited ,
228+ },
229+ Action : fsm .NoOpAction ,
230+ },
231+ Withdrawn : fsm.State {
232+ Transitions : fsm.Transitions {
233+ OnExpiry : Expired ,
234+ },
235+ Action : f .WithdrawnDepositAction ,
236+ },
213237 Failed : fsm.State {
214238 Transitions : fsm.Transitions {
215239 OnExpiry : Failed ,
0 commit comments