Skip to content

Commit

Permalink
sovereign chain simulator updates
Browse files Browse the repository at this point in the history
  • Loading branch information
axenteoctavian committed Feb 3, 2025
1 parent 965f50c commit d28fb21
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
109 changes: 109 additions & 0 deletions cmd/sovereignnode/chainSimulator/sovereignChainSimulator_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package chainSimulator

import (
"math/big"
"testing"
"time"

"github.com/multiversx/mx-chain-core-go/core"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"
"github.com/stretchr/testify/require"

chainSimulatorCommon "github.com/multiversx/mx-chain-go/integrationTests/chainSimulator"
chainSim "github.com/multiversx/mx-chain-go/node/chainSimulator"
"github.com/multiversx/mx-chain-go/node/chainSimulator/components/api"
"github.com/multiversx/mx-chain-go/node/chainSimulator/dtos"
"github.com/multiversx/mx-chain-go/node/chainSimulator/process"
)

const (
Expand Down Expand Up @@ -76,6 +79,112 @@ func TestChainSimulator_GenerateBlocksShouldWork(t *testing.T) {
require.Nil(t, err)
}

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

chainSimulator, err := NewSovereignChainSimulator(ArgsSovereignChainSimulator{
SovereignConfigPath: sovereignConfigPath,
ArgsChainSimulator: &chainSim.ArgsChainSimulator{
BypassTxSignatureCheck: false,
TempDir: t.TempDir(),
PathToInitialConfig: defaultPathToInitialConfig,
GenesisTimestamp: time.Now().Unix(),
RoundDurationInMillis: uint64(6000),
RoundsPerEpoch: core.OptionalUint64{
HasValue: true,
Value: 20,
},
ApiInterface: api.NewNoApiInterface(),
MinNodesPerShard: 1,
},
})
require.Nil(t, err)
require.NotNil(t, chainSimulator)

defer chainSimulator.Close()

nodeHandler := chainSimulator.GetNodeHandler(core.SovereignChainShardId)

genesisBalances := make(map[string]*big.Int)
for _, stakeWallet := range chainSimulator.GetInitialWalletKeys().StakeWallets {
initialAccount, errGet := getAccount(nodeHandler, stakeWallet.Address.Bytes)
require.Nil(t, errGet)

genesisBalances[stakeWallet.Address.Bech32] = initialAccount.GetBalance()
}

time.Sleep(time.Second)

err = chainSimulator.GenerateBlocks(80)
require.Nil(t, err)

numAccountsWithIncreasedBalances := 0
for _, stakeWallet := range chainSimulator.GetInitialWalletKeys().StakeWallets {
account, errGet := getAccount(nodeHandler, stakeWallet.Address.Bytes)
require.Nil(t, errGet)

if account.GetBalance().Cmp(genesisBalances[stakeWallet.Address.Bech32]) > 0 {
numAccountsWithIncreasedBalances++
}
}

require.True(t, numAccountsWithIncreasedBalances > 0)
}

func getAccount(nodeHandler process.NodeHandler, addressBytes []byte) (vmcommon.UserAccountHandler, error) {
account, err := nodeHandler.GetStateComponents().AccountsAdapter().GetExistingAccount(addressBytes)
if err != nil {
return nil, err
}

return account.(vmcommon.UserAccountHandler), nil
}

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

chainSimulator, err := NewSovereignChainSimulator(ArgsSovereignChainSimulator{
SovereignConfigPath: sovereignConfigPath,
ArgsChainSimulator: &chainSim.ArgsChainSimulator{
BypassTxSignatureCheck: false,
TempDir: t.TempDir(),
PathToInitialConfig: defaultPathToInitialConfig,
GenesisTimestamp: time.Now().Unix(),
RoundDurationInMillis: uint64(6000),
RoundsPerEpoch: core.OptionalUint64{
HasValue: true,
Value: 20,
},
ApiInterface: api.NewNoApiInterface(),
MinNodesPerShard: 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)

nodeHandler := chainSimulator.GetNodeHandler(core.SovereignChainShardId)
currentEpoch := nodeHandler.GetProcessComponents().EpochStartTrigger().Epoch()
require.Equal(t, uint32(4), currentEpoch)
}

func TestChainSimulator_SetState(t *testing.T) {
if testing.Short() {
t.Skip("this is not a short test")
Expand Down
26 changes: 26 additions & 0 deletions node/chainSimulator/sovereignChainSimulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package chainSimulator

import (
"fmt"

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

type sovereignChainSimulator struct {
Expand Down Expand Up @@ -53,3 +55,27 @@ func (ss *sovereignChainSimulator) isSovereignTargetEpochReached(targetEpoch int

return true
}

// ForceResetValidatorStatisticsCache will force the reset of the cache used for the validators statistics endpoint
func (ss *sovereignChainSimulator) ForceResetValidatorStatisticsCache() error {
return ss.GetNodeHandler(core.SovereignChainShardId).GetProcessComponents().ValidatorsProvider().ForceUpdate()
}

// 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 (ss *sovereignChainSimulator) ForceChangeOfEpoch() error {
ss.mutex.Lock()
log.Info("force change of epoch")

node := ss.nodes[core.SovereignChainShardId]
err := node.ForceChangeOfEpoch()
if err != nil {
ss.mutex.Unlock()
return fmt.Errorf("force change of epoch error-%w", err)
}

epoch := node.GetProcessComponents().EpochStartTrigger().Epoch()
ss.mutex.Unlock()

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

0 comments on commit d28fb21

Please sign in to comment.