Skip to content

Commit

Permalink
Merge pull request #372 from multiversx/integration-tests-refactor-fo…
Browse files Browse the repository at this point in the history
…r-uniformity

Integration tests refactor for uniformity
  • Loading branch information
cosmatudor authored Nov 18, 2024
2 parents 20f6375 + 018b591 commit 4b1aa1f
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 76 deletions.
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.CalleeScAddress, startsFromEthFlow.tokens...)
setup.CreateBatchOnEthereum(setup.MultiversxHandler.CalleeScAddress, startsFromEthFlow.tokens...)
}
if len(startsFromMvXFlow.tokens) > 0 {
setup.CreateBatchOnMultiversX(startsFromMvXFlow.tokens...)
Expand Down Expand Up @@ -355,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.CalleeScAddress, startsFromEthFlow.tokens...)
setup.CreateBatchOnEthereum(setup.MultiversxHandler.CalleeScAddress, startsFromEthFlow.tokens...)
}
if len(startsFromMvXFlow.tokens) > 0 {
setup.CreateBatchOnMultiversX(startsFromMvXFlow.tokens...)
Expand Down
106 changes: 38 additions & 68 deletions integrationTests/relayers/slowTests/framework/ethereumHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,90 +391,60 @@ func (handler *EthereumHandler) mintTokens(
require.Equal(handler, mintAmount.String(), balance.String())
}

// CreateBatchOnEthereum will create a batch on Ethereum using the provided tokens parameters list
func (handler *EthereumHandler) CreateBatchOnEthereum(
// ApproveForToken will approve the spender to spend the amount of tokens on the behalf of the approver
func (handler *EthereumHandler) ApproveForToken(
ctx context.Context,
mvxCalleeScAddress core.AddressHandler,
tokensParams ...TestTokenParams,
token *TokenData,
approver KeysHolder,
spender common.Address,
amount *big.Int,
) {
for _, params := range tokensParams {
handler.createDepositsOnEthereumForToken(ctx, params, handler.AliceKeys, handler.BobKeys, mvxCalleeScAddress)
}

// wait until batch is settled
batchSettleLimit, _ := handler.SafeContract.BatchSettleLimit(nil)
for i := uint8(0); i < batchSettleLimit+1; i++ {
handler.SimulatedChain.Commit()
}
}

// 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)
}
auth, _ := bind.NewKeyedTransactorWithChainID(approver.EthSK, handler.ChainID)
tx, err := token.EthErc20Contract.Approve(auth, spender, amount)
require.NoError(handler, err)
handler.SimulatedChain.Commit()
handler.checkEthTxResult(ctx, tx.Hash())
}

func (handler *EthereumHandler) createDepositsOnEthereumForToken(
// SendDepositTransactionFromEthereum will send a deposit transaction from Ethereum to MultiversX
func (handler *EthereumHandler) SendDepositTransactionFromEthereum(
ctx context.Context,
params TestTokenParams,
from KeysHolder,
to KeysHolder,
targetSCAddress core.AddressHandler,
token *TokenData,
operation TokenOperations,
) {
// TODO: transfer only required amount for deposit to the test key

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

token := handler.TokensRegistry.GetTokenData(params.AbstractTokenIdentifier)
require.NotNil(handler, token)
require.NotNil(handler, token.EthErc20Contract)

allowanceValue := big.NewInt(0)
for _, operation := range params.TestOperations {
if operation.ValueToTransferToMvx == nil {
continue
}

allowanceValue.Add(allowanceValue, operation.ValueToTransferToMvx)
if operation.ValueToTransferToMvx == nil {
return
}

if allowanceValue.Cmp(zeroValueBigInt) > 0 {
tx, err := token.EthErc20Contract.Approve(auth, handler.SafeAddress, allowanceValue)
require.NoError(handler, err)
handler.SimulatedChain.Commit()
handler.checkEthTxResult(ctx, tx.Hash())
}
auth, _ := bind.NewKeyedTransactorWithChainID(from.EthSK, handler.ChainID)

var tx *types.Transaction
var err error
for _, operation := range params.TestOperations {
if operation.ValueToTransferToMvx == nil {
continue
}
if len(operation.MvxSCCallData) > 0 || operation.MvxForceSCCall {
tx, err = handler.SafeContract.DepositWithSCExecution(
auth,
token.EthErc20Address,
operation.ValueToTransferToMvx,
targetSCAddress.AddressSlice(),
operation.MvxSCCallData,
)
} else {
tx, err = handler.SafeContract.Deposit(auth, token.EthErc20Address, operation.ValueToTransferToMvx, to.MvxAddress.AddressSlice())
}

var tx *types.Transaction
if len(operation.MvxSCCallData) > 0 || operation.MvxForceSCCall {
tx, err = handler.SafeContract.DepositWithSCExecution(
auth,
token.EthErc20Address,
operation.ValueToTransferToMvx,
targetSCAddress.AddressSlice(),
operation.MvxSCCallData,
)
} else {
tx, err = handler.SafeContract.Deposit(auth, token.EthErc20Address, operation.ValueToTransferToMvx, to.MvxAddress.AddressSlice())
}
require.NoError(handler, err)
handler.SimulatedChain.Commit()
handler.checkEthTxResult(ctx, tx.Hash())
}

require.NoError(handler, err)
// SettleBatchOnEthereum commits as many blocks as needed to settle the batch on Ethereum
func (handler *EthereumHandler) SettleBatchOnEthereum() {
batchSettleLimit, _ := handler.SafeContract.BatchSettleLimit(nil)
for i := uint8(0); i < batchSettleLimit+1; i++ {
handler.SimulatedChain.Commit()
handler.checkEthTxResult(ctx, tx.Hash())
}
}

Expand Down
58 changes: 55 additions & 3 deletions integrationTests/relayers/slowTests/framework/testSetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ func (setup *TestSetup) createBatchOnMultiversXForToken(params TestTokenParams)
require.NotNil(setup, token)

// TODO: transfer only required amount for deposit to the test key
valueToMintOnEthereum := setup.createdDepositOnMultiversxForToken(setup.AliceKeys, setup.BobKeys, params)
valueToMintOnEthereum := setup.createDepositOnMultiversxForToken(setup.AliceKeys, setup.BobKeys, params)
setup.EthereumHandler.Mint(setup.Ctx, params, valueToMintOnEthereum)
}

Expand All @@ -410,11 +410,11 @@ func (setup *TestSetup) transferTokensToTestKey(params TestTokenParams) {
// SendFromMultiversxToEthereum will create the deposits that will be gathered in a batch on MultiversX (without mint on Ethereum)
func (setup *TestSetup) SendFromMultiversxToEthereum(from KeysHolder, to KeysHolder, tokensParams ...TestTokenParams) {
for _, params := range tokensParams {
_ = setup.createdDepositOnMultiversxForToken(from, to, params)
_ = setup.createDepositOnMultiversxForToken(from, to, params)
}
}

func (setup *TestSetup) createdDepositOnMultiversxForToken(from KeysHolder, to KeysHolder, params TestTokenParams) *big.Int {
func (setup *TestSetup) createDepositOnMultiversxForToken(from KeysHolder, to KeysHolder, params TestTokenParams) *big.Int {
token := setup.GetTokenData(params.AbstractTokenIdentifier)
require.NotNil(setup, token)

Expand All @@ -431,6 +431,58 @@ func (setup *TestSetup) createdDepositOnMultiversxForToken(from KeysHolder, to K
return depositValue
}

// CreateBatchOnEthereum will create deposits that will be gathered in a batch on Ethereum
func (setup *TestSetup) CreateBatchOnEthereum(mvxCalleeScAddress sdkCore.AddressHandler, tokensParams ...TestTokenParams) {
for _, params := range tokensParams {
setup.createBatchOnEthereumForToken(mvxCalleeScAddress, params)
}

// wait until batch is settled
setup.EthereumHandler.SettleBatchOnEthereum()
}

func (setup *TestSetup) createBatchOnEthereumForToken(mvxCalleeScAddress sdkCore.AddressHandler, params TestTokenParams) {
token := setup.GetTokenData(params.AbstractTokenIdentifier)
require.NotNil(setup, token)

// TODO: transfer only required amount for deposit to the test key
setup.createDepositOnEthereumForToken(setup.AliceKeys, setup.BobKeys, mvxCalleeScAddress, params)
}

// SendFromEthereumToMultiversX will create the deposits that will be gathered in a batch on Ethereum
func (setup *TestSetup) SendFromEthereumToMultiversX(from KeysHolder, to KeysHolder, mvxTestCallerAddress sdkCore.AddressHandler, tokensParams ...TestTokenParams) {
for _, params := range tokensParams {
setup.createDepositOnEthereumForToken(from, to, mvxTestCallerAddress, params)
}
}

func (setup *TestSetup) createDepositOnEthereumForToken(from KeysHolder, to KeysHolder, targetSCAddress sdkCore.AddressHandler, params TestTokenParams) {
token := setup.GetTokenData(params.AbstractTokenIdentifier)
require.NotNil(setup, token)
require.NotNil(setup, token.EthErc20Contract)

allowanceValue := big.NewInt(0)
for _, operation := range params.TestOperations {
if operation.ValueToTransferToMvx == nil {
continue
}

allowanceValue.Add(allowanceValue, operation.ValueToTransferToMvx)
}

if allowanceValue.Cmp(zeroValueBigInt) > 0 {
setup.EthereumHandler.ApproveForToken(setup.Ctx, token, from, setup.EthereumHandler.SafeAddress, allowanceValue)
}

for _, operation := range params.TestOperations {
if operation.ValueToTransferToMvx == nil {
continue
}

setup.EthereumHandler.SendDepositTransactionFromEthereum(setup.Ctx, from, to, targetSCAddress, token, operation)
}
}

// TestWithdrawTotalFeesOnEthereumForTokens will test the withdrawal functionality for the provided test tokens
func (setup *TestSetup) TestWithdrawTotalFeesOnEthereumForTokens(tokensParams ...TestTokenParams) {
for _, param := range tokensParams {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func testRelayersWithChainSimulatorAndTokensAndRefund(tb testing.TB, manualStopC
setup.IssueAndConfigureTokens(tokens...)
setup.MultiversxHandler.CheckForZeroBalanceOnReceivers(setup.Ctx, tokens...)
if len(startsFromEthFlow.tokens) > 0 {
setup.EthereumHandler.CreateBatchOnEthereum(setup.Ctx, setup.MultiversxHandler.CalleeScAddress, startsFromEthFlow.tokens...)
setup.CreateBatchOnEthereum(setup.MultiversxHandler.CalleeScAddress, startsFromEthFlow.tokens...)
}
if len(startsFromMvXFlow.tokens) > 0 {
setup.CreateBatchOnMultiversX(startsFromMvXFlow.tokens...)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func (flow *startsFromEthereumFlow) process() (finished bool) {
}

isTransferDoneFromEthereum := flow.setup.IsTransferDoneFromEthereum(flow.setup.AliceKeys, flow.setup.BobKeys, flow.tokens...)
fmt.Println(isTransferDoneFromEthereum)
if !flow.ethToMvxDone && isTransferDoneFromEthereum {
flow.ethToMvxDone = true
log.Info(fmt.Sprintf(framework.LogStepMarker, "Ethereum->MultiversX transfer finished, now sending back to Ethereum..."))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (flow *startsFromMultiversXFlow) process() (finished bool) {
flow.mvxToEthDone = true
log.Info(fmt.Sprintf(framework.LogStepMarker, "MultiversX->Ethereum transfer finished, now sending back to MultiversX..."))

flow.setup.EthereumHandler.SendFromEthereumToMultiversX(flow.setup.Ctx, flow.setup.BobKeys, flow.setup.CharlieKeys, flow.setup.MultiversxHandler.CalleeScAddress, flow.tokens...)
flow.setup.SendFromEthereumToMultiversX(flow.setup.BobKeys, flow.setup.CharlieKeys, flow.setup.MultiversxHandler.CalleeScAddress, flow.tokens...)
}
if !flow.mvxToEthDone {
// return here, no reason to check downwards
Expand Down

0 comments on commit 4b1aa1f

Please sign in to comment.