storage: move Operation into package of its own

Don't use storage.* types in rpc/response/result.
This commit is contained in:
Roman Khimov 2022-07-08 21:17:52 +03:00
parent 1e62474514
commit 96c4e61063
6 changed files with 33 additions and 25 deletions

View file

@ -7,14 +7,15 @@ import (
"path/filepath" "path/filepath"
"github.com/nspcc-dev/neo-go/pkg/core/storage" "github.com/nspcc-dev/neo-go/pkg/core/storage"
"github.com/nspcc-dev/neo-go/pkg/core/storage/dboper"
) )
type dump []blockDump type dump []blockDump
type blockDump struct { type blockDump struct {
Block uint32 `json:"block"` Block uint32 `json:"block"`
Size int `json:"size"` Size int `json:"size"`
Storage []storage.Operation `json:"storage"` Storage []dboper.Operation `json:"storage"`
} }
func newDump() *dump { func newDump() *dump {

View file

@ -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"`
}

View file

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"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/core/storage/dboper"
"github.com/syndtr/goleveldb/leveldb/util" "github.com/syndtr/goleveldb/leveldb/util"
) )
@ -41,15 +42,6 @@ const (
ExecTransaction byte = 2 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. // SeekRange represents options for Store.Seek operation.
type SeekRange struct { type SeekRange struct {
// Prefix denotes the Seek's lookup key. // Prefix denotes the Seek's lookup key.
@ -134,10 +126,10 @@ func NewStore(cfg dbconfig.DBConfiguration) (Store, error) {
return store, err return store, err
} }
// BatchToOperations converts a batch of changes into array of Operations. // BatchToOperations converts a batch of changes into array of dboper.Operation.
func BatchToOperations(batch *MemBatch) []Operation { func BatchToOperations(batch *MemBatch) []dboper.Operation {
size := len(batch.Put) + len(batch.Deleted) size := len(batch.Put) + len(batch.Deleted)
ops := make([]Operation, 0, size) ops := make([]dboper.Operation, 0, size)
for i := range batch.Put { for i := range batch.Put {
key := batch.Put[i].Key key := batch.Put[i].Key
if len(key) == 0 || key[0] != byte(STStorage) && key[0] != byte(STTempStorage) { if len(key) == 0 || key[0] != byte(STStorage) && key[0] != byte(STTempStorage) {
@ -149,7 +141,7 @@ func BatchToOperations(batch *MemBatch) []Operation {
op = "Changed" op = "Changed"
} }
ops = append(ops, Operation{ ops = append(ops, dboper.Operation{
State: op, State: op,
Key: key[1:], Key: key[1:],
Value: batch.Put[i].Value, Value: batch.Put[i].Value,
@ -163,7 +155,7 @@ func BatchToOperations(batch *MemBatch) []Operation {
continue continue
} }
ops = append(ops, Operation{ ops = append(ops, dboper.Operation{
State: "Deleted", State: "Deleted",
Key: key[1:], Key: key[1:],
}) })

View file

@ -3,6 +3,7 @@ package storage
import ( import (
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/core/storage/dboper"
"github.com/stretchr/testify/require" "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}, {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: "Added", Key: []byte{0x01}, Value: []byte{0x01}},
{State: "Changed", Key: []byte{0x03}, Value: []byte{0x03}}, {State: "Changed", Key: []byte{0x03}, Value: []byte{0x03}},
{State: "Deleted", Key: []byte{0x06}}, {State: "Deleted", Key: []byte{0x06}},

View file

@ -9,6 +9,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/interop/iterator" "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/state"
"github.com/nspcc-dev/neo-go/pkg/core/storage" "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/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/vm/invocations" "github.com/nspcc-dev/neo-go/pkg/vm/invocations"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "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. // InvokeDiag is an additional diagnostic data for invocation.
type InvokeDiag struct { type InvokeDiag struct {
Changes []storage.Operation `json:"storagechanges"` Changes []dboper.Operation `json:"storagechanges"`
Invocations []*invocations.Tree `json:"invokedcontracts"` Invocations []*invocations.Tree `json:"invokedcontracts"`
} }

View file

@ -27,7 +27,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/fee" "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/native/nativenames"
"github.com/nspcc-dev/neo-go/pkg/core/state" "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/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash" "github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "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, "HALT", res.State)
assert.Equal(t, []stackitem.Item{stackitem.Make(true)}, res.Stack) assert.Equal(t, []stackitem.Item{stackitem.Make(true)}, res.Stack)
assert.NotEqual(t, 0, res.GasConsumed) assert.NotEqual(t, 0, res.GasConsumed)
chg := []storage.Operation{{ chg := []dboper.Operation{{
State: "Changed", State: "Changed",
Key: []byte{0xfa, 0xff, 0xff, 0xff, 0xb}, Key: []byte{0xfa, 0xff, 0xff, 0xff, 0xb},
Value: []byte{0x70, 0xd9, 0x59, 0x9d, 0x51, 0x79, 0x12}, 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")}, Stack: []stackitem.Item{stackitem.Make("1.2.3.4")},
Notifications: []state.NotificationEvent{}, Notifications: []state.NotificationEvent{},
Diagnostics: &result.InvokeDiag{ Diagnostics: &result.InvokeDiag{
Changes: []storage.Operation{}, Changes: []dboper.Operation{},
Invocations: []*invocations.Tree{{ Invocations: []*invocations.Tree{{
Current: hash.Hash160(script), Current: hash.Hash160(script),
Calls: []*invocations.Tree{ Calls: []*invocations.Tree{
@ -1074,7 +1074,7 @@ var rpcTestCases = map[string][]rpcTestCase{
Stack: []stackitem.Item{stackitem.Make("1.2.3.4")}, Stack: []stackitem.Item{stackitem.Make("1.2.3.4")},
Notifications: []state.NotificationEvent{}, Notifications: []state.NotificationEvent{},
Diagnostics: &result.InvokeDiag{ Diagnostics: &result.InvokeDiag{
Changes: []storage.Operation{}, Changes: []dboper.Operation{},
Invocations: []*invocations.Tree{{ Invocations: []*invocations.Tree{{
Current: hash.Hash160(script), Current: hash.Hash160(script),
Calls: []*invocations.Tree{ Calls: []*invocations.Tree{
@ -1166,7 +1166,7 @@ var rpcTestCases = map[string][]rpcTestCase{
FaultException: "at instruction 0 (ROT): too big index", FaultException: "at instruction 0 (ROT): too big index",
Notifications: []state.NotificationEvent{}, Notifications: []state.NotificationEvent{},
Diagnostics: &result.InvokeDiag{ Diagnostics: &result.InvokeDiag{
Changes: []storage.Operation{}, Changes: []dboper.Operation{},
Invocations: []*invocations.Tree{{ Invocations: []*invocations.Tree{{
Current: hash.Hash160(script), Current: hash.Hash160(script),
}}, }},
@ -1277,7 +1277,7 @@ var rpcTestCases = map[string][]rpcTestCase{
FaultException: "at instruction 0 (ROT): too big index", FaultException: "at instruction 0 (ROT): too big index",
Notifications: []state.NotificationEvent{}, Notifications: []state.NotificationEvent{},
Diagnostics: &result.InvokeDiag{ Diagnostics: &result.InvokeDiag{
Changes: []storage.Operation{}, Changes: []dboper.Operation{},
Invocations: []*invocations.Tree{{ Invocations: []*invocations.Tree{{
Current: hash.Hash160(script), Current: hash.Hash160(script),
}}, }},