Skip to content

Commit

Permalink
Merge branch 'sprint-1.19' into fix/update-upload-artifcats-action
Browse files Browse the repository at this point in the history
  • Loading branch information
Jayashsatolia403 authored Jan 24, 2025
2 parents d943a1e + c68b004 commit d920315
Show file tree
Hide file tree
Showing 22 changed files with 356 additions and 75 deletions.
13 changes: 7 additions & 6 deletions core/client/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package client
import (
"encoding/json"
"fmt"
"github.com/0chain/errors"
"github.com/0chain/gosdk/core/conf"
"github.com/0chain/gosdk/core/util"
"github.com/shopspring/decimal"
"log"
"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"
)

// SCRestAPIHandler is a function type to handle the response from the SC Rest API
Expand All @@ -20,7 +21,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 @@ -159,7 +160,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
5 changes: 5 additions & 0 deletions core/client/init_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
var (
logging = logger.GetLogger()
nodeClient *Node
IsAppFlow = false
)

// Node Maintains central states of SDK (client's context, network).
Expand All @@ -34,6 +35,10 @@ type Node struct {
networkGuard sync.RWMutex
}

func SetIsAppFlow(val bool) {
IsAppFlow = true
}

// GetStableMiners Returns stable miner urls.
// Length of stable miners is depedent on config's MinSubmit and number of miners in network.
func (n *Node) GetStableMiners() []string {
Expand Down
27 changes: 27 additions & 0 deletions core/client/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ type Client struct {
sign SignFunc
}

type InitSdkOptions struct {
WalletJSON string
BlockWorker string
ChainID string
SignatureScheme string
Nonce int64
IsSplitWallet bool
AddWallet bool
TxnFee *int
MinConfirmation *int
MinSubmit *int
ConfirmationChainLength *int
SharderConsensous *int
ZboxHost string
ZboxAppType string
}

func init() {
sys.Sign = signHash
sys.SignWithAuth = signHash
Expand Down Expand Up @@ -326,6 +343,16 @@ func InitSDK(walletJSON string,
return nil
}

func InitSDKWithWebApp(params InitSdkOptions) error {
err := InitSDK(params.WalletJSON, params.BlockWorker, params.ChainID, params.SignatureScheme, params.Nonce, params.AddWallet, *params.MinConfirmation, *params.MinSubmit, *params.ConfirmationChainLength, *params.SharderConsensous)
if err != nil {
return err
}
conf.SetZboxAppConfigs(params.ZboxHost, params.ZboxAppType)
SetIsAppFlow(true)
return nil
}

func IsSDKInitialized() bool {
return sdkInitialized
}
Expand Down
1 change: 1 addition & 0 deletions core/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ func LoadConfig(v Reader) (Config, error) {
cfg.SignatureScheme = v.GetString("signature_scheme")
cfg.ChainID = v.GetString("chain_id")
cfg.ZauthServer = v.GetString("zauth_server")
cfg.EthereumNode = v.GetString("ethereum_node_url")

return cfg, nil
}
Expand Down
5 changes: 5 additions & 0 deletions core/conf/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func GetClientConfig() (*Config, error) {
return cfg, nil
}

func SetZboxAppConfigs(zboxHost, zboxAppType string) {
cfg.ZboxHost = zboxHost
cfg.ZboxAppType = zboxAppType
}

// InitClientConfig set global client config
func InitClientConfig(c *Config) {
onceCfg.Do(func() {
Expand Down
86 changes: 86 additions & 0 deletions core/screstapi/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package screstapi

import (
"context"
"encoding/json"

"github.com/0chain/gosdk/core/client"
"github.com/0chain/gosdk/core/conf"
"github.com/0chain/gosdk/zboxapi"
)

var urlPathSharderToZboxMap = map[string]string{
"/getStakePoolStat": "/getStakePoolStat",
"/getUserStakePoolStat": "/getUserStakePoolStat",
"/getChallengePoolStat": "/getChallengePoolStat",
"/getBlobber": "/blobber",
"/getblobbers": "/blobbers",
"/blobber_ids": "/blobber_ids",
"/alloc_blobbers": "/blobbers/allocation",
"/get_validator": "/validator",
"/validators": "/validators",
"/allocation": "/getAllocation",
"/allocations": "/getAllocations",
"/v1/mint_nonce": "/mintNonce",
"client/get/balance": "/balance",
"/v1/not_processed_burn_tickets": "/not_processed_burn_tickets",
}

func MakeSCRestAPICall(scAddress string, relativePath string, params map[string]string, restApiUrls ...string) (resp []byte, err error) {
_, ok := urlPathSharderToZboxMap[relativePath]
if client.IsAppFlow && ok {
resp, err = MakeSCRestAPICallToZbox(urlPathSharderToZboxMap[relativePath], params)
if err != nil {
resp, err = client.MakeSCRestAPICallToSharder(scAddress, relativePath, params, restApiUrls...)
}
} else {
resp, err = client.MakeSCRestAPICallToSharder(scAddress, relativePath, params, restApiUrls...)
}

return resp, err
}

func MakeSCRestAPICallToZbox(relativePath string, params map[string]string) ([]byte, error) {
// req, err := http.NewRequest(method, relativePath)
zboxApiClient := zboxapi.NewClient()
configObj, err := conf.GetClientConfig()
if err != nil {
return nil, err
}
zboxApiClient.SetRequest(configObj.ZboxHost, configObj.ZboxAppType)

resp, err := zboxApiClient.MakeRestApiCallToZbox(context.TODO(), relativePath, params)
if err != nil {
return nil, err
}

return resp, nil
}

func GetBalance(clientIDs ...string) (*client.GetBalanceResponse, error) {
var clientID string
if len(clientIDs) > 0 {
clientID = clientIDs[0]
} else {
clientID = client.Id()
}

const GetBalanceUrl = "client/get/balance"
var (
balance client.GetBalanceResponse
err error
resp []byte
)

if resp, err = MakeSCRestAPICall("", GetBalanceUrl, map[string]string{
"client_id": clientID,
}, "v1/"); err != nil {
return nil, err
}

if err = json.Unmarshal(resp, &balance); err != nil {
return nil, err
}

return &balance, err
}
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
27 changes: 18 additions & 9 deletions mobilesdk/sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,18 @@ func InitStorageSDK(clientJson string, configJson string) (*StorageSDK, error) {
l.Logger.Info(configObj.ChainID)
l.Logger.Info(configObj.SignatureScheme)
l.Logger.Info(configObj.PreferredBlobbers)
if err = client.InitSDK(clientJson,
configObj.BlockWorker,
configObj.ChainID,
configObj.SignatureScheme,
0, true); err != nil {
params := client.InitSdkOptions{
WalletJSON: clientJson,
BlockWorker: configObj.BlockWorker,
ChainID: configObj.ChainID,
SignatureScheme: configObj.SignatureScheme,
Nonce: int64(0),
AddWallet: true,
ZboxHost: configObj.ZboxHost,
ZboxAppType: configObj.ZboxAppType,
}

if err = client.InitSDKWithWebApp(params); err != nil {
l.Logger.Error(err)
return nil, err
}
Expand All @@ -155,7 +162,7 @@ func InitStorageSDK(clientJson string, configJson string) (*StorageSDK, error) {
// - expiration: duration to allocation expiration
// - lock: lock write pool with given number of tokens
// - blobberAuthTickets: list of blobber auth tickets needed for the restricted blobbers
func (s *StorageSDK) CreateAllocation(datashards, parityshards int, size, expiration int64, lock string, blobberAuthTickets []string) (*zbox.Allocation, error) {
func (s *StorageSDK) CreateAllocation(datashards, parityshards int, size, expiration, authRoundExpiry int64, lock string, blobberAuthTickets []string) (*zbox.Allocation, error) {
readPrice := sdk.PriceRange{Min: 0, Max: math.MaxInt64}
writePrice := sdk.PriceRange{Min: 0, Max: math.MaxInt64}

Expand All @@ -174,6 +181,7 @@ func (s *StorageSDK) CreateAllocation(datashards, parityshards int, size, expira
BlobberIds: []string{},
FileOptionsParams: &sdk.FileOptionsParameters{},
BlobberAuthTickets: blobberAuthTickets,
AuthRoundExpiry: authRoundExpiry,
}

sdkAllocationID, _, _, err := sdk.CreateAllocationWith(options)
Expand All @@ -196,7 +204,7 @@ func (s *StorageSDK) CreateAllocation(datashards, parityshards int, size, expira
// - lock: lock write pool with given number of tokens
// - blobberUrls: concat blobber urls with comma. leave it as empty if you don't have any preferred blobbers
// - blobberIds: concat blobber ids with comma. leave it as empty if you don't have any preferred blobbers
func (s *StorageSDK) CreateAllocationWithBlobbers(name string, datashards, parityshards int, size int64, lock string, blobberUrls, blobberIds string, blobberAuthTickets []string) (*zbox.Allocation, error) {
func (s *StorageSDK) CreateAllocationWithBlobbers(name string, datashards, parityshards int, size, authRoundExpiry int64, lock string, blobberUrls, blobberIds string, blobberAuthTickets []string) (*zbox.Allocation, error) {
readPrice := sdk.PriceRange{Min: 0, Max: math.MaxInt64}
writePrice := sdk.PriceRange{Min: 0, Max: math.MaxInt64}

Expand All @@ -213,6 +221,7 @@ func (s *StorageSDK) CreateAllocationWithBlobbers(name string, datashards, parit
WritePrice: writePrice,
ReadPrice: readPrice,
BlobberAuthTickets: blobberAuthTickets,
AuthRoundExpiry: authRoundExpiry,
}

if blobberUrls != "" {
Expand Down Expand Up @@ -364,12 +373,12 @@ func (s *StorageSDK) GetVersion() string {
// - extend: extend allocation
// - allocationID: allocation ID
// - lock: Number of tokens to lock to the allocation after the update
func (s *StorageSDK) UpdateAllocation(size int64, extend bool, allocationID string, lock uint64) (hash string, err error) {
func (s *StorageSDK) UpdateAllocation(size, authRoundExpiry int64, extend bool, allocationID string, lock uint64) (hash string, err error) {
if lock > math.MaxInt64 {
return "", errors.Errorf("int64 overflow in lock")
}

hash, _, err = sdk.UpdateAllocation(size, extend, allocationID, lock, "", "", "", "", "", false, &sdk.FileOptionsParameters{}, "")
hash, _, err = sdk.UpdateAllocation(size, authRoundExpiry, extend, allocationID, lock, "", "", "", "", "", false, &sdk.FileOptionsParameters{}, "")
return hash, err
}

Expand Down
17 changes: 10 additions & 7 deletions wasmsdk/allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func getAllocationBlobbers(preferredBlobberURLs []string,
// - lock is the lock value to add to the allocation.
// - blobberIds is the list of blobber ids.
// - blobberAuthTickets is the list of blobber auth tickets in case of using restricted blobbers.
func createAllocation(datashards, parityshards int, size int64,
func createAllocation(datashards, parityshards int, size, authRoundExpiry int64,
minReadPrice, maxReadPrice, minWritePrice, maxWritePrice int64, lock int64, blobberIds, blobberAuthTickets []string, setThirdPartyExtendable, IsEnterprise, force bool) (
*transaction.Transaction, error) {

Expand All @@ -111,6 +111,7 @@ func createAllocation(datashards, parityshards int, size int64,
StorageVersion: sdk.StorageV2,
BlobberAuthTickets: blobberAuthTickets,
Force: force,
AuthRoundExpiry: authRoundExpiry,
}

sdkLogger.Info(options)
Expand Down Expand Up @@ -161,7 +162,8 @@ func transferAllocation(allocationID, newOwnerId, newOwnerPublicKey string) erro
func UpdateForbidAllocation(allocationID string, forbidupload, forbiddelete, forbidupdate, forbidmove, forbidcopy, forbidrename bool) (string, error) {

hash, _, err := sdk.UpdateAllocation(
0, //size,
0, //size,
0,
false, //extend,
allocationID, // allocID,
0, //lock,
Expand Down Expand Up @@ -192,7 +194,8 @@ func UpdateForbidAllocation(allocationID string, forbidupload, forbiddelete, for
func freezeAllocation(allocationID string) (string, error) {

hash, _, err := sdk.UpdateAllocation(
0, //size,
0, //size,
0,
false, //extend,
allocationID, // allocID,
0, //lock,
Expand Down Expand Up @@ -245,7 +248,7 @@ func cancelAllocation(allocationID string) (string, error) {
// - addBlobberAuthTicket: blobber auth ticket to add to the allocation, in case of restricted blobbers
// - removeBlobberId: blobber ID to remove from the allocation
func updateAllocationWithRepair(allocationID string,
size int64,
size, authRoundExpiry int64,
extend bool,
lock int64,
addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey, updateAllocTicket, callbackFuncName string) (string, error) {
Expand All @@ -265,7 +268,7 @@ func updateAllocationWithRepair(allocationID string,
}
}

alloc, hash, isRepairRequired, err := allocationObj.UpdateWithStatus(size, extend, uint64(lock), addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey, false, &sdk.FileOptionsParameters{}, updateAllocTicket)
alloc, hash, isRepairRequired, err := allocationObj.UpdateWithStatus(size, authRoundExpiry, extend, uint64(lock), addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey, false, &sdk.FileOptionsParameters{}, updateAllocTicket)
if err != nil {
return hash, err
}
Expand Down Expand Up @@ -299,10 +302,10 @@ func updateAllocationWithRepair(allocationID string,
// - removeBlobberId: blobber ID to remove from the allocation
// - setThirdPartyExtendable: third party extendable flag, if true, the allocation can be extended (in terms of size) by a non-owner client
func updateAllocation(allocationID string,
size int64, extend bool,
size, authRoundExpiry int64, extend bool,
lock int64,
addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey string, setThirdPartyExtendable bool) (string, error) {
hash, _, err := sdk.UpdateAllocation(size, extend, allocationID, uint64(lock), addBlobberId, addBlobberAuthTicket, removeBlobberId, "", ownerSigninPublicKey, setThirdPartyExtendable, &sdk.FileOptionsParameters{}, "")
hash, _, err := sdk.UpdateAllocation(size, authRoundExpiry, extend, allocationID, uint64(lock), addBlobberId, addBlobberAuthTicket, removeBlobberId, "", ownerSigninPublicKey, setThirdPartyExtendable, &sdk.FileOptionsParameters{}, "")

if err == nil {
clearAllocation(allocationID)
Expand Down
Loading

0 comments on commit d920315

Please sign in to comment.