diff --git a/cmd/sovereignnode/chainSimulator/tests/bridge/bridge_test.go b/cmd/sovereignnode/chainSimulator/tests/bridge/bridge_test.go index dd6e68ac686..8cc7331f9b9 100644 --- a/cmd/sovereignnode/chainSimulator/tests/bridge/bridge_test.go +++ b/cmd/sovereignnode/chainSimulator/tests/bridge/bridge_test.go @@ -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" @@ -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" ) @@ -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", @@ -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) { + 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, }) @@ -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) diff --git a/cmd/sovereignnode/chainSimulator/tests/bridge/common.go b/cmd/sovereignnode/chainSimulator/tests/bridge/common.go index 781f45f65ae..b698b9b9a9e 100644 --- a/cmd/sovereignnode/chainSimulator/tests/bridge/common.go +++ b/cmd/sovereignnode/chainSimulator/tests/bridge/common.go @@ -43,25 +43,21 @@ 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, } diff --git a/cmd/sovereignnode/chainSimulator/tests/testdata/esdt-safe.wasm b/cmd/sovereignnode/chainSimulator/tests/testdata/esdt-safe.wasm deleted file mode 100755 index f712af261c7..00000000000 Binary files a/cmd/sovereignnode/chainSimulator/tests/testdata/esdt-safe.wasm and /dev/null differ diff --git a/cmd/sovereignnode/chainSimulator/tests/testdata/fee-market.wasm b/cmd/sovereignnode/chainSimulator/tests/testdata/fee-market.wasm index 2e623b33e16..43a39606875 100755 Binary files a/cmd/sovereignnode/chainSimulator/tests/testdata/fee-market.wasm and b/cmd/sovereignnode/chainSimulator/tests/testdata/fee-market.wasm differ diff --git a/cmd/sovereignnode/chainSimulator/tests/testdata/sov-esdt-safe.wasm b/cmd/sovereignnode/chainSimulator/tests/testdata/sov-esdt-safe.wasm new file mode 100755 index 00000000000..08e5f45e391 Binary files /dev/null and b/cmd/sovereignnode/chainSimulator/tests/testdata/sov-esdt-safe.wasm differ