Merge pull request #2737 from nspcc-dev/fix-win-cleanup

cli: properly cleanup state-dependand VM CLI
This commit is contained in:
Roman Khimov 2022-10-10 15:49:18 +07:00 committed by GitHub
commit 478b4b0c1c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 11 deletions

View file

@ -107,6 +107,8 @@ func newTestVMCLIWithLogoAndCustomConfig(t *testing.T, printLogo bool, cfg *conf
return e return e
} }
// newTestVMClIWithState creates executor backed by level DB filled by simple chain.
// LevelDB-backed CLI must be exited on cleanup.
func newTestVMClIWithState(t *testing.T) *executor { func newTestVMClIWithState(t *testing.T) *executor {
// Firstly create a DB with chain, save and close it. // Firstly create a DB with chain, save and close it.
path := t.TempDir() path := t.TempDir()
@ -776,7 +778,8 @@ func TestRunWithState(t *testing.T) {
emit.AppCall(script.BinWriter, h, "put", callflag.All, 3, 3) emit.AppCall(script.BinWriter, h, "put", callflag.All, 3, 3)
e.runProg(t, e.runProg(t,
"loadhex "+hex.EncodeToString(script.Bytes()), "loadhex "+hex.EncodeToString(script.Bytes()),
"run") "run",
"exit")
e.checkNextLine(t, "READY: loaded 37 instructions") e.checkNextLine(t, "READY: loaded 37 instructions")
e.checkStack(t, 3) e.checkStack(t, 3)
} }
@ -797,6 +800,7 @@ func TestRunWithHistoricState(t *testing.T) {
"run", "run",
"loadhex --historic 0 "+hex.EncodeToString(b), // historic invocation, contract is not deployed yet "loadhex --historic 0 "+hex.EncodeToString(b), // historic invocation, contract is not deployed yet
"run", "run",
"exit",
) )
e.checkNextLine(t, "READY: loaded 36 instructions") e.checkNextLine(t, "READY: loaded 36 instructions")
e.checkStack(t, []byte{2}) e.checkStack(t, []byte{2})
@ -816,7 +820,8 @@ func TestEvents(t *testing.T) {
e.runProg(t, e.runProg(t,
"loadhex "+hex.EncodeToString(script.Bytes()), "loadhex "+hex.EncodeToString(script.Bytes()),
"run", "run",
"events") "events",
"exit")
expectedEvent := state.NotificationEvent{ expectedEvent := state.NotificationEvent{
ScriptHash: h, ScriptHash: h,
Name: "Event", Name: "Event",
@ -844,7 +849,7 @@ func TestEnv(t *testing.T) {
}) })
t.Run("setup with state", func(t *testing.T) { t.Run("setup with state", func(t *testing.T) {
e := newTestVMClIWithState(t) e := newTestVMClIWithState(t)
e.runProg(t, "env") e.runProg(t, "env", "exit")
e.checkNextLine(t, "Chain height: 5") e.checkNextLine(t, "Chain height: 5")
e.checkNextLineExact(t, "VM height (may differ from chain height in case of historic call): 5\n") e.checkNextLineExact(t, "VM height (may differ from chain height in case of historic call): 5\n")
e.checkNextLine(t, "Network magic: 42") e.checkNextLine(t, "Network magic: 42")
@ -853,7 +858,7 @@ func TestEnv(t *testing.T) {
t.Run("setup with historic state", func(t *testing.T) { t.Run("setup with historic state", func(t *testing.T) {
e := newTestVMClIWithState(t) e := newTestVMClIWithState(t)
e.runProg(t, "loadbase64 --historic 3 "+base64.StdEncoding.EncodeToString([]byte{byte(opcode.PUSH1)}), e.runProg(t, "loadbase64 --historic 3 "+base64.StdEncoding.EncodeToString([]byte{byte(opcode.PUSH1)}),
"env") "env", "exit")
e.checkNextLine(t, "READY: loaded 1 instructions") e.checkNextLine(t, "READY: loaded 1 instructions")
e.checkNextLine(t, "Chain height: 5") e.checkNextLine(t, "Chain height: 5")
e.checkNextLineExact(t, "VM height (may differ from chain height in case of historic call): 3\n") e.checkNextLineExact(t, "VM height (may differ from chain height in case of historic call): 3\n")
@ -862,7 +867,7 @@ func TestEnv(t *testing.T) {
}) })
t.Run("verbose", func(t *testing.T) { t.Run("verbose", func(t *testing.T) {
e := newTestVMClIWithState(t) e := newTestVMClIWithState(t)
e.runProg(t, "env -v") e.runProg(t, "env -v", "exit")
e.checkNextLine(t, "Chain height: 5") e.checkNextLine(t, "Chain height: 5")
e.checkNextLineExact(t, "VM height (may differ from chain height in case of historic call): 5\n") e.checkNextLineExact(t, "VM height (may differ from chain height in case of historic call): 5\n")
e.checkNextLine(t, "Network magic: 42") e.checkNextLine(t, "Network magic: 42")
@ -887,6 +892,7 @@ func TestDumpStorage(t *testing.T) {
"storage 1", "storage 1",
"storage 1 "+hex.EncodeToString(expected[0].Key), "storage 1 "+hex.EncodeToString(expected[0].Key),
"storage 1 --backwards", "storage 1 --backwards",
"exit",
) )
e.checkStorage(t, expected...) e.checkStorage(t, expected...)
e.checkStorage(t, expected...) e.checkStorage(t, expected...)
@ -916,6 +922,7 @@ func TestDumpStorageDiff(t *testing.T) {
"run", "run",
"storage 1", "storage 1",
"storage 1 --diff", "storage 1 --diff",
"exit",
) )
e.checkStorage(t, expected...) e.checkStorage(t, expected...)
@ -969,6 +976,7 @@ func TestDumpChanges(t *testing.T) {
"changes 1 "+hex.EncodeToString([]byte{1}), "changes 1 "+hex.EncodeToString([]byte{1}),
"changes 1 "+hex.EncodeToString([]byte{2}), "changes 1 "+hex.EncodeToString([]byte{2}),
"changes 1 "+hex.EncodeToString([]byte{3}), "changes 1 "+hex.EncodeToString([]byte{3}),
"exit",
) )
// no script is executed => no diff // no script is executed => no diff

View file

@ -56,7 +56,12 @@ func NewBoltDBStore(cfg dbconfig.BoltDBOptions) (*BoltDBStore, error) {
}) })
} }
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to initialize BoltDB instance: %w", err) closeErr := db.Close()
err = fmt.Errorf("failed to initialize BoltDB instance: %w", err)
if closeErr != nil {
err = fmt.Errorf("%w, failed to close BoltDB instance: %v", err, closeErr)
}
return nil, err
} }
return &BoltDBStore{db: db}, nil return &BoltDBStore{db: db}, nil

View file

@ -7,7 +7,6 @@ import (
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/core/storage/dbconfig" "github.com/nspcc-dev/neo-go/pkg/core/storage/dbconfig"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"go.etcd.io/bbolt" "go.etcd.io/bbolt"
) )
@ -47,10 +46,9 @@ func TestROBoltDB(t *testing.T) {
require.NoError(t, store.Close()) require.NoError(t, store.Close())
// Create the DB without buckets and try to open it in RO mode, an error is expected. // Create the DB without buckets and try to open it in RO mode, an error is expected.
fileMode := os.FileMode(0600) tmp := t.TempDir()
cfg.FilePath = filepath.Join(d, "clean_ro_bolt_db") cfg.FilePath = filepath.Join(tmp, "clean_ro_bolt")
require.NoError(t, io.MakeDirForFile(cfg.FilePath, "BoltDB")) db, err := bbolt.Open(cfg.FilePath, os.FileMode(0600), nil)
db, err := bbolt.Open(cfg.FilePath, fileMode, nil)
require.NoError(t, err) require.NoError(t, err)
require.NoError(t, db.Close()) require.NoError(t, db.Close())