/
sc-api.go
102 lines (85 loc) · 2.71 KB
/
sc-api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package http
import (
"crypto/sha1"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"github.com/0chain/gosdk/core/util"
"github.com/0chain/gosdk/zcncore"
"github.com/0chain/gosdk/zmagmacore/errors"
)
// MakeSCRestAPICall calls smart contract with provided address
// and makes retryable request to smart contract resource with provided relative path using params.
func MakeSCRestAPICall(scAddress string, relativePath string, params map[string]string) ([]byte, error) {
var (
resMaxCounterBody []byte
hashMaxCounter int
hashCounters = make(map[string]int)
sharders = extractSharders()
lastErrMsg string
)
for _, sharder := range sharders {
var (
client = NewRetryableClient(5)
u = makeScURL(params, sharder, scAddress, relativePath)
)
resp, err := client.Get(u.String())
if err != nil {
lastErrMsg = fmt.Sprintf("error while requesting sharders: %v", err)
continue
}
hash, resBody, err := hashAndBytesOfReader(resp.Body)
_ = resp.Body.Close()
if err != nil {
lastErrMsg = fmt.Sprintf("error while reading response body: %v", err)
continue
}
if resp.StatusCode != http.StatusOK {
lastErrMsg = fmt.Sprintf("response status is not OK; response body: %s", string(resBody))
continue
}
hashCounters[hash]++
if hashCounters[hash] > hashMaxCounter {
hashMaxCounter = hashCounters[hash]
resMaxCounterBody = resBody
}
}
if hashMaxCounter == 0 {
return nil, errors.New("request_sharders", "no valid responses, last err: "+lastErrMsg)
}
return resMaxCounterBody, nil
}
// hashAndBytesOfReader computes hash of readers data and returns hash encoded to hex and bytes of reader data.
// If error occurs while reading data from reader, it returns non nil error.
func hashAndBytesOfReader(r io.Reader) (hash string, reader []byte, err error) {
h := sha1.New()
teeReader := io.TeeReader(r, h)
readerBytes, err := ioutil.ReadAll(teeReader)
if err != nil {
return "", nil, err
}
return hex.EncodeToString(h.Sum(nil)), readerBytes, nil
}
// extractSharders returns string slice of randomly ordered sharders existing in the current network.
func extractSharders() []string {
network := zcncore.GetNetwork()
return util.GetRandom(network.Sharders, len(network.Sharders))
}
const (
// ScRestApiUrl represents base URL path to execute smart contract rest points.
ScRestApiUrl = "v1/screst/"
)
// makeScURL creates url.URL to make smart contract request to sharder.
func makeScURL(params map[string]string, sharder, scAddress, relativePath string) *url.URL {
uString := fmt.Sprintf("%v/%v%v%v", sharder, ScRestApiUrl, scAddress, relativePath)
u, _ := url.Parse(uString)
q := u.Query()
for k, v := range params {
q.Add(k, v)
}
u.RawQuery = q.Encode()
return u
}