Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chancloser+lnwire: add range-based negotiation #7062

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions channeldb/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ const (
// A tlv type definition used to serialize and deserialize the
// confirmed ShortChannelID for a zero-conf channel.
realScidType tlv.Type = 4

// sentShutdown field.
sentShutdownType tlv.Type = 5
)

// indexStatus is an enum-like type that describes what state the
Expand Down Expand Up @@ -822,6 +825,10 @@ type OpenChannel struct {
// default ShortChannelID. This is only set for zero-conf channels.
confirmedScid lnwire.ShortChannelID

// sentShutdown denotes whether or not we've sent the Shutdown message
// for this channel.
sentShutdown bool

// TODO(roasbeef): eww
Db *ChannelStateDB

Expand Down Expand Up @@ -1364,6 +1371,96 @@ func (c *OpenChannel) SecondCommitmentPoint() (*btcec.PublicKey, error) {
return input.ComputeCommitmentPoint(revocation[:]), nil
}

// MarkShutdownSent is used to mark that we've sent a Shutdown message for this
// channel. This is so we can retransmit Shutdown.
func (c *OpenChannel) MarkShutdownSent() error {
c.Lock()
defer c.Unlock()

if err := kvdb.Update(c.Db.backend, func(tx kvdb.RwTx) error {
chanBucket, err := fetchChanBucketRw(
tx, c.IdentityPub, &c.FundingOutpoint, c.ChainHash,
)
if err != nil {
return err
}

channel, err := fetchOpenChannel(
chanBucket, &c.FundingOutpoint,
)
if err != nil {
return err
}

channel.sentShutdown = true

return putOpenChannel(chanBucket, channel)
}, func() {}); err != nil {
return err
}

c.sentShutdown = true

return nil
}

// HasSentShutdown returns whether or not we've ever sent Shutdown for this
// channel. For nodes upgrading to 0.16.0, this will initially be false even if
// a Shutdown has been sent. This doesn't matter since this function is only
// used when restarting a ChannelLink and pre-0.16.0 nodes only sent Shutdown
// after the link was permanently stopped.
func (c *OpenChannel) HasSentShutdown() bool {
c.RLock()
defer c.RUnlock()

return c.sentShutdown
}

// PersistDeliveryScript is used during the cooperative close flow to persist
// a script sent in Shutdown when we did not set an upfront shutdown script
// during the funding flow.
func (c *OpenChannel) PersistDeliveryScript(
deliveryScript lnwire.DeliveryAddress) error {

c.Lock()
defer c.Unlock()

if err := kvdb.Update(c.Db.backend, func(tx kvdb.RwTx) error {
chanBucket, err := fetchChanBucketRw(
tx, c.IdentityPub, &c.FundingOutpoint, c.ChainHash,
)
if err != nil {
return err
}

channel, err := fetchOpenChannel(
chanBucket, &c.FundingOutpoint,
)
if err != nil {
return err
}

channel.LocalShutdownScript = deliveryScript

return putOpenChannel(chanBucket, channel)
}, func() {}); err != nil {
return err
}

c.LocalShutdownScript = deliveryScript

return nil
}

// GetLocalShutdownScript fetches the local shutdown script with the read
// mutex.
func (c *OpenChannel) GetLocalShutdownScript() lnwire.DeliveryAddress {
c.RLock()
defer c.RUnlock()

return c.LocalShutdownScript
}

// ChanSyncMsg returns the ChannelReestablish message that should be sent upon
// reconnection with the remote peer that we're maintaining this channel with.
// The information contained within this message is necessary to re-sync our
Expand Down Expand Up @@ -3691,6 +3788,12 @@ func putChanInfo(chanBucket kvdb.RwBucket, channel *OpenChannel) error {
localBalance := uint64(channel.InitialLocalBalance)
remoteBalance := uint64(channel.InitialRemoteBalance)

// Convert sentShutdown to a uint8.
var sentShutdownVal uint8
if channel.sentShutdown {
sentShutdownVal = 1
}

// Create the tlv stream.
tlvStream, err := tlv.NewStream(
// Write the RevocationKeyLocator as the first entry in a tlv
Expand All @@ -3705,6 +3808,9 @@ func putChanInfo(chanBucket kvdb.RwBucket, channel *OpenChannel) error {
initialRemoteBalanceType, &remoteBalance,
),
MakeScidRecord(realScidType, &channel.confirmedScid),
tlv.MakePrimitiveRecord(
sentShutdownType, &sentShutdownVal,
),
)
if err != nil {
return err
Expand Down Expand Up @@ -3906,6 +4012,8 @@ func fetchChanInfo(chanBucket kvdb.RBucket, channel *OpenChannel) error {
var (
localBalance uint64
remoteBalance uint64

sentShutdownVal uint8
)

// Create the tlv stream.
Expand All @@ -3922,6 +4030,9 @@ func fetchChanInfo(chanBucket kvdb.RBucket, channel *OpenChannel) error {
initialRemoteBalanceType, &remoteBalance,
),
MakeScidRecord(realScidType, &channel.confirmedScid),
tlv.MakePrimitiveRecord(
sentShutdownType, &sentShutdownVal,
),
)
if err != nil {
return err
Expand All @@ -3935,6 +4046,11 @@ func fetchChanInfo(chanBucket kvdb.RBucket, channel *OpenChannel) error {
channel.InitialLocalBalance = lnwire.MilliSatoshi(localBalance)
channel.InitialRemoteBalance = lnwire.MilliSatoshi(remoteBalance)

// Populate the sentShutdown field.
if sentShutdownVal == 1 {
channel.sentShutdown = true
}

channel.Packager = NewChannelPackager(channel.ShortChannelID)

// Finally, read the optional shutdown scripts.
Expand Down
7 changes: 3 additions & 4 deletions htlcswitch/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,9 @@ type ChannelUpdateHandler interface {
// parameter.
MayAddOutgoingHtlc(lnwire.MilliSatoshi) error

// ShutdownIfChannelClean shuts the link down if the channel state is
// clean. This can be used with dynamic commitment negotiation or coop
// close negotiation which require a clean channel state.
ShutdownIfChannelClean() error
// NotifyShouldShutdown is used by the Switch to inform the link that
// it should begin the shutdown flow.
NotifyShouldShutdown(req *ChanClose) error
}

// ChannelLink is an interface which represents the subsystem for managing the
Expand Down
Loading