Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated core enums in state changes. #6495

Merged
merged 22 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 2 additions & 1 deletion cmd/node/config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,8 @@
MaxStateTrieLevelInMemory = 5
MaxPeerTrieLevelInMemory = 5
StateStatisticsEnabled = false
CollectStateChangesEnabled = false
StateChangesDataAnalysis = false
StateChangesTypesToCollect = []

[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
16 changes: 8 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,14 +308,14 @@ 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change needs to be reflected also in config.toml

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

StateChangesTypesToCollect []string
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
82 changes: 64 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,48 @@ func (scf *stateComponentsFactory) Create() (*stateComponents, error) {
}

func (scf *stateComponentsFactory) createStateChangesCollector() (state.StateChangesCollector, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

state changes collector is created 2 times in this file, it should be creating only once for main accounts db and then passed to peer accounts

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not related to this PR, but please fix this here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should have a different collector for PeerAccounts that can be disabled separately from config.

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

persisterFactory, err := storageFactory.NewPersisterFactory(dbConfig)
if err != nil {
return nil, err
if collectRead {
opts = append(opts, stateChanges.WithCollectRead())
}
if collectWrite {
opts = append(opts, stateChanges.WithCollectRead())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.WithCollectWrite()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

db, err := persisterFactory.CreateWithRetries(dbConfig.FilePath)
if err != nil {
return nil, fmt.Errorf("%w while creating the db for the trie nodes", 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))
}

return stateChanges.NewDataAnalysisStateChangesCollector(db)
return stateChanges.NewCollector(opts...), nil
}

func (scf *stateComponentsFactory) createSnapshotManager(
Expand Down Expand Up @@ -375,3 +392,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
79 changes: 79 additions & 0 deletions factory/state/stateParser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package state

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestStateComponents_ParseStateChangesTypesToCollect(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add tests for error cases?

t.Parallel()

t.Run("should parse state changes: 1 read 1 write", func(t *testing.T) {
t.Parallel()

collectRead, collectWrite, err := parseStateChangesTypesToCollect([]string{"read", "write"})
require.NoError(t, err)
require.True(t, collectRead)
require.True(t, collectWrite)
})

t.Run("should parse state changes: multiple types", func(t *testing.T) {
t.Parallel()

collectRead, collectWrite, err := parseStateChangesTypesToCollect([]string{"read", "read", "write", "write"})
require.NoError(t, err)
require.True(t, collectRead)
require.True(t, collectWrite)
})

t.Run("should parse state changes: inconsistent strings", func(t *testing.T) {
collectRead, collectWrite, err := parseStateChangesTypesToCollect([]string{"Read", "read", "Write", "write"})
require.NoError(t, err)
require.True(t, collectRead)
require.True(t, collectWrite)

collectRead, collectWrite, err = parseStateChangesTypesToCollect([]string{"Read"})
require.NoError(t, err)
require.True(t, collectRead)
require.False(t, collectWrite)

collectRead, collectWrite, err = parseStateChangesTypesToCollect([]string{"Read", "rEaD"})
require.NoError(t, err)
require.True(t, collectRead)
require.False(t, collectWrite)

collectRead, collectWrite, err = parseStateChangesTypesToCollect([]string{"Write"})
require.NoError(t, err)
require.False(t, collectRead)
require.True(t, collectWrite)

collectRead, collectWrite, err = parseStateChangesTypesToCollect([]string{"Write", "write", "wRiTe"})
require.NoError(t, err)
require.False(t, collectRead)
require.True(t, collectWrite)
})

t.Run("should parse state changes: no types", func(t *testing.T) {
t.Parallel()

collectRead, collectWrite, err := parseStateChangesTypesToCollect([]string{})
require.NoError(t, err)
require.False(t, collectRead)
require.False(t, collectWrite)

collectRead, collectWrite, err = parseStateChangesTypesToCollect(nil)
require.NoError(t, err)
require.False(t, collectRead)
require.False(t, collectWrite)
})

t.Run("should not parse state changes: invalid types", func(t *testing.T) {
t.Parallel()

collectRead, collectWrite, err := parseStateChangesTypesToCollect([]string{"r3ad", "writ3"})
require.ErrorContains(t, err, "unknown action type")
require.False(t, collectRead)
require.False(t, collectWrite)
})
}
1 change: 1 addition & 0 deletions factory/status/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package status

import (
"github.com/multiversx/mx-chain-core-go/core"

"github.com/multiversx/mx-chain-go/epochStart"
outportDriverFactory "github.com/multiversx/mx-chain-go/outport/factory"
"github.com/multiversx/mx-chain-go/p2p"
Expand Down
Loading
Loading