-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathcommit_sig_test.go
168 lines (141 loc) · 4.15 KB
/
commit_sig_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package lnwire
import (
"bytes"
"fmt"
"testing"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr/musig2"
"github.com/lightningnetwork/lnd/tlv"
"github.com/stretchr/testify/require"
)
// testCase is a test case for the CommitSig message.
type commitSigTestCase struct {
// Msg is the message to be encoded and decoded.
Msg CommitSig
// ExpectEncodeError is a flag that indicates whether we expect the
// encoding of the message to fail.
ExpectEncodeError bool
}
// generateCommitSigTestCases generates a set of CommitSig message test cases.
func generateCommitSigTestCases(t *testing.T) []commitSigTestCase {
// Firstly, we'll set basic values for the message fields.
//
// Generate random channel ID.
chanIDBytes, err := generateRandomBytes(32)
require.NoError(t, err)
var chanID ChannelID
copy(chanID[:], chanIDBytes)
// Generate random commit sig.
commitSigBytes, err := generateRandomBytes(64)
require.NoError(t, err)
sig, err := NewSigFromSchnorrRawSignature(commitSigBytes)
require.NoError(t, err)
sigScalar := new(btcec.ModNScalar)
sigScalar.SetByteSlice(sig.RawBytes())
var nonce [musig2.PubNonceSize]byte
copy(nonce[:], commitSigBytes)
sigWithNonce := NewPartialSigWithNonce(nonce, *sigScalar)
partialSig := MaybePartialSigWithNonce(sigWithNonce)
// Define custom records.
recordKey1 := uint64(MinCustomRecordsTlvType + 1)
recordValue1, err := generateRandomBytes(10)
require.NoError(t, err)
recordKey2 := uint64(MinCustomRecordsTlvType + 2)
recordValue2, err := generateRandomBytes(10)
require.NoError(t, err)
customRecords := CustomRecords{
recordKey1: recordValue1,
recordKey2: recordValue2,
}
// Construct an instance of extra data that contains records with TLV
// types below the minimum custom records threshold and that lack
// corresponding fields in the message struct. Content should persist in
// the extra data field after encoding and decoding.
var (
recordBytes45 = []byte("recordBytes45")
tlvRecord45 = tlv.NewPrimitiveRecord[tlv.TlvType45](
recordBytes45,
)
recordBytes55 = []byte("recordBytes55")
tlvRecord55 = tlv.NewPrimitiveRecord[tlv.TlvType55](
recordBytes55,
)
)
var extraData ExtraOpaqueData
err = extraData.PackRecords(
[]tlv.RecordProducer{&tlvRecord45, &tlvRecord55}...,
)
require.NoError(t, err)
invalidCustomRecords := CustomRecords{
MinCustomRecordsTlvType - 1: recordValue1,
}
return []commitSigTestCase{
{
Msg: CommitSig{
ChanID: chanID,
CommitSig: sig,
PartialSig: partialSig,
CustomRecords: customRecords,
ExtraData: extraData,
},
},
// Add a test case where the blinding point field is not
// populated.
{
Msg: CommitSig{
ChanID: chanID,
CommitSig: sig,
CustomRecords: customRecords,
},
},
// Add a test case where the custom records field is not
// populated.
{
Msg: CommitSig{
ChanID: chanID,
CommitSig: sig,
PartialSig: partialSig,
},
},
// Add a case where the custom records are invalid.
{
Msg: CommitSig{
ChanID: chanID,
CommitSig: sig,
PartialSig: partialSig,
CustomRecords: invalidCustomRecords,
},
ExpectEncodeError: true,
},
}
}
// TestCommitSigEncodeDecode tests CommitSig message encoding and decoding for
// all supported field values.
func TestCommitSigEncodeDecode(t *testing.T) {
t.Parallel()
// Generate test cases.
testCases := generateCommitSigTestCases(t)
// Execute test cases.
for tcIdx, tc := range testCases {
t.Run(fmt.Sprintf("testcase-%d", tcIdx), func(t *testing.T) {
// Encode test case message.
var buf bytes.Buffer
err := tc.Msg.Encode(&buf, 0)
// Check if we expect an encoding error.
if tc.ExpectEncodeError {
require.Error(t, err)
return
}
require.NoError(t, err)
// Decode the encoded message bytes message.
var actualMsg CommitSig
decodeReader := bytes.NewReader(buf.Bytes())
err = actualMsg.Decode(decodeReader, 0)
require.NoError(t, err)
// The signature type isn't serialized.
actualMsg.CommitSig.ForceSchnorr()
// Compare the two messages to ensure equality.
require.Equal(t, tc.Msg, actualMsg)
})
}
}