clientdb: add self channel balance to bid order

With the additional data encoded as tlv, we can simply add the new
self channel balance field to the tlv stream.
This commit is contained in:
Oliver Gugger 2021-03-19 11:49:46 +01:00
parent cbacb85f45
commit b308f2ee35
No known key found for this signature in database
GPG key ID: 8E4256593F177720
2 changed files with 63 additions and 3 deletions

View file

@ -6,11 +6,19 @@ import (
"fmt"
"io"
"github.com/btcsuite/btcutil"
"github.com/lightninglabs/pool/event"
"github.com/lightninglabs/pool/order"
"github.com/lightningnetwork/lnd/tlv"
"go.etcd.io/bbolt"
)
const (
// bidSelfChanBalanceType is the tlv type we use to store the self
// channel balance on bid orders.
bidSelfChanBalanceType tlv.Type = 1
)
var (
// ErrNoOrder is the error returned if no order with the given nonce
// exists in the store.
@ -621,11 +629,62 @@ func DeserializeOrder(nonce order.Nonce, r io.Reader) (
// supplied reader by interpreting it as a tlv stream. If successful any
// non-default values of the additional data will be set on the given order.
func deserializeOrderTlvData(r io.Reader, o order.Order) error {
var (
selfChanBalance uint64
)
// We'll add records for all possible additional order data fields here
// but will check below which of them were actually set, depending on
// the order type as well.
tlvStream, err := tlv.NewStream(tlv.MakePrimitiveRecord(
bidSelfChanBalanceType, &selfChanBalance,
))
if err != nil {
return err
}
parsedTypes, err := tlvStream.DecodeWithParsedTypes(r)
if err != nil {
return err
}
// Now check what records were actually parsed from the stream and
// assign any parsed fields to our order.
switch castOrder := o.(type) {
case *order.Ask:
case *order.Bid:
if t, ok := parsedTypes[bidSelfChanBalanceType]; ok && t == nil {
castOrder.SelfChanBalance = btcutil.Amount(
selfChanBalance,
)
}
}
return nil
}
// serializeOrderTlvData encodes all additional data of an order as a single tlv
// stream.
func serializeOrderTlvData(w io.Writer, o order.Order) error {
return nil
var tlvRecords []tlv.Record
switch castOrder := o.(type) {
case *order.Ask:
case *order.Bid:
if castOrder.SelfChanBalance != 0 {
selfChanBalance := uint64(castOrder.SelfChanBalance)
tlvRecords = append(tlvRecords, tlv.MakePrimitiveRecord(
bidSelfChanBalanceType, &selfChanBalance,
))
}
}
tlvStream, err := tlv.NewStream(tlvRecords...)
if err != nil {
return err
}
return tlvStream.Encode(w)
}

View file

@ -23,8 +23,9 @@ func TestSubmitOrder(t *testing.T) {
// Store a dummy order and see if we can retrieve it again.
o := &order.Bid{
Kit: *dummyOrder(500000, 1337),
MinNodeTier: 2,
Kit: *dummyOrder(500000, 1337),
MinNodeTier: 2,
SelfChanBalance: 123,
}
o.Details().MinUnitsMatch = 10
err := store.SubmitOrder(o)