From cbacb85f4580bc5608ffd3fb42b5649dd02e30c4 Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Fri, 19 Mar 2021 11:49:45 +0100 Subject: [PATCH] order: add SelfChanBalance field to bid orders With this commit we add the new SelfChanBalance to the bid order. We also add a new version for orders that signals compatibility with that new field. A version increase on the ask side is needed to make sure the changes in the asker's account balance are interpreted correctly on batch validation. --- order/interface.go | 48 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/order/interface.go b/order/interface.go index 74d2bbb..6ab029e 100644 --- a/order/interface.go +++ b/order/interface.go @@ -11,6 +11,7 @@ import ( "github.com/btcsuite/btcutil" "github.com/lightninglabs/pool/account" "github.com/lightninglabs/pool/auctioneerrpc" + "github.com/lightninglabs/pool/sidecar" "github.com/lightninglabs/pool/terms" "github.com/lightningnetwork/lnd/keychain" "github.com/lightningnetwork/lnd/lntypes" @@ -43,6 +44,11 @@ const ( // to use lease durations outside of the default/legacy 2016 block // duration. VersionLeaseDurationBuckets Version = 2 + + // VersionSelfChanBalance is the order version that added use of the + // self channel balance field. Only orders with this version are allowed + // to use the self channel balance field. + VersionSelfChanBalance Version = 3 ) // Type is the type of an order. We don't use iota for the constants due to the @@ -363,7 +369,9 @@ func (a *Ask) Digest() ([sha256.Size]byte, error) { return result, err } - case VersionNodeTierMinMatch, VersionLeaseDurationBuckets: + case VersionNodeTierMinMatch, VersionLeaseDurationBuckets, + VersionSelfChanBalance: + err := lnwire.WriteElements( &msg, a.nonce[:], uint32(a.Version), a.FixedRate, a.Amt, a.LeaseDuration, uint64(a.MaxBatchFeeRate), @@ -507,6 +515,11 @@ type Bid struct { // matched with. Only Asks backed by nodes on this tier or above will // be matched with this bid. MinNodeTier NodeTier + + // SelfChanBalance is the initial outbound balance that should be added + // to the channel resulting from matching this bid by moving additional + // funds from the taker's account into the channel. + SelfChanBalance btcutil.Amount } // Type returns the order type. @@ -546,6 +559,17 @@ func (b *Bid) Digest() ([sha256.Size]byte, error) { return result, err } + case VersionSelfChanBalance: + err := lnwire.WriteElements( + &msg, b.nonce[:], uint32(b.Version), b.FixedRate, + b.Amt, b.LeaseDuration, uint64(b.MaxBatchFeeRate), + uint32(b.MinNodeTier), uint32(b.MinUnitsMatch), + uint64(b.SelfChanBalance), + ) + if err != nil { + return result, err + } + default: return result, fmt.Errorf("unknown version %d", b.Kit.Version) } @@ -568,6 +592,28 @@ func (b *Bid) ReservedValue(feeSchedule terms.FeeSchedule) btcutil.Amount { }) } +// ValidateSelfChanBalance makes sure that all conditions to use the +// SelfChanBalance field on a bid order are met. +func (b *Bid) ValidateSelfChanBalance() error { + if b.Version < VersionSelfChanBalance { + return fmt.Errorf("cannot use self chan balance with old " + + "order version") + } + + if err := sidecar.CheckOfferParams( + b.Amt, b.SelfChanBalance, BaseSupplyUnit, + ); err != nil { + return fmt.Errorf("invalid self chan balance: %v", err) + } + + if b.Units != b.MinUnitsMatch { + return fmt.Errorf("to use self chan balance the min units " + + "match must be equal to the order amount in units") + } + + return nil +} + // This is a compile time check to make certain that both Ask and Bid implement // the Order interface. var _ Order = (*Ask)(nil)