Skip to content

Commit f3ec01d

Browse files
committed
multi: fix manual grouped asset creation in tests
In this commit, we update multiple unit tests to not set an asset group key directly, and instead use some test helper that calls asset.New(). This ensures that the group witness is copied into the asset. The only time setting the group key directly is acceptable is before a group witness has been created, or in a minting request.
1 parent d763d68 commit f3ec01d

File tree

7 files changed

+75
-58
lines changed

7 files changed

+75
-58
lines changed

asset/asset.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,9 +1208,10 @@ func (a *Asset) Copy() *Asset {
12081208

12091209
if a.GroupKey != nil {
12101210
assetCopy.GroupKey = &GroupKey{
1211-
RawKey: a.GroupKey.RawKey,
1212-
GroupPubKey: a.GroupKey.GroupPubKey,
1213-
Witness: a.GroupKey.Witness,
1211+
RawKey: a.GroupKey.RawKey,
1212+
GroupPubKey: a.GroupKey.GroupPubKey,
1213+
TapscriptRoot: a.GroupKey.TapscriptRoot,
1214+
Witness: a.GroupKey.Witness,
12141215
}
12151216
}
12161217

asset/mock.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func (m *MockGroupTxBuilder) BuildGenesisTx(newAsset *Asset) (*wire.MsgTx,
175175
// First, we check that the passed asset is a genesis grouped asset
176176
// that has no group witness.
177177
if !newAsset.NeedsGenesisWitnessForGroup() {
178-
return nil, nil, fmt.Errorf("asset is not a genesis grouped" +
178+
return nil, nil, fmt.Errorf("asset is not a genesis grouped " +
179179
"asset")
180180
}
181181

commitment/commitment_test.go

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -72,39 +72,44 @@ func TestNewAssetCommitment(t *testing.T) {
7272
t.Parallel()
7373

7474
genesis1 := asset.RandGenesis(t, asset.Normal)
75+
genesis2 := asset.RandGenesis(t, asset.Normal)
7576
genesis1Collectible := asset.RandGenesis(t, asset.Collectible)
76-
genesis1CollectibleScriptKey := asset.RandScriptKey(t)
7777
genesis1CollectibleProtoAsset := asset.NewAssetNoErr(
78-
t, genesis1Collectible, 1, 0, 0, genesis1CollectibleScriptKey,
79-
nil,
80-
)
81-
genesis2 := asset.RandGenesis(t, asset.Normal)
82-
genesis2ProtoAsset := asset.RandAssetWithValues(
83-
t, genesis2, nil, asset.RandScriptKey(t),
78+
t, genesis1Collectible, 1, 0, 0, asset.RandScriptKey(t), nil,
8479
)
8580
group1Anchor := randAsset(t, genesis1, nil)
8681
groupKey1, group1PrivBytes := asset.RandGroupKeyWithSigner(
8782
t, genesis1, group1Anchor,
8883
)
89-
group1Priv, group1Pub := btcec.PrivKeyFromBytes(group1PrivBytes)
90-
group1Anchor.GroupKey = groupKey1
84+
group1Anchor = asset.NewAssetNoErr(
85+
t, genesis1, group1Anchor.Amount, group1Anchor.LockTime,
86+
group1Anchor.RelativeLockTime, group1Anchor.ScriptKey,
87+
groupKey1,
88+
)
9189
groupKey1Collectible := asset.RandGroupKey(
9290
t, genesis1Collectible, genesis1CollectibleProtoAsset,
9391
)
92+
genesis2ProtoAsset := randAsset(t, genesis2, nil)
9493
groupKey2 := asset.RandGroupKey(t, genesis2, genesis2ProtoAsset)
9594
copyOfGroupKey1Collectible := &asset.GroupKey{
96-
RawKey: groupKey1Collectible.RawKey,
97-
GroupPubKey: groupKey1Collectible.GroupPubKey,
98-
Witness: groupKey1Collectible.Witness,
95+
RawKey: groupKey1Collectible.RawKey,
96+
GroupPubKey: groupKey1Collectible.GroupPubKey,
97+
TapscriptRoot: groupKey1Collectible.TapscriptRoot,
98+
Witness: groupKey1Collectible.Witness,
9999
}
100100
group1Reissued := randAsset(t, genesis2, nil)
101101
genTxBuilder := asset.MockGroupTxBuilder{}
102+
group1Priv, group1Pub := btcec.PrivKeyFromBytes(group1PrivBytes)
102103
group1ReissuedGroupKey, err := asset.DeriveGroupKey(
103104
asset.NewMockGenesisSigner(group1Priv), &genTxBuilder,
104105
test.PubToKeyDesc(group1Pub), genesis1, genesis2ProtoAsset,
105106
)
106107
require.NoError(t, err)
107-
group1Reissued.GroupKey = group1ReissuedGroupKey
108+
group1Reissued = asset.NewAssetNoErr(
109+
t, genesis2, group1Reissued.Amount, group1Reissued.LockTime,
110+
group1Reissued.RelativeLockTime, group1Reissued.ScriptKey,
111+
group1ReissuedGroupKey,
112+
)
108113

109114
testCases := []struct {
110115
name string
@@ -503,10 +508,6 @@ func TestSplitCommitment(t *testing.T) {
503508
groupKeyCollectible := asset.RandGroupKey(
504509
t, genesisCollectible, collectibleProtoAsset,
505510
)
506-
normalInputAsset := normalProtoAsset.Copy()
507-
normalInputAsset.GroupKey = groupKeyNormal
508-
collectibleInputAsset := collectibleProtoAsset.Copy()
509-
collectibleInputAsset.GroupKey = groupKeyCollectible
510511

511512
testCases := []struct {
512513
name string
@@ -898,7 +899,10 @@ func TestTapCommitmentKeyPopulation(t *testing.T) {
898899
var groupKey *asset.GroupKey
899900
if assetDesc.HasGroupKey {
900901
groupKey = asset.RandGroupKey(t, genesis, a)
901-
a.GroupKey = groupKey
902+
a = asset.NewAssetNoErr(
903+
t, genesis, a.Amount, a.LockTime,
904+
a.RelativeLockTime, a.ScriptKey, groupKey,
905+
)
902906
}
903907

904908
commitment, err := NewAssetCommitment(a)
@@ -932,18 +936,26 @@ func TestUpdateAssetCommitment(t *testing.T) {
932936
groupKey1, group1PrivBytes := asset.RandGroupKeyWithSigner(
933937
t, genesis1, group1Anchor,
934938
)
935-
group1Priv, group1Pub := btcec.PrivKeyFromBytes(group1PrivBytes)
936-
group1Anchor.GroupKey = groupKey1
939+
group1Anchor = asset.NewAssetNoErr(
940+
t, genesis1, group1Anchor.Amount, group1Anchor.LockTime,
941+
group1Anchor.RelativeLockTime, group1Anchor.ScriptKey,
942+
groupKey1,
943+
)
937944
group2Anchor := randAsset(t, genesis2, nil)
938945
groupKey2 := asset.RandGroupKey(t, genesis2, group2Anchor)
939-
group1Reissued := group2Anchor.Copy()
946+
group1Reissued := randAsset(t, genesis2, nil)
940947
genTxBuilder := asset.MockGroupTxBuilder{}
948+
group1Priv, group1Pub := btcec.PrivKeyFromBytes(group1PrivBytes)
941949
group1ReissuedGroupKey, err := asset.DeriveGroupKey(
942950
asset.NewMockGenesisSigner(group1Priv), &genTxBuilder,
943951
test.PubToKeyDesc(group1Pub), genesis1, group1Reissued,
944952
)
945953
require.NoError(t, err)
946-
group1Reissued.GroupKey = group1ReissuedGroupKey
954+
group1Reissued = asset.NewAssetNoErr(
955+
t, genesis2, group1Reissued.Amount, group1Reissued.LockTime,
956+
group1Reissued.RelativeLockTime, group1Reissued.ScriptKey,
957+
group1ReissuedGroupKey,
958+
)
947959

948960
assetNoGroup := randAsset(t, genesis2, nil)
949961
copyOfAssetNoGroup := assetNoGroup.Copy()
@@ -990,19 +1002,6 @@ func TestUpdateAssetCommitment(t *testing.T) {
9901002
{
9911003
name: "insertion of asset with group key",
9921004
f: func() (*asset.Asset, error) {
993-
group1Reissued := randAsset(t, genesis2, nil)
994-
genTxBuilder := asset.MockGroupTxBuilder{}
995-
gen2ProtoAsset := asset.RandAssetWithValues(
996-
t, genesis2, nil, asset.RandScriptKey(t),
997-
)
998-
group1ReissuedGroupKey, err := asset.DeriveGroupKey(
999-
asset.NewMockGenesisSigner(group1Priv),
1000-
&genTxBuilder,
1001-
test.PubToKeyDesc(group1Priv.PubKey()),
1002-
genesis1, gen2ProtoAsset,
1003-
)
1004-
require.NoError(t, err)
1005-
group1Reissued.GroupKey = group1ReissuedGroupKey
10061005
return group1Reissued,
10071006
groupAssetCommitment.Upsert(group1Reissued)
10081007
},
@@ -1088,11 +1087,15 @@ func TestUpdateTapCommitment(t *testing.T) {
10881087
genesis3 := asset.RandGenesis(t, asset.Normal)
10891088
asset3 := randAsset(t, genesis3, groupKey1)
10901089

1091-
asset1 := protoAsset1.Copy()
1092-
asset1.GroupKey = groupKey1
1090+
asset1 := asset.NewAssetNoErr(
1091+
t, genesis1, protoAsset1.Amount, protoAsset1.LockTime,
1092+
protoAsset1.RelativeLockTime, protoAsset1.ScriptKey, groupKey1,
1093+
)
10931094

1094-
asset2 := protoAsset2.Copy()
1095-
asset2.GroupKey = groupKey2
1095+
asset2 := asset.NewAssetNoErr(
1096+
t, genesis2, protoAsset2.Amount, protoAsset2.LockTime,
1097+
protoAsset2.RelativeLockTime, protoAsset2.ScriptKey, groupKey2,
1098+
)
10961099

10971100
assetCommitment1, err := NewAssetCommitment(asset1)
10981101
require.NoError(t, err)
@@ -1260,14 +1263,18 @@ func TestTapCommitmentDeepCopy(t *testing.T) {
12601263
genesis1 := asset.RandGenesis(t, asset.Normal)
12611264
protoAsset1 := randAsset(t, genesis1, nil)
12621265
groupKey1 := asset.RandGroupKey(t, genesis1, protoAsset1)
1263-
asset1 := protoAsset1.Copy()
1264-
asset1.GroupKey = groupKey1
1266+
asset1 := asset.NewAssetNoErr(
1267+
t, genesis1, protoAsset1.Amount, protoAsset1.LockTime,
1268+
protoAsset1.RelativeLockTime, protoAsset1.ScriptKey, groupKey1,
1269+
)
12651270

12661271
genesis2 := asset.RandGenesis(t, asset.Normal)
12671272
protoAsset2 := randAsset(t, genesis2, nil)
12681273
groupKey2 := asset.RandGroupKey(t, genesis2, protoAsset2)
1269-
asset2 := protoAsset2.Copy()
1270-
asset2.GroupKey = groupKey2
1274+
asset2 := asset.NewAssetNoErr(
1275+
t, genesis2, protoAsset2.Amount, protoAsset2.LockTime,
1276+
protoAsset2.RelativeLockTime, protoAsset2.ScriptKey, groupKey2,
1277+
)
12711278

12721279
assetCommitment1, err := NewAssetCommitment(asset1)
12731280
require.NoError(t, err)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
github.com/btcsuite/btcwallet v0.16.10-0.20231017144732-e3ff37491e9c
1313
github.com/caddyserver/certmagic v0.17.2
1414
github.com/davecgh/go-spew v1.1.1
15+
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1
1516
github.com/go-errors/errors v1.0.1
1617
github.com/golang-migrate/migrate/v4 v4.16.1
1718
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
@@ -74,7 +75,6 @@ require (
7475
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
7576
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
7677
github.com/decred/dcrd/crypto/blake256 v1.0.0 // indirect
77-
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
7878
github.com/decred/dcrd/lru v1.0.0 // indirect
7979
github.com/docker/cli v20.10.17+incompatible // indirect
8080
github.com/docker/docker v20.10.24+incompatible // indirect

tapdb/assets_store_test.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func randAsset(t *testing.T, genOpts ...assetGenOpt) *asset.Asset {
145145

146146
protoAsset = asset.NewAssetNoErr(
147147
t, genesis, opts.amt, lockTime, relativeLockTime,
148-
opts.scriptKey, nil,
148+
opts.scriptKey, nil, asset.WithAssetVersion(opts.version),
149149
)
150150

151151
if opts.groupAnchorGen != nil {
@@ -167,19 +167,27 @@ func randAsset(t *testing.T, genOpts ...assetGenOpt) *asset.Asset {
167167
ScriptKey: opts.scriptKey,
168168
}
169169

170+
// Go with an even amount to make the splits always work nicely.
171+
if newAsset.Amount%2 != 0 {
172+
newAsset.Amount++
173+
}
174+
170175
// 50/50 chance that we'll actually have a group key. Or we'll always
171176
// use it if a custom group key was specified.
172177
switch {
173178
case opts.noGroupKey:
174179
break
175180

176181
case opts.customGroup || test.RandInt[int]()%2 == 0:
177-
newAsset.GroupKey = assetGroupKey
178-
}
182+
// If we're using a group key, we want to leave the asset with
183+
// the group witness and not a random witness.
184+
assetWithGroup := asset.NewAssetNoErr(
185+
t, genesis, newAsset.Amount, newAsset.LockTime,
186+
newAsset.RelativeLockTime, newAsset.ScriptKey,
187+
assetGroupKey, asset.WithAssetVersion(opts.version),
188+
)
179189

180-
// Go with an even amount to make the splits always work nicely.
181-
if newAsset.Amount%2 != 0 {
182-
newAsset.Amount++
190+
return assetWithGroup
183191
}
184192

185193
// For the witnesses, we'll flip a coin: we'll either make a genesis

tapscript/mint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func BuildGenesisTx(newAsset *asset.Asset) (*wire.MsgTx,
2020
// First, we check that the passed asset is a genesis grouped asset
2121
// that has no group witness.
2222
if !newAsset.NeedsGenesisWitnessForGroup() {
23-
return nil, nil, fmt.Errorf("asset is not a genesis grouped" +
23+
return nil, nil, fmt.Errorf("asset is not a genesis grouped " +
2424
"asset")
2525
}
2626

vm/vm_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ func randAsset(t *testing.T, assetType asset.Type,
5454
protoAsset := asset.RandAssetWithValues(t, genesis, nil, scriptKey)
5555
groupKey := asset.RandGroupKey(t, genesis, protoAsset)
5656

57-
fullAsset := protoAsset.Copy()
58-
fullAsset.GroupKey = groupKey
59-
return fullAsset
57+
return asset.NewAssetNoErr(
58+
t, genesis, protoAsset.Amount, protoAsset.LockTime,
59+
protoAsset.RelativeLockTime, scriptKey, groupKey,
60+
)
6061
}
6162

6263
func genTaprootKeySpend(t *testing.T, privKey btcec.PrivateKey,

0 commit comments

Comments
 (0)