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 2022-11-18 03:15:22 -08:00
parent c602ac07e7
commit 383cb40f8d
No known key found for this signature in database
GPG Key ID: 4FFF2510928804DC
29 changed files with 400 additions and 315 deletions

View File

@ -17,6 +17,7 @@ import (
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcwallet/walletdb"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/htlcswitch/hop"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/keychain"
@ -177,11 +178,6 @@ var (
// tolerant.
ErrNoPendingCommit = fmt.Errorf("no pending commits found")
// ErrInvalidCircuitKeyLen signals that a circuit key could not be
// decoded because the byte slice is of an invalid length.
ErrInvalidCircuitKeyLen = fmt.Errorf(
"length of serialized circuit key must be 16 bytes")
// ErrNoCommitPoint is returned when no data loss commit point is found
// in the database.
ErrNoCommitPoint = fmt.Errorf("no commit point found")
@ -2156,74 +2152,6 @@ func deserializeLogUpdate(r io.Reader) (*LogUpdate, error) {
return l, nil
}
// CircuitKey is used by a channel to uniquely identify the HTLCs it receives
// from the switch, and is used to purge our in-memory state of HTLCs that have
// already been processed by a link. Two list of CircuitKeys are included in
// each CommitDiff to allow a link to determine which in-memory htlcs directed
// the opening and closing of circuits in the switch's circuit map.
type CircuitKey struct {
// ChanID is the short chanid indicating the HTLC's origin.
//
// NOTE: It is fine for this value to be blank, as this indicates a
// locally-sourced payment.
ChanID lnwire.ShortChannelID
// HtlcID is the unique htlc index predominately assigned by links,
// though can also be assigned by switch in the case of locally-sourced
// payments.
HtlcID uint64
}
// SetBytes deserializes the given bytes into this CircuitKey.
func (k *CircuitKey) SetBytes(bs []byte) error {
if len(bs) != 16 {
return ErrInvalidCircuitKeyLen
}
k.ChanID = lnwire.NewShortChanIDFromInt(
binary.BigEndian.Uint64(bs[:8]))
k.HtlcID = binary.BigEndian.Uint64(bs[8:])
return nil
}
// Bytes returns the serialized bytes for this circuit key.
func (k CircuitKey) Bytes() []byte {
bs := make([]byte, 16)
binary.BigEndian.PutUint64(bs[:8], k.ChanID.ToUint64())
binary.BigEndian.PutUint64(bs[8:], k.HtlcID)
return bs
}
// Encode writes a CircuitKey to the provided io.Writer.
func (k *CircuitKey) Encode(w io.Writer) error {
var scratch [16]byte
binary.BigEndian.PutUint64(scratch[:8], k.ChanID.ToUint64())
binary.BigEndian.PutUint64(scratch[8:], k.HtlcID)
_, err := w.Write(scratch[:])
return err
}
// Decode reads a CircuitKey from the provided io.Reader.
func (k *CircuitKey) Decode(r io.Reader) error {
var scratch [16]byte
if _, err := io.ReadFull(r, scratch[:]); err != nil {
return err
}
k.ChanID = lnwire.NewShortChanIDFromInt(
binary.BigEndian.Uint64(scratch[:8]))
k.HtlcID = binary.BigEndian.Uint64(scratch[8:])
return nil
}
// String returns a string representation of the CircuitKey.
func (k CircuitKey) String() string {
return fmt.Sprintf("(Chan ID=%s, HTLC ID=%d)", k.ChanID, k.HtlcID)
}
// CommitDiff represents the delta needed to apply the state transition between
// two subsequent commitment states. Given state N and state N+1, one is able
// to apply the set of messages contained within the CommitDiff to N to arrive
@ -2252,13 +2180,13 @@ type CommitDiff struct {
// Add packets included in this commitment txn. After a restart, this
// set of htlcs is acked from the link's incoming mailbox to ensure
// there isn't an attempt to re-add them to this commitment txn.
OpenedCircuitKeys []CircuitKey
OpenedCircuitKeys []models.CircuitKey
// ClosedCircuitKeys records the unique identifiers for any settle/fail
// packets that were resolved by this commitment txn. After a restart,
// this is used to ensure those circuits are removed from the circuit
// map, and the downstream packets in the link's mailbox are removed.
ClosedCircuitKeys []CircuitKey
ClosedCircuitKeys []models.CircuitKey
// AddAcks specifies the locations (commit height, pkg index) of any
// Adds that were failed/settled in this commit diff. This will ack
@ -2387,7 +2315,7 @@ func deserializeCommitDiff(r io.Reader) (*CommitDiff, error) {
return nil, err
}
d.OpenedCircuitKeys = make([]CircuitKey, numOpenRefs)
d.OpenedCircuitKeys = make([]models.CircuitKey, numOpenRefs)
for i := 0; i < int(numOpenRefs); i++ {
err := ReadElements(r,
&d.OpenedCircuitKeys[i].ChanID,
@ -2402,7 +2330,7 @@ func deserializeCommitDiff(r io.Reader) (*CommitDiff, error) {
return nil, err
}
d.ClosedCircuitKeys = make([]CircuitKey, numClosedRefs)
d.ClosedCircuitKeys = make([]models.CircuitKey, numClosedRefs)
for i := 0; i < int(numClosedRefs); i++ {
err := ReadElements(r,
&d.ClosedCircuitKeys[i].ChanID,

View File

@ -14,6 +14,7 @@ import (
"github.com/btcsuite/btcd/wire"
_ "github.com/btcsuite/btcwallet/walletdb/bdb"
"github.com/davecgh/go-spew/spew"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/clock"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/kvdb"
@ -716,8 +717,8 @@ func TestChannelStateTransition(t *testing.T) {
},
},
},
OpenedCircuitKeys: []CircuitKey{},
ClosedCircuitKeys: []CircuitKey{},
OpenedCircuitKeys: []models.CircuitKey{},
ClosedCircuitKeys: []models.CircuitKey{},
}
copy(commitDiff.LogUpdates[0].UpdateMsg.(*lnwire.UpdateAddHTLC).PaymentHash[:],
bytes.Repeat([]byte{1}, 32))

View File

@ -8,6 +8,7 @@ import (
"testing"
"time"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/clock"
"github.com/lightningnetwork/lnd/feature"
"github.com/lightningnetwork/lnd/lntypes"
@ -51,7 +52,7 @@ func randInvoice(value lnwire.MilliSatoshi) (*Invoice, error) {
Value: value,
Features: emptyFeatures,
},
Htlcs: map[CircuitKey]*InvoiceHTLC{},
Htlcs: map[models.CircuitKey]*InvoiceHTLC{},
AMPState: map[SetID]InvoiceStateAMP{},
}
i.Memo = []byte("memo")
@ -77,7 +78,7 @@ func settleTestInvoice(invoice *Invoice, settleIndex uint64) {
invoice.SettleDate = testNow
invoice.AmtPaid = invoice.Terms.Value
invoice.State = ContractSettled
invoice.Htlcs[CircuitKey{}] = &InvoiceHTLC{
invoice.Htlcs[models.CircuitKey{}] = &InvoiceHTLC{
Amt: invoice.Terms.Value,
AcceptTime: testNow,
ResolveTime: testNow,
@ -433,7 +434,7 @@ func TestInvoiceCancelSingleHtlc(t *testing.T) {
paymentHash := preimage.Hash()
testInvoice := &Invoice{
Htlcs: map[CircuitKey]*InvoiceHTLC{},
Htlcs: map[models.CircuitKey]*InvoiceHTLC{},
Terms: ContractTerm{
Value: lnwire.NewMSatFromSatoshis(10000),
Features: emptyFeatures,
@ -446,7 +447,10 @@ func TestInvoiceCancelSingleHtlc(t *testing.T) {
}
// Accept an htlc on this invoice.
key := CircuitKey{ChanID: lnwire.NewShortChanIDFromInt(1), HtlcID: 4}
key := models.CircuitKey{
ChanID: lnwire.NewShortChanIDFromInt(1),
HtlcID: 4,
}
htlc := HtlcAcceptDesc{
Amt: 500,
CustomRecords: make(record.CustomSet),
@ -456,7 +460,7 @@ func TestInvoiceCancelSingleHtlc(t *testing.T) {
invoice, err := db.UpdateInvoice(ref, nil,
func(invoice *Invoice) (*InvoiceUpdateDesc, error) {
return &InvoiceUpdateDesc{
AddHtlcs: map[CircuitKey]*HtlcAcceptDesc{
AddHtlcs: map[models.CircuitKey]*HtlcAcceptDesc{
key: &htlc,
},
}, nil
@ -473,7 +477,7 @@ func TestInvoiceCancelSingleHtlc(t *testing.T) {
invoice, err = db.UpdateInvoice(ref, nil,
func(invoice *Invoice) (*InvoiceUpdateDesc, error) {
return &InvoiceUpdateDesc{
CancelHtlcs: map[CircuitKey]struct{}{
CancelHtlcs: map[models.CircuitKey]struct{}{
key: {},
},
}, nil
@ -542,7 +546,7 @@ func TestInvoiceCancelSingleHtlcAMP(t *testing.T) {
_, err = db.UpdateInvoice(ref, (*SetID)(setID1),
func(invoice *Invoice) (*InvoiceUpdateDesc, error) {
return &InvoiceUpdateDesc{
CancelHtlcs: map[CircuitKey]struct{}{
CancelHtlcs: map[models.CircuitKey]struct{}{
{HtlcID: 0}: {},
},
SetID: (*SetID)(setID1),
@ -562,28 +566,29 @@ func TestInvoiceCancelSingleHtlcAMP(t *testing.T) {
invoice.State = ContractOpen
invoice.AmtPaid = 2 * amt
invoice.SettleDate = dbInvoice.SettleDate
invoice.Htlcs = map[CircuitKey]*InvoiceHTLC{
invoice.Htlcs = map[models.CircuitKey]*InvoiceHTLC{
{HtlcID: 0}: makeAMPInvoiceHTLC(amt, *setID1, payHash, &preimage),
{HtlcID: 1}: makeAMPInvoiceHTLC(amt, *setID2, payHash, &preimage),
{HtlcID: 2}: makeAMPInvoiceHTLC(amt, *setID2, payHash, &preimage),
}
invoice.AMPState[*setID1] = InvoiceStateAMP{
State: HtlcStateCanceled,
InvoiceKeys: map[CircuitKey]struct{}{
InvoiceKeys: map[models.CircuitKey]struct{}{
{HtlcID: 0}: {},
},
}
invoice.AMPState[*setID2] = InvoiceStateAMP{
State: HtlcStateAccepted,
AmtPaid: amt * 2,
InvoiceKeys: map[CircuitKey]struct{}{
InvoiceKeys: map[models.CircuitKey]struct{}{
{HtlcID: 1}: {},
{HtlcID: 2}: {},
},
}
invoice.Htlcs[CircuitKey{HtlcID: 0}].State = HtlcStateCanceled
invoice.Htlcs[CircuitKey{HtlcID: 0}].ResolveTime = time.Unix(1, 0)
htlc0 := models.CircuitKey{HtlcID: 0}
invoice.Htlcs[htlc0].State = HtlcStateCanceled
invoice.Htlcs[htlc0].ResolveTime = time.Unix(1, 0)
require.Equal(t, invoice, dbInvoice)
@ -592,7 +597,7 @@ func TestInvoiceCancelSingleHtlcAMP(t *testing.T) {
_, err = db.UpdateInvoice(ref, (*SetID)(setID2),
func(invoice *Invoice) (*InvoiceUpdateDesc, error) {
return &InvoiceUpdateDesc{
CancelHtlcs: map[CircuitKey]struct{}{
CancelHtlcs: map[models.CircuitKey]struct{}{
{HtlcID: 1}: {},
},
SetID: (*SetID)(setID2),
@ -604,8 +609,9 @@ func TestInvoiceCancelSingleHtlcAMP(t *testing.T) {
require.Nil(t, err)
dbInvoice = &freshInvoice
invoice.Htlcs[CircuitKey{HtlcID: 1}].State = HtlcStateCanceled
invoice.Htlcs[CircuitKey{HtlcID: 1}].ResolveTime = time.Unix(1, 0)
htlc1 := models.CircuitKey{HtlcID: 1}
invoice.Htlcs[htlc1].State = HtlcStateCanceled
invoice.Htlcs[htlc1].ResolveTime = time.Unix(1, 0)
invoice.AmtPaid = amt
ampState := invoice.AMPState[*setID2]
@ -620,7 +626,7 @@ func TestInvoiceCancelSingleHtlcAMP(t *testing.T) {
_, err = db.UpdateInvoice(ref, (*SetID)(setID2),
func(invoice *Invoice) (*InvoiceUpdateDesc, error) {
return &InvoiceUpdateDesc{
CancelHtlcs: map[CircuitKey]struct{}{
CancelHtlcs: map[models.CircuitKey]struct{}{
{HtlcID: 2}: {},
},
SetID: (*SetID)(setID2),
@ -636,8 +642,9 @@ func TestInvoiceCancelSingleHtlcAMP(t *testing.T) {
ampState.AmtPaid = 0
invoice.AMPState[*setID2] = ampState
invoice.Htlcs[CircuitKey{HtlcID: 2}].State = HtlcStateCanceled
invoice.Htlcs[CircuitKey{HtlcID: 2}].ResolveTime = time.Unix(1, 0)
htlc2 := models.CircuitKey{HtlcID: 2}
invoice.Htlcs[htlc2].State = HtlcStateCanceled
invoice.Htlcs[htlc2].ResolveTime = time.Unix(1, 0)
invoice.AmtPaid = 0
require.Equal(t, invoice, dbInvoice)
@ -865,7 +872,7 @@ func TestSettleIndexAmpPayments(t *testing.T) {
require.Equal(t, 1, len(invoiceFiltered.Htlcs))
// The set ID for the HTLC should match the queried set ID.
key := CircuitKey{HtlcID: uint64(i + 1)}
key := models.CircuitKey{HtlcID: uint64(i + 1)}
htlc := invoiceFiltered.Htlcs[key]
require.Equal(t, *setID, htlc.AMP.Record.SetID())
@ -878,21 +885,21 @@ func TestSettleIndexAmpPayments(t *testing.T) {
_, err = db.UpdateInvoice(
ref, (*SetID)(setID1),
getUpdateInvoiceAMPSettle(
setID1, preimage, CircuitKey{HtlcID: 1},
setID1, preimage, models.CircuitKey{HtlcID: 1},
),
)
require.Nil(t, err)
_, err = db.UpdateInvoice(
ref, (*SetID)(setID2),
getUpdateInvoiceAMPSettle(
setID2, preimage, CircuitKey{HtlcID: 2},
setID2, preimage, models.CircuitKey{HtlcID: 2},
),
)
require.Nil(t, err)
_, err = db.UpdateInvoice(
ref, (*SetID)(setID3),
getUpdateInvoiceAMPSettle(
setID3, preimage, CircuitKey{HtlcID: 3},
setID3, preimage, models.CircuitKey{HtlcID: 3},
),
)
require.Nil(t, err)
@ -931,7 +938,7 @@ func TestSettleIndexAmpPayments(t *testing.T) {
require.Equal(t, subInvoiceState.State, HtlcStateSettled)
require.Equal(t, int(subInvoiceState.SettleIndex), i+1)
invoiceKey := CircuitKey{HtlcID: uint64(i + 1)}
invoiceKey := models.CircuitKey{HtlcID: uint64(i + 1)}
_, keyFound := subInvoiceState.InvoiceKeys[invoiceKey]
require.True(t, keyFound)
}
@ -1043,7 +1050,7 @@ func TestDuplicateSettleInvoice(t *testing.T) {
invoice.State = ContractSettled
invoice.AmtPaid = amt
invoice.SettleDate = dbInvoice.SettleDate
invoice.Htlcs = map[CircuitKey]*InvoiceHTLC{
invoice.Htlcs = map[models.CircuitKey]*InvoiceHTLC{
{}: {
Amt: amt,
AcceptTime: time.Unix(1, 0),
@ -1484,7 +1491,7 @@ func getUpdateInvoice(amt lnwire.MilliSatoshi) InvoiceUpdateCallback {
Preimage: invoice.Terms.PaymentPreimage,
NewState: ContractSettled,
},
AddHtlcs: map[CircuitKey]*HtlcAcceptDesc{
AddHtlcs: map[models.CircuitKey]*HtlcAcceptDesc{
{}: {
Amt: amt,
CustomRecords: noRecords,
@ -1508,7 +1515,7 @@ func TestCustomRecords(t *testing.T) {
paymentHash := preimage.Hash()
testInvoice := &Invoice{
Htlcs: map[CircuitKey]*InvoiceHTLC{},
Htlcs: map[models.CircuitKey]*InvoiceHTLC{},
Terms: ContractTerm{
Value: lnwire.NewMSatFromSatoshis(10000),
Features: emptyFeatures,
@ -1521,7 +1528,10 @@ func TestCustomRecords(t *testing.T) {
}
// Accept an htlc with custom records on this invoice.
key := CircuitKey{ChanID: lnwire.NewShortChanIDFromInt(1), HtlcID: 4}
key := models.CircuitKey{
ChanID: lnwire.NewShortChanIDFromInt(1),
HtlcID: 4,
}
records := record.CustomSet{
100000: []byte{},
@ -1531,14 +1541,14 @@ func TestCustomRecords(t *testing.T) {
ref := InvoiceRefByHash(paymentHash)
_, err = db.UpdateInvoice(ref, nil,
func(invoice *Invoice) (*InvoiceUpdateDesc, error) {
return &InvoiceUpdateDesc{
AddHtlcs: map[CircuitKey]*HtlcAcceptDesc{
key: {
Amt: 500,
CustomRecords: records,
},
htlcs := map[models.CircuitKey]*HtlcAcceptDesc{
key: {
Amt: 500,
CustomRecords: records,
},
}, nil
}
return &InvoiceUpdateDesc{AddHtlcs: htlcs}, nil
},
)
require.NoError(t, err, "unable to add invoice htlc")
@ -1585,7 +1595,10 @@ func testInvoiceHtlcAMPFields(t *testing.T, isAMP bool) {
require.Nil(t, err)
// Accept an htlc with custom records on this invoice.
key := CircuitKey{ChanID: lnwire.NewShortChanIDFromInt(1), HtlcID: 4}
key := models.CircuitKey{
ChanID: lnwire.NewShortChanIDFromInt(1),
HtlcID: 4,
}
records := make(map[uint64][]byte)
var ampData *InvoiceHtlcAMPData
@ -1603,15 +1616,14 @@ func testInvoiceHtlcAMPFields(t *testing.T, isAMP bool) {
ref := InvoiceRefByHash(payHash)
_, err = db.UpdateInvoice(ref, nil,
func(invoice *Invoice) (*InvoiceUpdateDesc, error) {
return &InvoiceUpdateDesc{
AddHtlcs: map[CircuitKey]*HtlcAcceptDesc{
key: {
Amt: 500,
AMP: ampData,
CustomRecords: records,
},
htlcs := map[models.CircuitKey]*HtlcAcceptDesc{
key: {
Amt: 500,
AMP: ampData,
CustomRecords: records,
},
}, nil
}
return &InvoiceUpdateDesc{AddHtlcs: htlcs}, nil
},
)
require.Nil(t, err)
@ -1667,7 +1679,7 @@ func TestInvoiceRef(t *testing.T) {
// sets.
func TestHTLCSet(t *testing.T) {
inv := &Invoice{
Htlcs: make(map[CircuitKey]*InvoiceHTLC),
Htlcs: make(map[models.CircuitKey]*InvoiceHTLC),
}
// Construct two distinct set id's, in this test we'll also track the
@ -1677,9 +1689,9 @@ func TestHTLCSet(t *testing.T) {
// Create the expected htlc sets for each group, these will be updated
// as the invoice is modified.
expSetNil := make(map[CircuitKey]*InvoiceHTLC)
expSet1 := make(map[CircuitKey]*InvoiceHTLC)
expSet2 := make(map[CircuitKey]*InvoiceHTLC)
expSetNil := make(map[models.CircuitKey]*InvoiceHTLC)
expSet1 := make(map[models.CircuitKey]*InvoiceHTLC)
expSet2 := make(map[models.CircuitKey]*InvoiceHTLC)
checkHTLCSets := func() {
require.Equal(t, expSetNil, inv.HTLCSet(nil, HtlcStateAccepted))
@ -1722,7 +1734,7 @@ func TestHTLCSet(t *testing.T) {
}
// Add the HTLC to the invoice's set of HTLCs.
key := CircuitKey{HtlcID: uint64(i)}
key := models.CircuitKey{HtlcID: uint64(i)}
htlc := &InvoiceHTLC{
AMP: ampData,
State: h.state,
@ -1757,7 +1769,7 @@ func TestAddInvoiceWithHTLCs(t *testing.T) {
testInvoice, err := randInvoice(1000)
require.Nil(t, err)
key := CircuitKey{HtlcID: 1}
key := models.CircuitKey{HtlcID: 1}
testInvoice.Htlcs[key] = &InvoiceHTLC{}
payHash := testInvoice.Terms.PaymentPreimage.Hash()
@ -1802,14 +1814,14 @@ func TestSetIDIndex(t *testing.T) {
invoice.State = ContractOpen
invoice.AmtPaid = amt
invoice.SettleDate = dbInvoice.SettleDate
invoice.Htlcs = map[CircuitKey]*InvoiceHTLC{
invoice.Htlcs = map[models.CircuitKey]*InvoiceHTLC{
{HtlcID: 0}: makeAMPInvoiceHTLC(amt, *setID, payHash, &preimage),
}
invoice.AMPState = map[SetID]InvoiceStateAMP{}
invoice.AMPState[*setID] = InvoiceStateAMP{
State: HtlcStateAccepted,
AmtPaid: amt,
InvoiceKeys: map[CircuitKey]struct{}{
InvoiceKeys: map[models.CircuitKey]struct{}{
{HtlcID: 0}: {},
},
}
@ -1860,7 +1872,7 @@ func TestSetIDIndex(t *testing.T) {
invoice.State = ContractOpen
invoice.AmtPaid += 2 * amt
invoice.SettleDate = dbInvoice.SettleDate
invoice.Htlcs = map[CircuitKey]*InvoiceHTLC{
invoice.Htlcs = map[models.CircuitKey]*InvoiceHTLC{
{HtlcID: 0}: makeAMPInvoiceHTLC(amt, *setID, payHash, &preimage),
{HtlcID: 1}: makeAMPInvoiceHTLC(amt, *setID2, payHash, nil),
{HtlcID: 2}: makeAMPInvoiceHTLC(amt, *setID2, payHash, nil),
@ -1868,14 +1880,14 @@ func TestSetIDIndex(t *testing.T) {
invoice.AMPState[*setID] = InvoiceStateAMP{
State: HtlcStateAccepted,
AmtPaid: amt,
InvoiceKeys: map[CircuitKey]struct{}{
InvoiceKeys: map[models.CircuitKey]struct{}{
{HtlcID: 0}: {},
},
}
invoice.AMPState[*setID2] = InvoiceStateAMP{
State: HtlcStateAccepted,
AmtPaid: amt * 2,
InvoiceKeys: map[CircuitKey]struct{}{
InvoiceKeys: map[models.CircuitKey]struct{}{
{HtlcID: 1}: {},
{HtlcID: 2}: {},
},
@ -1901,7 +1913,10 @@ func TestSetIDIndex(t *testing.T) {
// zero setID so it isn't used for anything internally.
_, err = db.UpdateInvoice(
ref, nil,
getUpdateInvoiceAMPSettle(&[32]byte{}, [32]byte{}, CircuitKey{HtlcID: 99}),
getUpdateInvoiceAMPSettle(
&[32]byte{}, [32]byte{},
models.CircuitKey{HtlcID: 99},
),
)
require.Equal(t, ErrEmptyHTLCSet, err)
@ -1910,7 +1925,9 @@ func TestSetIDIndex(t *testing.T) {
// invoice to be settled multiple times.
_, err = db.UpdateInvoice(
ref, (*SetID)(setID),
getUpdateInvoiceAMPSettle(setID, preimage, CircuitKey{HtlcID: 0}),
getUpdateInvoiceAMPSettle(
setID, preimage, models.CircuitKey{HtlcID: 0},
),
)
require.Nil(t, err)
@ -1931,8 +1948,9 @@ func TestSetIDIndex(t *testing.T) {
invoice.AMPState[*setID] = ampState
invoice.Htlcs[CircuitKey{HtlcID: 0}].State = HtlcStateSettled
invoice.Htlcs[CircuitKey{HtlcID: 0}].ResolveTime = time.Unix(1, 0)
htlc0 := models.CircuitKey{HtlcID: 0}
invoice.Htlcs[htlc0].State = HtlcStateSettled
invoice.Htlcs[htlc0].ResolveTime = time.Unix(1, 0)
require.Equal(t, invoice, dbInvoice)
@ -1940,7 +1958,9 @@ func TestSetIDIndex(t *testing.T) {
// error, as it's already been settled.
_, err = db.UpdateInvoice(
ref, (*SetID)(setID),
getUpdateInvoiceAMPSettle(setID, preimage, CircuitKey{HtlcID: 0}),
getUpdateInvoiceAMPSettle(
setID, preimage, models.CircuitKey{HtlcID: 0},
),
)
require.Equal(t, ErrEmptyHTLCSet, err)
@ -1951,7 +1971,8 @@ func TestSetIDIndex(t *testing.T) {
_, err = db.UpdateInvoice(
ref, (*SetID)(setID2),
getUpdateInvoiceAMPSettle(
setID2, preimage, CircuitKey{HtlcID: 1}, CircuitKey{HtlcID: 2},
setID2, preimage, models.CircuitKey{HtlcID: 1},
models.CircuitKey{HtlcID: 2},
),
)
require.Nil(t, err)
@ -1968,13 +1989,15 @@ func TestSetIDIndex(t *testing.T) {
invoice.AMPState[*setID2] = ampState
invoice.Htlcs[CircuitKey{HtlcID: 1}].State = HtlcStateSettled
invoice.Htlcs[CircuitKey{HtlcID: 1}].ResolveTime = time.Unix(1, 0)
invoice.Htlcs[CircuitKey{HtlcID: 1}].AMP.Preimage = &preimage
htlc1 := models.CircuitKey{HtlcID: 1}
htlc2 := models.CircuitKey{HtlcID: 2}
invoice.Htlcs[htlc1].State = HtlcStateSettled
invoice.Htlcs[htlc1].ResolveTime = time.Unix(1, 0)
invoice.Htlcs[htlc1].AMP.Preimage = &preimage
invoice.Htlcs[CircuitKey{HtlcID: 2}].State = HtlcStateSettled
invoice.Htlcs[CircuitKey{HtlcID: 2}].ResolveTime = time.Unix(1, 0)
invoice.Htlcs[CircuitKey{HtlcID: 2}].AMP.Preimage = &preimage
invoice.Htlcs[htlc2].State = HtlcStateSettled
invoice.Htlcs[htlc2].ResolveTime = time.Unix(1, 0)
invoice.Htlcs[htlc2].AMP.Preimage = &preimage
require.Equal(t, invoice, dbInvoice)
@ -2033,7 +2056,7 @@ func updateAcceptAMPHtlc(id uint64, amt lnwire.MilliSatoshi,
}
update := &InvoiceUpdateDesc{
State: state,
AddHtlcs: map[CircuitKey]*HtlcAcceptDesc{
AddHtlcs: map[models.CircuitKey]*HtlcAcceptDesc{
{HtlcID: id}: {
Amt: amt,
CustomRecords: noRecords,
@ -2046,15 +2069,15 @@ func updateAcceptAMPHtlc(id uint64, amt lnwire.MilliSatoshi,
}
}
func getUpdateInvoiceAMPSettle(setID *[32]byte,
preimage [32]byte, circuitKeys ...CircuitKey) InvoiceUpdateCallback {
func getUpdateInvoiceAMPSettle(setID *[32]byte, preimage [32]byte,
circuitKeys ...models.CircuitKey) InvoiceUpdateCallback {
return func(invoice *Invoice) (*InvoiceUpdateDesc, error) {
if invoice.State == ContractSettled {
return nil, ErrInvoiceAlreadySettled
}
preImageSet := make(map[CircuitKey]lntypes.Preimage)
preImageSet := make(map[models.CircuitKey]lntypes.Preimage)
for _, key := range circuitKeys {
preImageSet[key] = preimage
}
@ -2164,7 +2187,7 @@ func testUpdateHTLCPreimages(t *testing.T, test updateHTLCPreimageTestCase) {
)
require.Nil(t, err)
htlcPreimages := make(map[CircuitKey]lntypes.Preimage)
htlcPreimages := make(map[models.CircuitKey]lntypes.Preimage)
for key := range dbInvoice.Htlcs {
// Set the either the same preimage used to accept above, or a
// blank preimage depending on the test case.
@ -2980,13 +3003,13 @@ func TestEncodeDecodeAmpInvoiceState(t *testing.T) {
setID2 := [32]byte{2}
setID3 := [32]byte{3}
circuitKey1 := CircuitKey{
circuitKey1 := models.CircuitKey{
ChanID: lnwire.NewShortChanIDFromInt(1), HtlcID: 1,
}
circuitKey2 := CircuitKey{
circuitKey2 := models.CircuitKey{
ChanID: lnwire.NewShortChanIDFromInt(2), HtlcID: 2,
}
circuitKey3 := CircuitKey{
circuitKey3 := models.CircuitKey{
ChanID: lnwire.NewShortChanIDFromInt(2), HtlcID: 3,
}
@ -2997,7 +3020,7 @@ func TestEncodeDecodeAmpInvoiceState(t *testing.T) {
State: HtlcStateSettled,
SettleDate: testNow,
SettleIndex: 1,
InvoiceKeys: map[CircuitKey]struct{}{
InvoiceKeys: map[models.CircuitKey]struct{}{
circuitKey1: {},
circuitKey2: {},
},
@ -3007,7 +3030,7 @@ func TestEncodeDecodeAmpInvoiceState(t *testing.T) {
State: HtlcStateCanceled,
SettleDate: testNow,
SettleIndex: 2,
InvoiceKeys: map[CircuitKey]struct{}{
InvoiceKeys: map[models.CircuitKey]struct{}{
circuitKey1: {},
},
AmtPaid: 6,
@ -3016,7 +3039,7 @@ func TestEncodeDecodeAmpInvoiceState(t *testing.T) {
State: HtlcStateAccepted,
SettleDate: testNow,
SettleIndex: 3,
InvoiceKeys: map[CircuitKey]struct{}{
InvoiceKeys: map[models.CircuitKey]struct{}{
circuitKey1: {},
circuitKey2: {},
circuitKey3: {},

View File

@ -9,6 +9,7 @@ import (
"strings"
"time"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/feature"
"github.com/lightningnetwork/lnd/htlcswitch/hop"
"github.com/lightningnetwork/lnd/kvdb"
@ -485,7 +486,7 @@ type InvoiceStateAMP struct {
// InvoiceKeys is the set of circuit keys that can be used to locate
// the invoices for a given set ID.
InvoiceKeys map[CircuitKey]struct{}
InvoiceKeys map[models.CircuitKey]struct{}
// AmtPaid is the total amount that was paid in the AMP sub-invoice.
// Fetching the full HTLC/invoice state allows one to extract the
@ -499,7 +500,7 @@ func (i *InvoiceStateAMP) copy() (InvoiceStateAMP, error) {
result := *i
// Make a copy of the InvoiceKeys map.
result.InvoiceKeys = make(map[CircuitKey]struct{})
result.InvoiceKeys = make(map[models.CircuitKey]struct{})
for k := range i.InvoiceKeys {
result.InvoiceKeys[k] = struct{}{}
}
@ -608,7 +609,7 @@ type Invoice struct {
// Htlcs records all htlcs that paid to this invoice. Some of these
// htlcs may have been marked as canceled.
Htlcs map[CircuitKey]*InvoiceHTLC
Htlcs map[models.CircuitKey]*InvoiceHTLC
// AMPState describes the state of any related sub-invoices AMP to this
// greater invoice. A sub-invoice is defined by a set of HTLCs with the
@ -627,8 +628,10 @@ type Invoice struct {
// case of legacy or MPP, and no HTLCs in the case of AMP. Otherwise, the
// returned set will be filtered by the populated setID which is used to
// retrieve AMP HTLC sets.
func (i *Invoice) HTLCSet(setID *[32]byte, state HtlcState) map[CircuitKey]*InvoiceHTLC {
htlcSet := make(map[CircuitKey]*InvoiceHTLC)
func (i *Invoice) HTLCSet(setID *[32]byte,
state HtlcState) map[models.CircuitKey]*InvoiceHTLC {
htlcSet := make(map[models.CircuitKey]*InvoiceHTLC)
for key, htlc := range i.Htlcs {
// Only add HTLCs that are in the requested HtlcState.
if htlc.State != state {
@ -649,9 +652,9 @@ func (i *Invoice) HTLCSet(setID *[32]byte, state HtlcState) map[CircuitKey]*Invo
// are in the target state. Passing a nil setID will return no invoices, since
// all MPP HTLCs are part of the same HTLC set.
func (i *Invoice) HTLCSetCompliment(setID *[32]byte,
state HtlcState) map[CircuitKey]*InvoiceHTLC {
state HtlcState) map[models.CircuitKey]*InvoiceHTLC {
htlcSet := make(map[CircuitKey]*InvoiceHTLC)
htlcSet := make(map[models.CircuitKey]*InvoiceHTLC)
for key, htlc := range i.Htlcs {
// Only add HTLCs that are in the requested HtlcState.
if htlc.State != state {
@ -845,11 +848,11 @@ type InvoiceUpdateDesc struct {
State *InvoiceStateUpdateDesc
// CancelHtlcs describes the htlcs that need to be canceled.
CancelHtlcs map[CircuitKey]struct{}
CancelHtlcs map[models.CircuitKey]struct{}
// AddHtlcs describes the newly accepted htlcs that need to be added to
// the invoice.
AddHtlcs map[CircuitKey]*HtlcAcceptDesc
AddHtlcs map[models.CircuitKey]*HtlcAcceptDesc
// SetID is an optional set ID for AMP invoices that allows operations
// to be more efficient by ensuring we don't need to read out the
@ -868,7 +871,7 @@ type InvoiceStateUpdateDesc struct {
// HTLCPreimages set the HTLC-level preimages stored for AMP HTLCs.
// These are only learned when settling the invoice as a whole. Must be
// set when settling an invoice with non-nil SetID.
HTLCPreimages map[CircuitKey]lntypes.Preimage
HTLCPreimages map[models.CircuitKey]lntypes.Preimage
// SetID identifies a specific set of HTLCs destined for the same
// invoice as part of a larger AMP payment. This value will be nil for
@ -1729,7 +1732,9 @@ func serializeInvoice(w io.Writer, i *Invoice) error {
// serializeHtlcs serializes a map containing circuit keys and invoice htlcs to
// a writer.
func serializeHtlcs(w io.Writer, htlcs map[CircuitKey]*InvoiceHTLC) error {
func serializeHtlcs(w io.Writer,
htlcs map[models.CircuitKey]*InvoiceHTLC) error {
for key, htlc := range htlcs {
// Encode the htlc in a tlv stream.
chanID := key.ChanID.ToUint64()
@ -1831,10 +1836,10 @@ func getNanoTime(ns uint64) time.Time {
// fetchFilteredAmpInvoices retrieves only a select set of AMP invoices
// identified by the setID value.
func fetchFilteredAmpInvoices(invoiceBucket kvdb.RBucket,
invoiceNum []byte, setIDs ...*SetID) (map[CircuitKey]*InvoiceHTLC, error) {
func fetchFilteredAmpInvoices(invoiceBucket kvdb.RBucket, invoiceNum []byte,
setIDs ...*SetID) (map[models.CircuitKey]*InvoiceHTLC, error) {
htlcs := make(map[CircuitKey]*InvoiceHTLC)
htlcs := make(map[models.CircuitKey]*InvoiceHTLC)
for _, setID := range setIDs {
invoiceSetIDKey := makeInvoiceSetIDKey(invoiceNum, setID[:])
@ -1899,8 +1904,8 @@ func forEachAMPInvoice(invoiceBucket kvdb.RBucket, invoiceNum []byte,
// AMP bucket to find all the individual HTLCs (by setID) associated with a
// given invoice. If a list of set IDs are specified, then only HTLCs
// associated with that setID will be retrieved.
func fetchAmpSubInvoices(invoiceBucket kvdb.RBucket,
invoiceNum []byte, setIDs ...*SetID) (map[CircuitKey]*InvoiceHTLC, error) {
func fetchAmpSubInvoices(invoiceBucket kvdb.RBucket, invoiceNum []byte,
setIDs ...*SetID) (map[models.CircuitKey]*InvoiceHTLC, error) {
// If a set of setIDs was specified, then we can skip the cursor and
// just read out exactly what we need.
@ -1912,20 +1917,21 @@ func fetchAmpSubInvoices(invoiceBucket kvdb.RBucket,
// Otherwise, iterate over all the htlc sets that are prefixed beside
// this invoice in the main invoice bucket.
htlcs := make(map[CircuitKey]*InvoiceHTLC)
err := forEachAMPInvoice(invoiceBucket, invoiceNum, func(key, htlcSet []byte) error {
htlcSetReader := bytes.NewReader(htlcSet)
htlcsBySetID, err := deserializeHtlcs(htlcSetReader)
if err != nil {
return err
}
htlcs := make(map[models.CircuitKey]*InvoiceHTLC)
err := forEachAMPInvoice(invoiceBucket, invoiceNum,
func(key, htlcSet []byte) error {
htlcSetReader := bytes.NewReader(htlcSet)
htlcsBySetID, err := deserializeHtlcs(htlcSetReader)
if err != nil {
return err
}
for key, htlc := range htlcsBySetID {
htlcs[key] = htlc
}
for key, htlc := range htlcsBySetID {
htlcs[key] = htlc
}
return nil
})
return nil
})
if err != nil {
return nil, err
}
@ -2130,7 +2136,7 @@ func deserializeInvoice(r io.Reader) (Invoice, error) {
}
func encodeCircuitKeys(w io.Writer, val interface{}, buf *[8]byte) error {
if v, ok := val.(*map[CircuitKey]struct{}); ok {
if v, ok := val.(*map[models.CircuitKey]struct{}); ok {
// We encode the set of circuit keys as a varint length prefix.
// followed by a series of fixed sized uint8 integers.
numKeys := uint64(len(*v))
@ -2156,8 +2162,10 @@ func encodeCircuitKeys(w io.Writer, val interface{}, buf *[8]byte) error {
return tlv.NewTypeForEncodingErr(val, "*map[CircuitKey]struct{}")
}
func decodeCircuitKeys(r io.Reader, val interface{}, buf *[8]byte, l uint64) error {
if v, ok := val.(*map[CircuitKey]struct{}); ok {
func decodeCircuitKeys(r io.Reader, val interface{}, buf *[8]byte,
l uint64) error {
if v, ok := val.(*map[models.CircuitKey]struct{}); ok {
// First, we'll read out the varint that encodes the number of
// circuit keys encoded.
numKeys, err := tlv.ReadVarInt(r, buf)
@ -2169,7 +2177,7 @@ func decodeCircuitKeys(r io.Reader, val interface{}, buf *[8]byte, l uint64) err
// one until we're done.
for i := uint64(0); i < numKeys; i++ {
var (
key CircuitKey
key models.CircuitKey
scid uint64
)
@ -2309,8 +2317,10 @@ func ampStateDecoder(r io.Reader, val interface{}, buf *[8]byte, l uint64) error
htlcState uint8
settleIndex uint64
settleDateBytes []byte
invoiceKeys = make(map[CircuitKey]struct{})
amtPaid uint64
invoiceKeys = make(
map[models.CircuitKey]struct{},
)
amtPaid uint64
)
tlvStream, err := tlv.NewStream(
tlv.MakePrimitiveRecord(
@ -2367,8 +2377,8 @@ func ampStateDecoder(r io.Reader, val interface{}, buf *[8]byte, l uint64) error
// deserializeHtlcs reads a list of invoice htlcs from a reader and returns it
// as a map.
func deserializeHtlcs(r io.Reader) (map[CircuitKey]*InvoiceHTLC, error) {
htlcs := make(map[CircuitKey]*InvoiceHTLC)
func deserializeHtlcs(r io.Reader) (map[models.CircuitKey]*InvoiceHTLC, error) {
htlcs := make(map[models.CircuitKey]*InvoiceHTLC)
for {
// Read the length of the tlv stream for this htlc.
@ -2388,7 +2398,7 @@ func deserializeHtlcs(r io.Reader) (map[CircuitKey]*InvoiceHTLC, error) {
// Decode the contents into the htlc fields.
var (
htlc InvoiceHTLC
key CircuitKey
key models.CircuitKey
chanID uint64
state uint8
acceptTime, resolveTime uint64
@ -2485,7 +2495,7 @@ func copyInvoice(src *Invoice) (*Invoice, error) {
State: src.State,
AmtPaid: src.AmtPaid,
Htlcs: make(
map[CircuitKey]*InvoiceHTLC, len(src.Htlcs),
map[models.CircuitKey]*InvoiceHTLC, len(src.Htlcs),
),
AMPState: make(map[SetID]InvoiceStateAMP),
HodlInvoice: src.HodlInvoice,
@ -2540,7 +2550,7 @@ func makeInvoiceSetIDKey(invoiceNum, setID []byte) [invoiceSetIDKeyLen]byte {
// potentially massive HTLC set, and also allows us to quickly find the HLTCs
// associated with a particular HTLC set.
func updateAMPInvoices(invoiceBucket kvdb.RwBucket, invoiceNum []byte,
htlcsToUpdate map[SetID]map[CircuitKey]*InvoiceHTLC) error {
htlcsToUpdate map[SetID]map[models.CircuitKey]*InvoiceHTLC) error {
for setID, htlcSet := range htlcsToUpdate {
// First write out the set of HTLCs including all the relevant TLV
@ -2567,8 +2577,8 @@ func updateAMPInvoices(invoiceBucket kvdb.RwBucket, invoiceNum []byte,
// set ID), and update sthe internal AMP state of an invoice, and also tallies
// the set of HTLCs to be updated on disk.
func updateHtlcsAmp(invoice *Invoice,
updateMap map[SetID]map[CircuitKey]*InvoiceHTLC, htlc *InvoiceHTLC,
setID SetID, circuitKey CircuitKey) {
updateMap map[SetID]map[models.CircuitKey]*InvoiceHTLC,
htlc *InvoiceHTLC, setID SetID, circuitKey models.CircuitKey) {
ampState, ok := invoice.AMPState[setID]
if !ok {
@ -2576,7 +2586,7 @@ func updateHtlcsAmp(invoice *Invoice,
// we'll need to create it.
ampState = InvoiceStateAMP{
State: HtlcStateAccepted,
InvoiceKeys: make(map[CircuitKey]struct{}),
InvoiceKeys: make(map[models.CircuitKey]struct{}),
}
}
@ -2605,8 +2615,8 @@ func updateHtlcsAmp(invoice *Invoice,
// apply the new update to the update MAP, since all the HTLCs for a given HTLC
// set need to be written in-line with each other.
func cancelHtlcsAmp(invoice *Invoice,
updateMap map[SetID]map[CircuitKey]*InvoiceHTLC, htlc *InvoiceHTLC,
circuitKey CircuitKey) {
updateMap map[SetID]map[models.CircuitKey]*InvoiceHTLC,
htlc *InvoiceHTLC, circuitKey models.CircuitKey) {
setID := htlc.AMP.Record.SetID()
@ -2649,8 +2659,8 @@ func cancelHtlcsAmp(invoice *Invoice,
// that this HTLC set needs to be re-written back to disk.
func settleHtlcsAmp(invoice *Invoice,
settledSetIDs map[SetID]struct{},
updateMap map[SetID]map[CircuitKey]*InvoiceHTLC, htlc *InvoiceHTLC,
circuitKey CircuitKey) {
updateMap map[SetID]map[models.CircuitKey]*InvoiceHTLC,
htlc *InvoiceHTLC, circuitKey models.CircuitKey) {
// First, add the set ID to the set that was settled in this invoice
// update. We'll use this later to update the settle index.
@ -2668,7 +2678,7 @@ func settleHtlcsAmp(invoice *Invoice,
// Finally, we'll add this to the set of HTLCs that need to be updated.
if _, ok := updateMap[setID]; !ok {
updateMap[setID] = make(map[CircuitKey]*InvoiceHTLC)
updateMap[setID] = make(map[models.CircuitKey]*InvoiceHTLC)
}
updateMap[setID][circuitKey] = htlc
}
@ -2735,7 +2745,7 @@ func (d *DB) updateInvoice(hash *lntypes.Hash, refSetID *SetID, invoices,
)
// Process add actions from update descriptor.
htlcsAmpUpdate := make(map[SetID]map[CircuitKey]*InvoiceHTLC)
htlcsAmpUpdate := make(map[SetID]map[models.CircuitKey]*InvoiceHTLC)
for key, htlcUpdate := range update.AddHtlcs {
if _, exists := invoice.Htlcs[key]; exists {
return nil, fmt.Errorf("duplicate add of htlc %v", key)

View File

@ -0,0 +1,92 @@
package models
import (
"encoding/binary"
"fmt"
"io"
"github.com/lightningnetwork/lnd/lnwire"
)
const (
// serializedCircuitKeyLen is the exact length needed to encode a
// serialized CircuitKey.
serializedCircuitKeyLen = 16
)
var (
// ErrInvalidCircuitKeyLen signals that a circuit key could not be
// decoded because the byte slice is of an invalid length.
ErrInvalidCircuitKeyLen = fmt.Errorf("length of serialized circuit " +
"key must be 16 bytes")
)
// CircuitKey is used by a channel to uniquely identify the HTLCs it receives
// from the switch, and is used to purge our in-memory state of HTLCs that have
// already been processed by a link. Two list of CircuitKeys are included in
// each CommitDiff to allow a link to determine which in-memory htlcs directed
// the opening and closing of circuits in the switch's circuit map.
type CircuitKey struct {
// ChanID is the short chanid indicating the HTLC's origin.
//
// NOTE: It is fine for this value to be blank, as this indicates a
// locally-sourced payment.
ChanID lnwire.ShortChannelID
// HtlcID is the unique htlc index predominately assigned by links,
// though can also be assigned by switch in the case of locally-sourced
// payments.
HtlcID uint64
}
// SetBytes deserializes the given bytes into this CircuitKey.
func (k *CircuitKey) SetBytes(bs []byte) error {
if len(bs) != serializedCircuitKeyLen {
return ErrInvalidCircuitKeyLen
}
k.ChanID = lnwire.NewShortChanIDFromInt(
binary.BigEndian.Uint64(bs[:8]))
k.HtlcID = binary.BigEndian.Uint64(bs[8:])
return nil
}
// Bytes returns the serialized bytes for this circuit key.
func (k CircuitKey) Bytes() []byte {
bs := make([]byte, serializedCircuitKeyLen)
binary.BigEndian.PutUint64(bs[:8], k.ChanID.ToUint64())
binary.BigEndian.PutUint64(bs[8:], k.HtlcID)
return bs
}
// Encode writes a CircuitKey to the provided io.Writer.
func (k *CircuitKey) Encode(w io.Writer) error {
var scratch [serializedCircuitKeyLen]byte
binary.BigEndian.PutUint64(scratch[:8], k.ChanID.ToUint64())
binary.BigEndian.PutUint64(scratch[8:], k.HtlcID)
_, err := w.Write(scratch[:])
return err
}
// Decode reads a CircuitKey from the provided io.Reader.
func (k *CircuitKey) Decode(r io.Reader) error {
var scratch [serializedCircuitKeyLen]byte
if _, err := io.ReadFull(r, scratch[:]); err != nil {
return err
}
k.ChanID = lnwire.NewShortChanIDFromInt(
binary.BigEndian.Uint64(scratch[:8]))
k.HtlcID = binary.BigEndian.Uint64(scratch[8:])
return nil
}
// String returns a string representation of the CircuitKey.
func (k CircuitKey) String() string {
return fmt.Sprintf("(Chan ID=%s, HTLC ID=%d)", k.ChanID, k.HtlcID)
}

View File

@ -14,6 +14,7 @@ import (
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/htlcswitch/hop"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/kvdb"
@ -2230,7 +2231,7 @@ func (c *ChannelArbitrator) prepContractResolutions(
for _, htlc := range htlcs {
htlc := htlc
key := channeldb.CircuitKey{
key := models.CircuitKey{
ChanID: c.cfg.ShortChanID,
HtlcID: htlc.HtlcIndex,
}

View File

@ -9,6 +9,7 @@ import (
"github.com/btcsuite/btcd/btcutil"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/htlcswitch/hop"
"github.com/lightningnetwork/lnd/invoices"
"github.com/lightningnetwork/lnd/lntypes"
@ -61,7 +62,7 @@ func (h *htlcIncomingContestResolver) processFinalHtlcFail() error {
// Send notification.
h.ChainArbitratorConfig.HtlcNotifier.NotifyFinalHtlcEvent(
channeldb.CircuitKey{
models.CircuitKey{
ChanID: h.ShortChanID,
HtlcID: h.htlc.HtlcIndex,
},
@ -265,7 +266,7 @@ func (h *htlcIncomingContestResolver) Resolve() (ContractResolver, error) {
// hop on-chain. If this HTLC indeed pays to an existing
// invoice, the invoice registry will tell us what to do with
// the HTLC. This is identical to HTLC resolution in the link.
circuitKey := channeldb.CircuitKey{
circuitKey := models.CircuitKey{
ChanID: h.ShortChanID,
HtlcID: h.htlc.HtlcIndex,
}

View File

@ -9,6 +9,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/htlcswitch/hop"
"github.com/lightningnetwork/lnd/invoices"
"github.com/lightningnetwork/lnd/kvdb"
@ -27,7 +28,7 @@ const (
var (
testResPreimage = lntypes.Preimage{1, 2, 3}
testResHash = testResPreimage.Hash()
testResCircuitKey = channeldb.CircuitKey{}
testResCircuitKey = models.CircuitKey{}
testOnionBlob = []byte{4, 5, 6}
testAcceptHeight int32 = 1234
testHtlcAmount = 2300

View File

@ -11,6 +11,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/input"
"github.com/lightningnetwork/lnd/labels"
"github.com/lightningnetwork/lnd/lnwallet"
@ -479,7 +480,7 @@ func (h *htlcSuccessResolver) checkpointClaim(spendTx *chainhash.Hash,
// Send notification.
h.ChainArbitratorConfig.HtlcNotifier.NotifyFinalHtlcEvent(
channeldb.CircuitKey{
models.CircuitKey{
ChanID: h.ShortChanID,
HtlcID: h.htlc.HtlcIndex,
},

View File

@ -5,6 +5,7 @@ import (
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/htlcswitch/hop"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/invoices"
@ -27,7 +28,7 @@ type Registry interface {
// the resolution is sent on the passed in hodlChan later.
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)
// HodlUnsubscribeAll unsubscribes from all htlc resolutions.
@ -70,6 +71,6 @@ type UtxoSweeper interface {
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)
}

View File

@ -1,11 +1,14 @@
package contractcourt
import "github.com/lightningnetwork/lnd/channeldb"
import (
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
)
type mockHTLCNotifier struct {
HtlcNotifier
}
func (m *mockHTLCNotifier) NotifyFinalHtlcEvent(key channeldb.CircuitKey,
func (m *mockHTLCNotifier) NotifyFinalHtlcEvent(key models.CircuitKey,
info channeldb.FinalHtlcInfo) { //nolint:whitespace
}

View File

@ -2,6 +2,7 @@ package contractcourt
import (
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/invoices"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire"
@ -23,7 +24,7 @@ type mockRegistry struct {
func (r *mockRegistry) 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) {
r.notifyChan <- notifyExitHopData{

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

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 {

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,
}

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,
},

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,
},

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)
}

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,
},

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
}

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,
})

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,
},

View File

@ -8,6 +8,7 @@ import (
"time"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/clock"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire"
@ -81,7 +82,7 @@ type htlcReleaseEvent struct {
invoiceRef channeldb.InvoiceRef
// key is the circuit key of the htlc to release.
key channeldb.CircuitKey
key models.CircuitKey
// releaseTime is the time at which to release the htlc.
releaseTime time.Time
@ -131,11 +132,11 @@ type InvoiceRegistry struct {
hodlSubscriptionsMux sync.RWMutex
// subscriptions is a map from a circuit key to a list of subscribers.
// It is used for efficient notification of links.
hodlSubscriptions map[channeldb.CircuitKey]map[chan<- interface{}]struct{}
hodlSubscriptions map[models.CircuitKey]map[chan<- interface{}]struct{}
// reverseSubscriptions tracks circuit keys subscribed to per
// subscriber. This is used to unsubscribe from all hashes efficiently.
hodlReverseSubscriptions map[chan<- interface{}]map[channeldb.CircuitKey]struct{}
hodlReverseSubscriptions map[chan<- interface{}]map[models.CircuitKey]struct{}
// htlcAutoReleaseChan contains the new htlcs that need to be
// auto-released.
@ -159,12 +160,16 @@ func NewRegistry(cdb *channeldb.DB, expiryWatcher *InvoiceExpiryWatcher,
notificationClients: make(map[uint32]*InvoiceSubscription),
singleNotificationClients: make(map[uint32]*SingleInvoiceSubscription),
invoiceEvents: make(chan *invoiceEvent, 100),
hodlSubscriptions: make(map[channeldb.CircuitKey]map[chan<- interface{}]struct{}),
hodlReverseSubscriptions: make(map[chan<- interface{}]map[channeldb.CircuitKey]struct{}),
cfg: cfg,
htlcAutoReleaseChan: make(chan *htlcReleaseEvent),
expiryWatcher: expiryWatcher,
quit: make(chan struct{}),
hodlSubscriptions: make(
map[models.CircuitKey]map[chan<- interface{}]struct{},
),
hodlReverseSubscriptions: make(
map[chan<- interface{}]map[models.CircuitKey]struct{},
),
cfg: cfg,
htlcAutoReleaseChan: make(chan *htlcReleaseEvent),
expiryWatcher: expiryWatcher,
quit: make(chan struct{}),
}
}
@ -622,7 +627,7 @@ func (i *InvoiceRegistry) LookupInvoiceByRef(
// startHtlcTimer starts a new timer via the invoice registry main loop that
// cancels a single htlc on an invoice when the htlc hold duration has passed.
func (i *InvoiceRegistry) startHtlcTimer(invoiceRef channeldb.InvoiceRef,
key channeldb.CircuitKey, acceptTime time.Time) error {
key models.CircuitKey, acceptTime time.Time) error {
releaseTime := acceptTime.Add(i.cfg.HtlcHoldDuration)
event := &htlcReleaseEvent{
@ -644,7 +649,7 @@ func (i *InvoiceRegistry) startHtlcTimer(invoiceRef channeldb.InvoiceRef,
// a resolution result which will be used to notify subscribed links and
// resolvers of the details of the htlc cancellation.
func (i *InvoiceRegistry) cancelSingleHtlc(invoiceRef channeldb.InvoiceRef,
key channeldb.CircuitKey, result FailResolutionResult) error {
key models.CircuitKey, result FailResolutionResult) error {
updateInvoice := func(invoice *channeldb.Invoice) (
*channeldb.InvoiceUpdateDesc, error) {
@ -702,7 +707,7 @@ func (i *InvoiceRegistry) cancelSingleHtlc(invoiceRef channeldb.InvoiceRef,
// Return an update descriptor that cancels htlc and keeps
// invoice open.
canceledHtlcs := map[channeldb.CircuitKey]struct{}{
canceledHtlcs := map[models.CircuitKey]struct{}{
key: {},
}
@ -910,7 +915,7 @@ func (i *InvoiceRegistry) processAMP(ctx invoiceUpdateCtx) error {
// held htlc.
func (i *InvoiceRegistry) NotifyExitHopHtlc(rHash lntypes.Hash,
amtPaid lnwire.MilliSatoshi, expiry uint32, currentHeight int32,
circuitKey channeldb.CircuitKey, hodlChan chan<- interface{},
circuitKey models.CircuitKey, hodlChan chan<- interface{},
payload Payload) (HtlcResolution, error) {
// Create the update context containing the relevant details of the
@ -1715,7 +1720,7 @@ func (i *InvoiceRegistry) notifyHodlSubscribers(htlcResolution HtlcResolution) {
// hodlSubscribe adds a new invoice subscription.
func (i *InvoiceRegistry) hodlSubscribe(subscriber chan<- interface{},
circuitKey channeldb.CircuitKey) {
circuitKey models.CircuitKey) {
i.hodlSubscriptionsMux.Lock()
defer i.hodlSubscriptionsMux.Unlock()
@ -1731,7 +1736,7 @@ func (i *InvoiceRegistry) hodlSubscribe(subscriber chan<- interface{},
reverseSubscriptions, ok := i.hodlReverseSubscriptions[subscriber]
if !ok {
reverseSubscriptions = make(map[channeldb.CircuitKey]struct{})
reverseSubscriptions = make(map[models.CircuitKey]struct{})
i.hodlReverseSubscriptions[subscriber] = reverseSubscriptions
}
reverseSubscriptions[circuitKey] = struct{}{}

View File

@ -3,7 +3,7 @@ package invoices
import (
"time"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/lntypes"
)
@ -11,14 +11,14 @@ import (
type HtlcResolution interface {
// CircuitKey returns the circuit key for the htlc that we have a
// resolution for.
CircuitKey() channeldb.CircuitKey
CircuitKey() models.CircuitKey
}
// HtlcFailResolution is an implementation of the HtlcResolution interface
// which is returned when a htlc is failed.
type HtlcFailResolution struct {
// circuitKey is the key of the htlc for which we have a resolution.
circuitKey channeldb.CircuitKey
circuitKey models.CircuitKey
// AcceptHeight is the original height at which the htlc was accepted.
AcceptHeight int32
@ -28,8 +28,8 @@ type HtlcFailResolution struct {
}
// NewFailResolution returns a htlc failure resolution.
func NewFailResolution(key channeldb.CircuitKey,
acceptHeight int32, outcome FailResolutionResult) *HtlcFailResolution {
func NewFailResolution(key models.CircuitKey, acceptHeight int32,
outcome FailResolutionResult) *HtlcFailResolution {
return &HtlcFailResolution{
circuitKey: key,
@ -42,7 +42,7 @@ func NewFailResolution(key channeldb.CircuitKey,
// resolution for.
//
// Note: it is part of the HtlcResolution interface.
func (f *HtlcFailResolution) CircuitKey() channeldb.CircuitKey {
func (f *HtlcFailResolution) CircuitKey() models.CircuitKey {
return f.circuitKey
}
@ -53,7 +53,7 @@ type HtlcSettleResolution struct {
Preimage lntypes.Preimage
// circuitKey is the key of the htlc for which we have a resolution.
circuitKey channeldb.CircuitKey
circuitKey models.CircuitKey
// acceptHeight is the original height at which the htlc was accepted.
AcceptHeight int32
@ -65,7 +65,7 @@ type HtlcSettleResolution struct {
// NewSettleResolution returns a htlc resolution which is associated with a
// settle.
func NewSettleResolution(preimage lntypes.Preimage,
key channeldb.CircuitKey, acceptHeight int32,
key models.CircuitKey, acceptHeight int32,
outcome SettleResolutionResult) *HtlcSettleResolution {
return &HtlcSettleResolution{
@ -80,7 +80,7 @@ func NewSettleResolution(preimage lntypes.Preimage,
// resolution for.
//
// Note: it is part of the HtlcResolution interface.
func (s *HtlcSettleResolution) CircuitKey() channeldb.CircuitKey {
func (s *HtlcSettleResolution) CircuitKey() models.CircuitKey {
return s.circuitKey
}
@ -92,7 +92,7 @@ func (s *HtlcSettleResolution) CircuitKey() channeldb.CircuitKey {
// acceptResolution, a nil resolution should be surfaced.
type htlcAcceptResolution struct {
// circuitKey is the key of the htlc for which we have a resolution.
circuitKey channeldb.CircuitKey
circuitKey models.CircuitKey
// autoRelease signals that the htlc should be automatically released
// after a timeout.
@ -107,7 +107,7 @@ type htlcAcceptResolution struct {
// newAcceptResolution returns a htlc resolution which is associated with a
// htlc accept.
func newAcceptResolution(key channeldb.CircuitKey,
func newAcceptResolution(key models.CircuitKey,
outcome acceptResolutionResult) *htlcAcceptResolution {
return &htlcAcceptResolution{
@ -120,6 +120,6 @@ func newAcceptResolution(key channeldb.CircuitKey,
// resolution for.
//
// Note: it is part of the HtlcResolution interface.
func (a *htlcAcceptResolution) CircuitKey() channeldb.CircuitKey {
func (a *htlcAcceptResolution) CircuitKey() models.CircuitKey {
return a.circuitKey
}

View File

@ -17,6 +17,7 @@ import (
"github.com/btcsuite/btcd/chaincfg/chainhash"
"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/lntypes"
"github.com/lightningnetwork/lnd/lnwire"
@ -227,8 +228,8 @@ func newTestContext(t *testing.T) *testContext {
return &ctx
}
func getCircuitKey(htlcID uint64) channeldb.CircuitKey {
return channeldb.CircuitKey{
func getCircuitKey(htlcID uint64) models.CircuitKey {
return models.CircuitKey{
ChanID: lnwire.ShortChannelID{
BlockHeight: 1, TxIndex: 2, TxPosition: 3,
},
@ -453,11 +454,11 @@ func setupHodlExpiry(t *testing.T, creationDate time.Time,
invoice := newTestInvoice(t, preimage, creationDate, expiry)
invoice.State = startState
invoice.HodlInvoice = true
invoice.Htlcs = make(map[channeldb.CircuitKey]*channeldb.InvoiceHTLC)
invoice.Htlcs = make(map[models.CircuitKey]*channeldb.InvoiceHTLC)
// If we have any htlcs, add them with unique circult keys.
for i, htlc := range startHtlcs {
key := channeldb.CircuitKey{
key := models.CircuitKey{
HtlcID: uint64(i),
}

View File

@ -6,6 +6,7 @@ import (
"github.com/lightningnetwork/lnd/amp"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/record"
@ -15,7 +16,7 @@ import (
// update to be carried out.
type invoiceUpdateCtx struct {
hash lntypes.Hash
circuitKey channeldb.CircuitKey
circuitKey models.CircuitKey
amtPaid lnwire.MilliSatoshi
expiry uint32
currentHeight int32
@ -233,7 +234,7 @@ func updateMpp(ctx *invoiceUpdateCtx,
}
// Record HTLC in the invoice database.
newHtlcs := map[channeldb.CircuitKey]*channeldb.HtlcAcceptDesc{
newHtlcs := map[models.CircuitKey]*channeldb.HtlcAcceptDesc{
ctx.circuitKey: acceptDesc,
}
@ -258,7 +259,7 @@ func updateMpp(ctx *invoiceUpdateCtx,
}
var (
htlcPreimages map[channeldb.CircuitKey]lntypes.Preimage
htlcPreimages map[models.CircuitKey]lntypes.Preimage
htlcPreimage lntypes.Preimage
)
if ctx.amp != nil {
@ -290,10 +291,10 @@ func updateMpp(ctx *invoiceUpdateCtx,
}
// HTLCSet is a map of CircuitKey to InvoiceHTLC.
type HTLCSet = map[channeldb.CircuitKey]*channeldb.InvoiceHTLC
type HTLCSet = map[models.CircuitKey]*channeldb.InvoiceHTLC
// HTLCPreimages is a map of CircuitKey to preimage.
type HTLCPreimages = map[channeldb.CircuitKey]lntypes.Preimage
type HTLCPreimages = map[models.CircuitKey]lntypes.Preimage
// reconstructAMPPreimages reconstructs the root seed for an AMP HTLC set and
// verifies that all derived child hashes match the payment hashes of the HTLCs
@ -316,7 +317,7 @@ func reconstructAMPPreimages(ctx *invoiceUpdateCtx,
// Next, construct an index mapping the position in childDescs to a
// circuit key for all preexisting HTLCs.
indexToCircuitKey := make(map[int]channeldb.CircuitKey)
indexToCircuitKey := make(map[int]models.CircuitKey)
// Add the child descriptor for each HTLC in the HTLC set, recording
// it's position within the slice.
@ -350,7 +351,7 @@ func reconstructAMPPreimages(ctx *invoiceUpdateCtx,
// Finally, construct the map of learned preimages indexed by circuit
// key, so that they can be persisted along with each HTLC when updating
// the invoice.
htlcPreimages := make(map[channeldb.CircuitKey]lntypes.Preimage)
htlcPreimages := make(map[models.CircuitKey]lntypes.Preimage)
htlcPreimages[ctx.circuitKey] = children[0].Preimage
for idx, child := range children[1:] {
circuitKey := indexToCircuitKey[idx]
@ -417,7 +418,7 @@ func updateLegacy(ctx *invoiceUpdateCtx,
}
// Record HTLC in the invoice database.
newHtlcs := map[channeldb.CircuitKey]*channeldb.HtlcAcceptDesc{
newHtlcs := map[models.CircuitKey]*channeldb.HtlcAcceptDesc{
ctx.circuitKey: {
Amt: ctx.amtPaid,
Expiry: ctx.expiry,

View File

@ -3,7 +3,7 @@ package routerrpc
import (
"errors"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/htlcswitch"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lntypes"
@ -102,7 +102,7 @@ func (r *forwardInterceptor) resolveFromClient(
log.Tracef("Resolving intercepted packet %v", in)
circuitKey := channeldb.CircuitKey{
circuitKey := models.CircuitKey{
ChanID: lnwire.NewShortChanIDFromInt(in.IncomingCircuitKey.ChanId),
HtlcID: in.IncomingCircuitKey.HtlcId,
}

View File

@ -23,6 +23,7 @@ import (
"github.com/lightningnetwork/lnd/build"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
"github.com/lightningnetwork/lnd/lnwire"
@ -286,7 +287,7 @@ type PaymentDescriptor struct {
// NOTE: This field is only populated for payment descriptors in the
// *local* update log, and if the Add packet was delivered by the
// switch.
OpenCircuitKey *channeldb.CircuitKey
OpenCircuitKey *models.CircuitKey
// ClosedCircuitKey references the incoming Chan/HTLC ID of the Add HTLC
// that opened the circuit.
@ -294,7 +295,7 @@ type PaymentDescriptor struct {
// NOTE: This field is only populated for payment descriptors in the
// *local* update log, and if settle/fails have a committed circuit in
// the circuit map.
ClosedCircuitKey *channeldb.CircuitKey
ClosedCircuitKey *models.CircuitKey
// localOutputIndex is the output index of this HTLc output in the
// commitment transaction of the local node.
@ -3294,8 +3295,8 @@ func (lc *LightningChannel) createCommitDiff(
logUpdates []channeldb.LogUpdate
ackAddRefs []channeldb.AddRef
settleFailRefs []channeldb.SettleFailRef
openCircuitKeys []channeldb.CircuitKey
closedCircuitKeys []channeldb.CircuitKey
openCircuitKeys []models.CircuitKey
closedCircuitKeys []models.CircuitKey
)
// We'll now run through our local update log to locate the items which
@ -3850,14 +3851,14 @@ func (lc *LightningChannel) SignNextCommitment() (lnwire.Sig, []lnwire.Sig,
// not received
//
// If we detect a scenario where we need to send a CommitSig+Updates, this
// method also returns two sets channeldb.CircuitKeys identifying the circuits
// method also returns two sets models.CircuitKeys identifying the circuits
// that were opened and closed, respectively, as a result of signing the
// previous commitment txn. This allows the link to clear its mailbox of those
// circuits in case they are still in memory, and ensure the switch's circuit
// map has been updated by deleting the closed circuits.
func (lc *LightningChannel) ProcessChanSyncMsg(
msg *lnwire.ChannelReestablish) ([]lnwire.Message, []channeldb.CircuitKey,
[]channeldb.CircuitKey, error) {
msg *lnwire.ChannelReestablish) ([]lnwire.Message, []models.CircuitKey,
[]models.CircuitKey, error) {
// Now we'll examine the state we have, vs what was contained in the
// chain sync message. If we're de-synchronized, then we'll send a
@ -3865,8 +3866,8 @@ func (lc *LightningChannel) ProcessChanSyncMsg(
// resync.
var (
updates []lnwire.Message
openedCircuits []channeldb.CircuitKey
closedCircuits []channeldb.CircuitKey
openedCircuits []models.CircuitKey
closedCircuits []models.CircuitKey
)
// If the remote party included the optional fields, then we'll verify
@ -5162,7 +5163,7 @@ func (lc *LightningChannel) InitNextRevocation(revKey *btcec.PublicKey) error {
//
// NOTE: It is okay for sourceRef to be nil when unit testing the wallet.
func (lc *LightningChannel) AddHTLC(htlc *lnwire.UpdateAddHTLC,
openKey *channeldb.CircuitKey) (uint64, error) {
openKey *models.CircuitKey) (uint64, error) {
lc.Lock()
defer lc.Unlock()
@ -5275,7 +5276,7 @@ func (lc *LightningChannel) MayAddOutgoingHtlc(amt lnwire.MilliSatoshi) error {
&lnwire.UpdateAddHTLC{
Amount: mockHtlcAmt,
},
&channeldb.CircuitKey{},
&models.CircuitKey{},
)
if err := lc.validateAddHtlc(pd); err != nil {
@ -5289,7 +5290,7 @@ func (lc *LightningChannel) MayAddOutgoingHtlc(amt lnwire.MilliSatoshi) error {
// htlcAddDescriptor returns a payment descriptor for the htlc and open key
// provided to add to our local update log.
func (lc *LightningChannel) htlcAddDescriptor(htlc *lnwire.UpdateAddHTLC,
openKey *channeldb.CircuitKey) *PaymentDescriptor {
openKey *models.CircuitKey) *PaymentDescriptor {
return &PaymentDescriptor{
EntryType: Add,
@ -5399,7 +5400,7 @@ func (lc *LightningChannel) ReceiveHTLC(htlc *lnwire.UpdateAddHTLC) (uint64, err
// testing the wallet.
func (lc *LightningChannel) SettleHTLC(preimage [32]byte,
htlcIndex uint64, sourceRef *channeldb.AddRef,
destRef *channeldb.SettleFailRef, closeKey *channeldb.CircuitKey) error {
destRef *channeldb.SettleFailRef, closeKey *models.CircuitKey) error {
lc.Lock()
defer lc.Unlock()
@ -5510,7 +5511,7 @@ func (lc *LightningChannel) ReceiveHTLCSettle(preimage [32]byte, htlcIndex uint6
// testing the wallet.
func (lc *LightningChannel) FailHTLC(htlcIndex uint64, reason []byte,
sourceRef *channeldb.AddRef, destRef *channeldb.SettleFailRef,
closeKey *channeldb.CircuitKey) error {
closeKey *models.CircuitKey) error {
lc.Lock()
defer lc.Unlock()

View File

@ -4,6 +4,7 @@ import (
"sync"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/contractcourt"
"github.com/lightningnetwork/lnd/htlcswitch"
"github.com/lightningnetwork/lnd/htlcswitch/hop"
@ -95,7 +96,7 @@ func (p *preimageBeacon) SubscribeUpdates(
Hash: htlc.RHash,
IncomingExpiry: htlc.RefundTimeout,
IncomingAmount: htlc.Amt,
IncomingCircuit: channeldb.CircuitKey{
IncomingCircuit: models.CircuitKey{
ChanID: chanID,
HtlcID: htlc.HtlcIndex,
},