From eb27b7a7d0618234655d92bf248dfe2a769d7701 Mon Sep 17 00:00:00 2001 From: Ash-KODES Date: Wed, 8 May 2024 23:46:07 +0530 Subject: [PATCH 1/8] Added authorizer stake/unstake commands handlers --- .../zwalletcli_authorizer_stake_test.go | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 tests/cli_tests/zwalletcli_authorizer_stake_test.go diff --git a/tests/cli_tests/zwalletcli_authorizer_stake_test.go b/tests/cli_tests/zwalletcli_authorizer_stake_test.go new file mode 100644 index 0000000000..1ac5320def --- /dev/null +++ b/tests/cli_tests/zwalletcli_authorizer_stake_test.go @@ -0,0 +1,111 @@ +package cli_tests + +import ( + "encoding/json" + "errors" + "fmt" + "io" + "os" + "regexp" + "strconv" + "strings" + "sync" + "testing" + "time" + + "github.com/0chain/system_test/internal/api/util/test" + + climodel "github.com/0chain/system_test/internal/cli/model" + cliutils "github.com/0chain/system_test/internal/cli/util" + "github.com/stretchr/testify/require" +) + +func TestAuthorizerStake(testSetup *testing.T) { + t := test.NewSystemTest(testSetup) + t.SetSmokeTests("Staking tokens against valid authorizer with valid tokens should work") + + var authorizer climodel.Validator + t.TestSetup("get sharders", func() { + if _, err := os.Stat("./config/" + sharder01NodeDelegateWalletName + "_wallet.json"); err != nil { + t.Skipf("miner node owner wallet located at %s is missing", "./config/"+sharder01NodeDelegateWalletName+"_wallet.json") + } + + createWallet(t) + + createWalletForName(sharder01NodeDelegateWalletName) + + sharders := getShardersListForWallet(t, sharder01NodeDelegateWalletName) + + sharderNodeDelegateWallet, err := getWalletForName(t, configPath, sharder01NodeDelegateWalletName) + require.Nil(t, err, "error fetching sharderNodeDelegate wallet") + + for i, s := range sharders { + if s.ID != sharderNodeDelegateWallet.ClientID { + sharder = sharders[i] + break + } + } + }) + + t.Parallel() + + t.RunWithTimeout("Staking tokens against valid authorizer with valid tokens should work", 5*time.Minute, func(t *test.SystemTest) { // todo: slow + createWallet(t) + + output, err := minerOrSharderLock(t, configPath, createParams(map[string]interface{}{ + "miner_id": miner.ID, + "tokens": 2.0, + }), true) + require.Nil(t, err, "error staking tokens against a node") + require.Len(t, output, 1) + require.Regexp(t, lockOutputRegex, output[0]) + + poolsInfo, err := pollForPoolInfo(t, miner.ID) + require.Nil(t, err) + require.Equal(t, float64(2.0), intToZCN(poolsInfo.Balance)) + + // Unlock should work + output, err = minerOrSharderUnlock(t, configPath, createParams(map[string]interface{}{ + "miner_id": miner.ID, + }), true) + require.Nil(t, err, "error unlocking tokens against a node") + require.Len(t, output, 1) + require.Equal(t, "tokens unlocked", output[0]) + + output, err = minerSharderPoolInfo(t, configPath, createParams(map[string]interface{}{ + "id": miner.ID, + }), true) + + require.NotNil(t, err, "expected error when requesting unlocked pool but got output", strings.Join(output, "\n")) + require.Len(t, output, 1) + require.Equal(t, `resource_not_found: can't find pool stats`, output[0]) + }) + + +} + +func authorizerLock(t *test.SystemTest, cliConfigFilename, params string, retry bool) ([]string, error) { + return authorizerLockForWallet(t, cliConfigFilename, params, escapedTestName(t), retry) +} + +func authorizerLockForWallet(t *test.SystemTest, cliConfigFilename, params, wallet string, retry bool) ([]string, error) { + t.Log("locking tokens against authorizers...") + if retry { + return cliutils.RunCommand(t, fmt.Sprintf("./zbox sp-lock %s --silent --wallet %s_wallet.json --configDir ./config --config %s", params, wallet, cliConfigFilename), 3, time.Second) + } else { + return cliutils.RunCommandWithoutRetry(fmt.Sprintf("./zbox sp-lock %s --silent --wallet %s_wallet.json --configDir ./config --config %s", params, wallet, cliConfigFilename)) + } +} + +func authorizerUnlock(t *test.SystemTest, cliConfigFilename, params string, retry bool) ([]string, error) { + return minerOrSharderUnlockForWallet(t, cliConfigFilename, params, escapedTestName(t), retry) +} + +func authorizerUnlockForWallet(t *test.SystemTest, cliConfigFilename, params, wallet string, retry bool) ([]string, error) { + t.Log("unlocking tokens from authorizer pool...") + if retry { + return cliutils.RunCommand(t, fmt.Sprintf("./zbox sp-unlock %s --silent --wallet %s_wallet.json --configDir ./config --config %s", params, wallet, cliConfigFilename), 3, time.Second) + } else { + return cliutils.RunCommandWithoutRetry(fmt.Sprintf("./zbox sp-unlock %s --silent --wallet %s_wallet.json --configDir ./config --config %s", params, wallet, cliConfigFilename)) + } +} \ No newline at end of file From 68a9cdca7b78c62dd94ad59dc4f8c7f541b13858 Mon Sep 17 00:00:00 2001 From: Ash-KODES Date: Thu, 9 May 2024 00:53:36 +0530 Subject: [PATCH 2/8] Adding more case for stake pool test. --- internal/cli/model/model.go | 4 + tests/cli_tests/main_test.go | 12 +- .../zwalletcli_authorizer_stake_test.go | 126 ++++++++++++++---- 3 files changed, 112 insertions(+), 30 deletions(-) diff --git a/internal/cli/model/model.go b/internal/cli/model/model.go index de279353f8..b2fac204f8 100644 --- a/internal/cli/model/model.go +++ b/internal/cli/model/model.go @@ -450,6 +450,10 @@ type MinerSCNodes struct { Nodes []Node `json:"Nodes"` } +type AutorizerNodes struct { + Nodes []Node `json:"Nodes"` +} + type MinerSCDelegatePoolInfo struct { ID string `json:"id"` Balance int64 `json:"balance"` diff --git a/tests/cli_tests/main_test.go b/tests/cli_tests/main_test.go index 742523e034..e346c1cc1b 100644 --- a/tests/cli_tests/main_test.go +++ b/tests/cli_tests/main_test.go @@ -93,12 +93,12 @@ const ( ) var ( - miner01ID string - miner02ID string - miner03ID string - sharder01ID string - sharder02ID string - + miner01ID string + miner02ID string + miner03ID string + sharder01ID string + sharder02ID string + authorizer01ID string ethereumNodeURL string tokenAddress string ethereumAddress string diff --git a/tests/cli_tests/zwalletcli_authorizer_stake_test.go b/tests/cli_tests/zwalletcli_authorizer_stake_test.go index 1ac5320def..e8c91981de 100644 --- a/tests/cli_tests/zwalletcli_authorizer_stake_test.go +++ b/tests/cli_tests/zwalletcli_authorizer_stake_test.go @@ -24,24 +24,18 @@ func TestAuthorizerStake(testSetup *testing.T) { t := test.NewSystemTest(testSetup) t.SetSmokeTests("Staking tokens against valid authorizer with valid tokens should work") - var authorizer climodel.Validator - t.TestSetup("get sharders", func() { - if _, err := os.Stat("./config/" + sharder01NodeDelegateWalletName + "_wallet.json"); err != nil { - t.Skipf("miner node owner wallet located at %s is missing", "./config/"+sharder01NodeDelegateWalletName+"_wallet.json") - } - - createWallet(t) - - createWalletForName(sharder01NodeDelegateWalletName) - - sharders := getShardersListForWallet(t, sharder01NodeDelegateWalletName) + var authorizer climodel.Node + var authorizers climodel.AutorizerNodes + t.TestSetup("Get authorizer details", func() { + output, err := listAuthorizer(t, configPath, "--json") + require.NoError(t, err, "error listing authorizers") + require.Len(t, output, 1) - sharderNodeDelegateWallet, err := getWalletForName(t, configPath, sharder01NodeDelegateWalletName) - require.Nil(t, err, "error fetching sharderNodeDelegate wallet") + err = json.Unmarshal([]byte(output[0]), &authorizers) + require.Nil(t, err, "error unmarshalling bridge-list-auth json output") - for i, s := range sharders { - if s.ID != sharderNodeDelegateWallet.ClientID { - sharder = sharders[i] + for _, authorizer = range authorizers.Nodes { + if authorizer.ID == authorizer01ID { break } } @@ -52,28 +46,29 @@ func TestAuthorizerStake(testSetup *testing.T) { t.RunWithTimeout("Staking tokens against valid authorizer with valid tokens should work", 5*time.Minute, func(t *test.SystemTest) { // todo: slow createWallet(t) - output, err := minerOrSharderLock(t, configPath, createParams(map[string]interface{}{ - "miner_id": miner.ID, - "tokens": 2.0, + // Lock should work + output, err := authorizerLock(t, configPath, createParams(map[string]interface{}{ + "authorizer_id": authorizer.ID, + "tokens": 2.0, }), true) require.Nil(t, err, "error staking tokens against a node") require.Len(t, output, 1) require.Regexp(t, lockOutputRegex, output[0]) - poolsInfo, err := pollForPoolInfo(t, miner.ID) + poolsInfo, err := pollForPoolInfo(t, authorizer.ID) require.Nil(t, err) require.Equal(t, float64(2.0), intToZCN(poolsInfo.Balance)) // Unlock should work - output, err = minerOrSharderUnlock(t, configPath, createParams(map[string]interface{}{ - "miner_id": miner.ID, + output, err = authorizerUnlock(t, configPath, createParams(map[string]interface{}{ + "authorizer_id": authorizer.ID, }), true) require.Nil(t, err, "error unlocking tokens against a node") require.Len(t, output, 1) require.Equal(t, "tokens unlocked", output[0]) output, err = minerSharderPoolInfo(t, configPath, createParams(map[string]interface{}{ - "id": miner.ID, + "id": authorizer.ID, }), true) require.NotNil(t, err, "expected error when requesting unlocked pool but got output", strings.Join(output, "\n")) @@ -81,7 +76,90 @@ func TestAuthorizerStake(testSetup *testing.T) { require.Equal(t, `resource_not_found: can't find pool stats`, output[0]) }) + t.Run("Staking tokens with insufficient balance should fail", func(t *test.SystemTest) { + _, err := executeFaucetWithTokens(t, configPath, 1.0) + require.Nil(t, err, "error executing faucet") + output, err := authorizerLock(t, configPath, createParams(map[string]interface{}{ + "miner_id": authorizer.ID, + "tokens": 10, + }), false) + require.NotNil(t, err, "expected error when staking tokens with insufficient balance but got output: ", strings.Join(output, "\n")) + require.Len(t, output, 1) + require.Equal(t, "stake_pool_lock_failed: stake pool digging error: lock amount is greater than balance", output[0]) + }) + + t.Run("Staking tokens against invalid node id should fail", func(t *test.SystemTest) { + createWallet(t) + + output, err := authorizerLock(t, configPath, createParams(map[string]interface{}{ + "authorizer_id": "abcdefgh", + "tokens": 1, + }), false) + require.NotNil(t, err, "expected error when staking tokens against invalid miner but got output", strings.Join(output, "\n")) + require.Len(t, output, 1) + require.Equal(t, "stake_pool_lock_failed: can't get stake pool: get_stake_pool: miner not found or genesis miner used", output[0]) + }) + + t.Run("Staking negative tokens against valid authorizer should fail", func(t *test.SystemTest) { + createWallet(t) + + output, err := authorizerLock(t, configPath, createParams(map[string]interface{}{ + "authorizer_id": authorizer.ID, + "tokens": -1, + }), false) + require.NotNil(t, err, "expected error when staking negative tokens but got output: ", strings.Join(output, "\n")) + require.Len(t, output, 1) + require.Equal(t, `invalid token amount: negative`, output[0]) + }) + + t.Run("Staking 0 tokens against authorizer should fail", func(t *test.SystemTest) { + createWallet(t) + + output, err := minerOrSharderLock(t, configPath, createParams(map[string]interface{}{ + "authorizer_id": authorizer.ID, + "tokens": 0, + }), false) + require.NotNil(t, err, "expected error when staking more tokens than max_stake but got output: ", strings.Join(output, "\n")) + require.Len(t, output, 1) + require.Equal(t, "stake_pool_lock_failed: no stake to lock: 0", output[0]) + }) + + t.Run("Unlock tokens with invalid node id should fail", func(t *test.SystemTest) { + createWallet(t) + + output, err := authorizerLock(t, configPath, createParams(map[string]interface{}{ + "authorizer_id": authorizer.ID, + "tokens": 2, + }), true) + require.Nil(t, err, "error staking tokens against a node") + require.Len(t, output, 1) + require.Regexp(t, lockOutputRegex, output[0]) + + output, err = authorizerUnlock(t, configPath, createParams(map[string]interface{}{ + "authorizer_id": "abcdefgh", + }), false) + require.NotNil(t, err, "expected error when using invalid node id") + require.Len(t, output, 1) + require.Equal(t, "stake_pool_unlock_failed: can't get related stake pool: get_stake_pool: miner not found or genesis miner used", output[0]) + + // teardown + _, err = authorizerUnlock(t, configPath, createParams(map[string]interface{}{ + "authorizer_id": authorizer.ID, + }), true) + if err != nil { + t.Log("error unlocking tokens after test: ", t.Name()) + } + }) +} + +func listAuthorizer(t *test.SystemTest, cliConfigFilename, params string) ([]string, error) { + cmd := fmt.Sprintf( + "./zwallet bridge-list-auth --silent "+ + "--configDir ./config --config %s", + configPath, + ) + return cliutils.RunCommand(t, cmd, 3, time.Second*2) } func authorizerLock(t *test.SystemTest, cliConfigFilename, params string, retry bool) ([]string, error) { @@ -108,4 +186,4 @@ func authorizerUnlockForWallet(t *test.SystemTest, cliConfigFilename, params, wa } else { return cliutils.RunCommandWithoutRetry(fmt.Sprintf("./zbox sp-unlock %s --silent --wallet %s_wallet.json --configDir ./config --config %s", params, wallet, cliConfigFilename)) } -} \ No newline at end of file +} From c7c194b19263a2e1e5b1a74353f2aee9b61267e3 Mon Sep 17 00:00:00 2001 From: Ash-KODES Date: Thu, 9 May 2024 01:08:27 +0530 Subject: [PATCH 3/8] Using authorizer 1 ID in case of staking 0 token --- tests/cli_tests/zwalletcli_authorizer_stake_test.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/tests/cli_tests/zwalletcli_authorizer_stake_test.go b/tests/cli_tests/zwalletcli_authorizer_stake_test.go index e8c91981de..57f3915c61 100644 --- a/tests/cli_tests/zwalletcli_authorizer_stake_test.go +++ b/tests/cli_tests/zwalletcli_authorizer_stake_test.go @@ -2,14 +2,8 @@ package cli_tests import ( "encoding/json" - "errors" "fmt" - "io" - "os" - "regexp" - "strconv" "strings" - "sync" "testing" "time" @@ -117,7 +111,7 @@ func TestAuthorizerStake(testSetup *testing.T) { createWallet(t) output, err := minerOrSharderLock(t, configPath, createParams(map[string]interface{}{ - "authorizer_id": authorizer.ID, + "authorizer_id": miner01ID, "tokens": 0, }), false) require.NotNil(t, err, "expected error when staking more tokens than max_stake but got output: ", strings.Join(output, "\n")) From 643d9084b9ac2c26c011c7a2c006de221b64b6f8 Mon Sep 17 00:00:00 2001 From: Ash-KODES Date: Thu, 9 May 2024 14:28:39 +0530 Subject: [PATCH 4/8] Bug fix in authorizerUnlockForWallet handler --- tests/cli_tests/zwalletcli_authorizer_stake_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/cli_tests/zwalletcli_authorizer_stake_test.go b/tests/cli_tests/zwalletcli_authorizer_stake_test.go index 57f3915c61..cd8de3f54f 100644 --- a/tests/cli_tests/zwalletcli_authorizer_stake_test.go +++ b/tests/cli_tests/zwalletcli_authorizer_stake_test.go @@ -111,7 +111,7 @@ func TestAuthorizerStake(testSetup *testing.T) { createWallet(t) output, err := minerOrSharderLock(t, configPath, createParams(map[string]interface{}{ - "authorizer_id": miner01ID, + "authorizer_id": authorizer01ID, "tokens": 0, }), false) require.NotNil(t, err, "expected error when staking more tokens than max_stake but got output: ", strings.Join(output, "\n")) @@ -170,7 +170,7 @@ func authorizerLockForWallet(t *test.SystemTest, cliConfigFilename, params, wall } func authorizerUnlock(t *test.SystemTest, cliConfigFilename, params string, retry bool) ([]string, error) { - return minerOrSharderUnlockForWallet(t, cliConfigFilename, params, escapedTestName(t), retry) + return authorizerUnlockForWallet(t, cliConfigFilename, params, escapedTestName(t), retry) } func authorizerUnlockForWallet(t *test.SystemTest, cliConfigFilename, params, wallet string, retry bool) ([]string, error) { From b29d6e1024fc0ad4ef02ecc4a9c0c29b0b9555d1 Mon Sep 17 00:00:00 2001 From: Ash-KODES Date: Sat, 11 May 2024 00:39:21 +0530 Subject: [PATCH 5/8] Adding configuration for default authorizer in main.go and edge case added in TestSetup --- tests/cli_tests/main_test.go | 4 ++++ tests/cli_tests/zwalletcli_authorizer_stake_test.go | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/tests/cli_tests/main_test.go b/tests/cli_tests/main_test.go index e346c1cc1b..a9ba2eae59 100644 --- a/tests/cli_tests/main_test.go +++ b/tests/cli_tests/main_test.go @@ -32,6 +32,7 @@ func setupDefaultConfig() { viper.SetDefault("nodes.miner03ID", "c6f4b8ce5da386b278ba8c4e6cf98b24b32d15bc675b4d12c95e082079c91937") viper.SetDefault("nodes.sharder01ID", "ea26431f8adb7061766f1d6bbcc3b292d70dd59960d857f04b8a75e6a5bbe04f") viper.SetDefault("nodes.sharder02ID", "30001a01a888584772b7fee13934021ab8557e0ed471c0a3a454e9164180aef1") + viper.SetDefault("nodes.authorizer01ID", "d6e9b3222434faa043c683d1a939d6a0fa2818c4d56e794974d64a32005330d3") } // SetupConfig setups the main configuration system. @@ -51,6 +52,7 @@ func setupConfig() { miner03ID = viper.GetString("nodes.miner03ID") sharder01ID = viper.GetString("nodes.sharder01ID") sharder02ID = viper.GetString("nodes.sharder02ID") + authorizer01ID = viper.GetString("nodes.authorizer01ID") parsedConfig := config.Parse(filepath.Join(".", path, "cli_tests_config.yaml")) defaultTestTimeout, err := time.ParseDuration(parsedConfig.DefaultTestCaseTimeout) @@ -88,6 +90,7 @@ const ( miner03NodeDelegateWalletName = "wallets/miner03_node_delegate" sharder01NodeDelegateWalletName = "wallets/sharder01_node_delegate" sharder02NodeDelegateWalletName = "wallets/sharder02_node_delegate" + authorizer01NodeDelegateWallet = "wallets/authorizer01_node_delegate" stakingWallet = "wallets/staking" zboxTeamWallet = "wallets/zbox_team" ) @@ -147,6 +150,7 @@ func TestMain(m *testing.M) { strings.HasSuffix(f, miner03NodeDelegateWalletName+"_wallet.json") || strings.HasSuffix(f, sharder01NodeDelegateWalletName+"_wallet.json") || strings.HasSuffix(f, sharder02NodeDelegateWalletName+"_wallet.json") || + strings.HasSuffix(f, authorizer01NodeDelegateWallet+"_wallet.json") || strings.HasSuffix(f, stakingWallet+"_wallet.json") || strings.HasSuffix(f, zboxTeamWallet+"_wallet.json") { continue diff --git a/tests/cli_tests/zwalletcli_authorizer_stake_test.go b/tests/cli_tests/zwalletcli_authorizer_stake_test.go index cd8de3f54f..5c496144fd 100644 --- a/tests/cli_tests/zwalletcli_authorizer_stake_test.go +++ b/tests/cli_tests/zwalletcli_authorizer_stake_test.go @@ -3,6 +3,7 @@ package cli_tests import ( "encoding/json" "fmt" + "os" "strings" "testing" "time" @@ -21,7 +22,11 @@ func TestAuthorizerStake(testSetup *testing.T) { var authorizer climodel.Node var authorizers climodel.AutorizerNodes t.TestSetup("Get authorizer details", func() { + if _, err := os.Stat("./config/" + authorizer01NodeDelegateWallet + "_wallet.json"); err != nil { + t.Skipf("miner node owner wallet located at %s is missing", "./config/"+authorizer01NodeDelegateWallet+"_wallet.json") + } output, err := listAuthorizer(t, configPath, "--json") + t.Log("list of authorizer " , output) require.NoError(t, err, "error listing authorizers") require.Len(t, output, 1) From 213f0f32a150da95d7994554e582862256c6e658 Mon Sep 17 00:00:00 2001 From: Ash-KODES Date: Sat, 11 May 2024 00:47:27 +0530 Subject: [PATCH 6/8] Lint fix --- tests/cli_tests/config/zbox_config.yaml | 2 +- tests/cli_tests/zwalletcli_authorizer_stake_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/cli_tests/config/zbox_config.yaml b/tests/cli_tests/config/zbox_config.yaml index bd2836e3b2..455a311873 100644 --- a/tests/cli_tests/config/zbox_config.yaml +++ b/tests/cli_tests/config/zbox_config.yaml @@ -1,4 +1,4 @@ -block_worker: https://dev.zus.network/dns +block_worker: https://dev-5.devnet-0chain.net/dns confirmation_chain_length: 3 ethereum_node_url: "https://rpc.tenderly.co/fork/5b7ffac9-50cc-4169-b0ca-6fd203d26ef6" min_confirmation: 50 diff --git a/tests/cli_tests/zwalletcli_authorizer_stake_test.go b/tests/cli_tests/zwalletcli_authorizer_stake_test.go index 5c496144fd..2f171974ea 100644 --- a/tests/cli_tests/zwalletcli_authorizer_stake_test.go +++ b/tests/cli_tests/zwalletcli_authorizer_stake_test.go @@ -26,7 +26,7 @@ func TestAuthorizerStake(testSetup *testing.T) { t.Skipf("miner node owner wallet located at %s is missing", "./config/"+authorizer01NodeDelegateWallet+"_wallet.json") } output, err := listAuthorizer(t, configPath, "--json") - t.Log("list of authorizer " , output) + t.Log("list of authorizer ", output) require.NoError(t, err, "error listing authorizers") require.Len(t, output, 1) From 245f9a2363ab61b03744aaf7c93676b69074df4f Mon Sep 17 00:00:00 2001 From: Ash-KODES Date: Sun, 12 May 2024 01:22:33 +0530 Subject: [PATCH 7/8] Adding sp-info and testcase fail fix --- tests/cli_tests/main_test.go | 2 +- .../zwalletcli_authorizer_stake_test.go | 211 ++++++++++-------- 2 files changed, 114 insertions(+), 99 deletions(-) diff --git a/tests/cli_tests/main_test.go b/tests/cli_tests/main_test.go index a9ba2eae59..cfb3ef09ba 100644 --- a/tests/cli_tests/main_test.go +++ b/tests/cli_tests/main_test.go @@ -32,7 +32,7 @@ func setupDefaultConfig() { viper.SetDefault("nodes.miner03ID", "c6f4b8ce5da386b278ba8c4e6cf98b24b32d15bc675b4d12c95e082079c91937") viper.SetDefault("nodes.sharder01ID", "ea26431f8adb7061766f1d6bbcc3b292d70dd59960d857f04b8a75e6a5bbe04f") viper.SetDefault("nodes.sharder02ID", "30001a01a888584772b7fee13934021ab8557e0ed471c0a3a454e9164180aef1") - viper.SetDefault("nodes.authorizer01ID", "d6e9b3222434faa043c683d1a939d6a0fa2818c4d56e794974d64a32005330d3") + viper.SetDefault("nodes.authorizer01ID", "7b07c0489e2f35d7c13160f4da2866b4aa69aa4e8d2b2cd9c4fc002693dca5d7") } // SetupConfig setups the main configuration system. diff --git a/tests/cli_tests/zwalletcli_authorizer_stake_test.go b/tests/cli_tests/zwalletcli_authorizer_stake_test.go index 2f171974ea..6fa93fa936 100644 --- a/tests/cli_tests/zwalletcli_authorizer_stake_test.go +++ b/tests/cli_tests/zwalletcli_authorizer_stake_test.go @@ -1,9 +1,9 @@ package cli_tests import ( - "encoding/json" + // "encoding/json" "fmt" - "os" + // "os" "strings" "testing" "time" @@ -13,51 +13,53 @@ import ( climodel "github.com/0chain/system_test/internal/cli/model" cliutils "github.com/0chain/system_test/internal/cli/util" "github.com/stretchr/testify/require" + "regexp" ) +var authorizerLockOutputRegex = regexp.MustCompile("txn hash: [a-f0-9]{64}") + func TestAuthorizerStake(testSetup *testing.T) { t := test.NewSystemTest(testSetup) t.SetSmokeTests("Staking tokens against valid authorizer with valid tokens should work") var authorizer climodel.Node - var authorizers climodel.AutorizerNodes + // var authorizers climodel.AutorizerNodes t.TestSetup("Get authorizer details", func() { - if _, err := os.Stat("./config/" + authorizer01NodeDelegateWallet + "_wallet.json"); err != nil { - t.Skipf("miner node owner wallet located at %s is missing", "./config/"+authorizer01NodeDelegateWallet+"_wallet.json") - } + // if _, err := os.Stat("./config/" + authorizer01NodeDelegateWallet + "_wallet.json"); err != nil { + // t.Skipf("authorizer node owner wallet located at %s is missing", "./config/"+authorizer01NodeDelegateWallet+"_wallet.json") + // } output, err := listAuthorizer(t, configPath, "--json") t.Log("list of authorizer ", output) require.NoError(t, err, "error listing authorizers") - require.Len(t, output, 1) - err = json.Unmarshal([]byte(output[0]), &authorizers) - require.Nil(t, err, "error unmarshalling bridge-list-auth json output") + // err = json.Unmarshal([]byte(output[0]), &authorizers) + // require.Nil(t, err, "error unmarshalling bridge-list-auth json output") - for _, authorizer = range authorizers.Nodes { - if authorizer.ID == authorizer01ID { - break - } - } + // for _, authorizer = range authorizers.Nodes { + // if authorizer.ID == authorizer01ID { + // break + // } + // } + authorizer.ID = authorizer01ID + t.Log("authorizer", authorizer.ID) }) t.Parallel() t.RunWithTimeout("Staking tokens against valid authorizer with valid tokens should work", 5*time.Minute, func(t *test.SystemTest) { // todo: slow createWallet(t) - + t.Log("Before authorizer") + t.Log("authorizerID", authorizer.ID) // Lock should work output, err := authorizerLock(t, configPath, createParams(map[string]interface{}{ "authorizer_id": authorizer.ID, "tokens": 2.0, }), true) + t.Log("output", output, "Error", err) require.Nil(t, err, "error staking tokens against a node") require.Len(t, output, 1) - require.Regexp(t, lockOutputRegex, output[0]) - - poolsInfo, err := pollForPoolInfo(t, authorizer.ID) - require.Nil(t, err) - require.Equal(t, float64(2.0), intToZCN(poolsInfo.Balance)) - + require.Regexp(t, authorizerLockOutputRegex, output[0]) + // Unlock should work output, err = authorizerUnlock(t, configPath, createParams(map[string]interface{}{ "authorizer_id": authorizer.ID, @@ -66,7 +68,7 @@ func TestAuthorizerStake(testSetup *testing.T) { require.Len(t, output, 1) require.Equal(t, "tokens unlocked", output[0]) - output, err = minerSharderPoolInfo(t, configPath, createParams(map[string]interface{}{ + output, err = authorizerPoolInfo(t, configPath, createParams(map[string]interface{}{ "id": authorizer.ID, }), true) @@ -74,82 +76,81 @@ func TestAuthorizerStake(testSetup *testing.T) { require.Len(t, output, 1) require.Equal(t, `resource_not_found: can't find pool stats`, output[0]) }) - - t.Run("Staking tokens with insufficient balance should fail", func(t *test.SystemTest) { - _, err := executeFaucetWithTokens(t, configPath, 1.0) - require.Nil(t, err, "error executing faucet") - - output, err := authorizerLock(t, configPath, createParams(map[string]interface{}{ - "miner_id": authorizer.ID, - "tokens": 10, - }), false) - require.NotNil(t, err, "expected error when staking tokens with insufficient balance but got output: ", strings.Join(output, "\n")) - require.Len(t, output, 1) - require.Equal(t, "stake_pool_lock_failed: stake pool digging error: lock amount is greater than balance", output[0]) - }) - - t.Run("Staking tokens against invalid node id should fail", func(t *test.SystemTest) { - createWallet(t) - - output, err := authorizerLock(t, configPath, createParams(map[string]interface{}{ - "authorizer_id": "abcdefgh", - "tokens": 1, - }), false) - require.NotNil(t, err, "expected error when staking tokens against invalid miner but got output", strings.Join(output, "\n")) - require.Len(t, output, 1) - require.Equal(t, "stake_pool_lock_failed: can't get stake pool: get_stake_pool: miner not found or genesis miner used", output[0]) - }) - - t.Run("Staking negative tokens against valid authorizer should fail", func(t *test.SystemTest) { - createWallet(t) - - output, err := authorizerLock(t, configPath, createParams(map[string]interface{}{ - "authorizer_id": authorizer.ID, - "tokens": -1, - }), false) - require.NotNil(t, err, "expected error when staking negative tokens but got output: ", strings.Join(output, "\n")) - require.Len(t, output, 1) - require.Equal(t, `invalid token amount: negative`, output[0]) - }) - - t.Run("Staking 0 tokens against authorizer should fail", func(t *test.SystemTest) { - createWallet(t) - - output, err := minerOrSharderLock(t, configPath, createParams(map[string]interface{}{ - "authorizer_id": authorizer01ID, - "tokens": 0, - }), false) - require.NotNil(t, err, "expected error when staking more tokens than max_stake but got output: ", strings.Join(output, "\n")) - require.Len(t, output, 1) - require.Equal(t, "stake_pool_lock_failed: no stake to lock: 0", output[0]) - }) - - t.Run("Unlock tokens with invalid node id should fail", func(t *test.SystemTest) { - createWallet(t) - - output, err := authorizerLock(t, configPath, createParams(map[string]interface{}{ - "authorizer_id": authorizer.ID, - "tokens": 2, - }), true) - require.Nil(t, err, "error staking tokens against a node") - require.Len(t, output, 1) - require.Regexp(t, lockOutputRegex, output[0]) - - output, err = authorizerUnlock(t, configPath, createParams(map[string]interface{}{ - "authorizer_id": "abcdefgh", - }), false) - require.NotNil(t, err, "expected error when using invalid node id") - require.Len(t, output, 1) - require.Equal(t, "stake_pool_unlock_failed: can't get related stake pool: get_stake_pool: miner not found or genesis miner used", output[0]) - - // teardown - _, err = authorizerUnlock(t, configPath, createParams(map[string]interface{}{ - "authorizer_id": authorizer.ID, - }), true) - if err != nil { - t.Log("error unlocking tokens after test: ", t.Name()) - } - }) + t.Run("Staking tokens with insufficient balance should fail", func(t *test.SystemTest) { + _, err := executeFaucetWithTokens(t, configPath, 1.0) + require.Nil(t, err, "error executing faucet") + + output, err := authorizerLock(t, configPath, createParams(map[string]interface{}{ + "miner_id": authorizer.ID, + "tokens": 10, + }), false) + require.NotNil(t, err, "expected error when staking tokens with insufficient balance but got output: ", strings.Join(output, "\n")) + require.Len(t, output, 1) + require.Equal(t, "stake_pool_lock_failed: stake pool digging error: lock amount is greater than balance", output[0]) + }) + + t.Run("Staking tokens against invalid node id should fail", func(t *test.SystemTest) { + createWallet(t) + + output, err := authorizerLock(t, configPath, createParams(map[string]interface{}{ + "authorizer_id": "abcdefgh", + "tokens": 1, + }), false) + require.NotNil(t, err, "expected error when staking tokens against invalid miner but got output", strings.Join(output, "\n")) + require.Len(t, output, 1) + require.Equal(t, "stake_pool_lock_failed: can't get stake pool: get_stake_pool: miner not found or genesis miner used", output[0]) + }) + + t.Run("Staking negative tokens against valid authorizer should fail", func(t *test.SystemTest) { + createWallet(t) + + output, err := authorizerLock(t, configPath, createParams(map[string]interface{}{ + "authorizer_id": authorizer.ID, + "tokens": -1, + }), false) + require.NotNil(t, err, "expected error when staking negative tokens but got output: ", strings.Join(output, "\n")) + require.Len(t, output, 1) + require.Equal(t, `invalid token amount: negative`, output[0]) + }) + + t.Run("Staking 0 tokens against authorizer should fail", func(t *test.SystemTest) { + createWallet(t) + + output, err := minerOrSharderLock(t, configPath, createParams(map[string]interface{}{ + "authorizer_id": authorizer01ID, + "tokens": 0, + }), false) + require.NotNil(t, err, "expected error when staking more tokens than max_stake but got output: ", strings.Join(output, "\n")) + require.Len(t, output, 1) + require.Equal(t, "stake_pool_lock_failed: no stake to lock: 0", output[0]) + }) + + t.Run("Unlock tokens with invalid node id should fail", func(t *test.SystemTest) { + createWallet(t) + + output, err := authorizerLock(t, configPath, createParams(map[string]interface{}{ + "authorizer_id": authorizer.ID, + "tokens": 2, + }), true) + require.Nil(t, err, "error staking tokens against a node") + require.Len(t, output, 1) + require.Regexp(t, lockOutputRegex, output[0]) + + output, err = authorizerUnlock(t, configPath, createParams(map[string]interface{}{ + "authorizer_id": "abcdefgh", + }), false) + require.NotNil(t, err, "expected error when using invalid node id") + require.Len(t, output, 1) + require.Equal(t, "stake_pool_unlock_failed: can't get related stake pool: get_stake_pool: miner not found or genesis miner used", output[0]) + + // teardown + _, err = authorizerUnlock(t, configPath, createParams(map[string]interface{}{ + "authorizer_id": authorizer.ID, + }), true) + if err != nil { + t.Log("error unlocking tokens after test: ", t.Name()) + } + }) } func listAuthorizer(t *test.SystemTest, cliConfigFilename, params string) ([]string, error) { @@ -167,6 +168,7 @@ func authorizerLock(t *test.SystemTest, cliConfigFilename, params string, retry func authorizerLockForWallet(t *test.SystemTest, cliConfigFilename, params, wallet string, retry bool) ([]string, error) { t.Log("locking tokens against authorizers...") + t.Log("params", params) if retry { return cliutils.RunCommand(t, fmt.Sprintf("./zbox sp-lock %s --silent --wallet %s_wallet.json --configDir ./config --config %s", params, wallet, cliConfigFilename), 3, time.Second) } else { @@ -186,3 +188,16 @@ func authorizerUnlockForWallet(t *test.SystemTest, cliConfigFilename, params, wa return cliutils.RunCommandWithoutRetry(fmt.Sprintf("./zbox sp-unlock %s --silent --wallet %s_wallet.json --configDir ./config --config %s", params, wallet, cliConfigFilename)) } } + +func authorizerPoolInfo(t *test.SystemTest, cliConfigFilename, params string, retry bool) ([]string, error) { + return authorizerPoolInfoForWallet(t, cliConfigFilename, params, escapedTestName(t), retry) +} + +func authorizerPoolInfoForWallet(t *test.SystemTest, cliConfigFilename, params, wallet string, retry bool) ([]string, error) { + t.Log("fetching sp-info...") + if retry { + return cliutils.RunCommand(t, fmt.Sprintf("./zbox sp-info %s --silent --wallet %s_wallet.json --configDir ./config --config %s", params, wallet, cliConfigFilename), 3, time.Second) + } else { + return cliutils.RunCommandWithoutRetry(fmt.Sprintf("./zbox sp-info %s --silent --wallet %s_wallet.json --configDir ./config --config %s", params, wallet, cliConfigFilename)) + } +} \ No newline at end of file From 18a6b4cc06fa0903db8b51e2bbae79715c99d51b Mon Sep 17 00:00:00 2001 From: Ash-KODES Date: Sun, 12 May 2024 21:19:04 +0530 Subject: [PATCH 8/8] Fail fixes --- .../zwalletcli_authorizer_stake_test.go | 174 +++++++++--------- 1 file changed, 83 insertions(+), 91 deletions(-) diff --git a/tests/cli_tests/zwalletcli_authorizer_stake_test.go b/tests/cli_tests/zwalletcli_authorizer_stake_test.go index 6fa93fa936..23f3a961bd 100644 --- a/tests/cli_tests/zwalletcli_authorizer_stake_test.go +++ b/tests/cli_tests/zwalletcli_authorizer_stake_test.go @@ -13,7 +13,7 @@ import ( climodel "github.com/0chain/system_test/internal/cli/model" cliutils "github.com/0chain/system_test/internal/cli/util" "github.com/stretchr/testify/require" - "regexp" + "regexp" ) var authorizerLockOutputRegex = regexp.MustCompile("txn hash: [a-f0-9]{64}") @@ -25,21 +25,11 @@ func TestAuthorizerStake(testSetup *testing.T) { var authorizer climodel.Node // var authorizers climodel.AutorizerNodes t.TestSetup("Get authorizer details", func() { - // if _, err := os.Stat("./config/" + authorizer01NodeDelegateWallet + "_wallet.json"); err != nil { - // t.Skipf("authorizer node owner wallet located at %s is missing", "./config/"+authorizer01NodeDelegateWallet+"_wallet.json") - // } + output, err := listAuthorizer(t, configPath, "--json") t.Log("list of authorizer ", output) require.NoError(t, err, "error listing authorizers") - // err = json.Unmarshal([]byte(output[0]), &authorizers) - // require.Nil(t, err, "error unmarshalling bridge-list-auth json output") - - // for _, authorizer = range authorizers.Nodes { - // if authorizer.ID == authorizer01ID { - // break - // } - // } authorizer.ID = authorizer01ID t.Log("authorizer", authorizer.ID) }) @@ -59,8 +49,9 @@ func TestAuthorizerStake(testSetup *testing.T) { require.Nil(t, err, "error staking tokens against a node") require.Len(t, output, 1) require.Regexp(t, authorizerLockOutputRegex, output[0]) - + // Unlock should work + require.Nil(t, err, "Error unstaking tokens") output, err = authorizerUnlock(t, configPath, createParams(map[string]interface{}{ "authorizer_id": authorizer.ID, }), true) @@ -69,88 +60,89 @@ func TestAuthorizerStake(testSetup *testing.T) { require.Equal(t, "tokens unlocked", output[0]) output, err = authorizerPoolInfo(t, configPath, createParams(map[string]interface{}{ - "id": authorizer.ID, + "authorizer_id": authorizer.ID, + "json": "", }), true) require.NotNil(t, err, "expected error when requesting unlocked pool but got output", strings.Join(output, "\n")) require.Len(t, output, 1) require.Equal(t, `resource_not_found: can't find pool stats`, output[0]) }) - t.Run("Staking tokens with insufficient balance should fail", func(t *test.SystemTest) { - _, err := executeFaucetWithTokens(t, configPath, 1.0) - require.Nil(t, err, "error executing faucet") - - output, err := authorizerLock(t, configPath, createParams(map[string]interface{}{ - "miner_id": authorizer.ID, - "tokens": 10, - }), false) - require.NotNil(t, err, "expected error when staking tokens with insufficient balance but got output: ", strings.Join(output, "\n")) - require.Len(t, output, 1) - require.Equal(t, "stake_pool_lock_failed: stake pool digging error: lock amount is greater than balance", output[0]) - }) - - t.Run("Staking tokens against invalid node id should fail", func(t *test.SystemTest) { - createWallet(t) - - output, err := authorizerLock(t, configPath, createParams(map[string]interface{}{ - "authorizer_id": "abcdefgh", - "tokens": 1, - }), false) - require.NotNil(t, err, "expected error when staking tokens against invalid miner but got output", strings.Join(output, "\n")) - require.Len(t, output, 1) - require.Equal(t, "stake_pool_lock_failed: can't get stake pool: get_stake_pool: miner not found or genesis miner used", output[0]) - }) - - t.Run("Staking negative tokens against valid authorizer should fail", func(t *test.SystemTest) { - createWallet(t) - - output, err := authorizerLock(t, configPath, createParams(map[string]interface{}{ - "authorizer_id": authorizer.ID, - "tokens": -1, - }), false) - require.NotNil(t, err, "expected error when staking negative tokens but got output: ", strings.Join(output, "\n")) - require.Len(t, output, 1) - require.Equal(t, `invalid token amount: negative`, output[0]) - }) - - t.Run("Staking 0 tokens against authorizer should fail", func(t *test.SystemTest) { - createWallet(t) - - output, err := minerOrSharderLock(t, configPath, createParams(map[string]interface{}{ - "authorizer_id": authorizer01ID, - "tokens": 0, - }), false) - require.NotNil(t, err, "expected error when staking more tokens than max_stake but got output: ", strings.Join(output, "\n")) - require.Len(t, output, 1) - require.Equal(t, "stake_pool_lock_failed: no stake to lock: 0", output[0]) - }) - - t.Run("Unlock tokens with invalid node id should fail", func(t *test.SystemTest) { - createWallet(t) - - output, err := authorizerLock(t, configPath, createParams(map[string]interface{}{ - "authorizer_id": authorizer.ID, - "tokens": 2, - }), true) - require.Nil(t, err, "error staking tokens against a node") - require.Len(t, output, 1) - require.Regexp(t, lockOutputRegex, output[0]) - - output, err = authorizerUnlock(t, configPath, createParams(map[string]interface{}{ - "authorizer_id": "abcdefgh", - }), false) - require.NotNil(t, err, "expected error when using invalid node id") - require.Len(t, output, 1) - require.Equal(t, "stake_pool_unlock_failed: can't get related stake pool: get_stake_pool: miner not found or genesis miner used", output[0]) - - // teardown - _, err = authorizerUnlock(t, configPath, createParams(map[string]interface{}{ - "authorizer_id": authorizer.ID, - }), true) - if err != nil { - t.Log("error unlocking tokens after test: ", t.Name()) - } - }) + t.Run("Staking tokens with insufficient balance should fail", func(t *test.SystemTest) { + _, err := executeFaucetWithTokens(t, configPath, 1.0) + require.Nil(t, err, "error executing faucet") + + output, err := authorizerLock(t, configPath, createParams(map[string]interface{}{ + "miner_id": authorizer.ID, + "tokens": 10, + }), false) + require.NotNil(t, err, "expected error when staking tokens with insufficient balance but got output: ", strings.Join(output, "\n")) + require.Len(t, output, 1) + require.Equal(t, "stake_pool_lock_failed: stake pool digging error: lock amount is greater than balance", output[0]) + }) + + t.Run("Staking tokens against invalid node id should fail", func(t *test.SystemTest) { + createWallet(t) + + output, err := authorizerLock(t, configPath, createParams(map[string]interface{}{ + "authorizer_id": "abcdefgh", + "tokens": 1, + }), false) + require.NotNil(t, err, "expected error when staking tokens against invalid authorizer but got output", strings.Join(output, "\n")) + require.Len(t, output, 1) + require.Equal(t, "stake_pool_lock_failed: can't get stake pool: get_stake_pool: authorizer not found or genesis authorizer used", output[0]) + }) + + t.Run("Staking negative tokens against valid authorizer should fail", func(t *test.SystemTest) { + createWallet(t) + + output, err := authorizerLock(t, configPath, createParams(map[string]interface{}{ + "authorizer_id": authorizer.ID, + "tokens": -1, + }), false) + require.NotNil(t, err, "expected error when staking negative tokens but got output: ", strings.Join(output, "\n")) + require.Len(t, output, 1) + require.Equal(t, `invalid token amount: negative`, output[0]) + }) + + t.Run("Staking 0 tokens against authorizer should fail", func(t *test.SystemTest) { + createWallet(t) + + output, err := authorizerLock(t, configPath, createParams(map[string]interface{}{ + "authorizer_id": authorizer01ID, + "tokens": 0, + }), false) + require.NotNil(t, err, "expected error when staking more tokens than max_stake but got output: ", strings.Join(output, "\n")) + require.Len(t, output, 1) + require.Equal(t, "stake_pool_lock_failed: no stake to lock: 0", output[0]) + }) + + t.Run("Locking unlocking tokens with invalid node id should fail", func(t *test.SystemTest) { + createWallet(t) + + output, err := authorizerLock(t, configPath, createParams(map[string]interface{}{ + "authorizer_id": authorizer.ID, + "tokens": 2, + }), true) + require.Nil(t, err, "error staking tokens against a node") + require.Len(t, output, 1) + require.Regexp(t, authorizerLockOutputRegex, output[0]) + + output, err = authorizerUnlock(t, configPath, createParams(map[string]interface{}{ + "authorizer_id": "abcdefgh", + }), false) + require.NotNil(t, err, "expected error when using invalid node id") + require.Len(t, output, 1) + require.Equal(t, "stake_pool_unlock_failed: can't get related stake pool: get_stake_pool: miner not found or genesis miner used", output[0]) + + // teardown + _, err = authorizerUnlock(t, configPath, createParams(map[string]interface{}{ + "authorizer_id": authorizer.ID, + }), true) + if err != nil { + t.Log("error unlocking tokens after test: ", t.Name()) + } + }) } func listAuthorizer(t *test.SystemTest, cliConfigFilename, params string) ([]string, error) { @@ -183,7 +175,7 @@ func authorizerUnlock(t *test.SystemTest, cliConfigFilename, params string, retr func authorizerUnlockForWallet(t *test.SystemTest, cliConfigFilename, params, wallet string, retry bool) ([]string, error) { t.Log("unlocking tokens from authorizer pool...") if retry { - return cliutils.RunCommand(t, fmt.Sprintf("./zbox sp-unlock %s --silent --wallet %s_wallet.json --configDir ./config --config %s", params, wallet, cliConfigFilename), 3, time.Second) + return cliutils.RunCommand(t, fmt.Sprintf("./zbox sp-unlock %s --silent --wallet %s_wallet.json --configDir ./config --config %s", params, wallet, cliConfigFilename), 3, time.Second*2) } else { return cliutils.RunCommandWithoutRetry(fmt.Sprintf("./zbox sp-unlock %s --silent --wallet %s_wallet.json --configDir ./config --config %s", params, wallet, cliConfigFilename)) } @@ -200,4 +192,4 @@ func authorizerPoolInfoForWallet(t *test.SystemTest, cliConfigFilename, params, } else { return cliutils.RunCommandWithoutRetry(fmt.Sprintf("./zbox sp-info %s --silent --wallet %s_wallet.json --configDir ./config --config %s", params, wallet, cliConfigFilename)) } -} \ No newline at end of file +}