Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

misc: group key testing #717

Merged
merged 5 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions asset/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -1208,9 +1208,10 @@ func (a *Asset) Copy() *Asset {

if a.GroupKey != nil {
assetCopy.GroupKey = &GroupKey{
RawKey: a.GroupKey.RawKey,
GroupPubKey: a.GroupKey.GroupPubKey,
Witness: a.GroupKey.Witness,
RawKey: a.GroupKey.RawKey,
GroupPubKey: a.GroupKey.GroupPubKey,
TapscriptRoot: a.GroupKey.TapscriptRoot,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Witness: a.GroupKey.Witness,
}
}

Expand Down
2 changes: 1 addition & 1 deletion asset/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (m *MockGroupTxBuilder) BuildGenesisTx(newAsset *Asset) (*wire.MsgTx,
// First, we check that the passed asset is a genesis grouped asset
// that has no group witness.
if !newAsset.NeedsGenesisWitnessForGroup() {
return nil, nil, fmt.Errorf("asset is not a genesis grouped" +
return nil, nil, fmt.Errorf("asset is not a genesis grouped " +
"asset")
}

Expand Down
93 changes: 50 additions & 43 deletions commitment/commitment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,39 +72,44 @@ func TestNewAssetCommitment(t *testing.T) {
t.Parallel()

genesis1 := asset.RandGenesis(t, asset.Normal)
genesis2 := asset.RandGenesis(t, asset.Normal)
genesis1Collectible := asset.RandGenesis(t, asset.Collectible)
genesis1CollectibleScriptKey := asset.RandScriptKey(t)
genesis1CollectibleProtoAsset := asset.NewAssetNoErr(
t, genesis1Collectible, 1, 0, 0, genesis1CollectibleScriptKey,
nil,
)
genesis2 := asset.RandGenesis(t, asset.Normal)
genesis2ProtoAsset := asset.RandAssetWithValues(
t, genesis2, nil, asset.RandScriptKey(t),
t, genesis1Collectible, 1, 0, 0, asset.RandScriptKey(t), nil,
)
group1Anchor := randAsset(t, genesis1, nil)
groupKey1, group1PrivBytes := asset.RandGroupKeyWithSigner(
t, genesis1, group1Anchor,
)
group1Priv, group1Pub := btcec.PrivKeyFromBytes(group1PrivBytes)
group1Anchor.GroupKey = groupKey1
group1Anchor = asset.NewAssetNoErr(
t, genesis1, group1Anchor.Amount, group1Anchor.LockTime,
group1Anchor.RelativeLockTime, group1Anchor.ScriptKey,
groupKey1,
)
groupKey1Collectible := asset.RandGroupKey(
t, genesis1Collectible, genesis1CollectibleProtoAsset,
)
genesis2ProtoAsset := randAsset(t, genesis2, nil)
groupKey2 := asset.RandGroupKey(t, genesis2, genesis2ProtoAsset)
copyOfGroupKey1Collectible := &asset.GroupKey{
RawKey: groupKey1Collectible.RawKey,
GroupPubKey: groupKey1Collectible.GroupPubKey,
Witness: groupKey1Collectible.Witness,
RawKey: groupKey1Collectible.RawKey,
GroupPubKey: groupKey1Collectible.GroupPubKey,
TapscriptRoot: groupKey1Collectible.TapscriptRoot,
Witness: groupKey1Collectible.Witness,
}
group1Reissued := randAsset(t, genesis2, nil)
genTxBuilder := asset.MockGroupTxBuilder{}
group1Priv, group1Pub := btcec.PrivKeyFromBytes(group1PrivBytes)
group1ReissuedGroupKey, err := asset.DeriveGroupKey(
asset.NewMockGenesisSigner(group1Priv), &genTxBuilder,
test.PubToKeyDesc(group1Pub), genesis1, genesis2ProtoAsset,
)
require.NoError(t, err)
group1Reissued.GroupKey = group1ReissuedGroupKey
group1Reissued = asset.NewAssetNoErr(
t, genesis2, group1Reissued.Amount, group1Reissued.LockTime,
group1Reissued.RelativeLockTime, group1Reissued.ScriptKey,
group1ReissuedGroupKey,
)

testCases := []struct {
name string
Expand Down Expand Up @@ -503,10 +508,6 @@ func TestSplitCommitment(t *testing.T) {
groupKeyCollectible := asset.RandGroupKey(
t, genesisCollectible, collectibleProtoAsset,
)
normalInputAsset := normalProtoAsset.Copy()
normalInputAsset.GroupKey = groupKeyNormal
collectibleInputAsset := collectibleProtoAsset.Copy()
collectibleInputAsset.GroupKey = groupKeyCollectible

testCases := []struct {
name string
Expand Down Expand Up @@ -898,7 +899,10 @@ func TestTapCommitmentKeyPopulation(t *testing.T) {
var groupKey *asset.GroupKey
if assetDesc.HasGroupKey {
groupKey = asset.RandGroupKey(t, genesis, a)
a.GroupKey = groupKey
a = asset.NewAssetNoErr(
t, genesis, a.Amount, a.LockTime,
a.RelativeLockTime, a.ScriptKey, groupKey,
)
}

commitment, err := NewAssetCommitment(a)
Expand Down Expand Up @@ -932,18 +936,26 @@ func TestUpdateAssetCommitment(t *testing.T) {
groupKey1, group1PrivBytes := asset.RandGroupKeyWithSigner(
t, genesis1, group1Anchor,
)
group1Priv, group1Pub := btcec.PrivKeyFromBytes(group1PrivBytes)
group1Anchor.GroupKey = groupKey1
group1Anchor = asset.NewAssetNoErr(
t, genesis1, group1Anchor.Amount, group1Anchor.LockTime,
group1Anchor.RelativeLockTime, group1Anchor.ScriptKey,
groupKey1,
)
group2Anchor := randAsset(t, genesis2, nil)
groupKey2 := asset.RandGroupKey(t, genesis2, group2Anchor)
group1Reissued := group2Anchor.Copy()
group1Reissued := randAsset(t, genesis2, nil)
genTxBuilder := asset.MockGroupTxBuilder{}
group1Priv, group1Pub := btcec.PrivKeyFromBytes(group1PrivBytes)
group1ReissuedGroupKey, err := asset.DeriveGroupKey(
asset.NewMockGenesisSigner(group1Priv), &genTxBuilder,
test.PubToKeyDesc(group1Pub), genesis1, group1Reissued,
)
require.NoError(t, err)
group1Reissued.GroupKey = group1ReissuedGroupKey
group1Reissued = asset.NewAssetNoErr(
t, genesis2, group1Reissued.Amount, group1Reissued.LockTime,
group1Reissued.RelativeLockTime, group1Reissued.ScriptKey,
group1ReissuedGroupKey,
)

assetNoGroup := randAsset(t, genesis2, nil)
copyOfAssetNoGroup := assetNoGroup.Copy()
Expand Down Expand Up @@ -990,19 +1002,6 @@ func TestUpdateAssetCommitment(t *testing.T) {
{
name: "insertion of asset with group key",
f: func() (*asset.Asset, error) {
group1Reissued := randAsset(t, genesis2, nil)
genTxBuilder := asset.MockGroupTxBuilder{}
gen2ProtoAsset := asset.RandAssetWithValues(
t, genesis2, nil, asset.RandScriptKey(t),
)
group1ReissuedGroupKey, err := asset.DeriveGroupKey(
asset.NewMockGenesisSigner(group1Priv),
&genTxBuilder,
test.PubToKeyDesc(group1Priv.PubKey()),
genesis1, gen2ProtoAsset,
)
require.NoError(t, err)
group1Reissued.GroupKey = group1ReissuedGroupKey
return group1Reissued,
groupAssetCommitment.Upsert(group1Reissued)
},
Expand Down Expand Up @@ -1088,11 +1087,15 @@ func TestUpdateTapCommitment(t *testing.T) {
genesis3 := asset.RandGenesis(t, asset.Normal)
asset3 := randAsset(t, genesis3, groupKey1)

asset1 := protoAsset1.Copy()
asset1.GroupKey = groupKey1
asset1 := asset.NewAssetNoErr(
t, genesis1, protoAsset1.Amount, protoAsset1.LockTime,
protoAsset1.RelativeLockTime, protoAsset1.ScriptKey, groupKey1,
)

asset2 := protoAsset2.Copy()
asset2.GroupKey = groupKey2
asset2 := asset.NewAssetNoErr(
t, genesis2, protoAsset2.Amount, protoAsset2.LockTime,
protoAsset2.RelativeLockTime, protoAsset2.ScriptKey, groupKey2,
)

assetCommitment1, err := NewAssetCommitment(asset1)
require.NoError(t, err)
Expand Down Expand Up @@ -1260,14 +1263,18 @@ func TestTapCommitmentDeepCopy(t *testing.T) {
genesis1 := asset.RandGenesis(t, asset.Normal)
protoAsset1 := randAsset(t, genesis1, nil)
groupKey1 := asset.RandGroupKey(t, genesis1, protoAsset1)
asset1 := protoAsset1.Copy()
asset1.GroupKey = groupKey1
asset1 := asset.NewAssetNoErr(
t, genesis1, protoAsset1.Amount, protoAsset1.LockTime,
protoAsset1.RelativeLockTime, protoAsset1.ScriptKey, groupKey1,
)

genesis2 := asset.RandGenesis(t, asset.Normal)
protoAsset2 := randAsset(t, genesis2, nil)
groupKey2 := asset.RandGroupKey(t, genesis2, protoAsset2)
asset2 := protoAsset2.Copy()
asset2.GroupKey = groupKey2
asset2 := asset.NewAssetNoErr(
t, genesis2, protoAsset2.Amount, protoAsset2.LockTime,
protoAsset2.RelativeLockTime, protoAsset2.ScriptKey, groupKey2,
)

assetCommitment1, err := NewAssetCommitment(asset1)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/btcsuite/btcwallet v0.16.10-0.20231017144732-e3ff37491e9c
github.com/caddyserver/certmagic v0.17.2
github.com/davecgh/go-spew v1.1.1
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1
github.com/go-errors/errors v1.0.1
github.com/golang-migrate/migrate/v4 v4.16.1
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
Expand Down Expand Up @@ -74,7 +75,6 @@ require (
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/decred/dcrd/crypto/blake256 v1.0.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/decred/dcrd/lru v1.0.0 // indirect
github.com/docker/cli v20.10.17+incompatible // indirect
github.com/docker/docker v20.10.24+incompatible // indirect
Expand Down
20 changes: 14 additions & 6 deletions tapdb/assets_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func randAsset(t *testing.T, genOpts ...assetGenOpt) *asset.Asset {

protoAsset = asset.NewAssetNoErr(
t, genesis, opts.amt, lockTime, relativeLockTime,
opts.scriptKey, nil,
opts.scriptKey, nil, asset.WithAssetVersion(opts.version),
)

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

// Go with an even amount to make the splits always work nicely.
if newAsset.Amount%2 != 0 {
newAsset.Amount++
}

// 50/50 chance that we'll actually have a group key. Or we'll always
// use it if a custom group key was specified.
switch {
case opts.noGroupKey:
break

case opts.customGroup || test.RandInt[int]()%2 == 0:
newAsset.GroupKey = assetGroupKey
}
// If we're using a group key, we want to leave the asset with
// the group witness and not a random witness.
assetWithGroup := asset.NewAssetNoErr(
t, genesis, newAsset.Amount, newAsset.LockTime,
newAsset.RelativeLockTime, newAsset.ScriptKey,
assetGroupKey, asset.WithAssetVersion(opts.version),
)

// Go with an even amount to make the splits always work nicely.
if newAsset.Amount%2 != 0 {
newAsset.Amount++
return assetWithGroup
}

// For the witnesses, we'll flip a coin: we'll either make a genesis
Expand Down
2 changes: 1 addition & 1 deletion tapscript/mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func BuildGenesisTx(newAsset *asset.Asset) (*wire.MsgTx,
// First, we check that the passed asset is a genesis grouped asset
// that has no group witness.
if !newAsset.NeedsGenesisWitnessForGroup() {
return nil, nil, fmt.Errorf("asset is not a genesis grouped" +
return nil, nil, fmt.Errorf("asset is not a genesis grouped " +
"asset")
}

Expand Down
7 changes: 4 additions & 3 deletions vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ func randAsset(t *testing.T, assetType asset.Type,
protoAsset := asset.RandAssetWithValues(t, genesis, nil, scriptKey)
groupKey := asset.RandGroupKey(t, genesis, protoAsset)

fullAsset := protoAsset.Copy()
fullAsset.GroupKey = groupKey
return fullAsset
return asset.NewAssetNoErr(
t, genesis, protoAsset.Amount, protoAsset.LockTime,
protoAsset.RelativeLockTime, scriptKey, groupKey,
)
}

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