2020 ErrSwapDoesNotExist = errors .New ("swap does not exist" )
2121)
2222
23+ // InitInstantOutRequest is a request to initialize an instant out.
24+ type InitInstantOutRequest struct {
25+ // reqCtx is the eventCtx for the OnStart event.
26+ reqCtx * InitInstantOutCtx
27+ // errResChan is a channel that will receive the result of the
28+ // initialization.
29+ errResChan chan error
30+ // fsmResChan is a channel that will receive the FSM.
31+ fsmResChan chan * FSM
32+ }
33+
2334// Manager manages the instantout state machines.
2435type Manager struct {
2536 // cfg contains all the services that the reservation manager needs to
@@ -29,23 +40,25 @@ type Manager struct {
2940 // activeInstantOuts contains all the active instantouts.
3041 activeInstantOuts map [lntypes.Hash ]* FSM
3142
43+ // instantOutInitRequests contains all the instant out init requests.
44+ instantOutInitRequests chan * InitInstantOutRequest
45+
3246 // currentHeight stores the currently best known block height.
3347 currentHeight int32
3448
3549 // blockEpochChan receives new block heights.
3650 blockEpochChan chan int32
3751
38- runCtx context.Context
39-
4052 sync.Mutex
4153}
4254
4355// NewInstantOutManager creates a new instantout manager.
4456func NewInstantOutManager (cfg * Config ) * Manager {
4557 return & Manager {
46- cfg : cfg ,
47- activeInstantOuts : make (map [lntypes.Hash ]* FSM ),
48- blockEpochChan : make (chan int32 ),
58+ cfg : cfg ,
59+ activeInstantOuts : make (map [lntypes.Hash ]* FSM ),
60+ blockEpochChan : make (chan int32 ),
61+ instantOutInitRequests : make (chan * InitInstantOutRequest ),
4962 }
5063}
5164
@@ -61,7 +74,6 @@ func (m *Manager) Run(ctx context.Context, initChan chan struct{},
6174 runCtx , cancel := context .WithCancel (ctx )
6275 defer cancel ()
6376
64- m .runCtx = runCtx
6577 m .currentHeight = height
6678
6779 err := m .recoverInstantOuts (runCtx )
@@ -89,6 +101,28 @@ func (m *Manager) Run(ctx context.Context, initChan chan struct{},
89101 m .currentHeight = height
90102 m .Unlock ()
91103
104+ case initReq := <- m .instantOutInitRequests :
105+ m .Lock ()
106+ instantOut , err := NewFSM (
107+ m .cfg , ProtocolVersionFullReservation ,
108+ )
109+ if err != nil {
110+ m .Unlock ()
111+ initReq .errResChan <- err
112+ continue
113+ }
114+ m .activeInstantOuts [instantOut .InstantOut .SwapHash ] = instantOut
115+ m .Unlock ()
116+
117+ // Start the instantout FSM.
118+ go func () {
119+ err := instantOut .SendEvent (runCtx , OnStart , initReq .reqCtx )
120+ if err != nil {
121+ log .Errorf ("Error sending event: %v" , err )
122+ }
123+ }()
124+ initReq .fsmResChan <- instantOut
125+
92126 case err := <- newBlockErrChan :
93127 return err
94128 }
@@ -152,33 +186,30 @@ func (m *Manager) NewInstantOut(ctx context.Context,
152186 }
153187 }
154188
155- m .Lock ()
156189 // Create the instantout request.
157- request := & InitInstantOutCtx {
158- cltvExpiry : m .currentHeight + int32 (defaultCltv ),
159- reservations : reservations ,
160- initationHeight : m .currentHeight ,
161- protocolVersion : CurrentProtocolVersion (),
162- sweepAddress : sweepAddr ,
190+ request := & InitInstantOutRequest {
191+ reqCtx : & InitInstantOutCtx {
192+ cltvExpiry : m .currentHeight + int32 (defaultCltv ),
193+ reservations : reservations ,
194+ initationHeight : m .currentHeight ,
195+ protocolVersion : CurrentProtocolVersion (),
196+ sweepAddress : sweepAddr ,
197+ },
198+ errResChan : make (chan error ),
199+ fsmResChan : make (chan * FSM ),
163200 }
164201
165- instantOut , err := NewFSM (
166- m .cfg , ProtocolVersionFullReservation ,
167- )
168- if err != nil {
169- m .Unlock ()
202+ m .instantOutInitRequests <- request
203+
204+ var instantOut * FSM
205+
206+ select {
207+ case err := <- request .errResChan :
170208 return nil , err
171- }
172- m .activeInstantOuts [instantOut .InstantOut .SwapHash ] = instantOut
173- m .Unlock ()
174209
175- // Start the instantout FSM.
176- go func () {
177- err := instantOut .SendEvent (m .runCtx , OnStart , request )
178- if err != nil {
179- log .Errorf ("Error sending event: %v" , err )
180- }
181- }()
210+ case instantOut = <- request .fsmResChan :
211+
212+ }
182213
183214 // If everything went well, we'll wait for the instant out to be
184215 // waiting for sweepless sweep to be confirmed.
0 commit comments