Skip to content

Commit

Permalink
Merge pull request #598 from lightninglabs/group_vm_fixes
Browse files Browse the repository at this point in the history
make+vm: increase vm test coverage, improve the coverage target
  • Loading branch information
Roasbeef authored Oct 18, 2023
2 parents b489d04 + 95ef323 commit d07b9c7
Show file tree
Hide file tree
Showing 7 changed files with 875 additions and 450 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ cmd/tapd/tapd

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
coverage.*

# Sed backup files.
*.bak

# Editor configs.
*.idea
*.iml
*.code-workspace
.vscode/

# Dependency directories (remove the comment below to include it)
vendor/
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ unit-trace:

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

unit-race:
@$(call print, "Running unit race tests.")
Expand Down
4 changes: 4 additions & 0 deletions make/testing_flags.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LOG_TAGS =
TEST_FLAGS =
ITEST_FLAGS = -logoutput
COVER_PKG = $$(go list -deps -tags="$(DEV_TAGS)" ./... | grep '$(PKG)' | grep -v lnrpc)
COVER_HTML = go tool cover -html=coverage.txt -o coverage.html
POSTGRES_START_DELAY = 5

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

# Construct the coverage test command path with the added build flags.
GOACC := $(GOACC_BIN) $(COVER_PKG) -- -tags="$(DEV_TAGS) $(LOG_TAGS)" $(TEST_FLAGS)

# Construct the load test command with the added build flags.
LOADTEST_TAGS := $(DEV_TAGS) $(RPC_TAGS) loadtest
542 changes: 302 additions & 240 deletions vm/testdata/vm_validation_generated.json

Large diffs are not rendered by default.

620 changes: 441 additions & 179 deletions vm/testdata/vm_validation_generated_error_cases.json

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,12 @@ func (vm *Engine) Execute() error {
if len(vm.splitAssets) > 0 || len(vm.prevAssets) > 0 {
return newErrKind(ErrInvalidGenesisStateTransition)
}

// A genesis asset with a group key must have a witness before
// being validated.
if vm.newAsset.GroupKey != nil {
return newErrKind(ErrInvalidGenesisStateTransition)
}
return nil
}

Expand Down
148 changes: 118 additions & 30 deletions vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ var (
generatedTestVectorName,
errorTestVectorName,
}

invalidHashLockErr = newErrInner(
ErrInvalidTransferWitness, txscript.Error{
ErrorCode: txscript.ErrEqualVerify,
Description: "OP_EQUALVERIFY failed",
},
)
invalidSigErr = newErrInner(
ErrInvalidTransferWitness, txscript.Error{
ErrorCode: txscript.ErrNullFail,
Description: "signature not empty on " +
"failed checksig",
},
)
)

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

func genesisStateTransition(assetType asset.Type,
valid bool) stateTransitionFunc {
valid, grouped bool) stateTransitionFunc {

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

a := asset.RandAsset(t, assetType)
if assetType == asset.Collectible && !valid {
inputSet := commitment.InputSet{
var (
inputSet commitment.InputSet
splitSet commitment.SplitSet
a = asset.RandAsset(t, assetType)
)

if !grouped {
a = asset.NewAssetNoErr(
t, a.Genesis, a.Amount, a.LockTime,
a.RelativeLockTime, a.ScriptKey, nil,
)
}

if !valid && assetType == asset.Collectible {
inputSet = commitment.InputSet{
asset.PrevID{}: a.Copy(),
}
return a, nil, inputSet
}

if assetType == asset.Normal && !valid {
splitSet := commitment.SplitSet{
if !valid && assetType == asset.Normal {
splitSet = commitment.SplitSet{
{}: &commitment.SplitAsset{},
}
return a, splitSet, nil
}

return a, splitSet, inputSet
}
}

func invalidGenesisStateTransitionWitness(assetType asset.Type,
grouped bool) stateTransitionFunc {

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

a := asset.RandAsset(t, assetType)
if grouped {
a.PrevWitnesses[0].TxWitness = nil

return a, nil, nil
}

a.GroupKey = nil

return a, nil, nil
}
}
Expand Down Expand Up @@ -509,31 +552,87 @@ func TestVM(t *testing.T) {
f stateTransitionFunc
err error
}{
{
name: "collectible group anchor",
f: genesisStateTransition(
asset.Collectible, true, true,
),
err: nil,
},
{
name: "collectible genesis",
f: genesisStateTransition(asset.Collectible, true),
err: nil,
f: genesisStateTransition(
asset.Collectible, true, false,
),
err: nil,
},
{
name: "invalid collectible group anchor",
f: genesisStateTransition(
asset.Collectible, false, true,
),
err: newErrKind(ErrInvalidGenesisStateTransition),
},
{
name: "invalid collectible genesis",
f: genesisStateTransition(asset.Collectible, false),
err: newErrKind(ErrInvalidGenesisStateTransition),
f: genesisStateTransition(
asset.Collectible, false, false,
),
err: newErrKind(ErrInvalidGenesisStateTransition),
},
{
name: "collectible genesis invalid witness",
f: invalidGenesisStateTransitionWitness(
asset.Collectible, false,
),
err: ErrNoInputs,
},
{
name: "collectible group anchor invalid witness",
f: invalidGenesisStateTransitionWitness(
asset.Collectible, true,
),
err: newErrKind(ErrInvalidGenesisStateTransition),
},
{
name: "invalid split collectible input",
f: splitCollectibleStateTransition(false),
err: newErrKind(ErrInvalidSplitAssetType),
},
{
name: "normal group anchor",
f: genesisStateTransition(asset.Normal, true, true),
err: nil,
},
{
name: "normal genesis",
f: genesisStateTransition(asset.Normal, true),
f: genesisStateTransition(asset.Normal, true, false),
err: nil,
},
{
name: "invalid normal group anchor",
f: genesisStateTransition(asset.Normal, false, true),
err: newErrKind(ErrInvalidGenesisStateTransition),
},
{
name: "invalid normal genesis",
f: genesisStateTransition(asset.Normal, false),
f: genesisStateTransition(asset.Normal, false, false),
err: newErrKind(ErrInvalidGenesisStateTransition),
},
{
name: "normal genesis invalid witness",
f: invalidGenesisStateTransitionWitness(
asset.Normal, false,
),
err: ErrNoInputs,
},
{
name: "normal group anchor invalid witness",
f: invalidGenesisStateTransitionWitness(
asset.Normal, true,
),
err: newErrKind(ErrInvalidGenesisStateTransition),
},
{
name: "collectible state transition",
f: collectibleStateTransition,
Expand Down Expand Up @@ -578,13 +677,8 @@ func TestVM(t *testing.T) {
{
name: "script tree spend state transition invalid " +
"hash lock",
f: scriptTreeSpendStateTransition(t, true, false, 999),
err: newErrInner(
ErrInvalidTransferWitness, txscript.Error{
ErrorCode: txscript.ErrEqualVerify,
Description: "OP_EQUALVERIFY failed",
},
),
f: scriptTreeSpendStateTransition(t, true, false, 999),
err: invalidHashLockErr,
},
{
name: "script tree spend state transition valid sig " +
Expand All @@ -605,14 +699,8 @@ func TestVM(t *testing.T) {
{
name: "script tree spend state transition invalid " +
"sig",
f: scriptTreeSpendStateTransition(t, false, false, 999),
err: newErrInner(
ErrInvalidTransferWitness, txscript.Error{
ErrorCode: txscript.ErrNullFail,
Description: "signature not empty on " +
"failed checksig",
},
),
f: scriptTreeSpendStateTransition(t, false, false, 999),
err: invalidSigErr,
},
}

Expand Down Expand Up @@ -688,7 +776,7 @@ func verifyTestCase(t testing.TB, expectedErr error, compareErrString bool,
)
}
} else {
require.Equal(t, expectedErr, err)
require.ErrorIs(t, err, expectedErr)
}
}

Expand Down

0 comments on commit d07b9c7

Please sign in to comment.