forked from lightningnetwork/lnd
-
Notifications
You must be signed in to change notification settings - Fork 2
/
taproot_briefcase_test.go
146 lines (114 loc) · 3.32 KB
/
taproot_briefcase_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
package contractcourt
import (
"bytes"
"math/rand"
"testing"
"github.com/lightningnetwork/lnd/tlv"
"github.com/stretchr/testify/require"
"pgregory.net/rapid"
)
func randResolverCtrlBlocks(t *testing.T) resolverCtrlBlocks {
numBlocks := rand.Int() % 256
blocks := make(resolverCtrlBlocks, numBlocks)
for i := 0; i < numBlocks; i++ {
var id resolverID
_, err := rand.Read(id[:])
require.NoError(t, err)
var block [200]byte
_, err = rand.Read(block[:])
require.NoError(t, err)
blocks[id] = block[:]
}
return blocks
}
func randHtlcTweaks(t *testing.T) htlcTapTweaks {
numTweaks := rand.Int() % 256
// If the numTweaks happens to be zero, we return a nil to avoid
// initializing the map.
if numTweaks == 0 {
return nil
}
tweaks := make(htlcTapTweaks, numTweaks)
for i := 0; i < numTweaks; i++ {
var id resolverID
_, err := rand.Read(id[:])
require.NoError(t, err)
var tweak [32]byte
_, err = rand.Read(tweak[:])
require.NoError(t, err)
tweaks[id] = tweak
}
return tweaks
}
func randHtlcAuxBlobs(t *testing.T) htlcAuxBlobs {
numBlobs := rand.Int() % 256
blobs := make(htlcAuxBlobs, numBlobs)
for i := 0; i < numBlobs; i++ {
var id resolverID
_, err := rand.Read(id[:])
require.NoError(t, err)
var blob [100]byte
_, err = rand.Read(blob[:])
require.NoError(t, err)
blobs[id] = blob[:]
}
return blobs
}
// TestTaprootBriefcase tests the encode/decode methods of the taproot
// briefcase extension.
func TestTaprootBriefcase(t *testing.T) {
t.Parallel()
var sweepCtrlBlock [200]byte
_, err := rand.Read(sweepCtrlBlock[:])
require.NoError(t, err)
var revokeCtrlBlock [200]byte
_, err = rand.Read(revokeCtrlBlock[:])
require.NoError(t, err)
var anchorTweak [32]byte
_, err = rand.Read(anchorTweak[:])
require.NoError(t, err)
var commitBlob [100]byte
_, err = rand.Read(commitBlob[:])
require.NoError(t, err)
testCase := &taprootBriefcase{
CtrlBlocks: tlv.NewRecordT[tlv.TlvType0](ctrlBlocks{
CommitSweepCtrlBlock: sweepCtrlBlock[:],
RevokeSweepCtrlBlock: revokeCtrlBlock[:],
OutgoingHtlcCtrlBlocks: randResolverCtrlBlocks(t),
IncomingHtlcCtrlBlocks: randResolverCtrlBlocks(t),
SecondLevelCtrlBlocks: randResolverCtrlBlocks(t),
}),
TapTweaks: tlv.NewRecordT[tlv.TlvType1](tapTweaks{
AnchorTweak: anchorTweak[:],
BreachedHtlcTweaks: randHtlcTweaks(t),
BreachedSecondLevelHltcTweaks: randHtlcTweaks(t),
}),
SettledCommitBlob: tlv.SomeRecordT(
tlv.NewPrimitiveRecord[tlv.TlvType2](commitBlob[:]),
),
BreachedCommitBlob: tlv.SomeRecordT(
tlv.NewPrimitiveRecord[tlv.TlvType3](commitBlob[:]),
),
HtlcBlobs: tlv.SomeRecordT(
tlv.NewRecordT[tlv.TlvType4](randHtlcAuxBlobs(t)),
),
}
var b bytes.Buffer
require.NoError(t, testCase.Encode(&b))
var decodedCase taprootBriefcase
require.NoError(t, decodedCase.Decode(&b))
require.Equal(t, testCase, &decodedCase)
}
// TestHtlcAuxBlobEncodeDecode tests the encode/decode methods of the HTLC aux
// blobs.
func TestHtlcAuxBlobEncodeDecode(t *testing.T) {
t.Parallel()
rapid.Check(t, func(t *rapid.T) {
htlcBlobs := rapid.Make[htlcAuxBlobs]().Draw(t, "htlcAuxBlobs")
var b bytes.Buffer
require.NoError(t, htlcBlobs.Encode(&b))
decodedBlobs := newAuxHtlcBlobs()
require.NoError(t, decodedBlobs.Decode(&b))
require.Equal(t, htlcBlobs, decodedBlobs)
})
}