diff --git a/cmd/node/config/enableEpochs.toml b/cmd/node/config/enableEpochs.toml index 819108b99eb..dd6e20d3589 100644 --- a/cmd/node/config/enableEpochs.toml +++ b/cmd/node/config/enableEpochs.toml @@ -287,6 +287,9 @@ # FixGasRemainingForSaveKeyValueBuiltinFunctionEnableEpoch represents the epoch when the fix for the remaining gas in the SaveKeyValue builtin function is enabled FixGasRemainingForSaveKeyValueBuiltinFunctionEnableEpoch = 3 + # CurrentRandomnessOnSortingEnableEpoch represents the epoch when the current randomness on sorting is enabled + CurrentRandomnessOnSortingEnableEpoch = 4 + # BLSMultiSignerEnableEpoch represents the activation epoch for different types of BLS multi-signers BLSMultiSignerEnableEpoch = [ { EnableEpoch = 0, Type = "no-KOSK" }, diff --git a/common/constants.go b/common/constants.go index 671edaba409..c2d76f0072a 100644 --- a/common/constants.go +++ b/common/constants.go @@ -999,5 +999,6 @@ const ( NFTStopCreateFlag core.EnableEpochFlag = "NFTStopCreateFlag" FixGasRemainingForSaveKeyValueFlag core.EnableEpochFlag = "FixGasRemainingForSaveKeyValueFlag" IsChangeOwnerAddressCrossShardThroughSCFlag core.EnableEpochFlag = "IsChangeOwnerAddressCrossShardThroughSCFlag" + CurrentRandomnessOnSortingFlag core.EnableEpochFlag = "CurrentRandomnessOnSortingFlag" // all new flags must be added to createAllFlagsMap method, as part of enableEpochsHandler allFlagsDefined ) diff --git a/common/enablers/enableEpochsHandler.go b/common/enablers/enableEpochsHandler.go index e5ab0f06100..197cab8fff8 100644 --- a/common/enablers/enableEpochsHandler.go +++ b/common/enablers/enableEpochsHandler.go @@ -695,6 +695,12 @@ func (handler *enableEpochsHandler) createAllFlagsMap() { }, activationEpoch: handler.enableEpochsConfig.ChangeOwnerAddressCrossShardThroughSCEnableEpoch, }, + common.CurrentRandomnessOnSortingFlag: { + isActiveInEpoch: func(epoch uint32) bool { + return epoch >= handler.enableEpochsConfig.CurrentRandomnessOnSortingEnableEpoch + }, + activationEpoch: handler.enableEpochsConfig.CurrentRandomnessOnSortingEnableEpoch, + }, } } diff --git a/common/enablers/enableEpochsHandler_test.go b/common/enablers/enableEpochsHandler_test.go index 75b97c35460..30949150e49 100644 --- a/common/enablers/enableEpochsHandler_test.go +++ b/common/enablers/enableEpochsHandler_test.go @@ -110,6 +110,7 @@ func createEnableEpochsConfig() config.EnableEpochs { NFTStopCreateEnableEpoch: 92, FixGasRemainingForSaveKeyValueBuiltinFunctionEnableEpoch: 93, ChangeOwnerAddressCrossShardThroughSCEnableEpoch: 94, + CurrentRandomnessOnSortingEnableEpoch: 95, } } @@ -297,6 +298,7 @@ func TestEnableEpochsHandler_IsFlagEnabled(t *testing.T) { require.True(t, handler.IsFlagEnabled(common.NFTStopCreateFlag)) require.True(t, handler.IsFlagEnabled(common.FixGasRemainingForSaveKeyValueFlag)) require.True(t, handler.IsFlagEnabled(common.IsChangeOwnerAddressCrossShardThroughSCFlag)) + require.True(t, handler.IsFlagEnabled(common.CurrentRandomnessOnSortingFlag)) } func TestEnableEpochsHandler_GetActivationEpoch(t *testing.T) { @@ -407,6 +409,7 @@ func TestEnableEpochsHandler_GetActivationEpoch(t *testing.T) { require.Equal(t, cfg.NFTStopCreateEnableEpoch, handler.GetActivationEpoch(common.NFTStopCreateFlag)) require.Equal(t, cfg.ChangeOwnerAddressCrossShardThroughSCEnableEpoch, handler.GetActivationEpoch(common.IsChangeOwnerAddressCrossShardThroughSCFlag)) require.Equal(t, cfg.FixGasRemainingForSaveKeyValueBuiltinFunctionEnableEpoch, handler.GetActivationEpoch(common.FixGasRemainingForSaveKeyValueFlag)) + require.Equal(t, cfg.CurrentRandomnessOnSortingEnableEpoch, handler.GetActivationEpoch(common.CurrentRandomnessOnSortingFlag)) } func TestEnableEpochsHandler_IsInterfaceNil(t *testing.T) { diff --git a/config/epochConfig.go b/config/epochConfig.go index c591b17c97b..854f80063be 100644 --- a/config/epochConfig.go +++ b/config/epochConfig.go @@ -108,6 +108,7 @@ type EnableEpochs struct { NFTStopCreateEnableEpoch uint32 ChangeOwnerAddressCrossShardThroughSCEnableEpoch uint32 FixGasRemainingForSaveKeyValueBuiltinFunctionEnableEpoch uint32 + CurrentRandomnessOnSortingEnableEpoch uint32 BLSMultiSignerEnableEpoch []MultiSignerConfig } diff --git a/config/tomlConfig_test.go b/config/tomlConfig_test.go index 42be4ab8713..a6e9f5c2086 100644 --- a/config/tomlConfig_test.go +++ b/config/tomlConfig_test.go @@ -839,6 +839,9 @@ func TestEnableEpochConfig(t *testing.T) { # FixGasRemainingForSaveKeyValueBuiltinFunctionEnableEpoch represents the epoch when the fix for the remaining gas in the SaveKeyValue builtin function is enabled FixGasRemainingForSaveKeyValueBuiltinFunctionEnableEpoch = 91 + # CurrentRandomnessOnSortingEnableEpoch represents the epoch when the current randomness on sorting is enabled + CurrentRandomnessOnSortingEnableEpoch = 92 + # MaxNodesChangeEnableEpoch holds configuration for changing the maximum number of nodes and the enabling epoch MaxNodesChangeEnableEpoch = [ { EpochEnable = 44, MaxNumNodes = 2169, NodesToShufflePerShard = 80 }, @@ -950,6 +953,7 @@ func TestEnableEpochConfig(t *testing.T) { NFTStopCreateEnableEpoch: 89, ChangeOwnerAddressCrossShardThroughSCEnableEpoch: 90, FixGasRemainingForSaveKeyValueBuiltinFunctionEnableEpoch: 91, + CurrentRandomnessOnSortingEnableEpoch: 92, MaxNodesChangeEnableEpoch: []MaxNodesChangeConfig{ { EpochEnable: 44, diff --git a/integrationTests/factory/componentsHelper.go b/integrationTests/factory/componentsHelper.go index 6238243659e..6ad6c5910bf 100644 --- a/integrationTests/factory/componentsHelper.go +++ b/integrationTests/factory/componentsHelper.go @@ -56,10 +56,13 @@ func CreateDefaultConfig(tb testing.TB) *config.Configs { configs.ExternalConfig = externalConfig configs.EpochConfig = epochConfig configs.RoundConfig = roundConfig + workingDir := tb.TempDir() + dbDir := tb.TempDir() + logsDir := tb.TempDir() configs.FlagsConfig = &config.ContextFlagsConfig{ - WorkingDir: tb.TempDir(), - DbDir: "dbDir", - LogsDir: "logsDir", + WorkingDir: workingDir, + DbDir: dbDir, + LogsDir: logsDir, UseLogView: true, BaseVersion: BaseVersion, Version: Version, diff --git a/process/block/baseProcess.go b/process/block/baseProcess.go index 1a8e501ee07..5049cece729 100644 --- a/process/block/baseProcess.go +++ b/process/block/baseProcess.go @@ -515,6 +515,7 @@ func checkProcessorParameters(arguments ArgBaseProcessor) error { err := core.CheckHandlerCompatibility(enableEpochsHandler, []core.EnableEpochFlag{ common.ScheduledMiniBlocksFlag, common.StakingV2Flag, + common.CurrentRandomnessOnSortingFlag, }) if err != nil { return err diff --git a/process/block/helpers/txsorting.go b/process/block/helpers/txsorting.go new file mode 100644 index 00000000000..19de2427dfe --- /dev/null +++ b/process/block/helpers/txsorting.go @@ -0,0 +1,15 @@ +package helpers + +import ( + "github.com/multiversx/mx-chain-core-go/data" + "github.com/multiversx/mx-chain-go/common" +) + +// ComputeRandomnessForTxSorting returns the randomness for transactions sorting +func ComputeRandomnessForTxSorting(header data.HeaderHandler, enableEpochsHandler common.EnableEpochsHandler) []byte { + if enableEpochsHandler.IsFlagEnabled(common.CurrentRandomnessOnSortingFlag) { + return header.GetRandSeed() + } + + return header.GetPrevRandSeed() +} diff --git a/process/block/helpers/txsorting_test.go b/process/block/helpers/txsorting_test.go new file mode 100644 index 00000000000..b4bcf500d5e --- /dev/null +++ b/process/block/helpers/txsorting_test.go @@ -0,0 +1,40 @@ +package helpers + +import ( + "testing" + + "github.com/multiversx/mx-chain-core-go/core" + "github.com/multiversx/mx-chain-core-go/data/block" + "github.com/multiversx/mx-chain-go/testscommon/enableEpochsHandlerMock" + "github.com/stretchr/testify/require" +) + +func TestComputeRandomnessForTxSorting(t *testing.T) { + t.Parallel() + + header := &block.Header{ + RandSeed: []byte{0x01}, + PrevRandSeed: []byte{0x02}, + } + + t.Run("flag not active should return previous randomness", func(t *testing.T) { + t.Parallel() + + enableEpochsHandler := &enableEpochsHandlerMock.EnableEpochsHandlerStub{ + IsFlagEnabledCalled: func(flag core.EnableEpochFlag) bool { + return false + }, + } + require.Equal(t, header.PrevRandSeed, ComputeRandomnessForTxSorting(header, enableEpochsHandler)) + }) + t.Run("flag active should return current randomness", func(t *testing.T) { + t.Parallel() + + enableEpochsHandler := &enableEpochsHandlerMock.EnableEpochsHandlerStub{ + IsFlagEnabledCalled: func(flag core.EnableEpochFlag) bool { + return true + }, + } + require.Equal(t, header.RandSeed, ComputeRandomnessForTxSorting(header, enableEpochsHandler)) + }) +} diff --git a/process/block/metablock.go b/process/block/metablock.go index a3fd32450cf..49b2504b3ce 100644 --- a/process/block/metablock.go +++ b/process/block/metablock.go @@ -18,6 +18,7 @@ import ( processOutport "github.com/multiversx/mx-chain-go/outport/process" "github.com/multiversx/mx-chain-go/process" "github.com/multiversx/mx-chain-go/process/block/bootstrapStorage" + "github.com/multiversx/mx-chain-go/process/block/helpers" "github.com/multiversx/mx-chain-go/process/block/processedMb" "github.com/multiversx/mx-chain-go/state" logger "github.com/multiversx/mx-chain-logger-go" @@ -928,7 +929,8 @@ func (mp *metaProcessor) createBlockBody(metaBlock data.HeaderHandler, haveTime "nonce", metaBlock.GetNonce(), ) - miniBlocks, err := mp.createMiniBlocks(haveTime, metaBlock.GetPrevRandSeed()) + randomness := helpers.ComputeRandomnessForTxSorting(metaBlock, mp.enableEpochsHandler) + miniBlocks, err := mp.createMiniBlocks(haveTime, randomness) if err != nil { return nil, err } diff --git a/process/block/preprocess/transactions.go b/process/block/preprocess/transactions.go index 1a52524048e..fd53f95aad5 100644 --- a/process/block/preprocess/transactions.go +++ b/process/block/preprocess/transactions.go @@ -18,6 +18,7 @@ import ( "github.com/multiversx/mx-chain-go/common" "github.com/multiversx/mx-chain-go/dataRetriever" "github.com/multiversx/mx-chain-go/process" + "github.com/multiversx/mx-chain-go/process/block/helpers" "github.com/multiversx/mx-chain-go/sharding" "github.com/multiversx/mx-chain-go/state" "github.com/multiversx/mx-chain-go/storage" @@ -140,6 +141,7 @@ func NewTransactionPreprocessor( common.OptimizeGasUsedInCrossMiniBlocksFlag, common.ScheduledMiniBlocksFlag, common.FrontRunningProtectionFlag, + common.CurrentRandomnessOnSortingFlag, }) if err != nil { return nil, err @@ -332,7 +334,8 @@ func (txs *transactions) ProcessBlockTransactions( } if txs.isBodyFromMe(body) { - return txs.processTxsFromMe(body, haveTime, header.GetPrevRandSeed()) + randomness := helpers.ComputeRandomnessForTxSorting(header, txs.enableEpochsHandler) + return txs.processTxsFromMe(body, haveTime, randomness) } return process.ErrInvalidBody diff --git a/process/block/shardblock.go b/process/block/shardblock.go index 6482df61730..91e2d79d29f 100644 --- a/process/block/shardblock.go +++ b/process/block/shardblock.go @@ -16,6 +16,7 @@ import ( processOutport "github.com/multiversx/mx-chain-go/outport/process" "github.com/multiversx/mx-chain-go/process" "github.com/multiversx/mx-chain-go/process/block/bootstrapStorage" + "github.com/multiversx/mx-chain-go/process/block/helpers" "github.com/multiversx/mx-chain-go/process/block/processedMb" "github.com/multiversx/mx-chain-go/state" logger "github.com/multiversx/mx-chain-logger-go" @@ -875,7 +876,8 @@ func (sp *shardProcessor) createBlockBody(shardHdr data.HeaderHandler, haveTime "nonce", shardHdr.GetNonce(), ) - miniBlocks, processedMiniBlocksDestMeInfo, err := sp.createMiniBlocks(haveTime, shardHdr.GetPrevRandSeed()) + randomness := helpers.ComputeRandomnessForTxSorting(shardHdr, sp.enableEpochsHandler) + miniBlocks, processedMiniBlocksDestMeInfo, err := sp.createMiniBlocks(haveTime, randomness) if err != nil { return nil, nil, err }