Skip to content

Commit

Permalink
force change of epoch
Browse files Browse the repository at this point in the history
  • Loading branch information
miiu96 committed May 16, 2024
1 parent ad05efd commit f47cf0c
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 0 deletions.
16 changes: 16 additions & 0 deletions node/chainSimulator/chainSimulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,22 @@ func (s *simulator) incrementRoundOnAllValidators() {
}
}

// ForceChangeOfEpoch will force the change of current epoch
// This method will call the epoch change trigger and generate block till a new epoch is reached
func (s *simulator) ForceChangeOfEpoch() error {
log.Info("force change of epoch")
for shardID, node := range s.nodes {
err := node.ForceChangeOfEpoch()
if err != nil {
return fmt.Errorf("force change of epoch shardID-%d: error-%w", shardID, err)
}
}

epoch := s.nodes[core.MetachainShardId].GetProcessComponents().EpochStartTrigger().Epoch()

return s.GenerateBlocksUntilEpochIsReached(int32(epoch + 1))
}

func (s *simulator) allNodesCreateBlocks() error {
for _, node := range s.handlers {
// TODO MX-15150 remove this when we remove all goroutines
Expand Down
43 changes: 43 additions & 0 deletions node/chainSimulator/chainSimulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,49 @@ func TestChainSimulator_GenerateBlocksAndEpochChangeShouldWork(t *testing.T) {
assert.True(t, numAccountsWithIncreasedBalances > 0)
}

func TestSimulator_TriggerChangeOfEpoch(t *testing.T) {
if testing.Short() {
t.Skip("this is not a short test")
}

startTime := time.Now().Unix()
roundDurationInMillis := uint64(6000)
roundsPerEpoch := core.OptionalUint64{
HasValue: true,
Value: 15000,
}
chainSimulator, err := NewChainSimulator(ArgsChainSimulator{
BypassTxSignatureCheck: false,
TempDir: t.TempDir(),
PathToInitialConfig: defaultPathToInitialConfig,
NumOfShards: 3,
GenesisTimestamp: startTime,
RoundDurationInMillis: roundDurationInMillis,
RoundsPerEpoch: roundsPerEpoch,
ApiInterface: api.NewNoApiInterface(),
MinNodesPerShard: 100,
MetaChainMinNodes: 100,
ConsensusGroupSize: 1,
MetaChainConsensusGroupSize: 1,
})
require.Nil(t, err)
require.NotNil(t, chainSimulator)

defer chainSimulator.Close()

err = chainSimulator.ForceChangeOfEpoch()
require.Nil(t, err)

err = chainSimulator.ForceChangeOfEpoch()
require.Nil(t, err)

err = chainSimulator.ForceChangeOfEpoch()
require.Nil(t, err)

err = chainSimulator.ForceChangeOfEpoch()
require.Nil(t, err)
}

func TestChainSimulator_SetState(t *testing.T) {
if testing.Short() {
t.Skip("this is not a short test")
Expand Down
12 changes: 12 additions & 0 deletions node/chainSimulator/components/testOnlyProcessingNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,18 @@ func (node *testOnlyProcessingNode) RemoveAccount(address []byte) error {
return err
}

// ForceChangeOfEpoch will force change of epoch
func (node *testOnlyProcessingNode) ForceChangeOfEpoch() error {
currentHeader := node.DataComponentsHolder.Blockchain().GetCurrentBlockHeader()
if currentHeader == nil {
currentHeader = node.DataComponentsHolder.Blockchain().GetGenesisHeader()
}

node.ProcessComponentsHolder.EpochStartTrigger().ForceEpochStart(currentHeader.GetRound() + 1)

return nil
}

func setNonceAndBalanceForAccount(userAccount state.UserAccountHandler, nonce *uint64, balance string) error {
if nonce != nil {
// set nonce to zero
Expand Down
1 change: 1 addition & 0 deletions node/chainSimulator/configs/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func CreateChainSimulatorConfigs(args ArgsChainSimulatorConfigs) (*ArgsConfigsSi

configs.GeneralConfig.EpochStartConfig.ExtraDelayForRequestBlockInfoInMilliseconds = 1
configs.GeneralConfig.EpochStartConfig.GenesisEpoch = args.InitialEpoch
configs.GeneralConfig.EpochStartConfig.MinRoundsBetweenEpochs = 1

if args.RoundsPerEpoch.HasValue {
configs.GeneralConfig.EpochStartConfig.RoundsPerEpoch = int64(args.RoundsPerEpoch.Value)
Expand Down
1 change: 1 addition & 0 deletions node/chainSimulator/process/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type NodeHandler interface {
SetKeyValueForAddress(addressBytes []byte, state map[string]string) error
SetStateForAddress(address []byte, state *dtos.AddressState) error
RemoveAccount(address []byte) error
ForceChangeOfEpoch() error
Close() error
IsInterfaceNil() bool
}
5 changes: 5 additions & 0 deletions testscommon/chainSimulator/nodeHandlerMock.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ type NodeHandlerMock struct {
CloseCalled func() error
}

// ForceChangeOfEpoch -
func (mock *NodeHandlerMock) ForceChangeOfEpoch() error {
return nil
}

// GetProcessComponents -
func (mock *NodeHandlerMock) GetProcessComponents() factory.ProcessComponentsHolder {
if mock.GetProcessComponentsCalled != nil {
Expand Down

0 comments on commit f47cf0c

Please sign in to comment.