Skip to content

Commit

Permalink
Merge pull request #1672 from 0chain/sprint-1.18
Browse files Browse the repository at this point in the history
Sprint 1.18
  • Loading branch information
dabasov authored Nov 13, 2024
2 parents 2383cd8 + 8cf79f5 commit 6304185
Show file tree
Hide file tree
Showing 255 changed files with 5,778 additions and 21,007 deletions.
26 changes: 13 additions & 13 deletions .github/workflows/build-sdks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ jobs:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Go 1.22
- name: Set up Go 1.23
uses: actions/setup-go@v3
with:
go-version: 1.22
go-version: 1.23

- name: Clean build
run: make clean-mobilesdk
Expand Down Expand Up @@ -96,10 +96,10 @@ jobs:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Go 1.22
- name: Set up Go 1.23
uses: actions/setup-go@v3
with:
go-version: 1.22
go-version: 1.23

- name: Install deps
run: |
Expand Down Expand Up @@ -199,10 +199,10 @@ jobs:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Go 1.22
- name: Set up Go 1.23
uses: actions/setup-go@v3
with:
go-version: 1.22
go-version: 1.23

- name: Clean build
run: make clean-mobilesdk
Expand Down Expand Up @@ -271,10 +271,10 @@ jobs:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Go 1.22
- name: Set up Go 1.23
uses: actions/setup-go@v3
with:
go-version: 1.22
go-version: 1.23

- name: Install deps
run: |
Expand Down Expand Up @@ -335,24 +335,24 @@ jobs:
name: Build-wasm
runs-on: [self-hosted, arc-runner]
steps:
- name: Set up Go 1.x
- name: Set up Go 1.23
uses: actions/setup-go@v3
with:
go-version: 1.21.5
go-version: 1.23

- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get -y install build-essential nghttp2 libnghttp2-dev libssl-dev wget
- name: Build
run: docker run --rm -v $PWD:/gosdk -w /gosdk golang:1.21.5 make wasm-build
run: docker run --rm -v $PWD:/gosdk -w /gosdk golang:1.23 make wasm-build

- name: 'Upload Artifact'
uses: actions/upload-artifact@v3
with:
name: zcn.wasm
path: zcn.wasm
path: zcn.wasm
2 changes: 1 addition & 1 deletion .github/workflows/system_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
uses: 0chain/actions/.github/workflows/manual_system_tests.yml@master
with:
gosdk_branch: ${{ github.ref_name }}
repo_snapshots_branch: ${{ github.event.inputs.repo_snapshots_branch }}
repo_snapshots_branch: fix/refactor-zboxcore
test_file_filter: ${{ github.event.inputs.test_file_filter }}
skip_tests: ${{ github.event.inputs.skip_tests }}
run_smoke_tests: ${{ github.event.inputs.run_smoke_tests }}
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@v3
with:
go-version: 1.21.5
go-version: 1.22.0

- uses: actions/checkout@v3

Expand All @@ -51,7 +51,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@v3
with:
go-version: 1.21.5
go-version: 1.22.0

- name: Install deps
run: |
Expand Down Expand Up @@ -167,10 +167,10 @@ jobs:
steps:
- uses: actions/checkout@v2

- name: Set up Go 1.x
uses: actions/setup-go@v3
- name: Set up Go 1.23
uses: actions/setup-go@v2
with:
go-version: 1.21.5
go-version: 1.23

- uses: actions/setup-node@v2
with:
Expand Down
13 changes: 13 additions & 0 deletions constants/signscheme.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package constants

type SignScheme string

const (
ED25519 SignScheme = "ed25519"
BLS0CHAIN SignScheme = "bls0chain"
)

func (s SignScheme) String() string {
return string(s)
}

3 changes: 2 additions & 1 deletion core/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ package block

import (
"fmt"

"github.com/0chain/gosdk/core/common"
"github.com/0chain/gosdk/core/encryption"
"github.com/0chain/gosdk/core/transaction"
)

const GET_BLOCK_INFO = `/v1/block/get?`

type Key []byte

type Header struct {
Expand Down
15 changes: 10 additions & 5 deletions core/node/cache.go → core/client/cache.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package node
package client

import (
"github.com/0chain/gosdk/core/logger"
"sync"
)

Expand Down Expand Up @@ -29,14 +30,18 @@ func (nc *NonceCache) GetNextNonce(clientId string) int64 {
nc.guard.Lock()
defer nc.guard.Unlock()
if _, ok := nc.cache[clientId]; !ok {
nonce, _, err := nc.sharders.GetNonceFromSharders(clientId)
if err != nil {
nonce = 0
bal, err := GetBalance(clientId)
if err != nil || bal == nil {
nc.cache[clientId] = 0
} else {
nc.cache[clientId] = bal.Nonce
}
nc.cache[clientId] = nonce
}

nc.cache[clientId] += 1

logger.Log.Info("GetNextNonce", "clientId", clientId, "nonce", nc.cache[clientId])

return nc.cache[clientId]
}

Expand Down
186 changes: 186 additions & 0 deletions core/client/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
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"
)

// SCRestAPIHandler is a function type to handle the response from the SC Rest API
//
// `response` - the response from the SC Rest API
// `numSharders` - the number of sharders that responded
// `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) {
const (
consensusThresh = float32(25.0)
ScRestApiUrl = "v1/screst/"
)

restApiUrl := ScRestApiUrl
if len(restApiUrls) > 0 {
restApiUrl = restApiUrls[0]
}

sharders := nodeClient.sharders.Healthy()
responses := make(map[int]int)
entityResult := make(map[string][]byte)

var (
retObj []byte
maxCount int
dominant = 200
wg sync.WaitGroup
mu sync.Mutex // Mutex to protect shared resources
)

cfg, err := conf.GetClientConfig()
if err != nil {
return nil, err
}

for _, sharder := range sharders {
wg.Add(1)
go func(sharder string) {
defer wg.Done()

urlString := fmt.Sprintf("%v/%v%v%v", sharder, restApiUrl, scAddress, relativePath)
urlObj, err := url.Parse(urlString)
if err != nil {
log.Println(err.Error())
return
}
q := urlObj.Query()
for k, v := range params {
q.Add(k, v)
}
urlObj.RawQuery = q.Encode()

req, err := util.NewHTTPGetRequest(urlObj.String())
if err != nil {
log.Println("Error creating request", err.Error())
return
}

response, err := req.Get()
if err != nil {
nodeClient.sharders.Fail(sharder)
log.Println("Error getting response", err.Error())
return
}

mu.Lock() // Lock before updating shared maps
defer mu.Unlock()

if response.StatusCode > http.StatusBadRequest {
nodeClient.sharders.Fail(sharder)
} else {
nodeClient.sharders.Success(sharder)
}

responses[response.StatusCode]++
if responses[response.StatusCode] > maxCount {
maxCount = responses[response.StatusCode]
}

if isCurrentDominantStatus(response.StatusCode, responses, maxCount) {
dominant = response.StatusCode
retObj = []byte(response.Body)
}

entityResult[sharder] = []byte(response.Body)
nodeClient.sharders.Success(sharder)
}(sharder)
}

wg.Wait()

rate := float32(maxCount*100) / float32(cfg.SharderConsensous)
if rate < consensusThresh {
err = errors.New("consensus_failed", "consensus failed on sharders")
}

if dominant != 200 {
var objmap map[string]json.RawMessage
err := json.Unmarshal(retObj, &objmap)
if err != nil {
return nil, errors.New("", string(retObj))
}

var parsed string
err = json.Unmarshal(objmap["error"], &parsed)
if err != nil || parsed == "" {
return nil, errors.New("", string(retObj))
}

return nil, errors.New("", parsed)
}

if rate > consensusThresh {
return retObj, nil
}
return nil, err
}

// isCurrentDominantStatus determines whether the current response status is the dominant status among responses.
//
// The dominant status is where the response status is counted the most.
// On tie-breakers, 200 will be selected if included.
//
// Function assumes runningTotalPerStatus can be accessed safely concurrently.
func isCurrentDominantStatus(respStatus int, currentTotalPerStatus map[int]int, currentMax int) bool {
// mark status as dominant if
// - running total for status is the max and response is 200 or
// - running total for status is the max and count for 200 is lower
return currentTotalPerStatus[respStatus] == currentMax && (respStatus == 200 || currentTotalPerStatus[200] < currentMax)
}

func GetBalance(clientIDs ...string) (*GetBalanceResponse, error) {
const GetBalance = "client/get/balance"
var (
balance GetBalanceResponse
err error
res []byte
)

var clientID string
if len(clientIDs) > 0 {
clientID = clientIDs[0]
} else {
clientID = Id()
}

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

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

return &balance, nil
}

type GetBalanceResponse struct {
Txn string `json:"txn"`
Round int64 `json:"round"`
Balance int64 `json:"balance"`
Nonce int64 `json:"nonce"`
}

// ToToken converts Balance to ZCN tokens.
func (b GetBalanceResponse) ToToken() (float64, error) {
f, _ := decimal.New(b.Balance, -10).Float64()
return f, nil
}
Loading

0 comments on commit 6304185

Please sign in to comment.