Skip to content

Commit

Permalink
Merge branch 'rc/v1.7.0' into fix-backwards-compatibility-problem-202…
Browse files Browse the repository at this point in the history
…4.04.03
  • Loading branch information
iulianpascalau committed Apr 3, 2024
2 parents 56b13a8 + ad0bc8e commit d21e61d
Show file tree
Hide file tree
Showing 6 changed files with 470 additions and 414 deletions.
133 changes: 133 additions & 0 deletions integrationTests/chainSimulator/staking/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package staking

import (
"encoding/hex"
"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/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
GasLimitForStakeOperation = 50_000_000
// GasLimitForUnBond the const for the gas limit value for the unBond operation
GasLimitForUnBond = 12_000_000
// MaxNumOfBlockToGenerateWhenExecutingTx the const for the maximum number of block to generate when execute a transaction
MaxNumOfBlockToGenerateWhenExecutingTx = 7

// QueuedStatus the const for the queued status of a validators
QueuedStatus = "queued"
// StakedStatus the const for the staked status of a validators
StakedStatus = "staked"
// NotStakedStatus the const for the notStaked status of a validators
NotStakedStatus = "notStaked"
// AuctionStatus the const for the action status of a validators
AuctionStatus = "auction"
// UnStakedStatus the const for the unStaked status of a validators
UnStakedStatus = "unStaked"
)

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))
)

// GetNonce will return the nonce of the provided address
func GetNonce(t *testing.T, cs chainSimulatorIntegrationTests.ChainSimulator, address dtos.WalletAddress) uint64 {
account, err := cs.GetAccount(address)
require.Nil(t, err)

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{
ScAddress: vm.StakingSCAddress,
FuncName: "getBLSKeyStatus",
CallerAddr: vm.StakingSCAddress,
CallValue: big.NewInt(0),
Arguments: [][]byte{blsKey},
}
result, _, err := metachainNode.GetFacadeHandler().ExecuteSCQuery(scQuery)
require.Nil(t, err)
require.Equal(t, OkReturnCode, result.ReturnCode)

return string(result.ReturnData[0])
}

// GetAllNodeStates will return the status of all the nodes that belong to the provided address
func GetAllNodeStates(t *testing.T, metachainNode chainSimulatorProcess.NodeHandler, address []byte) map[string]string {
scQuery := &process.SCQuery{
ScAddress: address,
FuncName: "getAllNodeStates",
CallerAddr: vm.StakingSCAddress,
CallValue: big.NewInt(0),
}
result, _, err := metachainNode.GetFacadeHandler().ExecuteSCQuery(scQuery)
require.Nil(t, err)
require.Equal(t, OkReturnCode, result.ReturnCode)

m := make(map[string]string)
status := ""
for _, resultData := range result.ReturnData {
if len(resultData) != 96 {
// not a BLS key
status = string(resultData)
continue
}

m[hex.EncodeToString(resultData)] = status
}

return m
}

// CheckValidatorStatus will compare the status of the provided bls key with the provided expected status
func CheckValidatorStatus(t *testing.T, cs chainSimulatorIntegrationTests.ChainSimulator, blsKey string, expectedStatus string) {
err := cs.ForceResetValidatorStatisticsCache()
require.Nil(t, err)

validatorsStatistics, err := cs.GetNodeHandler(core.MetachainShardId).GetFacadeHandler().ValidatorStatisticsApi()
require.Nil(t, err)
require.Equal(t, expectedStatus, validatorsStatistics[blsKey].ValidatorStatus)
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package staking
package jail

import (
"encoding/hex"
"fmt"

"math/big"
"testing"
"time"
Expand All @@ -11,7 +12,7 @@ import (
"github.com/multiversx/mx-chain-core-go/data/transaction"
"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"
Expand All @@ -21,6 +22,7 @@ import (

const (
stakingV4JailUnJailStep1EnableEpoch = 5
defaultPathToInitialConfig = "../../../../cmd/node/config/"

epochWhenNodeIsJailed = 4
)
Expand Down Expand Up @@ -92,13 +94,13 @@ func testChainSimulatorJailAndUnJail(t *testing.T, targetEpoch int32, nodeStatus
_, blsKeys, err := chainSimulator.GenerateBlsPrivateKeys(1)
require.Nil(t, err)

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

txDataField := fmt.Sprintf("stake@01@%s@%s", blsKeys[0], mockBLSSignature)
txStake := generateTransaction(walletAddress.Bytes, 0, vm.ValidatorSCAddress, minimumStakeValue, txDataField, gasLimitForStakeOperation)
stakeTx, err := cs.SendTxAndGenerateBlockTilTxIsExecuted(txStake, maxNumOfBlockToGenerateWhenExecutingTx)
txDataField := fmt.Sprintf("stake@01@%s@%s", blsKeys[0], staking.MockBLSSignature)
txStake := staking.GenerateTransaction(walletAddress.Bytes, 0, vm.ValidatorSCAddress, staking.MinimumStakeValue, txDataField, staking.GasLimitForStakeOperation)
stakeTx, err := cs.SendTxAndGenerateBlockTilTxIsExecuted(txStake, staking.MaxNumOfBlockToGenerateWhenExecutingTx)
require.Nil(t, err)
require.NotNil(t, stakeTx)

Expand All @@ -107,39 +109,39 @@ func testChainSimulatorJailAndUnJail(t *testing.T, targetEpoch int32, nodeStatus
require.Nil(t, err)

decodedBLSKey, _ := hex.DecodeString(blsKeys[0])
status := getBLSKeyStatus(t, metachainNode, decodedBLSKey)
status := staking.GetBLSKeyStatus(t, metachainNode, decodedBLSKey)
require.Equal(t, "jailed", status)

// do an unjail transaction
unJailValue, _ := big.NewInt(0).SetString("2500000000000000000", 10)
txUnJailDataField := fmt.Sprintf("unJail@%s", blsKeys[0])
txUnJail := generateTransaction(walletAddress.Bytes, 1, vm.ValidatorSCAddress, unJailValue, txUnJailDataField, gasLimitForStakeOperation)
txUnJail := staking.GenerateTransaction(walletAddress.Bytes, 1, vm.ValidatorSCAddress, unJailValue, txUnJailDataField, staking.GasLimitForStakeOperation)

err = cs.GenerateBlocksUntilEpochIsReached(targetEpoch)
require.Nil(t, err)

unJailTx, err := cs.SendTxAndGenerateBlockTilTxIsExecuted(txUnJail, maxNumOfBlockToGenerateWhenExecutingTx)
unJailTx, err := cs.SendTxAndGenerateBlockTilTxIsExecuted(txUnJail, staking.MaxNumOfBlockToGenerateWhenExecutingTx)
require.Nil(t, err)
require.NotNil(t, unJailTx)
require.Equal(t, transaction.TxStatusSuccess, unJailTx.Status)

err = cs.GenerateBlocks(1)
require.Nil(t, err)

status = getBLSKeyStatus(t, metachainNode, decodedBLSKey)
status = staking.GetBLSKeyStatus(t, metachainNode, decodedBLSKey)
require.Equal(t, "staked", status)

checkValidatorStatus(t, cs, blsKeys[0], nodeStatusAfterUnJail)
staking.CheckValidatorStatus(t, cs, blsKeys[0], nodeStatusAfterUnJail)

err = cs.GenerateBlocksUntilEpochIsReached(targetEpoch + 1)
require.Nil(t, err)

checkValidatorStatus(t, cs, blsKeys[0], "waiting")
staking.CheckValidatorStatus(t, cs, blsKeys[0], "waiting")

err = cs.GenerateBlocksUntilEpochIsReached(targetEpoch + 2)
require.Nil(t, err)

checkValidatorStatus(t, cs, blsKeys[0], "eligible")
staking.CheckValidatorStatus(t, cs, blsKeys[0], "eligible")
}

// Test description
Expand Down Expand Up @@ -196,13 +198,13 @@ func TestChainSimulator_FromQueueToAuctionList(t *testing.T) {
err = cs.AddValidatorKeys([][]byte{privateKeys[1]})
require.Nil(t, err)

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

txDataField := fmt.Sprintf("stake@01@%s@%s", blsKeys[0], mockBLSSignature)
txStake := generateTransaction(walletAddress.Bytes, 0, vm.ValidatorSCAddress, minimumStakeValue, txDataField, gasLimitForStakeOperation)
stakeTx, err := cs.SendTxAndGenerateBlockTilTxIsExecuted(txStake, maxNumOfBlockToGenerateWhenExecutingTx)
txDataField := fmt.Sprintf("stake@01@%s@%s", blsKeys[0], staking.MockBLSSignature)
txStake := staking.GenerateTransaction(walletAddress.Bytes, 0, vm.ValidatorSCAddress, staking.MinimumStakeValue, txDataField, staking.GasLimitForStakeOperation)
stakeTx, err := cs.SendTxAndGenerateBlockTilTxIsExecuted(txStake, staking.MaxNumOfBlockToGenerateWhenExecutingTx)
require.Nil(t, err)
require.NotNil(t, stakeTx)

Expand All @@ -211,47 +213,38 @@ func TestChainSimulator_FromQueueToAuctionList(t *testing.T) {
require.Nil(t, err)

decodedBLSKey0, _ := hex.DecodeString(blsKeys[0])
status := getBLSKeyStatus(t, metachainNode, decodedBLSKey0)
status := staking.GetBLSKeyStatus(t, metachainNode, decodedBLSKey0)
require.Equal(t, "jailed", status)

// add one more node
txDataField = fmt.Sprintf("stake@01@%s@%s", blsKeys[1], mockBLSSignature)
txStake = generateTransaction(walletAddress.Bytes, 1, vm.ValidatorSCAddress, minimumStakeValue, txDataField, gasLimitForStakeOperation)
stakeTx, err = cs.SendTxAndGenerateBlockTilTxIsExecuted(txStake, maxNumOfBlockToGenerateWhenExecutingTx)
txDataField = fmt.Sprintf("stake@01@%s@%s", blsKeys[1], staking.MockBLSSignature)
txStake = staking.GenerateTransaction(walletAddress.Bytes, 1, vm.ValidatorSCAddress, staking.MinimumStakeValue, txDataField, staking.GasLimitForStakeOperation)
stakeTx, err = cs.SendTxAndGenerateBlockTilTxIsExecuted(txStake, staking.MaxNumOfBlockToGenerateWhenExecutingTx)
require.Nil(t, err)
require.NotNil(t, stakeTx)

decodedBLSKey1, _ := hex.DecodeString(blsKeys[1])
status = getBLSKeyStatus(t, metachainNode, decodedBLSKey1)
status = staking.GetBLSKeyStatus(t, metachainNode, decodedBLSKey1)
require.Equal(t, "staked", status)

// unJail the first node
unJailValue, _ := big.NewInt(0).SetString("2500000000000000000", 10)
txUnJailDataField := fmt.Sprintf("unJail@%s", blsKeys[0])
txUnJail := generateTransaction(walletAddress.Bytes, 2, vm.ValidatorSCAddress, unJailValue, txUnJailDataField, gasLimitForStakeOperation)
txUnJail := staking.GenerateTransaction(walletAddress.Bytes, 2, vm.ValidatorSCAddress, unJailValue, txUnJailDataField, staking.GasLimitForStakeOperation)

unJailTx, err := cs.SendTxAndGenerateBlockTilTxIsExecuted(txUnJail, maxNumOfBlockToGenerateWhenExecutingTx)
unJailTx, err := cs.SendTxAndGenerateBlockTilTxIsExecuted(txUnJail, staking.MaxNumOfBlockToGenerateWhenExecutingTx)
require.Nil(t, err)
require.NotNil(t, unJailTx)
require.Equal(t, transaction.TxStatusSuccess, unJailTx.Status)

status = getBLSKeyStatus(t, metachainNode, decodedBLSKey0)
status = staking.GetBLSKeyStatus(t, metachainNode, decodedBLSKey0)
require.Equal(t, "queued", status)

err = cs.GenerateBlocksUntilEpochIsReached(stakingV4JailUnJailStep1EnableEpoch)
require.Nil(t, err)

status = getBLSKeyStatus(t, metachainNode, decodedBLSKey0)
require.Equal(t, unStakedStatus, status)

checkValidatorStatus(t, cs, blsKeys[0], string(common.InactiveList))
}

func checkValidatorStatus(t *testing.T, cs chainSimulatorIntegrationTests.ChainSimulator, blsKey string, expectedStatus string) {
err := cs.ForceResetValidatorStatisticsCache()
require.Nil(t, err)
status = staking.GetBLSKeyStatus(t, metachainNode, decodedBLSKey0)
require.Equal(t, staking.UnStakedStatus, status)

validatorsStatistics, err := cs.GetNodeHandler(core.MetachainShardId).GetFacadeHandler().ValidatorStatisticsApi()
require.Nil(t, err)
require.Equal(t, expectedStatus, validatorsStatistics[blsKey].ValidatorStatus)
staking.CheckValidatorStatus(t, cs, blsKeys[0], string(common.InactiveList))
}

0 comments on commit d21e61d

Please sign in to comment.