dao: serialize state.StorageItem as raw bytes

This commit is contained in:
Evgeniy Stratonikov 2021-03-09 12:09:44 +03:00
parent 55698d0426
commit e551432b30
7 changed files with 8 additions and 84 deletions

View file

@ -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)

View file

@ -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

View file

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

View file

@ -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)
}
}
}

View file

@ -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()
}

View file

@ -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))
}

View file

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