-
Notifications
You must be signed in to change notification settings - Fork 19
/
updateallocation.go
94 lines (79 loc) · 2.69 KB
/
updateallocation.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
package cmd
import (
"log"
"time"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/0chain/gosdk/zcncore"
"github.com/spf13/cobra"
)
// updateAllocationCmd used to change allocation size and expiration
var updateAllocationCmd = &cobra.Command{
Use: "updateallocation",
Short: "Updates allocation's expiry and size",
Long: `Updates allocation's expiry and size`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
var flags = cmd.Flags()
if flags.Changed("allocation") == false {
log.Fatal("Error: allocation flag is missing")
}
allocID, err := flags.GetString("allocation")
if err != nil {
log.Fatal("invalid 'allocation_id' flag: ", err)
}
if flags.Changed("free_storage") {
lock, freeStorageMarker := processFreeStorageFlags(flags)
txnHash, err := sdk.CreateFreeUpdateAllocation(freeStorageMarker, allocID, lock)
if err != nil {
log.Fatal("Error free update allocation: ", err)
}
log.Print("Allocation updated with txId : " + txnHash)
return
}
var updateTerms = false
if flags.Changed("update_terms") {
updateTerms, err = flags.GetBool("update_terms")
if err != nil {
log.Fatal("invalid update terms entry: ", err)
}
}
var lockf float64
var lock int64
if lockf, err = flags.GetFloat64("lock"); err != nil {
log.Fatal("error: invalid 'lock' value:", err)
}
lock = zcncore.ConvertToValue(lockf)
size, err := flags.GetInt64("size")
if err != nil {
log.Fatal("invalid 'size' flag: ", err)
}
expiry, err := flags.GetDuration("expiry")
if err != nil {
log.Fatal("invalid 'expiry' flag: ", err)
}
setImmutable, _ := cmd.Flags().GetBool("set_immutable")
txnHash, err := sdk.UpdateAllocation(size,
int64(expiry/time.Second), allocID, lock, setImmutable, updateTerms)
if err != nil {
log.Fatal("Error updating allocation:", err)
}
log.Print("Allocation updated with txId : " + txnHash)
},
}
func init() {
rootCmd.AddCommand(updateAllocationCmd)
updateAllocationCmd.PersistentFlags().String("allocation", "",
"Allocation ID")
updateAllocationCmd.PersistentFlags().Float64("lock", 0.0,
"lock write pool with given number of tokens, required")
updateAllocationCmd.PersistentFlags().Int64("size", 0,
"adjust allocation size, bytes")
updateAllocationCmd.PersistentFlags().Duration("expiry", 0,
"adjust storage expiration time, duration")
updateAllocationCmd.Flags().Bool("set_immutable", false, "set the allocation's data to be immutable")
updateAllocationCmd.Flags().String("free_storage", "",
"json file containing marker for free storage")
updateAllocationCmd.Flags().Bool("update_terms", false,
"update blobber terms")
updateAllocationCmd.MarkFlagRequired("allocation")
}