cnct: do not depend on ChainIO in incoming contest resolver

New behaviour of the chain notifier to always send the current block
immediately after registration takes away the need to make a separate
GetBestBlock call on ChainIO.
This commit is contained in:
Joost Jager 2019-04-15 19:05:10 +02:00
parent d55a8b7b29
commit ec6a35d6e8
No known key found for this signature in database
GPG Key ID: A61B9D4C393C59C7

@ -50,18 +50,32 @@ func (h *htlcIncomingContestResolver) Resolve() (ContractResolver, error) {
return nil, nil return nil, nil
} }
// We'll first check if this HTLC has been timed out, if so, we can // Register for block epochs. After registration, the current height
// return now and mark ourselves as resolved. // will be sent on the channel immediately.
_, currentHeight, err := h.ChainIO.GetBestBlock() blockEpochs, err := h.Notifier.RegisterBlockEpochNtfn(nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer blockEpochs.Cancel()
// If we're past the point of expiry of the HTLC, then at this point the var currentHeight int32
// sender can sweep it, so we'll end our lifetime. Here we deliberately select {
// forego the chance that the sender doesn't sweep and we already have case newBlock, ok := <-blockEpochs.Epochs:
// or will learn the preimage. Otherwise the resolver could potentially if !ok {
// stay active indefinitely and the channel will never close properly. return nil, fmt.Errorf("quitting")
}
currentHeight = newBlock.Height
case <-h.Quit:
return nil, fmt.Errorf("resolver stopped")
}
// We'll first check if this HTLC has been timed out, if so, we can
// return now and mark ourselves as resolved. If we're past the point of
// expiry of the HTLC, then at this point the sender can sweep it, so
// we'll end our lifetime. Here we deliberately forego the chance that
// the sender doesn't sweep and we already have or will learn the
// preimage. Otherwise the resolver could potentially stay active
// indefinitely and the channel will never close properly.
if uint32(currentHeight) >= h.htlcExpiry { if uint32(currentHeight) >= h.htlcExpiry {
// TODO(roasbeef): should also somehow check if outgoing is // TODO(roasbeef): should also somehow check if outgoing is
// resolved or not // resolved or not
@ -117,14 +131,7 @@ func (h *htlcIncomingContestResolver) Resolve() (ContractResolver, error) {
// ensure the preimage can't be delivered between querying and // ensure the preimage can't be delivered between querying and
// registering for the preimage subscription. // registering for the preimage subscription.
preimageSubscription := h.PreimageDB.SubscribeUpdates() preimageSubscription := h.PreimageDB.SubscribeUpdates()
blockEpochs, err := h.Notifier.RegisterBlockEpochNtfn(nil) defer preimageSubscription.CancelSubscription()
if err != nil {
return nil, err
}
defer func() {
preimageSubscription.CancelSubscription()
blockEpochs.Cancel()
}()
// Create a buffered hodl chan to prevent deadlock. // Create a buffered hodl chan to prevent deadlock.
hodlChan := make(chan interface{}, 1) hodlChan := make(chan interface{}, 1)