netann/channel_update: set timestamp via modifier

This commit is contained in:
Conner Fromknecht 2020-03-17 16:25:22 -07:00
parent f6c194c3cd
commit 5147a6d63d
No known key found for this signature in database
GPG Key ID: E7D737B67FA592C7
3 changed files with 21 additions and 15 deletions

@ -527,7 +527,7 @@ func (m *ChanStatusManager) signAndSendNextUpdate(outpoint wire.OutPoint,
err = SignChannelUpdate( err = SignChannelUpdate(
m.cfg.MessageSigner, m.cfg.OurPubKey, chanUpdate, m.cfg.MessageSigner, m.cfg.OurPubKey, chanUpdate,
ChannelUpdateSetDisable(disabled), ChanUpdSetDisable(disabled), ChanUpdSetTimestamp,
) )
if err != nil { if err != nil {
return err return err

@ -15,9 +15,9 @@ import (
// lnwire.ChannelUpdate. // lnwire.ChannelUpdate.
type ChannelUpdateModifier func(*lnwire.ChannelUpdate) type ChannelUpdateModifier func(*lnwire.ChannelUpdate)
// ChannelUpdateSetDisable sets the disabled channel flag if disabled is true, // ChanUpdSetDisable is a functional option that sets the disabled channel flag
// and clears the bit otherwise. // if disabled is true, and clears the bit otherwise.
func ChannelUpdateSetDisable(disabled bool) ChannelUpdateModifier { func ChanUpdSetDisable(disabled bool) ChannelUpdateModifier {
return func(update *lnwire.ChannelUpdate) { return func(update *lnwire.ChannelUpdate) {
if disabled { if disabled {
// Set the bit responsible for marking a channel as // Set the bit responsible for marking a channel as
@ -31,6 +31,20 @@ func ChannelUpdateSetDisable(disabled bool) ChannelUpdateModifier {
} }
} }
// ChanUpdSetTimestamp is a functional option that sets the timestamp of the
// update to the current time, or increments it if the timestamp is already in
// the future.
func ChanUpdSetTimestamp(update *lnwire.ChannelUpdate) {
newTimestamp := uint32(time.Now().Unix())
if newTimestamp <= update.Timestamp {
// Increment the prior value to ensure the timestamp
// monotonically increases, otherwise the update won't
// propagate.
newTimestamp = update.Timestamp + 1
}
update.Timestamp = newTimestamp
}
// SignChannelUpdate applies the given modifiers to the passed // SignChannelUpdate applies the given modifiers to the passed
// lnwire.ChannelUpdate, then signs the resulting update. The provided update // lnwire.ChannelUpdate, then signs the resulting update. The provided update
// should be the most recent, valid update, otherwise the timestamp may not // should be the most recent, valid update, otherwise the timestamp may not
@ -45,16 +59,6 @@ func SignChannelUpdate(signer lnwallet.MessageSigner, pubKey *btcec.PublicKey,
modifier(update) modifier(update)
} }
// Update the message's timestamp to the current time. If the update's
// current time is already in the future, we increment the prior value
// to ensure the timestamps monotonically increase, otherwise the
// update won't propagate.
newTimestamp := uint32(time.Now().Unix())
if newTimestamp <= update.Timestamp {
newTimestamp = update.Timestamp + 1
}
update.Timestamp = newTimestamp
// Create the DER-encoded ECDSA signature over the message digest. // Create the DER-encoded ECDSA signature over the message digest.
sig, err := SignAnnouncement(signer, pubKey, update) sig, err := SignAnnouncement(signer, pubKey, update)
if err != nil { if err != nil {

@ -103,6 +103,7 @@ func TestUpdateDisableFlag(t *testing.T) {
t.Parallel() t.Parallel()
for _, tc := range updateDisableTests { for _, tc := range updateDisableTests {
tc := tc
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
// Create the initial update, the only fields we are // Create the initial update, the only fields we are
// concerned with in this test are the timestamp and the // concerned with in this test are the timestamp and the
@ -127,7 +128,8 @@ func TestUpdateDisableFlag(t *testing.T) {
// disabled or enabled as prescribed in the test case. // disabled or enabled as prescribed in the test case.
err := netann.SignChannelUpdate( err := netann.SignChannelUpdate(
tc.signer, pubKey, newUpdate, tc.signer, pubKey, newUpdate,
netann.ChannelUpdateSetDisable(tc.disable), netann.ChanUpdSetDisable(tc.disable),
netann.ChanUpdSetTimestamp,
) )
var fail bool var fail bool