invoices: store custom records in invoice database
This commit is contained in:
10 files changed
+725
-584
No files matched your search
@@ -7,6 +7,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
"github.com/lightningnetwork/lnd/htlcswitch/hop"
|
||||||
"github.com/lightningnetwork/lnd/lntypes"
|
"github.com/lightningnetwork/lnd/lntypes"
|
||||||
"github.com/lightningnetwork/lnd/lnwire"
|
"github.com/lightningnetwork/lnd/lnwire"
|
||||||
)
|
)
|
||||||
@@ -209,13 +210,15 @@ func TestInvoiceCancelSingleHtlc(t *testing.T) {
|
|||||||
|
|
||||||
// Accept an htlc on this invoice.
|
// Accept an htlc on this invoice.
|
||||||
key := CircuitKey{ChanID: lnwire.NewShortChanIDFromInt(1), HtlcID: 4}
|
key := CircuitKey{ChanID: lnwire.NewShortChanIDFromInt(1), HtlcID: 4}
|
||||||
|
htlc := HtlcAcceptDesc{
|
||||||
|
Amt: 500,
|
||||||
|
CustomRecords: make(hop.CustomRecordSet),
|
||||||
|
}
|
||||||
invoice, err := db.UpdateInvoice(paymentHash,
|
invoice, err := db.UpdateInvoice(paymentHash,
|
||||||
func(invoice *Invoice) (*InvoiceUpdateDesc, error) {
|
func(invoice *Invoice) (*InvoiceUpdateDesc, error) {
|
||||||
return &InvoiceUpdateDesc{
|
return &InvoiceUpdateDesc{
|
||||||
AddHtlcs: map[CircuitKey]*HtlcAcceptDesc{
|
AddHtlcs: map[CircuitKey]*HtlcAcceptDesc{
|
||||||
key: {
|
key: &htlc,
|
||||||
Amt: 500,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
})
|
})
|
||||||
@@ -432,10 +435,11 @@ func TestDuplicateSettleInvoice(t *testing.T) {
|
|||||||
invoice.SettleDate = dbInvoice.SettleDate
|
invoice.SettleDate = dbInvoice.SettleDate
|
||||||
invoice.Htlcs = map[CircuitKey]*InvoiceHTLC{
|
invoice.Htlcs = map[CircuitKey]*InvoiceHTLC{
|
||||||
{}: {
|
{}: {
|
||||||
Amt: amt,
|
Amt: amt,
|
||||||
AcceptTime: time.Unix(1, 0),
|
AcceptTime: time.Unix(1, 0),
|
||||||
ResolveTime: time.Unix(1, 0),
|
ResolveTime: time.Unix(1, 0),
|
||||||
State: HtlcStateSettled,
|
State: HtlcStateSettled,
|
||||||
|
CustomRecords: make(hop.CustomRecordSet),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -747,6 +751,8 @@ func getUpdateInvoice(amt lnwire.MilliSatoshi) InvoiceUpdateCallback {
|
|||||||
return nil, ErrInvoiceAlreadySettled
|
return nil, ErrInvoiceAlreadySettled
|
||||||
}
|
}
|
||||||
|
|
||||||
|
noRecords := make(hop.CustomRecordSet)
|
||||||
|
|
||||||
update := &InvoiceUpdateDesc{
|
update := &InvoiceUpdateDesc{
|
||||||
State: &InvoiceStateUpdateDesc{
|
State: &InvoiceStateUpdateDesc{
|
||||||
Preimage: invoice.Terms.PaymentPreimage,
|
Preimage: invoice.Terms.PaymentPreimage,
|
||||||
@@ -754,7 +760,8 @@ func getUpdateInvoice(amt lnwire.MilliSatoshi) InvoiceUpdateCallback {
|
|||||||
},
|
},
|
||||||
AddHtlcs: map[CircuitKey]*HtlcAcceptDesc{
|
AddHtlcs: map[CircuitKey]*HtlcAcceptDesc{
|
||||||
{}: {
|
{}: {
|
||||||
Amt: amt,
|
Amt: amt,
|
||||||
|
CustomRecords: noRecords,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -762,3 +769,64 @@ func getUpdateInvoice(amt lnwire.MilliSatoshi) InvoiceUpdateCallback {
|
|||||||
return update, nil
|
return update, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestCustomRecords tests that custom records are properly recorded in the
|
||||||
|
// invoice database.
|
||||||
|
func TestCustomRecords(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
db, cleanUp, err := makeTestDB()
|
||||||
|
defer cleanUp()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unable to make test db: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
testInvoice := &Invoice{
|
||||||
|
Htlcs: map[CircuitKey]*InvoiceHTLC{},
|
||||||
|
}
|
||||||
|
testInvoice.Terms.Value = lnwire.NewMSatFromSatoshis(10000)
|
||||||
|
testInvoice.Terms.Features = emptyFeatures
|
||||||
|
|
||||||
|
var paymentHash lntypes.Hash
|
||||||
|
if _, err := db.AddInvoice(testInvoice, paymentHash); err != nil {
|
||||||
|
t.Fatalf("unable to find invoice: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Accept an htlc with custom records on this invoice.
|
||||||
|
key := CircuitKey{ChanID: lnwire.NewShortChanIDFromInt(1), HtlcID: 4}
|
||||||
|
|
||||||
|
records := hop.CustomRecordSet{
|
||||||
|
100000: []byte{},
|
||||||
|
100001: []byte{1, 2},
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = db.UpdateInvoice(paymentHash,
|
||||||
|
func(invoice *Invoice) (*InvoiceUpdateDesc, error) {
|
||||||
|
return &InvoiceUpdateDesc{
|
||||||
|
AddHtlcs: map[CircuitKey]*HtlcAcceptDesc{
|
||||||
|
key: {
|
||||||
|
Amt: 500,
|
||||||
|
CustomRecords: records,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unable to add invoice htlc: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Retrieve the invoice from that database and verify that the custom
|
||||||
|
// records are present.
|
||||||
|
dbInvoice, err := db.LookupInvoice(paymentHash)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unable to lookup invoice: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(dbInvoice.Htlcs) != 1 {
|
||||||
|
t.Fatalf("expected the htlc to be added")
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(records, dbInvoice.Htlcs[key].CustomRecords) {
|
||||||
|
t.Fatalf("invalid custom records")
|
||||||
|
}
|
||||||
|
}
|
||||||
+40
-7
@@ -9,6 +9,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/coreos/bbolt"
|
"github.com/coreos/bbolt"
|
||||||
|
"github.com/lightningnetwork/lnd/htlcswitch/hop"
|
||||||
"github.com/lightningnetwork/lnd/lntypes"
|
"github.com/lightningnetwork/lnd/lntypes"
|
||||||
"github.com/lightningnetwork/lnd/lnwire"
|
"github.com/lightningnetwork/lnd/lnwire"
|
||||||
"github.com/lightningnetwork/lnd/tlv"
|
"github.com/lightningnetwork/lnd/tlv"
|
||||||
@@ -308,6 +309,10 @@ type InvoiceHTLC struct {
|
|||||||
// canceled htlc isn't just removed from the invoice htlcs map, because
|
// canceled htlc isn't just removed from the invoice htlcs map, because
|
||||||
// we need AcceptHeight to properly cancel the htlc back.
|
// we need AcceptHeight to properly cancel the htlc back.
|
||||||
State HtlcState
|
State HtlcState
|
||||||
|
|
||||||
|
// CustomRecords contains the custom key/value pairs that accompanied
|
||||||
|
// the htlc.
|
||||||
|
CustomRecords hop.CustomRecordSet
|
||||||
}
|
}
|
||||||
|
|
||||||
// HtlcAcceptDesc describes the details of a newly accepted htlc.
|
// HtlcAcceptDesc describes the details of a newly accepted htlc.
|
||||||
@@ -320,6 +325,10 @@ type HtlcAcceptDesc struct {
|
|||||||
|
|
||||||
// Expiry is the expiry height of this htlc.
|
// Expiry is the expiry height of this htlc.
|
||||||
Expiry uint32
|
Expiry uint32
|
||||||
|
|
||||||
|
// CustomRecords contains the custom key/value pairs that accompanied
|
||||||
|
// the htlc.
|
||||||
|
CustomRecords hop.CustomRecordSet
|
||||||
}
|
}
|
||||||
|
|
||||||
// InvoiceUpdateDesc describes the changes that should be applied to the
|
// InvoiceUpdateDesc describes the changes that should be applied to the
|
||||||
@@ -1013,7 +1022,8 @@ func serializeHtlcs(w io.Writer, htlcs map[CircuitKey]*InvoiceHTLC) error {
|
|||||||
resolveTime := uint64(htlc.ResolveTime.UnixNano())
|
resolveTime := uint64(htlc.ResolveTime.UnixNano())
|
||||||
state := uint8(htlc.State)
|
state := uint8(htlc.State)
|
||||||
|
|
||||||
tlvStream, err := tlv.NewStream(
|
var records []tlv.Record
|
||||||
|
records = append(records,
|
||||||
tlv.MakePrimitiveRecord(chanIDType, &chanID),
|
tlv.MakePrimitiveRecord(chanIDType, &chanID),
|
||||||
tlv.MakePrimitiveRecord(htlcIDType, &key.HtlcID),
|
tlv.MakePrimitiveRecord(htlcIDType, &key.HtlcID),
|
||||||
tlv.MakePrimitiveRecord(amtType, &amt),
|
tlv.MakePrimitiveRecord(amtType, &amt),
|
||||||
@@ -1025,6 +1035,16 @@ func serializeHtlcs(w io.Writer, htlcs map[CircuitKey]*InvoiceHTLC) error {
|
|||||||
tlv.MakePrimitiveRecord(expiryHeightType, &htlc.Expiry),
|
tlv.MakePrimitiveRecord(expiryHeightType, &htlc.Expiry),
|
||||||
tlv.MakePrimitiveRecord(htlcStateType, &state),
|
tlv.MakePrimitiveRecord(htlcStateType, &state),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Convert the custom records to tlv.Record types that are ready
|
||||||
|
// for serialization.
|
||||||
|
customRecords := tlv.MapToRecords(htlc.CustomRecords)
|
||||||
|
|
||||||
|
// Append the custom records. Their ids are in the experimental
|
||||||
|
// range and sorted, so there is no need to sort again.
|
||||||
|
records = append(records, customRecords...)
|
||||||
|
|
||||||
|
tlvStream, err := tlv.NewStream(records...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -1191,7 +1211,8 @@ func deserializeHtlcs(r io.Reader) (map[CircuitKey]*InvoiceHTLC, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := tlvStream.Decode(htlcReader); err != nil {
|
parsedTypes, err := tlvStream.DecodeWithParsedTypes(htlcReader)
|
||||||
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1201,6 +1222,10 @@ func deserializeHtlcs(r io.Reader) (map[CircuitKey]*InvoiceHTLC, error) {
|
|||||||
htlc.State = HtlcState(state)
|
htlc.State = HtlcState(state)
|
||||||
htlc.Amt = lnwire.MilliSatoshi(amt)
|
htlc.Amt = lnwire.MilliSatoshi(amt)
|
||||||
|
|
||||||
|
// Reconstruct the custom records fields from the parsed types
|
||||||
|
// map return from the tlv parser.
|
||||||
|
htlc.CustomRecords = hop.NewCustomRecords(parsedTypes)
|
||||||
|
|
||||||
htlcs[key] = &htlc
|
htlcs[key] = &htlc
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1290,12 +1315,20 @@ func (d *DB) updateInvoice(hash lntypes.Hash, invoices, settleIndex *bbolt.Bucke
|
|||||||
if _, exists := invoice.Htlcs[key]; exists {
|
if _, exists := invoice.Htlcs[key]; exists {
|
||||||
return nil, fmt.Errorf("duplicate add of htlc %v", key)
|
return nil, fmt.Errorf("duplicate add of htlc %v", key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Force caller to supply htlc without custom records in a
|
||||||
|
// consistent way.
|
||||||
|
if htlcUpdate.CustomRecords == nil {
|
||||||
|
return nil, errors.New("nil custom records map")
|
||||||
|
}
|
||||||
|
|
||||||
htlc := &InvoiceHTLC{
|
htlc := &InvoiceHTLC{
|
||||||
Amt: htlcUpdate.Amt,
|
Amt: htlcUpdate.Amt,
|
||||||
Expiry: htlcUpdate.Expiry,
|
Expiry: htlcUpdate.Expiry,
|
||||||
AcceptHeight: uint32(htlcUpdate.AcceptHeight),
|
AcceptHeight: uint32(htlcUpdate.AcceptHeight),
|
||||||
AcceptTime: now,
|
AcceptTime: now,
|
||||||
State: HtlcStateAccepted,
|
State: HtlcStateAccepted,
|
||||||
|
CustomRecords: htlcUpdate.CustomRecords,
|
||||||
}
|
}
|
||||||
|
|
||||||
invoice.Htlcs[key] = htlc
|
invoice.Htlcs[key] = htlc
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package invoices
|
package invoices
|
||||||
|
|
||||||
import "github.com/lightningnetwork/lnd/record"
|
import (
|
||||||
|
"github.com/lightningnetwork/lnd/htlcswitch/hop"
|
||||||
|
"github.com/lightningnetwork/lnd/record"
|
||||||
|
)
|
||||||
|
|
||||||
// Payload abstracts access to any additional fields provided in the final hop's
|
// Payload abstracts access to any additional fields provided in the final hop's
|
||||||
// TLV onion payload.
|
// TLV onion payload.
|
||||||
@@ -8,4 +11,8 @@ type Payload interface {
|
|||||||
// MultiPath returns the record corresponding the option_mpp parsed from
|
// MultiPath returns the record corresponding the option_mpp parsed from
|
||||||
// the onion payload.
|
// the onion payload.
|
||||||
MultiPath() *record.MPP
|
MultiPath() *record.MPP
|
||||||
|
|
||||||
|
// CustomRecords returns the custom tlv type records that were parsed
|
||||||
|
// from the payload.
|
||||||
|
CustomRecords() hop.CustomRecordSet
|
||||||
}
|
}
|
||||||
@@ -443,6 +443,7 @@ func (i *InvoiceRegistry) NotifyExitHopHtlc(rHash lntypes.Hash,
|
|||||||
expiry: expiry,
|
expiry: expiry,
|
||||||
currentHeight: currentHeight,
|
currentHeight: currentHeight,
|
||||||
finalCltvRejectDelta: i.finalCltvRejectDelta,
|
finalCltvRejectDelta: i.finalCltvRejectDelta,
|
||||||
|
customRecords: payload.CustomRecords(),
|
||||||
}
|
}
|
||||||
|
|
||||||
// We'll attempt to settle an invoice matching this rHash on disk (if
|
// We'll attempt to settle an invoice matching this rHash on disk (if
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/lightningnetwork/lnd/channeldb"
|
"github.com/lightningnetwork/lnd/channeldb"
|
||||||
|
"github.com/lightningnetwork/lnd/htlcswitch/hop"
|
||||||
"github.com/lightningnetwork/lnd/lntypes"
|
"github.com/lightningnetwork/lnd/lntypes"
|
||||||
"github.com/lightningnetwork/lnd/lnwire"
|
"github.com/lightningnetwork/lnd/lnwire"
|
||||||
"github.com/lightningnetwork/lnd/record"
|
"github.com/lightningnetwork/lnd/record"
|
||||||
@@ -669,3 +670,7 @@ type mockPayload struct {
|
|||||||
func (p *mockPayload) MultiPath() *record.MPP {
|
func (p *mockPayload) MultiPath() *record.MPP {
|
||||||
return p.mpp
|
return p.mpp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *mockPayload) CustomRecords() hop.CustomRecordSet {
|
||||||
|
return make(hop.CustomRecordSet)
|
||||||
|
}
|
||||||
+7
-3
@@ -3,6 +3,8 @@ package invoices
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
|
"github.com/lightningnetwork/lnd/htlcswitch/hop"
|
||||||
|
|
||||||
"github.com/lightningnetwork/lnd/channeldb"
|
"github.com/lightningnetwork/lnd/channeldb"
|
||||||
"github.com/lightningnetwork/lnd/lnwire"
|
"github.com/lightningnetwork/lnd/lnwire"
|
||||||
)
|
)
|
||||||
@@ -74,6 +76,7 @@ type invoiceUpdateCtx struct {
|
|||||||
expiry uint32
|
expiry uint32
|
||||||
currentHeight int32
|
currentHeight int32
|
||||||
finalCltvRejectDelta int32
|
finalCltvRejectDelta int32
|
||||||
|
customRecords hop.CustomRecordSet
|
||||||
}
|
}
|
||||||
|
|
||||||
// updateInvoice is a callback for DB.UpdateInvoice that contains the invoice
|
// updateInvoice is a callback for DB.UpdateInvoice that contains the invoice
|
||||||
@@ -125,9 +128,10 @@ func updateInvoice(ctx *invoiceUpdateCtx, inv *channeldb.Invoice) (
|
|||||||
// Record HTLC in the invoice database.
|
// Record HTLC in the invoice database.
|
||||||
newHtlcs := map[channeldb.CircuitKey]*channeldb.HtlcAcceptDesc{
|
newHtlcs := map[channeldb.CircuitKey]*channeldb.HtlcAcceptDesc{
|
||||||
ctx.circuitKey: {
|
ctx.circuitKey: {
|
||||||
Amt: ctx.amtPaid,
|
Amt: ctx.amtPaid,
|
||||||
Expiry: ctx.expiry,
|
Expiry: ctx.expiry,
|
||||||
AcceptHeight: ctx.currentHeight,
|
AcceptHeight: ctx.currentHeight,
|
||||||
|
CustomRecords: ctx.customRecords,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -75,13 +75,14 @@ func CreateRPCInvoice(invoice *channeldb.Invoice,
|
|||||||
}
|
}
|
||||||
|
|
||||||
rpcHtlc := lnrpc.InvoiceHTLC{
|
rpcHtlc := lnrpc.InvoiceHTLC{
|
||||||
ChanId: key.ChanID.ToUint64(),
|
ChanId: key.ChanID.ToUint64(),
|
||||||
HtlcIndex: key.HtlcID,
|
HtlcIndex: key.HtlcID,
|
||||||
AcceptHeight: int32(htlc.AcceptHeight),
|
AcceptHeight: int32(htlc.AcceptHeight),
|
||||||
AcceptTime: htlc.AcceptTime.Unix(),
|
AcceptTime: htlc.AcceptTime.Unix(),
|
||||||
ExpiryHeight: int32(htlc.Expiry),
|
ExpiryHeight: int32(htlc.Expiry),
|
||||||
AmtMsat: uint64(htlc.Amt),
|
AmtMsat: uint64(htlc.Amt),
|
||||||
State: state,
|
State: state,
|
||||||
|
CustomRecords: htlc.CustomRecords,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only report resolved times if htlc is resolved.
|
// Only report resolved times if htlc is resolved.
|
||||||
|
|||||||
+569
-558
File diff suppressed because it is too large.
Load diff
@@ -2396,6 +2396,9 @@ message InvoiceHTLC {
|
|||||||
|
|
||||||
/// Current state the htlc is in.
|
/// Current state the htlc is in.
|
||||||
InvoiceHTLCState state = 8 [json_name = "state"];
|
InvoiceHTLCState state = 8 [json_name = "state"];
|
||||||
|
|
||||||
|
/// Custom tlv records.
|
||||||
|
map<uint64, bytes> custom_records = 9 [json_name = "custom_records"];
|
||||||
}
|
}
|
||||||
|
|
||||||
message AddInvoiceResponse {
|
message AddInvoiceResponse {
|
||||||
|
|||||||
@@ -2819,6 +2819,14 @@
|
|||||||
"state": {
|
"state": {
|
||||||
"$ref": "#/definitions/lnrpcInvoiceHTLCState",
|
"$ref": "#/definitions/lnrpcInvoiceHTLCState",
|
||||||
"description": "/ Current state the htlc is in."
|
"description": "/ Current state the htlc is in."
|
||||||
|
},
|
||||||
|
"custom_records": {
|
||||||
|
"type": "object",
|
||||||
|
"additionalProperties": {
|
||||||
|
"type": "string",
|
||||||
|
"format": "byte"
|
||||||
|
},
|
||||||
|
"description": "/ Custom tlv records."
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"title": "/ Details of an HTLC that paid to an invoice"
|
"title": "/ Details of an HTLC that paid to an invoice"
|
||||||
|
|||||||
Reference in new issue
Block a user