Skip to content

Commit

Permalink
Merge pull request #5683 from multiversx/sorting-on-current-randomness
Browse files Browse the repository at this point in the history
switch to current block randomness for ordering transactions
  • Loading branch information
iulianpascalau authored Jan 16, 2024
2 parents 023a194 + 945c4fb commit d49fcca
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 6 deletions.
3 changes: 3 additions & 0 deletions cmd/node/config/enableEpochs.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down
1 change: 1 addition & 0 deletions common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
6 changes: 6 additions & 0 deletions common/enablers/enableEpochsHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
}

Expand Down
3 changes: 3 additions & 0 deletions common/enablers/enableEpochsHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func createEnableEpochsConfig() config.EnableEpochs {
NFTStopCreateEnableEpoch: 92,
FixGasRemainingForSaveKeyValueBuiltinFunctionEnableEpoch: 93,
ChangeOwnerAddressCrossShardThroughSCEnableEpoch: 94,
CurrentRandomnessOnSortingEnableEpoch: 95,
}
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions config/epochConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ type EnableEpochs struct {
NFTStopCreateEnableEpoch uint32
ChangeOwnerAddressCrossShardThroughSCEnableEpoch uint32
FixGasRemainingForSaveKeyValueBuiltinFunctionEnableEpoch uint32
CurrentRandomnessOnSortingEnableEpoch uint32
BLSMultiSignerEnableEpoch []MultiSignerConfig
}

Expand Down
4 changes: 4 additions & 0 deletions config/tomlConfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -950,6 +953,7 @@ func TestEnableEpochConfig(t *testing.T) {
NFTStopCreateEnableEpoch: 89,
ChangeOwnerAddressCrossShardThroughSCEnableEpoch: 90,
FixGasRemainingForSaveKeyValueBuiltinFunctionEnableEpoch: 91,
CurrentRandomnessOnSortingEnableEpoch: 92,
MaxNodesChangeEnableEpoch: []MaxNodesChangeConfig{
{
EpochEnable: 44,
Expand Down
9 changes: 6 additions & 3 deletions integrationTests/factory/componentsHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions process/block/baseProcess.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions process/block/helpers/txsorting.go
Original file line number Diff line number Diff line change
@@ -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()
}
40 changes: 40 additions & 0 deletions process/block/helpers/txsorting_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
}
4 changes: 3 additions & 1 deletion process/block/metablock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 4 additions & 1 deletion process/block/preprocess/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -140,6 +141,7 @@ func NewTransactionPreprocessor(
common.OptimizeGasUsedInCrossMiniBlocksFlag,
common.ScheduledMiniBlocksFlag,
common.FrontRunningProtectionFlag,
common.CurrentRandomnessOnSortingFlag,
})
if err != nil {
return nil, err
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion process/block/shardblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit d49fcca

Please sign in to comment.