Skip to content

Commit cfd0ef3

Browse files
authored
Merge pull request #1755 from lightninglabs/unit-test-coverage
multi: increase commitment v0/v1/v2 unit test coverage
2 parents 290c828 + 606e59d commit cfd0ef3

File tree

9 files changed

+566
-9
lines changed

9 files changed

+566
-9
lines changed

commitment/commitment_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package commitment
33
import (
44
"bytes"
55
"context"
6+
"crypto/sha256"
67
"encoding/hex"
78
"math/rand"
89
"testing"
@@ -1483,7 +1484,9 @@ func TestIsTaprootAssetCommitmentScript(t *testing.T) {
14831484
t.Parallel()
14841485

14851486
require.True(t, IsTaprootAssetCommitmentScript(testTapCommitmentScript))
1486-
require.False(t, IsTaprootAssetCommitmentScript(TaprootAssetsMarker[:]))
1487+
1488+
tag := sha256.Sum256(markerV2)
1489+
require.False(t, IsTaprootAssetCommitmentScript(tag[:]))
14871490
}
14881491

14891492
// TestAssetCommitmentNoWitness tests that an asset commitment of a v1 asset is

commitment/tap_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package commitment
2+
3+
import (
4+
"crypto/sha256"
5+
"testing"
6+
7+
"github.com/lightninglabs/taproot-assets/asset"
8+
"github.com/lightninglabs/taproot-assets/fn"
9+
"github.com/lightninglabs/taproot-assets/internal/test"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
var (
14+
// markerV2 is the marker tag for Taproot Assets used in V2 commitments.
15+
markerV2 = []byte(taprootAssetsMarkerTag + ":194243")
16+
)
17+
18+
// randTapCommitment generates a random Taproot commitment for the given
19+
// assetVersion.
20+
func randTapCommitment(t *testing.T, commitVersion *TapCommitmentVersion,
21+
assetVersion asset.Version) *TapCommitment {
22+
23+
t.Helper()
24+
randAssetType := test.RandFlip(asset.Normal, asset.Collectible)
25+
randGenesis := asset.RandGenesis(t, randAssetType)
26+
randAsset := randAsset(t, randGenesis, nil)
27+
28+
randAsset.Version = assetVersion
29+
30+
tapCommitment, err := FromAssets(commitVersion, randAsset)
31+
require.NoError(t, err)
32+
33+
return tapCommitment
34+
}
35+
36+
// TestTaprootAssetsMarkerV0 tests if it can find the TaprootAssetsMarker V0 at
37+
// the correct spot according to the legacy MarkerV0 digest with assetVersion 0
38+
// and 1.
39+
func TestTaprootAssetsMarkerV0(t *testing.T) {
40+
t.Parallel()
41+
42+
assetVersions := []asset.Version{asset.V0, asset.V1}
43+
for _, assetVersion := range assetVersions {
44+
// Create a random Taproot commitment, and extract the tapLeaf
45+
// script.
46+
randTapCommitment := randTapCommitment(t, nil, assetVersion)
47+
tapLeaf := randTapCommitment.TapLeaf()
48+
script := tapLeaf.Script
49+
50+
require.Equal(t, byte(assetVersion), script[0])
51+
require.Equal(t, TaprootAssetsMarker[:], script[1:33])
52+
}
53+
}
54+
55+
// TestTaprootAssetsMarkerV1 tests if it can find the TaprootAssetsMarker V2 at
56+
// the correct spot according to the MarkerV1 digest with assetVersion 0 and 1.
57+
func TestTaprootAssetsMarkerV1(t *testing.T) {
58+
t.Parallel()
59+
60+
assetVersions := []asset.Version{asset.V0, asset.V1}
61+
for _, assetVersion := range assetVersions {
62+
// Create a random Taproot commitment, and extract the tapLeaf
63+
// script.
64+
randTapCommitment := randTapCommitment(
65+
t, fn.Ptr(TapCommitmentV2), assetVersion,
66+
)
67+
68+
// Check if MarkerVersion is set to MarkerV2, which should be
69+
// the default.
70+
require.Equal(t, randTapCommitment.Version, TapCommitmentV2)
71+
72+
tapLeaf := randTapCommitment.TapLeaf()
73+
script := tapLeaf.Script
74+
75+
tag := sha256.Sum256(markerV2)
76+
require.Equal(t, tag[:], script[:32])
77+
require.Equal(t, byte(TapCommitmentV2), script[32])
78+
}
79+
}

commitment/taproot_test.go

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package commitment
22

33
import (
44
"bytes"
5+
"crypto/sha256"
56
"encoding/binary"
67
"encoding/hex"
78
"testing"
@@ -38,14 +39,26 @@ func TestTapscriptPreimage(t *testing.T) {
3839

3940
// Create a random byte slice with the same structure as a Taproot
4041
// Asset commitment root, that can be used in a TapLeaf.
41-
randTapCommitmentRoot := func(version asset.Version) []byte {
42+
43+
randTapCommitmentRoot := func(version asset.Version,
44+
legacyCommitment bool) []byte {
45+
4246
var dummyRootSum [8]byte
4347
binary.BigEndian.PutUint64(
4448
dummyRootSum[:], test.RandInt[uint64](),
4549
)
46-
dummyRootHashParts := [][]byte{
47-
{byte(version)}, TaprootAssetsMarker[:],
48-
fn.ByteSlice(test.RandHash()), dummyRootSum[:],
50+
var dummyRootHashParts [][]byte
51+
if legacyCommitment {
52+
dummyRootHashParts = [][]byte{
53+
{byte(version)}, TaprootAssetsMarker[:],
54+
fn.ByteSlice(test.RandHash()), dummyRootSum[:],
55+
}
56+
} else {
57+
tag := sha256.Sum256(markerV2)
58+
dummyRootHashParts = [][]byte{
59+
tag[:], {byte(version)},
60+
fn.ByteSlice(test.RandHash()), dummyRootSum[:],
61+
}
4962
}
5063
return bytes.Join(dummyRootHashParts, nil)
5164
}
@@ -115,7 +128,33 @@ func TestTapscriptPreimage(t *testing.T) {
115128
}, {
116129
name: "tap commitment leaf pre-image",
117130
makePreimage: func(t *testing.T) *TapscriptPreimage {
118-
tapCommitmentRoot := randTapCommitmentRoot(asset.V0)
131+
tapCommitmentRoot := randTapCommitmentRoot(
132+
asset.V0, false,
133+
)
134+
var encodedLeaf bytes.Buffer
135+
136+
_ = encodedLeaf.WriteByte(
137+
byte(txscript.BaseLeafVersion),
138+
)
139+
_ = wire.WriteVarBytes(
140+
&encodedLeaf, 0, tapCommitmentRoot,
141+
)
142+
143+
return &TapscriptPreimage{
144+
siblingType: LeafPreimage,
145+
siblingPreimage: encodedLeaf.Bytes(),
146+
}
147+
},
148+
expectedType: LeafPreimage,
149+
expectedName: "LeafPreimage",
150+
expectedEmpty: false,
151+
expectedHashErr: ErrPreimageIsTapCommitment.Error(),
152+
}, {
153+
name: "tap commitment leaf pre-image legacy commitment",
154+
makePreimage: func(t *testing.T) *TapscriptPreimage {
155+
tapCommitmentRoot := randTapCommitmentRoot(
156+
asset.V0, true,
157+
)
119158
var encodedLeaf bytes.Buffer
120159

121160
_ = encodedLeaf.WriteByte(

itest/multisig.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,9 @@ type publishAndLogTransferOptions struct {
501501

502502
// label is the label to use for the transfer.
503503
label string
504+
505+
// expectedErr is the expected error when calling PublishAndLogTransfer.
506+
expectedErr string
504507
}
505508

506509
// defaultPublishAndLogTransferOptions returns the default options for
@@ -525,6 +528,14 @@ func withLabel(label string) PublishAndLogTransferOption {
525528
}
526529
}
527530

531+
// withExpectedErr is an option for PublishAndLogTransfer that sets the
532+
// expected error.
533+
func withExpectedErr(expectedErr string) PublishAndLogTransferOption {
534+
return func(opts *publishAndLogTransferOptions) {
535+
opts.expectedErr = expectedErr
536+
}
537+
}
538+
528539
// PublishAndLogTransfer is a helper function that invokes the
529540
// PublishAndLogTransfer RPC endpoint on the specified tapd node. This endpoint
530541
// performs a pre-anchored transfer.
@@ -572,7 +583,15 @@ func PublishAndLogTransfer(t *testing.T, tapd commands.RpcClientsBundle,
572583
}
573584

574585
resp, err := tapd.PublishAndLogTransfer(ctxt, request)
575-
require.NoError(t, err)
586+
587+
if options.expectedErr != "" {
588+
require.Error(t, err)
589+
require.Contains(t, err.Error(), options.expectedErr,
590+
"unexpected error: %v", err)
591+
return nil
592+
} else {
593+
require.NoError(t, err)
594+
}
576595

577596
return resp
578597
}

0 commit comments

Comments
 (0)