core: do not copy key/value results got from MemCachedStore.Seek

They are already copied inside the MemCachedStore.Seek, so that
persistent storage  can't change them anymore.
This commit is contained in:
Anna Shaleva 2021-09-24 18:44:42 +03:00
parent 72726d46d3
commit f2ac07a3c0
3 changed files with 10 additions and 18 deletions

View file

@ -490,9 +490,8 @@ func (bc *Blockchain) jumpToStateInternal(p uint32, stage stateJumpStage) error
// Firstly, remove all old genesis-related items. // Firstly, remove all old genesis-related items.
b := bc.dao.Store.Batch() b := bc.dao.Store.Batch()
bc.dao.Store.Seek([]byte{byte(storage.STStorage)}, func(k, _ []byte) { bc.dao.Store.Seek([]byte{byte(storage.STStorage)}, func(k, _ []byte) {
// Must copy here, #1468. // #1468, but don't need to copy here, because it is done by Store.
key := slice.Copy(k) b.Delete(k)
b.Delete(key)
}) })
b.Put(jumpStageKey, []byte{byte(oldStorageItemsRemoved)}) b.Put(jumpStageKey, []byte{byte(oldStorageItemsRemoved)})
err := bc.dao.Store.PutBatch(b) err := bc.dao.Store.PutBatch(b)
@ -509,14 +508,12 @@ func (bc *Blockchain) jumpToStateInternal(p uint32, stage stateJumpStage) error
if count >= maxStorageBatchSize { if count >= maxStorageBatchSize {
return return
} }
// Must copy here, #1468. // #1468, but don't need to copy here, because it is done by Store.
oldKey := slice.Copy(k) b.Delete(k)
b.Delete(oldKey)
key := make([]byte, len(k)) key := make([]byte, len(k))
key[0] = byte(storage.STStorage) key[0] = byte(storage.STStorage)
copy(key[1:], k[1:]) copy(key[1:], k[1:])
value := slice.Copy(v) b.Put(key, slice.Copy(v))
b.Put(key, value)
count += 2 count += 2
}) })
if count > 0 { if count > 0 {

View file

@ -16,7 +16,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger" "github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/util/slice"
) )
// HasTransaction errors. // HasTransaction errors.
@ -324,12 +323,10 @@ func (dao *Simple) GetStorageItemsWithPrefix(id int32, prefix []byte) ([]state.S
saveToArr := func(k, v []byte) { saveToArr := func(k, v []byte) {
// Cut prefix and hash. // Cut prefix and hash.
// Must copy here, #1468. // #1468, but don't need to copy here, because it is done by Store.
key := slice.Copy(k)
val := slice.Copy(v)
siArr = append(siArr, state.StorageItemWithKey{ siArr = append(siArr, state.StorageItemWithKey{
Key: key, Key: k,
Item: state.StorageItem(val), Item: state.StorageItem(v),
}) })
} }
dao.Seek(id, prefix, saveToArr) dao.Seek(id, prefix, saveToArr)

View file

@ -13,7 +13,6 @@ import (
"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/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/util/slice"
"go.uber.org/atomic" "go.uber.org/atomic"
"go.uber.org/zap" "go.uber.org/zap"
) )
@ -147,9 +146,8 @@ func (s *Module) CleanStorage() error {
// //
b := s.Store.Batch() b := s.Store.Batch()
s.Store.Seek([]byte{byte(storage.DataMPT)}, func(k, _ []byte) { s.Store.Seek([]byte{byte(storage.DataMPT)}, func(k, _ []byte) {
// Must copy here, #1468. // #1468, but don't need to copy here, because it is done by Store.
key := slice.Copy(k) b.Delete(k)
b.Delete(key)
}) })
err = s.Store.PutBatch(b) err = s.Store.PutBatch(b)
if err != nil { if err != nil {