contractcourt: add onchain interception

This commit is contained in:
Joost Jager
2022-02-04 14:32:15 +01:00
parent c392e003aa
commit 721fb4ee88
11 changed files with 264 additions and 16 deletions

View File

@@ -3,8 +3,12 @@ package lnd
import (
"sync"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/contractcourt"
"github.com/lightningnetwork/lnd/htlcswitch"
"github.com/lightningnetwork/lnd/htlcswitch/hop"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire"
)
// preimageSubscriber reprints an active subscription to be notified once the
@@ -36,18 +40,27 @@ type preimageBeacon struct {
clientCounter uint64
subscribers map[uint64]*preimageSubscriber
interceptor func(htlcswitch.InterceptedForward) error
}
func newPreimageBeacon(wCache witnessCache) *preimageBeacon {
func newPreimageBeacon(wCache witnessCache,
interceptor func(htlcswitch.InterceptedForward) error) *preimageBeacon {
return &preimageBeacon{
wCache: wCache,
interceptor: interceptor,
subscribers: make(map[uint64]*preimageSubscriber),
}
}
// SubscribeUpdates returns a channel that will be sent upon *each* time a new
// preimage is discovered.
func (p *preimageBeacon) SubscribeUpdates() *contractcourt.WitnessSubscription {
func (p *preimageBeacon) SubscribeUpdates(
chanID lnwire.ShortChannelID, htlc *channeldb.HTLC,
payload *hop.Payload,
nextHopOnionBlob []byte) (*contractcourt.WitnessSubscription, error) {
p.Lock()
defer p.Unlock()
@@ -64,7 +77,7 @@ func (p *preimageBeacon) SubscribeUpdates() *contractcourt.WitnessSubscription {
srvrLog.Debugf("Creating new witness beacon subscriber, id=%v",
p.clientCounter)
return &contractcourt.WitnessSubscription{
sub := &contractcourt.WitnessSubscription{
WitnessUpdates: client.updateChan,
CancelSubscription: func() {
p.Lock()
@@ -75,6 +88,32 @@ func (p *preimageBeacon) SubscribeUpdates() *contractcourt.WitnessSubscription {
close(client.quit)
},
}
// Notify the htlc interceptor. There may be a client connected
// and willing to supply a preimage.
packet := &htlcswitch.InterceptedPacket{
Hash: htlc.RHash,
IncomingExpiry: htlc.RefundTimeout,
IncomingAmount: htlc.Amt,
IncomingCircuit: channeldb.CircuitKey{
ChanID: chanID,
HtlcID: htlc.HtlcIndex,
},
OutgoingChanID: payload.FwdInfo.NextHop,
OutgoingExpiry: payload.FwdInfo.OutgoingCTLV,
OutgoingAmount: payload.FwdInfo.AmountToForward,
CustomRecords: payload.CustomRecords(),
}
copy(packet.OnionBlob[:], nextHopOnionBlob)
fwd := newInterceptedForward(packet, p)
err := p.interceptor(fwd)
if err != nil {
return nil, err
}
return sub, nil
}
// LookupPreImage attempts to lookup a preimage in the global cache. True is