From f38c6d66627ec5b2b180ae8689f4cad9ce8c3d30 Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Wed, 14 Jul 2021 16:30:19 -0700 Subject: [PATCH] channeldb: add new ChannelType bit for script enforced leased channels --- channeldb/channel.go | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/channeldb/channel.go b/channeldb/channel.go index 858cead91..2461efecb 100644 --- a/channeldb/channel.go +++ b/channeldb/channel.go @@ -257,6 +257,11 @@ const ( // ZeroHtlcTxFeeBit indicates that the channel should use zero-fee // second-level HTLC transactions. ZeroHtlcTxFeeBit ChannelType = 1 << 5 + + // LeaseExpirationBit indicates that the channel has been leased for a + // period of time, constraining every output that pays to the channel + // initiator with an additional CLTV of the lease maturity. + LeaseExpirationBit ChannelType = 1 << 6 ) // IsSingleFunder returns true if the channel type if one of the known single @@ -301,6 +306,11 @@ func (c ChannelType) IsFrozen() bool { return c&FrozenBit == FrozenBit } +// HasLeaseExpiration returns true if the channel originated from a lease. +func (c ChannelType) HasLeaseExpiration() bool { + return c&LeaseExpirationBit == LeaseExpirationBit +} + // ChannelConstraints represents a set of constraints meant to allow a node to // limit their exposure, enact flow control and ensure that all HTLCs are // economically relevant. This struct will be mirrored for both sides of the @@ -1394,7 +1404,7 @@ func putOpenChannel(chanBucket kvdb.RwBucket, channel *OpenChannel) error { // Next, if this is a frozen channel, we'll add in the axillary // information we need to store. - if channel.ChanType.IsFrozen() { + if channel.ChanType.IsFrozen() || channel.ChanType.HasLeaseExpiration() { err := storeThawHeight( chanBucket, channel.ThawHeight, ) @@ -1435,7 +1445,7 @@ func fetchOpenChannel(chanBucket kvdb.RBucket, // Next, if this is a frozen channel, we'll add in the axillary // information we need to store. - if channel.ChanType.IsFrozen() { + if channel.ChanType.IsFrozen() || channel.ChanType.HasLeaseExpiration() { thawHeight, err := fetchThawHeight(chanBucket) if err != nil { return nil, fmt.Errorf("unable to store thaw "+ @@ -2910,7 +2920,7 @@ func (c *OpenChannel) CloseChannel(summary *ChannelCloseSummary, // We'll also remove the channel from the frozen channel bucket // if we need to. - if c.ChanType.IsFrozen() { + if c.ChanType.IsFrozen() || c.ChanType.HasLeaseExpiration() { err := deleteThawHeight(chanBucket) if err != nil { return err @@ -3110,13 +3120,14 @@ func (c *OpenChannel) RemoteRevocationStore() (shachain.Store, error) { // channel is not frozen, then 0 is returned. func (c *OpenChannel) AbsoluteThawHeight() (uint32, error) { // Only frozen channels have a thaw height. - if !c.ChanType.IsFrozen() { + if !c.ChanType.IsFrozen() && !c.ChanType.HasLeaseExpiration() { return 0, nil } - // If the channel's thaw height is below the absolute threshold, then - // it's interpreted as a relative height to the chain's current height. - if c.ThawHeight < AbsoluteThawHeightThreshold { + // If the channel has the frozen bit set and it's thaw height is below + // the absolute threshold, then it's interpreted as a relative height to + // the chain's current height. + if c.ChanType.IsFrozen() && c.ThawHeight < AbsoluteThawHeightThreshold { // We'll only known of the channel's short ID once it's // confirmed. if c.IsPending { @@ -3125,6 +3136,7 @@ func (c *OpenChannel) AbsoluteThawHeight() (uint32, error) { } return c.ShortChannelID.BlockHeight + c.ThawHeight, nil } + return c.ThawHeight, nil }