From 96c4e61063fe467cf0f8f80721ba5ae4aadae778 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 8 Jul 2022 21:17:52 +0300 Subject: [PATCH] storage: move Operation into package of its own Don't use storage.* types in rpc/response/result. --- cli/server/dump.go | 7 ++++--- pkg/core/storage/dboper/operation.go | 13 +++++++++++++ pkg/core/storage/store.go | 20 ++++++-------------- pkg/core/storage/store_test.go | 3 ++- pkg/rpc/response/result/invoke.go | 3 ++- pkg/rpc/server/server_test.go | 12 ++++++------ 6 files changed, 33 insertions(+), 25 deletions(-) create mode 100644 pkg/core/storage/dboper/operation.go diff --git a/cli/server/dump.go b/cli/server/dump.go index 762b2e636..d7b6a1803 100644 --- a/cli/server/dump.go +++ b/cli/server/dump.go @@ -7,14 +7,15 @@ import ( "path/filepath" "github.com/nspcc-dev/neo-go/pkg/core/storage" + "github.com/nspcc-dev/neo-go/pkg/core/storage/dboper" ) type dump []blockDump type blockDump struct { - Block uint32 `json:"block"` - Size int `json:"size"` - Storage []storage.Operation `json:"storage"` + Block uint32 `json:"block"` + Size int `json:"size"` + Storage []dboper.Operation `json:"storage"` } func newDump() *dump { diff --git a/pkg/core/storage/dboper/operation.go b/pkg/core/storage/dboper/operation.go new file mode 100644 index 000000000..8207b2b9e --- /dev/null +++ b/pkg/core/storage/dboper/operation.go @@ -0,0 +1,13 @@ +/* +Package dboper contains a type used to represent single DB operation. +*/ +package dboper + +// Operation represents a single KV operation (add/del/change) performed +// in the DB. +type Operation struct { + // State can be Added, Changed or Deleted. + State string `json:"state"` + Key []byte `json:"key"` + Value []byte `json:"value,omitempty"` +} diff --git a/pkg/core/storage/store.go b/pkg/core/storage/store.go index 785e47886..931d153ff 100644 --- a/pkg/core/storage/store.go +++ b/pkg/core/storage/store.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/nspcc-dev/neo-go/pkg/core/storage/dbconfig" + "github.com/nspcc-dev/neo-go/pkg/core/storage/dboper" "github.com/syndtr/goleveldb/leveldb/util" ) @@ -41,15 +42,6 @@ const ( ExecTransaction byte = 2 ) -// Operation represents a single KV operation (add/del/change) performed -// in the DB. -type Operation struct { - // State can be Added, Changed or Deleted. - State string `json:"state"` - Key []byte `json:"key"` - Value []byte `json:"value,omitempty"` -} - // SeekRange represents options for Store.Seek operation. type SeekRange struct { // Prefix denotes the Seek's lookup key. @@ -134,10 +126,10 @@ func NewStore(cfg dbconfig.DBConfiguration) (Store, error) { return store, err } -// BatchToOperations converts a batch of changes into array of Operations. -func BatchToOperations(batch *MemBatch) []Operation { +// BatchToOperations converts a batch of changes into array of dboper.Operation. +func BatchToOperations(batch *MemBatch) []dboper.Operation { size := len(batch.Put) + len(batch.Deleted) - ops := make([]Operation, 0, size) + ops := make([]dboper.Operation, 0, size) for i := range batch.Put { key := batch.Put[i].Key if len(key) == 0 || key[0] != byte(STStorage) && key[0] != byte(STTempStorage) { @@ -149,7 +141,7 @@ func BatchToOperations(batch *MemBatch) []Operation { op = "Changed" } - ops = append(ops, Operation{ + ops = append(ops, dboper.Operation{ State: op, Key: key[1:], Value: batch.Put[i].Value, @@ -163,7 +155,7 @@ func BatchToOperations(batch *MemBatch) []Operation { continue } - ops = append(ops, Operation{ + ops = append(ops, dboper.Operation{ State: "Deleted", Key: key[1:], }) diff --git a/pkg/core/storage/store_test.go b/pkg/core/storage/store_test.go index 7191e46ad..125ebb1ed 100644 --- a/pkg/core/storage/store_test.go +++ b/pkg/core/storage/store_test.go @@ -3,6 +3,7 @@ package storage import ( "testing" + "github.com/nspcc-dev/neo-go/pkg/core/storage/dboper" "github.com/stretchr/testify/require" ) @@ -19,7 +20,7 @@ func TestBatchToOperations(t *testing.T) { {KeyValue: KeyValue{Key: []byte{byte(STStorage), 0x06}, Value: []byte{0x06}}, Exists: true}, }, } - o := []Operation{ + o := []dboper.Operation{ {State: "Added", Key: []byte{0x01}, Value: []byte{0x01}}, {State: "Changed", Key: []byte{0x03}, Value: []byte{0x03}}, {State: "Deleted", Key: []byte{0x06}}, diff --git a/pkg/rpc/response/result/invoke.go b/pkg/rpc/response/result/invoke.go index e1073954c..e51cb4042 100644 --- a/pkg/rpc/response/result/invoke.go +++ b/pkg/rpc/response/result/invoke.go @@ -9,6 +9,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/core/interop/iterator" "github.com/nspcc-dev/neo-go/pkg/core/state" "github.com/nspcc-dev/neo-go/pkg/core/storage" + "github.com/nspcc-dev/neo-go/pkg/core/storage/dboper" "github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/vm/invocations" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" @@ -36,7 +37,7 @@ type RegisterIterator func(sessionID string, item stackitem.Item, id int, finali // InvokeDiag is an additional diagnostic data for invocation. type InvokeDiag struct { - Changes []storage.Operation `json:"storagechanges"` + Changes []dboper.Operation `json:"storagechanges"` Invocations []*invocations.Tree `json:"invokedcontracts"` } diff --git a/pkg/rpc/server/server_test.go b/pkg/rpc/server/server_test.go index b36c10f62..271d4688f 100644 --- a/pkg/rpc/server/server_test.go +++ b/pkg/rpc/server/server_test.go @@ -27,7 +27,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/core/fee" "github.com/nspcc-dev/neo-go/pkg/core/native/nativenames" "github.com/nspcc-dev/neo-go/pkg/core/state" - "github.com/nspcc-dev/neo-go/pkg/core/storage" + "github.com/nspcc-dev/neo-go/pkg/core/storage/dboper" "github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/crypto/hash" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" @@ -926,7 +926,7 @@ var rpcTestCases = map[string][]rpcTestCase{ assert.Equal(t, "HALT", res.State) assert.Equal(t, []stackitem.Item{stackitem.Make(true)}, res.Stack) assert.NotEqual(t, 0, res.GasConsumed) - chg := []storage.Operation{{ + chg := []dboper.Operation{{ State: "Changed", Key: []byte{0xfa, 0xff, 0xff, 0xff, 0xb}, Value: []byte{0x70, 0xd9, 0x59, 0x9d, 0x51, 0x79, 0x12}, @@ -961,7 +961,7 @@ var rpcTestCases = map[string][]rpcTestCase{ Stack: []stackitem.Item{stackitem.Make("1.2.3.4")}, Notifications: []state.NotificationEvent{}, Diagnostics: &result.InvokeDiag{ - Changes: []storage.Operation{}, + Changes: []dboper.Operation{}, Invocations: []*invocations.Tree{{ Current: hash.Hash160(script), Calls: []*invocations.Tree{ @@ -1074,7 +1074,7 @@ var rpcTestCases = map[string][]rpcTestCase{ Stack: []stackitem.Item{stackitem.Make("1.2.3.4")}, Notifications: []state.NotificationEvent{}, Diagnostics: &result.InvokeDiag{ - Changes: []storage.Operation{}, + Changes: []dboper.Operation{}, Invocations: []*invocations.Tree{{ Current: hash.Hash160(script), Calls: []*invocations.Tree{ @@ -1166,7 +1166,7 @@ var rpcTestCases = map[string][]rpcTestCase{ FaultException: "at instruction 0 (ROT): too big index", Notifications: []state.NotificationEvent{}, Diagnostics: &result.InvokeDiag{ - Changes: []storage.Operation{}, + Changes: []dboper.Operation{}, Invocations: []*invocations.Tree{{ Current: hash.Hash160(script), }}, @@ -1277,7 +1277,7 @@ var rpcTestCases = map[string][]rpcTestCase{ FaultException: "at instruction 0 (ROT): too big index", Notifications: []state.NotificationEvent{}, Diagnostics: &result.InvokeDiag{ - Changes: []storage.Operation{}, + Changes: []dboper.Operation{}, Invocations: []*invocations.Tree{{ Current: hash.Hash160(script), }},