Merge pull request #1097 from nspcc-dev/fix/mpt

core,dao: use MPT only if it is enabled in config
This commit is contained in:
Roman Khimov 2020-06-24 16:18:59 +03:00 committed by GitHub
commit 3ccf19fc1e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 7 deletions

View file

@ -214,6 +214,11 @@ func (bc *Blockchain) init() error {
if err != nil {
return err
}
if bc.config.EnableStateRoot {
if err := bc.dao.InitMPT(0); err != nil {
return err
}
}
return bc.storeBlock(genesisBlock)
}
if ver != version {

View file

@ -81,7 +81,7 @@ type Simple struct {
// NewSimple creates new simple dao using provided backend store.
func NewSimple(backend storage.Store) *Simple {
st := storage.NewMemCachedStore(backend)
return &Simple{Store: st, MPT: mpt.NewTrie(nil, st)}
return &Simple{Store: st}
}
// GetBatch returns currently accumulated DB changeset.
@ -492,11 +492,13 @@ func (dao *Simple) GetStorageItem(scripthash util.Uint160, key []byte) *state.St
// key into the given store.
func (dao *Simple) PutStorageItem(scripthash util.Uint160, key []byte, si *state.StorageItem) error {
stKey := makeStorageItemKey(scripthash, key)
k := mpt.ToNeoStorageKey(stKey[1:]) // strip STStorage prefix
v := mpt.ToNeoStorageValue(si)
if dao.MPT != nil {
k := mpt.ToNeoStorageKey(stKey[1:]) // strip STStorage prefix
if err := dao.MPT.Put(k, v); err != nil && err != mpt.ErrNotFound {
return err
}
}
return dao.Store.Put(stKey, v[1:])
}
@ -504,10 +506,12 @@ func (dao *Simple) PutStorageItem(scripthash util.Uint160, key []byte, si *state
// given key from the store.
func (dao *Simple) DeleteStorageItem(scripthash util.Uint160, key []byte) error {
stKey := makeStorageItemKey(scripthash, key)
if dao.MPT != nil {
k := mpt.ToNeoStorageKey(stKey[1:]) // strip STStorage prefix
if err := dao.MPT.Delete(k); err != nil && err != mpt.ErrNotFound {
return err
}
}
return dao.Store.Delete(stKey)
}