What changed, and why it matters
This commit only adds a new internal helper interface and two timestamp types for comparing channel and node update ordering values. It does not change any existing behavior, fix a bug, or alter how the program handles user input. There is no security issue visible in this change.
No action required. Review as normal code-quality change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch introduces a Timestamp interface in lnwire with two concrete implementations: UnixTimestamp (uint64, for v1 gossip) and BlockHeightTimestamp (uint32, for v2 gossip). Each implements IsZero() and Cmp(), and Cmp returns an error when comparing across the two concrete types. The commit includes unit tests. No existing code paths are modified, and no security-sensitive logic is present.
Changed components
lnwire/timestamp.golnwire/timestamp_test.goInspect captured patch +155 / −0
diff --git a/lnwire/timestamp.go b/lnwire/timestamp.go
new file mode 100644
index 0000000..0fe4db7
--- /dev/null
+++ b/lnwire/timestamp.go
@@ -0,0 +1,67 @@
+package lnwire
+
+import "fmt"
+
+// Timestamp is an interface for channel/node update ordering values. A
+// timestamp can represent either unix time (v1) or block height (v2).
+type Timestamp interface {
+ // IsZero returns true if the timestamp has no value.
+ IsZero() bool
+
+ // Cmp compares this timestamp to the passed timestamp. Implementations
+ // only support comparisons against the same concrete timestamp type.
+ Cmp(other Timestamp) (CompareResult, error)
+}
+
+// UnixTimestamp is a unix-time based update timestamp, used by v1 gossip
+// channels and nodes.
+type UnixTimestamp uint64
+
+// IsZero returns true if the timestamp has no value.
+func (u UnixTimestamp) IsZero() bool {
+ return u == 0
+}
+
+// Cmp compares this timestamp to another unix timestamp.
+func (u UnixTimestamp) Cmp(other Timestamp) (CompareResult, error) {
+ o, ok := other.(UnixTimestamp)
+ if !ok {
+ return 0, fmt.Errorf("expected UnixTimestamp, got: %T", other)
+ }
+
+ switch {
+ case u < o:
+ return LessThan, nil
+ case u > o:
+ return GreaterThan, nil
+ default:
+ return EqualTo, nil
+ }
+}
+
+// BlockHeightTimestamp is a block-height based update timestamp, used by v2
+// gossip channels and nodes.
+type BlockHeightTimestamp uint32
+
+// IsZero returns true if the timestamp has no value.
+func (b BlockHeightTimestamp) IsZero() bool {
+ return b == 0
+}
+
+// Cmp compares this timestamp to another block-height timestamp.
+func (b BlockHeightTimestamp) Cmp(other Timestamp) (CompareResult, error) {
+ o, ok := other.(BlockHeightTimestamp)
+ if !ok {
+ return 0, fmt.Errorf("expected BlockHeightTimestamp, got: %T",
+ other)
+ }
+
+ switch {
+ case b < o:
+ return LessThan, nil
+ case b > o:
+ return GreaterThan, nil
+ default:
+ return EqualTo, nil
+ }
+}
diff --git a/lnwire/timestamp_test.go b/lnwire/timestamp_test.go
new file mode 100644
index 0000000..01df3e6
--- /dev/null
+++ b/lnwire/timestamp_test.go
@@ -0,0 +1,88 @@
+package lnwire
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+// TestUnixTimestamp tests the IsZero and Cmp methods of UnixTimestamp.
+func TestUnixTimestamp(t *testing.T) {
+ t.Parallel()
+
+ t.Run("IsZero", func(t *testing.T) {
+ t.Parallel()
+
+ require.True(t, UnixTimestamp(0).IsZero())
+ require.False(t, UnixTimestamp(1).IsZero())
+ require.False(t, UnixTimestamp(1_000_000).IsZero())
+ })
+
+ t.Run("Cmp", func(t *testing.T) {
+ t.Parallel()
+
+ a := UnixTimestamp(100)
+ b := UnixTimestamp(200)
+
+ result, err := a.Cmp(b)
+ require.NoError(t, err)
+ require.Equal(t, LessThan, result)
+
+ result, err = b.Cmp(a)
+ require.NoError(t, err)
+ require.Equal(t, GreaterThan, result)
+
+ c := UnixTimestamp(100)
+ result, err = a.Cmp(c)
+ require.NoError(t, err)
+ require.Equal(t, EqualTo, result)
+ })
+
+ t.Run("Cmp wrong type", func(t *testing.T) {
+ t.Parallel()
+
+ _, err := UnixTimestamp(1).Cmp(BlockHeightTimestamp(1))
+ require.ErrorContains(t, err, "expected UnixTimestamp")
+ })
+}
+
+// TestBlockHeightTimestamp tests the IsZero and Cmp methods of
+// BlockHeightTimestamp.
+func TestBlockHeightTimestamp(t *testing.T) {
+ t.Parallel()
+
+ t.Run("IsZero", func(t *testing.T) {
+ t.Parallel()
+
+ require.True(t, BlockHeightTimestamp(0).IsZero())
+ require.False(t, BlockHeightTimestamp(1).IsZero())
+ require.False(t, BlockHeightTimestamp(800_000).IsZero())
+ })
+
+ t.Run("Cmp", func(t *testing.T) {
+ t.Parallel()
+
+ a := BlockHeightTimestamp(500)
+ b := BlockHeightTimestamp(800)
+
+ result, err := a.Cmp(b)
+ require.NoError(t, err)
+ require.Equal(t, LessThan, result)
+
+ result, err = b.Cmp(a)
+ require.NoError(t, err)
+ require.Equal(t, GreaterThan, result)
+
+ c := BlockHeightTimestamp(500)
+ result, err = a.Cmp(c)
+ require.NoError(t, err)
+ require.Equal(t, EqualTo, result)
+ })
+
+ t.Run("Cmp wrong type", func(t *testing.T) {
+ t.Parallel()
+
+ _, err := BlockHeightTimestamp(1).Cmp(UnixTimestamp(1))
+ require.ErrorContains(t, err, "expected BlockHeightTimestamp")
+ })
+}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.