Skip to content

Commit 7ec8b50

Browse files
committed
tappsbt: populate AltLeaves in tests
1 parent 4915ff5 commit 7ec8b50

File tree

4 files changed

+102
-60
lines changed

4 files changed

+102
-60
lines changed

tapchannelmsg/records_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ func TestContractResolution(t *testing.T) {
489489

490490
testPkts := make([]*tappsbt.VPacket, numPackets)
491491
for i := 0; i < numPackets; i++ {
492-
testPkts[i] = tappsbt.RandPacket(t, true)
492+
testPkts[i] = tappsbt.RandPacket(t, true, false)
493493
}
494494

495495
testRes := NewContractResolution(testPkts)

tappsbt/decode_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,18 @@ func TestEncodingDecoding(t *testing.T) {
146146
}, {
147147
name: "random packet",
148148
pkg: func(t *testing.T) *VPacket {
149-
return RandPacket(t, true)
149+
return RandPacket(t, true, true)
150150
},
151151
}, {
152152
name: "random packet with no explicit version",
153153
pkg: func(t *testing.T) *VPacket {
154-
return RandPacket(t, false)
154+
return RandPacket(t, false, true)
155155
},
156156
}, {
157157
name: "invalid packet version",
158158
pkg: func(t *testing.T) *VPacket {
159159
validVers := fn.NewSet(uint8(V0), uint8(V1))
160-
pkt := RandPacket(t, false)
160+
pkt := RandPacket(t, false, true)
161161

162162
invalidPktVersion := test.RandInt[uint8]()
163163
for validVers.Contains(invalidPktVersion) {

tappsbt/encode_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
func TestEncodeAsPsbt(t *testing.T) {
1212
t.Parallel()
1313

14-
pkg := RandPacket(t, test.RandBool())
14+
pkg := RandPacket(t, test.RandBool(), test.RandBool())
1515
packet, err := pkg.EncodeAsPsbt()
1616
require.NoError(t, err)
1717

tappsbt/mock.go

Lines changed: 97 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,37 @@ var (
3737
)
3838
)
3939

40+
// RandAltLeaf generates a random Asset that is a valid AltLeaf.
41+
func RandAltLeaf(t testing.TB) *asset.Asset {
42+
randWitness := []asset.Witness{
43+
{TxWitness: test.RandTxWitnesses(t)},
44+
}
45+
randKey := asset.RandScriptKey(t)
46+
randVersion := asset.ScriptVersion(test.RandInt[uint16]())
47+
randLeaf, err := asset.NewAltLeaf(randKey, randVersion, randWitness)
48+
require.NoError(t, err)
49+
50+
require.NoError(t, randLeaf.ValidateAltLeaf())
51+
52+
return randLeaf
53+
}
54+
55+
// RandAltLeaves generates a set of random number of random alt leaves.
56+
func RandAltLeaves(t testing.TB) []AltLeafAsset {
57+
// Limit the number of leaves to keep the test vectors small.
58+
maxLeaves := int32(4)
59+
numLeaves := test.RandInt31n(maxLeaves)
60+
if numLeaves == 0 {
61+
return nil
62+
}
63+
64+
altLeaves := make([]AltLeafAsset, 0, numLeaves)
65+
for range numLeaves {
66+
altLeaves = append(altLeaves, AltLeafAsset(RandAltLeaf(t)))
67+
}
68+
69+
return altLeaves
70+
}
4071

4172
func RandAssetForPacket(t testing.TB, assetType asset.Type,
4273
desc keychain.KeyDescriptor) *asset.Asset {
@@ -54,7 +85,7 @@ func RandAssetForPacket(t testing.TB, assetType asset.Type,
5485
}
5586

5687
// RandPacket generates a random virtual packet for testing purposes.
57-
func RandPacket(t testing.TB, setVersion bool) *VPacket {
88+
func RandPacket(t testing.TB, setVersion, altLeaves bool) *VPacket {
5889
testPubKey := test.RandPubKey(t)
5990
op := test.RandOp(t)
6091
keyDesc := keychain.KeyDescriptor{
@@ -117,61 +148,72 @@ func RandPacket(t testing.TB, setVersion bool) *VPacket {
117148
courierAddress, err := url.Parse("https://example.com")
118149
require.NoError(t, err)
119150

151+
randVInput := VInput{
152+
PrevID: asset.PrevID{
153+
OutPoint: op,
154+
ID: asset.RandID(t),
155+
ScriptKey: asset.RandSerializedKey(t),
156+
},
157+
Anchor: Anchor{
158+
Value: 777,
159+
PkScript: []byte("anchor pkscript"),
160+
SigHashType: txscript.SigHashSingle,
161+
InternalKey: testPubKey,
162+
MerkleRoot: []byte("merkle root"),
163+
TapscriptSibling: []byte("sibling"),
164+
Bip32Derivation: bip32Derivations,
165+
TrBip32Derivation: trBip32Derivations,
166+
},
167+
Proof: &inputProof,
168+
}
169+
170+
randVOutput1 := VOutput{
171+
Amount: 123,
172+
AssetVersion: asset.Version(
173+
test.RandIntn(2),
174+
),
175+
Type: TypeSplitRoot,
176+
Interactive: true,
177+
AnchorOutputIndex: 0,
178+
AnchorOutputInternalKey: testPubKey,
179+
AnchorOutputBip32Derivation: bip32Derivations,
180+
AnchorOutputTaprootBip32Derivation: trBip32Derivations,
181+
Asset: testOutputAsset,
182+
ScriptKey: testOutputAsset.ScriptKey,
183+
SplitAsset: testOutputAsset,
184+
AnchorOutputTapscriptSibling: testPreimage1,
185+
ProofDeliveryAddress: courierAddress,
186+
ProofSuffix: &inputProof,
187+
RelativeLockTime: 345,
188+
LockTime: 456,
189+
}
190+
191+
randVOutput2 := VOutput{
192+
Amount: 345,
193+
AssetVersion: asset.Version(
194+
test.RandIntn(2),
195+
),
196+
Type: TypeSplitRoot,
197+
Interactive: false,
198+
AnchorOutputIndex: 1,
199+
AnchorOutputInternalKey: testPubKey,
200+
AnchorOutputBip32Derivation: bip32Derivations,
201+
AnchorOutputTaprootBip32Derivation: trBip32Derivations,
202+
Asset: testOutputAsset,
203+
ScriptKey: testOutputAsset.ScriptKey,
204+
AnchorOutputTapscriptSibling: &testPreimage2,
205+
}
206+
207+
if altLeaves {
208+
randVInput.AltLeaves = RandAltLeaves(t)
209+
randVOutput1.AltLeaves = RandAltLeaves(t)
210+
randVOutput2.AltLeaves = RandAltLeaves(t)
211+
}
212+
120213
vPacket := &VPacket{
121-
Inputs: []*VInput{{
122-
PrevID: asset.PrevID{
123-
OutPoint: op,
124-
ID: asset.RandID(t),
125-
ScriptKey: asset.RandSerializedKey(t),
126-
},
127-
Anchor: Anchor{
128-
Value: 777,
129-
PkScript: []byte("anchor pkscript"),
130-
SigHashType: txscript.SigHashSingle,
131-
InternalKey: testPubKey,
132-
MerkleRoot: []byte("merkle root"),
133-
TapscriptSibling: []byte("sibling"),
134-
Bip32Derivation: bip32Derivations,
135-
TrBip32Derivation: trBip32Derivations,
136-
},
137-
Proof: &inputProof,
138-
}, {
139-
// Empty input.
140-
}},
141-
Outputs: []*VOutput{{
142-
Amount: 123,
143-
AssetVersion: asset.Version(
144-
test.RandIntn(2),
145-
),
146-
Type: TypeSplitRoot,
147-
Interactive: true,
148-
AnchorOutputIndex: 0,
149-
AnchorOutputInternalKey: testPubKey,
150-
AnchorOutputBip32Derivation: bip32Derivations,
151-
AnchorOutputTaprootBip32Derivation: trBip32Derivations,
152-
Asset: testOutputAsset,
153-
ScriptKey: testOutputAsset.ScriptKey,
154-
SplitAsset: testOutputAsset,
155-
AnchorOutputTapscriptSibling: testPreimage1,
156-
ProofDeliveryAddress: courierAddress,
157-
ProofSuffix: &inputProof,
158-
RelativeLockTime: 345,
159-
LockTime: 456,
160-
}, {
161-
Amount: 345,
162-
AssetVersion: asset.Version(
163-
test.RandIntn(2),
164-
),
165-
Type: TypeSplitRoot,
166-
Interactive: false,
167-
AnchorOutputIndex: 1,
168-
AnchorOutputInternalKey: testPubKey,
169-
AnchorOutputBip32Derivation: bip32Derivations,
170-
AnchorOutputTaprootBip32Derivation: trBip32Derivations,
171-
Asset: testOutputAsset,
172-
ScriptKey: testOutputAsset.ScriptKey,
173-
AnchorOutputTapscriptSibling: &testPreimage2,
174-
}},
214+
// Empty input.
215+
Inputs: []*VInput{&randVInput, {}},
216+
Outputs: []*VOutput{&randVOutput1, &randVOutput2},
175217
ChainParams: testParams,
176218
}
177219
vPacket.SetInputAsset(0, testAsset)

0 commit comments

Comments
 (0)