Skip to content

Commit 828486e

Browse files
committed
lnwire: fix unit test for DynCommit
1 parent 8456aef commit 828486e

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

lnwire/dyn_commit.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@ type DynCommit struct {
2525
ExtraData ExtraOpaqueData
2626
}
2727

28-
// A compile time check to ensure DynAck implements the lnwire.Message
28+
// A compile time check to ensure DynCommit implements the lnwire.Message
2929
// interface.
3030
var _ Message = (*DynCommit)(nil)
3131

32+
// A compile time check to ensure DynCommit implements the
33+
// lnwire.SizeableMessage interface.
34+
var _ SizeableMessage = (*DynCommit)(nil)
35+
3236
// Encode serializes the target DynAck into the passed io.Writer. Serialization
3337
// will observe the rules defined by the passed protocol version.
3438
//
@@ -133,3 +137,10 @@ func (dc *DynCommit) Decode(r io.Reader, _ uint32) error {
133137
func (dc *DynCommit) MsgType() MessageType {
134138
return MsgDynCommit
135139
}
140+
141+
// SerializedSize returns the serialized size of the message in bytes.
142+
//
143+
// This is part of the lnwire.SizeableMessage interface.
144+
func (dc *DynCommit) SerializedSize() (uint32, error) {
145+
return MessageSerializedSize(dc)
146+
}

lnwire/test_message.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,91 @@ func (dr *DynReject) RandTestMessage(t *rapid.T) Message {
909909
}
910910
}
911911

912+
// A compile time check to ensure DynCommit implements the lnwire.TestMessage
913+
// interface.
914+
var _ TestMessage = (*DynCommit)(nil)
915+
916+
// RandTestMessage populates the message with random data suitable for testing.
917+
// It uses the rapid testing framework to generate random values.
918+
//
919+
// This is part of the TestMessage interface.
920+
func (dc *DynCommit) RandTestMessage(t *rapid.T) Message {
921+
chanID := RandChannelID(t)
922+
923+
da := &DynAck{
924+
ChanID: chanID,
925+
}
926+
927+
dp := &DynPropose{
928+
ChanID: chanID,
929+
}
930+
931+
// Randomly decide which optional fields to include
932+
includeDustLimit := rapid.Bool().Draw(t, "includeDustLimit")
933+
includeMaxValueInFlight := rapid.Bool().Draw(
934+
t, "includeMaxValueInFlight",
935+
)
936+
includeChannelReserve := rapid.Bool().Draw(t, "includeChannelReserve")
937+
includeCsvDelay := rapid.Bool().Draw(t, "includeCsvDelay")
938+
includeMaxAcceptedHTLCs := rapid.Bool().Draw(
939+
t, "includeMaxAcceptedHTLCs",
940+
)
941+
includeChannelType := rapid.Bool().Draw(t, "includeChannelType")
942+
943+
// Generate random values for each included field
944+
if includeDustLimit {
945+
var rec tlv.RecordT[tlv.TlvType0, btcutil.Amount]
946+
val := btcutil.Amount(rapid.Uint32().Draw(t, "dustLimit"))
947+
rec.Val = val
948+
dp.DustLimit = tlv.SomeRecordT(rec)
949+
}
950+
951+
if includeMaxValueInFlight {
952+
var rec tlv.RecordT[tlv.TlvType2, MilliSatoshi]
953+
val := MilliSatoshi(rapid.Uint64().Draw(t, "maxValueInFlight"))
954+
rec.Val = val
955+
dp.MaxValueInFlight = tlv.SomeRecordT(rec)
956+
}
957+
958+
if includeChannelReserve {
959+
var rec tlv.RecordT[tlv.TlvType6, btcutil.Amount]
960+
val := btcutil.Amount(rapid.Uint32().Draw(t, "channelReserve"))
961+
rec.Val = val
962+
dp.ChannelReserve = tlv.SomeRecordT(rec)
963+
}
964+
965+
if includeCsvDelay {
966+
csvDelay := dp.CsvDelay.Zero()
967+
val := rapid.Uint16().Draw(t, "csvDelay")
968+
csvDelay.Val = val
969+
dp.CsvDelay = tlv.SomeRecordT(csvDelay)
970+
}
971+
972+
if includeMaxAcceptedHTLCs {
973+
maxHtlcs := dp.MaxAcceptedHTLCs.Zero()
974+
maxHtlcs.Val = rapid.Uint16().Draw(t, "maxAcceptedHTLCs")
975+
dp.MaxAcceptedHTLCs = tlv.SomeRecordT(maxHtlcs)
976+
}
977+
978+
if includeChannelType {
979+
chanType := dp.ChannelType.Zero()
980+
chanType.Val = *RandChannelType(t)
981+
dp.ChannelType = tlv.SomeRecordT(chanType)
982+
}
983+
984+
var extraData ExtraOpaqueData
985+
randData := RandExtraOpaqueData(t, nil)
986+
if len(randData) > 0 {
987+
extraData = randData
988+
}
989+
990+
return &DynCommit{
991+
DynPropose: *dp,
992+
DynAck: *da,
993+
ExtraData: extraData,
994+
}
995+
}
996+
912997
// A compile time check to ensure FundingCreated implements the TestMessage
913998
// interface.
914999
var _ TestMessage = (*FundingCreated)(nil)

0 commit comments

Comments
 (0)