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 update for new contract version, added new test #11

Open
wants to merge 3 commits into
base: feat/new-esdt-safe
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 112 additions & 12 deletions cmd/sovereignnode/chainSimulator/tests/bridge/bridge_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package bridge

import (
"encoding/hex"
"math/big"
"testing"
"time"

"github.com/multiversx/mx-chain-core-go/core"
coreAPI "github.com/multiversx/mx-chain-core-go/data/api"
"github.com/multiversx/mx-chain-core-go/data/esdt"
"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/stretchr/testify/require"

Expand All @@ -22,7 +24,7 @@ import (
const (
defaultPathToInitialConfig = "../../../../node/config/"
sovereignConfigPath = "../../../config/"
esdtSafeWasmPath = "../testdata/esdt-safe.wasm"
esdtSafeWasmPath = "../testdata/sov-esdt-safe.wasm"
feeMarketWasmPath = "../testdata/fee-market.wasm"
issuePrice = "5000000000000000000"
)
Expand Down Expand Up @@ -51,11 +53,6 @@ func TestSovereignChainSimulator_DeployBridgeContractsThenIssueAndDeposit(t *tes
ApiInterface: api.NewNoApiInterface(),
MinNodesPerShard: 2,
AlterConfigsFunction: func(cfg *config.Configs) {
// Put every enable epoch on 0
cfg.EpochConfig.EnableEpochs = config.EnableEpochs{
MaxNodesChangeEnableEpoch: cfg.EpochConfig.EnableEpochs.MaxNodesChangeEnableEpoch,
BLSMultiSignerEnableEpoch: cfg.EpochConfig.EnableEpochs.BLSMultiSignerEnableEpoch,
}
cfg.GeneralConfig.SovereignConfig.OutgoingSubscribedEvents.SubscribedEvents = []config.SubscribedEvent{
{
Identifier: "deposit",
Expand Down Expand Up @@ -108,10 +105,113 @@ func TestSovereignChainSimulator_DeployBridgeContractsThenIssueAndDeposit(t *tes
numDecimals := 18
tokenIdentifier := chainSim.IssueFungible(t, cs, wallet.Bytes, &nonce, issueCost, tokenName, tokenTicker, numDecimals, supply)

depositAndCheckTokens(t, cs, wallet, nonce, bridgeData, tokenIdentifier, supply)

// Wait for outgoing operations to get unconfirmed and check we have one, which is also saved in storage
time.Sleep(time.Second)

checkOutGoingOperation(t, cs)
}

// This test will:
// - deploy bridge contracts setup
// - generate new wallet and set a token without prefix (a.k.a main chain token)
// - deposit the token in esdt-safe contract
// - check the sender balance is correct
// - check the token burned amount is correct after deposit
func TestSovereignChainSimulator_DeployBridgeContractsAndDepositMainChainToken(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[optional]
So this test "manually" -aka SetKeyValueForAddress + SetEsdtInWallet creates this main chain token in the sovereign chain as if it was previously bridged from main->sov.

Wouldn't it be a more interesting test to bring those main chain tokens first from main->sov, instead of "artifically" planting them here?

Just a thought, could've made the test more "real-life", could be done in another PR if you see it suitable

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added task MX-16637 for this improvement

if testing.Short() {
t.Skip("this is not a short test")
}

outGoingSubscribedAddress := "erd1qqqqqqqqqqqqqpgqmzzm05jeav6d5qvna0q2pmcllelkz8xddz3syjszx5"
cs, err := sovereignChainSimulator.NewSovereignChainSimulator(sovereignChainSimulator.ArgsSovereignChainSimulator{
SovereignConfigPath: sovereignConfigPath,
ArgsChainSimulator: &chainSimulator.ArgsChainSimulator{
BypassTxSignatureCheck: true,
TempDir: t.TempDir(),
PathToInitialConfig: defaultPathToInitialConfig,
GenesisTimestamp: time.Now().Unix(),
RoundDurationInMillis: uint64(6000),
RoundsPerEpoch: core.OptionalUint64{},
ApiInterface: api.NewNoApiInterface(),
MinNodesPerShard: 2,
AlterConfigsFunction: func(cfg *config.Configs) {
cfg.GeneralConfig.SovereignConfig.OutgoingSubscribedEvents.SubscribedEvents = []config.SubscribedEvent{
{
Identifier: "deposit",
Addresses: []string{outGoingSubscribedAddress},
},
}
cfg.GeneralConfig.SovereignConfig.OutgoingSubscribedEvents.TimeToWaitForUnconfirmedOutGoingOperationInSeconds = 1
},
},
})
require.Nil(t, err)
require.NotNil(t, cs)

defer cs.Close()

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

nodeHandler := cs.GetNodeHandler(core.SovereignChainShardId)
mainChainToken := "MAIN-1a2b3c"

initialAddress := "erd1l6xt0rqlyzw56a3k8xwwshq2dcjwy3q9cppucvqsmdyw8r98dz3sae0kxl"
initialAddrBytes, err := cs.GetNodeHandler(0).GetCoreComponents().AddressPubKeyConverter().Decode(initialAddress)
require.Nil(t, err)

chainSim.InitAddressesAndSysAccState(t, cs, initialAddress)
// set main chain token in system account
tokenKey := hex.EncodeToString([]byte(core.ProtectedKeyPrefix + core.ESDTKeyIdentifier + mainChainToken))
err = cs.SetKeyValueForAddress(chainSim.ESDTSystemAccount,
map[string]string{
tokenKey: "0400",
},
)
require.Nil(t, err)

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

expectedESDTSafeAddressBytes, err := nodeHandler.GetCoreComponents().AddressPubKeyConverter().Decode(outGoingSubscribedAddress)
require.Nil(t, err)

initialWallet := dtos.WalletAddress{Bech32: initialAddress, Bytes: initialAddrBytes}
bridgeData := deploySovereignBridgeSetup(t, cs, initialWallet, esdtSafeWasmPath, feeMarketWasmPath)
require.Equal(t, expectedESDTSafeAddressBytes, bridgeData.ESDTSafeAddress)

// generate wallet and set main chain token supply
wallet, err := cs.GenerateAndMintWalletAddress(core.SovereignChainShardId, chainSim.InitialAmount)
require.Nil(t, err)
mainChainTokenSupply, _ := big.NewInt(0).SetString("123000000000000000000", 10)
chainSim.SetEsdtInWallet(t, cs, wallet, mainChainToken, 0, esdt.ESDigitalToken{Value: mainChainTokenSupply})
nonce := GetNonce(t, nodeHandler, wallet.Bech32)

depositAndCheckTokens(t, cs, wallet, nonce, bridgeData, mainChainToken, mainChainTokenSupply)

// Wait for outgoing operations to get unconfirmed and check we have one, which is also saved in storage
time.Sleep(time.Second)

checkOutGoingOperation(t, cs)
}

func depositAndCheckTokens(
t *testing.T,
cs chainSim.ChainSimulator,
wallet dtos.WalletAddress,
nonce uint64,
bridgeData ArgsBridgeSetup,
mainChainToken string,
mainChainTokenSupply *big.Int,
) {
nodeHandler := cs.GetNodeHandler(core.SovereignChainShardId)

amountToDeposit, _ := big.NewInt(0).SetString("2000000000000000000", 10)
depositTokens := make([]chainSim.ArgsDepositToken, 0)
depositTokens = append(depositTokens, chainSim.ArgsDepositToken{
Identifier: tokenIdentifier,
Identifier: mainChainToken,
Nonce: 0,
Amount: amountToDeposit,
})
Expand All @@ -123,20 +223,20 @@ func TestSovereignChainSimulator_DeployBridgeContractsThenIssueAndDeposit(t *tes
require.Nil(t, err)
require.NotNil(t, tokens)
require.True(t, len(tokens) == 2)
require.Equal(t, big.NewInt(0).Sub(supply, amountToDeposit).String(), tokens[tokenIdentifier].GetValue().String())
require.Equal(t, big.NewInt(0).Sub(mainChainTokenSupply, amountToDeposit).String(), tokens[mainChainToken].GetValue().String())

tokenSupply, err := nodeHandler.GetFacadeHandler().GetTokenSupply(tokenIdentifier)
tokenSupply, err := nodeHandler.GetFacadeHandler().GetTokenSupply(mainChainToken)
require.Nil(t, err)
require.NotNil(t, tokenSupply)
require.Equal(t, amountToDeposit.String(), tokenSupply.Burned)
}

// Wait for outgoing operations to get unconfirmed and check we have one, which is also saved in storage
time.Sleep(time.Second)
func checkOutGoingOperation(t *testing.T, cs chainSim.ChainSimulator) {
nodeHandler := cs.GetNodeHandler(core.SovereignChainShardId)

outGoingOps := nodeHandler.GetRunTypeComponents().OutGoingOperationsPoolHandler().GetUnconfirmedOperations()
require.Len(t, outGoingOps, 1)
require.Len(t, outGoingOps[0].OutGoingOperations, 1)

outGoingOp := outGoingOps[0].OutGoingOperations[0]
savedMarshalledTx, err := nodeHandler.GetDataComponents().StorageService().Get(dataRetriever.TransactionUnit, outGoingOp.Hash)
require.Nil(t, err)
Expand Down
12 changes: 3 additions & 9 deletions cmd/sovereignnode/chainSimulator/tests/bridge/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,19 @@ func deploySovereignBridgeSetup(
wallet dtos.WalletAddress,
esdtSafeWasmPath string,
feeMarketWasmPath string,
) *ArgsBridgeSetup {
) ArgsBridgeSetup {
nodeHandler := cs.GetNodeHandler(core.SovereignChainShardId)
systemScAddress := chainSim.GetSysAccBytesAddress(t, nodeHandler)
nonce := GetNonce(t, nodeHandler, wallet.Bech32)

esdtSafeArgs := "@01" // is_sovereign_chain
esdtSafeArgs := "@000000000000000005002412c4ab184562d62a3eddaa7227730e9f17c53268a3" // pre-computed fee_market_address
esdtSafeAddress := chainSim.DeployContract(t, cs, wallet.Bytes, &nonce, systemScAddress, esdtSafeArgs, esdtSafeWasmPath)

feeMarketArgs := "@" + hex.EncodeToString(esdtSafeAddress) + // esdt_safe_address
"@00" // no fee
feeMarketAddress := chainSim.DeployContract(t, cs, wallet.Bytes, &nonce, systemScAddress, feeMarketArgs, feeMarketWasmPath)

setFeeMarketAddressData := "setFeeMarketAddress" +
"@" + hex.EncodeToString(feeMarketAddress)
chainSim.SendTransactionWithSuccess(t, cs, wallet.Bytes, &nonce, esdtSafeAddress, chainSim.ZeroValue, setFeeMarketAddressData, uint64(10000000))

chainSim.SendTransactionWithSuccess(t, cs, wallet.Bytes, &nonce, esdtSafeAddress, chainSim.ZeroValue, "unpause", uint64(10000000))

return &ArgsBridgeSetup{
return ArgsBridgeSetup{
ESDTSafeAddress: esdtSafeAddress,
FeeMarketAddress: feeMarketAddress,
}
Expand Down
Binary file not shown.
Binary file modified cmd/sovereignnode/chainSimulator/tests/testdata/fee-market.wasm
Binary file not shown.
Binary file not shown.