Skip to content
Open
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
45 changes: 44 additions & 1 deletion channeldb/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,47 @@ func assertRevocationLogEntryEqual(t *testing.T, c *ChannelCommitment,
}
}

// TestUpdateCommitmentRevocationAuxSigsRollback asserts that the revocation
// aux sigs held in memory always match what was persisted. They are set on
// the channel before the store writes them, so a write that fails must leave
// the previous value in place. Otherwise a retransmission would send a blob
// that is gone after a restart.
func TestUpdateCommitmentRevocationAuxSigsRollback(t *testing.T) {
t.Parallel()

fullDB, err := MakeTestDB(t)
require.NoError(t, err, "unable to make test database")

cdb := fullDB.ChannelStateDB()
channel := createTestChannel(t, cdb)

commitment := channel.LocalCommitment
commitment.CommitHeight++

// A successful update persists the sigs.
firstSigs := fn.Some(tlv.Blob{1, 2, 3})
_, err = channel.UpdateCommitment(&commitment, nil, firstSigs)
require.NoError(t, err)
require.Equal(t, firstSigs, channel.RevocationAuxSigs)

// Once the channel is borked, further updates are refused by the
// store. The sigs of the refused update must not stick in memory.
require.NoError(t, channel.MarkBorked())

commitment.CommitHeight++
_, err = channel.UpdateCommitment(
&commitment, nil, fn.Some(tlv.Blob{4, 5, 6}),
)
require.ErrorIs(t, err, ErrChanBorked)
require.Equal(t, firstSigs, channel.RevocationAuxSigs)

// What is on disk is the first blob as well.
openChannels, err := cdb.FetchOpenChannels(channel.IdentityPub)
require.NoError(t, err)
require.Len(t, openChannels, 1)
require.Equal(t, firstSigs, openChannels[0].RevocationAuxSigs)
}

func TestChannelStateTransition(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -736,7 +777,9 @@ func TestChannelStateTransition(t *testing.T) {
},
}

_, err = channel.UpdateCommitment(&commitment, unsignedAckedUpdates)
_, err = channel.UpdateCommitment(
&commitment, unsignedAckedUpdates, fn.None[tlv.Blob](),
)
require.NoError(t, err, "unable to update commitment")

// Assert that update is correctly written to the database.
Expand Down
4 changes: 3 additions & 1 deletion channeldb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import (
"github.com/btcsuite/btcd/btcutil/v2"
"github.com/btcsuite/btcd/chainhash/v2"
"github.com/btcsuite/btcd/wire/v2"
"github.com/lightningnetwork/lnd/fn/v2"
"github.com/lightningnetwork/lnd/graph/db/models"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/routing/route"
"github.com/lightningnetwork/lnd/shachain"
"github.com/lightningnetwork/lnd/tlv"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -382,7 +384,7 @@ func TestRestoreChannelShells(t *testing.T) {
// Ensure that it isn't possible to modify the commitment state machine
// of this restored channel.
channel := nodeChans[0]
_, err = channel.UpdateCommitment(nil, nil)
_, err = channel.UpdateCommitment(nil, nil, fn.None[tlv.Blob]())
if err != ErrNoRestoredChannelMutation {
t.Fatalf("able to mutate restored channel")
}
Expand Down
26 changes: 26 additions & 0 deletions chanstate/kv_open_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,13 @@ type openChannelTlvData struct {
// Note: if not set, it means either the channel has not been
// closed yet, or it was closed before this field was introduced.
closeConfirmationHeight tlv.OptionalRecordT[tlv.TlvType9, uint32]

// revocationAuxSigs is an optional blob carrying the revocation aux
// signatures attached to the most recently sent RevokeAndAck. It is
// only ever set for aux/custom (taproot asset) channels, so the same
// signatures can be re-attached if that RevokeAndAck has to be
// retransmitted on channel reestablish.
revocationAuxSigs tlv.OptionalRecordT[tlv.TlvType10, tlv.Blob]
}

// encode serializes the openChannelTlvData to the given io.Writer.
Expand Down Expand Up @@ -648,6 +655,12 @@ func (c *openChannelTlvData) encode(w io.Writer) error {
},
)

c.revocationAuxSigs.WhenSome(
func(sigs tlv.RecordT[tlv.TlvType10, tlv.Blob]) {
tlvRecords = append(tlvRecords, sigs.Record())
},
)

tlv.SortRecords(tlvRecords)

// Create the tlv stream.
Expand All @@ -665,6 +678,7 @@ func (c *openChannelTlvData) decode(r io.Reader) error {
tapscriptRoot := c.tapscriptRoot.Zero()
blob := c.customBlob.Zero()
closeConfHeight := c.closeConfirmationHeight.Zero()
revocationAuxSigs := c.revocationAuxSigs.Zero()

// Create the tlv stream.
tlvStream, err := tlv.NewStream(
Expand All @@ -677,6 +691,7 @@ func (c *openChannelTlvData) decode(r io.Reader) error {
blob.Record(),
c.confirmationHeight.Record(),
closeConfHeight.Record(),
revocationAuxSigs.Record(),
)
if err != nil {
return err
Expand All @@ -699,6 +714,9 @@ func (c *openChannelTlvData) decode(r io.Reader) error {
if _, ok := tlvs[closeConfHeight.TlvType()]; ok {
c.closeConfirmationHeight = tlv.SomeRecordT(closeConfHeight)
}
if _, ok := tlvs[revocationAuxSigs.TlvType()]; ok {
c.revocationAuxSigs = tlv.SomeRecordT(revocationAuxSigs)
}

return nil
}
Expand Down Expand Up @@ -748,6 +766,9 @@ func amendOpenChannelTlvData(channel *OpenChannel, auxData openChannelTlvData) {
auxData.closeConfirmationHeight.WhenSomeV(func(h uint32) {
channel.CloseConfirmationHeight = fn.Some(h)
})
auxData.revocationAuxSigs.WhenSomeV(func(sigs tlv.Blob) {
channel.RevocationAuxSigs = fn.Some(sigs)
})
}

// extractOpenChannelTlvData creates a new openChannelTlvData from the given
Expand Down Expand Up @@ -791,6 +812,11 @@ func extractOpenChannelTlvData(channel *OpenChannel) openChannelTlvData {
tlv.NewPrimitiveRecord[tlv.TlvType9](h),
)
})
channel.RevocationAuxSigs.WhenSome(func(sigs tlv.Blob) {
auxData.revocationAuxSigs = tlv.SomeRecordT(
tlv.NewPrimitiveRecord[tlv.TlvType10](sigs),
)
})

return auxData
}
26 changes: 25 additions & 1 deletion chanstate/open_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,15 @@ type OpenChannel struct {
// immutable.
CustomBlob fn.Option[tlv.Blob]

// RevocationAuxSigs is an optional blob carrying the revocation aux
// signatures attached to the most recently sent RevokeAndAck. It is
// only ever set for aux/custom (taproot asset) channels, and is
// persisted so the exact same signatures can be re-attached if that
// RevokeAndAck has to be retransmitted on channel reestablish. Only
// the latest revocation can ever be owed to the peer, so a single
// slot suffices; it is overwritten on every revocation.
RevocationAuxSigs fn.Option[tlv.Blob]

// Db persists channel state through the Store contract. This field
// intentionally keeps the existing name while callers still construct
// channels through the channeldb compatibility alias. The store
Expand Down Expand Up @@ -814,7 +823,8 @@ func (c *OpenChannel) SyncPending(addr net.Addr, pendingHeight uint32) error {
// commitment. Keys correspond to htlc indices and values indicate whether the
// htlc was settled or failed.
func (c *OpenChannel) UpdateCommitment(newCommitment *ChannelCommitment,
unsignedAckedUpdates []LogUpdate) (map[uint64]bool, error) {
unsignedAckedUpdates []LogUpdate,
revocationAuxSigs fn.Option[tlv.Blob]) (map[uint64]bool, error) {

c.Lock()
defer c.Unlock()
Expand All @@ -826,10 +836,24 @@ func (c *OpenChannel) UpdateCommitment(newCommitment *ChannelCommitment,
return nil, ErrNoRestoredChannelMutation
}

// Record the aux sigs attached to the RevokeAndAck this commitment
// update corresponds to (custom channels only, None otherwise), so
// they persist alongside it and survive for retransmission. This
// overwrites the previous revocation's sigs, which can no longer be
// owed to the peer. The store serializes them from the channel
// itself, so they have to be set before the write, and are rolled
// back if the write fails: the in-memory copy must never hold sigs
// that were not persisted, or a retransmission would send a blob
// that does not survive a restart.
prevRevocationAuxSigs := c.RevocationAuxSigs
c.RevocationAuxSigs = revocationAuxSigs

finalHtlcs, err := c.Db.UpdateChannelCommitment(
c, newCommitment, unsignedAckedUpdates,
)
if err != nil {
c.RevocationAuxSigs = prevRevocationAuxSigs

return nil, err
}

Expand Down
Loading
Loading