This repository has been archived by the owner on Oct 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapi.go
144 lines (126 loc) · 2.95 KB
/
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
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
package blockmeta
import (
"context"
"fmt"
"time"
"github.com/eoscanada/eos-go"
"github.com/golang/protobuf/ptypes"
"github.com/streamingfast/blockmeta"
pbheadinfo "github.com/streamingfast/pbgo/dfuse/headinfo/v1"
"go.uber.org/zap"
)
var APIs []*eos.API
var ExtraAPIs []*eos.API
func init() {
blockmeta.BlockNumToIDFromAPI = blockNumToIDFromAPI
blockmeta.GetHeadInfoFromAPI = headInfoFromAPI
blockmeta.GetIrrIDFromAPI = IrrIDFromAPI
}
func IrrIDFromAPI(ctx context.Context, _ uint64, libNum uint64) (string, error) {
return blockNumToIDFromAPI(ctx, libNum)
}
func blockNumToIDFromAPI(ctx context.Context, blockNum uint64) (string, error) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
apis := append(APIs, ExtraAPIs...)
if len(apis) == 0 {
return "", nil
}
if blockNum < 2 {
return "", fmt.Errorf("trying to get block ID below block 2 on EOS")
}
respChan := make(chan string)
errChan := make(chan error)
for _, a := range APIs {
api := a
go func() {
blk, err := api.GetBlockByNum(ctx, uint32(blockNum))
if err != nil || blk == nil {
select {
case errChan <- err:
case <-ctx.Done():
}
return
}
id, err := blk.BlockID()
if err != nil {
select {
case errChan <- err:
case <-ctx.Done():
}
return
}
select {
case respChan <- id.String():
case <-ctx.Done():
}
}()
}
var errors []error
for {
if len(errors) == len(apis) {
return "", fmt.Errorf("all EOS APIs failed with errors: %v", errors)
}
select {
case resp := <-respChan:
return resp, nil
case err := <-errChan:
errors = append(errors, err)
case <-ctx.Done():
return "", ctx.Err()
}
}
}
func headInfoFromAPI(ctx context.Context) (*pbheadinfo.HeadInfoResponse, error) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
respChan := make(chan *pbheadinfo.HeadInfoResponse)
errChan := make(chan error)
for _, a := range APIs {
api := a
go func() {
info, err := api.GetInfo(ctx)
if err != nil {
select {
case errChan <- err:
case <-ctx.Done():
}
return
}
headTimestamp, err := ptypes.TimestampProto(info.HeadBlockTime.Time)
if err != nil {
zlog.Error("invalid timestamp conversion from head block", zap.Error(err))
select {
case errChan <- err:
case <-ctx.Done():
}
return
}
resp := &pbheadinfo.HeadInfoResponse{
LibNum: uint64(info.LastIrreversibleBlockNum),
LibID: info.LastIrreversibleBlockID.String(),
HeadNum: uint64(info.HeadBlockNum),
HeadID: info.HeadBlockID.String(),
HeadTime: headTimestamp,
}
select {
case respChan <- resp:
case <-ctx.Done():
}
}()
}
var errors []error
for {
if len(errors) == len(APIs) {
return nil, fmt.Errorf("all APIs failed with errors: %v", errors)
}
select {
case resp := <-respChan:
return resp, nil
case err := <-errChan:
errors = append(errors, err)
case <-ctx.Done():
return nil, ctx.Err()
}
}
}