Skip to content

Commit

Permalink
Merge pull request #310 from multiversx/re-enable-tests
Browse files Browse the repository at this point in the history
Re enable part of the tests
  • Loading branch information
dragos-rebegea authored Jul 11, 2024
2 parents 19e550e + 5005c95 commit d245809
Show file tree
Hide file tree
Showing 16 changed files with 587 additions and 197 deletions.
2 changes: 2 additions & 0 deletions clients/balanceValidator/balanceValidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ func (validator *balanceValidator) CheckToken(ctx context.Context, ethToken comm
"amount", amount.String(),
)

// TODO(next PRs): fix here to not consider the pending batch in the mvx->eth direction that executed on eth.

if ethAmount.Cmp(mvxAmount) != 0 {
return fmt.Errorf("%w, balance for ERC20 token %s is %s and the balance for ESDT token %s is %s, direction %s",
ErrBalanceMismatch, ethToken.String(), ethAmount.String(), mvxToken, mvxAmount.String(), direction)
Expand Down
2 changes: 1 addition & 1 deletion clients/ethereum/contract/GenericERC20.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion clients/ethereum/contract/MintBurnERC20.go

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -21,9 +20,10 @@ const (
)

func TestRelayersShouldExecuteTransfers(t *testing.T) {
t.Run("ETH->MVX and back, ethNative = true, ethMintBurn = false, mvxNative = false, mvxMintBurn = true", func(t *testing.T) {
token1 := issueTokenParams{
abstractTokenIdentifier: "ETHUSDC",
// USDC is ethNative = true, ethMintBurn = false, mvxNative = false, mvxMintBurn = true
usdcToken := testTokenParams{
issueTokenParams: issueTokenParams{
abstractTokenIdentifier: "USDC",
numOfDecimalsUniversal: 6,
numOfDecimalsChainSpecific: 6,
mvxUniversalTokenTicker: "USDC",
Expand All @@ -38,66 +38,137 @@ func TestRelayersShouldExecuteTransfers(t *testing.T) {
valueToMintOnEth: "10000000000",
isMintBurnOnEth: false,
isNativeOnEth: true,
}

valueToTransferToMvx := big.NewInt(5000)
// todo: refactor this
valueToSendFromMvX := big.NewInt(0).Div(valueToTransferToMvx, big.NewInt(2))
var expectedFinalValueOnEth *big.Int
var expectedFinalValueOnMvXSafe *big.Int
setupFunc := func(tb testing.TB, testSetup *simulatedSetup) {
testSetup.issueAndConfigureTokens(token1)

balance := testSetup.getESDTUniversalTokenBalance(testSetup.mvxReceiverAddress, token1.abstractTokenIdentifier)
assert.Equal(tb, big.NewInt(0).String(), balance.String())
},
testOperations: []tokenOperations{
{
valueToTransferToMvx: big.NewInt(5000),
valueToSendFromMvX: big.NewInt(2500),
ethSCCallMethod: "",
ethSCCallGasLimit: 0,
ethSCCallArguments: nil,
},
{
valueToTransferToMvx: big.NewInt(7000),
valueToSendFromMvX: big.NewInt(300),
ethSCCallMethod: "",
ethSCCallGasLimit: 0,
ethSCCallArguments: nil,
},
{
valueToTransferToMvx: big.NewInt(1000),
valueToSendFromMvX: nil,
ethSCCallMethod: "callPayable",
ethSCCallGasLimit: 50000000,
ethSCCallArguments: nil,
},
},
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
}

testSetup.createBatchOnEthereum(token1.abstractTokenIdentifier, valueToTransferToMvx, "", 0)
//MEME is ethNative = false, ethMintBurn = true, mvxNative = true, mvxMintBurn = false
memeToken := testTokenParams{
issueTokenParams: issueTokenParams{
abstractTokenIdentifier: "MEME",
numOfDecimalsUniversal: 1,
numOfDecimalsChainSpecific: 1,
mvxUniversalTokenTicker: "MEME",
mvxChainSpecificTokenTicker: "ETHMEME",
mvxUniversalTokenDisplayName: "WrappedMEME",
mvxChainSpecificTokenDisplayName: "EthereumWrappedMEME",
valueToMintOnMvx: "10000000000",
isMintBurnOnMvX: false,
isNativeOnMvX: true,
ethTokenName: "ETHMEME",
ethTokenSymbol: "ETHM",
valueToMintOnEth: "10000000000",
isMintBurnOnEth: true,
isNativeOnEth: false,
},
testOperations: []tokenOperations{
{
valueToTransferToMvx: big.NewInt(2400),
valueToSendFromMvX: big.NewInt(4000),
ethSCCallMethod: "",
ethSCCallGasLimit: 0,
ethSCCallArguments: nil,
},
{
valueToTransferToMvx: big.NewInt(200),
valueToSendFromMvX: big.NewInt(6000),
ethSCCallMethod: "",
ethSCCallGasLimit: 0,
ethSCCallArguments: nil,
},
{
valueToTransferToMvx: big.NewInt(1000),
valueToSendFromMvX: big.NewInt(2000),
ethSCCallMethod: "callPayable",
ethSCCallGasLimit: 50000000,
ethSCCallArguments: nil,
},
},
esdtSafeExtraBalance: big.NewInt(4000 + 6000 + 2000), // everything is locked in the safe esdt contract
ethTestAddrExtraBalance: big.NewInt(4000 - 50 + 6000 - 50 + 2000 - 50),
}

initialSafeValue := testSetup.getESDTChainSpecificTokenBalance(testSetup.mvxSafeAddress, token1.abstractTokenIdentifier)
expectedFinalValueOnMvXSafe = big.NewInt(0).Add(initialSafeValue, feeInt)
expectedFinalValueOnEth = big.NewInt(0).Sub(valueToSendFromMvX, feeInt)
}
testRelayersWithChainSimulatorAndTokens(t, usdcToken, memeToken)
}

ethToMVXDone := false
mvxToETHDone := false
processFunc := func(tb testing.TB, testSetup *simulatedSetup) bool {
receiverToCheckBalance := testSetup.mvxReceiverAddress
balance := testSetup.getESDTUniversalTokenBalance(receiverToCheckBalance, token1.abstractTokenIdentifier)
isTransferDoneFromETH := balance.String() == valueToTransferToMvx.String()
if !ethToMVXDone && isTransferDoneFromETH {
ethToMVXDone = true
log.Info("ETH->MvX transfer finished, now sending back to ETH...")
func testRelayersWithChainSimulatorAndTokens(tb testing.TB, tokens ...testTokenParams) {
startsFromEthFlow := &startsFromEthereumFlow{
TB: tb,
tokens: make([]testTokenParams, 0, len(tokens)),
}

testSetup.sendMVXToEthTransaction(token1.abstractTokenIdentifier, valueToSendFromMvX)
}
startsFromMvXFlow := &startsFromMultiversXFlow{
TB: tb,
tokens: make([]testTokenParams, 0, len(tokens)),
}

ethOwnerBalance := testSetup.getEthBalance(testSetup.ethOwnerAddress, token1.abstractTokenIdentifier)
isTransferDoneFromMVX := ethOwnerBalance.String() == expectedFinalValueOnEth.String()
// split the tokens from where should the bridge start
for _, token := range tokens {
if token.isNativeOnEth {
startsFromEthFlow.tokens = append(startsFromEthFlow.tokens, token)
continue
}
if token.isNativeOnMvX {
startsFromMvXFlow.tokens = append(startsFromMvXFlow.tokens, token)
continue
}
require.Fail(tb, "invalid setup, found a token that is not native on any chain", "abstract identifier", token.abstractTokenIdentifier)
}

balance = testSetup.getESDTChainSpecificTokenBalance(testSetup.mvxSafeAddress, token1.abstractTokenIdentifier)
safeSavedFee := expectedFinalValueOnMvXSafe.String() == balance.String()
setupFunc := func(tb testing.TB, testSetup *simulatedSetup) {
startsFromMvXFlow.testSetup = testSetup
startsFromEthFlow.testSetup = testSetup

if !mvxToETHDone && isTransferDoneFromMVX && safeSavedFee {
mvxToETHDone = true
}
testSetup.issueAndConfigureTokens(tokens...)
testSetup.checkForZeroBalanceOnReceivers(tokens...)
if len(startsFromEthFlow.tokens) > 0 {
testSetup.createBatchOnEthereum(startsFromEthFlow.tokens...)
}
if len(startsFromMvXFlow.tokens) > 0 {
testSetup.createBatchOnMultiversX(startsFromMvXFlow.tokens...)
}
}

if ethToMVXDone && mvxToETHDone {
log.Info("MvX<->ETH transfers done")
return true
}
processFunc := func(tb testing.TB, testSetup *simulatedSetup) bool {
if startsFromEthFlow.process() && startsFromMvXFlow.process() {
return true
}

// commit blocks in order to execute incoming txs from relayers
testSetup.simulatedETHChain.Commit()
testSetup.mvxChainSimulator.GenerateBlocks(testSetup.testContext, 1)
// commit blocks in order to execute incoming txs from relayers
testSetup.simulatedETHChain.Commit()
testSetup.mvxChainSimulator.GenerateBlocks(testSetup.testContext, 1)

return false
}
return false
}

testRelayersWithChainSimulator(t,
setupFunc,
processFunc,
)
})
testRelayersWithChainSimulator(tb,
setupFunc,
processFunc,
)
}

func testRelayersWithChainSimulator(tb testing.TB,
Expand All @@ -107,7 +178,7 @@ func testRelayersWithChainSimulator(tb testing.TB,
defer func() {
r := recover()
if r != nil {
require.Fail(tb, "should have not panicked")
require.Fail(tb, fmt.Sprintf("should have not panicked: %v", r))
}
}()

Expand Down
7 changes: 7 additions & 0 deletions integrationTests/relayers/slowTests/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math/big"

goEthereum "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/multiversx/mx-bridge-eth-go/clients/multiversx"
Expand Down Expand Up @@ -42,3 +43,9 @@ type bridgeComponents interface {
Start() error
Close() error
}

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

0 comments on commit d245809

Please sign in to comment.