Skip to content

Commit

Permalink
improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
axenteoctavian committed Oct 25, 2024
1 parent 627b4ce commit 5d8fe50
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 22 deletions.
84 changes: 64 additions & 20 deletions integrationTests/chainSimulator/bridge/esdtSafeFail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@ import (
"github.com/multiversx/mx-chain-go/node/chainSimulator/components/api"
)

// 1. transfer from sovereign chain to main chain
// 2. transfer from main chain to sovereign chain
// 3. transfer again the same tokens from sovereign chain to main chain
// transfer from sovereign chain to main chain with transfer data
// tokens are originated from sovereign chain
// esdt-safe contract in main chain will issue its own tokens at registerToken step
// and will work with a mapper between sov_id <-> main_id
func TestChainSimulator_ExecuteWithTransferDataFails(t *testing.T) {
if testing.Short() {
t.Skip("this is not a short test")
Expand Down Expand Up @@ -61,64 +57,112 @@ func TestChainSimulator_ExecuteWithTransferDataFails(t *testing.T) {
esdtSafeAddr, _ := cs.GetNodeHandler(0).GetCoreComponents().AddressPubKeyConverter().Encode(bridgeData.ESDTSafeAddress)
esdtSafeAddrShard := chainSim.GetShardForAddress(cs, esdtSafeAddr)

nodeHandler := cs.GetNodeHandler(esdtSafeAddrShard)

wallet, err := cs.GenerateAndMintWalletAddress(esdtSafeAddrShard, chainSim.InitialAmount)
wallet, err := cs.GenerateAndMintWalletAddress(0, chainSim.InitialAmount)
require.Nil(t, err)
nonce := uint64(0)

err = cs.GenerateBlocks(1)
require.Nil(t, err)

systemContractDeploy := chainSim.GetSysContactDeployAddressBytes(t, nodeHandler)
helloAddress := chainSim.DeployContract(t, cs, wallet.Bytes, &nonce, systemContractDeploy, "", helloWasmPath)
helloAddrBech32, _ := nodeHandler.GetCoreComponents().AddressPubKeyConverter().Encode(helloAddress)
receiverShardId := uint32(0)

// TODO MX-15942 uncomment dynamic tokens, currently there is no issue function in SC framework for dynamic esdts
tokens := make([]chainSim.ArgsDepositToken, 0)
tokens = append(tokens, chainSim.ArgsDepositToken{
Identifier: "ab1-TKN-fasd35",
Nonce: uint64(0),
Amount: big.NewInt(14556666767),
Type: core.Fungible,
})
tokens = append(tokens, chainSim.ArgsDepositToken{
Identifier: "ab1-NFTV2-1ds234",
Nonce: uint64(1),
Amount: big.NewInt(1),
Type: core.NonFungibleV2,
})
//tokens = append(tokens, chainSim.ArgsDepositToken{
// Identifier: "ab2-DNFT-fdfe3r",
// Nonce: uint64(1),
// Amount: big.NewInt(1),
// Type: core.DynamicNFT,
//})
tokens = append(tokens, chainSim.ArgsDepositToken{
Identifier: "ab2-SFT-gw4fw2",
Nonce: uint64(1),
Amount: big.NewInt(1421),
Type: core.SemiFungible,
})
//tokens = append(tokens, chainSim.ArgsDepositToken{
// Identifier: "ab4-DSFT-g43g2s",
// Nonce: uint64(1),
// Amount: big.NewInt(1534),
// Type: core.DynamicSFT,
//})
tokens = append(tokens, chainSim.ArgsDepositToken{
Identifier: "ab5-META-1ds234",
Nonce: uint64(1),
Amount: big.NewInt(6231),
Type: core.MetaFungible,
})
//tokens = append(tokens, chainSim.ArgsDepositToken{
// Identifier: "ab5-DMETA-f23g2f",
// Nonce: uint64(1),
// Amount: big.NewInt(162367),
// Type: core.DynamicMeta,
//})

// generate hello contracts in each shard
// hello contract has one endpoint "hello" which receives a number as argument
// if number is 0 then will throw error, otherwise will do nothing
receiverContracts := deployReceiverContractInAllShards(t, cs)

tokensMapper := make(map[string]string)

// transfer sovereign chain -> main chain with transfer data
// transfer sovereign chain -> main chain -> sovereign chain
// token originated from sovereign chain
for _, token := range tokens {
// register sovereign token identifier
// this will issue a new token on main chain and create a mapper between the identifiers
registerTokens(t, cs, wallet, &nonce, bridgeData.ESDTSafeAddress, token)
tokensMapper[token.Identifier] = chainSim.GetIssuedEsdtIdentifier(t, cs.GetNodeHandler(core.MetachainShardId), getTokenTicker(token.Identifier), token.Type.String())

// get contract from next shard
receiver := receiverContracts[receiverShardId]

// execute operations received from sovereign chain
// expecting the token to be minted in esdt-safe contract with the same properties and transferred with SC call to hello contract
// -------------
// for (dynamic) SFT/MetaESDT the contract will create one more token and keep it forever
// because if the same token is received 2nd time, the contract will just add quantity, not create different token
trnsData := &transferData{
GasLimit: uint64(10000000),
Function: []byte("hello"),
Args: [][]byte{{0x00}},
}
txResult := executeOperation(t, cs, bridgeData.OwnerAccount.Wallet, helloAddress, &bridgeData.OwnerAccount.Nonce, bridgeData.ESDTSafeAddress, []chainSim.ArgsDepositToken{token}, wallet.Bytes, trnsData)
// expecting the operation to fail in hello contract with error "Value should be greater than 0"
// but receive the callback in esdt-safe contract
// the executed operation in hello contract is expected to fail, tokens will be minted and then burned
txResult := executeOperation(t, cs, bridgeData.OwnerAccount.Wallet, receiver.Bytes, &bridgeData.OwnerAccount.Nonce, bridgeData.ESDTSafeAddress, []chainSim.ArgsDepositToken{token}, wallet.Bytes, trnsData)
chainSim.RequireSuccessfulTransaction(t, txResult)
receivedToken := chainSim.ArgsDepositToken{
Identifier: tokensMapper[token.Identifier],
Nonce: token.Nonce,
Amount: token.Amount,
Type: token.Type,
}
if isSftOrMeta(receivedToken.Type) {
chainSim.RequireAccountHasToken(t, cs, getTokenIdentifier(receivedToken), esdtSafeAddr, big.NewInt(1))
} else {
chainSim.RequireAccountHasToken(t, cs, getTokenIdentifier(receivedToken), esdtSafeAddr, big.NewInt(0))
}

// no tokens in contracts
chainSim.RequireAccountHasToken(t, cs, getTokenIdentifier(receivedToken), esdtSafeAddr, big.NewInt(0))
chainSim.RequireAccountHasToken(t, cs, getTokenIdentifier(receivedToken), helloAddrBech32, big.NewInt(0))
waitIfCrossShardProcessing(cs, esdtSafeAddrShard, receiverShardId)
chainSim.RequireAccountHasToken(t, cs, getTokenIdentifier(receivedToken), receiver.Bech32, big.NewInt(0))

// tokens should be burned because operation failed
_ = cs.GenerateBlocks(5)
tokenSupply, err := nodeHandler.GetFacadeHandler().GetTokenSupply(getTokenIdentifier(receivedToken))
tokenSupply, err := cs.GetNodeHandler(esdtSafeAddrShard).GetFacadeHandler().GetTokenSupply(getTokenIdentifier(receivedToken))
require.Nil(t, err)
require.NotNil(t, tokenSupply)
require.Equal(t, receivedToken.Amount.String(), tokenSupply.Burned)

nextShardId(&receiverShardId)
}
}
4 changes: 3 additions & 1 deletion integrationTests/chainSimulator/bridge/esdtSafe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,9 @@ func deployReceiverContractInAllShards(t *testing.T, cs chainSim.ChainSimulator)
}

func isNft(esdtType core.ESDTType) bool {
return esdtType == core.NonFungible || esdtType == core.NonFungibleV2
return esdtType == core.NonFungible ||
esdtType == core.NonFungibleV2 ||
esdtType == core.DynamicNFT
}

func isSftOrMeta(esdtType core.ESDTType) bool {
Expand Down
2 changes: 1 addition & 1 deletion integrationTests/chainSimulator/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func IssueFungible(
func GetIssuedEsdtIdentifier(t *testing.T, nodeHandler process.NodeHandler, ticker string, tokenType string) string {
issuedTokens, err := nodeHandler.GetFacadeHandler().GetAllIssuedESDTs(tokenType)
require.Nil(t, err)
require.GreaterOrEqual(t, len(issuedTokens), 1)
require.GreaterOrEqual(t, len(issuedTokens), 1, "no issued tokens found of type %s", tokenType)

for _, issuedToken := range issuedTokens {
if strings.Contains(issuedToken, ticker) {
Expand Down

0 comments on commit 5d8fe50

Please sign in to comment.