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

Feature/ Challenge based on rounds #1191

Merged
merged 25 commits into from
Sep 15, 2023
Merged
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
5 changes: 4 additions & 1 deletion core/transaction/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import (
"github.com/0chain/gosdk/core/util"
)

const GET_BALANCE = `/v1/client/get/balance?client_id=`
const (
GET_BALANCE = `/v1/client/get/balance?client_id=`
)

const consensusThresh = float32(25.0)

var Cache *NonceCache
Expand Down
106 changes: 106 additions & 0 deletions zboxcore/sdk/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package sdk
import (
"context"
"encoding/json"
stdErrors "errors"
"fmt"
"github.com/0chain/gosdk/core/util"
"io/ioutil"
"net/http"
"path"
"sort"
"strconv"
"sync"
"time"
Expand All @@ -19,6 +22,10 @@ import (
"github.com/0chain/gosdk/zboxcore/zboxutil"
)

const (
CURRENT_ROUND = "/v1/current-round"
)

func getObjectTreeFromBlobber(ctx context.Context, allocationID, allocationTx string, remoteFilePath string, blobber *blockchain.StorageNode) (fileref.RefEntity, error) {
httpreq, err := zboxutil.NewObjectTreeRequest(blobber.Baseurl, allocationID, allocationTx, remoteFilePath)
if err != nil {
Expand Down Expand Up @@ -121,3 +128,102 @@ func ValidateRemoteFileName(remotePath string) error {

return nil
}

func GetRoundFromSharders(sharders []string) (int64, error) {
Jayashsatolia403 marked this conversation as resolved.
Show resolved Hide resolved

if len(sharders) == 0 {
return 0, stdErrors.New("get round failed. no sharders")
}

result := make(chan *util.GetResponse, len(sharders))

var numSharders = len(sharders)
util.Shuffle(sharders)

// use 5 sharders to get round
if numSharders > 5 {
numSharders = 5
sharders = sharders[:numSharders]
}

queryFromSharders(sharders, fmt.Sprintf("%v", CURRENT_ROUND), result)

const consensusThresh = float32(25.0)

var rounds []int64

consensus := int64(0)
roundMap := make(map[int64]int64)

round := int64(0)

waitTimeC := time.After(10 * time.Second)
for i := 0; i < numSharders; i++ {
select {
case <-waitTimeC:
return 0, stdErrors.New("get round failed. consensus not reached")
case rsp := <-result:
if rsp.StatusCode != http.StatusOK {
continue
}

var respRound int64
err := json.Unmarshal([]byte(rsp.Body), &respRound)

if err != nil {
continue
}

rounds = append(rounds, respRound)

sort.Slice(rounds, func(i, j int) bool {
return false
})

medianRound := rounds[len(rounds)/2]

roundMap[medianRound]++

if roundMap[medianRound] > consensus {

consensus = roundMap[medianRound]
round = medianRound
rate := consensus * 100 / int64(numSharders)

if rate >= int64(consensusThresh) {
return round, nil
}
}
}
}

return round, nil
}

func queryFromSharders(sharders []string, query string,
result chan *util.GetResponse) {
queryFromShardersContext(context.Background(), sharders, query, result)
}

func queryFromShardersContext(ctx context.Context, sharders []string,
query string, result chan *util.GetResponse) {

for _, sharder := range sharders {
go func(sharderurl string) {
url := fmt.Sprintf("%v%v", sharderurl, query)
req, err := util.NewHTTPGetRequestContext(ctx, url)

if err != nil {
return
}

res, err := req.Get()

if err != nil {
return
}

result <- res
}(sharder)
}
}
Loading