-
Notifications
You must be signed in to change notification settings - Fork 203
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
Changes from 21 commits
ea2cf1a
8ae48f7
d866489
0aa84f3
3e7330b
bf83198
277733b
7767701
c527a42
81a982f
9c13bd0
4621778
dd21bf0
77c4ab6
215a5a7
133f1c1
5cd1778
64e01aa
89f452d
d0efc49
23498fa
305041c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -122,34 +125,48 @@ func (scf *stateComponentsFactory) Create() (*stateComponents, error) { | |
} | ||
|
||
func (scf *stateComponentsFactory) createStateChangesCollector() (state.StateChangesCollector, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not related to this PR, but please fix this here There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. .WithCollectWrite() There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
|
@@ -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 | ||
} |
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
}) | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done