Skip to content

Commit

Permalink
add the trieLeavesRetriever to the stateComponents
Browse files Browse the repository at this point in the history
  • Loading branch information
BeniaminDrasovean committed Jan 9, 2025
1 parent 78b58db commit 34cae32
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 6 deletions.
4 changes: 4 additions & 0 deletions cmd/node/config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,10 @@
MaxPeerTrieLevelInMemory = 5
StateStatisticsEnabled = false

[TrieLeavesRetrieverConfig]
Enabled = false
MaxSizeInBytes = 104857600 #100MB

[BlockSizeThrottleConfig]
MinSizeInBytes = 104857 # 104857 is 10% from 1MB
MaxSizeInBytes = 943718 # 943718 is 90% from 1MB
Expand Down
19 changes: 13 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,13 @@ type Config struct {
BootstrapStorage StorageConfig
MetaBlockStorage StorageConfig

AccountsTrieStorage StorageConfig
PeerAccountsTrieStorage StorageConfig
EvictionWaitingList EvictionWaitingListConfig
StateTriesConfig StateTriesConfig
TrieStorageManagerConfig TrieStorageManagerConfig
BadBlocksCache CacheConfig
AccountsTrieStorage StorageConfig
PeerAccountsTrieStorage StorageConfig
EvictionWaitingList EvictionWaitingListConfig
StateTriesConfig StateTriesConfig
TrieStorageManagerConfig TrieStorageManagerConfig
TrieLeavesRetrieverConfig TrieLeavesRetrieverConfig
BadBlocksCache CacheConfig

TxBlockBodyDataPool CacheConfig
PeerBlockBodyDataPool CacheConfig
Expand Down Expand Up @@ -640,3 +641,9 @@ type PoolsCleanersConfig struct {
type RedundancyConfig struct {
MaxRoundsOfInactivityAccepted int
}

// TrieLeavesRetrieverConfig represents the config options to be used when setting up the trie leaves retriever
type TrieLeavesRetrieverConfig struct {
Enabled bool
MaxSizeInBytes uint64
}
3 changes: 3 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,3 +598,6 @@ var ErrNilSentSignatureTracker = errors.New("nil sent signature tracker")

// ErrNilEpochSystemSCProcessor defines the error for setting a nil EpochSystemSCProcessor
var ErrNilEpochSystemSCProcessor = errors.New("nil epoch system SC processor")

// ErrNilTrieLeavesRetriever defines the error for setting a nil TrieLeavesRetriever
var ErrNilTrieLeavesRetriever = errors.New("nil trie leaves retriever")
1 change: 1 addition & 0 deletions factory/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ type StateComponentsHolder interface {
TriesContainer() common.TriesHolder
TrieStorageManagers() map[string]common.StorageManager
MissingTrieNodesNotifier() common.MissingTrieNodesNotifier
TrieLeavesRetriever() common.TrieLeavesRetriever
Close() error
IsInterfaceNil() bool
}
Expand Down
9 changes: 9 additions & 0 deletions factory/mock/stateComponentsHolderStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type StateComponentsHolderStub struct {
TriesContainerCalled func() common.TriesHolder
TrieStorageManagersCalled func() map[string]common.StorageManager
MissingTrieNodesNotifierCalled func() common.MissingTrieNodesNotifier
TrieLeavesRetrieverCalled func() common.TrieLeavesRetriever
}

// PeerAccounts -
Expand Down Expand Up @@ -79,6 +80,14 @@ func (s *StateComponentsHolderStub) MissingTrieNodesNotifier() common.MissingTri
return nil
}

// TrieLeavesRetriever -
func (s *StateComponentsHolderStub) TrieLeavesRetriever() common.TrieLeavesRetriever {
if s.TrieLeavesRetrieverCalled != nil {
return s.TrieLeavesRetrieverCalled()
}
return nil
}

// Close -
func (s *StateComponentsHolderStub) Close() error {
return nil
Expand Down
21 changes: 21 additions & 0 deletions factory/state/stateComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/multiversx/mx-chain-go/state/storagePruningManager/evictionWaitingList"
"github.com/multiversx/mx-chain-go/state/syncer"
trieFactory "github.com/multiversx/mx-chain-go/trie/factory"
"github.com/multiversx/mx-chain-go/trie/leavesRetriever"
)

// TODO: merge this with data components
Expand Down Expand Up @@ -53,6 +54,7 @@ type stateComponents struct {
triesContainer common.TriesHolder
trieStorageManagers map[string]common.StorageManager
missingTrieNodesNotifier common.MissingTrieNodesNotifier
trieLeavesRetriever common.TrieLeavesRetriever
}

// NewStateComponentsFactory will return a new instance of stateComponentsFactory
Expand Down Expand Up @@ -100,6 +102,11 @@ func (scf *stateComponentsFactory) Create() (*stateComponents, error) {
return nil, err
}

trieLeavesRetriever, err := scf.createTrieLeavesRetriever(trieStorageManagers[dataRetriever.UserAccountsUnit.String()])
if err != nil {
return nil, err
}

return &stateComponents{
peerAccounts: peerAdapter,
accountsAdapter: accountsAdapter,
Expand All @@ -108,9 +115,23 @@ func (scf *stateComponentsFactory) Create() (*stateComponents, error) {
triesContainer: triesContainer,
trieStorageManagers: trieStorageManagers,
missingTrieNodesNotifier: syncer.NewMissingTrieNodesNotifier(),
trieLeavesRetriever: trieLeavesRetriever,
}, nil
}

func (scf *stateComponentsFactory) createTrieLeavesRetriever(trieStorage common.TrieStorageInteractor) (common.TrieLeavesRetriever, error) {
if !scf.config.TrieLeavesRetrieverConfig.Enabled {
return leavesRetriever.NewDisabledLeavesRetriever(), nil
}

return leavesRetriever.NewLeavesRetriever(
trieStorage,
scf.core.InternalMarshalizer(),
scf.core.Hasher(),
scf.config.TrieLeavesRetrieverConfig.MaxSizeInBytes,
)
}

func (scf *stateComponentsFactory) createSnapshotManager(
accountFactory state.AccountFactory,
stateMetrics state.StateMetrics,
Expand Down
15 changes: 15 additions & 0 deletions factory/state/stateComponentsHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ func (msc *managedStateComponents) CheckSubcomponents() error {
if check.IfNil(msc.missingTrieNodesNotifier) {
return errors.ErrNilMissingTrieNodesNotifier
}
if check.IfNil(msc.trieLeavesRetriever) {
return errors.ErrNilTrieLeavesRetriever
}

return nil
}
Expand Down Expand Up @@ -214,6 +217,18 @@ func (msc *managedStateComponents) MissingTrieNodesNotifier() common.MissingTrie
return msc.stateComponents.missingTrieNodesNotifier
}

// TrieLeavesRetriever returns the trie leaves retriever
func (msc *managedStateComponents) TrieLeavesRetriever() common.TrieLeavesRetriever {
msc.mutStateComponents.RLock()
defer msc.mutStateComponents.RUnlock()

if msc.stateComponents == nil {
return nil
}

return msc.stateComponents.trieLeavesRetriever
}

// IsInterfaceNil returns true if the interface is nil
func (msc *managedStateComponents) IsInterfaceNil() bool {
return msc == nil
Expand Down
7 changes: 7 additions & 0 deletions node/chainSimulator/components/stateComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type stateComponentsHolder struct {
triesContainer common.TriesHolder
triesStorageManager map[string]common.StorageManager
missingTrieNodesNotifier common.MissingTrieNodesNotifier
trieLeavesRetriever common.TrieLeavesRetriever
stateComponentsCloser io.Closer
}

Expand Down Expand Up @@ -70,6 +71,7 @@ func CreateStateComponents(args ArgsStateComponents) (*stateComponentsHolder, er
triesContainer: stateComp.TriesContainer(),
triesStorageManager: stateComp.TrieStorageManagers(),
missingTrieNodesNotifier: stateComp.MissingTrieNodesNotifier(),
trieLeavesRetriever: stateComp.TrieLeavesRetriever(),
stateComponentsCloser: stateComp,
}, nil
}
Expand Down Expand Up @@ -109,6 +111,11 @@ func (s *stateComponentsHolder) MissingTrieNodesNotifier() common.MissingTrieNod
return s.missingTrieNodesNotifier
}

// TrieLeavesRetriever will return the trie leaves retriever
func (s *stateComponentsHolder) TrieLeavesRetriever() common.TrieLeavesRetriever {
return s.trieLeavesRetriever
}

// Close will close the state components
func (s *stateComponentsHolder) Close() error {
return s.stateComponentsCloser.Close()
Expand Down
7 changes: 7 additions & 0 deletions testscommon/factory/stateComponentsMock.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type StateComponentsMock struct {
Tries common.TriesHolder
StorageManagers map[string]common.StorageManager
MissingNodesNotifier common.MissingTrieNodesNotifier
LeavesRetriever common.TrieLeavesRetriever
}

// NewStateComponentsMockFromRealComponent -
Expand All @@ -28,6 +29,7 @@ func NewStateComponentsMockFromRealComponent(stateComponents factory.StateCompon
Tries: stateComponents.TriesContainer(),
StorageManagers: stateComponents.TrieStorageManagers(),
MissingNodesNotifier: stateComponents.MissingTrieNodesNotifier(),
LeavesRetriever: stateComponents.TrieLeavesRetriever(),
}
}

Expand Down Expand Up @@ -89,6 +91,11 @@ func (scm *StateComponentsMock) MissingTrieNodesNotifier() common.MissingTrieNod
return scm.MissingNodesNotifier
}

// TrieLeavesRetriever -
func (scm *StateComponentsMock) TrieLeavesRetriever() common.TrieLeavesRetriever {
return scm.LeavesRetriever
}

// IsInterfaceNil -
func (scm *StateComponentsMock) IsInterfaceNil() bool {
return scm == nil
Expand Down
20 changes: 20 additions & 0 deletions trie/leavesRetriever/disabledLeavesRetriever.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package leavesRetriever

import "context"

type disabledLeavesRetriever struct{}

// NewDisabledLeavesRetriever creates a new disabled leaves retriever
func NewDisabledLeavesRetriever() *disabledLeavesRetriever {
return &disabledLeavesRetriever{}
}

// GetLeaves returns an empty map and a nil byte slice for this implementation
func (dlr *disabledLeavesRetriever) GetLeaves(_ int, _ []byte, _ []byte, _ context.Context) (map[string]string, []byte, error) {
return make(map[string]string), []byte{}, nil
}

// IsInterfaceNil returns true if there is no value under the interface
func (dlr *disabledLeavesRetriever) IsInterfaceNil() bool {
return dlr == nil
}

0 comments on commit 34cae32

Please sign in to comment.