Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration Tests Refactor #394

Merged
merged 8 commits into from
Dec 19, 2024
78 changes: 56 additions & 22 deletions integrationTests/relayers/slowTests/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"bytes"
"math/big"

"github.com/ethereum/go-ethereum/common"
bridgeCore "github.com/multiversx/mx-bridge-eth-go/core"
"github.com/multiversx/mx-bridge-eth-go/integrationTests/relayers/slowTests/framework"
"github.com/multiversx/mx-bridge-eth-go/parsers"
Expand All @@ -16,6 +17,7 @@ import (
var (
log = logger.GetOrCreate("integrationTests/relayers/slowTests")
mvxZeroAddress = bytes.Repeat([]byte{0x00}, 32)
ethZeroAddress = common.Address{}
)

// GenerateTestUSDCToken will generate a test USDC token
Expand Down Expand Up @@ -64,6 +66,12 @@ func GenerateTestUSDCToken() framework.TestTokenParams {
ValueToSendFromMvX: nil,
InvalidReceiver: mvxZeroAddress,
},
{
ValueToTransferToMvx: nil,
ValueToSendFromMvX: big.NewInt(730),
InvalidReceiver: ethZeroAddress,
IsFaultyDeposit: true,
},
},
DeltaBalances: map[framework.HalfBridgeIdentifier]framework.DeltaBalancesOnKeys{
framework.FirstHalfBridge: map[string]*framework.DeltaBalanceHolder{
Expand Down Expand Up @@ -206,7 +214,17 @@ func GenerateTestMEMEToken() framework.TestTokenParams {
ValueToSendFromMvX: big.NewInt(38),
IsFaultyDeposit: true,
},
// TODO: add a test where the receiver is the zero address
{
ValueToTransferToMvx: nil,
ValueToSendFromMvX: big.NewInt(420),
InvalidReceiver: ethZeroAddress,
IsFaultyDeposit: true,
},
{
ValueToTransferToMvx: big.NewInt(1300),
ValueToSendFromMvX: nil,
InvalidReceiver: mvxZeroAddress,
},
},
DeltaBalances: map[framework.HalfBridgeIdentifier]framework.DeltaBalancesOnKeys{
framework.FirstHalfBridge: map[string]*framework.DeltaBalanceHolder{
Expand Down Expand Up @@ -243,7 +261,7 @@ func GenerateTestMEMEToken() framework.TestTokenParams {
MvxToken: framework.UniversalToken,
},
framework.Bob: {
OnEth: big.NewInt(4000 - 50 - 2400 + 6000 - 50 - 200 + 2000 - 50 - 1000),
OnEth: big.NewInt(4000 - 50 - 2400 + 6000 - 50 - 200 + 2000 - 50 - 1000 - 1300 + 1250),
OnMvx: big.NewInt(0),
MvxToken: framework.UniversalToken,
},
Expand All @@ -254,7 +272,7 @@ func GenerateTestMEMEToken() framework.TestTokenParams {
},
framework.SafeSC: {
OnEth: big.NewInt(0),
OnMvx: big.NewInt(4000 - 2400 + 6000 - 200 + 2000 - 1000),
OnMvx: big.NewInt(4000 - 2400 + 6000 - 200 + 2000 - 1000 - 1300 + 1300),
MvxToken: framework.ChainSpecificToken,
},
framework.CalledTestSC: {
Expand All @@ -277,8 +295,8 @@ func GenerateTestMEMEToken() framework.TestTokenParams {
MvxSafeMintValue: big.NewInt(0),
MvxSafeBurnValue: big.NewInt(0),

EthSafeMintValue: big.NewInt(4000 - 50 + 6000 - 50 + 2000 - 50),
EthSafeBurnValue: big.NewInt(2400 + 200 + 1000),
EthSafeMintValue: big.NewInt(4000 - 50 + 6000 - 50 + 2000 - 50 + 1300 - 50),
EthSafeBurnValue: big.NewInt(2400 + 200 + 1000 + 1300),
},
SpecialChecks: &framework.SpecialBalanceChecks{
WrapperDeltaLiquidityCheck: big.NewInt(0),
Expand All @@ -289,13 +307,13 @@ func GenerateTestMEMEToken() framework.TestTokenParams {
// ApplyMEMERefundBalances will apply the refund balances on the involved entities for the MEME token
func ApplyMEMERefundBalances(token *framework.TestTokenParams) {
// we need to add the 1000 MEME tokens as the third bridge was done that include the refund on the Ethereum side
token.DeltaBalances[framework.SecondHalfBridge][framework.SafeSC].OnMvx = big.NewInt(4000 - 2400 + 6000 - 200 + 2000 - 1000 + 1000)
token.DeltaBalances[framework.SecondHalfBridge][framework.SafeSC].OnMvx = big.NewInt(4000 - 2400 + 6000 - 200 + 2000 - 1300 + 1300 - 1000 + 1000)
// Bob will get his tokens back from the refund
token.DeltaBalances[framework.SecondHalfBridge][framework.Bob].OnEth = big.NewInt(4000 - 50 - 2400 + 6000 - 50 - 200 + 2000 - 50 - 1000 + 950)
token.DeltaBalances[framework.SecondHalfBridge][framework.Bob].OnEth = big.NewInt(4000 - 50 - 2400 + 6000 - 50 - 200 + 2000 - 50 - 1300 + 1250 - 1000 + 950)
// no funds remain in the test caller SC
token.DeltaBalances[framework.SecondHalfBridge][framework.CalledTestSC].OnMvx = big.NewInt(0)

token.MintBurnChecks.EthSafeMintValue = big.NewInt(4000 - 50 + 6000 - 50 + 2000 - 50 + 1000 - 50)
token.MintBurnChecks.EthSafeMintValue = big.NewInt(4000 - 50 + 6000 - 50 + 2000 - 50 + 1300 - 50 + 1000 - 50)
}

// GenerateTestEUROCToken will generate a test EUROC token
Expand Down Expand Up @@ -344,6 +362,12 @@ func GenerateTestEUROCToken() framework.TestTokenParams {
ValueToSendFromMvX: nil,
InvalidReceiver: mvxZeroAddress,
},
{
ValueToTransferToMvx: nil,
ValueToSendFromMvX: big.NewInt(853),
InvalidReceiver: ethZeroAddress,
IsFaultyDeposit: true,
},
},
DeltaBalances: map[framework.HalfBridgeIdentifier]framework.DeltaBalancesOnKeys{
framework.FirstHalfBridge: map[string]*framework.DeltaBalanceHolder{
Expand Down Expand Up @@ -480,7 +504,17 @@ func GenerateTestMEXToken() framework.TestTokenParams {
ValueToSendFromMvX: nil,
IsFaultyDeposit: true,
},
// TODO: add a test where the receiver is the zero address
{
ValueToTransferToMvx: nil,
ValueToSendFromMvX: big.NewInt(500),
InvalidReceiver: ethZeroAddress,
IsFaultyDeposit: true,
},
{
ValueToTransferToMvx: big.NewInt(3000),
ValueToSendFromMvX: nil,
InvalidReceiver: mvxZeroAddress,
},
},
DeltaBalances: map[framework.HalfBridgeIdentifier]framework.DeltaBalancesOnKeys{
framework.FirstHalfBridge: map[string]*framework.DeltaBalanceHolder{
Expand Down Expand Up @@ -517,7 +551,7 @@ func GenerateTestMEXToken() framework.TestTokenParams {
MvxToken: framework.UniversalToken,
},
framework.Bob: {
OnEth: big.NewInt(4010 - 50 - 2410 + 6010 - 50 - 210 + 2010 - 50 - 1010),
OnEth: big.NewInt(4010 - 50 - 2410 + 6010 - 50 - 210 + 2010 - 50 - 1010 - 3000 + 2950),
OnMvx: big.NewInt(0),
MvxToken: framework.UniversalToken,
},
Expand All @@ -528,7 +562,7 @@ func GenerateTestMEXToken() framework.TestTokenParams {
},
framework.SafeSC: {
OnEth: big.NewInt(0),
OnMvx: big.NewInt(50 + 50 + 50),
OnMvx: big.NewInt(50 + 50 + 50 + 50),
MvxToken: framework.ChainSpecificToken,
},
framework.CalledTestSC: {
Expand All @@ -544,15 +578,15 @@ func GenerateTestMEXToken() framework.TestTokenParams {
},
},
MintBurnChecks: &framework.MintBurnBalances{
MvxTotalUniversalMint: big.NewInt(2410 + 210 + 1010),
MvxTotalUniversalMint: big.NewInt(2410 + 210 + 1010 + 3000),
MvxTotalChainSpecificMint: big.NewInt(0),
MvxTotalUniversalBurn: big.NewInt(4010 - 50 + 6010 - 50 + 2010 - 50),
MvxTotalUniversalBurn: big.NewInt(4010 - 50 + 6010 - 50 + 2010 - 50 + 3000 - 50),
MvxTotalChainSpecificBurn: big.NewInt(0),
MvxSafeMintValue: big.NewInt(2410 + 210 + 1010),
MvxSafeBurnValue: big.NewInt(4010 - 50 + 6010 - 50 + 2010 - 50),
MvxSafeMintValue: big.NewInt(2410 + 210 + 1010 + 3000),
MvxSafeBurnValue: big.NewInt(4010 - 50 + 6010 - 50 + 2010 - 50 + 3000 - 50),

EthSafeMintValue: big.NewInt(4010 - 50 + 6010 - 50 + 2010 - 50),
EthSafeBurnValue: big.NewInt(2410 + 210 + 1010),
EthSafeMintValue: big.NewInt(4010 - 50 + 6010 - 50 + 2010 - 50 + 3000 - 50),
EthSafeBurnValue: big.NewInt(2410 + 210 + 1010 + 3000),
},
SpecialChecks: &framework.SpecialBalanceChecks{
WrapperDeltaLiquidityCheck: big.NewInt(0),
Expand All @@ -563,15 +597,15 @@ func GenerateTestMEXToken() framework.TestTokenParams {
// ApplyMEXRefundBalances will apply the refund balances on the involved entities for the MEX token
func ApplyMEXRefundBalances(token *framework.TestTokenParams) {
// 3 normal swaps + the refund one
token.DeltaBalances[framework.SecondHalfBridge][framework.SafeSC].OnMvx = big.NewInt(50 + 50 + 50 + 50)
token.DeltaBalances[framework.SecondHalfBridge][framework.SafeSC].OnMvx = big.NewInt(50 + 50 + 50 + 50 + 50)
// Bob will get his tokens back from the refund
token.DeltaBalances[framework.SecondHalfBridge][framework.Bob].OnEth = big.NewInt(4010 - 50 - 2410 + 6010 - 50 - 210 + 2010 - 50 - 1010 + 960)
token.DeltaBalances[framework.SecondHalfBridge][framework.Bob].OnEth = big.NewInt(4010 - 50 - 2410 + 6010 - 50 - 210 + 2010 - 50 - 1010 + 960 - 3000 + 2950)
// no funds remain in the test caller SC
token.DeltaBalances[framework.SecondHalfBridge][framework.CalledTestSC].OnMvx = big.NewInt(0)

token.MintBurnChecks.MvxTotalUniversalBurn = big.NewInt(4010 - 50 + 6010 - 50 + 2010 - 50 + 1010 - 50)
token.MintBurnChecks.MvxSafeBurnValue = big.NewInt(4010 - 50 + 6010 - 50 + 2010 - 50 + 1010 - 50)
token.MintBurnChecks.EthSafeMintValue = big.NewInt(4010 - 50 + 6010 - 50 + 2010 - 50 + 1010 - 50)
token.MintBurnChecks.MvxTotalUniversalBurn = big.NewInt(4010 - 50 + 6010 - 50 + 2010 - 50 + 3000 - 50 + 1010 - 50)
token.MintBurnChecks.MvxSafeBurnValue = big.NewInt(4010 - 50 + 6010 - 50 + 2010 - 50 + 3000 - 50 + 1010 - 50)
token.MintBurnChecks.EthSafeMintValue = big.NewInt(4010 - 50 + 6010 - 50 + 2010 - 50 + 3000 - 50 + 1010 - 50)
}

// GenerateUnlistedTokenFromEth will generate an unlisted token on Eth
Expand Down
1 change: 1 addition & 0 deletions integrationTests/relayers/slowTests/edgeCases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func TestRelayerShouldExecuteSimultaneousSwapsAndNotCatchErrors(t *testing.T) {
}()

usdcToken := GenerateTestUSDCToken()
usdcToken.MultipleSpendings = big.NewInt(2)
usdcToken.TestOperations = []framework.TokenOperations{
{
ValueToTransferToMvx: big.NewInt(5000),
Expand Down
30 changes: 13 additions & 17 deletions integrationTests/relayers/slowTests/framework/ethereumHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,13 +351,6 @@ func (handler *EthereumHandler) deployTestERC20Contract(ctx context.Context, par
require.NoError(handler, err)
require.Equal(handler, mintAmount.String(), balance.String())

if params.IsNativeOnEth {
tx, err = ethMintBurnContract.Mint(auth, handler.AliceKeys.EthAddress, mintAmount)
require.NoError(handler, err)
handler.SimulatedChain.Commit()
handler.checkEthTxResult(ctx, tx.Hash())
}

return ethMintBurnAddress, ethMintBurnContract
}

Expand All @@ -374,8 +367,8 @@ func (handler *EthereumHandler) deployTestERC20Contract(ctx context.Context, par
ethGenericTokenContract, err := contract.NewGenericERC20(ethGenericTokenAddress, handler.SimulatedChain.Client())
require.NoError(handler, err)

// mint the address that will create the transfers
handler.mintTokens(ctx, ethGenericTokenContract, params.ValueToMintOnEth, handler.AliceKeys.EthAddress)
// mint to the depositor
handler.mintTokens(ctx, ethGenericTokenContract, params.ValueToMintOnEth, handler.DepositorKeys.EthAddress)
if len(params.InitialSupplyValue) > 0 {
handler.mintTokens(ctx, ethGenericTokenContract, params.InitialSupplyValue, handler.SafeAddress)
}
Expand Down Expand Up @@ -465,15 +458,18 @@ func (handler *EthereumHandler) SettleBatchOnEthereum() {
}
}

// 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)
require.NotNil(handler, token)
require.NotNil(handler, token.EthErc20Contract)
// TransferToken will transfer the amount of tokens from one address to another
func (handler *EthereumHandler) TransferToken(
ctx context.Context,
params TestTokenParams,
from KeysHolder,
to KeysHolder,
amount *big.Int,
) {
tkData := handler.TokensRegistry.GetTokenData(params.AbstractTokenIdentifier)

// mint erc20 token into eth safe
auth, _ := bind.NewKeyedTransactorWithChainID(handler.DepositorKeys.EthSK, handler.ChainID)
tx, err := token.EthErc20Contract.Mint(auth, handler.SafeAddress, valueToMint)
auth, _ := bind.NewKeyedTransactorWithChainID(from.EthSK, handler.ChainID)
tx, err := tkData.EthErc20Contract.Transfer(auth, to.EthAddress, amount)
require.NoError(handler, err)
handler.SimulatedChain.Commit()
handler.checkEthTxResult(ctx, tx.Hash())
Expand Down
1 change: 1 addition & 0 deletions integrationTests/relayers/slowTests/framework/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type ERC20Contract interface {
BalanceOf(opts *bind.CallOpts, account common.Address) (*big.Int, error)
Mint(opts *bind.TransactOpts, recipientAddress common.Address, amount *big.Int) (*types.Transaction, error)
Approve(opts *bind.TransactOpts, spender common.Address, value *big.Int) (*types.Transaction, error)
Transfer(opts *bind.TransactOpts, to common.Address, value *big.Int) (*types.Transaction, error)
}

// TokensRegistry defines the registry used for the tokens in tests
Expand Down
60 changes: 44 additions & 16 deletions integrationTests/relayers/slowTests/framework/testSetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,6 @@ func (setup *TestSetup) IssueAndConfigureTokens(tokens ...TestTokenParams) {
setup.AddToken(token.IssueTokenParams)
setup.EthereumHandler.IssueAndWhitelistToken(setup.Ctx, token.IssueTokenParams)
setup.MultiversxHandler.IssueAndWhitelistToken(setup.Ctx, token.IssueTokenParams)
if token.IsNativeOnMvX {
setup.transferTokensToMvxTestKey(token) // TODO: (Next PRs) this will be moved an batch creation time
}
setup.ChainSimulator.GenerateBlocks(setup.Ctx, 10)

esdtBalanceForSafe := setup.MultiversxHandler.GetESDTChainSpecificTokenBalance(setup.Ctx, setup.MultiversxHandler.SafeAddress, token.AbstractTokenIdentifier)

setup.mutBalances.Lock()
setup.initMvxInitialBalancesForUniversalUnsafe(token,
Expand All @@ -189,6 +183,7 @@ func (setup *TestSetup) IssueAndConfigureTokens(tokens ...TestTokenParams) {
)
setup.mutBalances.Unlock()

esdtBalanceForSafe := setup.MultiversxHandler.GetESDTChainSpecificTokenBalance(setup.Ctx, setup.MultiversxHandler.SafeAddress, token.AbstractTokenIdentifier)
log.Info("recorded the ESDT balance for safe contract", "token", token.AbstractTokenIdentifier, "balance", esdtBalanceForSafe.String())
}

Expand Down Expand Up @@ -380,11 +375,17 @@ func (setup *TestSetup) createBatchOnMultiversXForToken(params TestTokenParams)
token := setup.GetTokenData(params.AbstractTokenIdentifier)
require.NotNil(setup, token)

// TODO: transfer only required amount for deposit to the test key
setup.transferTokensToMvxTestKey(params, setup.AliceKeys)
setup.ChainSimulator.GenerateBlocks(setup.Ctx, 10)

setup.mutBalances.Lock()
setup.initMvxInitialBalancesForUniversalUnsafe(params, setup.AliceKeys.MvxAddress)
setup.mutBalances.Unlock()

_ = setup.createDepositOnMultiversxForToken(setup.AliceKeys, setup.BobKeys, params)
}

func (setup *TestSetup) transferTokensToMvxTestKey(params TestTokenParams) {
func (setup *TestSetup) transferTokensToMvxTestKey(params TestTokenParams, holder KeysHolder) {
depositValue := big.NewInt(0)
for _, operation := range params.TestOperations {
if operation.ValueToSendFromMvX == nil {
Expand All @@ -397,7 +398,7 @@ func (setup *TestSetup) transferTokensToMvxTestKey(params TestTokenParams) {
setup.MultiversxHandler.TransferToken(
setup.Ctx,
setup.OwnerKeys,
setup.AliceKeys,
holder,
depositValue,
params.IssueTokenParams,
)
Expand All @@ -421,8 +422,7 @@ func (setup *TestSetup) createDepositOnMultiversxForToken(from KeysHolder, to Ke
}

if operation.InvalidReceiver != nil && !setup.hasCallData(operation) {
invalidReceiver := common.Address(operation.InvalidReceiver)
to = KeysHolder{EthAddress: invalidReceiver}
to = KeysHolder{EthAddress: operation.InvalidReceiver.(common.Address)}
}

depositValue.Add(depositValue, operation.ValueToSendFromMvX)
Expand Down Expand Up @@ -451,10 +451,37 @@ func (setup *TestSetup) createBatchOnEthereumForToken(mvxCalleeScAddress sdkCore
token := setup.GetTokenData(params.AbstractTokenIdentifier)
require.NotNil(setup, token)

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

setup.mutBalances.Lock()
setup.initEthInitialBalancesUnsafe(params, setup.AliceKeys.EthAddress)
setup.mutBalances.Unlock()

setup.createDepositOnEthereumForToken(setup.AliceKeys, setup.BobKeys, mvxCalleeScAddress, params)
}

func (setup *TestSetup) transferTokensToEthTestKey(params TestTokenParams, holder KeysHolder) {
depositValue := big.NewInt(0)
for _, operation := range params.TestOperations {
if operation.ValueToTransferToMvx == nil {
continue
}

depositValue.Add(depositValue, operation.ValueToTransferToMvx)
}

if params.MultipleSpendings != nil {
depositValue.Mul(depositValue, params.MultipleSpendings)
}

setup.EthereumHandler.TransferToken(
setup.Ctx,
params,
setup.DepositorKeys,
holder,
depositValue)
}

// 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 {
Expand Down Expand Up @@ -486,7 +513,7 @@ func (setup *TestSetup) createDepositOnEthereumForToken(from KeysHolder, to Keys
}

if operation.InvalidReceiver != nil {
invalidReceiver := NewMvxAddressFromBytes(setup, operation.InvalidReceiver)
invalidReceiver := NewMvxAddressFromBytes(setup, operation.InvalidReceiver.([]byte))

if setup.hasCallData(operation) {
targetSCAddress = invalidReceiver
Expand Down Expand Up @@ -516,6 +543,10 @@ func (setup *TestSetup) TestWithdrawTotalFeesOnEthereumForTokens(tokensParams ..
}

for _, operation := range param.TestOperations {
if operation.IsFaultyDeposit {
continue
}

if operation.InvalidReceiver != nil {
expectedRefund.Add(expectedRefund, feeInt)
}
Expand All @@ -526,9 +557,6 @@ func (setup *TestSetup) TestWithdrawTotalFeesOnEthereumForTokens(tokensParams ..
if operation.ValueToSendFromMvX.Cmp(zeroValueBigInt) == 0 {
continue
}
if operation.IsFaultyDeposit {
continue
}

expectedAccumulated.Add(expectedAccumulated, feeInt)
}
Expand Down
Loading
Loading