core: move StorageItem-related constants to storage pkg

Need this to avoid import cycle problem.
This commit is contained in:
Anna Shaleva 2021-03-30 16:47:15 +03:00
parent ec19a087bb
commit f0c222b385
3 changed files with 15 additions and 13 deletions

View file

@ -9,6 +9,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/core/native"
"github.com/nspcc-dev/neo-go/pkg/core/storage"
"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"
@ -16,14 +17,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)
const (
// MaxStorageKeyLen is the maximum length of a key for storage items.
MaxStorageKeyLen = 64
// MaxStorageValueLen is the maximum length of a value for storage items.
// It is set to be the maximum value for uint16.
MaxStorageValueLen = 65535
)
// StorageContext contains storing id and read/write flag, it's used as
// a context for storage manipulation functions.
type StorageContext struct {
@ -104,10 +97,10 @@ func storageGetContextInternal(ic *interop.Context, isReadOnly bool) error {
}
func putWithContext(ic *interop.Context, stc *StorageContext, key []byte, value []byte) error {
if len(key) > MaxStorageKeyLen {
if len(key) > storage.MaxStorageKeyLen {
return errors.New("key is too big")
}
if len(value) > MaxStorageValueLen {
if len(value) > storage.MaxStorageValueLen {
return errors.New("value is too big")
}
if stc.ReadOnly {

View file

@ -13,6 +13,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/interop/runtime"
"github.com/nspcc-dev/neo-go/pkg/core/native"
"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/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
@ -198,7 +199,7 @@ func TestStoragePut(t *testing.T) {
})
t.Run("check limits", func(t *testing.T) {
initVM(t, make([]byte, MaxStorageKeyLen), make([]byte, MaxStorageValueLen), -1)
initVM(t, make([]byte, storage.MaxStorageKeyLen), make([]byte, storage.MaxStorageValueLen), -1)
require.NoError(t, storagePut(ic))
})
@ -209,11 +210,11 @@ func TestStoragePut(t *testing.T) {
require.Error(t, storagePut(ic))
})
t.Run("big key", func(t *testing.T) {
initVM(t, make([]byte, MaxStorageKeyLen+1), []byte{1}, -1)
initVM(t, make([]byte, storage.MaxStorageKeyLen+1), []byte{1}, -1)
require.Error(t, storagePut(ic))
})
t.Run("big value", func(t *testing.T) {
initVM(t, []byte{1}, make([]byte, MaxStorageValueLen+1), -1)
initVM(t, []byte{1}, make([]byte, storage.MaxStorageValueLen+1), -1)
require.Error(t, storagePut(ic))
})
})

View file

@ -23,6 +23,14 @@ const (
SYSVersion KeyPrefix = 0xf0
)
const (
// MaxStorageKeyLen is the maximum length of a key for storage items.
MaxStorageKeyLen = 64
// MaxStorageValueLen is the maximum length of a value for storage items.
// It is set to be the maximum value for uint16.
MaxStorageValueLen = 65535
)
// ErrKeyNotFound is an error returned by Store implementations
// when a certain key is not found.
var ErrKeyNotFound = errors.New("key not found")