From 0f5ff6a5e11c96cfb59dc28800506f312b768703 Mon Sep 17 00:00:00 2001 From: zorro30 Date: Sat, 27 Jan 2024 20:30:02 +0530 Subject: [PATCH 01/11] updated flag condition --- cmd/get_download_cost.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/cmd/get_download_cost.go b/cmd/get_download_cost.go index 2ea0f9dd..b1badd30 100644 --- a/cmd/get_download_cost.go +++ b/cmd/get_download_cost.go @@ -99,16 +99,12 @@ var getDownloadCostCmd = &cobra.Command{ } } - if remotePath == "" && authTicket == "" { - log.Fatal("'remotepath' or 'authticket' flag required") - } - var ( alloc *sdk.Allocation meta *sdk.ConsolidatedFileMeta ) - if remotePath != "" { + if remotePath != "" && allocID != ""{ // by remote path @@ -126,6 +122,11 @@ var getDownloadCostCmd = &cobra.Command{ // by authentication ticket + //if authTicket is provided no need of anything else to get the cost + if authTicket == "" { + log.Fatal("'authTicket' flag OR 'remotepath' & 'allocation' flag required") + } + alloc, err = sdk.GetAllocationFromAuthTicket(authTicket) if err != nil { log.Fatal("can't get allocation object: ", err) From 3bd45535762e6d3bb8494b1261b65b1c14e5a76e Mon Sep 17 00:00:00 2001 From: zorro30 Date: Sat, 27 Jan 2024 20:42:52 +0530 Subject: [PATCH 02/11] updated flag condition --- cmd/get_download_cost.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/cmd/get_download_cost.go b/cmd/get_download_cost.go index b1badd30..7fa1155c 100644 --- a/cmd/get_download_cost.go +++ b/cmd/get_download_cost.go @@ -60,10 +60,6 @@ var getDownloadCostCmd = &cobra.Command{ 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") From 5ede1ad8e874de1d3d2743d7c63eb8f44008e1d4 Mon Sep 17 00:00:00 2001 From: zorro30 Date: Sat, 27 Jan 2024 20:55:51 +0530 Subject: [PATCH 03/11] updated flag condition --- cmd/get_download_cost.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/cmd/get_download_cost.go b/cmd/get_download_cost.go index 7fa1155c..7397d28a 100644 --- a/cmd/get_download_cost.go +++ b/cmd/get_download_cost.go @@ -119,9 +119,6 @@ var getDownloadCostCmd = &cobra.Command{ // by authentication ticket //if authTicket is provided no need of anything else to get the cost - if authTicket == "" { - log.Fatal("'authTicket' flag OR 'remotepath' & 'allocation' flag required") - } alloc, err = sdk.GetAllocationFromAuthTicket(authTicket) if err != nil { From 351aeb6b0dbb56da2619a0b657899c3978a64550 Mon Sep 17 00:00:00 2001 From: zorro30 Date: Sat, 27 Jan 2024 20:56:37 +0530 Subject: [PATCH 04/11] updated flag condition --- cmd/get_download_cost.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cmd/get_download_cost.go b/cmd/get_download_cost.go index 7397d28a..772bc1a5 100644 --- a/cmd/get_download_cost.go +++ b/cmd/get_download_cost.go @@ -60,6 +60,10 @@ var getDownloadCostCmd = &cobra.Command{ 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") From 098c83db782f5b660edec8517e9d85259d675214 Mon Sep 17 00:00:00 2001 From: zorro30 Date: Sat, 27 Jan 2024 20:56:57 +0530 Subject: [PATCH 05/11] updated flag condition --- cmd/get_download_cost.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/get_download_cost.go b/cmd/get_download_cost.go index 772bc1a5..80484c6b 100644 --- a/cmd/get_download_cost.go +++ b/cmd/get_download_cost.go @@ -104,7 +104,7 @@ var getDownloadCostCmd = &cobra.Command{ meta *sdk.ConsolidatedFileMeta ) - if remotePath != "" && allocID != ""{ + if remotePath != ""{ // by remote path From a0aab0793f7305232d40a94c3385d4dd2cbb5298 Mon Sep 17 00:00:00 2001 From: zorro30 Date: Sat, 27 Jan 2024 20:57:11 +0530 Subject: [PATCH 06/11] updated flag condition --- cmd/get_download_cost.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/get_download_cost.go b/cmd/get_download_cost.go index 80484c6b..d53eb788 100644 --- a/cmd/get_download_cost.go +++ b/cmd/get_download_cost.go @@ -104,7 +104,7 @@ var getDownloadCostCmd = &cobra.Command{ meta *sdk.ConsolidatedFileMeta ) - if remotePath != ""{ + if remotePath != "" { // by remote path From 353412340a18ae2a410a64368afa5c562bc35cc7 Mon Sep 17 00:00:00 2001 From: zorro30 Date: Sat, 27 Jan 2024 20:57:38 +0530 Subject: [PATCH 07/11] updated flag condition --- cmd/get_download_cost.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/cmd/get_download_cost.go b/cmd/get_download_cost.go index d53eb788..b7958d6a 100644 --- a/cmd/get_download_cost.go +++ b/cmd/get_download_cost.go @@ -122,8 +122,6 @@ var getDownloadCostCmd = &cobra.Command{ // by authentication ticket - //if authTicket is provided no need of anything else to get the cost - alloc, err = sdk.GetAllocationFromAuthTicket(authTicket) if err != nil { log.Fatal("can't get allocation object: ", err) From 22f090be4e2e6f0e89e6120c26fee32ea6281981 Mon Sep 17 00:00:00 2001 From: zorro30 Date: Fri, 2 Feb 2024 22:38:11 +0530 Subject: [PATCH 08/11] removed get-download-cost command --- README.md | 28 ------- cmd/get_download_cost.go | 155 --------------------------------------- 2 files changed, 183 deletions(-) delete mode 100644 cmd/get_download_cost.go diff --git a/README.md b/README.md index eeb15f13..685e85f5 100644 --- a/README.md +++ b/README.md @@ -144,7 +144,6 @@ Available Commands: download download file from blobbers feed download segment files from remote live feed, and upload get-diff Get difference of local and allocation root - get-download-cost Get downloading cost get-mpt Directly view blockchain data get-upload-cost Get uploading cost getallocation Gets the allocation info @@ -1999,33 +1998,6 @@ An expired write pool, associated with an allocation, can be locked until alloca ./zbox wp-unlock ``` -#### Download cost - -`get-download-cost` determines the cost for downloading the remote file from dStorage. The client must be an -owner, collaborator, or using an auth ticket to determine the download cost of the file. - -| Parameter | Required | Description | default | Valid values | -| ---------- | -------- | ----------------------------------------- | ------- | ------------ | -| allocation | yes | allocation id | | string | -| authticket | no | auth ticket to use if not the owner | | string | -| lookuphash | no | hash of remote file, use with auth ticket | | string | -| remotepath | no | file of which to get stats, use if owner | | string | - -
- get-download-cost - -![image](https://user-images.githubusercontent.com/6240686/124497750-41ef0500-ddb3-11eb-99ea-115a4e234eda.png) - -
-Command: -``` -./zbox get-download-cost --allocation --remotepath /path/file.ext -``` -Response: -``` -0.0000107434 tokens for 10 64KB blocks (24 B) of . -``` - #### Upload cost `get-upload-cost` determines the cost for uploading a local file on dStorage. diff --git a/cmd/get_download_cost.go b/cmd/get_download_cost.go deleted file mode 100644 index b7958d6a..00000000 --- a/cmd/get_download_cost.go +++ /dev/null @@ -1,155 +0,0 @@ -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) - } - } - - 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") -} From c9fbf4252cd87b5bc2719cfa78bc91a2017162e3 Mon Sep 17 00:00:00 2001 From: zorro30 Date: Mon, 5 Feb 2024 23:00:02 +0530 Subject: [PATCH 09/11] removed relevant code usage --- cmd/getallocation.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/cmd/getallocation.go b/cmd/getallocation.go index 2431b665..38683763 100644 --- a/cmd/getallocation.go +++ b/cmd/getallocation.go @@ -7,7 +7,6 @@ import ( "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/logger" "github.com/0chain/gosdk/zboxcore/sdk" "github.com/0chain/zboxcli/util" @@ -112,7 +111,6 @@ var getallocationCmd = &cobra.Command{ fmt.Println(" price:") fmt.Println(" time_unit: ", alloc.TimeUnit) - fmt.Println(" read_price: ", calculateDownloadCost(alloc, GB, (GB+fileref.CHUNK_SIZE-1)/fileref.CHUNK_SIZE), "/ GB (by 64KB)") fmt.Println(" write_price:", uploadCostFor1GB(alloc), fmt.Sprintf("/ GB / %s", alloc.TimeUnit)) }, } From 5898f9dba24ac20138ba36fff8bc93391ef94075 Mon Sep 17 00:00:00 2001 From: zorro30 Date: Mon, 26 Feb 2024 22:24:03 +0530 Subject: [PATCH 10/11] added get-download-cost cmd back --- cmd/get_download_cost.go | 159 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 cmd/get_download_cost.go diff --git a/cmd/get_download_cost.go b/cmd/get_download_cost.go new file mode 100644 index 00000000..6ba0fa8f --- /dev/null +++ b/cmd/get_download_cost.go @@ -0,0 +1,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") +} \ No newline at end of file From 7513847f21a7d9cc7b6f33883b19c86182293db7 Mon Sep 17 00:00:00 2001 From: zorro30 Date: Mon, 26 Feb 2024 22:25:58 +0530 Subject: [PATCH 11/11] added get-download-cost cmd back --- cmd/get_download_cost.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/get_download_cost.go b/cmd/get_download_cost.go index 6ba0fa8f..2ea0f9dd 100644 --- a/cmd/get_download_cost.go +++ b/cmd/get_download_cost.go @@ -156,4 +156,4 @@ func init() { dcpf.String("authticket", "", "authticket") dcpf.String("lookuphash", "", "lookuphash, for the remote file") getDownloadCostCmd.MarkFlagRequired("allocation") -} \ No newline at end of file +}