channeldb: update channeldb to set and store SettleDate for invoices
This commit is contained in:
parent
231ed5bb32
commit
cf6f313cff
@ -87,8 +87,9 @@ func TestInvoiceWorkflow(t *testing.T) {
|
|||||||
spew.Sdump(fakeInvoice), spew.Sdump(dbInvoice))
|
spew.Sdump(fakeInvoice), spew.Sdump(dbInvoice))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Settle the invoice, the versin retreived from the database should
|
// Settle the invoice, the version retrieved from the database should
|
||||||
// now have the settled bit toggle to true.
|
// now have the settled bit toggle to true and a non-default
|
||||||
|
// SettledDate
|
||||||
if err := db.SettleInvoice(paymentHash); err != nil {
|
if err := db.SettleInvoice(paymentHash); err != nil {
|
||||||
t.Fatalf("unable to settle invoice: %v", err)
|
t.Fatalf("unable to settle invoice: %v", err)
|
||||||
}
|
}
|
||||||
@ -100,6 +101,10 @@ func TestInvoiceWorkflow(t *testing.T) {
|
|||||||
t.Fatalf("invoice should now be settled but isn't")
|
t.Fatalf("invoice should now be settled but isn't")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if dbInvoice2.SettleDate.IsZero() {
|
||||||
|
t.Fatalf("invoice should have non-zero SettledDate but isn't")
|
||||||
|
}
|
||||||
|
|
||||||
// Attempt to insert generated above again, this should fail as
|
// Attempt to insert generated above again, this should fail as
|
||||||
// duplicates are rejected by the processing logic.
|
// duplicates are rejected by the processing logic.
|
||||||
if err := db.AddInvoice(fakeInvoice); err != ErrDuplicateInvoice {
|
if err := db.AddInvoice(fakeInvoice); err != ErrDuplicateInvoice {
|
||||||
|
@ -100,6 +100,9 @@ type Invoice struct {
|
|||||||
// CreationDate is the exact time the invoice was created.
|
// CreationDate is the exact time the invoice was created.
|
||||||
CreationDate time.Time
|
CreationDate time.Time
|
||||||
|
|
||||||
|
// SettleDate is the exact time the invoice was settled.
|
||||||
|
SettleDate time.Time
|
||||||
|
|
||||||
// Terms are the contractual payment terms of the invoice. Once
|
// Terms are the contractual payment terms of the invoice. Once
|
||||||
// all the terms have been satisfied by the payer, then the invoice can
|
// all the terms have been satisfied by the payer, then the invoice can
|
||||||
// be considered fully fulfilled.
|
// be considered fully fulfilled.
|
||||||
@ -330,10 +333,20 @@ func serializeInvoice(w io.Writer, i *Invoice) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := wire.WriteVarBytes(w, 0, birthBytes); err != nil {
|
if err := wire.WriteVarBytes(w, 0, birthBytes); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
settleBytes, err := i.SettleDate.MarshalBinary()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := wire.WriteVarBytes(w, 0, settleBytes); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := w.Write(i.Terms.PaymentPreimage[:]); err != nil {
|
if _, err := w.Write(i.Terms.PaymentPreimage[:]); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -389,6 +402,14 @@ func deserializeInvoice(r io.Reader) (*Invoice, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
settledBytes, err := wire.ReadVarBytes(r, 0, 300, "settled")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := invoice.SettleDate.UnmarshalBinary(settledBytes); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := io.ReadFull(r, invoice.Terms.PaymentPreimage[:]); err != nil {
|
if _, err := io.ReadFull(r, invoice.Terms.PaymentPreimage[:]); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -412,13 +433,12 @@ func settleInvoice(invoices *bolt.Bucket, invoiceNum []byte) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
invoice.Terms.Settled = true
|
invoice.Terms.Settled = true
|
||||||
|
invoice.SettleDate = time.Now()
|
||||||
|
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
if err := serializeInvoice(&buf, invoice); err != nil {
|
if err := serializeInvoice(&buf, invoice); err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(roasbeef): add timestamp
|
|
||||||
|
|
||||||
return invoices.Put(invoiceNum[:], buf.Bytes())
|
return invoices.Put(invoiceNum[:], buf.Bytes())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user