/
get_upload_cost.go
139 lines (111 loc) · 3.42 KB
/
get_upload_cost.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
package cmd
import (
"fmt"
"log"
"os"
"time"
"github.com/0chain/gosdk/core/common"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/spf13/cobra"
)
// The getUploadCostCmd returns value in tokens to upload a file.
var getUploadCostCmd = &cobra.Command{
Use: "get-upload-cost",
Short: "Get uploading cost",
Long: `Get uploading cost`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
var (
fflags = cmd.Flags()
allocID string
duration time.Duration
end bool
err error
)
if !fflags.Changed("allocation") {
log.Fatal("missing required 'allocation' flag")
}
allocID = cmd.Flag("allocation").Value.String()
var localPath string
if !fflags.Changed("localpath") {
log.Fatal("missing requried 'localpath' flag")
}
if localPath, err = fflags.GetString("localpath"); err != nil {
log.Fatal("invalid 'localpath' flag: ", err)
}
if localPath == "" {
log.Fatal("empty local path")
}
var fi os.FileInfo
if fi, err = os.Stat(localPath); err != nil {
log.Fatal(err)
}
if !fi.Mode().IsRegular() {
log.Fatal("not a regular file")
}
if duration, err = fflags.GetDuration("duration"); err != nil {
log.Fatal("invalid 'duration' flag:", err)
} else if duration < 0 {
log.Fatal("negative duration not allowed: ", duration)
}
if end, err = fflags.GetBool("end"); err != nil {
log.Fatal("invalid 'end' flag:", err)
}
var alloc *sdk.Allocation
if alloc, err = sdk.GetAllocation(allocID); err != nil {
log.Fatal("fetching the allocation: ", err)
}
// until allocation ends
if end {
var expiry = time.Unix(alloc.Expiration, 0)
duration = time.Until(expiry)
}
uploadCost(alloc, fi.Size(), localPath, duration)
},
}
// The uploadCost for a size and duration (if given). If the duration is zero
// of less, then it returns upload cost until allocation ends.
func uploadCost(alloc *sdk.Allocation, size int64, path string,
duration time.Duration) {
var cost common.Balance // total price for size / duration
for _, d := range alloc.BlobberDetails {
cost += uploadCostForBlobber(float64(d.Terms.WritePrice), size,
alloc.DataShards)
}
switch {
case duration == 0:
fmt.Printf("%s tokens / %s for %s of %s",
cost, alloc.TimeUnit, common.Size(size), path)
case duration < 0:
fmt.Println("allocation expired, 'end' flag can't be used")
return
default:
var dtu = float64(duration) / float64(alloc.TimeUnit)
cost = common.Balance(float64(cost) * dtu)
fmt.Printf("%s tokens / %s for %s of %s",
cost, duration, common.Size(size), path)
}
fmt.Println()
}
func uploadCostForBlobber(price float64, size int64, data int) (
cost common.Balance) {
var ps = (size + int64(data) - 1) / int64(data)
return common.Balance(price * sizeInGB(ps))
}
func uploadCostFor1GB(alloc *sdk.Allocation) (cost common.Balance) {
for _, d := range alloc.BlobberDetails {
cost += uploadCostForBlobber(float64(d.Terms.WritePrice), 1*GB,
alloc.DataShards)
}
return
}
func init() {
rootCmd.AddCommand(getUploadCostCmd)
ucpf := getUploadCostCmd.PersistentFlags()
ucpf.String("allocation", "", "allocation ID, required")
ucpf.String("localpath", "", "local file path, required")
ucpf.Duration("duration", 0, "expected duration keep uploaded file")
ucpf.Bool("end", false, "(default false) use the duration until allocation ends")
getUploadCostCmd.MarkFlagRequired("allocation")
getUploadCostCmd.MarkFlagRequired("localpath")
}