Skip to content

Commit d07b9c7

Browse files
authored
Merge pull request #598 from lightninglabs/group_vm_fixes
make+vm: increase vm test coverage, improve the coverage target
2 parents b489d04 + 95ef323 commit d07b9c7

File tree

7 files changed

+875
-450
lines changed

7 files changed

+875
-450
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@ cmd/tapd/tapd
2626

2727
# Output of the go coverage tool, specifically when used with LiteIDE
2828
*.out
29+
coverage.*
2930

3031
# Sed backup files.
3132
*.bak
3233

3334
# Editor configs.
3435
*.idea
3536
*.iml
37+
*.code-workspace
38+
.vscode/
3639

3740
# Dependency directories (remove the comment below to include it)
3841
vendor/

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ unit-trace:
162162

163163
unit-cover: $(GOACC_BIN)
164164
@$(call print, "Running unit coverage tests.")
165-
$(GOACC_BIN) $(GOLIST_COVER)
165+
$(GOACC); $(COVER_HTML)
166166

167167
unit-race:
168168
@$(call print, "Running unit race tests.")

make/testing_flags.mk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LOG_TAGS =
55
TEST_FLAGS =
66
ITEST_FLAGS = -logoutput
77
COVER_PKG = $$(go list -deps -tags="$(DEV_TAGS)" ./... | grep '$(PKG)' | grep -v lnrpc)
8+
COVER_HTML = go tool cover -html=coverage.txt -o coverage.html
89
POSTGRES_START_DELAY = 5
910

1011
# If rpc option is set also add all extra RPC tags to DEV_TAGS
@@ -112,5 +113,8 @@ endif
112113
# Construct the integration test command with the added build flags.
113114
ITEST_TAGS := $(DEV_TAGS) $(RPC_TAGS) integration itest $(backend)
114115

116+
# Construct the coverage test command path with the added build flags.
117+
GOACC := $(GOACC_BIN) $(COVER_PKG) -- -tags="$(DEV_TAGS) $(LOG_TAGS)" $(TEST_FLAGS)
118+
115119
# Construct the load test command with the added build flags.
116120
LOADTEST_TAGS := $(DEV_TAGS) $(RPC_TAGS) loadtest

vm/testdata/vm_validation_generated.json

Lines changed: 302 additions & 240 deletions
Large diffs are not rendered by default.

vm/testdata/vm_validation_generated_error_cases.json

Lines changed: 441 additions & 179 deletions
Large diffs are not rendered by default.

vm/vm.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,12 @@ func (vm *Engine) Execute() error {
313313
if len(vm.splitAssets) > 0 || len(vm.prevAssets) > 0 {
314314
return newErrKind(ErrInvalidGenesisStateTransition)
315315
}
316+
317+
// A genesis asset with a group key must have a witness before
318+
// being validated.
319+
if vm.newAsset.GroupKey != nil {
320+
return newErrKind(ErrInvalidGenesisStateTransition)
321+
}
316322
return nil
317323
}
318324

vm/vm_test.go

Lines changed: 118 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ var (
2828
generatedTestVectorName,
2929
errorTestVectorName,
3030
}
31+
32+
invalidHashLockErr = newErrInner(
33+
ErrInvalidTransferWitness, txscript.Error{
34+
ErrorCode: txscript.ErrEqualVerify,
35+
Description: "OP_EQUALVERIFY failed",
36+
},
37+
)
38+
invalidSigErr = newErrInner(
39+
ErrInvalidTransferWitness, txscript.Error{
40+
ErrorCode: txscript.ErrNullFail,
41+
Description: "signature not empty on " +
42+
"failed checksig",
43+
},
44+
)
3145
)
3246

3347
func randAsset(t *testing.T, assetType asset.Type,
@@ -102,26 +116,55 @@ type stateTransitionFunc = func(t *testing.T) (*asset.Asset,
102116
commitment.SplitSet, commitment.InputSet)
103117

104118
func genesisStateTransition(assetType asset.Type,
105-
valid bool) stateTransitionFunc {
119+
valid, grouped bool) stateTransitionFunc {
106120

107121
return func(t *testing.T) (*asset.Asset, commitment.SplitSet,
108122
commitment.InputSet) {
109123

110-
a := asset.RandAsset(t, assetType)
111-
if assetType == asset.Collectible && !valid {
112-
inputSet := commitment.InputSet{
124+
var (
125+
inputSet commitment.InputSet
126+
splitSet commitment.SplitSet
127+
a = asset.RandAsset(t, assetType)
128+
)
129+
130+
if !grouped {
131+
a = asset.NewAssetNoErr(
132+
t, a.Genesis, a.Amount, a.LockTime,
133+
a.RelativeLockTime, a.ScriptKey, nil,
134+
)
135+
}
136+
137+
if !valid && assetType == asset.Collectible {
138+
inputSet = commitment.InputSet{
113139
asset.PrevID{}: a.Copy(),
114140
}
115-
return a, nil, inputSet
116141
}
117142

118-
if assetType == asset.Normal && !valid {
119-
splitSet := commitment.SplitSet{
143+
if !valid && assetType == asset.Normal {
144+
splitSet = commitment.SplitSet{
120145
{}: &commitment.SplitAsset{},
121146
}
122-
return a, splitSet, nil
123147
}
124148

149+
return a, splitSet, inputSet
150+
}
151+
}
152+
153+
func invalidGenesisStateTransitionWitness(assetType asset.Type,
154+
grouped bool) stateTransitionFunc {
155+
156+
return func(t *testing.T) (*asset.Asset, commitment.SplitSet,
157+
commitment.InputSet) {
158+
159+
a := asset.RandAsset(t, assetType)
160+
if grouped {
161+
a.PrevWitnesses[0].TxWitness = nil
162+
163+
return a, nil, nil
164+
}
165+
166+
a.GroupKey = nil
167+
125168
return a, nil, nil
126169
}
127170
}
@@ -509,31 +552,87 @@ func TestVM(t *testing.T) {
509552
f stateTransitionFunc
510553
err error
511554
}{
555+
{
556+
name: "collectible group anchor",
557+
f: genesisStateTransition(
558+
asset.Collectible, true, true,
559+
),
560+
err: nil,
561+
},
512562
{
513563
name: "collectible genesis",
514-
f: genesisStateTransition(asset.Collectible, true),
515-
err: nil,
564+
f: genesisStateTransition(
565+
asset.Collectible, true, false,
566+
),
567+
err: nil,
568+
},
569+
{
570+
name: "invalid collectible group anchor",
571+
f: genesisStateTransition(
572+
asset.Collectible, false, true,
573+
),
574+
err: newErrKind(ErrInvalidGenesisStateTransition),
516575
},
517576
{
518577
name: "invalid collectible genesis",
519-
f: genesisStateTransition(asset.Collectible, false),
520-
err: newErrKind(ErrInvalidGenesisStateTransition),
578+
f: genesisStateTransition(
579+
asset.Collectible, false, false,
580+
),
581+
err: newErrKind(ErrInvalidGenesisStateTransition),
582+
},
583+
{
584+
name: "collectible genesis invalid witness",
585+
f: invalidGenesisStateTransitionWitness(
586+
asset.Collectible, false,
587+
),
588+
err: ErrNoInputs,
589+
},
590+
{
591+
name: "collectible group anchor invalid witness",
592+
f: invalidGenesisStateTransitionWitness(
593+
asset.Collectible, true,
594+
),
595+
err: newErrKind(ErrInvalidGenesisStateTransition),
521596
},
522597
{
523598
name: "invalid split collectible input",
524599
f: splitCollectibleStateTransition(false),
525600
err: newErrKind(ErrInvalidSplitAssetType),
526601
},
602+
{
603+
name: "normal group anchor",
604+
f: genesisStateTransition(asset.Normal, true, true),
605+
err: nil,
606+
},
527607
{
528608
name: "normal genesis",
529-
f: genesisStateTransition(asset.Normal, true),
609+
f: genesisStateTransition(asset.Normal, true, false),
530610
err: nil,
531611
},
612+
{
613+
name: "invalid normal group anchor",
614+
f: genesisStateTransition(asset.Normal, false, true),
615+
err: newErrKind(ErrInvalidGenesisStateTransition),
616+
},
532617
{
533618
name: "invalid normal genesis",
534-
f: genesisStateTransition(asset.Normal, false),
619+
f: genesisStateTransition(asset.Normal, false, false),
535620
err: newErrKind(ErrInvalidGenesisStateTransition),
536621
},
622+
{
623+
name: "normal genesis invalid witness",
624+
f: invalidGenesisStateTransitionWitness(
625+
asset.Normal, false,
626+
),
627+
err: ErrNoInputs,
628+
},
629+
{
630+
name: "normal group anchor invalid witness",
631+
f: invalidGenesisStateTransitionWitness(
632+
asset.Normal, true,
633+
),
634+
err: newErrKind(ErrInvalidGenesisStateTransition),
635+
},
537636
{
538637
name: "collectible state transition",
539638
f: collectibleStateTransition,
@@ -578,13 +677,8 @@ func TestVM(t *testing.T) {
578677
{
579678
name: "script tree spend state transition invalid " +
580679
"hash lock",
581-
f: scriptTreeSpendStateTransition(t, true, false, 999),
582-
err: newErrInner(
583-
ErrInvalidTransferWitness, txscript.Error{
584-
ErrorCode: txscript.ErrEqualVerify,
585-
Description: "OP_EQUALVERIFY failed",
586-
},
587-
),
680+
f: scriptTreeSpendStateTransition(t, true, false, 999),
681+
err: invalidHashLockErr,
588682
},
589683
{
590684
name: "script tree spend state transition valid sig " +
@@ -605,14 +699,8 @@ func TestVM(t *testing.T) {
605699
{
606700
name: "script tree spend state transition invalid " +
607701
"sig",
608-
f: scriptTreeSpendStateTransition(t, false, false, 999),
609-
err: newErrInner(
610-
ErrInvalidTransferWitness, txscript.Error{
611-
ErrorCode: txscript.ErrNullFail,
612-
Description: "signature not empty on " +
613-
"failed checksig",
614-
},
615-
),
702+
f: scriptTreeSpendStateTransition(t, false, false, 999),
703+
err: invalidSigErr,
616704
},
617705
}
618706

@@ -688,7 +776,7 @@ func verifyTestCase(t testing.TB, expectedErr error, compareErrString bool,
688776
)
689777
}
690778
} else {
691-
require.Equal(t, expectedErr, err)
779+
require.ErrorIs(t, err, expectedErr)
692780
}
693781
}
694782

0 commit comments

Comments
 (0)