|
| 1 | +package instantout |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "sync" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/lightninglabs/loop/instantout/reservation" |
| 11 | + "github.com/lightningnetwork/lnd/lntypes" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + defaultStateWaitTime = 30 * time.Second |
| 16 | + defaultCltv = 100 |
| 17 | + ErrSwapDoesNotExist = errors.New("swap does not exist") |
| 18 | +) |
| 19 | + |
| 20 | +// Manager manages the instantout state machines. |
| 21 | +type Manager struct { |
| 22 | + // cfg contains all the services that the reservation manager needs to |
| 23 | + // operate. |
| 24 | + cfg *Config |
| 25 | + |
| 26 | + // activeInstantOuts contains all the active instantouts. |
| 27 | + activeInstantOuts map[lntypes.Hash]*FSM |
| 28 | + |
| 29 | + // currentHeight stores the currently best known block height. |
| 30 | + currentHeight int32 |
| 31 | + |
| 32 | + // blockEpochChan receives new block heights. |
| 33 | + blockEpochChan chan int32 |
| 34 | + |
| 35 | + runCtx context.Context |
| 36 | + |
| 37 | + sync.Mutex |
| 38 | +} |
| 39 | + |
| 40 | +// NewInstantOutManager creates a new instantout manager. |
| 41 | +func NewInstantOutManager(cfg *Config) *Manager { |
| 42 | + return &Manager{ |
| 43 | + cfg: cfg, |
| 44 | + activeInstantOuts: make(map[lntypes.Hash]*FSM), |
| 45 | + blockEpochChan: make(chan int32), |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// Run runs the instantout manager. |
| 50 | +func (m *Manager) Run(ctx context.Context, initChan chan struct{}, |
| 51 | + height int32) error { |
| 52 | + |
| 53 | + log.Debugf("Starting instantout manager") |
| 54 | + defer func() { |
| 55 | + log.Debugf("Stopping instantout manager") |
| 56 | + }() |
| 57 | + |
| 58 | + runCtx, cancel := context.WithCancel(ctx) |
| 59 | + defer cancel() |
| 60 | + |
| 61 | + m.runCtx = runCtx |
| 62 | + m.currentHeight = height |
| 63 | + |
| 64 | + err := m.recoverInstantOuts(runCtx) |
| 65 | + if err != nil { |
| 66 | + close(initChan) |
| 67 | + return err |
| 68 | + } |
| 69 | + |
| 70 | + newBlockChan, newBlockErrChan, err := m.cfg.ChainNotifier. |
| 71 | + RegisterBlockEpochNtfn(ctx) |
| 72 | + if err != nil { |
| 73 | + close(initChan) |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + close(initChan) |
| 78 | + |
| 79 | + for { |
| 80 | + select { |
| 81 | + case <-runCtx.Done(): |
| 82 | + return nil |
| 83 | + |
| 84 | + case height := <-newBlockChan: |
| 85 | + m.Lock() |
| 86 | + m.currentHeight = height |
| 87 | + m.Unlock() |
| 88 | + |
| 89 | + case err := <-newBlockErrChan: |
| 90 | + return err |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// recoverInstantOuts recovers all the active instantouts from the database. |
| 96 | +func (m *Manager) recoverInstantOuts(ctx context.Context) error { |
| 97 | + // Fetch all the active instantouts from the database. |
| 98 | + activeInstantOuts, err := m.cfg.Store.ListInstantLoopOuts(ctx) |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + |
| 103 | + for _, instantOut := range activeInstantOuts { |
| 104 | + if isFinalState(instantOut.State) { |
| 105 | + continue |
| 106 | + } |
| 107 | + |
| 108 | + log.Debugf("Recovering instantout %v", instantOut.SwapHash) |
| 109 | + |
| 110 | + instantOutFSM, err := NewFSMFromInstantOut( |
| 111 | + ctx, m.cfg, instantOut, |
| 112 | + ) |
| 113 | + if err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + |
| 117 | + m.activeInstantOuts[instantOut.SwapHash] = instantOutFSM |
| 118 | + |
| 119 | + // As SendEvent can block, we'll start a goroutine to process |
| 120 | + // the event. |
| 121 | + go func() { |
| 122 | + err := instantOutFSM.SendEvent(OnRecover, nil) |
| 123 | + if err != nil { |
| 124 | + log.Errorf("FSM %v Error sending recover "+ |
| 125 | + "event %v, state: %v", |
| 126 | + instantOutFSM.InstantOut.SwapHash, err, |
| 127 | + instantOutFSM.InstantOut.State) |
| 128 | + } |
| 129 | + }() |
| 130 | + } |
| 131 | + |
| 132 | + return nil |
| 133 | +} |
| 134 | + |
| 135 | +// NewInstantOut creates a new instantout. |
| 136 | +func (m *Manager) NewInstantOut(ctx context.Context, |
| 137 | + reservations []reservation.ID) (*FSM, error) { |
| 138 | + |
| 139 | + m.Lock() |
| 140 | + // Create the instantout request. |
| 141 | + request := &InitInstantOutCtx{ |
| 142 | + cltvExpiry: m.currentHeight + int32(defaultCltv), |
| 143 | + reservations: reservations, |
| 144 | + initationHeight: m.currentHeight, |
| 145 | + protocolVersion: CurrentProtocolVersion(), |
| 146 | + } |
| 147 | + |
| 148 | + instantOut, err := NewFSM( |
| 149 | + m.runCtx, m.cfg, ProtocolVersionFullReservation, |
| 150 | + ) |
| 151 | + if err != nil { |
| 152 | + m.Unlock() |
| 153 | + return nil, err |
| 154 | + } |
| 155 | + m.activeInstantOuts[instantOut.InstantOut.SwapHash] = instantOut |
| 156 | + m.Unlock() |
| 157 | + |
| 158 | + // Start the instantout FSM. |
| 159 | + go func() { |
| 160 | + err := instantOut.SendEvent(OnStart, request) |
| 161 | + if err != nil { |
| 162 | + log.Errorf("Error sending event: %v", err) |
| 163 | + } |
| 164 | + }() |
| 165 | + |
| 166 | + // If everything went well, we'll wait for the instant out to be |
| 167 | + // waiting for sweepless sweep to be confirmed. |
| 168 | + err = instantOut.DefaultObserver.WaitForState( |
| 169 | + ctx, defaultStateWaitTime, WaitForSweeplessSweepConfirmed, |
| 170 | + ) |
| 171 | + if err != nil { |
| 172 | + if instantOut.LastActionError != nil { |
| 173 | + return instantOut, fmt.Errorf( |
| 174 | + "error waiting for sweepless sweep "+ |
| 175 | + "confirmed: %w", instantOut.LastActionError, |
| 176 | + ) |
| 177 | + } |
| 178 | + return instantOut, nil |
| 179 | + } |
| 180 | + |
| 181 | + return instantOut, nil |
| 182 | +} |
| 183 | + |
| 184 | +// GetActiveInstantOut returns an active instant out. |
| 185 | +func (m *Manager) GetActiveInstantOut(swapHash lntypes.Hash) (*FSM, error) { |
| 186 | + m.Lock() |
| 187 | + defer m.Unlock() |
| 188 | + |
| 189 | + fsm, ok := m.activeInstantOuts[swapHash] |
| 190 | + if !ok { |
| 191 | + return nil, ErrSwapDoesNotExist |
| 192 | + } |
| 193 | + |
| 194 | + // If the instant out is in a final state, we'll remove it from the |
| 195 | + // active instant outs. |
| 196 | + if isFinalState(fsm.InstantOut.State) { |
| 197 | + delete(m.activeInstantOuts, swapHash) |
| 198 | + } |
| 199 | + |
| 200 | + return fsm, nil |
| 201 | +} |
0 commit comments