Skip to content

Commit

Permalink
Merge pull request #367 from multiversx/post-audit-testS-refactor-part1
Browse files Browse the repository at this point in the history
Post audit tests refactor part1
  • Loading branch information
cosmatudor authored Nov 14, 2024
2 parents 9993ccc + 197aa7f commit 20f6375
Show file tree
Hide file tree
Showing 10 changed files with 280 additions and 127 deletions.
68 changes: 60 additions & 8 deletions integrationTests/relayers/slowTests/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,21 @@ func GenerateTestUSDCToken() framework.TestTokenParams {
MvxSCCallData: createScCallData("callPayable", 50000000),
},
},
ESDTSafeExtraBalance: big.NewInt(100), // extra is just for the fees for the 2 transfers mvx->eth
EthTestAddrExtraBalance: big.NewInt(-5000 + 2500 - 50 - 7000 + 300 - 50 - 1000), // -(eth->mvx) + (mvx->eth) - fees
ESDTSafeExtraBalance: big.NewInt(100), // extra is just for the fees for the 2 transfers mvx->eth
ExtraBalances: map[string]framework.ExtraBalanceHolder{
framework.Alice: {
SentAmount: big.NewInt(-5000 - 7000 - 1000),
ReceivedAmount: big.NewInt(0),
},
framework.Bob: {
SentAmount: big.NewInt(-2500 - 300),
ReceivedAmount: big.NewInt(5000 + 7000),
},
framework.Charlie: {
SentAmount: big.NewInt(0),
ReceivedAmount: big.NewInt(2500 - 50 + 300 - 50),
},
},
}
}

Expand Down Expand Up @@ -95,8 +108,21 @@ func GenerateTestMEMEToken() framework.TestTokenParams {
MvxSCCallData: createScCallData("callPayable", 50000000),
},
},
ESDTSafeExtraBalance: big.NewInt(4000 + 6000 + 2000), // everything is locked in the safe esdt contract
EthTestAddrExtraBalance: big.NewInt(4000 - 50 + 6000 - 50 + 2000 - 50),
ESDTSafeExtraBalance: big.NewInt(4000 + 6000 + 2000), // everything is locked in the safe esdt contract
ExtraBalances: map[string]framework.ExtraBalanceHolder{
framework.Alice: {
SentAmount: big.NewInt(-4000 - 6000 - 2000),
ReceivedAmount: big.NewInt(0),
},
framework.Bob: {
SentAmount: big.NewInt(-2400 - 200 - 1000),
ReceivedAmount: big.NewInt(4000 - 50 + 6000 - 50 + 2000 - 50),
},
framework.Charlie: {
SentAmount: big.NewInt(0),
ReceivedAmount: big.NewInt(2400 + 200),
},
},
}
}

Expand Down Expand Up @@ -137,8 +163,21 @@ func GenerateTestEUROCToken() framework.TestTokenParams {
MvxSCCallData: createScCallData("callPayable", 50000000),
},
},
ESDTSafeExtraBalance: big.NewInt(100), // extra is just for the fees for the 2 transfers mvx->eth
EthTestAddrExtraBalance: big.NewInt(-5010 + 2510 - 50 - 7010 + 310 - 50 - 1010), // -(eth->mvx) + (mvx->eth) - fees
ESDTSafeExtraBalance: big.NewInt(100), // extra is just for the fees for the 2 transfers mvx->eth
ExtraBalances: map[string]framework.ExtraBalanceHolder{
framework.Alice: {
SentAmount: big.NewInt(-5010 - 7010 - 1010),
ReceivedAmount: big.NewInt(0),
},
framework.Bob: {
SentAmount: big.NewInt(-2510 - 310),
ReceivedAmount: big.NewInt(5010 + 7010),
},
framework.Charlie: {
SentAmount: big.NewInt(0),
ReceivedAmount: big.NewInt(2510 - 50 + 310 - 50),
},
},
}
}

Expand Down Expand Up @@ -179,8 +218,21 @@ func GenerateTestMEXToken() framework.TestTokenParams {
MvxSCCallData: createScCallData("callPayable", 50000000),
},
},
ESDTSafeExtraBalance: big.NewInt(150), // just the fees should be collected in ESDT safe
EthTestAddrExtraBalance: big.NewInt(4010 - 50 + 6010 - 50 + 2010 - 50),
ESDTSafeExtraBalance: big.NewInt(150), // just the fees should be collected in ESDT safe
ExtraBalances: map[string]framework.ExtraBalanceHolder{
framework.Alice: {
SentAmount: big.NewInt(-4010 - 6010 - 2010),
ReceivedAmount: big.NewInt(0),
},
framework.Bob: {
SentAmount: big.NewInt(-2410 - 210 - 1010),
ReceivedAmount: big.NewInt(4010 - 50 + 6010 - 50 + 2010 - 50),
},
framework.Charlie: {
SentAmount: big.NewInt(0),
ReceivedAmount: big.NewInt(2410 + 210),
},
},
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func testRelayersWithChainSimulatorAndTokens(tb testing.TB, manualStopChan chan
setup.IssueAndConfigureTokens(tokens...)
setup.MultiversxHandler.CheckForZeroBalanceOnReceivers(setup.Ctx, tokens...)
if len(startsFromEthFlow.tokens) > 0 {
setup.EthereumHandler.CreateBatchOnEthereum(setup.Ctx, setup.MultiversxHandler.TestCallerAddress, startsFromEthFlow.tokens...)
setup.EthereumHandler.CreateBatchOnEthereum(setup.Ctx, setup.MultiversxHandler.CalleeScAddress, startsFromEthFlow.tokens...)
}
if len(startsFromMvXFlow.tokens) > 0 {
setup.CreateBatchOnMultiversX(startsFromMvXFlow.tokens...)
Expand Down Expand Up @@ -287,8 +287,12 @@ func createBadToken() framework.TestTokenParams {
MvxSCCallData: createScCallData("callPayable", 50000000),
},
},
ESDTSafeExtraBalance: big.NewInt(0),
EthTestAddrExtraBalance: big.NewInt(0),
ESDTSafeExtraBalance: big.NewInt(0),
ExtraBalances: map[string]framework.ExtraBalanceHolder{
"Alice": {big.NewInt(-5000 - 7000 - 1000), big.NewInt(0)},
"Bob": {big.NewInt(-2500 - 300), big.NewInt(5000 + 7000)},
"Charlie": {big.NewInt(0), big.NewInt(2500 - 50 + 300 - 50)},
},
}
}

Expand Down Expand Up @@ -351,7 +355,7 @@ func testRelayersShouldNotExecuteTransfers(
setup.IssueAndConfigureTokens(tokens...)
setup.MultiversxHandler.CheckForZeroBalanceOnReceivers(setup.Ctx, tokens...)
if len(startsFromEthFlow.tokens) > 0 {
setup.EthereumHandler.CreateBatchOnEthereum(setup.Ctx, setup.MultiversxHandler.TestCallerAddress, startsFromEthFlow.tokens...)
setup.EthereumHandler.CreateBatchOnEthereum(setup.Ctx, setup.MultiversxHandler.CalleeScAddress, startsFromEthFlow.tokens...)
}
if len(startsFromMvXFlow.tokens) > 0 {
setup.CreateBatchOnMultiversX(startsFromMvXFlow.tokens...)
Expand Down Expand Up @@ -446,7 +450,7 @@ func testCallPayableWithParamsWasCalled(testSetup *framework.TestSetup, value ui
}

vmRequest := &data.VmValueRequest{
Address: testSetup.MultiversxHandler.TestCallerAddress.Bech32(),
Address: testSetup.MultiversxHandler.CalleeScAddress.Bech32(),
FuncName: "getCalledDataParams",
}

Expand Down
48 changes: 26 additions & 22 deletions integrationTests/relayers/slowTests/framework/ethereumHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package framework
import (
"bytes"
"context"
"crypto/ecdsa"
"math/big"
"os"
"testing"
Expand Down Expand Up @@ -50,7 +49,7 @@ type EthereumHandler struct {
*KeysStore
TokensRegistry TokensRegistry
Quorum string
MvxTestCallerAddress core.AddressHandler
MvxCalleeScAddress core.AddressHandler
SimulatedChain *simulated.Backend
SimulatedChainWrapper EthereumBlockchainClient
ChainID *big.Int
Expand Down Expand Up @@ -340,7 +339,7 @@ func (handler *EthereumHandler) deployTestERC20Contract(ctx context.Context, par
require.Equal(handler, mintAmount.String(), balance.String())

if params.IsNativeOnEth {
tx, err = ethMintBurnContract.Mint(auth, handler.TestKeys.EthAddress, mintAmount)
tx, err = ethMintBurnContract.Mint(auth, handler.AliceKeys.EthAddress, mintAmount)
require.NoError(handler, err)
handler.SimulatedChain.Commit()
handler.checkEthTxResult(ctx, tx.Hash())
Expand All @@ -363,7 +362,7 @@ func (handler *EthereumHandler) deployTestERC20Contract(ctx context.Context, par
require.NoError(handler, err)

// mint the address that will create the transfers
handler.mintTokens(ctx, ethGenericTokenContract, params.ValueToMintOnEth, handler.TestKeys.EthAddress)
handler.mintTokens(ctx, ethGenericTokenContract, params.ValueToMintOnEth, handler.AliceKeys.EthAddress)
if len(params.InitialSupplyValue) > 0 {
handler.mintTokens(ctx, ethGenericTokenContract, params.InitialSupplyValue, handler.SafeAddress)
}
Expand Down Expand Up @@ -395,11 +394,11 @@ func (handler *EthereumHandler) mintTokens(
// CreateBatchOnEthereum will create a batch on Ethereum using the provided tokens parameters list
func (handler *EthereumHandler) CreateBatchOnEthereum(
ctx context.Context,
mvxTestCallerAddress core.AddressHandler,
mvxCalleeScAddress core.AddressHandler,
tokensParams ...TestTokenParams,
) {
for _, params := range tokensParams {
handler.createDepositsOnEthereumForToken(ctx, params, handler.TestKeys.EthSK, mvxTestCallerAddress)
handler.createDepositsOnEthereumForToken(ctx, params, handler.AliceKeys, handler.BobKeys, mvxCalleeScAddress)
}

// wait until batch is settled
Expand All @@ -409,14 +408,30 @@ func (handler *EthereumHandler) CreateBatchOnEthereum(
}
}

// SendFromEthereumToMultiversX will create the deposit transactions on the Ethereum side
func (handler *EthereumHandler) SendFromEthereumToMultiversX(
ctx context.Context,
from KeysHolder,
to KeysHolder,
mvxTestCallerAddress core.AddressHandler,
tokensParams ...TestTokenParams,
) {
for _, params := range tokensParams {
handler.createDepositsOnEthereumForToken(ctx, params, from, to, mvxTestCallerAddress)
}
}

func (handler *EthereumHandler) createDepositsOnEthereumForToken(
ctx context.Context,
params TestTokenParams,
from *ecdsa.PrivateKey,
mvxTestCallerAddress core.AddressHandler,
from KeysHolder,
to KeysHolder,
targetSCAddress core.AddressHandler,
) {
// TODO: transfer only required amount for deposit to the test key

// add allowance for the sender
auth, _ := bind.NewKeyedTransactorWithChainID(from, handler.ChainID)
auth, _ := bind.NewKeyedTransactorWithChainID(from.EthSK, handler.ChainID)

token := handler.TokensRegistry.GetTokenData(params.AbstractTokenIdentifier)
require.NotNil(handler, token)
Expand Down Expand Up @@ -450,11 +465,11 @@ func (handler *EthereumHandler) createDepositsOnEthereumForToken(
auth,
token.EthErc20Address,
operation.ValueToTransferToMvx,
mvxTestCallerAddress.AddressSlice(),
targetSCAddress.AddressSlice(),
operation.MvxSCCallData,
)
} else {
tx, err = handler.SafeContract.Deposit(auth, token.EthErc20Address, operation.ValueToTransferToMvx, handler.TestKeys.MvxAddress.AddressSlice())
tx, err = handler.SafeContract.Deposit(auth, token.EthErc20Address, operation.ValueToTransferToMvx, to.MvxAddress.AddressSlice())
}

require.NoError(handler, err)
Expand All @@ -463,17 +478,6 @@ func (handler *EthereumHandler) createDepositsOnEthereumForToken(
}
}

// SendFromEthereumToMultiversX will create the deposit transactions on the Ethereum side
func (handler *EthereumHandler) SendFromEthereumToMultiversX(
ctx context.Context,
mvxTestCallerAddress core.AddressHandler,
tokensParams ...TestTokenParams,
) {
for _, params := range tokensParams {
handler.createDepositsOnEthereumForToken(ctx, params, handler.TestKeys.EthSK, mvxTestCallerAddress)
}
}

// Mint will mint the provided token on Ethereum with the provided value on the behalf of the Depositor address
func (handler *EthereumHandler) Mint(ctx context.Context, params TestTokenParams, valueToMint *big.Int) {
token := handler.TokensRegistry.GetTokenData(params.AbstractTokenIdentifier)
Expand Down
26 changes: 22 additions & 4 deletions integrationTests/relayers/slowTests/framework/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,22 @@ type KeysStore struct {
SCExecutorKeys KeysHolder
OwnerKeys KeysHolder
DepositorKeys KeysHolder
TestKeys KeysHolder
AliceKeys KeysHolder
BobKeys KeysHolder
CharlieKeys KeysHolder
AddressToName map[string]string
workingDir string
}

const (
ethOwnerSK = "b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291"
ethDepositorSK = "9bb971db41e3815a669a71c3f1bcb24e0b81f21e04bf11faa7a34b9b40e7cfb1"
ethTestSk = "dafea2c94bfe5d25f1a508808c2bc2c2e6c6f18b6b010fc841d8eb80755ba27a"
aliceSk = "3a944a35d9cb7be4dd4e91429d28cec594db960221724cc3a3c81594e0140acb"
bobSk = "c658971dab0b3f2586ef35444554a2ddf5169f750ca46c29d769930205078ded"
charlieSk = "43cba80c6e2ee37fc9cf13f1d445ebbb7fb74f54800884f1162603c6de8d4530"
Alice = "Alice"
Bob = "Bob"
Charlie = "Charlie"
)

// NewKeysStore will create a KeysStore instance and generate all keys
Expand All @@ -63,6 +71,7 @@ func NewKeysStore(
TB: tb,
RelayersKeys: make([]KeysHolder, 0, numRelayers),
SCExecutorKeys: KeysHolder{},
AddressToName: make(map[string]string),
workingDir: workingDir,
}

Expand All @@ -74,7 +83,16 @@ func NewKeysStore(
"MvX address", keysStore.OwnerKeys.MvxAddress.Bech32(),
"Eth address", keysStore.OwnerKeys.EthAddress.String())
keysStore.DepositorKeys = keysStore.generateKey(ethDepositorSK, projectedShardForDepositor)
keysStore.TestKeys = keysStore.generateKey(ethTestSk, projectedShardForTestKeys)
keysStore.AliceKeys = keysStore.generateKey(aliceSk, projectedShardForTestKeys)
keysStore.BobKeys = keysStore.generateKey(bobSk, projectedShardForTestKeys)
keysStore.CharlieKeys = keysStore.generateKey(charlieSk, projectedShardForTestKeys)

keysStore.AddressToName[keysStore.AliceKeys.MvxAddress.String()] = Alice
keysStore.AddressToName[keysStore.BobKeys.MvxAddress.String()] = Bob
keysStore.AddressToName[keysStore.CharlieKeys.MvxAddress.String()] = Charlie
keysStore.AddressToName[keysStore.AliceKeys.EthAddress.String()] = Alice
keysStore.AddressToName[keysStore.BobKeys.EthAddress.String()] = Bob
keysStore.AddressToName[keysStore.CharlieKeys.EthAddress.String()] = Charlie

filename := path.Join(keysStore.workingDir, SCCallerFilename)
SaveMvxKey(keysStore, filename, keysStore.SCExecutorKeys)
Expand Down Expand Up @@ -139,7 +157,7 @@ func (keyStore *KeysStore) getAllKeys() []KeysHolder {
allKeys := make([]KeysHolder, 0, len(keyStore.RelayersKeys)+10)
allKeys = append(allKeys, keyStore.RelayersKeys...)
allKeys = append(allKeys, keyStore.OraclesKeys...)
allKeys = append(allKeys, keyStore.SCExecutorKeys, keyStore.OwnerKeys, keyStore.DepositorKeys, keyStore.TestKeys)
allKeys = append(allKeys, keyStore.SCExecutorKeys, keyStore.OwnerKeys, keyStore.DepositorKeys, keyStore.AliceKeys, keyStore.BobKeys, keyStore.CharlieKeys)

return allKeys
}
Expand Down
22 changes: 11 additions & 11 deletions integrationTests/relayers/slowTests/framework/multiversxHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ type MultiversxHandler struct {
MultisigAddress *MvxAddress
MultiTransferAddress *MvxAddress
ScProxyAddress *MvxAddress
TestCallerAddress *MvxAddress
CalleeScAddress *MvxAddress
ESDTSystemContractAddress *MvxAddress
}

Expand Down Expand Up @@ -239,15 +239,15 @@ func (handler *MultiversxHandler) deployContracts(ctx context.Context) {
log.Info("Deploy: multisig contract", "address", handler.MultisigAddress, "transaction hash", hash)

// deploy test-caller
handler.TestCallerAddress, hash, _ = handler.ChainSimulator.DeploySC(
handler.CalleeScAddress, hash, _ = handler.ChainSimulator.DeploySC(
ctx,
testCallerContractPath,
handler.OwnerKeys.MvxSk,
deployGasLimit,
[]string{},
)
require.NotEqual(handler, emptyAddress, handler.TestCallerAddress)
log.Info("Deploy: test-caller contract", "address", handler.TestCallerAddress, "transaction hash", hash)
require.NotEqual(handler, emptyAddress, handler.CalleeScAddress)
log.Info("Deploy: test-caller contract", "address", handler.CalleeScAddress, "transaction hash", hash)
}

func (handler *MultiversxHandler) wireMultiTransfer(ctx context.Context) {
Expand Down Expand Up @@ -453,10 +453,10 @@ func (handler *MultiversxHandler) CheckForZeroBalanceOnReceivers(ctx context.Con

// CheckForZeroBalanceOnReceiversForToken will check that the balance for the test address and the test SC call address is 0
func (handler *MultiversxHandler) CheckForZeroBalanceOnReceiversForToken(ctx context.Context, token TestTokenParams) {
balance := handler.GetESDTUniversalTokenBalance(ctx, handler.TestKeys.MvxAddress, token.AbstractTokenIdentifier)
balance := handler.GetESDTUniversalTokenBalance(ctx, handler.BobKeys.MvxAddress, token.AbstractTokenIdentifier)
require.Equal(handler, big.NewInt(0).String(), balance.String())

balance = handler.GetESDTUniversalTokenBalance(ctx, handler.TestCallerAddress, token.AbstractTokenIdentifier)
balance = handler.GetESDTUniversalTokenBalance(ctx, handler.CalleeScAddress, token.AbstractTokenIdentifier)
require.Equal(handler, big.NewInt(0).String(), balance.String())
}

Expand Down Expand Up @@ -942,20 +942,20 @@ func (handler *MultiversxHandler) submitAggregatorBatchForKey(ctx context.Contex
}

// SendDepositTransactionFromMultiversx will send the deposit transaction from MultiversX
func (handler *MultiversxHandler) SendDepositTransactionFromMultiversx(ctx context.Context, token *TokenData, value *big.Int) {
func (handler *MultiversxHandler) SendDepositTransactionFromMultiversx(ctx context.Context, from KeysHolder, to KeysHolder, token *TokenData, value *big.Int) {
// create transaction params
params := []string{
hex.EncodeToString([]byte(token.MvxUniversalToken)),
hex.EncodeToString(value.Bytes()),
hex.EncodeToString([]byte(unwrapTokenCreateTransactionFunction)),
hex.EncodeToString([]byte(token.MvxChainSpecificToken)),
hex.EncodeToString(handler.TestKeys.EthAddress.Bytes()),
hex.EncodeToString(to.EthAddress.Bytes()),
}
dataField := strings.Join(params, "@")

hash, txResult := handler.ChainSimulator.ScCall(
ctx,
handler.TestKeys.MvxSk,
from.MvxSk,
handler.WrapperAddress,
zeroStringValue,
createDepositGasLimit+gasLimitPerDataByte*uint64(len(dataField)),
Expand Down Expand Up @@ -1020,10 +1020,10 @@ func (handler *MultiversxHandler) withdrawFees(ctx context.Context,
}

// TransferToken is able to create an ESDT transfer
func (handler *MultiversxHandler) TransferToken(ctx context.Context, source KeysHolder, receiver KeysHolder, amount *big.Int, params TestTokenParams) {
func (handler *MultiversxHandler) TransferToken(ctx context.Context, source KeysHolder, receiver KeysHolder, amount *big.Int, params IssueTokenParams) {
tkData := handler.TokensRegistry.GetTokenData(params.AbstractTokenIdentifier)

// transfer to the test key, so it will have funds to carry on with the deposits
// transfer to receiver, so it will have funds to carry on with the deposits
hash, txResult := handler.ChainSimulator.ScCall(
ctx,
source.MvxSk,
Expand Down
Loading

0 comments on commit 20f6375

Please sign in to comment.