Skip to content

Commit

Permalink
temp: remove boolean type
Browse files Browse the repository at this point in the history
  • Loading branch information
ellemouton committed Sep 2, 2024
1 parent fbeab29 commit 3cea90d
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 230 deletions.
9 changes: 7 additions & 2 deletions funding/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4562,13 +4562,18 @@ func (f *Manager) newTaprootChanAnnouncement(localPubKey,
chanUpdateAnn.ChainHash.Val = chainHash
chanUpdateAnn.ShortChannelID.Val = shortChanID
chanUpdateAnn.BlockHeight.Val = currentHeight
chanUpdateAnn.Direction.Val.B = direction == 1
chanUpdateAnn.CLTVExpiryDelta.Val = uint16(
f.cfg.DefaultRoutingPolicy.TimeLockDelta,
)
chanUpdateAnn.HTLCMinimumMsat.Val = fwdMinHTLC
chanUpdateAnn.HTLCMaximumMsat.Val = fwdMaxHTLC

if direction == 1 {
chanUpdateAnn.SecondPeer = tlv.SomeRecordT(
tlv.ZeroRecordT[tlv.TlvType8, lnwire.TrueBoolean](),
)
}

// The caller of newChanAnnouncement is expected to provide the initial
// forwarding policy to be announced. If no persisted initial policy
// values are found, then we will use the default policy values in the
Expand All @@ -4583,7 +4588,7 @@ func (f *Manager) newTaprootChanAnnouncement(localPubKey,
case ourPolicy != nil:
// If ourPolicy is non-nil, modify the default parameters of the
// ChannelUpdate.
chanUpdateAnn.Direction = ourPolicy.Direction
chanUpdateAnn.SecondPeer = ourPolicy.SecondPeer
chanUpdateAnn.CLTVExpiryDelta = ourPolicy.CLTVExpiryDelta
chanUpdateAnn.HTLCMinimumMsat = ourPolicy.HTLCMinimumMsat
chanUpdateAnn.HTLCMaximumMsat = ourPolicy.HTLCMaximumMsat
Expand Down
2 changes: 1 addition & 1 deletion lnrpc/routerrpc/router_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -1672,7 +1672,7 @@ func marshallChannelUpdate(update lnwire.ChannelUpdate) (*lnrpc.ChannelUpdate,
ChanId: upd.ShortChannelID.Val.ToUint64(),
BlockHeight: upd.BlockHeight.Val,
DisabledFlags: uint32(upd.DisabledFlags.Val),
Direction: upd.Direction.Val.B,
Direction: upd.SecondPeer.IsSome(),
TimeLockDelta: uint32(upd.CLTVExpiryDelta.Val),
BaseFee: upd.FeeBaseMsat.Val,
FeeRate: upd.FeeProportionalMillionths.Val,
Expand Down
80 changes: 0 additions & 80 deletions lnwire/boolean.go

This file was deleted.

133 changes: 0 additions & 133 deletions lnwire/boolean_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion lnwire/channel_announcement_2.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (
// ChannelAnnouncement2 message. This string will be used during the
// construction of the tagged hash message to be signed when producing
// the signature for the ChannelAnnouncement2 message.
chanAnn2SigFieldName = "announcement_signature"
chanAnn2SigFieldName = "signature"
)

// ChannelAnnouncement2 message is used to announce the existence of a taproot
Expand Down
61 changes: 50 additions & 11 deletions lnwire/channel_update_2.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const (
// ChannelUpdate2 message. This string will be used during the
// construction of the tagged hash message to be signed when producing
// the signature for the ChannelUpdate2 message.
chanUpdate2SigField = "bip340_sig"
chanUpdate2SigField = "signature"
)

// ChannelUpdate2 message is used after taproot channel has been initially
Expand Down Expand Up @@ -62,9 +62,10 @@ type ChannelUpdate2 struct {
// disabled.
DisabledFlags tlv.RecordT[tlv.TlvType6, ChanUpdateDisableFlags]

// Direction is false if this update was produced by node 1 of the
// channel announcement and true if it is from node 2.
Direction tlv.RecordT[tlv.TlvType8, Boolean]
// SecondPeer is used to indicate which node the channel node has
// created and signed this message. If this field is absent, it was
// node 2 otherwise it was node 1.
SecondPeer tlv.OptionalRecordT[tlv.TlvType8, TrueBoolean]

// CLTVExpiryDelta is the minimum number of blocks this node requires to
// be added to the expiry of HTLCs. This is a security parameter
Expand Down Expand Up @@ -116,10 +117,13 @@ func (c *ChannelUpdate2) DecodeTLVRecords(r io.Reader) error {
return err
}

var chainHash = tlv.ZeroRecordT[tlv.TlvType0, [32]byte]()
var (
chainHash = tlv.ZeroRecordT[tlv.TlvType0, [32]byte]()
secondPeer = tlv.ZeroRecordT[tlv.TlvType8, TrueBoolean]()
)
typeMap, err := tlvRecords.ExtractRecords(
&chainHash, &c.ShortChannelID, &c.BlockHeight, &c.DisabledFlags,
&c.Direction, &c.CLTVExpiryDelta, &c.HTLCMinimumMsat,
&secondPeer, &c.CLTVExpiryDelta, &c.HTLCMinimumMsat,
&c.HTLCMaximumMsat, &c.FeeBaseMsat,
&c.FeeProportionalMillionths,
)
Expand All @@ -133,6 +137,11 @@ func (c *ChannelUpdate2) DecodeTLVRecords(r io.Reader) error {
c.ChainHash.Val = chainHash.Val
}

// The presence of the second_peer tlv type indicates "true".
if _, ok := typeMap[c.SecondPeer.TlvType()]; ok {
c.SecondPeer = tlv.SomeRecordT(secondPeer)
}

// If the CLTV expiry delta was not encoded, then set it to the default
// value.
if _, ok := typeMap[c.CLTVExpiryDelta.TlvType()]; !ok {
Expand Down Expand Up @@ -221,10 +230,10 @@ func (c *ChannelUpdate2) DataToSign() ([]byte, error) {
recordProducers = append(recordProducers, &c.DisabledFlags)
}

// We only need to encode the direction if the direction is set to 1.
if c.Direction.Val.B {
recordProducers = append(recordProducers, &c.Direction)
}
// We only need to encode the second peer boolean if it is true
c.SecondPeer.WhenSome(func(r tlv.RecordT[tlv.TlvType8, TrueBoolean]) {
recordProducers = append(recordProducers, &r)
})

// We only encode the cltv expiry delta if it is not equal to the
// default.
Expand Down Expand Up @@ -284,7 +293,7 @@ func (c *ChannelUpdate2) SCID() ShortChannelID {
//
// NOTE: this is part of the ChannelUpdate interface.
func (c *ChannelUpdate2) IsNode1() bool {
return !c.Direction.Val.B
return c.SecondPeer.IsNone()
}

// IsDisabled is true if the update is announcing that the channel should be
Expand Down Expand Up @@ -475,3 +484,33 @@ func decodeDisableFlags(r io.Reader, val interface{}, buf *[8]byte,
return tlv.NewTypeForDecodingErr(val, "lnwire.ChanUpdateDisableFlags",
l, l)
}

// TrueBoolean is a record that indicates true or false using the presence of
// the record. If the record is absent, it indicates false. If it is presence,
// it indicates true.
type TrueBoolean struct{}

// Record returns the tlv record for the boolean entry.
func (b *TrueBoolean) Record() tlv.Record {
return tlv.MakeStaticRecord(
0, b, 0, booleanEncoder, booleanDecoder,
)
}

func booleanEncoder(_ io.Writer, val interface{}, _ *[8]byte) error {
if _, ok := val.(*TrueBoolean); ok {
return nil
}

return tlv.NewTypeForEncodingErr(val, "TrueBoolean")
}

func booleanDecoder(_ io.Reader, val interface{}, _ *[8]byte,
l uint64) error {

if _, ok := val.(*TrueBoolean); ok && (l == 0 || l == 1) {
return nil
}

return tlv.NewTypeForEncodingErr(val, "TrueBoolean")
}
4 changes: 3 additions & 1 deletion lnwire/lnwire_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1563,7 +1563,9 @@ func TestLightningWireProtocol(t *testing.T) {

// Alternate between the two direction possibilities.
if r.Int31()%2 == 0 {
req.Direction.Val.B = true
req.SecondPeer = tlv.SomeRecordT(
tlv.ZeroRecordT[tlv.TlvType8, TrueBoolean](), //nolint:lll
)
}

// Sometimes set the incoming disabled flag.
Expand Down
2 changes: 1 addition & 1 deletion netann/channel_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func unsignedChanPolicy2ToUpdate(chainHash chainhash.Hash,
ShortChannelID: policy.ShortChannelID,
BlockHeight: policy.BlockHeight,
DisabledFlags: policy.DisabledFlags,
Direction: policy.Direction,
SecondPeer: policy.SecondPeer,
CLTVExpiryDelta: policy.CLTVExpiryDelta,
HTLCMinimumMsat: policy.HTLCMinimumMsat,
HTLCMaximumMsat: policy.HTLCMaximumMsat,
Expand Down

0 comments on commit 3cea90d

Please sign in to comment.