-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Merge pull request #6221 from multiversx/update-feat-chain-simulator-…
…30.05 Update feat chain simulator 30.05
Showing
13 changed files
with
525 additions
and
474 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
182 changes: 91 additions & 91 deletions
182
integrationTests/chainSimulator/staking/stake/stakeAndUnStake_test.go
Large diffs are not rendered by default.
Oops, something went wrong.
160 changes: 80 additions & 80 deletions
160
integrationTests/chainSimulator/staking/stakingProvider/delegation_test.go
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,245 @@ | ||
package chainSimulator | ||
|
||
import ( | ||
"encoding/base64" | ||
"math/big" | ||
"testing" | ||
"time" | ||
|
||
"github.com/multiversx/mx-chain-go/node/chainSimulator/dtos" | ||
"github.com/multiversx/mx-chain-go/node/chainSimulator/errors" | ||
chainSimulatorProcess "github.com/multiversx/mx-chain-go/node/chainSimulator/process" | ||
"github.com/multiversx/mx-chain-go/process" | ||
|
||
"github.com/multiversx/mx-chain-core-go/core" | ||
coreAPI "github.com/multiversx/mx-chain-core-go/data/api" | ||
"github.com/multiversx/mx-chain-core-go/data/transaction" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// CheckSetState - | ||
func CheckSetState(t *testing.T, chainSimulator ChainSimulator, nodeHandler chainSimulatorProcess.NodeHandler) { | ||
keyValueMap := map[string]string{ | ||
"01": "01", | ||
"02": "02", | ||
} | ||
|
||
address := "erd1qtc600lryvytxuy4h7vn7xmsy5tw6vuw3tskr75cwnmv4mnyjgsq6e5zgj" | ||
err := chainSimulator.SetKeyValueForAddress(address, keyValueMap) | ||
require.Nil(t, err) | ||
|
||
err = chainSimulator.GenerateBlocks(1) | ||
require.Nil(t, err) | ||
|
||
keyValuePairs, _, err := nodeHandler.GetFacadeHandler().GetKeyValuePairs(address, coreAPI.AccountQueryOptions{}) | ||
require.Nil(t, err) | ||
require.Equal(t, keyValueMap, keyValuePairs) | ||
} | ||
|
||
// CheckSetEntireState - | ||
func CheckSetEntireState(t *testing.T, chainSimulator ChainSimulator, nodeHandler chainSimulatorProcess.NodeHandler, accountState *dtos.AddressState) { | ||
err := chainSimulator.SetStateMultiple([]*dtos.AddressState{accountState}) | ||
require.Nil(t, err) | ||
|
||
err = chainSimulator.GenerateBlocks(30) | ||
require.Nil(t, err) | ||
|
||
scAddress, _ := nodeHandler.GetCoreComponents().AddressPubKeyConverter().Decode(accountState.Address) | ||
res, _, err := nodeHandler.GetFacadeHandler().ExecuteSCQuery(&process.SCQuery{ | ||
ScAddress: scAddress, | ||
FuncName: "getSum", | ||
CallerAddr: nil, | ||
BlockNonce: core.OptionalUint64{}, | ||
}) | ||
require.Nil(t, err) | ||
|
||
counterValue := big.NewInt(0).SetBytes(res.ReturnData[0]).Int64() | ||
require.Equal(t, 10, int(counterValue)) | ||
|
||
time.Sleep(time.Second) | ||
|
||
account, _, err := nodeHandler.GetFacadeHandler().GetAccount(accountState.Address, coreAPI.AccountQueryOptions{}) | ||
require.Nil(t, err) | ||
require.Equal(t, accountState.Balance, account.Balance) | ||
require.Equal(t, accountState.DeveloperRewards, account.DeveloperReward) | ||
require.Equal(t, accountState.Code, account.Code) | ||
require.Equal(t, accountState.CodeHash, base64.StdEncoding.EncodeToString(account.CodeHash)) | ||
require.Equal(t, accountState.CodeMetadata, base64.StdEncoding.EncodeToString(account.CodeMetadata)) | ||
require.Equal(t, accountState.Owner, account.OwnerAddress) | ||
require.Equal(t, accountState.RootHash, base64.StdEncoding.EncodeToString(account.RootHash)) | ||
} | ||
|
||
// CheckSetEntireStateWithRemoval - | ||
func CheckSetEntireStateWithRemoval(t *testing.T, chainSimulator ChainSimulator, nodeHandler chainSimulatorProcess.NodeHandler, accountState *dtos.AddressState) { | ||
// activate the auto balancing tries so the results will be the same | ||
err := chainSimulator.GenerateBlocks(30) | ||
require.Nil(t, err) | ||
|
||
err = chainSimulator.SetStateMultiple([]*dtos.AddressState{accountState}) | ||
require.Nil(t, err) | ||
|
||
err = chainSimulator.GenerateBlocks(2) | ||
require.Nil(t, err) | ||
|
||
scAddress, _ := nodeHandler.GetCoreComponents().AddressPubKeyConverter().Decode(accountState.Address) | ||
res, _, err := nodeHandler.GetFacadeHandler().ExecuteSCQuery(&process.SCQuery{ | ||
ScAddress: scAddress, | ||
FuncName: "getSum", | ||
CallerAddr: nil, | ||
BlockNonce: core.OptionalUint64{}, | ||
}) | ||
require.Nil(t, err) | ||
|
||
counterValue := big.NewInt(0).SetBytes(res.ReturnData[0]).Int64() | ||
require.Equal(t, 10, int(counterValue)) | ||
|
||
account, _, err := nodeHandler.GetFacadeHandler().GetAccount(accountState.Address, coreAPI.AccountQueryOptions{}) | ||
require.Nil(t, err) | ||
require.Equal(t, accountState.Balance, account.Balance) | ||
require.Equal(t, accountState.DeveloperRewards, account.DeveloperReward) | ||
require.Equal(t, accountState.Code, account.Code) | ||
require.Equal(t, accountState.CodeHash, base64.StdEncoding.EncodeToString(account.CodeHash)) | ||
require.Equal(t, accountState.CodeMetadata, base64.StdEncoding.EncodeToString(account.CodeMetadata)) | ||
require.Equal(t, accountState.Owner, account.OwnerAddress) | ||
require.Equal(t, accountState.RootHash, base64.StdEncoding.EncodeToString(account.RootHash)) | ||
|
||
// Now we remove the account | ||
err = chainSimulator.RemoveAccounts([]string{accountState.Address}) | ||
require.Nil(t, err) | ||
|
||
err = chainSimulator.GenerateBlocks(2) | ||
require.Nil(t, err) | ||
|
||
account, _, err = nodeHandler.GetFacadeHandler().GetAccount(accountState.Address, coreAPI.AccountQueryOptions{}) | ||
require.Nil(t, err) | ||
require.Equal(t, "0", account.Balance) | ||
require.Equal(t, "0", account.DeveloperReward) | ||
require.Equal(t, "", account.Code) | ||
require.Equal(t, "", base64.StdEncoding.EncodeToString(account.CodeHash)) | ||
require.Equal(t, "", base64.StdEncoding.EncodeToString(account.CodeMetadata)) | ||
require.Equal(t, "", account.OwnerAddress) | ||
require.Equal(t, "", base64.StdEncoding.EncodeToString(account.RootHash)) | ||
|
||
// Set the state again | ||
err = chainSimulator.SetStateMultiple([]*dtos.AddressState{accountState}) | ||
require.Nil(t, err) | ||
|
||
err = chainSimulator.GenerateBlocks(2) | ||
require.Nil(t, err) | ||
|
||
account, _, err = nodeHandler.GetFacadeHandler().GetAccount(accountState.Address, coreAPI.AccountQueryOptions{}) | ||
require.Nil(t, err) | ||
|
||
require.Equal(t, accountState.Balance, account.Balance) | ||
require.Equal(t, accountState.DeveloperRewards, account.DeveloperReward) | ||
require.Equal(t, accountState.Code, account.Code) | ||
require.Equal(t, accountState.CodeHash, base64.StdEncoding.EncodeToString(account.CodeHash)) | ||
require.Equal(t, accountState.CodeMetadata, base64.StdEncoding.EncodeToString(account.CodeMetadata)) | ||
require.Equal(t, accountState.Owner, account.OwnerAddress) | ||
require.Equal(t, accountState.RootHash, base64.StdEncoding.EncodeToString(account.RootHash)) | ||
} | ||
|
||
// CheckGetAccount - | ||
func CheckGetAccount(t *testing.T, chainSimulator ChainSimulator) { | ||
// the facade's GetAccount method requires that at least one block was produced over the genesis block | ||
err := chainSimulator.GenerateBlocks(1) | ||
require.Nil(t, err) | ||
|
||
address := dtos.WalletAddress{ | ||
Bech32: "erd1qtc600lryvytxuy4h7vn7xmsy5tw6vuw3tskr75cwnmv4mnyjgsq6e5zgj", | ||
} | ||
address.Bytes, err = chainSimulator.GetNodeHandler(0).GetCoreComponents().AddressPubKeyConverter().Decode(address.Bech32) | ||
require.Nil(t, err) | ||
|
||
account, err := chainSimulator.GetAccount(address) | ||
require.Nil(t, err) | ||
require.Equal(t, uint64(0), account.Nonce) | ||
require.Equal(t, "0", account.Balance) | ||
|
||
nonce := uint64(37) | ||
err = chainSimulator.SetStateMultiple([]*dtos.AddressState{ | ||
{ | ||
Address: address.Bech32, | ||
Nonce: &nonce, | ||
Balance: big.NewInt(38).String(), | ||
}, | ||
}) | ||
require.Nil(t, err) | ||
|
||
// without this call the test will fail because the latest produced block points to a state roothash that tells that | ||
// the account has the nonce 0 | ||
_ = chainSimulator.GenerateBlocks(1) | ||
|
||
account, err = chainSimulator.GetAccount(address) | ||
require.Nil(t, err) | ||
require.Equal(t, uint64(37), account.Nonce) | ||
require.Equal(t, "38", account.Balance) | ||
} | ||
|
||
// CheckGenerateTransactions - | ||
func CheckGenerateTransactions(t *testing.T, chainSimulator ChainSimulator) { | ||
transferValue := big.NewInt(0).Mul(OneEGLD, big.NewInt(5)) | ||
|
||
wallet0, err := chainSimulator.GenerateAndMintWalletAddress(0, InitialAmount) | ||
require.Nil(t, err) | ||
|
||
wallet1, err := chainSimulator.GenerateAndMintWalletAddress(1, InitialAmount) | ||
require.Nil(t, err) | ||
|
||
wallet2, err := chainSimulator.GenerateAndMintWalletAddress(2, InitialAmount) | ||
require.Nil(t, err) | ||
|
||
wallet3, err := chainSimulator.GenerateAndMintWalletAddress(2, InitialAmount) | ||
require.Nil(t, err) | ||
|
||
wallet4, err := chainSimulator.GenerateAndMintWalletAddress(2, InitialAmount) | ||
require.Nil(t, err) | ||
|
||
gasLimit := uint64(50000) | ||
tx0 := GenerateTransaction(wallet0.Bytes, 0, wallet2.Bytes, transferValue, "", gasLimit) | ||
tx1 := GenerateTransaction(wallet1.Bytes, 0, wallet2.Bytes, transferValue, "", gasLimit) | ||
tx3 := GenerateTransaction(wallet3.Bytes, 0, wallet4.Bytes, transferValue, "", gasLimit) | ||
|
||
maxNumOfBlockToGenerateWhenExecutingTx := 15 | ||
|
||
t.Run("nil or empty slice of transactions should error", func(t *testing.T) { | ||
sentTxs, errSend := chainSimulator.SendTxsAndGenerateBlocksTilAreExecuted(nil, 1) | ||
assert.Equal(t, errors.ErrEmptySliceOfTxs, errSend) | ||
assert.Nil(t, sentTxs) | ||
|
||
sentTxs, errSend = chainSimulator.SendTxsAndGenerateBlocksTilAreExecuted(make([]*transaction.Transaction, 0), 1) | ||
assert.Equal(t, errors.ErrEmptySliceOfTxs, errSend) | ||
assert.Nil(t, sentTxs) | ||
}) | ||
t.Run("invalid max number of blocks to generate should error", func(t *testing.T) { | ||
sentTxs, errSend := chainSimulator.SendTxsAndGenerateBlocksTilAreExecuted([]*transaction.Transaction{tx0, tx1}, 0) | ||
assert.Equal(t, errors.ErrInvalidMaxNumOfBlocks, errSend) | ||
assert.Nil(t, sentTxs) | ||
}) | ||
t.Run("nil transaction in slice should error", func(t *testing.T) { | ||
sentTxs, errSend := chainSimulator.SendTxsAndGenerateBlocksTilAreExecuted([]*transaction.Transaction{nil}, 1) | ||
assert.ErrorIs(t, errSend, errors.ErrNilTransaction) | ||
assert.Nil(t, sentTxs) | ||
}) | ||
t.Run("2 transactions from different shard should call send correctly", func(t *testing.T) { | ||
sentTxs, errSend := chainSimulator.SendTxsAndGenerateBlocksTilAreExecuted([]*transaction.Transaction{tx0, tx1}, maxNumOfBlockToGenerateWhenExecutingTx) | ||
assert.Equal(t, 2, len(sentTxs)) | ||
assert.Nil(t, errSend) | ||
|
||
account, errGet := chainSimulator.GetAccount(wallet2) | ||
assert.Nil(t, errGet) | ||
expectedBalance := big.NewInt(0).Add(InitialAmount, transferValue) | ||
expectedBalance.Add(expectedBalance, transferValue) | ||
assert.Equal(t, expectedBalance.String(), account.Balance) | ||
}) | ||
t.Run("1 transaction should be sent correctly", func(t *testing.T) { | ||
_, errSend := chainSimulator.SendTxAndGenerateBlockTilTxIsExecuted(tx3, maxNumOfBlockToGenerateWhenExecutingTx) | ||
assert.Nil(t, errSend) | ||
|
||
account, errGet := chainSimulator.GetAccount(wallet4) | ||
assert.Nil(t, errGet) | ||
expectedBalance := big.NewInt(0).Add(InitialAmount, transferValue) | ||
assert.Equal(t, expectedBalance.String(), account.Balance) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package errors | ||
|
||
import "errors" | ||
|
||
// ErrEmptySliceOfTxs signals that an empty slice of transactions has been provided | ||
var ErrEmptySliceOfTxs = errors.New("empty slice of transactions to send") | ||
|
||
// ErrNilTransaction signals that a nil transaction has been provided | ||
var ErrNilTransaction = errors.New("nil transaction") | ||
|
||
// ErrInvalidMaxNumOfBlocks signals that an invalid max numerof blocks has been provided | ||
var ErrInvalidMaxNumOfBlocks = errors.New("invalid max number of blocks to generate") |