Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Methods migration from sharders to 0box #1661

Open
wants to merge 3 commits into
base: sprint-1.18
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions core/client/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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/"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion core/transaction/get_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package transaction

import (
"encoding/json"

"github.com/0chain/errors"
coreHttp "github.com/0chain/gosdk/core/client"
)
Expand Down Expand Up @@ -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:")
}
Expand Down
4 changes: 3 additions & 1 deletion wasmsdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
33 changes: 32 additions & 1 deletion zboxapi/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
104 changes: 61 additions & 43 deletions zboxcore/sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:")
}
Expand Down Expand Up @@ -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:")
}
Expand Down Expand Up @@ -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:")
}
Expand All @@ -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:")
}
Expand Down Expand Up @@ -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:")
Expand All @@ -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
Expand Down Expand Up @@ -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:")
}
Expand All @@ -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:")
}
Expand All @@ -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")
}
Expand Down Expand Up @@ -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())
}
Expand All @@ -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())
}
Expand Down Expand Up @@ -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())
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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")
}
Expand Down
3 changes: 2 additions & 1 deletion zboxcore/zboxutil/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/json"
"github.com/hashicorp/golang-lru/v2/simplelru"
"io"
"net"
"net/http"
Expand All @@ -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"
Expand Down
Loading