-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathzwalletcli_miner_stake_test.go
378 lines (313 loc) · 14.1 KB
/
zwalletcli_miner_stake_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
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"
)
var lockOutputRegex = regexp.MustCompile("locked with: [a-f0-9]{64}")
func TestMinerStake(testSetup *testing.T) {
t := test.NewSystemTest(testSetup)
t.SetSmokeTests("Staking tokens against valid miner with valid tokens should work")
var miner climodel.Node
var miners climodel.MinerSCNodes
t.TestSetup("Get miner details", func() {
if _, err := os.Stat("./config/" + miner01NodeDelegateWalletName + "_wallet.json"); err != nil {
t.Skipf("miner node owner wallet located at %s is missing", "./config/"+miner01NodeDelegateWalletName+"_wallet.json")
}
output, err := listMiners(t, configPath, "--json")
require.NoError(t, err, "error listing miners")
require.Len(t, output, 1)
err = json.Unmarshal([]byte(output[0]), &miners)
require.Nil(t, err, "error unmarshalling ls-miners json output")
for _, miner = range miners.Nodes {
if miner.ID == miner01ID {
break
}
}
})
t.Parallel()
t.RunWithTimeout("Staking tokens against valid miner 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, 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])
})
t.RunWithTimeout("Multiple stakes against a miner should add balance to client's stake pool", 5*time.Minute, func(t *test.SystemTest) {
createWallet(t)
var poolsInfoBefore climodel.MinerSCUserPoolsInfo
output, err := stakePoolsInMinerSCInfo(t, configPath, "", true)
require.Nil(t, err, "error fetching Miner SC User pools")
require.Len(t, output, 1)
err = json.Unmarshal([]byte(output[0]), &poolsInfoBefore)
require.Nil(t, err, "error unmarshalling Miner SC user pool info")
output, err = minerOrSharderLock(t, configPath, createParams(map[string]interface{}{
"miner_id": miner.ID,
"tokens": 2,
}), true)
require.Nil(t, err,
"error staking tokens against node")
require.Len(t, output, 1)
require.Regexp(t, regexp.MustCompile("locked with: [a-z0-9]{64}"), output[0])
// wait for pool to be active from pending status, usually need to wait for 50 rounds
waitForStakePoolActive(t)
output, err = minerOrSharderLock(t, configPath, createParams(map[string]interface{}{
"miner_id": miner.ID,
"tokens": 2,
}), true)
require.Nil(t, err, "error staking tokens against node")
require.Len(t, output, 1)
require.Regexp(t, regexp.MustCompile("locked with: [a-z0-9]{64}"), output[0])
var poolsInfo climodel.MinerSCUserPoolsInfo
output, err = stakePoolsInMinerSCInfo(t, configPath, "", true)
require.Nil(t, err, "error fetching Miner SC User pools")
require.Len(t, output, 1)
err = json.Unmarshal([]byte(output[0]), &poolsInfo)
require.NoError(t, err)
require.Len(t, poolsInfo.Pools[miner.ID], 1)
})
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 := minerOrSharderLock(t, configPath, createParams(map[string]interface{}{
"miner_id": miner.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])
})
// this case covers both invalid miner and sharder id, so is not repeated in zwalletcli_sharder_stake_test.go
t.Run("Staking tokens against invalid node id should fail", func(t *test.SystemTest) {
createWallet(t)
output, err := minerOrSharderLock(t, configPath, createParams(map[string]interface{}{
"miner_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 miner should fail", func(t *test.SystemTest) {
createWallet(t)
output, err := minerOrSharderLock(t, configPath, createParams(map[string]interface{}{
"miner_id": miner.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])
})
// todo rewards not transferred to wallet until a collect reward transaction
t.RunSequentially("Staking tokens against miner should return interest to wallet", func(t *test.SystemTest) {
createWallet(t)
wallet, err := getWallet(t, configPath)
require.Nil(t, err, "error getting wallet")
output, err := minerOrSharderLock(t, configPath, createParams(map[string]interface{}{
"miner_id": miner.ID,
"tokens": 1,
}), 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)
balance := getBalanceFromSharders(t, wallet.ClientID)
require.GreaterOrEqual(t, balance, poolsInfo.Reward)
// teardown
_, err = minerOrSharderUnlock(t, configPath, createParams(map[string]interface{}{
"miner_id": miner.ID,
}), true)
if err != nil {
t.Log("error unlocking tokens after test: ", t.Name())
}
})
t.Run("Making more pools than allowed by max_delegates in minersc should fail", func(t *test.SystemTest) {
var newMiner climodel.Node // Choose a different miner so it has 0 pools
for _, newMiner = range miners.Nodes {
if newMiner.ID == miner02ID {
break
}
}
createWallet(t)
output, err := getMinerSCConfig(t, configPath, true)
require.Nil(t, err, strings.Join(output, "\n"))
require.Greater(t, len(output), 0, strings.Join(output, "\n"))
cfg, _ := keyValuePairStringToMap(output)
maxDelegates, err := strconv.ParseInt(cfg["max_delegates"], 10, 0)
require.Nil(t, err)
wg := &sync.WaitGroup{}
for i := 0; i < int(maxDelegates); i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
walletName := escapedTestName(t) + fmt.Sprintf("%d", i)
createWalletForName(walletName)
output, err = minerOrSharderLockForWallet(t, configPath, createParams(map[string]interface{}{
"miner_id": newMiner.ID,
"tokens": 1,
}), walletName, true)
require.NoError(t, err)
require.Len(t, output, 1)
require.Regexp(t, lockOutputRegex, output[0])
}(i)
}
wg.Wait()
require.NotEqual(t, 0, newMiner.Settings.MaxNumDelegates)
output, err = minerOrSharderLock(t, configPath, createParams(map[string]interface{}{
"miner_id": newMiner.ID,
"tokens": 9.0,
}), false)
require.NotNil(t, err, "expected error when making more pools than max_delegates but got output: ", strings.Join(output, "\n"))
require.Len(t, output, 1)
require.Equal(t, fmt.Sprintf("stake_pool_lock_failed: max_delegates reached: %d, no more stake pools allowed", maxDelegates), output[0])
})
t.Run("Staking 0 tokens against miner should fail", func(t *test.SystemTest) {
createWallet(t)
output, err := minerOrSharderLock(t, configPath, createParams(map[string]interface{}{
"miner_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"))
require.Len(t, output, 1)
require.Equal(t, "stake_pool_lock_failed: no stake to lock: 0", output[0])
})
// this case covers both invalid miner and sharder id, so is not repeated in zwalletcli_sharder_stake_test.go
t.Run("Unlock tokens with invalid node id should fail", func(t *test.SystemTest) {
createWallet(t)
output, err := minerOrSharderLock(t, configPath, createParams(map[string]interface{}{
"miner_id": miner.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 = minerOrSharderUnlock(t, configPath, createParams(map[string]interface{}{
"miner_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 = minerOrSharderUnlock(t, configPath, createParams(map[string]interface{}{
"miner_id": miner.ID,
}), true)
if err != nil {
t.Log("error unlocking tokens after test: ", t.Name())
}
})
}
func pollForPoolInfo(t *test.SystemTest, minerID string) (climodel.DelegatePool, error) {
t.Log(`polling for pool info till it is "ACTIVE"...`)
timeout := time.After(time.Minute * 5)
time.Sleep(10 * time.Second)
var poolsInfo climodel.DelegatePool
for {
output, err := minerSharderPoolInfo(t, configPath, createParams(map[string]interface{}{
"id": minerID,
}), true)
require.Nil(t, err, "error fetching Miner Sharder pools")
require.Len(t, output, 1)
err = json.Unmarshal([]byte(output[0]), &poolsInfo)
require.Nil(t, err, "error unmarshalling Miner Sharder pools")
require.NotEmpty(t, poolsInfo)
if poolsInfo.Status == int(climodel.Active) {
return poolsInfo, nil
}
select {
case <-timeout:
return climodel.DelegatePool{}, errors.New("Pool status did not change to active")
default:
cliutils.Wait(t, time.Second*15)
}
}
}
func minerSharderPoolInfo(t *test.SystemTest, cliConfigFilename, params string, retry bool) ([]string, error) {
return minerSharderPoolInfoForWallet(t, cliConfigFilename, params, escapedTestName(t), retry)
}
func minerSharderPoolInfoForWallet(t *test.SystemTest, cliConfigFilename, params, wallet string, retry bool) ([]string, error) {
t.Log("fetching mn-pool-info...")
if retry {
return cliutils.RunCommand(t, fmt.Sprintf("./zwallet mn-pool-info %s --silent --wallet %s_wallet.json --configDir ./config --config %s", params, wallet, cliConfigFilename), 3, time.Second)
} else {
return cliutils.RunCommandWithoutRetry(fmt.Sprintf("./zwallet mn-pool-info %s --silent --wallet %s_wallet.json --configDir ./config --config %s", params, wallet, cliConfigFilename))
}
}
func getBalanceFromSharders(t *test.SystemTest, clientId string) int64 {
output, err := getSharders(t, configPath)
require.Nil(t, err, "get sharders failed", strings.Join(output, "\n"))
require.Greater(t, len(output), 1)
require.Equal(t, "MagicBlock Sharders", output[0])
var sharders map[string]*climodel.Sharder
err = json.Unmarshal([]byte(strings.Join(output[1:], "")), &sharders)
require.Nil(t, err, "Error deserializing JSON string `%s`: %v", strings.Join(output[1:], "\n"), err)
require.NotEmpty(t, sharders, "No sharders found: %v", strings.Join(output[1:], "\n"))
// Get base URL for API calls.
sharderBaseURLs := getAllSharderBaseURLs(sharders)
res, err := apiGetBalance(t, sharderBaseURLs[0], clientId)
require.Nil(t, err, "error getting balance")
if res.StatusCode == 400 {
return 0
}
require.True(t, res.StatusCode >= 200 && res.StatusCode < 300, "Failed API request to get balance")
require.NotNil(t, res.Body, "Balance API response must not be nil")
resBody, err := io.ReadAll(res.Body)
require.Nil(t, err, "Error reading response body")
var startBalance climodel.Balance
err = json.Unmarshal(resBody, &startBalance)
require.Nil(t, err, "Error deserializing JSON string `%s`: %v", string(resBody), err)
return startBalance.Balance
}
func minerOrSharderLock(t *test.SystemTest, cliConfigFilename, params string, retry bool) ([]string, error) {
return minerOrSharderLockForWallet(t, cliConfigFilename, params, escapedTestName(t), retry)
}
func minerOrSharderLockForWallet(t *test.SystemTest, cliConfigFilename, params, wallet string, retry bool) ([]string, error) {
t.Log("locking tokens against miner/sharder...")
if retry {
return cliutils.RunCommand(t, fmt.Sprintf("./zwallet mn-lock %s --silent --wallet %s_wallet.json --configDir ./config --config %s", params, wallet, cliConfigFilename), 3, time.Second)
} else {
return cliutils.RunCommandWithoutRetry(fmt.Sprintf("./zwallet mn-lock %s --silent --wallet %s_wallet.json --configDir ./config --config %s", params, wallet, cliConfigFilename))
}
}
func minerOrSharderUnlock(t *test.SystemTest, cliConfigFilename, params string, retry bool) ([]string, error) {
return minerOrSharderUnlockForWallet(t, cliConfigFilename, params, escapedTestName(t), retry)
}
func minerOrSharderUnlockForWallet(t *test.SystemTest, cliConfigFilename, params, wallet string, retry bool) ([]string, error) {
t.Log("unlocking tokens from miner/sharder pool...")
if retry {
return cliutils.RunCommand(t, fmt.Sprintf("./zwallet mn-unlock %s --silent --wallet %s_wallet.json --configDir ./config --config %s", params, wallet, cliConfigFilename), 3, time.Second)
} else {
return cliutils.RunCommandWithoutRetry(fmt.Sprintf("./zwallet mn-unlock %s --silent --wallet %s_wallet.json --configDir ./config --config %s", params, wallet, cliConfigFilename))
}
}