forked from TrueCloudLab/neoneo-go
dao: serialize state.StorageItem
as raw bytes
This commit is contained in:
parent
55698d0426
commit
e551432b30
7 changed files with 8 additions and 84 deletions
|
@ -364,28 +364,14 @@ func (dao *Simple) GetStorageItem(id int32, key []byte) state.StorageItem {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
r := io.NewBinReaderFromBuf(b)
|
return b
|
||||||
|
|
||||||
si := state.StorageItem{}
|
|
||||||
si.DecodeBinary(r)
|
|
||||||
if r.Err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return si
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// PutStorageItem puts given StorageItem for given id with given
|
// PutStorageItem puts given StorageItem for given id with given
|
||||||
// key into the given store.
|
// key into the given store.
|
||||||
func (dao *Simple) PutStorageItem(id int32, key []byte, si state.StorageItem) error {
|
func (dao *Simple) PutStorageItem(id int32, key []byte, si state.StorageItem) error {
|
||||||
stKey := makeStorageItemKey(id, key)
|
stKey := makeStorageItemKey(id, key)
|
||||||
buf := io.NewBufBinWriter()
|
return dao.Store.Put(stKey, si)
|
||||||
si.EncodeBinary(buf.BinWriter)
|
|
||||||
if buf.Err != nil {
|
|
||||||
return buf.Err
|
|
||||||
}
|
|
||||||
v := buf.Bytes()
|
|
||||||
return dao.Store.Put(stKey, v)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteStorageItem drops storage item for the given id with the
|
// 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.
|
// given scripthash.
|
||||||
func (dao *Simple) GetStorageItemsWithPrefix(id int32, prefix []byte) (map[string]state.StorageItem, error) {
|
func (dao *Simple) GetStorageItemsWithPrefix(id int32, prefix []byte) (map[string]state.StorageItem, error) {
|
||||||
var siMap = make(map[string]state.StorageItem)
|
var siMap = make(map[string]state.StorageItem)
|
||||||
var err error
|
|
||||||
|
|
||||||
saveToMap := func(k, v []byte) {
|
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.
|
// Cut prefix and hash.
|
||||||
// Must copy here, #1468.
|
// Must copy here, #1468.
|
||||||
key := make([]byte, len(k))
|
key := make([]byte, len(k))
|
||||||
copy(key, k)
|
copy(key, k)
|
||||||
|
si := make(state.StorageItem, len(v))
|
||||||
|
copy(si, v)
|
||||||
siMap[string(key)] = si
|
siMap[string(key)] = si
|
||||||
}
|
}
|
||||||
dao.Seek(id, prefix, saveToMap)
|
dao.Seek(id, prefix, saveToMap)
|
||||||
|
|
|
@ -480,16 +480,8 @@ func (m *Management) InitializeCache(d dao.DAO) error {
|
||||||
|
|
||||||
var initErr error
|
var initErr error
|
||||||
d.Seek(m.ID, []byte{prefixContract}, func(_, v []byte) {
|
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
|
var cs state.Contract
|
||||||
r = io.NewBinReaderFromBuf(si)
|
r := io.NewBinReaderFromBuf(v)
|
||||||
cs.DecodeBinary(r)
|
cs.DecodeBinary(r)
|
||||||
if r.Err != nil {
|
if r.Err != nil {
|
||||||
initErr = r.Err
|
initErr = r.Err
|
||||||
|
|
|
@ -533,13 +533,7 @@ func (n *NameService) getRecordsInternal(d dao.DAO, name string) map[RecordType]
|
||||||
res := make(map[RecordType]string)
|
res := make(map[RecordType]string)
|
||||||
d.Seek(n.ID, key, func(k, v []byte) {
|
d.Seek(n.ID, key, func(k, v []byte) {
|
||||||
rt := RecordType(k[len(k)-1])
|
rt := RecordType(k[len(k)-1])
|
||||||
var si state.StorageItem
|
res[rt] = string(v)
|
||||||
r := io.NewBinReaderFromBuf(v)
|
|
||||||
si.DecodeBinary(r)
|
|
||||||
if r.Err != nil {
|
|
||||||
panic(r.Err)
|
|
||||||
}
|
|
||||||
res[rt] = string(si)
|
|
||||||
})
|
})
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,6 @@ import (
|
||||||
"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"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
|
"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"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
"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 {
|
func (n *NEO) getGASPerVote(d dao.DAO, key []byte, index ...uint32) []big.Int {
|
||||||
var max = make([]uint32, len(index))
|
var max = make([]uint32, len(index))
|
||||||
var reward = make([]big.Int, len(index))
|
var reward = make([]big.Int, len(index))
|
||||||
var si state.StorageItem
|
|
||||||
d.Seek(n.ID, key, func(k, v []byte) {
|
d.Seek(n.ID, key, func(k, v []byte) {
|
||||||
if len(k) == 4 {
|
if len(k) == 4 {
|
||||||
num := binary.BigEndian.Uint32(k)
|
num := binary.BigEndian.Uint32(k)
|
||||||
for i, ind := range index {
|
for i, ind := range index {
|
||||||
if max[i] < num && num <= ind {
|
if max[i] < num && num <= ind {
|
||||||
max[i] = num
|
max[i] = num
|
||||||
r := io.NewBinReaderFromBuf(v)
|
reward[i] = *bigint.FromBytes(v)
|
||||||
si.DecodeBinary(r)
|
|
||||||
if r.Err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
reward[i] = *bigint.FromBytes(si)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,4 @@
|
||||||
package state
|
package state
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
||||||
)
|
|
||||||
|
|
||||||
// StorageItem is the value to be stored with read-only flag.
|
// StorageItem is the value to be stored with read-only flag.
|
||||||
type StorageItem []byte
|
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()
|
|
||||||
}
|
|
||||||
|
|
|
@ -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))
|
|
||||||
}
|
|
|
@ -862,13 +862,7 @@ func (s *Server) verifyProof(ps request.Params) (interface{}, *response.Error) {
|
||||||
vp := new(result.VerifyProof)
|
vp := new(result.VerifyProof)
|
||||||
val, ok := mpt.VerifyProof(root, p.Key, p.Proof)
|
val, ok := mpt.VerifyProof(root, p.Key, p.Proof)
|
||||||
if ok {
|
if ok {
|
||||||
var si state.StorageItem
|
vp.Value = val
|
||||||
r := io.NewBinReaderFromBuf(val)
|
|
||||||
si.DecodeBinary(r)
|
|
||||||
if r.Err != nil {
|
|
||||||
return nil, response.NewInternalServerError("invalid item in trie", r.Err)
|
|
||||||
}
|
|
||||||
vp.Value = si
|
|
||||||
}
|
}
|
||||||
return vp, nil
|
return vp, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue