multi: create channeldb/models package

Add a new subpackage to `lnd/channeldb` to hold some of the types that
are used in the package itself and in other packages that should not
depend on `channeldb`.
This commit is contained in:
positiveblue committed 2023-01-16 07:14:55 -08:00
1 parent c602ac07e7
commit 383cb40f8d
29 files changed
+400 -315

No files matched your search

+2 -1
View File
@@ -5,6 +5,7 @@ import (
"io"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/htlcswitch/hop"
"github.com/lightningnetwork/lnd/lnwire"
)
@@ -19,7 +20,7 @@ var EmptyCircuitKey CircuitKey
// HTLCs in a circuit. Circuits are identified primarily by the circuit key of
// the incoming HTLC. However, a circuit may also be referenced by its outgoing
// circuit key after the HTLC has been forwarded via the outgoing link.
type CircuitKey = channeldb.CircuitKey
type CircuitKey = models.CircuitKey
// PaymentCircuit is used by the switch as placeholder between when the
// switch makes a forwarding decision and the outgoing link determines the
+7 -7
View File
@@ -4,19 +4,19 @@ import (
"errors"
"fmt"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
)
// heldHtlcSet keeps track of outstanding intercepted forwards. It exposes
// several methods to manipulate the underlying map structure in a consistent
// way.
type heldHtlcSet struct {
set map[channeldb.CircuitKey]InterceptedForward
set map[models.CircuitKey]InterceptedForward
}
func newHeldHtlcSet() *heldHtlcSet {
return &heldHtlcSet{
set: make(map[channeldb.CircuitKey]InterceptedForward),
set: make(map[models.CircuitKey]InterceptedForward),
}
}
@@ -34,7 +34,7 @@ func (h *heldHtlcSet) popAll(cb func(InterceptedForward)) {
cb(fwd)
}
h.set = make(map[channeldb.CircuitKey]InterceptedForward)
h.set = make(map[models.CircuitKey]InterceptedForward)
}
// popAutoFails calls the callback for each forward that has an auto-fail height
@@ -52,7 +52,7 @@ func (h *heldHtlcSet) popAutoFails(height uint32, cb func(InterceptedForward)) {
}
// pop returns the specified forward and removes it from the set.
func (h *heldHtlcSet) pop(key channeldb.CircuitKey) (InterceptedForward, error) {
func (h *heldHtlcSet) pop(key models.CircuitKey) (InterceptedForward, error) {
intercepted, ok := h.set[key]
if !ok {
return nil, fmt.Errorf("fwd %v not found", key)
@@ -64,7 +64,7 @@ func (h *heldHtlcSet) pop(key channeldb.CircuitKey) (InterceptedForward, error)
}
// exists tests whether the specified forward is part of the set.
func (h *heldHtlcSet) exists(key channeldb.CircuitKey) bool {
func (h *heldHtlcSet) exists(key models.CircuitKey) bool {
_, ok := h.set[key]
return ok
@@ -72,7 +72,7 @@ func (h *heldHtlcSet) exists(key channeldb.CircuitKey) bool {
// push adds the specified forward to the set. An error is returned if the
// forward exists already.
func (h *heldHtlcSet) push(key channeldb.CircuitKey,
func (h *heldHtlcSet) push(key models.CircuitKey,
fwd InterceptedForward) error {
if fwd == nil {
+5 -5
View File
@@ -3,7 +3,7 @@ package htlcswitch
import (
"testing"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/stretchr/testify/require"
)
@@ -12,9 +12,9 @@ func TestHeldHtlcSetEmpty(t *testing.T) {
set := newHeldHtlcSet()
// Test operations on an empty set.
require.False(t, set.exists(channeldb.CircuitKey{}))
require.False(t, set.exists(models.CircuitKey{}))
_, err := set.pop(channeldb.CircuitKey{})
_, err := set.pop(models.CircuitKey{})
require.Error(t, err)
set.popAll(
@@ -27,7 +27,7 @@ func TestHeldHtlcSetEmpty(t *testing.T) {
func TestHeldHtlcSet(t *testing.T) {
set := newHeldHtlcSet()
key := channeldb.CircuitKey{
key := models.CircuitKey{
ChanID: lnwire.NewShortChanIDFromInt(1),
HtlcID: 2,
}
@@ -82,7 +82,7 @@ func TestHeldHtlcSet(t *testing.T) {
func TestHeldHtlcSetAutoFails(t *testing.T) {
set := newHeldHtlcSet()
key := channeldb.CircuitKey{
key := models.CircuitKey{
ChanID: lnwire.NewShortChanIDFromInt(1),
HtlcID: 2,
}
+5 -4
View File
@@ -7,6 +7,7 @@ import (
"time"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/htlcswitch/hop"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire"
@@ -108,10 +109,10 @@ func (h *HtlcNotifier) SubscribeHtlcEvents() (*subscribe.Client, error) {
// HtlcKey uniquely identifies the htlc.
type HtlcKey struct {
// IncomingCircuit is the channel an htlc id of the incoming htlc.
IncomingCircuit channeldb.CircuitKey
IncomingCircuit models.CircuitKey
// OutgoingCircuit is the channel and htlc id of the outgoing htlc.
OutgoingCircuit channeldb.CircuitKey
OutgoingCircuit models.CircuitKey
}
// String returns a string representation of a htlc key.
@@ -398,7 +399,7 @@ func (h *HtlcNotifier) NotifySettleEvent(key HtlcKey,
// htlc has been determined.
//
// Note this is part of the htlcNotifier interface.
func (h *HtlcNotifier) NotifyFinalHtlcEvent(key channeldb.CircuitKey,
func (h *HtlcNotifier) NotifyFinalHtlcEvent(key models.CircuitKey,
info channeldb.FinalHtlcInfo) {
event := &FinalHtlcEvent{
@@ -423,7 +424,7 @@ func (h *HtlcNotifier) NotifyFinalHtlcEvent(key channeldb.CircuitKey,
// originate at our node.
func newHtlcKey(pkt *htlcPacket) HtlcKey {
htlcKey := HtlcKey{
IncomingCircuit: channeldb.CircuitKey{
IncomingCircuit: models.CircuitKey{
ChanID: pkt.incomingChanID,
HtlcID: pkt.incomingHTLCID,
},
+3 -3
View File
@@ -7,7 +7,7 @@ import (
"github.com/go-errors/errors"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/htlcswitch/hop"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire"
@@ -110,7 +110,7 @@ const (
// FwdResolution defines the action to be taken on an intercepted packet.
type FwdResolution struct {
// Key is the incoming circuit key of the htlc.
Key channeldb.CircuitKey
Key models.CircuitKey
// Action is the action to take on the intercepted htlc.
Action FwdAction
@@ -592,7 +592,7 @@ type interceptedForward struct {
// Packet returns the intercepted htlc packet.
func (f *interceptedForward) Packet() InterceptedPacket {
return InterceptedPacket{
IncomingCircuit: channeldb.CircuitKey{
IncomingCircuit: models.CircuitKey{
ChanID: f.packet.incomingChanID,
HtlcID: f.packet.incomingHTLCID,
},
+4 -3
View File
@@ -3,6 +3,7 @@ package htlcswitch
import (
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/invoices"
"github.com/lightningnetwork/lnd/lnpeer"
"github.com/lightningnetwork/lnd/lntypes"
@@ -28,7 +29,7 @@ type InvoiceDatabase interface {
// for decoding purposes.
NotifyExitHopHtlc(payHash lntypes.Hash, paidAmount lnwire.MilliSatoshi,
expiry uint32, currentHeight int32,
circuitKey channeldb.CircuitKey, hodlChan chan<- interface{},
circuitKey models.CircuitKey, hodlChan chan<- interface{},
payload invoices.Payload) (invoices.HtlcResolution, error)
// CancelInvoice attempts to cancel the invoice corresponding to the
@@ -284,7 +285,7 @@ type ForwardInterceptor func(InterceptedPacket) error
type InterceptedPacket struct {
// IncomingCircuit contains the incoming channel and htlc id of the
// packet.
IncomingCircuit channeldb.CircuitKey
IncomingCircuit models.CircuitKey
// OutgoingChanID is the destination channel for this packet.
OutgoingChanID lnwire.ShortChannelID
@@ -373,6 +374,6 @@ type htlcNotifier interface {
// NotifyFinalHtlcEvent notifies the HtlcNotifier that the final outcome
// for an htlc has been determined.
NotifyFinalHtlcEvent(key channeldb.CircuitKey,
NotifyFinalHtlcEvent(key models.CircuitKey,
info channeldb.FinalHtlcInfo)
}
+7 -6
View File
@@ -17,6 +17,7 @@ import (
"github.com/go-errors/errors"
"github.com/lightningnetwork/lnd/build"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/contractcourt"
"github.com/lightningnetwork/lnd/htlcswitch/hodl"
"github.com/lightningnetwork/lnd/htlcswitch/hop"
@@ -395,7 +396,7 @@ type channelLink struct {
// hodlMap stores related htlc data for a circuit key. It allows
// resolving those htlcs when we receive a message on hodlQueue.
hodlMap map[channeldb.CircuitKey]hodlHtlc
hodlMap map[models.CircuitKey]hodlHtlc
// log is a link-specific logging instance.
log btclog.Logger
@@ -422,7 +423,7 @@ func NewChannelLink(cfg ChannelLinkConfig,
channel: channel,
shortChanID: channel.ShortChanID(),
shutdownRequest: make(chan *shutdownReq),
hodlMap: make(map[channeldb.CircuitKey]hodlHtlc),
hodlMap: make(map[models.CircuitKey]hodlHtlc),
hodlQueue: queue.NewConcurrentQueue(10),
log: build.NewPrefixLog(logPrefix, log),
quit: make(chan struct{}),
@@ -1962,7 +1963,7 @@ func (l *channelLink) handleUpstreamMsg(msg lnwire.Message) {
// locked in.
for id, settled := range finalHTLCs {
l.cfg.HtlcNotifier.NotifyFinalHtlcEvent(
channeldb.CircuitKey{
models.CircuitKey{
ChanID: l.shortChanID,
HtlcID: id,
},
@@ -3260,7 +3261,7 @@ func (l *channelLink) processExitHop(pd *lnwallet.PaymentDescriptor,
// receive back a resolution event.
invoiceHash := lntypes.Hash(pd.RHash)
circuitKey := channeldb.CircuitKey{
circuitKey := models.CircuitKey{
ChanID: l.ShortChanID(),
HtlcID: pd.HtlcIndex,
}
@@ -3324,7 +3325,7 @@ func (l *channelLink) settleHTLC(preimage lntypes.Preimage,
// Once we have successfully settled the htlc, notify a settle event.
l.cfg.HtlcNotifier.NotifySettleEvent(
HtlcKey{
IncomingCircuit: channeldb.CircuitKey{
IncomingCircuit: models.CircuitKey{
ChanID: l.ShortChanID(),
HtlcID: pd.HtlcIndex,
},
@@ -3394,7 +3395,7 @@ func (l *channelLink) sendHTLCError(pd *lnwallet.PaymentDescriptor,
l.cfg.HtlcNotifier.NotifyLinkFailEvent(
HtlcKey{
IncomingCircuit: channeldb.CircuitKey{
IncomingCircuit: models.CircuitKey{
ChanID: l.ShortChanID(),
HtlcID: pd.HtlcIndex,
},
+16 -9
View File
@@ -23,6 +23,7 @@ import (
sphinx "github.com/lightningnetwork/lightning-onion"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/clock"
"github.com/lightningnetwork/lnd/contractcourt"
"github.com/lightningnetwork/lnd/htlcswitch/hop"
@@ -845,7 +846,9 @@ func (f *mockChannelLink) CheckHtlcTransit(payHash [32]byte,
return f.checkHtlcTransitResult
}
func (f *mockChannelLink) Stats() (uint64, lnwire.MilliSatoshi, lnwire.MilliSatoshi) {
func (f *mockChannelLink) Stats() (
uint64, lnwire.MilliSatoshi, lnwire.MilliSatoshi) {
return 0, 0, 0
}
@@ -982,13 +985,15 @@ func (i *mockInvoiceRegistry) LookupInvoice(rHash lntypes.Hash) (
return i.registry.LookupInvoice(rHash)
}
func (i *mockInvoiceRegistry) SettleHodlInvoice(preimage lntypes.Preimage) error {
func (i *mockInvoiceRegistry) SettleHodlInvoice(
preimage lntypes.Preimage) error {
return i.registry.SettleHodlInvoice(preimage)
}
func (i *mockInvoiceRegistry) NotifyExitHopHtlc(rhash lntypes.Hash,
amt lnwire.MilliSatoshi, expiry uint32, currentHeight int32,
circuitKey channeldb.CircuitKey, hodlChan chan<- interface{},
circuitKey models.CircuitKey, hodlChan chan<- interface{},
payload invoices.Payload) (invoices.HtlcResolution, error) {
event, err := i.registry.NotifyExitHopHtlc(
@@ -1016,7 +1021,9 @@ func (i *mockInvoiceRegistry) AddInvoice(invoice channeldb.Invoice,
return err
}
func (i *mockInvoiceRegistry) HodlUnsubscribeAll(subscriber chan<- interface{}) {
func (i *mockInvoiceRegistry) HodlUnsubscribeAll(
subscriber chan<- interface{}) {
i.registry.HodlUnsubscribeAll(subscriber)
}
@@ -1099,22 +1106,22 @@ type mockHTLCNotifier struct {
}
func (h *mockHTLCNotifier) NotifyForwardingEvent(key HtlcKey, info HtlcInfo,
eventType HtlcEventType) { // nolint:whitespace
eventType HtlcEventType) { //nolint:whitespace
}
func (h *mockHTLCNotifier) NotifyLinkFailEvent(key HtlcKey, info HtlcInfo,
eventType HtlcEventType, linkErr *LinkError,
incoming bool) { // nolint:whitespace
incoming bool) { //nolint:whitespace
}
func (h *mockHTLCNotifier) NotifyForwardingFailEvent(key HtlcKey,
eventType HtlcEventType) { // nolint:whitespace
eventType HtlcEventType) { //nolint:whitespace
}
func (h *mockHTLCNotifier) NotifySettleEvent(key HtlcKey,
preimage lntypes.Preimage, eventType HtlcEventType) { // nolint:whitespace
preimage lntypes.Preimage, eventType HtlcEventType) { //nolint:whitespace,lll
}
func (h *mockHTLCNotifier) NotifyFinalHtlcEvent(key channeldb.CircuitKey,
func (h *mockHTLCNotifier) NotifyFinalHtlcEvent(key models.CircuitKey,
info channeldb.FinalHtlcInfo) { //nolint:whitespace
}
+2 -1
View File
@@ -15,6 +15,7 @@ import (
"github.com/davecgh/go-spew/spew"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/clock"
"github.com/lightningnetwork/lnd/contractcourt"
"github.com/lightningnetwork/lnd/htlcswitch/hop"
@@ -640,7 +641,7 @@ func (s *Switch) UpdateForwardingPolicies(
func (s *Switch) IsForwardedHTLC(chanID lnwire.ShortChannelID,
htlcIndex uint64) bool {
circuit := s.circuits.LookupOpenCircuit(channeldb.CircuitKey{
circuit := s.circuits.LookupOpenCircuit(models.CircuitKey{
ChanID: chanID,
HtlcID: htlcIndex,
})
+7 -6
View File
@@ -15,6 +15,7 @@ import (
"github.com/go-errors/errors"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/contractcourt"
"github.com/lightningnetwork/lnd/htlcswitch/hodl"
"github.com/lightningnetwork/lnd/htlcswitch/hop"
@@ -25,7 +26,7 @@ import (
"github.com/stretchr/testify/require"
)
var zeroCircuit = channeldb.CircuitKey{}
var zeroCircuit = models.CircuitKey{}
var emptyScid = lnwire.ShortChannelID{}
func genPreimage() ([32]byte, error) {
@@ -3585,7 +3586,7 @@ func getThreeHopEvents(channels *clusterChannels, htlcID uint64,
aliceKey := HtlcKey{
IncomingCircuit: zeroCircuit,
OutgoingCircuit: channeldb.CircuitKey{
OutgoingCircuit: models.CircuitKey{
ChanID: channels.aliceToBob.ShortChanID(),
HtlcID: htlcID,
},
@@ -3606,11 +3607,11 @@ func getThreeHopEvents(channels *clusterChannels, htlcID uint64,
}
bobKey := HtlcKey{
IncomingCircuit: channeldb.CircuitKey{
IncomingCircuit: models.CircuitKey{
ChanID: channels.bobToAlice.ShortChanID(),
HtlcID: htlcID,
},
OutgoingCircuit: channeldb.CircuitKey{
OutgoingCircuit: models.CircuitKey{
ChanID: channels.bobToCarol.ShortChanID(),
HtlcID: htlcID,
},
@@ -3691,7 +3692,7 @@ func getThreeHopEvents(channels *clusterChannels, htlcID uint64,
carolEvents := []interface{}{
&SettleEvent{
HtlcKey: HtlcKey{
IncomingCircuit: channeldb.CircuitKey{
IncomingCircuit: models.CircuitKey{
ChanID: channels.carolToBob.ShortChanID(),
HtlcID: htlcID,
},
@@ -3701,7 +3702,7 @@ func getThreeHopEvents(channels *clusterChannels, htlcID uint64,
HtlcEventType: HtlcEventTypeReceive,
Timestamp: ts,
}, &FinalHtlcEvent{
CircuitKey: channeldb.CircuitKey{
CircuitKey: models.CircuitKey{
ChanID: channels.carolToBob.ShortChanID(),
HtlcID: htlcID,
},