-
Notifications
You must be signed in to change notification settings - Fork 23
/
get_download_cost.go
159 lines (123 loc) · 4 KB
/
get_download_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package cmd
import (
"fmt"
"log"
"github.com/0chain/gosdk/core/common"
"github.com/0chain/gosdk/zboxcore/fileref"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/spf13/cobra"
)
func calculateDownloadCost(alloc *sdk.Allocation, fileSize int64, numBlocks int64) common.Balance {
var cost float64
for _, d := range alloc.BlobberDetails {
readPrice, err := d.Terms.ReadPrice.ToToken()
if err != nil {
log.Fatalf("failed to convert %v to token, %v", d.Terms.ReadPrice, err)
}
cost += sizeInGB(numBlocks*fileref.CHUNK_SIZE) * float64(readPrice)
}
balance, err := common.ToBalance(cost)
if err != nil {
log.Fatalf("failed to convert %v to balance, %v", cost, err)
}
return balance
}
func downloadCost(alloc *sdk.Allocation, meta *sdk.ConsolidatedFileMeta, blocksPerMarker int) {
if meta.Type != fileref.FILE {
log.Fatal("not a file")
}
shardSize := (meta.ActualFileSize + int64(alloc.DataShards) - 1) / int64(alloc.DataShards)
numBlocks := (shardSize + fileref.CHUNK_SIZE - 1) / fileref.CHUNK_SIZE
requiredBalance := calculateDownloadCost(alloc, meta.ActualFileSize, numBlocks)
fmt.Printf("%s tokens for %d 64KB blocks (%s) of %s\n", requiredBalance, numBlocks*int64(alloc.DataShards+alloc.ParityShards),
common.Size(meta.Size), meta.Path)
}
// The getDownloadCostCmd returns value in tokens to download a file.
var getDownloadCostCmd = &cobra.Command{
Use: "get-download-cost",
Short: "Get downloading cost",
Long: `Get downloading cost`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
var (
fflags = cmd.Flags()
allocID string
err error
)
if !fflags.Changed("allocation") {
log.Fatal("missing required 'allocation' flag")
}
allocID = cmd.Flag("allocation").Value.String()
blocksPerMarker, err := cmd.Flags().GetInt("blocks-per-marker")
if err != nil {
log.Fatal("invalid blocks-per-marker value: ", err)
}
if blocksPerMarker <= 0 {
log.Fatal("blocks-per-marker value cannot be <= 0")
}
var (
remotePath string
authTicket string
lookupHash string
)
if fflags.Changed("remotepath") {
if remotePath, err = fflags.GetString("remotepath"); err != nil {
log.Fatal("invalid 'remotepath' flag: ", err)
}
}
if fflags.Changed("authticket") {
if authTicket, err = fflags.GetString("authticket"); err != nil {
log.Fatal("invalid 'authticket' flag: ", err)
}
}
if fflags.Changed("lookuphash") {
if lookupHash, err = fflags.GetString("lookuphash"); err != nil {
log.Fatal("invalid 'lookuphash' flag: ", err)
}
}
if remotePath == "" && authTicket == "" {
log.Fatal("'remotepath' or 'authticket' flag required")
}
var (
alloc *sdk.Allocation
meta *sdk.ConsolidatedFileMeta
)
if remotePath != "" {
// by remote path
if alloc, err = sdk.GetAllocation(allocID); err != nil {
log.Fatal("fetching the allocation: ", err)
}
if meta, err = alloc.GetFileMeta(remotePath); err != nil {
log.Fatal("can't get file meta: ", err)
}
downloadCost(alloc, meta, blocksPerMarker)
return
}
// by authentication ticket
alloc, err = sdk.GetAllocationFromAuthTicket(authTicket)
if err != nil {
log.Fatal("can't get allocation object: ", err)
}
var at = sdk.InitAuthTicket(authTicket)
if lookupHash == "" {
if lookupHash, err = at.GetLookupHash(); err != nil {
log.Fatal("can't get lookup hash from auth ticket: ", err)
}
}
meta, err = alloc.GetFileMetaFromAuthTicket(authTicket, lookupHash)
if err != nil {
log.Fatal("can't get file meta: ", err)
}
downloadCost(alloc, meta, blocksPerMarker)
},
}
func init() {
rootCmd.AddCommand(getDownloadCostCmd)
dcpf := getDownloadCostCmd.PersistentFlags()
dcpf.String("allocation", "", "allocation ID, required")
dcpf.String("remotepath", "", "remote path of file")
dcpf.Int("blocks-per-marker", 10, "blocks signed per Read Marker")
dcpf.String("authticket", "", "authticket")
dcpf.String("lookuphash", "", "lookuphash, for the remote file")
getDownloadCostCmd.MarkFlagRequired("allocation")
}