From ccdda217183d0ee4b104b9e1e12816a29a9b4373 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Sat, 12 Feb 2022 22:48:16 +0300 Subject: [PATCH] stateroot: add and use DataMPTAux for auxiliary data Use DataMPT for nodes only, otherwise with 1M blocks with have 1M height-stateroot mapping entries that our GC has to iterate over for no reason. --- pkg/core/blockchain.go | 2 +- pkg/core/stateroot/module.go | 14 ++------------ pkg/core/stateroot/store.go | 6 +++--- pkg/core/storage/store.go | 12 ++++++++---- 4 files changed, 14 insertions(+), 20 deletions(-) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 69863d652..2d7d4345f 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -44,7 +44,7 @@ import ( // Tuning parameters. const ( headerBatchCount = 2000 - version = "0.2.2" + version = "0.2.3" defaultInitialGAS = 52000000_00000000 defaultGCPeriod = 10000 diff --git a/pkg/core/stateroot/module.go b/pkg/core/stateroot/module.go index 8c73d7e28..6b28e9c21 100644 --- a/pkg/core/stateroot/module.go +++ b/pkg/core/stateroot/module.go @@ -118,7 +118,7 @@ func (s *Module) CurrentValidatedHeight() uint32 { // Init initializes state root module at the given height. func (s *Module) Init(height uint32) error { - data, err := s.Store.Get([]byte{byte(storage.DataMPT), prefixValidated}) + data, err := s.Store.Get([]byte{byte(storage.DataMPTAux), prefixValidated}) if err == nil { s.validatedHeight.Store(binary.LittleEndian.Uint32(data)) } @@ -156,16 +156,6 @@ func (s *Module) CleanStorage() error { if err != nil { return fmt.Errorf("failed to remove outdated MPT-reated items: %w", err) } - currentLocal := s.currentLocal.Load().(util.Uint256) - if !currentLocal.Equals(util.Uint256{}) { - err := s.addLocalStateRoot(s.Store, &state.MPTRoot{ - Index: s.localHeight.Load(), - Root: currentLocal, - }) - if err != nil { - return fmt.Errorf("failed to store current local stateroot: %w", err) - } - } return nil } @@ -177,7 +167,7 @@ func (s *Module) JumpToState(sr *state.MPTRoot) error { data := make([]byte, 4) binary.LittleEndian.PutUint32(data, sr.Index) - if err := s.Store.Put([]byte{byte(storage.DataMPT), prefixValidated}, data); err != nil { + if err := s.Store.Put([]byte{byte(storage.DataMPTAux), prefixValidated}, data); err != nil { return fmt.Errorf("failed to store validated height: %w", err) } s.validatedHeight.Store(sr.Index) diff --git a/pkg/core/stateroot/store.go b/pkg/core/stateroot/store.go index f059d1c31..6c1494cc2 100644 --- a/pkg/core/stateroot/store.go +++ b/pkg/core/stateroot/store.go @@ -29,7 +29,7 @@ func (s *Module) addLocalStateRoot(store *storage.MemCachedStore, sr *state.MPTR data := make([]byte, 4) binary.LittleEndian.PutUint32(data, sr.Index) - return store.Put([]byte{byte(storage.DataMPT), prefixLocal}, data) + return store.Put([]byte{byte(storage.DataMPTAux), prefixLocal}, data) } func putStateRoot(store *storage.MemCachedStore, key []byte, sr *state.MPTRoot) error { @@ -52,7 +52,7 @@ func (s *Module) getStateRoot(key []byte) (*state.MPTRoot, error) { func makeStateRootKey(index uint32) []byte { key := make([]byte, 5) - key[0] = byte(storage.DataMPT) + key[0] = byte(storage.DataMPTAux) binary.BigEndian.PutUint32(key, index) return key } @@ -79,7 +79,7 @@ func (s *Module) AddStateRoot(sr *state.MPTRoot) error { data := make([]byte, 4) binary.LittleEndian.PutUint32(data, sr.Index) - if err := s.Store.Put([]byte{byte(storage.DataMPT), prefixValidated}, data); err != nil { + if err := s.Store.Put([]byte{byte(storage.DataMPTAux), prefixValidated}, data); err != nil { return err } s.validatedHeight.Store(sr.Index) diff --git a/pkg/core/storage/store.go b/pkg/core/storage/store.go index 029d21fbf..de5e14ec4 100644 --- a/pkg/core/storage/store.go +++ b/pkg/core/storage/store.go @@ -11,10 +11,14 @@ import ( // KeyPrefix constants. const ( DataExecutable KeyPrefix = 0x01 - DataMPT KeyPrefix = 0x03 - STAccount KeyPrefix = 0x40 - STContractID KeyPrefix = 0x51 - STStorage KeyPrefix = 0x70 + // DataMPT is used for MPT node entries identified by Uint256. + DataMPT KeyPrefix = 0x03 + // DataMPTAux is used to store additional MPT data like height-root + // mappings and local/validated heights. + DataMPTAux KeyPrefix = 0x04 + STAccount KeyPrefix = 0x40 + STContractID KeyPrefix = 0x51 + STStorage KeyPrefix = 0x70 // STTempStorage is used to store contract storage items during state sync process // in order not to mess up the previous state which has its own items stored by // STStorage prefix. Once state exchange process is completed, all items with