Skip to content

Commit

Permalink
Merge pull request #6221 from multiversx/update-feat-chain-simulator-…
Browse files Browse the repository at this point in the history
…30.05

Update feat chain simulator 30.05
  • Loading branch information
miiu96 authored May 30, 2024
2 parents 256f000 + 31cfe98 commit 19b1685
Show file tree
Hide file tree
Showing 13 changed files with 525 additions and 474 deletions.
45 changes: 45 additions & 0 deletions integrationTests/chainSimulator/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package chainSimulator

import (
"math/big"

"github.com/multiversx/mx-chain-go/node/chainSimulator/configs"

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

const (
minGasPrice = 1000000000
txVersion = 1
mockTxSignature = "sig"

// OkReturnCode the const for the ok return code
OkReturnCode = "ok"
)

var (
// ZeroValue the variable for the zero big int
ZeroValue = big.NewInt(0)
// OneEGLD the variable for one egld value
OneEGLD = big.NewInt(1000000000000000000)
// MinimumStakeValue the variable for the minimum stake value
MinimumStakeValue = big.NewInt(0).Mul(OneEGLD, big.NewInt(2500))
// InitialAmount the variable for initial minting amount in account
InitialAmount = big.NewInt(0).Mul(OneEGLD, big.NewInt(100))
)

// GenerateTransaction will generate a transaction based on input data
func GenerateTransaction(sender []byte, nonce uint64, receiver []byte, value *big.Int, data string, gasLimit uint64) *transaction.Transaction {
return &transaction.Transaction{
Nonce: nonce,
Value: value,
SndAddr: sender,
RcvAddr: receiver,
Data: []byte(data),
GasLimit: gasLimit,
GasPrice: minGasPrice,
ChainID: []byte(configs.ChainID),
Version: txVersion,
Signature: []byte(mockTxSignature),
}
}
8 changes: 6 additions & 2 deletions integrationTests/chainSimulator/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package chainSimulator
import (
"math/big"

"github.com/multiversx/mx-chain-go/node/chainSimulator/dtos"
"github.com/multiversx/mx-chain-go/node/chainSimulator/process"

"github.com/multiversx/mx-chain-core-go/data/api"
"github.com/multiversx/mx-chain-core-go/data/transaction"
crypto "github.com/multiversx/mx-chain-crypto-go"
"github.com/multiversx/mx-chain-go/node/chainSimulator/dtos"
"github.com/multiversx/mx-chain-go/node/chainSimulator/process"
)

// ChainSimulator defines the operations for an entity that can simulate operations of a chain
Expand All @@ -16,6 +17,7 @@ type ChainSimulator interface {
GenerateBlocksUntilEpochIsReached(targetEpoch int32) error
AddValidatorKeys(validatorsPrivateKeys [][]byte) error
GetNodeHandler(shardID uint32) process.NodeHandler
RemoveAccounts(addresses []string) error
SendTxAndGenerateBlockTilTxIsExecuted(txToSend *transaction.Transaction, maxNumOfBlockToGenerateWhenExecutingTx int) (*transaction.ApiTransactionResult, error)
SendTxsAndGenerateBlocksTilAreExecuted(txsToSend []*transaction.Transaction, maxNumOfBlocksToGenerateWhenExecutingTx int) ([]*transaction.ApiTransactionResult, error)
SetStateMultiple(stateSlice []*dtos.AddressState) error
Expand All @@ -24,4 +26,6 @@ type ChainSimulator interface {
GetAccount(address dtos.WalletAddress) (api.AccountResponse, error)
ForceResetValidatorStatisticsCache() error
GetValidatorPrivateKeys() []crypto.PrivateKey
SetKeyValueForAddress(address string, keyValueMap map[string]string) error
Close()
}
39 changes: 5 additions & 34 deletions integrationTests/chainSimulator/staking/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,17 @@ import (
"math/big"
"testing"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/data/transaction"
chainSimulatorIntegrationTests "github.com/multiversx/mx-chain-go/integrationTests/chainSimulator"
"github.com/multiversx/mx-chain-go/node/chainSimulator/configs"
"github.com/multiversx/mx-chain-go/node/chainSimulator/dtos"
chainSimulatorProcess "github.com/multiversx/mx-chain-go/node/chainSimulator/process"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/vm"

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

const (
minGasPrice = 1000000000
txVersion = 1
mockTxSignature = "sig"

// OkReturnCode the const for the ok return code
OkReturnCode = "ok"
// MockBLSSignature the const for a mocked bls signature
MockBLSSignature = "010101"
// GasLimitForStakeOperation the const for the gas limit value for the stake operation
Expand All @@ -45,14 +38,8 @@ const (
)

var (
// ZeroValue the variable for the zero big int
ZeroValue = big.NewInt(0)
// OneEGLD the variable for one egld value
OneEGLD = big.NewInt(1000000000000000000)
//InitialDelegationValue the variable for the initial delegation value
InitialDelegationValue = big.NewInt(0).Mul(OneEGLD, big.NewInt(1250))
// MinimumStakeValue the variable for the minimum stake value
MinimumStakeValue = big.NewInt(0).Mul(OneEGLD, big.NewInt(2500))
InitialDelegationValue = big.NewInt(0).Mul(chainSimulatorIntegrationTests.OneEGLD, big.NewInt(1250))
)

// GetNonce will return the nonce of the provided address
Expand All @@ -63,22 +50,6 @@ func GetNonce(t *testing.T, cs chainSimulatorIntegrationTests.ChainSimulator, ad
return account.Nonce
}

// GenerateTransaction will generate a transaction based on input data
func GenerateTransaction(sender []byte, nonce uint64, receiver []byte, value *big.Int, data string, gasLimit uint64) *transaction.Transaction {
return &transaction.Transaction{
Nonce: nonce,
Value: value,
SndAddr: sender,
RcvAddr: receiver,
Data: []byte(data),
GasLimit: gasLimit,
GasPrice: minGasPrice,
ChainID: []byte(configs.ChainID),
Version: txVersion,
Signature: []byte(mockTxSignature),
}
}

// GetBLSKeyStatus will return the bls key status
func GetBLSKeyStatus(t *testing.T, metachainNode chainSimulatorProcess.NodeHandler, blsKey []byte) string {
scQuery := &process.SCQuery{
Expand All @@ -90,7 +61,7 @@ func GetBLSKeyStatus(t *testing.T, metachainNode chainSimulatorProcess.NodeHandl
}
result, _, err := metachainNode.GetFacadeHandler().ExecuteSCQuery(scQuery)
require.Nil(t, err)
require.Equal(t, OkReturnCode, result.ReturnCode)
require.Equal(t, chainSimulatorIntegrationTests.OkReturnCode, result.ReturnCode)

return string(result.ReturnData[0])
}
Expand All @@ -105,7 +76,7 @@ func GetAllNodeStates(t *testing.T, metachainNode chainSimulatorProcess.NodeHand
}
result, _, err := metachainNode.GetFacadeHandler().ExecuteSCQuery(scQuery)
require.Nil(t, err)
require.Equal(t, OkReturnCode, result.ReturnCode)
require.Equal(t, chainSimulatorIntegrationTests.OkReturnCode, result.ReturnCode)

m := make(map[string]string)
status := ""
Expand Down
15 changes: 8 additions & 7 deletions integrationTests/chainSimulator/staking/jail/jail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/config"
chainSimulatorIntegrationTests "github.com/multiversx/mx-chain-go/integrationTests/chainSimulator"
"github.com/multiversx/mx-chain-go/integrationTests/chainSimulator/staking"
"github.com/multiversx/mx-chain-go/node/chainSimulator"
"github.com/multiversx/mx-chain-go/node/chainSimulator/components/api"
Expand Down Expand Up @@ -96,12 +97,12 @@ func testChainSimulatorJailAndUnJail(t *testing.T, targetEpoch int32, nodeStatus
_, blsKeys, err := chainSimulator.GenerateBlsPrivateKeys(1)
require.Nil(t, err)

mintValue := big.NewInt(0).Mul(staking.OneEGLD, big.NewInt(3000))
mintValue := big.NewInt(0).Mul(chainSimulatorIntegrationTests.OneEGLD, big.NewInt(3000))
walletAddress, err := cs.GenerateAndMintWalletAddress(core.AllShardId, mintValue)
require.Nil(t, err)

txDataField := fmt.Sprintf("stake@01@%s@%s", blsKeys[0], staking.MockBLSSignature)
txStake := staking.GenerateTransaction(walletAddress.Bytes, 0, vm.ValidatorSCAddress, staking.MinimumStakeValue, txDataField, staking.GasLimitForStakeOperation)
txStake := chainSimulatorIntegrationTests.GenerateTransaction(walletAddress.Bytes, 0, vm.ValidatorSCAddress, chainSimulatorIntegrationTests.MinimumStakeValue, txDataField, staking.GasLimitForStakeOperation)
stakeTx, err := cs.SendTxAndGenerateBlockTilTxIsExecuted(txStake, staking.MaxNumOfBlockToGenerateWhenExecutingTx)
require.Nil(t, err)
require.NotNil(t, stakeTx)
Expand All @@ -117,7 +118,7 @@ func testChainSimulatorJailAndUnJail(t *testing.T, targetEpoch int32, nodeStatus
// do an unjail transaction
unJailValue, _ := big.NewInt(0).SetString("2500000000000000000", 10)
txUnJailDataField := fmt.Sprintf("unJail@%s", blsKeys[0])
txUnJail := staking.GenerateTransaction(walletAddress.Bytes, 1, vm.ValidatorSCAddress, unJailValue, txUnJailDataField, staking.GasLimitForStakeOperation)
txUnJail := chainSimulatorIntegrationTests.GenerateTransaction(walletAddress.Bytes, 1, vm.ValidatorSCAddress, unJailValue, txUnJailDataField, staking.GasLimitForStakeOperation)

err = cs.GenerateBlocksUntilEpochIsReached(targetEpoch)
require.Nil(t, err)
Expand Down Expand Up @@ -202,12 +203,12 @@ func TestChainSimulator_FromQueueToAuctionList(t *testing.T) {
err = cs.AddValidatorKeys([][]byte{privateKeys[1]})
require.Nil(t, err)

mintValue := big.NewInt(0).Mul(staking.OneEGLD, big.NewInt(6000))
mintValue := big.NewInt(0).Mul(chainSimulatorIntegrationTests.OneEGLD, big.NewInt(6000))
walletAddress, err := cs.GenerateAndMintWalletAddress(core.AllShardId, mintValue)
require.Nil(t, err)

txDataField := fmt.Sprintf("stake@01@%s@%s", blsKeys[0], staking.MockBLSSignature)
txStake := staking.GenerateTransaction(walletAddress.Bytes, 0, vm.ValidatorSCAddress, staking.MinimumStakeValue, txDataField, staking.GasLimitForStakeOperation)
txStake := chainSimulatorIntegrationTests.GenerateTransaction(walletAddress.Bytes, 0, vm.ValidatorSCAddress, chainSimulatorIntegrationTests.MinimumStakeValue, txDataField, staking.GasLimitForStakeOperation)
stakeTx, err := cs.SendTxAndGenerateBlockTilTxIsExecuted(txStake, staking.MaxNumOfBlockToGenerateWhenExecutingTx)
require.Nil(t, err)
require.NotNil(t, stakeTx)
Expand All @@ -222,7 +223,7 @@ func TestChainSimulator_FromQueueToAuctionList(t *testing.T) {

// add one more node
txDataField = fmt.Sprintf("stake@01@%s@%s", blsKeys[1], staking.MockBLSSignature)
txStake = staking.GenerateTransaction(walletAddress.Bytes, 1, vm.ValidatorSCAddress, staking.MinimumStakeValue, txDataField, staking.GasLimitForStakeOperation)
txStake = chainSimulatorIntegrationTests.GenerateTransaction(walletAddress.Bytes, 1, vm.ValidatorSCAddress, chainSimulatorIntegrationTests.MinimumStakeValue, txDataField, staking.GasLimitForStakeOperation)
stakeTx, err = cs.SendTxAndGenerateBlockTilTxIsExecuted(txStake, staking.MaxNumOfBlockToGenerateWhenExecutingTx)
require.Nil(t, err)
require.NotNil(t, stakeTx)
Expand All @@ -234,7 +235,7 @@ func TestChainSimulator_FromQueueToAuctionList(t *testing.T) {
// unJail the first node
unJailValue, _ := big.NewInt(0).SetString("2500000000000000000", 10)
txUnJailDataField := fmt.Sprintf("unJail@%s", blsKeys[0])
txUnJail := staking.GenerateTransaction(walletAddress.Bytes, 2, vm.ValidatorSCAddress, unJailValue, txUnJailDataField, staking.GasLimitForStakeOperation)
txUnJail := chainSimulatorIntegrationTests.GenerateTransaction(walletAddress.Bytes, 2, vm.ValidatorSCAddress, unJailValue, txUnJailDataField, staking.GasLimitForStakeOperation)

unJailTx, err := cs.SendTxAndGenerateBlockTilTxIsExecuted(txUnJail, staking.MaxNumOfBlockToGenerateWhenExecutingTx)
require.Nil(t, err)
Expand Down
27 changes: 14 additions & 13 deletions integrationTests/chainSimulator/staking/stake/simpleStake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ import (
"testing"
"time"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/stretchr/testify/require"

"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/config"
chainSimulatorIntegrationTests "github.com/multiversx/mx-chain-go/integrationTests/chainSimulator"
"github.com/multiversx/mx-chain-go/integrationTests/chainSimulator/staking"
"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/configs"
"github.com/multiversx/mx-chain-go/node/chainSimulator/process"
"github.com/multiversx/mx-chain-go/vm"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/stretchr/testify/require"
)

// Test scenarios
Expand Down Expand Up @@ -87,7 +88,7 @@ func testChainSimulatorSimpleStake(t *testing.T, targetEpoch int32, nodesStatus
require.NotNil(t, cs)
defer cs.Close()

mintValue := big.NewInt(0).Mul(staking.OneEGLD, big.NewInt(3000))
mintValue := big.NewInt(0).Mul(chainSimulatorIntegrationTests.OneEGLD, big.NewInt(3000))
wallet1, err := cs.GenerateAndMintWalletAddress(0, mintValue)
require.Nil(t, err)
wallet2, err := cs.GenerateAndMintWalletAddress(0, mintValue)
Expand All @@ -102,15 +103,15 @@ func testChainSimulatorSimpleStake(t *testing.T, targetEpoch int32, nodesStatus
require.Nil(t, err)

dataFieldTx1 := fmt.Sprintf("stake@01@%s@%s", blsKeys[0], staking.MockBLSSignature)
tx1Value := big.NewInt(0).Mul(big.NewInt(2499), staking.OneEGLD)
tx1 := staking.GenerateTransaction(wallet1.Bytes, 0, vm.ValidatorSCAddress, tx1Value, dataFieldTx1, staking.GasLimitForStakeOperation)
tx1Value := big.NewInt(0).Mul(big.NewInt(2499), chainSimulatorIntegrationTests.OneEGLD)
tx1 := chainSimulatorIntegrationTests.GenerateTransaction(wallet1.Bytes, 0, vm.ValidatorSCAddress, tx1Value, dataFieldTx1, staking.GasLimitForStakeOperation)

dataFieldTx2 := fmt.Sprintf("stake@01@%s@%s", blsKeys[1], staking.MockBLSSignature)
tx2 := staking.GenerateTransaction(wallet3.Bytes, 0, vm.ValidatorSCAddress, staking.MinimumStakeValue, dataFieldTx2, staking.GasLimitForStakeOperation)
tx2 := chainSimulatorIntegrationTests.GenerateTransaction(wallet3.Bytes, 0, vm.ValidatorSCAddress, chainSimulatorIntegrationTests.MinimumStakeValue, dataFieldTx2, staking.GasLimitForStakeOperation)

dataFieldTx3 := fmt.Sprintf("stake@01@%s@%s", blsKeys[2], staking.MockBLSSignature)
tx3Value := big.NewInt(0).Mul(big.NewInt(2501), staking.OneEGLD)
tx3 := staking.GenerateTransaction(wallet2.Bytes, 0, vm.ValidatorSCAddress, tx3Value, dataFieldTx3, staking.GasLimitForStakeOperation)
tx3Value := big.NewInt(0).Mul(big.NewInt(2501), chainSimulatorIntegrationTests.OneEGLD)
tx3 := chainSimulatorIntegrationTests.GenerateTransaction(wallet2.Bytes, 0, vm.ValidatorSCAddress, tx3Value, dataFieldTx3, staking.GasLimitForStakeOperation)

results, err := cs.SendTxsAndGenerateBlocksTilAreExecuted([]*transaction.Transaction{tx1, tx2, tx3}, staking.MaxNumOfBlockToGenerateWhenExecutingTx)
require.Nil(t, err)
Expand Down Expand Up @@ -200,13 +201,13 @@ func TestChainSimulator_StakingV4Step2APICalls(t *testing.T) {
err = cs.AddValidatorKeys(privateKey)
require.Nil(t, err)

mintValue := big.NewInt(0).Add(staking.MinimumStakeValue, staking.OneEGLD)
mintValue := big.NewInt(0).Add(chainSimulatorIntegrationTests.MinimumStakeValue, chainSimulatorIntegrationTests.OneEGLD)
validatorOwner, err := cs.GenerateAndMintWalletAddress(core.AllShardId, mintValue)
require.Nil(t, err)

// Stake a new validator that should end up in auction in step 1
txDataField := fmt.Sprintf("stake@01@%s@%s", blsKeys[0], staking.MockBLSSignature)
txStake := staking.GenerateTransaction(validatorOwner.Bytes, 0, vm.ValidatorSCAddress, staking.MinimumStakeValue, txDataField, staking.GasLimitForStakeOperation)
txStake := chainSimulatorIntegrationTests.GenerateTransaction(validatorOwner.Bytes, 0, vm.ValidatorSCAddress, chainSimulatorIntegrationTests.MinimumStakeValue, txDataField, staking.GasLimitForStakeOperation)
stakeTx, err := cs.SendTxAndGenerateBlockTilTxIsExecuted(txStake, staking.MaxNumOfBlockToGenerateWhenExecutingTx)
require.Nil(t, err)
require.NotNil(t, stakeTx)
Expand All @@ -226,7 +227,7 @@ func TestChainSimulator_StakingV4Step2APICalls(t *testing.T) {

// re-stake the node
txDataField = fmt.Sprintf("reStakeUnStakedNodes@%s", blsKeys[0])
txReStake := staking.GenerateTransaction(validatorOwner.Bytes, 1, vm.ValidatorSCAddress, big.NewInt(0), txDataField, staking.GasLimitForStakeOperation)
txReStake := chainSimulatorIntegrationTests.GenerateTransaction(validatorOwner.Bytes, 1, vm.ValidatorSCAddress, big.NewInt(0), txDataField, staking.GasLimitForStakeOperation)
reStakeTx, err := cs.SendTxAndGenerateBlockTilTxIsExecuted(txReStake, staking.MaxNumOfBlockToGenerateWhenExecutingTx)
require.Nil(t, err)
require.NotNil(t, reStakeTx)
Expand Down
Loading

0 comments on commit 19b1685

Please sign in to comment.