diff --git a/core/client/http.go b/core/client/http.go index de7f14b11..eb7cab895 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -3,13 +3,14 @@ package client import ( "encoding/json" "fmt" + "net/http" + "net/url" + "sync" + "github.com/0chain/errors" "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/util" "github.com/shopspring/decimal" - "net/http" - "net/url" - "sync" ) // SCRestAPIHandler is a function type to handle the response from the SC Rest API @@ -19,7 +20,7 @@ import ( // `err` - the error if any type SCRestAPIHandler func(response map[string][]byte, numSharders int, err error) -func MakeSCRestAPICall(scAddress string, relativePath string, params map[string]string, restApiUrls ...string) ([]byte, error) { +func MakeSCRestAPICallToSharder(scAddress string, relativePath string, params map[string]string, restApiUrls ...string) ([]byte, error) { const ( consensusThresh = float32(25.0) ScRestApiUrl = "v1/screst/" @@ -157,7 +158,7 @@ func GetBalance(clientIDs ...string) (*GetBalanceResponse, error) { clientID = Id() } - if res, err = MakeSCRestAPICall("", GetBalance, map[string]string{ + if res, err = MakeSCRestAPICallToSharder("", GetBalance, map[string]string{ "client_id": clientID, }, "v1/"); err != nil { return nil, err diff --git a/core/transaction/get_data.go b/core/transaction/get_data.go index cf7a60065..7ac58359a 100644 --- a/core/transaction/get_data.go +++ b/core/transaction/get_data.go @@ -2,6 +2,7 @@ package transaction import ( "encoding/json" + "github.com/0chain/errors" coreHttp "github.com/0chain/gosdk/core/client" ) @@ -45,7 +46,7 @@ func GetConfig(configType string) (conf *InputMap, err error) { relativePath = GET_MINERSC_CONFIGS } - b, err = coreHttp.MakeSCRestAPICall(scAddress, relativePath, nil) + b, err = coreHttp.MakeSCRestAPICallToSharder(scAddress, relativePath, nil) if err != nil { return nil, errors.Wrap(err, "error requesting storage SC configs:") } diff --git a/wasmsdk/sdk.go b/wasmsdk/sdk.go index 189d99ab6..3dfec3d85 100644 --- a/wasmsdk/sdk.go +++ b/wasmsdk/sdk.go @@ -7,6 +7,7 @@ import ( "encoding/hex" "encoding/json" "fmt" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/imageutil" @@ -141,7 +142,8 @@ func makeSCRestAPICall(scAddress, relativePath, paramsJson string) (string, erro if err != nil { sdkLogger.Error(fmt.Sprintf("Error parsing JSON: %v", err)) } - b, err := client.MakeSCRestAPICall(scAddress, relativePath, params) + + b, err := client.MakeSCRestAPICallToSharder(scAddress, relativePath, params) return string(b), err } diff --git a/zboxapi/sdk.go b/zboxapi/sdk.go index 0ac6e7064..270e18c63 100644 --- a/zboxapi/sdk.go +++ b/zboxapi/sdk.go @@ -6,11 +6,14 @@ import ( "encoding/json" "errors" "fmt" - "github.com/0chain/gosdk/core/client" + "io" "net/http" + "net/url" "strconv" "time" + "github.com/0chain/gosdk/core/client" + thrown "github.com/0chain/errors" "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/logger" @@ -371,3 +374,31 @@ func (c *Client) GetSharedToMe(ctx context.Context, phoneNumber, token string) ( return result.Data, nil } + +func (c *Client) MakeRestApiCallToZbox(ctx context.Context, relativePath string, params map[string]string) ([]byte, error) { + urlPath := c.baseUrl + "/v2" + relativePath + u, err := url.Parse(urlPath) + if err != nil { + return nil, fmt.Errorf("error parsing URL: %w", err) + } + + // Add query parameters + q := u.Query() + for key, value := range params { + q.Add(key, value) + } + u.RawQuery = q.Encode() + + resp, err := http.Get(u.String()) + if err != nil { + return nil, fmt.Errorf("error making GET request: %w", err) + } + + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("error reading response body: %w", err) + } + resp.Body.Close() + + return body, nil +} diff --git a/zboxcore/sdk/sdk.go b/zboxcore/sdk/sdk.go index ffd625d1f..cf4bdef0e 100644 --- a/zboxcore/sdk/sdk.go +++ b/zboxcore/sdk/sdk.go @@ -4,17 +4,19 @@ import ( "encoding/base64" "encoding/json" "fmt" - "github.com/0chain/common/core/currency" - "github.com/0chain/errors" - "github.com/0chain/gosdk/core/logger" - "gopkg.in/natefinch/lumberjack.v2" "io" "math" "net/http" "strconv" + "github.com/0chain/common/core/currency" + "github.com/0chain/errors" + "github.com/0chain/gosdk/core/logger" + "gopkg.in/natefinch/lumberjack.v2" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/common" + "github.com/0chain/gosdk/core/scRestApi" "github.com/0chain/gosdk/core/transaction" "github.com/0chain/gosdk/core/version" "github.com/0chain/gosdk/zboxcore/blockchain" @@ -162,8 +164,10 @@ func GetStakePoolInfo(providerType ProviderType, providerID string) (info *Stake } var b []byte - b, err = client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/getStakePoolStat", - map[string]string{"provider_type": strconv.Itoa(int(providerType)), "provider_id": providerID}) + + b, err = scRestApi.MakeSCRestAPICall(STORAGE_SCADDRESS, "/getStakePoolStat", + map[string]string{"provider_type": strconv.Itoa(int(providerType)), "provider_id": providerID}, IsWasm) + if err != nil { return nil, errors.Wrap(err, "error requesting stake pool info:") } @@ -203,8 +207,9 @@ 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, - "/getUserStakePoolStat", params) + + b, err = scRestApi.MakeSCRestAPICall(STORAGE_SCADDRESS, "/getUserStakePoolStat", + params, IsWasm) if err != nil { return nil, errors.Wrap(err, "error requesting stake pool user info:") } @@ -255,8 +260,10 @@ func GetChallengePoolInfo(allocID string) (info *ChallengePoolInfo, err error) { } var b []byte - b, err = client.MakeSCRestAPICall(STORAGE_SCADDRESS, - "/getChallengePoolStat", map[string]string{"allocation_id": allocID}) + + b, err = scRestApi.MakeSCRestAPICall(STORAGE_SCADDRESS, "/getChallengePoolStat", + map[string]string{"allocation_id": allocID}, IsWasm) + if err != nil { return nil, errors.Wrap(err, "error requesting challenge pool info:") } @@ -279,9 +286,8 @@ func GetMptData(key string) ([]byte, error) { } var b []byte - b, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, - "/get_mpt_key", map[string]string{"key": key}, - ) + b, err := client.MakeSCRestAPICallToSharder(STORAGE_SCADDRESS, + "/get_mpt_key", map[string]string{"key": key}) if err != nil { return nil, errors.Wrap(err, "error requesting mpt key data:") } @@ -445,13 +451,12 @@ func getBlobbersInternal(active, stakable bool, limit, offset int) (bs []*Blobbe Nodes []*Blobber } - url := fmt.Sprintf("/getblobbers?active=%s&limit=%d&offset=%d&stakable=%s", - strconv.FormatBool(active), - limit, - offset, - strconv.FormatBool(stakable), - ) - b, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, url, nil) + var b []byte + + b, err = scRestApi.MakeSCRestAPICall(STORAGE_SCADDRESS, "/getblobbers", map[string]string{"active": strconv.FormatBool(active), "limit": strconv.FormatInt(int64(limit), 10), + "offset": strconv.FormatInt(int64(offset), 10), + "stakable": strconv.FormatBool(stakable)}, IsWasm) + var wrap nodes if err != nil { return nil, errors.Wrap(err, "error requesting blobbers:") @@ -461,7 +466,7 @@ func getBlobbersInternal(active, stakable bool, limit, offset int) (bs []*Blobbe } if err = json.Unmarshal(b, &wrap); err != nil { - return nil, errors.Wrap(err, "6 error decoding response:") + return nil, errors.Wrap(err, "error decoding response:") } return wrap.Nodes, nil @@ -509,11 +514,9 @@ func GetBlobber(blobberID string) (blob *Blobber, err error) { return nil, sdkNotInitialized } var b []byte - b, err = client.MakeSCRestAPICall( - STORAGE_SCADDRESS, - "/getBlobber", - map[string]string{"blobber_id": blobberID}, - ) + + b, err = scRestApi.MakeSCRestAPICall(STORAGE_SCADDRESS, "/getBlobber", + map[string]string{"blobber_id": blobberID}, IsWasm) if err != nil { return nil, errors.Wrap(err, "requesting blobber:") } @@ -534,11 +537,9 @@ func GetValidator(validatorID string) (validator *Validator, err error) { return nil, sdkNotInitialized } var b []byte - b, err = client.MakeSCRestAPICall( - STORAGE_SCADDRESS, - "/get_validator", - map[string]string{"validator_id": validatorID}, - ) + + b, err = scRestApi.MakeSCRestAPICall(STORAGE_SCADDRESS, "/get_validator", + map[string]string{"validator_id": validatorID}, IsWasm) if err != nil { return nil, errors.Wrap(err, "requesting validator:") } @@ -559,13 +560,11 @@ func GetValidators(stakable bool) (validators []*Validator, err error) { return nil, sdkNotInitialized } var b []byte - b, err = client.MakeSCRestAPICall( - STORAGE_SCADDRESS, - "/validators", + + b, err = scRestApi.MakeSCRestAPICall(STORAGE_SCADDRESS, "/validators", map[string]string{ "stakable": strconv.FormatBool(stakable), - }, - ) + }, IsWasm) if err != nil { return nil, errors.Wrap(err, "requesting validator list") } @@ -623,7 +622,11 @@ func GetAllocation(allocationID string) (*Allocation, error) { } params := make(map[string]string) params["allocation"] = allocationID - allocationBytes, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocation", params) + var allocationBytes []byte + var err error + + allocationBytes, err = scRestApi.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocation", + params, IsWasm) if err != nil { return nil, errors.New("allocation_fetch_error", "Error fetching the allocation."+err.Error()) } @@ -644,7 +647,12 @@ func GetAllocationUpdates(allocation *Allocation) error { params := make(map[string]string) params["allocation"] = allocation.ID - allocationBytes, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocation", params) + var allocationBytes []byte + var err error + + allocationBytes, err = scRestApi.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocation", + params, IsWasm) + if err != nil { return errors.New("allocation_fetch_error", "Error fetching the allocation."+err.Error()) } @@ -697,7 +705,11 @@ 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) + var allocationsBytes []byte + var err error + + allocationsBytes, err = scRestApi.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocations", + params, IsWasm) if err != nil { return nil, errors.New("allocations_fetch_error", "Error fetching the allocations."+err.Error()) } @@ -825,8 +837,11 @@ func GetAllocationBlobbers( if len(force) > 0 && force[0] { params["force"] = strconv.FormatBool(force[0]) } + var allocBlobber []byte + var err error - allocBlobber, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/alloc_blobbers", params) + allocBlobber, err = scRestApi.MakeSCRestAPICall(STORAGE_SCADDRESS, "/alloc_blobbers", + params, IsWasm) if err != nil { return nil, err } @@ -913,7 +928,10 @@ 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) + var idsStr []byte + + idsStr, err = scRestApi.MakeSCRestAPICall(STORAGE_SCADDRESS, "/blobber_ids", + params, IsWasm) if err != nil { return nil, err } @@ -938,7 +956,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 := client.MakeSCRestAPICallToSharder(STORAGE_SCADDRESS, "/free_alloc_blobbers", params) if err != nil { return nil, err } @@ -1354,7 +1372,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 := client.MakeSCRestAPICallToSharder(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/zboxutil/http.go b/zboxcore/zboxutil/http.go index 8613a78c8..e578c24a8 100644 --- a/zboxcore/zboxutil/http.go +++ b/zboxcore/zboxutil/http.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "encoding/json" - "github.com/hashicorp/golang-lru/v2/simplelru" "io" "net" "net/http" @@ -14,6 +13,8 @@ import ( "strconv" "time" + "github.com/hashicorp/golang-lru/v2/simplelru" + "github.com/0chain/errors" "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/encryption" diff --git a/zcnbridge/rest.go b/zcnbridge/rest.go index ab860b16d..f5c12241f 100644 --- a/zcnbridge/rest.go +++ b/zcnbridge/rest.go @@ -3,6 +3,7 @@ package zcnbridge import ( "encoding/json" "fmt" + coreHttp "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/common" @@ -88,7 +89,7 @@ func GetAuthorizer(id string) (res []byte, err error) { return nil, err } - return coreHttp.MakeSCRestAPICall(zcncore.ZCNSCSmartContractAddress, PathGetAuthorizer, zcncore.Params{ + return coreHttp.MakeSCRestAPICallToSharder(zcncore.ZCNSCSmartContractAddress, PathGetAuthorizer, zcncore.Params{ "id": id, }) } @@ -101,7 +102,7 @@ func GetAuthorizers(active bool) (res []byte, err error) { if err != nil { return nil, err } - return coreHttp.MakeSCRestAPICall(zcncore.ZCNSCSmartContractAddress, fmt.Sprintf(PathGetAuthorizerNodes, active), nil) + return coreHttp.MakeSCRestAPICallToSharder(zcncore.ZCNSCSmartContractAddress, fmt.Sprintf(PathGetAuthorizerNodes, active), nil) } // GetGlobalConfig Returns global config @@ -111,5 +112,5 @@ func GetGlobalConfig() (res []byte, err error) { if err != nil { return nil, err } - return coreHttp.MakeSCRestAPICall(zcncore.ZCNSCSmartContractAddress, PathGetGlobalConfig, nil) + return coreHttp.MakeSCRestAPICallToSharder(zcncore.ZCNSCSmartContractAddress, PathGetGlobalConfig, nil) } diff --git a/zcncore/get_data.go b/zcncore/get_data.go index d408ef916..484665481 100644 --- a/zcncore/get_data.go +++ b/zcncore/get_data.go @@ -5,13 +5,16 @@ import ( "encoding/json" "errors" "fmt" + "net/url" + "strconv" + "github.com/0chain/gosdk/core/block" "github.com/0chain/gosdk/core/client" + "github.com/0chain/gosdk/core/scRestApi" "github.com/0chain/gosdk/core/tokenrate" "github.com/0chain/gosdk/core/util" "github.com/0chain/gosdk/core/zcncrypto" - "net/url" - "strconv" + "github.com/0chain/gosdk/zboxcore/sdk" ) type GetClientResponse struct { @@ -177,7 +180,7 @@ func GetMinerSCNodeInfo(id string) ([]byte, error) { return nil, err } - return client.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINERSC_NODE, Params{ + return client.MakeSCRestAPICallToSharder(MinerSmartContractAddress, GET_MINERSC_NODE, Params{ "id": id, }) } @@ -189,10 +192,13 @@ func GetMintNonce() ([]byte, error) { if err != nil { return nil, err } + var res []byte - return client.MakeSCRestAPICall(ZCNSCSmartContractAddress, GET_MINT_NONCE, Params{ + res, err = scRestApi.MakeSCRestAPICall(ZCNSCSmartContractAddress, GET_MINT_NONCE, Params{ "client_id": client.Id(), - }) + }, sdk.IsWasm) + + return res, err } func GetMiners(active, stakable bool, limit, offset int) ([]byte, error) { @@ -200,7 +206,7 @@ func GetMiners(active, stakable bool, limit, offset int) ([]byte, error) { return nil, err } - return client.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINERSC_MINERS, Params{ + return client.MakeSCRestAPICallToSharder(MinerSmartContractAddress, GET_MINERSC_MINERS, Params{ "active": strconv.FormatBool(active), "stakable": strconv.FormatBool(stakable), "offset": strconv.FormatInt(int64(offset), 10), @@ -213,7 +219,7 @@ func GetSharders(active, stakable bool, limit, offset int) ([]byte, error) { return nil, err } - return client.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINERSC_SHARDERS, Params{ + return client.MakeSCRestAPICallToSharder(MinerSmartContractAddress, GET_MINERSC_SHARDERS, Params{ "active": strconv.FormatBool(active), "stakable": strconv.FormatBool(stakable), "offset": strconv.FormatInt(int64(offset), 10), @@ -225,7 +231,7 @@ func GetSharders(active, stakable bool, limit, offset int) ([]byte, error) { // - numSharders: number of sharders // - timeout: request timeout func GetLatestFinalizedMagicBlock() (m *block.MagicBlock, err error) { - res, err := client.MakeSCRestAPICall("", GET_LATEST_FINALIZED_MAGIC_BLOCK, nil, "") + res, err := client.MakeSCRestAPICallToSharder("", GET_LATEST_FINALIZED_MAGIC_BLOCK, nil, "") if err != nil { return nil, err } @@ -254,7 +260,7 @@ func GetMinerSCUserInfo(clientID string) ([]byte, error) { clientID = client.Id() } - return client.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINERSC_USER, Params{ + return client.MakeSCRestAPICallToSharder(MinerSmartContractAddress, GET_MINERSC_USER, Params{ "client_id": clientID, }) } @@ -266,7 +272,7 @@ func GetMinerSCNodePool(id string) ([]byte, error) { return nil, err } - return client.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINERSC_POOL, Params{ + return client.MakeSCRestAPICallToSharder(MinerSmartContractAddress, GET_MINERSC_POOL, Params{ "id": id, "pool_id": client.Id(), }) @@ -284,10 +290,13 @@ func GetNotProcessedZCNBurnTickets(ethereumAddress, startNonce string) ([]byte, const GET_NOT_PROCESSED_BURN_TICKETS = `/v1/not_processed_burn_tickets` - return client.MakeSCRestAPICall(ZCNSCSmartContractAddress, GET_NOT_PROCESSED_BURN_TICKETS, Params{ + var res []byte + + res, err = scRestApi.MakeSCRestAPICall(ZCNSCSmartContractAddress, GET_NOT_PROCESSED_BURN_TICKETS, Params{ "ethereum_address": ethereumAddress, "nonce": startNonce, - }) + }, sdk.IsWasm) + return res, err } // GetUserLockedTotal get total token user locked @@ -303,9 +312,9 @@ func GetUserLockedTotal(clientID string) (int64, error) { const GET_USER_LOCKED_TOTAL = `/v1/getUserLockedTotal` - info, err := client.MakeSCRestAPICall(ZCNSCSmartContractAddress, GET_USER_LOCKED_TOTAL, Params{ + info, err := scRestApi.MakeSCRestAPICall(ZCNSCSmartContractAddress, GET_USER_LOCKED_TOTAL, Params{ "client_id": clientID, - }) + }, false) if err != nil { return 0, errors.New("error while making rest api call: " + err.Error())