From fcca97e1d86cd137565c1d8753721f8148125062 Mon Sep 17 00:00:00 2001 From: smaulik13 Date: Fri, 17 Jan 2025 00:18:53 +0530 Subject: [PATCH] refactor 3 --- go.mod | 2 +- go.sum | 4 + mobilesdk/zbox/models.go | 2 +- mobilesdk/zbox/storage.go | 2 +- mobilesdk/zbox/streaming.go | 2 +- wasmsdk/auth_txn.go | 2 +- wasmsdk/blobber.go | 4 +- wasmsdk/bridge.go | 13 ++- wasmsdk/chunked_upload_progress_storer.go | 2 +- wasmsdk/demo/main.go | 4 +- wasmsdk/ethwallet.go | 3 +- wasmsdk/player_file.go | 2 +- wasmsdk/player_stream.go | 2 +- wasmsdk/proxy.go | 2 +- wasmsdk/sdk.go | 12 +- wasmsdk/statusbar.go | 2 +- wasmsdk/tokenrate.go | 2 +- wasmsdk/wallet_base.go | 2 +- wasmsdk/zbox.go | 2 +- wasmsdk/zcn.go | 4 +- zboxcore/sdk/allocation.go | 27 +++-- zboxcore/sdk/blobber_operations.go | 33 ++---- zboxcore/sdk/chunked_upload.go | 2 +- zboxcore/sdk/chunked_upload_blobber.go | 2 +- zboxcore/sdk/chunked_upload_model.go | 2 +- zboxcore/sdk/commitworker.go | 2 +- zboxcore/sdk/copyworker.go | 2 +- zboxcore/sdk/deleteworker.go | 4 +- zboxcore/sdk/dirworker.go | 2 +- zboxcore/sdk/moveworker.go | 2 +- zboxcore/sdk/multi_operation_worker.go | 3 +- zboxcore/sdk/networkworker.go | 4 +- zboxcore/sdk/renameworker.go | 2 +- zboxcore/sdk/sdk.go | 48 ++++---- zboxcore/sdk/transaction.go | 130 ---------------------- zboxcore/sdk/upload_worker.go | 2 +- zboxcore/zboxutil/util.go | 2 +- 37 files changed, 107 insertions(+), 232 deletions(-) delete mode 100644 zboxcore/sdk/transaction.go diff --git a/go.mod b/go.mod index ebdc38d8c..49206cca3 100644 --- a/go.mod +++ b/go.mod @@ -44,7 +44,7 @@ require ( ) require ( - github.com/0chain/gosdk_common v0.0.0-20250116114303-c44047234606 + github.com/0chain/gosdk_common v0.0.0-20250116161245-a631a8c4c9fe github.com/hack-pad/go-webworkers v0.1.0 github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d github.com/minio/sha256-simd v1.0.1 diff --git a/go.sum b/go.sum index 87035ac38..64aea6774 100644 --- a/go.sum +++ b/go.sum @@ -48,6 +48,10 @@ github.com/0chain/gosdk_common v0.0.0-20250108074831-524b0ec9b7cd h1:zeLdGO7e5Ov github.com/0chain/gosdk_common v0.0.0-20250108074831-524b0ec9b7cd/go.mod h1:A6zAa6S8jWML2razteUYtk9aINjHW4u8MjFKITgIdp4= github.com/0chain/gosdk_common v0.0.0-20250116114303-c44047234606 h1:/pEJYnH04BoG5UJsQ+k74F/rJOtehXOgMVbB44h1gAY= github.com/0chain/gosdk_common v0.0.0-20250116114303-c44047234606/go.mod h1:5MtnciUdibSOd2nDEWw346eWXmvpZZAywwrgFm/qmKM= +github.com/0chain/gosdk_common v0.0.0-20250116160841-eeec3c6a9fcd h1:LbZTG/mIwxmR1mcS5oNsDh0hsnrRLNt0u/6/mqg8V/A= +github.com/0chain/gosdk_common v0.0.0-20250116160841-eeec3c6a9fcd/go.mod h1:5MtnciUdibSOd2nDEWw346eWXmvpZZAywwrgFm/qmKM= +github.com/0chain/gosdk_common v0.0.0-20250116161245-a631a8c4c9fe h1:opO2O58fZh2Ca1rdHMNRCkSTys95AOnpCd3WUapgeFU= +github.com/0chain/gosdk_common v0.0.0-20250116161245-a631a8c4c9fe/go.mod h1:5MtnciUdibSOd2nDEWw346eWXmvpZZAywwrgFm/qmKM= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= diff --git a/mobilesdk/zbox/models.go b/mobilesdk/zbox/models.go index 13f55e39f..d617a09fc 100644 --- a/mobilesdk/zbox/models.go +++ b/mobilesdk/zbox/models.go @@ -1,7 +1,7 @@ package zbox import ( - "github.com/0chain/gosdk/core/transaction" + "github.com/0chain/gosdk_common/core/transaction" ) type StatusCallbackMocked interface { diff --git a/mobilesdk/zbox/storage.go b/mobilesdk/zbox/storage.go index bb8ca5028..fd3342342 100644 --- a/mobilesdk/zbox/storage.go +++ b/mobilesdk/zbox/storage.go @@ -6,10 +6,10 @@ import ( "strings" "time" - "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/logger" "github.com/0chain/gosdk/zboxcore/sdk" + "github.com/0chain/gosdk_common/constants" ) type fileResp struct { diff --git a/mobilesdk/zbox/streaming.go b/mobilesdk/zbox/streaming.go index eab889540..2eff586df 100644 --- a/mobilesdk/zbox/streaming.go +++ b/mobilesdk/zbox/streaming.go @@ -9,7 +9,7 @@ import ( "sync" "time" - "github.com/0chain/gosdk/core/transaction" + "github.com/0chain/gosdk_common/core/transaction" "github.com/0chain/gosdk/zboxcore/sdk" ) diff --git a/wasmsdk/auth_txn.go b/wasmsdk/auth_txn.go index 7138d51f3..42bac292a 100644 --- a/wasmsdk/auth_txn.go +++ b/wasmsdk/auth_txn.go @@ -7,9 +7,9 @@ import ( "fmt" "syscall/js" - "github.com/0chain/gosdk/zcncore" "github.com/0chain/gosdk_common/core/sys" "github.com/0chain/gosdk_common/wasmsdk/jsbridge" + "github.com/0chain/gosdk_common/zcncore" ) type AuthCallbackFunc func(msg string) string diff --git a/wasmsdk/blobber.go b/wasmsdk/blobber.go index bcaa67368..99c3b0280 100644 --- a/wasmsdk/blobber.go +++ b/wasmsdk/blobber.go @@ -15,7 +15,7 @@ import ( "syscall/js" "time" - "github.com/0chain/gosdk/constants" + "github.com/0chain/gosdk_common/constants" "github.com/0chain/gosdk_common/core/client" "github.com/0chain/gosdk_common/core/common" "github.com/0chain/gosdk_common/core/encryption" @@ -25,9 +25,9 @@ import ( "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/sdk" - "github.com/0chain/gosdk/zboxcore/zboxutil" "github.com/0chain/gosdk_common/core/transaction" "github.com/0chain/gosdk_common/wasmsdk/jsbridge" + "github.com/0chain/gosdk_common/zboxcore/zboxutil" "github.com/hack-pad/go-webworkers/worker" ) diff --git a/wasmsdk/bridge.go b/wasmsdk/bridge.go index f0f655e12..3e60bfa06 100644 --- a/wasmsdk/bridge.go +++ b/wasmsdk/bridge.go @@ -4,14 +4,15 @@ import ( "context" "encoding/base64" "encoding/json" - "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/zcnbridge" - "github.com/0chain/gosdk/zcnbridge/errors" - "github.com/0chain/gosdk/zcnbridge/log" - "github.com/0chain/gosdk/zcncore" - "github.com/ethereum/go-ethereum/ethclient" "path" "strconv" + + "github.com/0chain/gosdk_common/core/client" + "github.com/0chain/gosdk_common/zcnbridge" + "github.com/0chain/gosdk_common/zcnbridge/errors" + "github.com/0chain/gosdk_common/zcnbridge/log" + "github.com/0chain/gosdk_common/zcncore" + "github.com/ethereum/go-ethereum/ethclient" ) var bridge *zcnbridge.BridgeClient //nolint:unused diff --git a/wasmsdk/chunked_upload_progress_storer.go b/wasmsdk/chunked_upload_progress_storer.go index d839d5be0..a3435b4ee 100644 --- a/wasmsdk/chunked_upload_progress_storer.go +++ b/wasmsdk/chunked_upload_progress_storer.go @@ -7,7 +7,7 @@ import ( "sync" "github.com/0chain/gosdk/zboxcore/sdk" - "github.com/0chain/gosdk/zboxcore/zboxutil" + "github.com/0chain/gosdk_common/zboxcore/zboxutil" ) // chunkedUploadProgressStorer load and save upload progress diff --git a/wasmsdk/demo/main.go b/wasmsdk/demo/main.go index 64934403a..02c903934 100644 --- a/wasmsdk/demo/main.go +++ b/wasmsdk/demo/main.go @@ -7,8 +7,8 @@ import ( "net/http" "sync" - "github.com/0chain/gosdk/core/common" - "github.com/0chain/gosdk/zcncore" + "github.com/0chain/gosdk_common/core/common" + "github.com/0chain/gosdk_common/zcncore" "github.com/uptrace/bunrouter" ) diff --git a/wasmsdk/ethwallet.go b/wasmsdk/ethwallet.go index bc2dbc40b..6a9b8e696 100644 --- a/wasmsdk/ethwallet.go +++ b/wasmsdk/ethwallet.go @@ -1,3 +1,4 @@ +//go:build js && wasm // +build js,wasm package main @@ -8,7 +9,7 @@ import ( "sync" "syscall/js" - "github.com/0chain/gosdk/zcncore" + "github.com/0chain/gosdk_common/zcncore" ) // JS does not have int64 so we must take a string instead of int64. diff --git a/wasmsdk/player_file.go b/wasmsdk/player_file.go index 82de0d4c8..7d847adb5 100644 --- a/wasmsdk/player_file.go +++ b/wasmsdk/player_file.go @@ -7,8 +7,8 @@ import ( "context" "fmt" - "github.com/0chain/gosdk/zboxcore/marker" "github.com/0chain/gosdk/zboxcore/sdk" + "github.com/0chain/gosdk_common/zboxcore/marker" ) type FilePlayer struct { diff --git a/wasmsdk/player_stream.go b/wasmsdk/player_stream.go index bce670691..fa81be29d 100644 --- a/wasmsdk/player_stream.go +++ b/wasmsdk/player_stream.go @@ -11,10 +11,10 @@ import ( "sync" "time" - "github.com/0chain/gosdk/zboxcore/marker" "github.com/0chain/gosdk/zboxcore/sdk" "github.com/0chain/gosdk_common/core/sys" "github.com/0chain/gosdk_common/wasmsdk/jsbridge" + "github.com/0chain/gosdk_common/zboxcore/marker" ) type StreamPlayer struct { diff --git a/wasmsdk/proxy.go b/wasmsdk/proxy.go index caef87573..231d80954 100644 --- a/wasmsdk/proxy.go +++ b/wasmsdk/proxy.go @@ -14,12 +14,12 @@ import ( "time" "github.com/0chain/gosdk/zboxcore/sdk" - "github.com/0chain/gosdk/zcncore" "github.com/0chain/gosdk_common/core/client" "github.com/0chain/gosdk_common/core/sys" "github.com/0chain/gosdk_common/core/version" "github.com/0chain/gosdk_common/core/zcncrypto" "github.com/0chain/gosdk_common/wasmsdk/jsbridge" + "github.com/0chain/gosdk_common/zcncore" "github.com/hack-pad/safejs" diff --git a/wasmsdk/sdk.go b/wasmsdk/sdk.go index 07f9974ad..94a760179 100644 --- a/wasmsdk/sdk.go +++ b/wasmsdk/sdk.go @@ -8,13 +8,13 @@ import ( "encoding/json" "fmt" - "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/core/encryption" - "github.com/0chain/gosdk/core/imageutil" - "github.com/0chain/gosdk/core/logger" - "github.com/0chain/gosdk/core/screstapi" "github.com/0chain/gosdk/zboxcore/sdk" - "github.com/0chain/gosdk/zcncore" + "github.com/0chain/gosdk_common/core/client" + "github.com/0chain/gosdk_common/core/encryption" + "github.com/0chain/gosdk_common/core/imageutil" + "github.com/0chain/gosdk_common/core/logger" + "github.com/0chain/gosdk_common/core/screstapi" + "github.com/0chain/gosdk_common/zcncore" "io" "os" diff --git a/wasmsdk/statusbar.go b/wasmsdk/statusbar.go index 526e2e4f7..e42370e3b 100644 --- a/wasmsdk/statusbar.go +++ b/wasmsdk/statusbar.go @@ -7,7 +7,7 @@ import ( "path" "sync" - "github.com/0chain/gosdk/core/sys" + "github.com/0chain/gosdk_common/core/sys" "github.com/0chain/gosdk/zboxcore/sdk" "gopkg.in/cheggaaa/pb.v1" ) diff --git a/wasmsdk/tokenrate.go b/wasmsdk/tokenrate.go index dd5897657..456206331 100644 --- a/wasmsdk/tokenrate.go +++ b/wasmsdk/tokenrate.go @@ -3,7 +3,7 @@ package main import ( "context" - "github.com/0chain/gosdk/core/tokenrate" + "github.com/0chain/gosdk_common/core/tokenrate" ) // getUSDRate gets the USD rate for the given crypto symbol diff --git a/wasmsdk/wallet_base.go b/wasmsdk/wallet_base.go index 3f7e1be44..eb30044d7 100644 --- a/wasmsdk/wallet_base.go +++ b/wasmsdk/wallet_base.go @@ -1,7 +1,7 @@ package main import ( - "github.com/0chain/gosdk/zcncore" + "github.com/0chain/gosdk_common/zcncore" ) // Split keys from the primary master key diff --git a/wasmsdk/zbox.go b/wasmsdk/zbox.go index 1d4497eb7..880f5811e 100644 --- a/wasmsdk/zbox.go +++ b/wasmsdk/zbox.go @@ -7,7 +7,7 @@ import ( "context" "errors" - "github.com/0chain/gosdk/zboxapi" + "github.com/0chain/gosdk_common/zboxapi" ) var ( diff --git a/wasmsdk/zcn.go b/wasmsdk/zcn.go index f0272e4b5..259f1d907 100644 --- a/wasmsdk/zcn.go +++ b/wasmsdk/zcn.go @@ -4,8 +4,8 @@ package main import ( - "github.com/0chain/gosdk/core/screstapi" - "github.com/0chain/gosdk/zcncore" + "github.com/0chain/gosdk_common/core/screstapi" + "github.com/0chain/gosdk_common/zcncore" ) type Balance struct { diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index f1fa9d8d0..90de34e8a 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -26,6 +26,7 @@ import ( thrown "github.com/0chain/errors" "github.com/0chain/gosdk_common/constants" "github.com/0chain/gosdk_common/core/common" + "github.com/0chain/gosdk_common/core/encryption" "github.com/0chain/gosdk_common/core/pathutil" "github.com/0chain/gosdk_common/core/sys" "github.com/0chain/gosdk_common/zboxcore/blockchain" @@ -2421,6 +2422,16 @@ func (a *Allocation) CancelDownload(remotepath string) error { return errors.New("remote_path_not_found", "Invalid path. No download in progress for the path "+remotepath) } +func (a *Allocation) CancelDownloadBlocks(remotepath string, start, end int64) error { + hash := encryption.Hash(fmt.Sprintf("%s:%d:%d", remotepath, start, end)) + if downloadReq, ok := a.downloadProgressMap[hash]; ok { + downloadReq.isDownloadCanceled = true + downloadReq.ctxCncl() + return nil + } + return errors.New("remote_path_not_found", "Invalid path. No download in progress for the path "+remotepath) +} + // DownloadFromReader downloads a file from the allocation to the specified local path using the provided reader. // [DEPRECATED] Use DownloadFile or DownloadFromAuthTicket instead. func (a *Allocation) DownloadFromReader( @@ -3102,14 +3113,14 @@ func (a *Allocation) SetConsensusThreshold() { // - fileOptionsParams: The file options parameters which control permissions of the files of the allocations. // - statusCB: A callback function to receive status updates during the update operation. func (a *Allocation) UpdateWithRepair( - size int64, + size, authRoundExpiry int64, extend bool, lock uint64, - addBlobberId, addBlobberAuthTicket, removeBlobberId string, - setThirdPartyExtendable bool, fileOptionsParams *FileOptionsParameters, + addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey string, + setThirdPartyExtendable bool, fileOptionsParams *FileOptionsParameters, updateAllocTicket string, statusCB StatusCallback, ) (string, error) { - updatedAlloc, hash, isRepairRequired, err := a.UpdateWithStatus(size, extend, lock, addBlobberId, addBlobberAuthTicket, removeBlobberId, setThirdPartyExtendable, fileOptionsParams, statusCB) + updatedAlloc, hash, isRepairRequired, err := a.UpdateWithStatus(size, authRoundExpiry, extend, lock, addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey, setThirdPartyExtendable, fileOptionsParams, updateAllocTicket) if err != nil { return hash, err } @@ -3137,12 +3148,12 @@ func (a *Allocation) UpdateWithRepair( // // Returns the updated allocation, hash, and a boolean indicating whether repair is required. func (a *Allocation) UpdateWithStatus( - size int64, + size, authRoundExpiry int64, extend bool, lock uint64, - addBlobberId, addBlobberAuthTicket, removeBlobberId string, + addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey string, setThirdPartyExtendable bool, fileOptionsParams *FileOptionsParameters, - statusCB StatusCallback, + updateAllocTicket string, ) (*Allocation, string, bool, error) { var ( alloc *Allocation @@ -3153,7 +3164,7 @@ func (a *Allocation) UpdateWithStatus( } l.Logger.Info("Updating allocation") - hash, _, err := UpdateAllocation(size, extend, a.ID, lock, addBlobberId, addBlobberAuthTicket, removeBlobberId, setThirdPartyExtendable, fileOptionsParams) + hash, _, err := UpdateAllocation(size, authRoundExpiry, extend, a.ID, lock, addBlobberId, addBlobberAuthTicket, removeBlobberId, "", ownerSigninPublicKey, setThirdPartyExtendable, fileOptionsParams, updateAllocTicket) if err != nil { return alloc, "", isRepairRequired, err } diff --git a/zboxcore/sdk/blobber_operations.go b/zboxcore/sdk/blobber_operations.go index e1d158788..06ba48548 100644 --- a/zboxcore/sdk/blobber_operations.go +++ b/zboxcore/sdk/blobber_operations.go @@ -8,7 +8,6 @@ import ( "encoding/json" "fmt" "math" - "strings" "github.com/0chain/errors" "github.com/0chain/gosdk_common/core/transaction" @@ -45,7 +44,7 @@ func CreateAllocationForOwner( return "", 0, nil, errors.New("allocation_validation_failed", "atleast 1 data and 1 parity shards are required") } - allocationRequest, err := getNewAllocationBlobbers( + allocationRequest, err := getNewAllocationBlobbers(StorageV2, datashards, parityshards, size, readPrice, writePrice, preferredBlobberIds, blobberAuthTickets, force) if err != nil { return "", 0, nil, errors.New("failed_get_allocation_blobbers", "failed to get blobbers for allocation: "+err.Error()) @@ -115,12 +114,12 @@ func CreateFreeAllocation(marker string, value uint64) (string, int64, error) { // // returns the hash of the transaction, the nonce of the transaction and an error if any. func UpdateAllocation( - size int64, + size, authRoundExpiry int64, extend bool, allocationID string, lock uint64, - addBlobberId, addBlobberAuthTicket, removeBlobberId string, - setThirdPartyExtendable bool, fileOptionsParams *FileOptionsParameters, + addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerID, ownerSigninPublicKey string, + setThirdPartyExtendable bool, fileOptionsParams *FileOptionsParameters, ticket string, ) (hash string, nonce int64, err error) { if lock > math.MaxInt64 { @@ -225,7 +224,7 @@ func StakePoolLock(providerType ProviderType, providerID string, value, fee uint // - providerType: provider type // - providerID: provider ID // - fee: transaction fee -func StakePoolUnlock(providerType ProviderType, providerID string, fee uint64) (unstake int64, nonce int64, err error) { +func StakePoolUnlock(providerType ProviderType, providerID, clientID string, fee uint64) (unstake int64, nonce int64, err error) { if !sdkInitialized { return 0, 0, sdkNotInitialized } @@ -241,6 +240,7 @@ func StakePoolUnlock(providerType ProviderType, providerID string, fee uint64) ( spr := stakePoolRequest{ ProviderType: providerType, ProviderID: providerID, + ClientID: clientID, } var sn = transaction.SmartContractTxnData{ @@ -390,24 +390,7 @@ func storageSmartContractTxnValue(sn transaction.SmartContractTxnData, value uin func smartContractTxnValueFeeWithRetry(scAddress string, sn transaction.SmartContractTxnData, value, fee uint64) (hash, out string, nonce int64, t *transaction.Transaction, err error) { - hash, out, nonce, t, err = smartContractTxnValueFee(scAddress, sn, value, fee) - if err != nil && strings.Contains(err.Error(), "invalid transaction nonce") { - return smartContractTxnValueFee(scAddress, sn, value, fee) - } - return -} - -func smartContractTxnValueFee(scAddress string, sn transaction.SmartContractTxnData, - value, fee uint64) (hash, out string, nonce int64, t *transaction.Transaction, err error) { - t, err = ExecuteSmartContract(scAddress, sn, value, fee) - if err != nil { - if t != nil { - return "", "", t.TransactionNonce, nil, err - } - - return "", "", 0, nil, err - } - - return t.Hash, t.TransactionOutput, t.TransactionNonce, t, nil + // Fee is set during sdk initialization. + return transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, value, client.TxnFee(), true) } diff --git a/zboxcore/sdk/chunked_upload.go b/zboxcore/sdk/chunked_upload.go index ec3a7a14a..bd52e8b39 100644 --- a/zboxcore/sdk/chunked_upload.go +++ b/zboxcore/sdk/chunked_upload.go @@ -16,12 +16,12 @@ import ( "errors" thrown "github.com/0chain/errors" + "github.com/0chain/gosdk/zboxcore/allocationchange" "github.com/0chain/gosdk_common/constants" "github.com/0chain/gosdk_common/core/common" coreEncryption "github.com/0chain/gosdk_common/core/encryption" "github.com/0chain/gosdk_common/core/sys" "github.com/0chain/gosdk_common/core/util" - "github.com/0chain/gosdk_common/zboxcore/allocationchange" "github.com/0chain/gosdk_common/zboxcore/blockchain" "github.com/0chain/gosdk_common/zboxcore/client" "github.com/0chain/gosdk_common/zboxcore/encryption" diff --git a/zboxcore/sdk/chunked_upload_blobber.go b/zboxcore/sdk/chunked_upload_blobber.go index 58430ff70..fb6f5e817 100644 --- a/zboxcore/sdk/chunked_upload_blobber.go +++ b/zboxcore/sdk/chunked_upload_blobber.go @@ -14,8 +14,8 @@ import ( "github.com/0chain/errors" thrown "github.com/0chain/errors" + "github.com/0chain/gosdk/zboxcore/allocationchange" "github.com/0chain/gosdk_common/constants" - "github.com/0chain/gosdk_common/zboxcore/allocationchange" "github.com/0chain/gosdk_common/zboxcore/blockchain" "github.com/0chain/gosdk_common/zboxcore/client" "github.com/0chain/gosdk_common/zboxcore/fileref" diff --git a/zboxcore/sdk/chunked_upload_model.go b/zboxcore/sdk/chunked_upload_model.go index c03a54505..d1cfaa4bf 100644 --- a/zboxcore/sdk/chunked_upload_model.go +++ b/zboxcore/sdk/chunked_upload_model.go @@ -10,8 +10,8 @@ import ( "sync" "time" + "github.com/0chain/gosdk/zboxcore/allocationchange" "github.com/0chain/gosdk_common/core/common" - "github.com/0chain/gosdk_common/zboxcore/allocationchange" "github.com/0chain/gosdk_common/zboxcore/encryption" "github.com/0chain/gosdk_common/zboxcore/fileref" "github.com/0chain/gosdk_common/zboxcore/zboxutil" diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index e5aff6a96..a882caca5 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -16,7 +16,7 @@ import ( "github.com/0chain/errors" thrown "github.com/0chain/errors" - "github.com/0chain/gosdk_common/zboxcore/allocationchange" + "github.com/0chain/gosdk/zboxcore/allocationchange" "github.com/0chain/gosdk_common/zboxcore/blockchain" "github.com/0chain/gosdk_common/zboxcore/client" "github.com/0chain/gosdk_common/zboxcore/fileref" diff --git a/zboxcore/sdk/copyworker.go b/zboxcore/sdk/copyworker.go index e0dd398ac..3730fc906 100644 --- a/zboxcore/sdk/copyworker.go +++ b/zboxcore/sdk/copyworker.go @@ -22,7 +22,7 @@ import ( "github.com/0chain/gosdk_common/zboxcore/fileref" "github.com/0chain/gosdk_common/zboxcore/logger" - "github.com/0chain/gosdk_common/zboxcore/allocationchange" + "github.com/0chain/gosdk/zboxcore/allocationchange" "github.com/0chain/gosdk_common/zboxcore/blockchain" l "github.com/0chain/gosdk_common/zboxcore/logger" "github.com/0chain/gosdk_common/zboxcore/zboxutil" diff --git a/zboxcore/sdk/deleteworker.go b/zboxcore/sdk/deleteworker.go index fa9e07d22..bfe185b3d 100644 --- a/zboxcore/sdk/deleteworker.go +++ b/zboxcore/sdk/deleteworker.go @@ -16,9 +16,9 @@ import ( thrown "github.com/0chain/errors" "github.com/google/uuid" + "github.com/0chain/gosdk/zboxcore/allocationchange" "github.com/0chain/gosdk_common/constants" "github.com/0chain/gosdk_common/core/common" - "github.com/0chain/gosdk_common/zboxcore/allocationchange" "github.com/0chain/gosdk_common/zboxcore/blockchain" "github.com/0chain/gosdk_common/zboxcore/client" "github.com/0chain/gosdk_common/zboxcore/fileref" @@ -108,7 +108,7 @@ func (req *DeleteRequest) deleteBlobberFile( } if resp.StatusCode == http.StatusBadRequest { body, err := ioutil.ReadAll(resp.Body) - if err!= nil { + if err != nil { logger.Logger.Error("Failed to read response body", err) } diff --git a/zboxcore/sdk/dirworker.go b/zboxcore/sdk/dirworker.go index 3ea0c2dc3..32c4fcf9d 100644 --- a/zboxcore/sdk/dirworker.go +++ b/zboxcore/sdk/dirworker.go @@ -13,9 +13,9 @@ import ( "time" "github.com/0chain/errors" + "github.com/0chain/gosdk/zboxcore/allocationchange" "github.com/0chain/gosdk_common/core/common" "github.com/0chain/gosdk_common/core/util" - "github.com/0chain/gosdk_common/zboxcore/allocationchange" "github.com/0chain/gosdk_common/zboxcore/blockchain" "github.com/0chain/gosdk_common/zboxcore/client" "github.com/0chain/gosdk_common/zboxcore/fileref" diff --git a/zboxcore/sdk/moveworker.go b/zboxcore/sdk/moveworker.go index efbdad4a9..03bba421c 100644 --- a/zboxcore/sdk/moveworker.go +++ b/zboxcore/sdk/moveworker.go @@ -21,7 +21,7 @@ import ( "github.com/0chain/gosdk_common/zboxcore/fileref" "github.com/0chain/gosdk_common/zboxcore/logger" - "github.com/0chain/gosdk_common/zboxcore/allocationchange" + "github.com/0chain/gosdk/zboxcore/allocationchange" "github.com/0chain/gosdk_common/zboxcore/blockchain" l "github.com/0chain/gosdk_common/zboxcore/logger" "github.com/0chain/gosdk_common/zboxcore/zboxutil" diff --git a/zboxcore/sdk/multi_operation_worker.go b/zboxcore/sdk/multi_operation_worker.go index 2d2f6c19a..ab77e7e84 100644 --- a/zboxcore/sdk/multi_operation_worker.go +++ b/zboxcore/sdk/multi_operation_worker.go @@ -13,8 +13,8 @@ import ( "github.com/0chain/errors" "github.com/remeh/sizedwaitgroup" + "github.com/0chain/gosdk/zboxcore/allocationchange" "github.com/0chain/gosdk_common/core/common" - "github.com/0chain/gosdk_common/zboxcore/allocationchange" "github.com/0chain/gosdk_common/zboxcore/client" "github.com/0chain/gosdk_common/zboxcore/fileref" "github.com/0chain/gosdk_common/zboxcore/logger" @@ -26,6 +26,7 @@ import ( const ( DefaultCreateConnectionTimeOut = 45 * time.Second + StorageV2 = 1 ) var BatchSize = 6 diff --git a/zboxcore/sdk/networkworker.go b/zboxcore/sdk/networkworker.go index 0cd8359e6..6fec75c52 100644 --- a/zboxcore/sdk/networkworker.go +++ b/zboxcore/sdk/networkworker.go @@ -9,7 +9,7 @@ import ( "strconv" "time" - "github.com/0chain/gosdk_common/core/node" + "github.com/0chain/gosdk_common/core/client" l "github.com/0chain/gosdk_common/zboxcore/logger" "go.uber.org/zap" @@ -73,7 +73,7 @@ func forceUpdateNetworkDetails(networkDetails *Network) { sdkInitialized = false blockchain.SetMiners(networkDetails.Miners) blockchain.SetSharders(networkDetails.Sharders) - node.InitCache(blockchain.Sharders) + client.InitCache(blockchain.Sharders) conf.InitChainNetwork(&conf.Network{ Sharders: networkDetails.Sharders, Miners: networkDetails.Miners, diff --git a/zboxcore/sdk/renameworker.go b/zboxcore/sdk/renameworker.go index 591a9bc2a..cbff56cd4 100644 --- a/zboxcore/sdk/renameworker.go +++ b/zboxcore/sdk/renameworker.go @@ -22,7 +22,7 @@ import ( "github.com/0chain/gosdk_common/zboxcore/fileref" "github.com/0chain/gosdk_common/zboxcore/logger" - "github.com/0chain/gosdk_common/zboxcore/allocationchange" + "github.com/0chain/gosdk/zboxcore/allocationchange" "github.com/0chain/gosdk_common/zboxcore/blockchain" l "github.com/0chain/gosdk_common/zboxcore/logger" "github.com/0chain/gosdk_common/zboxcore/zboxutil" diff --git a/zboxcore/sdk/sdk.go b/zboxcore/sdk/sdk.go index bba563756..bfcb241b6 100644 --- a/zboxcore/sdk/sdk.go +++ b/zboxcore/sdk/sdk.go @@ -14,7 +14,7 @@ import ( "github.com/0chain/errors" "github.com/0chain/gosdk_common/core/conf" "github.com/0chain/gosdk_common/core/logger" - "github.com/0chain/gosdk_common/core/node" + "github.com/0chain/gosdk_common/core/screstapi" "gopkg.in/natefinch/lumberjack.v2" "github.com/0chain/gosdk_common/core/client" @@ -189,7 +189,7 @@ func SetMinConfirmation(num int) { func SetNetwork(miners []string, sharders []string) { blockchain.SetMiners(miners) blockchain.SetSharders(sharders) - node.InitCache(blockchain.Sharders) + client.InitCache(blockchain.Sharders) } // CreateReadPool creates a read pool for the SDK client. @@ -231,7 +231,7 @@ func GetReadPoolInfo(clientID string) (info *ReadPool, err error) { } var b []byte - b, err = client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/getReadPoolStat", + b, err = screstapi.MakeSCRestAPICall(STORAGE_SCADDRESS, "/getReadPoolStat", map[string]string{"client_id": clientID}) if err != nil { return nil, errors.Wrap(err, "error requesting read pool info") @@ -306,7 +306,7 @@ func GetStakePoolInfo(providerType ProviderType, providerID string) (info *Stake } var b []byte - b, err = client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/getStakePoolStat", + b, err = screstapi.MakeSCRestAPICall(STORAGE_SCADDRESS, "/getStakePoolStat", map[string]string{"provider_type": strconv.Itoa(int(providerType)), "provider_id": providerID}) if err != nil { return nil, errors.Wrap(err, "error requesting stake pool info:") @@ -347,7 +347,7 @@ func GetStakePoolUserInfo(clientID string, offset, limit int) (info *StakePoolUs "offset": strconv.FormatInt(int64(offset), 10), "limit": strconv.FormatInt(int64(limit), 10), } - b, err = client.MakeSCRestAPICall(STORAGE_SCADDRESS, + b, err = screstapi.MakeSCRestAPICall(STORAGE_SCADDRESS, "/getUserStakePoolStat", params) if err != nil { return nil, errors.Wrap(err, "error requesting stake pool user info:") @@ -367,6 +367,7 @@ func GetStakePoolUserInfo(clientID string, offset, limit int) (info *StakePoolUs type stakePoolRequest struct { ProviderType ProviderType `json:"provider_type,omitempty"` ProviderID string `json:"provider_id,omitempty"` + ClientID string `json:"client_id,omitempty"` } // stakePoolLock is stake pool unlock response in case where tokens @@ -399,7 +400,7 @@ func GetChallengePoolInfo(allocID string) (info *ChallengePoolInfo, err error) { } var b []byte - b, err = client.MakeSCRestAPICall(STORAGE_SCADDRESS, + b, err = screstapi.MakeSCRestAPICall(STORAGE_SCADDRESS, "/getChallengePoolStat", map[string]string{"allocation_id": allocID}) if err != nil { return nil, errors.Wrap(err, "error requesting challenge pool info:") @@ -423,7 +424,7 @@ func GetMptData(key string) ([]byte, error) { } var b []byte - b, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, + b, err := screstapi.MakeSCRestAPICall(STORAGE_SCADDRESS, "/get_mpt_key", map[string]string{"key": key}) if err != nil { return nil, errors.Wrap(err, "error requesting mpt key data:") @@ -450,7 +451,7 @@ func GetStorageSCConfig() (conf *InputMap, err error) { } var b []byte - b, err = client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/storage-config", nil) + b, err = screstapi.MakeSCRestAPICall(STORAGE_SCADDRESS, "/storage-config", nil) if err != nil { return nil, errors.Wrap(err, "error requesting storage SC configs:") } @@ -624,7 +625,7 @@ func getBlobbersInternal(active, stakable bool, limit, offset int) (bs []*Blobbe offset, strconv.FormatBool(stakable), ) - b, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, url, nil) + b, err := screstapi.MakeSCRestAPICall(STORAGE_SCADDRESS, url, nil) var wrap nodes if err != nil { return nil, errors.Wrap(err, "error requesting blobbers:") @@ -682,7 +683,7 @@ func GetBlobber(blobberID string) (blob *Blobber, err error) { return nil, sdkNotInitialized } var b []byte - b, err = client.MakeSCRestAPICall( + b, err = screstapi.MakeSCRestAPICall( STORAGE_SCADDRESS, "/getBlobber", map[string]string{"blobber_id": blobberID}) @@ -706,7 +707,7 @@ func GetValidator(validatorID string) (validator *Validator, err error) { return nil, sdkNotInitialized } var b []byte - b, err = client.MakeSCRestAPICall( + b, err = screstapi.MakeSCRestAPICall( STORAGE_SCADDRESS, "/get_validator", map[string]string{"validator_id": validatorID}) @@ -730,7 +731,7 @@ func GetValidators(stakable bool) (validators []*Validator, err error) { return nil, sdkNotInitialized } var b []byte - b, err = client.MakeSCRestAPICall( + b, err = screstapi.MakeSCRestAPICall( STORAGE_SCADDRESS, "/validators", map[string]string{ @@ -793,7 +794,7 @@ func GetAllocation(allocationID string) (*Allocation, error) { } params := make(map[string]string) params["allocation"] = allocationID - allocationBytes, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocation", params) + allocationBytes, err := screstapi.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocation", params) if err != nil { return nil, errors.New("allocation_fetch_error", "Error fetching the allocation."+err.Error()) } @@ -825,7 +826,7 @@ func GetAllocationUpdates(allocation *Allocation) error { params := make(map[string]string) params["allocation"] = allocation.ID - allocationBytes, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocation", params) + allocationBytes, err := screstapi.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocation", params) if err != nil { return errors.New("allocation_fetch_error", "Error fetching the allocation."+err.Error()) } @@ -878,7 +879,7 @@ func getAllocationsInternal(clientID string, limit, offset int) ([]*Allocation, params["client"] = clientID params["limit"] = fmt.Sprint(limit) params["offset"] = fmt.Sprint(offset) - allocationsBytes, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocations", params) + allocationsBytes, err := screstapi.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocations", params) if err != nil { return nil, errors.New("allocations_fetch_error", "Error fetching the allocations."+err.Error()) } @@ -955,6 +956,8 @@ type CreateAllocationOptions struct { IsEnterprise bool FileOptionsParams *FileOptionsParameters Force bool + StorageVersion int + AuthRoundExpiry int64 } // CreateAllocationWith creates a new allocation with the given options for the current client using the SDK. @@ -982,7 +985,7 @@ func CreateAllocationWith(options CreateAllocationOptions) ( // // returns the list of blobber ids and an error if any. func GetAllocationBlobbers( - datashards, parityshards int, + storageVersion, datashards, parityshards int, size int64, isRestricted int, readPrice, writePrice PriceRange, @@ -995,6 +998,7 @@ func GetAllocationBlobbers( "read_price_range": readPrice, "write_price_range": writePrice, "is_restricted": isRestricted, + "storage_version": storageVersion, } allocationData, _ := json.Marshal(allocationRequest) @@ -1005,7 +1009,7 @@ func GetAllocationBlobbers( params["force"] = strconv.FormatBool(force[0]) } - allocBlobber, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/alloc_blobbers", params) + allocBlobber, err := screstapi.MakeSCRestAPICall(STORAGE_SCADDRESS, "/alloc_blobbers", params) if err != nil { return nil, err } @@ -1020,7 +1024,7 @@ func GetAllocationBlobbers( } func getNewAllocationBlobbers( - datashards, parityshards int, + storageVersion, datashards, parityshards int, size int64, readPrice, writePrice PriceRange, preferredBlobberIds, blobberAuthTickets []string, force bool, @@ -1040,7 +1044,7 @@ func getNewAllocationBlobbers( } allocBlobberIDs, err := GetAllocationBlobbers( - datashards, parityshards, size, 2, readPrice, writePrice, force, + storageVersion, datashards, parityshards, size, 2, readPrice, writePrice, force, ) if err != nil { return nil, err @@ -1090,7 +1094,7 @@ func GetBlobberIds(blobberUrls []string) ([]string, error) { params := make(map[string]string) params["blobber_urls"] = string(urlsStr) - idsStr, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/blobber_ids", params) + idsStr, err := screstapi.MakeSCRestAPICall(STORAGE_SCADDRESS, "/blobber_ids", params) if err != nil { return nil, err } @@ -1115,7 +1119,7 @@ func GetFreeAllocationBlobbers(request map[string]interface{}) ([]string, error) params := make(map[string]string) params["free_allocation_data"] = string(data) - allocBlobber, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/free_alloc_blobbers", params) + allocBlobber, err := screstapi.MakeSCRestAPICall(STORAGE_SCADDRESS, "/free_alloc_blobbers", params) if err != nil { return nil, err } @@ -1512,7 +1516,7 @@ func GetUpdateAllocationMinLock( params := make(map[string]string) params["data"] = string(data) - responseBytes, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocation-update-min-lock", params) + responseBytes, err := screstapi.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocation-update-min-lock", params) if err != nil { return 0, errors.Wrap(err, "failed to request allocation update min lock") } diff --git a/zboxcore/sdk/transaction.go b/zboxcore/sdk/transaction.go deleted file mode 100644 index 93131c0dc..000000000 --- a/zboxcore/sdk/transaction.go +++ /dev/null @@ -1,130 +0,0 @@ -//go:build !mobile -// +build !mobile - -package sdk - -import ( - "fmt" - "sync" - - "errors" - - "github.com/0chain/gosdk_common/core/transaction" - l "github.com/0chain/gosdk_common/zboxcore/logger" - "github.com/0chain/gosdk_common/zcncore" -) - -type transactionCallback struct { - wg *sync.WaitGroup - success bool - errMsg string - - txn *zcncore.Transaction -} - -func (cb *transactionCallback) OnTransactionComplete(t *zcncore.Transaction, status int) { - defer cb.wg.Done() - cb.txn = t - if status == zcncore.StatusSuccess { - cb.success = true - } else { - cb.errMsg = t.GetTransactionError() - } -} - -func (cb *transactionCallback) OnVerifyComplete(t *zcncore.Transaction, status int) { - defer cb.wg.Done() - cb.txn = t - if status == zcncore.StatusSuccess { - cb.success = true - } else { - cb.errMsg = t.GetVerifyError() - } -} - -func (cb *transactionCallback) OnAuthComplete(t *zcncore.Transaction, status int) { - cb.txn = t - fmt.Println("Authorization complete on zauth.", status) -} - -// ExecuteSmartContract executes the smart contract -func ExecuteSmartContract(address string, sn transaction.SmartContractTxnData, value, fee uint64) (*transaction.Transaction, error) { - wg := &sync.WaitGroup{} - cb := &transactionCallback{wg: wg} - txn, err := zcncore.NewTransaction(cb, fee, 0) - if err != nil { - return nil, err - } - - wg.Add(1) - t, err := txn.ExecuteSmartContract(address, sn.Name, sn.InputArgs, value) - if err != nil { - return nil, err - } - - msg := fmt.Sprintf("Executing transaction '%s' with hash %s ", sn.Name, t.Hash) - l.Logger.Info(msg) - l.Logger.Info("estimated txn fee: ", t.TransactionFee) - - wg.Wait() - - if !cb.success { - return nil, fmt.Errorf("smartcontract: %s", cb.errMsg) - } - - cb.success = false - wg.Add(1) - err = txn.Verify() - if err != nil { - return nil, err - } - - wg.Wait() - - if !cb.success { - return nil, fmt.Errorf("smartcontract: %s", cb.errMsg) - } - - switch txn.GetVerifyConfirmationStatus() { - case zcncore.ChargeableError: - return t, errors.New(txn.GetVerifyOutput()) - case zcncore.Success: - return t, nil - } - - return nil, fmt.Errorf("smartcontract: %v", txn.GetVerifyConfirmationStatus()) -} - -// ExecuteSmartContractSend create send transaction to transfer tokens from the caller to target address -func ExecuteSmartContractSend(to string, tokens, fee uint64, desc string) (string, error) { - wg := &sync.WaitGroup{} - cb := &transactionCallback{wg: wg} - txn, err := zcncore.NewTransaction(cb, fee, 0) - if err != nil { - return "", err - } - - wg.Add(1) - err = txn.Send(to, tokens, desc) - if err == nil { - wg.Wait() - } else { - return "", err - } - - if cb.success { - cb.success = false - wg.Add(1) - err := txn.Verify() - if err == nil { - wg.Wait() - } else { - return "", err - } - if cb.success { - return txn.GetVerifyOutput(), nil - } - } - - return "", errors.New(cb.errMsg) -} diff --git a/zboxcore/sdk/upload_worker.go b/zboxcore/sdk/upload_worker.go index e76aa6dd8..947d117a1 100644 --- a/zboxcore/sdk/upload_worker.go +++ b/zboxcore/sdk/upload_worker.go @@ -7,9 +7,9 @@ import ( "io" "strings" + "github.com/0chain/gosdk/zboxcore/allocationchange" "github.com/0chain/gosdk_common/constants" "github.com/0chain/gosdk_common/core/sys" - "github.com/0chain/gosdk_common/zboxcore/allocationchange" "github.com/0chain/gosdk_common/zboxcore/fileref" l "github.com/0chain/gosdk_common/zboxcore/logger" "github.com/0chain/gosdk_common/zboxcore/zboxutil" diff --git a/zboxcore/zboxutil/util.go b/zboxcore/zboxutil/util.go index 966cc35d3..25ea986c9 100644 --- a/zboxcore/zboxutil/util.go +++ b/zboxcore/zboxutil/util.go @@ -19,7 +19,7 @@ import ( "errors" thrown "github.com/0chain/errors" - "github.com/0chain/gosdk_common/zboxcore/allocationchange" + "github.com/0chain/gosdk/zboxcore/allocationchange" "github.com/0chain/gosdk_common/zboxcore/blockchain" "github.com/h2non/filetype" "github.com/hitenjain14/fasthttp"