@@ -3079,7 +3079,8 @@ func makeFundingScript(channel *channeldb.OpenChannel) ([]byte, error) {
3079
3079
// process once the funding transaction has been broadcast. The primary
3080
3080
// function of waitForFundingConfirmation is to wait for blockchain
3081
3081
// confirmation, and then to notify the other systems that must be notified
3082
- // when a channel has become active for lightning transactions.
3082
+ // when a channel has become active for lightning transactions. It also updates
3083
+ // the channel’s opening transaction block height in the database.
3083
3084
// The wait can be canceled by closing the cancelChan. In case of success,
3084
3085
// a *lnwire.ShortChannelID will be passed to confChan.
3085
3086
//
@@ -3123,55 +3124,120 @@ func (f *Manager) waitForFundingConfirmation(
3123
3124
log .Infof ("Waiting for funding tx (%v) to reach %v confirmations" ,
3124
3125
txid , numConfs )
3125
3126
3126
- var confDetails * chainntnfs.TxConfirmation
3127
- var ok bool
3127
+ // Monitor the confirmation event for the funding transaction. And
3128
+ // return when the funding tx has received the required number of
3129
+ // confirmations.
3130
+ for {
3131
+ select {
3132
+ case updDetails := <- confNtfn .Updates :
3133
+ log .Tracef ("funding tx %s received a confirmation, %d " +
3134
+ " number of confirmations still required" , txid ,
3135
+ updDetails .NumConfsLeft )
3136
+
3137
+ // Handle first confirmation of the funding transaction.
3138
+ if updDetails .NumConfsLeft == numConfs - 1 {
3139
+ log .Infof ("funding tx %s received first " +
3140
+ "confirmation" , txid )
3141
+
3142
+ err := completeChan .MarkConfirmationHeight (
3143
+ updDetails .BlockHeight ,
3144
+ )
3145
+ if err != nil {
3146
+ log .Errorf ("failed to mark " +
3147
+ "confirmation height: %v" , err )
3128
3148
3129
- // Wait until the specified number of confirmations has been reached,
3130
- // we get a cancel signal, or the wallet signals a shutdown.
3131
- select {
3132
- case confDetails , ok = <- confNtfn .Confirmed :
3133
- // fallthrough
3149
+ return
3150
+ }
3151
+ }
3134
3152
3135
- case <- cancelChan :
3136
- log . Warnf ( "canceled waiting for funding confirmation, " +
3137
- "stopping funding flow for ChannelPoint(%v)" ,
3138
- completeChan . FundingOutpoint )
3139
- return
3153
+ // If we haven't reached final confirmation, continue
3154
+ // waiting for confirmations.
3155
+ if updDetails . NumConfsLeft > 0 {
3156
+ continue
3157
+ }
3140
3158
3141
- case <- f .quit :
3142
- log .Warnf ("fundingManager shutting down, stopping funding " +
3143
- "flow for ChannelPoint(%v)" ,
3144
- completeChan .FundingOutpoint )
3145
- return
3146
- }
3159
+ log .Infof ("funding tx %s received all confirmations" ,
3160
+ txid )
3147
3161
3148
- if ! ok {
3149
- log .Warnf ("ChainNotifier shutting down, cannot complete " +
3150
- "funding flow for ChannelPoint(%v)" ,
3151
- completeChan .FundingOutpoint )
3152
- return
3153
- }
3162
+ // We've reached final confirmation. Wait for the
3163
+ // confirmation event to trigger and then send the
3164
+ // result to the confirmation channel.
3165
+ err := f .handleFinalConfirmation (
3166
+ confNtfn , confChan , completeChan ,
3167
+ )
3168
+ if err != nil {
3169
+ log .Errorf ("failed to handle final " +
3170
+ "confirmation: %v" , err )
3171
+ }
3154
3172
3155
- fundingPoint := completeChan .FundingOutpoint
3156
- log .Infof ("ChannelPoint(%v) is now active: ChannelID(%v)" ,
3157
- fundingPoint , lnwire .NewChanIDFromOutPoint (fundingPoint ))
3173
+ return
3174
+
3175
+ case <- confNtfn .NegativeConf :
3176
+ log .Warnf ("funding tx %s was reorged out; channel " +
3177
+ "point: %s" , txid , completeChan .FundingOutpoint )
3178
+
3179
+ // Reset the confirmation height to 0 because the
3180
+ // funding transaction was reorged out.
3181
+ err := completeChan .MarkConfirmationHeight (uint32 (0 ))
3182
+ if err != nil {
3183
+ log .Errorf ("failed to update state for " +
3184
+ "ChannelPoint(%v): %v" ,
3185
+ completeChan .FundingOutpoint , err )
3186
+
3187
+ return
3188
+ }
3189
+
3190
+ case <- cancelChan :
3191
+ log .Warnf ("canceled waiting for funding confirmation, " +
3192
+ "stopping funding flow for ChannelPoint(%v)" ,
3193
+ completeChan .FundingOutpoint )
3194
+
3195
+ return
3158
3196
3159
- // With the block height and the transaction index known, we can
3160
- // construct the compact chanID which is used on the network to unique
3161
- // identify channels.
3162
- shortChanID := lnwire. ShortChannelID {
3163
- BlockHeight : confDetails . BlockHeight ,
3164
- TxIndex : confDetails . TxIndex ,
3165
- TxPosition : uint16 ( fundingPoint . Index ),
3197
+ case <- f . quit :
3198
+ log . Warnf ( "fundingManager shutting down, stopping " +
3199
+ "funding flow for ChannelPoint(%v)" ,
3200
+ completeChan . FundingOutpoint )
3201
+
3202
+ return
3203
+ }
3166
3204
}
3205
+ }
3206
+
3207
+ // handleFinalConfirmation is a helper function that listens for the final
3208
+ // confirmation event and sends the final confirmation to the confirmation
3209
+ // channel which was provided by the caller.
3210
+ func (f * Manager ) handleFinalConfirmation (
3211
+ confNtfn * chainntnfs.ConfirmationEvent ,
3212
+ confChan chan <- * confirmedChannel ,
3213
+ completeChan * channeldb.OpenChannel ) error {
3167
3214
3168
3215
select {
3169
- case confChan <- & confirmedChannel {
3170
- shortChanID : shortChanID ,
3171
- fundingTx : confDetails .Tx ,
3172
- }:
3216
+ case confDetails , ok := <- confNtfn .Confirmed :
3217
+ if ! ok {
3218
+ return fmt .Errorf ("confirmation channel closed " +
3219
+ "unexpectedly" )
3220
+ }
3221
+
3222
+ // Send the result to the confirmation channel.
3223
+ shortChanID := lnwire.ShortChannelID {
3224
+ BlockHeight : confDetails .BlockHeight ,
3225
+ TxIndex : confDetails .TxIndex ,
3226
+ TxPosition : uint16 (completeChan .FundingOutpoint .Index ),
3227
+ }
3228
+
3229
+ if ! fn .SendOrQuit (confChan , & confirmedChannel {
3230
+ shortChanID : shortChanID ,
3231
+ fundingTx : confDetails .Tx ,
3232
+ }, f .quit ) {
3233
+
3234
+ return fmt .Errorf ("failed to send confirmation" )
3235
+ }
3236
+
3237
+ return nil
3238
+
3173
3239
case <- f .quit :
3174
- return
3240
+ return fmt . Errorf ( "funding manager shutting down" )
3175
3241
}
3176
3242
}
3177
3243
0 commit comments