From e551432b3072b28fcfe80b531fe55b7f1075ae14 Mon Sep 17 00:00:00 2001 From: Evgeniy Stratonikov Date: Tue, 9 Mar 2021 12:09:44 +0300 Subject: [PATCH] dao: serialize `state.StorageItem` as raw bytes --- pkg/core/dao/dao.go | 31 ++++------------------------- pkg/core/native/management.go | 10 +--------- pkg/core/native/name_service.go | 8 +------- pkg/core/native/native_neo.go | 9 +-------- pkg/core/state/storage_item.go | 14 ------------- pkg/core/state/storage_item_test.go | 12 ----------- pkg/rpc/server/server.go | 8 +------- 7 files changed, 8 insertions(+), 84 deletions(-) delete mode 100644 pkg/core/state/storage_item_test.go diff --git a/pkg/core/dao/dao.go b/pkg/core/dao/dao.go index eed439aa1..af40a37f6 100644 --- a/pkg/core/dao/dao.go +++ b/pkg/core/dao/dao.go @@ -364,28 +364,14 @@ func (dao *Simple) GetStorageItem(id int32, key []byte) state.StorageItem { if err != nil { return nil } - r := io.NewBinReaderFromBuf(b) - - si := state.StorageItem{} - si.DecodeBinary(r) - if r.Err != nil { - return nil - } - - return si + return b } // PutStorageItem puts given StorageItem for given id with given // key into the given store. func (dao *Simple) PutStorageItem(id int32, key []byte, si state.StorageItem) error { stKey := makeStorageItemKey(id, key) - buf := io.NewBufBinWriter() - si.EncodeBinary(buf.BinWriter) - if buf.Err != nil { - return buf.Err - } - v := buf.Bytes() - return dao.Store.Put(stKey, v) + return dao.Store.Put(stKey, si) } // DeleteStorageItem drops storage item for the given id with the @@ -404,23 +390,14 @@ func (dao *Simple) GetStorageItems(id int32) (map[string]state.StorageItem, erro // given scripthash. func (dao *Simple) GetStorageItemsWithPrefix(id int32, prefix []byte) (map[string]state.StorageItem, error) { var siMap = make(map[string]state.StorageItem) - var err error saveToMap := func(k, v []byte) { - if err != nil { - return - } - r := io.NewBinReaderFromBuf(v) - si := state.StorageItem{} - si.DecodeBinary(r) - if r.Err != nil { - err = r.Err - return - } // Cut prefix and hash. // Must copy here, #1468. key := make([]byte, len(k)) copy(key, k) + si := make(state.StorageItem, len(v)) + copy(si, v) siMap[string(key)] = si } dao.Seek(id, prefix, saveToMap) diff --git a/pkg/core/native/management.go b/pkg/core/native/management.go index febf291cd..2e74518e8 100644 --- a/pkg/core/native/management.go +++ b/pkg/core/native/management.go @@ -480,16 +480,8 @@ func (m *Management) InitializeCache(d dao.DAO) error { var initErr error d.Seek(m.ID, []byte{prefixContract}, func(_, v []byte) { - var r = io.NewBinReaderFromBuf(v) - var si state.StorageItem - si.DecodeBinary(r) - if r.Err != nil { - initErr = r.Err - return - } - var cs state.Contract - r = io.NewBinReaderFromBuf(si) + r := io.NewBinReaderFromBuf(v) cs.DecodeBinary(r) if r.Err != nil { initErr = r.Err diff --git a/pkg/core/native/name_service.go b/pkg/core/native/name_service.go index aa61a1c11..23d263156 100644 --- a/pkg/core/native/name_service.go +++ b/pkg/core/native/name_service.go @@ -533,13 +533,7 @@ func (n *NameService) getRecordsInternal(d dao.DAO, name string) map[RecordType] res := make(map[RecordType]string) d.Seek(n.ID, key, func(k, v []byte) { rt := RecordType(k[len(k)-1]) - var si state.StorageItem - r := io.NewBinReaderFromBuf(v) - si.DecodeBinary(r) - if r.Err != nil { - panic(r.Err) - } - res[rt] = string(si) + res[rt] = string(v) }) return res } diff --git a/pkg/core/native/native_neo.go b/pkg/core/native/native_neo.go index 4f3760709..dc7868adb 100644 --- a/pkg/core/native/native_neo.go +++ b/pkg/core/native/native_neo.go @@ -20,7 +20,6 @@ import ( "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/encoding/bigint" - "github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag" "github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest" @@ -330,19 +329,13 @@ func (n *NEO) PostPersist(ic *interop.Context) error { func (n *NEO) getGASPerVote(d dao.DAO, key []byte, index ...uint32) []big.Int { var max = make([]uint32, len(index)) var reward = make([]big.Int, len(index)) - var si state.StorageItem d.Seek(n.ID, key, func(k, v []byte) { if len(k) == 4 { num := binary.BigEndian.Uint32(k) for i, ind := range index { if max[i] < num && num <= ind { max[i] = num - r := io.NewBinReaderFromBuf(v) - si.DecodeBinary(r) - if r.Err != nil { - return - } - reward[i] = *bigint.FromBytes(si) + reward[i] = *bigint.FromBytes(v) } } } diff --git a/pkg/core/state/storage_item.go b/pkg/core/state/storage_item.go index 3bda93595..ba9777cf8 100644 --- a/pkg/core/state/storage_item.go +++ b/pkg/core/state/storage_item.go @@ -1,18 +1,4 @@ package state -import ( - "github.com/nspcc-dev/neo-go/pkg/io" -) - // StorageItem is the value to be stored with read-only flag. type StorageItem []byte - -// EncodeBinary implements Serializable interface. -func (si *StorageItem) EncodeBinary(w *io.BinWriter) { - w.WriteVarBytes(*si) -} - -// DecodeBinary implements Serializable interface. -func (si *StorageItem) DecodeBinary(r *io.BinReader) { - *si = r.ReadVarBytes() -} diff --git a/pkg/core/state/storage_item_test.go b/pkg/core/state/storage_item_test.go deleted file mode 100644 index 204df1aac..000000000 --- a/pkg/core/state/storage_item_test.go +++ /dev/null @@ -1,12 +0,0 @@ -package state - -import ( - "testing" - - "github.com/nspcc-dev/neo-go/internal/testserdes" -) - -func TestEncodeDecodeStorageItem(t *testing.T) { - storageItem := &StorageItem{1, 2, 3} - testserdes.EncodeDecodeBinary(t, storageItem, new(StorageItem)) -} diff --git a/pkg/rpc/server/server.go b/pkg/rpc/server/server.go index 084c9a1cb..db22543d7 100644 --- a/pkg/rpc/server/server.go +++ b/pkg/rpc/server/server.go @@ -862,13 +862,7 @@ func (s *Server) verifyProof(ps request.Params) (interface{}, *response.Error) { vp := new(result.VerifyProof) val, ok := mpt.VerifyProof(root, p.Key, p.Proof) if ok { - var si state.StorageItem - r := io.NewBinReaderFromBuf(val) - si.DecodeBinary(r) - if r.Err != nil { - return nil, response.NewInternalServerError("invalid item in trie", r.Err) - } - vp.Value = si + vp.Value = val } return vp, nil }