diff --git a/lnwire/channel_update.go b/lnwire/channel_update.go index 530bd3ab..02dfa5db 100644 --- a/lnwire/channel_update.go +++ b/lnwire/channel_update.go @@ -2,6 +2,7 @@ package lnwire import ( "bytes" + "fmt" "io" "io/ioutil" @@ -12,6 +13,11 @@ import ( // present in the ChannelUpdate. type ChanUpdateMsgFlags uint8 +// String returns the bitfield flags as a string. +func (c ChanUpdateMsgFlags) String() string { + return fmt.Sprintf("%08b", c) +} + // ChanUpdateChanFlags is a bitfield that signals various options concerning a // particular channel edge. Each bit is to be examined in order to determine // how the ChannelUpdate message is to be interpreted. @@ -29,6 +35,11 @@ const ( ChanUpdateDisabled ) +// String returns the bitfield flags as a string. +func (c ChanUpdateChanFlags) String() string { + return fmt.Sprintf("%08b", c) +} + // ChannelUpdate message is used after channel has been initially announced. // Each side independently announces its fees and minimum expiry for HTLCs and // other parameters. Also this message is used to redeclare initially set diff --git a/lnwire/lnwire_test.go b/lnwire/lnwire_test.go index d7c59193..91de511b 100644 --- a/lnwire/lnwire_test.go +++ b/lnwire/lnwire_test.go @@ -178,6 +178,50 @@ func randAddrs(r *rand.Rand) ([]net.Addr, error) { return []net.Addr{tcp4Addr, tcp6Addr, v2OnionAddr, v3OnionAddr}, nil } +// TestChanUpdateChanFlags ensures that converting the ChanUpdateChanFlags and +// ChanUpdateMsgFlags bitfields to a string behaves as expected. +func TestChanUpdateChanFlags(t *testing.T) { + t.Parallel() + + testCases := []struct { + flags uint8 + expected string + }{ + { + flags: 0, + expected: "00000000", + }, + { + flags: 1, + expected: "00000001", + }, + { + flags: 3, + expected: "00000011", + }, + { + flags: 255, + expected: "11111111", + }, + } + + for _, test := range testCases { + chanFlag := ChanUpdateChanFlags(test.flags) + toStr := chanFlag.String() + if toStr != test.expected { + t.Fatalf("expected %v, got %v", + test.expected, toStr) + } + + msgFlag := ChanUpdateMsgFlags(test.flags) + toStr = msgFlag.String() + if toStr != test.expected { + t.Fatalf("expected %v, got %v", + test.expected, toStr) + } + } +} + func TestMaxOutPointIndex(t *testing.T) { t.Parallel()