|
| 1 | +package reservation |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/btcsuite/btcd/btcec/v2" |
| 7 | + "github.com/btcsuite/btcd/btcutil" |
| 8 | + "github.com/lightninglabs/loop/fsm" |
| 9 | + looprpc "github.com/lightninglabs/loop/swapserverrpc" |
| 10 | +) |
| 11 | + |
| 12 | +// InitReservationContext contains the request parameters for a reservation. |
| 13 | +type InitReservationContext struct { |
| 14 | + reservationID ID |
| 15 | + serverPubkey *btcec.PublicKey |
| 16 | + value btcutil.Amount |
| 17 | + expiry uint32 |
| 18 | + heightHint uint32 |
| 19 | +} |
| 20 | + |
| 21 | +// InitAction is the action that is executed when the reservation state machine |
| 22 | +// is initialized. It creates the reservation in the database and dispatches the |
| 23 | +// payment to the server. |
| 24 | +func (r *FSM) InitAction(eventCtx fsm.EventContext) fsm.EventType { |
| 25 | + // Check if the context is of the correct type. |
| 26 | + reservationRequest, ok := eventCtx.(*InitReservationContext) |
| 27 | + if !ok { |
| 28 | + return r.HandleError(fsm.ErrInvalidContextType) |
| 29 | + } |
| 30 | + |
| 31 | + keyRes, err := r.cfg.Wallet.DeriveNextKey( |
| 32 | + r.ctx, KeyFamily, |
| 33 | + ) |
| 34 | + if err != nil { |
| 35 | + return r.HandleError(err) |
| 36 | + } |
| 37 | + |
| 38 | + // Send the client reservation details to the server. |
| 39 | + log.Debugf("Dispatching reservation to server: %x", |
| 40 | + reservationRequest.reservationID) |
| 41 | + |
| 42 | + request := &looprpc.ServerOpenReservationRequest{ |
| 43 | + ReservationId: reservationRequest.reservationID[:], |
| 44 | + ClientKey: keyRes.PubKey.SerializeCompressed(), |
| 45 | + } |
| 46 | + |
| 47 | + _, err = r.cfg.ReservationClient.OpenReservation(r.ctx, request) |
| 48 | + if err != nil { |
| 49 | + return r.HandleError(err) |
| 50 | + } |
| 51 | + |
| 52 | + reservation, err := NewReservation( |
| 53 | + reservationRequest.reservationID, |
| 54 | + reservationRequest.serverPubkey, |
| 55 | + keyRes.PubKey, |
| 56 | + reservationRequest.value, |
| 57 | + reservationRequest.expiry, |
| 58 | + reservationRequest.heightHint, |
| 59 | + keyRes.KeyLocator, |
| 60 | + ) |
| 61 | + if err != nil { |
| 62 | + return r.HandleError(err) |
| 63 | + } |
| 64 | + |
| 65 | + r.reservation = reservation |
| 66 | + |
| 67 | + // Create the reservation in the database. |
| 68 | + err = r.cfg.Store.CreateReservation(r.ctx, reservation) |
| 69 | + if err != nil { |
| 70 | + return r.HandleError(err) |
| 71 | + } |
| 72 | + |
| 73 | + return OnBroadcast |
| 74 | +} |
| 75 | + |
| 76 | +// SubscribeToConfirmationAction is the action that is executed when the |
| 77 | +// reservation is waiting for confirmation. It subscribes to the confirmation |
| 78 | +// of the reservation transaction. |
| 79 | +func (r *FSM) SubscribeToConfirmationAction(_ fsm.EventContext) fsm.EventType { |
| 80 | + pkscript, err := r.reservation.GetPkScript() |
| 81 | + if err != nil { |
| 82 | + return r.HandleError(err) |
| 83 | + } |
| 84 | + |
| 85 | + callCtx, cancel := context.WithCancel(r.ctx) |
| 86 | + defer cancel() |
| 87 | + |
| 88 | + // Subscribe to the confirmation of the reservation transaction. |
| 89 | + log.Debugf("Subscribing to conf for reservation: %x pkscript: %x, "+ |
| 90 | + "initiation height: %v", r.reservation.ID, pkscript, |
| 91 | + r.reservation.InitiationHeight) |
| 92 | + |
| 93 | + confChan, errConfChan, err := r.cfg.ChainNotifier.RegisterConfirmationsNtfn( |
| 94 | + callCtx, nil, pkscript, DefaultConfTarget, |
| 95 | + r.reservation.InitiationHeight, |
| 96 | + ) |
| 97 | + if err != nil { |
| 98 | + r.Errorf("unable to subscribe to conf notification: %v", err) |
| 99 | + return r.HandleError(err) |
| 100 | + } |
| 101 | + |
| 102 | + blockChan, errBlockChan, err := r.cfg.ChainNotifier.RegisterBlockEpochNtfn( |
| 103 | + callCtx, |
| 104 | + ) |
| 105 | + if err != nil { |
| 106 | + r.Errorf("unable to subscribe to block notifications: %v", err) |
| 107 | + return r.HandleError(err) |
| 108 | + } |
| 109 | + |
| 110 | + // We'll now wait for the confirmation of the reservation transaction. |
| 111 | + for { |
| 112 | + select { |
| 113 | + case err := <-errConfChan: |
| 114 | + r.Errorf("conf subscription error: %v", err) |
| 115 | + return r.HandleError(err) |
| 116 | + |
| 117 | + case err := <-errBlockChan: |
| 118 | + r.Errorf("block subscription error: %v", err) |
| 119 | + return r.HandleError(err) |
| 120 | + |
| 121 | + case confInfo := <-confChan: |
| 122 | + r.Debugf("reservation confirmed: %v", confInfo) |
| 123 | + outpoint, err := r.reservation.findReservationOutput( |
| 124 | + confInfo.Tx, |
| 125 | + ) |
| 126 | + if err != nil { |
| 127 | + return r.HandleError(err) |
| 128 | + } |
| 129 | + |
| 130 | + r.reservation.ConfirmationHeight = confInfo.BlockHeight |
| 131 | + r.reservation.Outpoint = outpoint |
| 132 | + |
| 133 | + return OnConfirmed |
| 134 | + |
| 135 | + case block := <-blockChan: |
| 136 | + r.Debugf("block received: %v expiry: %v", block, |
| 137 | + r.reservation.Expiry) |
| 138 | + |
| 139 | + if uint32(block) >= r.reservation.Expiry { |
| 140 | + return OnTimedOut |
| 141 | + } |
| 142 | + |
| 143 | + case <-r.ctx.Done(): |
| 144 | + return fsm.NoOp |
| 145 | + } |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +// ReservationConfirmedAction waits for the reservation to be either expired or |
| 150 | +// waits for other actions to happen. |
| 151 | +func (r *FSM) ReservationConfirmedAction(_ fsm.EventContext) fsm.EventType { |
| 152 | + blockHeightChan, errEpochChan, err := r.cfg.ChainNotifier. |
| 153 | + RegisterBlockEpochNtfn(r.ctx) |
| 154 | + if err != nil { |
| 155 | + return r.HandleError(err) |
| 156 | + } |
| 157 | + |
| 158 | + for { |
| 159 | + select { |
| 160 | + case err := <-errEpochChan: |
| 161 | + return r.HandleError(err) |
| 162 | + |
| 163 | + case blockHeight := <-blockHeightChan: |
| 164 | + expired := blockHeight >= int32(r.reservation.Expiry) |
| 165 | + if expired { |
| 166 | + r.Debugf("Reservation %v expired", |
| 167 | + r.reservation.ID) |
| 168 | + |
| 169 | + return OnTimedOut |
| 170 | + } |
| 171 | + |
| 172 | + case <-r.ctx.Done(): |
| 173 | + return fsm.NoOp |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments