Skip to content

Commit

Permalink
Merge pull request #5585 from multiversx/state-changes-collector
Browse files Browse the repository at this point in the history
State changes collector
  • Loading branch information
AdoAdoAdo authored Sep 4, 2024
2 parents 49fc4cc + 5bf6ee7 commit ccffe6d
Show file tree
Hide file tree
Showing 36 changed files with 912 additions and 209 deletions.
1 change: 1 addition & 0 deletions cmd/node/config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@
MaxStateTrieLevelInMemory = 5
MaxPeerTrieLevelInMemory = 5
StateStatisticsEnabled = false
CollectStateChangesEnabled = false

[BlockSizeThrottleConfig]
MinSizeInBytes = 104857 # 104857 is 10% from 1MB
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ type StateTriesConfig struct {
SnapshotsEnabled bool
AccountsStatePruningEnabled bool
PeerStatePruningEnabled bool
CollectStateChangesEnabled bool
MaxStateTrieLevelInMemory uint
MaxPeerTrieLevelInMemory uint
StateStatisticsEnabled bool
Expand Down
9 changes: 9 additions & 0 deletions epochStart/bootstrap/disabled/disabledAccountsAdapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ func (a *accountsAdapter) GetStackDebugFirstEntry() []byte {
return nil
}

// SetTxHashForLatestStateChanges -
func (a *accountsAdapter) SetTxHashForLatestStateChanges(_ []byte) {
}

// ResetStateChangesCollector -
func (a *accountsAdapter) ResetStateChangesCollector() []state.StateChangesForTx {
return nil
}

// Close -
func (a *accountsAdapter) Close() error {
return nil
Expand Down
1 change: 1 addition & 0 deletions epochStart/metachain/systemSCs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,7 @@ func createAccountsDB(
StoragePruningManager: spm,
AddressConverter: &testscommon.PubkeyConverterMock{},
SnapshotsManager: disabledState.NewDisabledSnapshotsManager(),
StateChangesCollector: disabledState.NewDisabledStateChangesCollector(),
}
adb, _ := state.NewAccountsDB(args)
return adb
Expand Down
1 change: 1 addition & 0 deletions factory/api/apiResolverFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ func createNewAccountsAdapterApi(args scQueryElementArgs, chainHandler data.Chai
StoragePruningManager: storagePruning,
AddressConverter: args.coreComponents.AddressPubKeyConverter(),
SnapshotsManager: disabledState.NewDisabledSnapshotsManager(),
StateChangesCollector: disabledState.NewDisabledStateChangesCollector(),
}

provider, err := blockInfoProviders.NewCurrentBlockInfo(chainHandler)
Expand Down
1 change: 1 addition & 0 deletions factory/processing/blockProcessorCreator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ func createAccountAdapter(
StoragePruningManager: disabled.NewDisabledStoragePruningManager(),
AddressConverter: &testscommon.PubkeyConverterMock{},
SnapshotsManager: disabledState.NewDisabledSnapshotsManager(),
StateChangesCollector: disabledState.NewDisabledStateChangesCollector(),
}
adb, err := state.NewAccountsDB(args)
if err != nil {
Expand Down
41 changes: 27 additions & 14 deletions factory/state/stateComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ func (scf *stateComponentsFactory) createAccountsAdapters(triesContainer common.
return nil, nil, nil, err
}

stateChangesCollector := disabled.NewDisabledStateChangesCollector()
if scf.config.StateTriesConfig.CollectStateChangesEnabled {
stateChangesCollector = state.NewStateChangesCollector()
}

argStateMetrics := stateMetrics.ArgsStateMetrics{
SnapshotInProgressKey: common.MetricAccountsSnapshotInProgress,
LastSnapshotDurationKey: common.MetricLastAccountsSnapshotDurationSec,
Expand All @@ -168,13 +173,14 @@ func (scf *stateComponentsFactory) createAccountsAdapters(triesContainer common.
}

argsProcessingAccountsDB := state.ArgsAccountsDB{
Trie: merkleTrie,
Hasher: scf.core.Hasher(),
Marshaller: scf.core.InternalMarshalizer(),
AccountFactory: accountFactory,
StoragePruningManager: storagePruning,
AddressConverter: scf.core.AddressPubKeyConverter(),
SnapshotsManager: snapshotsManager,
Trie: merkleTrie,
Hasher: scf.core.Hasher(),
Marshaller: scf.core.InternalMarshalizer(),
AccountFactory: accountFactory,
StoragePruningManager: storagePruning,
AddressConverter: scf.core.AddressPubKeyConverter(),
SnapshotsManager: snapshotsManager,
StateChangesCollector: stateChangesCollector,
}
accountsAdapter, err := state.NewAccountsDB(argsProcessingAccountsDB)
if err != nil {
Expand All @@ -189,6 +195,7 @@ func (scf *stateComponentsFactory) createAccountsAdapters(triesContainer common.
StoragePruningManager: storagePruning,
AddressConverter: scf.core.AddressPubKeyConverter(),
SnapshotsManager: disabled.NewDisabledSnapshotsManager(),
StateChangesCollector: disabled.NewDisabledStateChangesCollector(),
}

accountsAdapterApiOnFinal, err := factoryState.CreateAccountsAdapterAPIOnFinal(argsAPIAccountsDB, scf.chainHandler)
Expand Down Expand Up @@ -228,6 +235,11 @@ func (scf *stateComponentsFactory) createPeerAdapter(triesContainer common.Tries
return nil, err
}

stateChangesCollector := disabled.NewDisabledStateChangesCollector()
if scf.config.StateTriesConfig.CollectStateChangesEnabled {
stateChangesCollector = state.NewStateChangesCollector()
}

argStateMetrics := stateMetrics.ArgsStateMetrics{
SnapshotInProgressKey: common.MetricPeersSnapshotInProgress,
LastSnapshotDurationKey: common.MetricLastPeersSnapshotDurationSec,
Expand All @@ -244,13 +256,14 @@ func (scf *stateComponentsFactory) createPeerAdapter(triesContainer common.Tries
}

argsProcessingPeerAccountsDB := state.ArgsAccountsDB{
Trie: merkleTrie,
Hasher: scf.core.Hasher(),
Marshaller: scf.core.InternalMarshalizer(),
AccountFactory: accountFactory,
StoragePruningManager: storagePruning,
AddressConverter: scf.core.AddressPubKeyConverter(),
SnapshotsManager: snapshotManager,
Trie: merkleTrie,
Hasher: scf.core.Hasher(),
Marshaller: scf.core.InternalMarshalizer(),
AccountFactory: accountFactory,
StoragePruningManager: storagePruning,
AddressConverter: scf.core.AddressPubKeyConverter(),
SnapshotsManager: snapshotManager,
StateChangesCollector: stateChangesCollector,
}
peerAdapter, err := state.NewPeerAccountsDB(argsProcessingPeerAccountsDB)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions genesis/mock/userAccountMock.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/state"
)

// ErrNegativeValue -
Expand Down Expand Up @@ -147,8 +148,8 @@ func (uam *UserAccountMock) GetUserName() []byte {
}

// SaveDirtyData -
func (uam *UserAccountMock) SaveDirtyData(_ common.Trie) ([]core.TrieData, error) {
return nil, nil
func (uam *UserAccountMock) SaveDirtyData(_ common.Trie) ([]state.DataTrieChange, []core.TrieData, error) {
return nil, nil, nil
}

// IsGuarded -
Expand Down
1 change: 1 addition & 0 deletions genesis/process/memoryComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func createAccountAdapter(
StoragePruningManager: disabled.NewDisabledStoragePruningManager(),
AddressConverter: addressConverter,
SnapshotsManager: disabledState.NewDisabledSnapshotsManager(),
StateChangesCollector: state.NewStateChangesCollector(),
}

adb, err := state.NewAccountsDB(args)
Expand Down
2 changes: 2 additions & 0 deletions integrationTests/state/stateTrie/stateTrie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,7 @@ func createAccounts(
StoragePruningManager: spm,
AddressConverter: &testscommon.PubkeyConverterMock{},
SnapshotsManager: snapshotsManager,
StateChangesCollector: state.NewStateChangesCollector(),
}
adb, _ := state.NewAccountsDB(argsAccountsDB)

Expand Down Expand Up @@ -2757,6 +2758,7 @@ func createAccountsDBTestSetup() *state.AccountsDB {
StoragePruningManager: spm,
AddressConverter: &testscommon.PubkeyConverterMock{},
SnapshotsManager: snapshotsManager,
StateChangesCollector: state.NewStateChangesCollector(),
}
adb, _ := state.NewAccountsDB(argsAccountsDB)

Expand Down
1 change: 1 addition & 0 deletions integrationTests/testInitializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ func CreateAccountsDBWithEnableEpochsHandler(
StoragePruningManager: spm,
AddressConverter: &testscommon.PubkeyConverterMock{},
SnapshotsManager: snapshotsManager,
StateChangesCollector: state.NewStateChangesCollector(),
}
adb, _ := state.NewAccountsDB(args)

Expand Down
2 changes: 2 additions & 0 deletions integrationTests/vm/staking/componentsHolderCreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/multiversx/mx-chain-go/sharding"
"github.com/multiversx/mx-chain-go/sharding/nodesCoordinator"
"github.com/multiversx/mx-chain-go/state"
disabledState "github.com/multiversx/mx-chain-go/state/disabled"
stateFactory "github.com/multiversx/mx-chain-go/state/factory"
"github.com/multiversx/mx-chain-go/state/storagePruningManager"
"github.com/multiversx/mx-chain-go/state/storagePruningManager/evictionWaitingList"
Expand Down Expand Up @@ -215,6 +216,7 @@ func createAccountsDB(
StoragePruningManager: spm,
AddressConverter: coreComponents.AddressPubKeyConverter(),
SnapshotsManager: &stateTests.SnapshotsManagerStub{},
StateChangesCollector: disabledState.NewDisabledStateChangesCollector(),
}
adb, _ := state.NewAccountsDB(argsAccountsDb)
return adb
Expand Down
3 changes: 3 additions & 0 deletions process/transaction/metaProcess.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ func (txProc *metaTxProcessor) ProcessTransaction(tx *transaction.Transaction) (
txProc.pubkeyConv,
)

defer txProc.accounts.SetTxHashForLatestStateChanges(txHash)

err = txProc.checkTxValues(tx, acntSnd, acntDst, false)
if err != nil {
if errors.Is(err, process.ErrUserNameDoesNotMatchInCrossShardTx) {
Expand All @@ -136,6 +138,7 @@ func (txProc *metaTxProcessor) ProcessTransaction(tx *transaction.Transaction) (
}

txType, _ := txProc.txTypeHandler.ComputeTransactionType(tx)

switch txType {
case process.SCDeployment:
return txProc.processSCDeployment(tx, tx.SndAddr)
Expand Down
3 changes: 3 additions & 0 deletions process/transaction/shardProcess.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ func (txProc *txProcessor) ProcessTransaction(tx *transaction.Transaction) (vmco
txProc.pubkeyConv,
)

// TODO refactor to set the tx hash for the following state changes before the processing occurs
defer txProc.accounts.SetTxHashForLatestStateChanges(txHash)

txType, dstShardTxType := txProc.txTypeHandler.ComputeTransactionType(tx)
err = txProc.checkTxValues(tx, acntSnd, acntDst, false)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions process/transactionEvaluator/simulationAccountsDB.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@ func (r *simulationAccountsDB) GetStackDebugFirstEntry() []byte {
return nil
}

// SetTxHashForLatestStateChanges -
func (r *simulationAccountsDB) SetTxHashForLatestStateChanges(txHash []byte) {
r.originalAccounts.SetTxHashForLatestStateChanges(txHash)
}

// ResetStateChangesCollector -
func (r *simulationAccountsDB) ResetStateChangesCollector() []state.StateChangesForTx {
return nil
}

// Close will handle the closing of the underlying components
func (r *simulationAccountsDB) Close() error {
return nil
Expand Down
Loading

0 comments on commit ccffe6d

Please sign in to comment.