Skip to content

Commit

Permalink
Feat/caching get fees estimate (#1069)
Browse files Browse the repository at this point in the history
* added caching instead of the network call

* clean up

* clean up

* fixes for update image (#1058)

* update wasm demo for download

* preserver order in blobber of newallocationrequestg

* fixes for update image

* use multiUpload

* demo for multi operation

* clean up logs

* init Consensus

* feat wasm: UpdateForbidAllocation

* expose lockReadPool

* fix for unique blobbers

* init Consensus in Lock function (#1063)

* init Consensus

* fix panic in dirworker

---------

Co-authored-by: Hitenjain14 <[email protected]>

* fixed comments

---------

Co-authored-by: sarvesh raut <[email protected]>
Co-authored-by: boddumanohar <[email protected]>
Co-authored-by: Hitenjain14 <[email protected]>
  • Loading branch information
4 people authored Jun 29, 2023
1 parent 174cffd commit 84f2b61
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 107 deletions.
43 changes: 43 additions & 0 deletions core/transaction/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import (
"net/http"
"strings"
"sync"
"time"

"github.com/0chain/common/core/logging"
"github.com/0chain/errors"
"github.com/0chain/gosdk/core/common"
"github.com/0chain/gosdk/core/encryption"
"github.com/0chain/gosdk/core/util"
lru "github.com/hashicorp/golang-lru"
)

const TXN_SUBMIT_URL = "v1/transaction/put"
Expand Down Expand Up @@ -174,6 +177,16 @@ type SignFunc = func(msg string) (string, error)
type VerifyFunc = func(signature, msgHash, publicKey string) (bool, error)
type SignWithWallet = func(msg string, wallet interface{}) (string, error)

var cache *lru.Cache

func init() {
var err error
cache, err = lru.New(100)
if err != nil {
fmt.Println("caching Initilization failed, err:", err)
}
}

func NewTransactionEntity(clientID string, chainID string, publicKey string, nonce int64) *Transaction {
txn := &Transaction{}
txn.Version = "1.0"
Expand Down Expand Up @@ -270,8 +283,14 @@ func sendTransactionToURL(url string, txn *Transaction, wg *sync.WaitGroup) ([]b
return nil, errors.Wrap(err, errors.New("transaction_send_error", postResponse.Body))
}

type cachedObject struct {
Expiration time.Duration
Value interface{}
}

// EstimateFee estimates transaction fee
func EstimateFee(txn *Transaction, miners []string, reqPercent ...float32) (uint64, error) {

const minReqNum = 3
var reqN int

Expand All @@ -295,6 +314,20 @@ func EstimateFee(txn *Transaction, miners []string, reqPercent ...float32) (uint
go func(minerUrl string) {
defer wg.Done()

// Retrieve the object from the cache
cached, ok := cache.Get(STORAGESC_CREATE_ALLOCATION)

if ok {
cachedObj, ok := cached.(*cachedObject)
if !ok {
logging.Logger.Error("Object of bad type")
return
}
val := cachedObj.Value.(map[string]interface{})["fee"].(int)
feeC <- uint64(val)
return
}

url := minerUrl + ESTIMATE_TRANSACTION_COST
req, err := util.NewHTTPPostRequest(url, txn)
if err != nil {
Expand Down Expand Up @@ -344,6 +377,16 @@ func EstimateFee(txn *Transaction, miners []string, reqPercent ...float32) (uint
}
}

// adding response to cache
obj := map[string]interface{}{
"fee": fee,
}

cache.Add(STORAGESC_CREATE_ALLOCATION, &cachedObject{
Expiration: 30 * time.Hour,
Value: obj,
})

return fee, nil
}

Expand Down
2 changes: 1 addition & 1 deletion core/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
//====== THIS IS AUTOGENERATED FILE. DO NOT MODIFY ========

package version
const VERSIONSTR = "v1.8.16-58-g520bdba8"
const VERSIONSTR = "v1.8.16-87-gc6a0e22b"

31 changes: 31 additions & 0 deletions wasmsdk/allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,32 @@ func transferAllocation(allocationID, newOwnerId, newOwnerPublicKey string) erro
return err
}

// updateForbidAllocation updates the settings for forbid alocation
func UpdateForbidAllocation(allocationID string, forbidupload, forbiddelete, forbidupdate, forbidmove, forbidcopy, forbidrename bool) (string, error) {

hash, _, err := sdk.UpdateAllocation(
0, //size,
0, //int64(expiry/time.Second),
allocationID, // allocID,
0, //lock,
false, //updateTerms,
"", //addBlobberId,
"", //removeBlobberId,
false, //thirdPartyExtendable,
&sdk.FileOptionsParameters{
ForbidUpload: sdk.FileOptionParam{Changed: forbidupload, Value: forbidupload},
ForbidDelete: sdk.FileOptionParam{Changed: forbiddelete, Value: forbiddelete},
ForbidUpdate: sdk.FileOptionParam{Changed: forbidupdate, Value: forbidupdate},
ForbidMove: sdk.FileOptionParam{Changed: forbidmove, Value: forbidmove},
ForbidCopy: sdk.FileOptionParam{Changed: forbidcopy, Value: forbidcopy},
ForbidRename: sdk.FileOptionParam{Changed: forbidrename, Value: forbidrename},
},
)

return hash, err

}

func freezeAllocation(allocationID string) (string, error) {

hash, _, err := sdk.UpdateAllocation(
Expand Down Expand Up @@ -241,6 +267,11 @@ func lockStakePool(providerType, tokens, fee uint64, providerID string) (string,
return hash, err
}

func lockReadPool(tokens, fee uint64) (string, error) {
hash, _, err := sdk.ReadPoolLock(tokens, fee)
return hash, err
}

func unlockStakePool(providerType, fee uint64, providerID string) (int64, error) {
unstake, _, err := sdk.StakePoolUnlock(sdk.ProviderType(providerType), providerID, fee)
return unstake, err
Expand Down
14 changes: 11 additions & 3 deletions wasmsdk/blobber.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ type BulkUploadOption struct {

ThumbnailBytes jsbridge.Bytes `json:"thumbnailBytes,omitempty"`
Encrypt bool `json:"encrypt,omitempty"`
webstreaming bool `json:"webstreaming,omitempty"`
Webstreaming bool `json:"webstreaming,omitempty"`
IsUpdate bool `json:"isUpdate,omitempty"`
IsRepair bool `json:"isRepair,omitempty"`

Expand All @@ -420,6 +420,7 @@ type MultiUploadResult struct {
Success bool `json:"success,omitempty"`
Error string `json:"error,omitempty"`
}

type MultiOperationOption struct {
OperationType string `json:"operationType,omitempty"`
RemotePath string `json:"remotePath,omitempty"`
Expand All @@ -430,17 +431,23 @@ type MultiOperationOption struct {
// MultiOperation - do copy, move, delete and createdir operation together
// ## Inputs
// - allocationID
// - jsonMultiUploadOpetions: Json Array of MultiOperationOption. eg: "[{"operationType":"move","remotePath":"/README.md","destPath":"/folder1/"},{"operationType":"delete","remotePath":"/t3.txt"}]"
// - jsonMultiUploadOptions: Json Array of MultiOperationOption. eg: "[{"operationType":"move","remotePath":"/README.md","destPath":"/folder1/"},{"operationType":"delete","remotePath":"/t3.txt"}]"
//
// ## Outputs
// - error
func MultiOperation(allocationID string, jsonMultiUploadOptions string) error {
if allocationID == "" {
return errors.New("AllocationID is required")
}

if jsonMultiUploadOptions == "" {
return errors.New("operations are empty")
}

var options []MultiOperationOption
err := json.Unmarshal([]byte(jsonMultiUploadOptions), &options)
if err != nil {
sdkLogger.Info("error unmarshalling")
return err
}
totalOp := len(options)
Expand Down Expand Up @@ -481,7 +488,7 @@ func bulkUpload(jsonBulkUploadOptions string) ([]BulkUploadResult, error) {
o.ReadChunkFuncName,
o.FileSize,
o.ThumbnailBytes.Buffer,
o.webstreaming,
o.Webstreaming,
o.Encrypt,
o.IsUpdate,
o.IsRepair,
Expand All @@ -505,6 +512,7 @@ func bulkUpload(jsonBulkUploadOptions string) ([]BulkUploadResult, error) {

return results, nil
}

func multiUpload(jsonBulkUploadOptions string) (MultiUploadResult, error) {
var options []BulkUploadOption
result := MultiUploadResult{}
Expand Down
Loading

0 comments on commit 84f2b61

Please sign in to comment.