-
Notifications
You must be signed in to change notification settings - Fork 23
/
readpool.go
216 lines (183 loc) · 5.39 KB
/
readpool.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
package cmd
import (
"fmt"
"log"
"time"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/0chain/zboxcli/util"
"github.com/0chain/gosdk/zcncore"
"github.com/spf13/cobra"
)
// rpCreate creates read pool
var rpCreate = &cobra.Command{
Use: "rp-create",
Short: "Create read pool if missing",
Long: `Create read pool in storage SC if the pool is missing.`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
var err error
if err = sdk.CreateReadPool(); err != nil {
log.Fatalf("Failed to create read pool: %v\n", err)
}
fmt.Println("Read pool created successfully")
},
}
func printReadPoolStat(stat []*sdk.AllocationPoolStat) {
for _, st := range stat {
fmt.Println("- id: ", st.ID)
fmt.Println(" balance: ", st.Balance.String())
fmt.Println(" expire_at: ", st.ExpireAt.ToTime().String())
fmt.Println(" allocation_id: ", st.AllocationID)
fmt.Println(" locked: ", st.Locked)
fmt.Println(" blobbers:")
for _, b := range st.Blobbers {
fmt.Println(" - blobber_id: ", b.BlobberID)
fmt.Println(" - balance: ", b.Balance.String())
}
}
fmt.Println()
}
// rpInfo information
var rpInfo = &cobra.Command{
Use: "rp-info",
Short: "Read pool information.",
Long: `Read pool information.`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
var (
flags = cmd.Flags()
allocID string
err error
)
if flags.Changed("allocation") {
if allocID, err = flags.GetString("allocation"); err != nil {
log.Fatalf("can't get 'allocation' flag: %v", err)
}
}
doJSON, _ := cmd.Flags().GetBool("json")
var info *sdk.AllocationPoolStats
if info, err = sdk.GetReadPoolInfo(""); err != nil {
log.Fatalf("Failed to get read pool info: %v", err)
}
if len(info.Pools) == 0 {
fmt.Println("no tokens locked")
return
}
info.AllocFilter(allocID)
if doJSON {
util.PrintJSON(info.Pools)
return
}
printReadPoolStat(info.Pools)
},
}
// rpLock locks tokens in read pool
var rpLock = &cobra.Command{
Use: "rp-lock",
Short: "Lock some tokens in read pool.",
Long: `Lock some tokens in read pool.`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
var (
flags = cmd.Flags()
duration time.Duration
allocID string // required
blobberID string // optional
tokens float64
fee float64
err error
)
if !flags.Changed("duration") {
log.Fatal("missing required 'duration' flag")
}
if !flags.Changed("allocation") {
log.Fatal("missing required 'allocation' flag")
}
if !flags.Changed("tokens") {
log.Fatal("missing required 'tokens' flag")
}
if duration, err = flags.GetDuration("duration"); err != nil {
log.Fatal("invalid 'duration' flag: ", err)
}
if allocID, err = flags.GetString("allocation"); err != nil {
log.Fatal("invalid 'allocation' flag: ", err)
}
if flags.Changed("blobber") {
if blobberID, err = flags.GetString("blobber"); err != nil {
log.Fatal("invalid 'blobber' flag: ", err)
}
}
if tokens, err = flags.GetFloat64("tokens"); err != nil {
log.Fatal("invalid 'tokens' flag: ", err)
}
if flags.Changed("fee") {
if fee, err = flags.GetFloat64("fee"); err != nil {
log.Fatal("invalid 'fee' flag: ", err)
}
}
err = sdk.ReadPoolLock(duration, allocID, blobberID,
zcncore.ConvertToValue(tokens), zcncore.ConvertToValue(fee))
if err != nil {
log.Fatalf("Failed to lock tokens in read pool: %v", err)
}
fmt.Println("locked")
},
}
// rpUnlock unlocks tokens in a read pool
var rpUnlock = &cobra.Command{
Use: "rp-unlock",
Short: "Unlock some expired tokens in a read pool.",
Long: `Unlock some expired tokens in a read pool.`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
var (
flags = cmd.Flags()
poolID string
fee float64
err error
)
if !flags.Changed("pool_id") {
log.Fatal("missing required 'pool_id' flag")
}
if poolID, err = flags.GetString("pool_id"); err != nil {
log.Fatal("invalid 'pool_id' flag: ", err)
}
if flags.Changed("fee") {
if fee, err = flags.GetFloat64("fee"); err != nil {
log.Fatal("invalid 'fee' flag: ", err)
}
}
err = sdk.ReadPoolUnlock(poolID, zcncore.ConvertToValue(fee))
if err != nil {
log.Fatalf("Failed to unlock tokens in read pool: %v", err)
}
fmt.Println("unlocked")
},
}
func init() {
rootCmd.AddCommand(rpCreate)
rootCmd.AddCommand(rpInfo)
rootCmd.AddCommand(rpLock)
rootCmd.AddCommand(rpUnlock)
rpInfo.PersistentFlags().String("allocation", "",
"allocation id, optional")
rpInfo.Flags().Bool("json", false, "pass this option to print response as json data")
rpLock.PersistentFlags().Duration("duration", 0,
"lock duration, required")
rpLock.PersistentFlags().String("allocation", "",
"allocation id to lock for, required")
rpLock.PersistentFlags().String("blobber", "",
"blobber id to lock for, optional")
rpLock.PersistentFlags().Float64("tokens", 0.0,
"lock tokens number, required")
rpLock.PersistentFlags().Float64("fee", 0.0,
"transaction fee, default 0")
rpLock.MarkFlagRequired("duration")
rpLock.MarkFlagRequired("allocation")
rpLock.MarkFlagRequired("tokens")
rpUnlock.PersistentFlags().String("pool_id", "",
"expired read pool identifier, required")
rpUnlock.PersistentFlags().Float64("fee", 0.0,
"transaction fee, default 0")
rpUnlock.MarkFlagRequired("pool_id")
}