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

add logging for state changes collector #6803

Draft
wants to merge 4 commits into
base: feat/state-changes
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions epochStart/bootstrap/disabled/disabledAccountsAdapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package disabled

import (
"context"

"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/state"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"
Expand Down Expand Up @@ -130,7 +130,7 @@ func (a *accountsAdapter) GetStackDebugFirstEntry() []byte {
}

// SetTxHashForLatestStateChanges -
func (a *accountsAdapter) SetTxHashForLatestStateChanges(_ []byte, _ *transaction.Transaction) {
func (a *accountsAdapter) SetTxHashForLatestStateChanges(_ []byte, _ data.TransactionHandler) {
}

// Close -
Expand Down
2 changes: 2 additions & 0 deletions factory/processing/blockProcessorCreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ func (pcf *processComponentsFactory) newShardBlockProcessor(
pcf.state.AccountsAdapter(),
pcf.coreData.AddressPubKeyConverter(),
pcf.bootstrapComponents.ShardCoordinator(),
pcf.coreData.InternalMarshalizer(),
pcf.coreData.Hasher(),
)
if err != nil {
return nil, err
Expand Down
2 changes: 2 additions & 0 deletions genesis/process/shardGenesisBlockCreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,8 @@ func createProcessorsForShardGenesisBlock(arg ArgsGenesisBlockCreator, enableEpo
arg.Accounts,
arg.Core.AddressPubKeyConverter(),
arg.ShardCoordinator,
arg.Core.InternalMarshalizer(),
arg.Core.Hasher(),
)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/klauspost/cpuid/v2 v2.2.5
github.com/mitchellh/mapstructure v1.5.0
github.com/multiversx/mx-chain-communication-go v1.1.1
github.com/multiversx/mx-chain-core-go v1.2.25-0.20250206084405-a19e65762bf4
github.com/multiversx/mx-chain-core-go v1.2.25-0.20250218140156-ae1c6ad7aca1
github.com/multiversx/mx-chain-crypto-go v1.2.12
github.com/multiversx/mx-chain-es-indexer-go v1.7.14
github.com/multiversx/mx-chain-logger-go v1.0.15
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,8 @@ github.com/multiversx/concurrent-map v0.1.4 h1:hdnbM8VE4b0KYJaGY5yJS2aNIW9TFFsUY
github.com/multiversx/concurrent-map v0.1.4/go.mod h1:8cWFRJDOrWHOTNSqgYCUvwT7c7eFQ4U2vKMOp4A/9+o=
github.com/multiversx/mx-chain-communication-go v1.1.1 h1:y4DoQeQOJTaSUsRzczQFazf8JYQmInddypApqA3AkwM=
github.com/multiversx/mx-chain-communication-go v1.1.1/go.mod h1:WK6bP4pGEHGDDna/AYRIMtl6G9OA0NByI1Lw8PmOnRM=
github.com/multiversx/mx-chain-core-go v1.2.25-0.20250206084405-a19e65762bf4 h1:k8s4I2FeCocES/musoetqYEL1VQY5Mtk6bmxiXbsE3w=
github.com/multiversx/mx-chain-core-go v1.2.25-0.20250206084405-a19e65762bf4/go.mod h1:B5zU4MFyJezmEzCsAHE9YNULmGCm2zbPHvl9hazNxmE=
github.com/multiversx/mx-chain-core-go v1.2.25-0.20250218140156-ae1c6ad7aca1 h1:1E0UgHQzijZ9abzQqCR4S/OCuGFmRr4etExbWH7jSLE=
github.com/multiversx/mx-chain-core-go v1.2.25-0.20250218140156-ae1c6ad7aca1/go.mod h1:B5zU4MFyJezmEzCsAHE9YNULmGCm2zbPHvl9hazNxmE=
github.com/multiversx/mx-chain-crypto-go v1.2.12 h1:zWip7rpUS4CGthJxfKn5MZfMfYPjVjIiCID6uX5BSOk=
github.com/multiversx/mx-chain-crypto-go v1.2.12/go.mod h1:HzcPpCm1zanNct/6h2rIh+MFrlXbjA5C8+uMyXj3LI4=
github.com/multiversx/mx-chain-es-indexer-go v1.7.14 h1:V4fuubEUYskWCLQIkbuoB0WHoKyldLQRq/fllIzW1CU=
Expand Down
2 changes: 2 additions & 0 deletions integrationTests/testProcessorNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -1591,6 +1591,8 @@ func (tpn *TestProcessorNode) initInnerProcessors(gasMap map[string]map[string]u
tpn.AccntState,
TestAddressPubkeyConverter,
tpn.ShardCoordinator,
TestMarshalizer,
TestHasher,
)

mapDNSAddresses := make(map[string]struct{})
Expand Down
8 changes: 7 additions & 1 deletion integrationTests/vm/wasm/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,13 @@ func SetupTestContextWithGasSchedule(t *testing.T, gasSchedule map[string]map[st
}
context.QueryService, _ = smartContract.NewSCQueryService(argsNewSCQueryService)

context.RewardsProcessor, err = rewardTransaction.NewRewardTxProcessor(context.Accounts, pkConverter, oneShardCoordinator)
context.RewardsProcessor, err = rewardTransaction.NewRewardTxProcessor(
context.Accounts,
pkConverter,
oneShardCoordinator,
integrationTests.TestMarshalizer,
integrationTests.TestHasher,
)
require.Nil(t, err)

require.NotNil(t, context.TxProcessor)
Expand Down
29 changes: 29 additions & 0 deletions process/rewardTransaction/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,35 @@ import (
"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-core-go/data/rewardTx"
"github.com/multiversx/mx-chain-core-go/hashing"
"github.com/multiversx/mx-chain-core-go/marshal"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/sharding"
"github.com/multiversx/mx-chain-go/state"
logger "github.com/multiversx/mx-chain-logger-go"
)

var _ process.RewardTransactionProcessor = (*rewardTxProcessor)(nil)

var log = logger.GetOrCreate("process/reward")

const rewardKey = "reward"

type rewardTxProcessor struct {
accounts state.AccountsAdapter
pubkeyConv core.PubkeyConverter
shardCoordinator sharding.Coordinator
marshaller marshal.Marshalizer
hasher hashing.Hasher
}

// NewRewardTxProcessor creates a rewardTxProcessor instance
func NewRewardTxProcessor(
accountsDB state.AccountsAdapter,
pubkeyConv core.PubkeyConverter,
coordinator sharding.Coordinator,
marshaller marshal.Marshalizer,
hasher hashing.Hasher,
) (*rewardTxProcessor, error) {
if check.IfNil(accountsDB) {
return nil, process.ErrNilAccountsAdapter
Expand All @@ -36,11 +45,19 @@ func NewRewardTxProcessor(
if check.IfNil(coordinator) {
return nil, process.ErrNilShardCoordinator
}
if check.IfNil(marshaller) {
return nil, process.ErrNilMarshalizer
}
if check.IfNil(hasher) {
return nil, process.ErrNilHasher
}

return &rewardTxProcessor{
accounts: accountsDB,
pubkeyConv: pubkeyConv,
shardCoordinator: coordinator,
marshaller: marshaller,
hasher: hasher,
}, nil
}

Expand Down Expand Up @@ -91,6 +108,18 @@ func (rtp *rewardTxProcessor) ProcessRewardTransaction(rTx *rewardTx.RewardTx) e
rtp.pubkeyConv,
)

txHash, err := core.CalculateHash(rtp.marshaller, rtp.hasher, rTx)
if err != nil {
log.Debug("CalculateHash error", "error", err)
return err
}

// TODO refactor to set the tx hash for the following state changes before the processing occurs
Copy link
Contributor

Choose a reason for hiding this comment

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

is this comment still needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, will refactor like the comment says in a future PR

defer func() {
rtp.accounts.SetTxHashForLatestStateChanges(txHash, rTx)
log.Debug("SetTxHashForLatestStateChanges", "txHash", txHash)
}()

err = accHandler.AddToBalance(rTx.Value)
if err != nil {
return err
Expand Down
53 changes: 53 additions & 0 deletions process/rewardTransaction/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func TestNewRewardTxProcessor_NilAccountsDbShouldErr(t *testing.T) {
nil,
createMockPubkeyConverter(),
mock.NewMultiShardsCoordinatorMock(3),
&marshallerMock.MarshalizerMock{},
&hashingMocks.HasherMock{},
)

assert.Nil(t, rtp)
Expand All @@ -42,6 +44,8 @@ func TestNewRewardTxProcessor_NilPubkeyConverterShouldErr(t *testing.T) {
&stateMock.AccountsStub{},
nil,
mock.NewMultiShardsCoordinatorMock(3),
&marshallerMock.MarshalizerMock{},
&hashingMocks.HasherMock{},
)

assert.Nil(t, rtp)
Expand All @@ -55,19 +59,52 @@ func TestNewRewardTxProcessor_NilShardCoordinatorShouldErr(t *testing.T) {
&stateMock.AccountsStub{},
createMockPubkeyConverter(),
nil,
&marshallerMock.MarshalizerMock{},
&hashingMocks.HasherMock{},
)

assert.Nil(t, rtp)
assert.Equal(t, process.ErrNilShardCoordinator, err)
}

func TestNewRewardTxProcessor_NilMarshallerShouldErr(t *testing.T) {
t.Parallel()

rtp, err := rewardTransaction.NewRewardTxProcessor(
&stateMock.AccountsStub{},
createMockPubkeyConverter(),
mock.NewMultiShardsCoordinatorMock(3),
nil,
&hashingMocks.HasherMock{},
)

assert.Nil(t, rtp)
assert.Equal(t, process.ErrNilMarshalizer, err)
}
func TestNewRewardTxProcessor_NilHasherShouldErr(t *testing.T) {
t.Parallel()

rtp, err := rewardTransaction.NewRewardTxProcessor(
&stateMock.AccountsStub{},
createMockPubkeyConverter(),
mock.NewMultiShardsCoordinatorMock(3),
&marshallerMock.MarshalizerMock{},
nil,
)

assert.Nil(t, rtp)
assert.Equal(t, process.ErrNilHasher, err)
}

func TestNewRewardTxProcessor_OkValsShouldWork(t *testing.T) {
t.Parallel()

rtp, err := rewardTransaction.NewRewardTxProcessor(
&stateMock.AccountsStub{},
createMockPubkeyConverter(),
mock.NewMultiShardsCoordinatorMock(3),
&marshallerMock.MarshalizerMock{},
&hashingMocks.HasherMock{},
)

assert.NotNil(t, rtp)
Expand All @@ -82,6 +119,8 @@ func TestRewardTxProcessor_ProcessRewardTransactionNilTxShouldErr(t *testing.T)
&stateMock.AccountsStub{},
createMockPubkeyConverter(),
mock.NewMultiShardsCoordinatorMock(3),
&marshallerMock.MarshalizerMock{},
&hashingMocks.HasherMock{},
)

err := rtp.ProcessRewardTransaction(nil)
Expand All @@ -95,6 +134,8 @@ func TestRewardTxProcessor_ProcessRewardTransactionNilTxValueShouldErr(t *testin
&stateMock.AccountsStub{},
createMockPubkeyConverter(),
mock.NewMultiShardsCoordinatorMock(3),
&marshallerMock.MarshalizerMock{},
&hashingMocks.HasherMock{},
)

rwdTx := rewardTx.RewardTx{Value: nil}
Expand All @@ -119,6 +160,8 @@ func TestRewardTxProcessor_ProcessRewardTransactionAddressNotInNodesShardShouldN
},
createMockPubkeyConverter(),
shardCoord,
&marshallerMock.MarshalizerMock{},
&hashingMocks.HasherMock{},
)

rwdTx := rewardTx.RewardTx{
Expand Down Expand Up @@ -146,6 +189,8 @@ func TestRewardTxProcessor_ProcessRewardTransactionCannotGetAccountShouldErr(t *
},
createMockPubkeyConverter(),
mock.NewMultiShardsCoordinatorMock(3),
&marshallerMock.MarshalizerMock{},
&hashingMocks.HasherMock{},
)

rwdTx := rewardTx.RewardTx{
Expand All @@ -172,6 +217,8 @@ func TestRewardTxProcessor_ProcessRewardTransactionWrongTypeAssertionAccountHold
accountsDb,
createMockPubkeyConverter(),
mock.NewMultiShardsCoordinatorMock(3),
&marshallerMock.MarshalizerMock{},
&hashingMocks.HasherMock{},
)

rwdTx := rewardTx.RewardTx{
Expand Down Expand Up @@ -204,6 +251,8 @@ func TestRewardTxProcessor_ProcessRewardTransactionShouldWork(t *testing.T) {
accountsDb,
createMockPubkeyConverter(),
mock.NewMultiShardsCoordinatorMock(3),
&marshallerMock.MarshalizerMock{},
&hashingMocks.HasherMock{},
)

rwdTx := rewardTx.RewardTx{
Expand Down Expand Up @@ -240,6 +289,8 @@ func TestRewardTxProcessor_ProcessRewardTransactionMissingTrieNode(t *testing.T)
accountsDb,
createMockPubkeyConverter(),
mock.NewMultiShardsCoordinatorMock(3),
&marshallerMock.MarshalizerMock{},
&hashingMocks.HasherMock{},
)

rwdTx := rewardTx.RewardTx{
Expand Down Expand Up @@ -282,6 +333,8 @@ func TestRewardTxProcessor_ProcessRewardTransactionToASmartContractShouldWork(t
accountsDb,
createMockPubkeyConverter(),
mock.NewMultiShardsCoordinatorMock(3),
&marshallerMock.MarshalizerMock{},
&hashingMocks.HasherMock{},
)

rwdTx := rewardTx.RewardTx{
Expand Down
6 changes: 6 additions & 0 deletions process/smartContract/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -2806,6 +2806,12 @@ func (sc *scProcessor) ProcessSmartContractResult(scr *smartContractResult.Smart
sc.pubkeyConv,
)

// TODO refactor to set the tx hash for the following state changes before the processing occurs
defer func() {
sc.accounts.SetTxHashForLatestStateChanges(txHash, scr)
log.Debug("SetTxHashForLatestStateChanges", "txHash", txHash)
}()

gasLocked := sc.getGasLockedFromSCR(scr)

txType, _, _ := sc.txTypeHandler.ComputeTransactionType(scr)
Expand Down
5 changes: 5 additions & 0 deletions process/smartContract/processorV2/processV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2803,6 +2803,11 @@ func (sc *scProcessor) ProcessSmartContractResult(scr *smartContractResult.Smart
txHash,
sc.pubkeyConv,
)
// TODO refactor to set the tx hash for the following state changes before the processing occurs
defer func() {
sc.accounts.SetTxHashForLatestStateChanges(txHash, scr)
log.Debug("SetTxHashForLatestStateChanges", "txHash", txHash)
}()

gasLocked := sc.getGasLockedFromSCR(scr)

Expand Down
4 changes: 2 additions & 2 deletions process/transactionEvaluator/simulationAccountsDB.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"sync"

"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/state"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"
Expand Down Expand Up @@ -169,7 +169,7 @@ func (r *simulationAccountsDB) GetStackDebugFirstEntry() []byte {
}

// SetTxHashForLatestStateChanges -
func (r *simulationAccountsDB) SetTxHashForLatestStateChanges(txHash []byte, tx *transaction.Transaction) {
func (r *simulationAccountsDB) SetTxHashForLatestStateChanges(txHash []byte, tx data.TransactionHandler) {
r.originalAccounts.SetTxHashForLatestStateChanges(txHash, tx)
}

Expand Down
11 changes: 3 additions & 8 deletions state/accountsDB.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-core-go/data/stateChange"
"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/multiversx/mx-chain-core-go/hashing"
"github.com/multiversx/mx-chain-core-go/marshal"
logger "github.com/multiversx/mx-chain-logger-go"
Expand Down Expand Up @@ -854,12 +854,7 @@ func (adb *AccountsDB) RevertToSnapshot(snapshot int) error {

adb.entries = adb.entries[:snapshot]

err := adb.stateChangesCollector.RevertToIndex(snapshot)
if err != nil {
return err
}

return nil
return adb.stateChangesCollector.RevertToIndex(snapshot)
}

// JournalLen will return the number of entries
Expand Down Expand Up @@ -1301,7 +1296,7 @@ func collectStats(
}

// SetTxHashForLatestStateChanges will return the state changes since the last call of this method
func (adb *AccountsDB) SetTxHashForLatestStateChanges(txHash []byte, tx *transaction.Transaction) {
func (adb *AccountsDB) SetTxHashForLatestStateChanges(txHash []byte, tx data.TransactionHandler) {
adb.stateChangesCollector.AddTxHashToCollectedStateChanges(txHash, tx)
}

Expand Down
4 changes: 2 additions & 2 deletions state/accountsDBApi.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"sync"

"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/multiversx/mx-chain-core-go/data"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"

"github.com/multiversx/mx-chain-go/common"
Expand Down Expand Up @@ -235,7 +235,7 @@ func (accountsDB *accountsDBApi) GetStackDebugFirstEntry() []byte {
}

// SetTxHashForLatestStateChanges will call the inner accountsAdapter method
func (accountsDB *accountsDBApi) SetTxHashForLatestStateChanges(txHash []byte, tx *transaction.Transaction) {
func (accountsDB *accountsDBApi) SetTxHashForLatestStateChanges(txHash []byte, tx data.TransactionHandler) {
accountsDB.innerAccountsAdapter.SetTxHashForLatestStateChanges(txHash, tx)
}

Expand Down
Loading