-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathblobber_slash_penalty_test.go
144 lines (111 loc) · 4.74 KB
/
blobber_slash_penalty_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
package tokenomics_tests
import (
"encoding/json"
"fmt"
"path/filepath"
"strings"
"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/0chain/system_test/tests/tokenomics_tests/utils"
"github.com/stretchr/testify/require"
)
func TestBlobberSlashPenalty(testSetup *testing.T) {
t := test.NewSystemTest(testSetup)
t.TestSetup("set storage config to use time_unit as 10 minutes", func() {
output, err := utils.UpdateStorageSCConfig(t, scOwnerWallet, map[string]string{
"time_unit": "20m",
}, true)
require.Nil(t, err, strings.Join(output, "\n"))
})
t.Cleanup(func() {
output, err := utils.UpdateStorageSCConfig(t, scOwnerWallet, map[string]string{
"time_unit": "1h",
}, true)
require.Nil(t, err, strings.Join(output, "\n"))
})
prevBlock := utils.GetLatestFinalizedBlock(t)
t.Log("prevBlock", prevBlock)
output, err := utils.CreateWallet(t, configPath)
require.Nil(t, err, "Error registering wallet", strings.Join(output, "\n"))
var blobberList []climodel.BlobberInfo
output, err = utils.ListBlobbers(t, configPath, "--json")
require.Nil(t, err, "Error listing blobbers", strings.Join(output, "\n"))
require.Len(t, output, 1)
err = json.Unmarshal([]byte(output[0]), &blobberList)
require.Nil(t, err, "Error unmarshalling blobber list", strings.Join(output, "\n"))
require.True(t, len(blobberList) > 0, "No blobbers found in blobber list")
var blobberListString []string
for _, blobber := range blobberList {
blobberListString = append(blobberListString, blobber.Id)
}
var validatorList []climodel.Validator
output, err = utils.ListValidators(t, configPath, "--json")
require.Nil(t, err, "Error listing validators", strings.Join(output, "\n"))
require.Len(t, output, 1)
err = json.Unmarshal([]byte(output[0]), &validatorList)
require.Nil(t, err, "Error unmarshalling validator list", strings.Join(output, "\n"))
require.True(t, len(validatorList) > 0, "No validators found in validator list")
var validatorListString []string
for _, validator := range validatorList {
validatorListString = append(validatorListString, validator.ID)
}
t.RunSequentiallyWithTimeout("Upload 10% of allocation and Kill blobber in the middle, One blobber should get approx double rewards than other", 1*time.Hour, func(t *test.SystemTest) {
stakeTokensToBlobbersAndValidators(t, blobberListString, validatorListString, configPath, []float64{
1, 1, 1, 1,
}, 1)
output, err := utils.CreateWallet(t, configPath)
require.Nil(t, err, "error registering wallet", strings.Join(output, "\n"))
// 1. Create an allocation with 1 data shard and 1 parity shard.
allocationId := utils.SetupAllocation(t, configPath, map[string]interface{}{
"size": 1 * GB,
"tokens": 1,
"data": 1,
"parity": 1,
})
remotepath := "/dir/"
filesize := 0.1 * GB
filename := utils.GenerateRandomTestFileName(t)
err = utils.CreateFileWithSize(filename, int64(filesize))
require.Nil(t, err)
output, err = utils.UploadFile(t, configPath, map[string]interface{}{
"allocation": allocationId,
"remotepath": remotepath + filepath.Base(filename),
"localpath": filename,
}, true)
require.Nil(t, err, "error uploading file", strings.Join(output, "\n"))
// check allocation remaining time
allocation := utils.GetAllocation(t, allocationId)
remainingTime := allocation.ExpirationDate - time.Now().Unix()
// sleep for half of the remaining time
time.Sleep(time.Duration(remainingTime/3) * time.Second)
// 2. Kill a blobber
_, err = killBlobber(t, configPath, utils.CreateParams(map[string]interface{}{
"id": blobberList[1].Id,
}), true)
require.Nil(t, err, "error killing blobber", strings.Join(output, "\n"))
// 3. Sleep for the remaining time
time.Sleep(time.Duration(remainingTime) * time.Second)
allocation = utils.GetAllocation(t, allocationId)
t.Log(allocation.MovedToChallenge)
blobberRewards := getAllocationChallengeRewards(t, allocationId)
t.Log(blobberRewards)
blobber1Reward := blobberRewards[blobberList[0].Id].(float64)
blobber2Reward := blobberRewards[blobberList[1].Id].(float64)
t.Log(blobber1Reward, blobber2Reward)
require.Greater(t, blobber1Reward/blobber2Reward, 2.0, "Killed blobber should get approx half the rewards than other")
})
}
func killBlobber(t *test.SystemTest, cliConfigFilename, params string, retry bool) ([]string, error) {
t.Log("kill blobber...")
cmd := fmt.Sprintf("./zbox kill-blobber %s --silent --wallet %s_wallet.json --configDir ./config --config %s",
params, scOwnerWallet, cliConfigFilename)
t.Log(cmd)
if retry {
return cliutils.RunCommand(t, cmd, 3, time.Second*2)
} else {
return cliutils.RunCommandWithoutRetry(cmd)
}
}