Skip to content

Commit

Permalink
Merge pull request #6495 from multiversx/update_enums_state
Browse files Browse the repository at this point in the history
updated core enums in state changes.
  • Loading branch information
cristure authored Nov 4, 2024
2 parents d892499 + 305041c commit 5450ae1
Show file tree
Hide file tree
Showing 35 changed files with 1,265 additions and 978 deletions.
1 change: 1 addition & 0 deletions cmd/node/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ GLOBAL OPTIONS:
--operation-mode operation mode String flag for specifying the desired operation mode(s) of the node, resulting in altering some configuration values accordingly. Possible values are: snapshotless-observer, full-archive, db-lookup-extension, historical-balances or `""` (empty). Multiple values can be separated via ,
--repopulate-tokens-supplies Boolean flag for repopulating the tokens supplies database. It will delete the current data, iterate over the entire trie and add he new obtained supplies
--p2p-prometheus-metrics Boolean option for enabling the /debug/metrics/prometheus route for p2p prometheus metrics
--state-changes-types-to-collect value String slice option for enabling collecting specified state changes types. Can be (READ, WRITE)
--help, -h show help
--version, -v print the version
Expand Down
4 changes: 3 additions & 1 deletion cmd/node/config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,9 @@
MaxStateTrieLevelInMemory = 5
MaxPeerTrieLevelInMemory = 5
StateStatisticsEnabled = false
CollectStateChangesEnabled = false
StateChangesTypesToCollect = []
StateChangesDataAnalysis = false
StateChangesPeerAccountsEnabled = false

[BlockSizeThrottleConfig]
MinSizeInBytes = 104857 # 104857 is 10% from 1MB
Expand Down
13 changes: 11 additions & 2 deletions cmd/node/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
"os"
"runtime"

logger "github.com/multiversx/mx-chain-logger-go"
"github.com/urfave/cli"

"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/common/operationmodes"
"github.com/multiversx/mx-chain-go/config"
"github.com/multiversx/mx-chain-go/facade"
logger "github.com/multiversx/mx-chain-logger-go"
"github.com/urfave/cli"
)

var (
Expand Down Expand Up @@ -408,6 +409,13 @@ var (
Name: "p2p-prometheus-metrics",
Usage: "Boolean option for enabling the /debug/metrics/prometheus route for p2p prometheus metrics",
}

// stateChangesTypesToCollect defines a flag for collecting specified types of state changes
// If enabled, it will override the configuration
stateChangesTypesToCollect = cli.StringSliceFlag{
Name: "state-changes-types-to-collect",
Usage: "String slice option for enabling collecting specified state changes types. Can be (READ, WRITE)",
}
)

func getFlags() []cli.Flag {
Expand Down Expand Up @@ -470,6 +478,7 @@ func getFlags() []cli.Flag {
operationMode,
repopulateTokensSupplies,
p2pPrometheusMetrics,
stateChangesTypesToCollect,
}
}

Expand Down
10 changes: 7 additions & 3 deletions cmd/node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (
"github.com/klauspost/cpuid/v2"
"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/core/check"
logger "github.com/multiversx/mx-chain-logger-go"
"github.com/multiversx/mx-chain-logger-go/file"
"github.com/urfave/cli"

"github.com/multiversx/mx-chain-go/cmd/node/factory"
"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/config"
"github.com/multiversx/mx-chain-go/config/overridableConfig"
"github.com/multiversx/mx-chain-go/node"
logger "github.com/multiversx/mx-chain-logger-go"
"github.com/multiversx/mx-chain-logger-go/file"
"github.com/urfave/cli"
// test point 1 for custom profiler
)

Expand Down Expand Up @@ -253,6 +254,9 @@ func readConfigs(ctx *cli.Context, log logger.Logger) (*config.Configs, error) {
if ctx.IsSet(identityFlagName.Name) {
preferencesConfig.Preferences.Identity = ctx.GlobalString(identityFlagName.Name)
}
if ctx.IsSet(stateChangesTypesToCollect.Name) {
generalConfig.StateTriesConfig.StateChangesTypesToCollect = ctx.GlobalStringSlice(stateChangesTypesToCollect.Name)
}

return &config.Configs{
GeneralConfig: generalConfig,
Expand Down
17 changes: 9 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,14 +308,15 @@ type FacadeConfig struct {

// StateTriesConfig will hold information about state tries
type StateTriesConfig struct {
SnapshotsEnabled bool
AccountsStatePruningEnabled bool
PeerStatePruningEnabled bool
CollectStateChangesEnabled bool
CollectStateChangesWithReadEnabled bool
MaxStateTrieLevelInMemory uint
MaxPeerTrieLevelInMemory uint
StateStatisticsEnabled bool
SnapshotsEnabled bool
AccountsStatePruningEnabled bool
PeerStatePruningEnabled bool
StateChangesDataAnalysis bool
StateChangesTypesToCollect []string
StateChangesPeerAccountsEnabled bool
MaxStateTrieLevelInMemory uint
MaxPeerTrieLevelInMemory uint
StateStatisticsEnabled bool
}

// TrieStorageManagerConfig will hold config information about trie storage manager
Expand Down
10 changes: 5 additions & 5 deletions epochStart/metachain/baseRewards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ import (
"github.com/multiversx/mx-chain-core-go/data/rewardTx"
"github.com/multiversx/mx-chain-core-go/hashing/sha256"
"github.com/multiversx/mx-chain-core-go/marshal"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/multiversx/mx-chain-go/epochStart"
"github.com/multiversx/mx-chain-go/epochStart/mock"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/sharding"
"github.com/multiversx/mx-chain-go/state/factory"
"github.com/multiversx/mx-chain-go/state/stateChanges"
"github.com/multiversx/mx-chain-go/testscommon"
txExecOrderStub "github.com/multiversx/mx-chain-go/testscommon/common"
dataRetrieverMock "github.com/multiversx/mx-chain-go/testscommon/dataRetriever"
Expand All @@ -30,9 +33,6 @@ import (
stateMock "github.com/multiversx/mx-chain-go/testscommon/state"
"github.com/multiversx/mx-chain-go/testscommon/storage"
"github.com/multiversx/mx-chain-go/trie"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestBaseRewardsCreator_NilShardCoordinator(t *testing.T) {
Expand Down Expand Up @@ -1181,7 +1181,7 @@ func getBaseRewardsArguments() BaseRewardsCreatorArgs {
Hasher: hasher,
Marshaller: marshalizer,
EnableEpochsHandler: &enableEpochsHandlerMock.EnableEpochsHandlerStub{},
StateChangesCollector: stateChanges.NewStateChangesCollector(),
StateChangesCollector: &stateMock.StateChangesCollectorStub{},
}
accCreator, _ := factory.NewAccountCreator(argsAccCreator)
enableEpochsHandler := &enableEpochsHandlerMock.EnableEpochsHandlerStub{}
Expand Down
8 changes: 4 additions & 4 deletions factory/processing/blockProcessorCreator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/hashing"
"github.com/multiversx/mx-chain-core-go/marshal"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"
"github.com/stretchr/testify/require"

"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/dataRetriever"
dataComp "github.com/multiversx/mx-chain-go/factory/data"
Expand All @@ -17,7 +20,6 @@ import (
"github.com/multiversx/mx-chain-go/state/accounts"
disabledState "github.com/multiversx/mx-chain-go/state/disabled"
factoryState "github.com/multiversx/mx-chain-go/state/factory"
"github.com/multiversx/mx-chain-go/state/stateChanges"
"github.com/multiversx/mx-chain-go/state/storagePruningManager/disabled"
"github.com/multiversx/mx-chain-go/testscommon"
componentsMock "github.com/multiversx/mx-chain-go/testscommon/components"
Expand All @@ -27,8 +29,6 @@ import (
storageManager "github.com/multiversx/mx-chain-go/testscommon/storage"
trieMock "github.com/multiversx/mx-chain-go/testscommon/trie"
"github.com/multiversx/mx-chain-go/trie"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"
"github.com/stretchr/testify/require"
)

func Test_newBlockProcessorCreatorForShard(t *testing.T) {
Expand Down Expand Up @@ -107,7 +107,7 @@ func Test_newBlockProcessorCreatorForMeta(t *testing.T) {
Hasher: coreComponents.Hasher(),
Marshaller: coreComponents.InternalMarshalizer(),
EnableEpochsHandler: coreComponents.EnableEpochsHandler(),
StateChangesCollector: stateChanges.NewStateChangesCollector(),
StateChangesCollector: &stateMock.StateChangesCollectorStub{},
}
accCreator, _ := factoryState.NewAccountCreator(argsAccCreator)

Expand Down
89 changes: 71 additions & 18 deletions factory/state/stateComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package state

import (
"fmt"
"strings"

"github.com/multiversx/mx-chain-core-go/core/check"
chainData "github.com/multiversx/mx-chain-core-go/data"
data "github.com/multiversx/mx-chain-core-go/data/stateChange"

"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/config"
"github.com/multiversx/mx-chain-go/dataRetriever"
Expand Down Expand Up @@ -122,34 +125,55 @@ func (scf *stateComponentsFactory) Create() (*stateComponents, error) {
}

func (scf *stateComponentsFactory) createStateChangesCollector() (state.StateChangesCollector, error) {
if !scf.config.StateTriesConfig.CollectStateChangesEnabled {
if len(scf.config.StateTriesConfig.StateChangesTypesToCollect) == 0 {
return disabled.NewDisabledStateChangesCollector(), nil
}

if !scf.config.StateTriesConfig.CollectStateChangesWithReadEnabled {
return stateChanges.NewStateChangesCollector(), nil
collectRead, collectWrite, err := parseStateChangesTypesToCollect(scf.config.StateTriesConfig.StateChangesTypesToCollect)
if err != nil {
return nil, fmt.Errorf("failed to parse state changes types to collect: %w", err)
}

// TODO: move to toml config file
dbConfig := config.DBConfig{
FilePath: "stateChanges",
Type: "LvlDBSerial",
BatchDelaySeconds: 2,
MaxBatchSize: 100,
MaxOpenFiles: 10,
var opts []stateChanges.CollectorOption
if collectRead {
opts = append(opts, stateChanges.WithCollectRead())
}
if collectWrite {
opts = append(opts, stateChanges.WithCollectWrite())
}

persisterFactory, err := storageFactory.NewPersisterFactory(dbConfig)
if err != nil {
return nil, err
if scf.config.StateTriesConfig.StateChangesDataAnalysis {
// TODO: move to toml config file
dbConfig := config.DBConfig{
FilePath: "stateChanges",
Type: "LvlDBSerial",
BatchDelaySeconds: 2,
MaxBatchSize: 100,
MaxOpenFiles: 10,
}

persisterFactory, err := storageFactory.NewPersisterFactory(dbConfig)
if err != nil {
return nil, err
}

db, err := persisterFactory.CreateWithRetries(dbConfig.FilePath)
if err != nil {
return nil, fmt.Errorf("%w while creating the db for the trie nodes", err)
}

opts = append(opts, stateChanges.WithStorer(db))
}

db, err := persisterFactory.CreateWithRetries(dbConfig.FilePath)
if err != nil {
return nil, fmt.Errorf("%w while creating the db for the trie nodes", err)
return stateChanges.NewCollector(opts...), nil
}

func (scf *stateComponentsFactory) createStateChangesCollectorPeerAccounts() (state.StateChangesCollector, error) {
if !scf.config.StateTriesConfig.StateChangesPeerAccountsEnabled {
return disabled.NewDisabledStateChangesCollector(), nil
}

return stateChanges.NewDataAnalysisStateChangesCollector(db)
return scf.createStateChangesCollector()
}

func (scf *stateComponentsFactory) createSnapshotManager(
Expand Down Expand Up @@ -287,7 +311,7 @@ func (scf *stateComponentsFactory) createPeerAdapter(triesContainer common.Tries
return nil, err
}

stateChangesCollector, err := scf.createStateChangesCollector()
stateChangesCollector, err := scf.createStateChangesCollectorPeerAccounts()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -375,3 +399,32 @@ func (pc *stateComponents) Close() error {
}
return nil
}

func parseStateChangesTypesToCollect(stateChangesTypes []string) (collectRead bool, collectWrite bool, err error) {
types := sanitizeActionTypes(data.ActionType_value)
for _, stateChangeType := range stateChangesTypes {
if value, ok := types[strings.ToLower(stateChangeType)]; ok {
switch value {
case 0:
collectRead = true

case 1:
collectWrite = true
}
} else {
return false, false, fmt.Errorf("unknown action type %s", stateChangeType)
}
}

return collectRead, collectWrite, nil
}

func sanitizeActionTypes(actionTypes map[string]int32) map[string]int32 {
sanitizedActionTypes := make(map[string]int32, len(actionTypes))

for actionType, value := range actionTypes {
sanitizedActionTypes[strings.ToLower(actionType)] = value
}

return sanitizedActionTypes
}
3 changes: 2 additions & 1 deletion factory/state/stateComponents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (

"github.com/multiversx/mx-chain-core-go/hashing"
"github.com/multiversx/mx-chain-core-go/marshal"
"github.com/stretchr/testify/require"

"github.com/multiversx/mx-chain-go/errors"
stateComp "github.com/multiversx/mx-chain-go/factory/state"
"github.com/multiversx/mx-chain-go/testscommon"
componentsMock "github.com/multiversx/mx-chain-go/testscommon/components"
"github.com/multiversx/mx-chain-go/testscommon/factory"
"github.com/stretchr/testify/require"
)

func TestNewStateComponentsFactory(t *testing.T) {
Expand Down
Loading

0 comments on commit 5450ae1

Please sign in to comment.