-
Notifications
You must be signed in to change notification settings - Fork 29
/
utils.go
435 lines (358 loc) · 11.1 KB
/
utils.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package transaction
import (
"context"
"encoding/json"
"fmt"
"math"
"net/http"
"strconv"
"strings"
"time"
"github.com/0chain/common/core/encryption"
"github.com/0chain/errors"
"github.com/0chain/gosdk/core/conf"
"github.com/0chain/gosdk/core/resty"
"github.com/0chain/gosdk/core/util"
)
const retriesCount = 30
type OptimisticVerifier struct {
allSharders []string
sharders []string
options []resty.Option
}
func NewOptimisticVerifier(sharders []string) *OptimisticVerifier {
//initialize resty
header := map[string]string{
"Content-Type": "application/json; charset=utf-8",
"Access-Control-Allow-Origin": "*",
}
transport := createTransport(resty.DefaultDialTimeout)
options := []resty.Option{
resty.WithRetry(resty.DefaultRetry),
resty.WithHeader(header),
resty.WithTransport(transport),
}
return &OptimisticVerifier{
allSharders: sharders,
options: options,
}
}
func (v *OptimisticVerifier) VerifyTransactionOptimistic(txnHash string) (*Transaction, error) {
cfg, err := conf.GetClientConfig()
if err != nil {
return nil, err
}
//refresh sharders
v.sharders = v.allSharders
//amount of sharders to query
minNumConfirmation := int(math.Ceil(float64(cfg.MinConfirmation*len(v.sharders)) / 100))
if minNumConfirmation > len(v.sharders) {
return nil, errors.New("verify_optimistic", "wrong number of min_confirmations")
}
shuffled := util.Shuffle(v.sharders)[:minNumConfirmation]
//prepare urls for confirmation request
urls := make([]string, 0, len(shuffled))
mappedSharders := make(map[string]string)
for _, sharder := range shuffled {
url := fmt.Sprintf("%v/%v%v", sharder, TXN_VERIFY_URL, txnHash)
urls = append(urls, url)
mappedSharders[url] = sharder
}
var url string
var chain []*RoundBlockHeader
var txn *Transaction
r := resty.New(v.options...).Then(func(req *http.Request, resp *http.Response, respBody []byte, cf context.CancelFunc, err error) error {
if err != nil { //network issue
return err
}
if resp.StatusCode != 200 {
return errors.Throw(ErrInvalidRequest, strconv.Itoa(resp.StatusCode)+": "+resp.Status)
}
//parse response
var objmap map[string]json.RawMessage
err = json.Unmarshal(respBody, &objmap)
if err != nil {
return err
}
txnRawJSON, ok := objmap["txn"]
// txn data is found, success
if !ok {
return errors.New("handle_response", "bad transaction response")
}
merklePathRawJSON, ok := objmap["merkle_tree_path"]
if !ok {
return errors.New("handle_response", "bad merkle_tree_path response")
}
txn = &Transaction{}
err = json.Unmarshal(txnRawJSON, txn)
if err != nil {
return err
}
b := &RoundBlockHeader{}
err = json.Unmarshal(respBody, b)
if err != nil {
return err
}
err = validateBlockHash(b)
if err != nil {
return err
}
err = verifyMerklePath(merklePathRawJSON, txn.Hash, b.MerkleTreeRoot)
if err != nil {
return err
}
url = req.URL.String()
chain = append(chain, b)
return nil
})
retries := 0
ticker := time.NewTicker(time.Second)
L:
//loop query confirmation
for retries < retriesCount {
<-ticker.C
retries++
r.DoGet(context.TODO(), urls...)
//need single valid confirmation
errs := r.First()
if len(errs) == 0 {
break L
}
}
if len(chain) == 0 {
return nil, errors.Newf("verify", "can't get confirmation after %v retries", retriesCount)
}
//remove current sharder from the list to avoid building chain with it
toDelete := mappedSharders[url]
for i, s := range v.sharders {
if s == toDelete {
v.sharders = append(v.sharders[:i], v.sharders[i+1:]...)
break
}
}
err = v.checkConfirmation(chain)
if err != nil {
return nil, err
}
return txn, err
}
func (v *OptimisticVerifier) checkConfirmation(chain []*RoundBlockHeader) error {
cfg, err := conf.GetClientConfig()
if err != nil {
return err
}
//build blockchain starting from confirmation block
curRound := chain[0].Round
rb := resty.New(v.options...).Then(func(req *http.Request, resp *http.Response, respBody []byte, cf context.CancelFunc, err error) error {
if err != nil { //network issue
return err
}
if resp.StatusCode != 200 {
return errors.Throw(ErrInvalidRequest, strconv.Itoa(resp.StatusCode)+": "+resp.Status)
}
curBlock := &Block{}
err = json.Unmarshal(respBody, &curBlock)
if err != nil {
return err
}
//get tail block and check that current extends it
prevBlock := chain[len(chain)-1]
if prevBlock.Hash == curBlock.PrevHash && prevBlock.Round+1 == curBlock.Round {
blockHeader := &RoundBlockHeader{
Version: curBlock.Version,
CreationDate: curBlock.CreationDate,
Hash: curBlock.Hash,
PreviousBlockHash: curBlock.PrevHash,
MinerID: curBlock.MinerID,
Round: curBlock.Round,
RoundRandomSeed: curBlock.RoundRandomSeed,
MerkleTreeRoot: curBlock.MerkleTreeRoot,
StateChangesCount: curBlock.StateChangesCount,
StateHash: curBlock.StateHash,
ReceiptMerkleTreeRoot: curBlock.ReceiptMerkleTreeRoot,
NumberOfTxns: int64(curBlock.NumTxns),
}
err = validateBlockHash(blockHeader)
if err != nil {
return err
}
chain = append(chain, blockHeader)
return nil
}
return errors.New("get_block", "wrong block")
})
//query for blocks until ConfirmationChainLength is built or every sharder is queried
for len(chain) < cfg.ConfirmationChainLength && len(v.sharders) > 0 {
//for every new block create sharder list to query
rand := util.NewRand(len(v.sharders))
//iterate through all sharders sequentially to get next block
for {
next, err := rand.Next()
if err != nil {
return errors.New("get_round_block", "can't get round block, blockchain might be stuck")
}
cur := v.sharders[next]
burl := fmt.Sprintf("%v/%v%v", cur, BLOCK_BY_ROUND_URL, curRound+1)
rb.DoGet(context.TODO(), burl)
wait := rb.Wait()
if len(wait) != 0 {
continue
}
//exclude sharder if it gave block, we do it to avoid building blockchain from single sharder
v.sharders = append(v.sharders[:next], v.sharders[next+1:]...)
curRound++
break
}
}
return nil
}
func verifyMerklePath(merklePathRawJSON json.RawMessage, txnHash string, merkleRoot string) error {
merklePath := &util.MTPath{}
err := json.Unmarshal(merklePathRawJSON, merklePath)
if err != nil {
return err
}
if !util.VerifyMerklePath(txnHash, merklePath, merkleRoot) {
return errors.New("handle_response", "invalid merkle path")
}
return nil
}
func validateBlockHash(b *RoundBlockHeader) error {
hashBuilder := strings.Builder{}
hashBuilder.WriteString(b.MinerID)
hashBuilder.WriteString(":")
hashBuilder.WriteString(b.PreviousBlockHash)
hashBuilder.WriteString(":")
hashBuilder.WriteString(strconv.FormatInt(b.CreationDate, 10))
hashBuilder.WriteString(":")
hashBuilder.WriteString(strconv.FormatInt(b.Round, 10))
hashBuilder.WriteString(":")
hashBuilder.WriteString(strconv.FormatInt(b.RoundRandomSeed, 10))
hashBuilder.WriteString(":")
hashBuilder.WriteString(strconv.Itoa(b.StateChangesCount))
hashBuilder.WriteString(":")
hashBuilder.WriteString(b.MerkleTreeRoot)
hashBuilder.WriteString(":")
hashBuilder.WriteString(b.ReceiptMerkleTreeRoot)
//todo handling of magic block here
hash := encryption.Hash(hashBuilder.String())
if hash != b.Hash {
return errors.New("handle_response", "invalid block hash")
}
return nil
}
// VerifyTransaction query transaction status from sharders, and verify it by mininal confirmation
func VerifyTransaction(txnHash string, sharders []string) (*Transaction, error) {
cfg, err := conf.GetClientConfig()
if err != nil {
return nil, err
}
if cfg.VerifyOptimistic {
ov := NewOptimisticVerifier(sharders)
return ov.VerifyTransactionOptimistic(txnHash)
} else {
return VerifyTransactionTrusted(txnHash, sharders)
}
}
// VerifyTransaction query transaction status from sharders, and verify it by mininal confirmation
func VerifyTransactionTrusted(txnHash string, sharders []string) (*Transaction, error) {
cfg, err := conf.GetClientConfig()
if err != nil {
return nil, err
}
numSharders := len(sharders)
if numSharders == 0 {
return nil, ErrNoAvailableSharder
}
minNumConfirmation := int(math.Ceil(float64(cfg.MinConfirmation*numSharders) / 100))
rand := util.NewRand(numSharders)
selectedSharders := make([]string, 0, minNumConfirmation+1)
// random pick minNumConfirmation+1 first
for i := 0; i <= minNumConfirmation; i++ {
n, err := rand.Next()
if err != nil {
break
}
selectedSharders = append(selectedSharders, sharders[n])
}
numSuccess := 0
var retTxn *Transaction
//leave first item for ErrTooLessConfirmation
var msgList = make([]string, 1, numSharders)
urls := make([]string, 0, len(selectedSharders))
for _, sharder := range selectedSharders {
urls = append(urls, fmt.Sprintf("%v/%v%v", sharder, TXN_VERIFY_URL, txnHash))
}
header := map[string]string{
"Content-Type": "application/json; charset=utf-8",
"Access-Control-Allow-Origin": "*",
}
transport := createTransport(resty.DefaultDialTimeout)
options := []resty.Option{
resty.WithRetry(resty.DefaultRetry),
resty.WithHeader(header),
resty.WithTransport(transport),
}
r := resty.New(options...).
Then(func(req *http.Request, resp *http.Response, respBody []byte, cf context.CancelFunc, err error) error {
url := req.URL.String()
if err != nil { //network issue
msgList = append(msgList, err.Error())
return err
}
if resp.StatusCode != 200 {
msgList = append(msgList, url+": ["+strconv.Itoa(resp.StatusCode)+"] "+string(respBody))
return errors.Throw(ErrInvalidRequest, strconv.Itoa(resp.StatusCode)+": "+resp.Status)
}
var objmap map[string]json.RawMessage
err = json.Unmarshal(respBody, &objmap)
if err != nil {
msgList = append(msgList, "json: "+string(respBody))
return err
}
txnRawJSON, ok := objmap["txn"]
// txn data is found, success
if ok {
txn := &Transaction{}
err = json.Unmarshal(txnRawJSON, txn)
if err != nil {
msgList = append(msgList, "json: "+string(txnRawJSON))
return err
}
if len(txn.Signature) > 0 {
retTxn = txn
}
numSuccess++
} else {
// txn data is not found, but get block_hash, success
if _, ok := objmap["block_hash"]; ok {
numSuccess++
} else {
// txn and block_hash
msgList = append(msgList, fmt.Sprintf("Sharder does not have the block summary with url: %s, contents: %s", url, string(respBody)))
}
}
return nil
})
for {
r.DoGet(context.TODO(), urls...)
r.Wait()
if numSuccess >= minNumConfirmation {
break
}
// pick one more sharder to query transaction
n, err := rand.Next()
if errors.Is(err, util.ErrNoItem) {
break
}
urls = []string{fmt.Sprintf("%v/%v%v", sharders[n], TXN_VERIFY_URL, txnHash)}
}
if numSuccess > 0 && numSuccess >= minNumConfirmation {
if retTxn == nil {
return nil, errors.Throw(ErrNoTxnDetail, strings.Join(msgList, "\r\n"))
}
return retTxn, nil
}
msgList[0] = fmt.Sprintf("min_confirmation is %v%%, but got %v/%v sharders", cfg.MinConfirmation, numSuccess, numSharders)
return nil, errors.Throw(ErrTooLessConfirmation, strings.Join(msgList, "\r\n"))
}