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

Fix get transaction consensus reach #1420

Open
wants to merge 6 commits into
base: staging
Choose a base branch
from
31 changes: 18 additions & 13 deletions zboxcore/zboxutil/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"io/ioutil"
"math"
"net"
"net/http"
"net/url"
Expand All @@ -17,7 +18,6 @@ import (
"time"

"github.com/0chain/errors"
"github.com/0chain/gosdk/core/conf"
"github.com/0chain/gosdk/core/encryption"
"github.com/0chain/gosdk/core/logger"
"github.com/0chain/gosdk/zboxcore/blockchain"
Expand All @@ -30,9 +30,6 @@ const SC_REST_API_URL = "v1/screst/"
const MAX_RETRIES = 5
const SLEEP_BETWEEN_RETRIES = 5

// In percentage
const consensusThresh = float32(25.0)

type SCRestAPIHandler func(response map[string][]byte, numSharders int, err error)

type HttpClient interface {
Expand Down Expand Up @@ -928,6 +925,7 @@ func NewRollbackRequest(baseUrl, allocationID string, allocationTx string, body

func MakeSCRestAPICall(scAddress string, relativePath string, params map[string]string, handler SCRestAPIHandler) ([]byte, error) {
numSharders := len(blockchain.GetSharders())
reqConsensus := int(math.Ceil(float64(numSharders) * float64(0.5)))
sharders := blockchain.GetSharders()
responses := make(map[int]int)
mu := &sync.Mutex{}
Expand All @@ -937,11 +935,8 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string]
dominant := 200
wg := sync.WaitGroup{}

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

ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
for _, sharder := range sharders {
wg.Add(1)
go func(sharder string) {
Expand All @@ -958,7 +953,12 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string]
}
urlObj.RawQuery = q.Encode()
client := &http.Client{Transport: DefaultTransport}
response, err := client.Get(urlObj.String())
req, err := http.NewRequestWithContext(ctx, http.MethodGet, urlObj.String(), nil)
if err != nil {
log.Error(err)
return
}
response, err := client.Do(req)
if err != nil {
blockchain.Sharders.Fail(sharder)
return
Expand All @@ -984,13 +984,18 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string]

entityResult[sharder] = entityBytes
blockchain.Sharders.Success(sharder)

// if consensus is reached by enough sharders, cancel other requests
if maxCount >= reqConsensus {
cancelFunc()
}
mu.Unlock()
}(sharder)
}
wg.Wait()

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

Expand All @@ -1014,7 +1019,7 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string]
handler(entityResult, numSharders, err)
}

if rate > consensusThresh {
if maxCount >= reqConsensus {
return retObj, nil
}
return nil, err
Expand Down
5 changes: 3 additions & 2 deletions zcncore/transaction_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,9 @@ func (tq *TransactionQuery) GetInfo(ctx context.Context, query string) (*QueryRe
consensusesResp = qr
}

return false

// check whether consensus is reached by enough sharders, and return true to cancel other requests
rate := maxConsensus * 100 / tq.max
return rate >= consensusThresh
})

if err != nil {
Expand Down