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 { if err != nil {
return err return err
} }
if bc.config.EnableStateRoot {
if err := bc.dao.InitMPT(0); err != nil {
return err
}
}
return bc.storeBlock(genesisBlock) return bc.storeBlock(genesisBlock)
} }
if ver != version { if ver != version {

View file

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