forked from dfuse-io/dfuse-eosio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheos_client.go
204 lines (167 loc) · 5.69 KB
/
eos_client.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
package searchclient
import (
"context"
"fmt"
"io"
pbcodec "github.com/dfuse-io/dfuse-eosio/pb/dfuse/eosio/codec/v1"
pbsearcheos "github.com/dfuse-io/dfuse-eosio/pb/dfuse/eosio/search/v1"
"github.com/dfuse-io/dfuse-eosio/trxdb"
"github.com/streamingfast/dhammer"
"github.com/streamingfast/logging"
pbsearch "github.com/streamingfast/pbgo/dfuse/search/v1"
"github.com/golang/protobuf/ptypes"
searchclient "github.com/streamingfast/search-client"
"go.uber.org/zap"
"google.golang.org/grpc"
)
type EOSClient struct {
*searchclient.CommonClient
dbReader trxdb.DBReader
}
type EOSStreamMatchesClient interface {
Recv() (*EOSSearchMatch, error)
}
type EOSSearchMatch struct {
*pbsearch.SearchMatch
BlockID string
BlockHeader *pbcodec.BlockHeader
TransactionTrace *pbcodec.TransactionTrace
MatchingActions []*pbcodec.ActionTrace
}
func NewEOSClient(cc *grpc.ClientConn, dbReader trxdb.DBReader) *EOSClient {
return &EOSClient{searchclient.NewCommonClient(cc), dbReader}
}
func (e *EOSClient) StreamMatches(callerCtx context.Context, req *pbsearch.RouterRequest) (EOSStreamMatchesClient, error) {
hammer := dhammer.NewHammer(30, 20, e.hammerBatchProcessor)
hammer.Start(callerCtx)
go e.StreamSearchToHammer(callerCtx, hammer, req)
esm := &eosStreamMatches{
ctx: callerCtx,
errors: make(chan error),
matches: make(chan *EOSSearchMatch),
}
go e.HammerToConsumer(callerCtx, hammer, esm.onItem, esm.onError)
return esm, nil
}
func (e *EOSClient) hammerBatchProcessor(ctx context.Context, items []interface{}) (out []interface{}, err error) {
zlogger := logging.Logger(ctx, zlog)
zlogger.Debug("processing hammer batch", zap.Int("item_count", len(items)))
prefixes, prefixToIndex := searchclient.GatherTransactionPrefixesToFetch(items, isIrreversibleEOSMatch)
var rows [][]*pbcodec.TransactionEvent
if len(prefixes) > 0 {
zlogger.Debug("performing retrieval of transaction traces", zap.Int("prefix_count", len(prefixes)))
rows, err = e.dbReader.GetTransactionTracesBatch(ctx, prefixes)
if err != nil {
return nil, fmt.Errorf("unable to fetch transaction traces batch: %w", err)
}
}
for _, v := range items {
m := v.(*searchclient.MatchOrError)
resp, err := processEOSHammerItem(ctx, m, rows, prefixToIndex)
if err != nil {
return out, err
}
out = append(out, resp)
}
return out, nil
}
func processEOSHammerItem(ctx context.Context, m *searchclient.MatchOrError, rows [][]*pbcodec.TransactionEvent, rowMap map[string]int) (*EOSSearchMatch, error) {
if m.Err != nil {
return nil, m.Err
}
trxIDPrefix := m.Match.TrxIdPrefix
var eosMatchAny ptypes.DynamicAny
err := ptypes.UnmarshalAny(m.Match.GetChainSpecific(), &eosMatchAny)
if err != nil {
return nil, err
}
eosMatch := eosMatchAny.Message.(*pbsearcheos.Match)
var blockID string
var blockHeader *pbcodec.BlockHeader
var trace *pbcodec.TransactionTrace
if eosMatch.Block != nil {
blockID = eosMatch.Block.BlockID
blockHeader = eosMatch.Block.BlockHeader
trace = eosMatch.Block.Trace
} else {
idx, ok := rowMap[trxIDPrefix]
if !ok {
return nil, fmt.Errorf("no transaction events row pointer for trx prefix %q", trxIDPrefix)
}
events := rows[idx]
if events == nil {
return nil, fmt.Errorf("transaction events for trx prefix %q are missing", trxIDPrefix)
}
// If we are here, it must be because the result was irreversible (otherwise,
// the `eosMatch.Block != nil` would have been `true`). Hence, it's ok to not have
// a chain discriminator here.
lifecycle := pbcodec.MergeTransactionEvents(events, func(id string) bool { return true })
if lifecycle.ExecutionTrace == nil {
return nil, fmt.Errorf("unable to merge transaction events correctly")
}
blockID = lifecycle.ExecutionTrace.ProducerBlockId
blockHeader = lifecycle.ExecutionBlockHeader
trace = lifecycle.ExecutionTrace
}
var matchingActions []*pbcodec.ActionTrace
if trace != nil {
matchingActions = make([]*pbcodec.ActionTrace, len(eosMatch.ActionIndexes))
for i, callIndex := range eosMatch.ActionIndexes {
matchingActions[i] = trace.ActionTraces[callIndex]
}
}
return &EOSSearchMatch{
SearchMatch: m.Match,
BlockID: blockID,
BlockHeader: blockHeader,
TransactionTrace: trace,
MatchingActions: matchingActions,
}, nil
}
func isIrreversibleEOSMatch(match *pbsearch.SearchMatch) bool {
// This sucks really hard. This was before a simple check if a variable was nil, now, it requires
// a full decoding of the any message to the correct type. This is probably a performance hit here
// to do that.
//
// Instead, the standard search engine should let us know if this match comes from a reversible or
// an irreversible segment. That would make sense and would remove the need to perform some extra
// decoding just to check if the block payload is present.
var eosMatchAny ptypes.DynamicAny
err := ptypes.UnmarshalAny(match.GetChainSpecific(), &eosMatchAny)
if err != nil {
panic("this should be an EOS match object, it should already been validated at this point, this should not happen")
}
return eosMatchAny.Message.(*pbsearcheos.Match).Block == nil
}
type eosStreamMatches struct {
ctx context.Context
errors chan error
matches chan *EOSSearchMatch
}
func (e *eosStreamMatches) Recv() (*EOSSearchMatch, error) {
select {
case <-e.ctx.Done():
if err := e.ctx.Err(); err != nil {
return nil, err
}
return nil, io.EOF
case err := <-e.errors:
return nil, err
case match := <-e.matches:
return match, nil
}
}
func (e *eosStreamMatches) onError(err error) {
select {
case <-e.ctx.Done():
return
case e.errors <- err:
}
}
func (e *eosStreamMatches) onItem(v interface{}) {
select {
case <-e.ctx.Done():
return
case e.matches <- v.(*EOSSearchMatch):
}
}