-
Notifications
You must be signed in to change notification settings - Fork 45
/
tablerows.go
297 lines (251 loc) · 9.04 KB
/
tablerows.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
// Copyright 2020 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package eosws
import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"strings"
"github.com/streamingfast/bstream"
"github.com/streamingfast/bstream/forkable"
"github.com/streamingfast/derr"
"github.com/dfuse-io/dfuse-eosio/eosws/metrics"
"github.com/dfuse-io/dfuse-eosio/eosws/wsmsg"
pbcodec "github.com/dfuse-io/dfuse-eosio/pb/dfuse/eosio/codec/v1"
pbstatedb "github.com/dfuse-io/dfuse-eosio/pb/dfuse/eosio/statedb/v1"
"github.com/streamingfast/dtracing"
v1 "github.com/dfuse-io/eosws-go/mdl/v1"
"github.com/streamingfast/logging"
eos "github.com/eoscanada/eos-go"
"go.uber.org/zap"
)
func (ws *WSConn) onGetTableRows(ctx context.Context, msg *wsmsg.GetTableRows) {
scope := ""
if msg.Data.Scope != nil {
scope = string(*msg.Data.Scope)
}
zlogger := logging.Logger(ctx, zlog)
zlogger.Debug("handling get table rows stream",
zap.String("account", string(msg.Data.Code)),
zap.String("scope", string(scope)),
zap.String("table_name", string(msg.Data.TableName)),
)
authReq, ok := ws.AuthorizeRequest(ctx, msg)
if !ok {
return
}
startBlockNum := authReq.StartBlockNum
if msg.Fetch && authReq.IsFutureBlock {
ws.EmitErrorReply(ctx, msg, AppTableRowsCannotFetchInFutureError(ctx, startBlockNum))
return
}
fetchTableRows(ctx, ws, startBlockNum, msg, ws.abiGetter, ws, ws.stateClient, ws.irreversibleFinder)
}
func fetchTableRows(
ctx context.Context,
ws *WSConn,
startBlockNum uint32,
msg *wsmsg.GetTableRows,
abiGetter ABIGetter,
emitter Emitter,
stateClient pbstatedb.StateClient,
irrFinder IrreversibleFinder,
) {
zlogger := logging.Logger(ctx, zlog)
startBlockID := ""
if msg.Fetch {
spanContext, fetchSpan := dtracing.StartSpan(ctx, "fetch table rows")
if msg.StartBlock == 0 {
zlogger.Info("user requested start block 0, let statedb turns into head block instead of us doing it to prevent race condition")
startBlockNum = 0
}
request := &pbstatedb.StreamTableRowsRequest{
BlockNum: uint64(startBlockNum),
Contract: string(msg.Data.Code),
Table: string(msg.Data.TableName),
Scope: string(*msg.Data.Scope),
ToJson: true,
}
zlogger.Info("requesting data from statedb", zap.Any("request", request))
ref, snapshot, err := fetchStateTableRows(spanContext, stateClient, request)
if err != nil {
emitter.EmitErrorReply(ctx, msg, fmt.Errorf("fetch table rows: %w", err))
fetchSpan.End()
return
}
metrics.DocumentResponseCounter.Inc()
emitter.EmitReply(spanContext, msg, snapshot)
if ref.UpToBlock != nil {
startBlockID = ref.UpToBlock.ID()
startBlockNum = uint32(ref.UpToBlock.Num())
zlogger.Info("state client response", zap.Stringer("up_to_block", ref.UpToBlock))
}
fetchSpan.End()
}
if msg.Listen {
_, listenSpan := dtracing.StartSpan(ctx, "ws listen table rows")
var err error
var abiChangeHandler *ABIChangeHandler
tableDeltaHandler := newTableDeltaHandler(ctx, msg, emitter, zlog, func() *eos.ABI {
return abiChangeHandler.CurrentABI()
})
abiChangeHandler, err = NewABIChangeHandler(abiGetter, startBlockNum, msg.Data.Code, tableDeltaHandler, ctx)
if err != nil {
emitter.EmitErrorReply(ctx, msg, derr.Wrap(err, "unable to retrieve abi"))
return
}
var handler bstream.Handler
handler = abiChangeHandler
if freq := msg.WithProgress; freq != 0 {
handler = NewProgressHandler(abiChangeHandler, emitter, msg, ctx)
}
var forkablePostGate bstream.Handler
var irrID string
if startBlockID != "" { //Flux return this ID
irrID, err = irrFinder.IrreversibleIDAtBlockID(ctx, startBlockID)
if err != nil {
emitter.EmitErrorReply(ctx, msg, derr.Wrap(err, "unable to retrieve irreversibility"))
return
}
forkablePostGate = bstream.NewBlockIDGate(startBlockID, bstream.GateExclusive, handler, bstream.GateOptionWithLogger(zlog))
} else {
irrID, err = irrFinder.IrreversibleIDAtBlockNum(ctx, startBlockNum)
if err != nil {
emitter.EmitErrorReply(ctx, msg, derr.Wrap(err, "unable to retrieve irreversibility"))
return
}
forkablePostGate = bstream.NewBlockNumGate(uint64(startBlockNum), bstream.GateInclusive, handler, bstream.GateOptionWithLogger(zlog))
}
irrRef := bstream.NewBlockRefFromID(irrID)
forkableHandler := forkable.New(forkablePostGate, forkable.WithLogger(zlog), forkable.WithExclusiveLIB(irrRef))
metrics.IncListeners("get_table_rows")
source := ws.subscriptionHub.NewSourceFromBlockNumWithOpts(irrRef.Num(), forkableHandler, bstream.JoiningSourceTargetBlockID(irrRef.ID()), bstream.JoiningSourceRateLimit(300, ws.filesourceBlockRateLimit))
source.OnTerminating(func(_ error) {
metrics.CurrentListeners.Dec("get_table_rows")
listenSpan.End()
})
err = ws.RegisterListener(ctx, msg.ReqID, func() error {
zlogger.Debug("fetchTableRows: canceller call", zap.String("req_id", msg.ReqID))
source.Shutdown(nil)
return nil
})
if err != nil {
source.Shutdown(nil) // important to ensure that OnRunFunc is run
emitter.EmitErrorReply(ctx, msg, derr.Wrap(err, "unable to register listener to ws connection"))
return
}
emitter.EmitReply(ctx, msg, wsmsg.NewListening(startBlockNum+1))
go source.Run()
}
}
func tableDeltasFromBlock(block *bstream.Block, msg *wsmsg.GetTableRows, abi *eos.ABI, step forkable.StepType, zlog *zap.Logger) []*wsmsg.TableDelta {
zlog.Debug("about to stream table deltas from block", zap.Stringer("block", block), zap.Stringer("step", step))
var deltas []*wsmsg.TableDelta
blk := block.ToNative().(*pbcodec.Block)
for _, trxTrace := range blk.TransactionTraces() {
actionMatcher := blk.FilteringActionMatcher(trxTrace)
for _, dbOp := range trxTrace.DbOps {
if !actionMatcher.Matched(dbOp.ActionIndex) {
continue
}
if dbOp.Code != string(msg.Data.Code) || dbOp.TableName != string(msg.Data.TableName) || dbOp.Scope != string(*msg.Data.Scope) {
continue
}
v1DBOp := &v1.DBOp{
Op: strings.ToLower(dbOp.LegacyOperation()),
Key: dbOp.PrimaryKey,
ActionIndex: int(dbOp.ActionIndex),
Scope: dbOp.Scope,
Account: dbOp.Code,
Table: dbOp.TableName,
}
if len(dbOp.OldData) != 0 {
v1DBOp.Old = newDBRow(dbOp.OldData, msg.Data.TableName, abi, dbOp.OldPayer, msg.Data.JSON, zlog)
}
if len(dbOp.NewData) != 0 {
v1DBOp.New = newDBRow(dbOp.NewData, msg.Data.TableName, abi, dbOp.NewPayer, msg.Data.JSON, zlog)
}
if step == forkable.StepUndo {
oldRow := v1DBOp.Old
v1DBOp.Old = v1DBOp.New
v1DBOp.New = oldRow
switch v1DBOp.Op {
case "ins":
v1DBOp.Op = "rem"
case "rem":
v1DBOp.Op = "ins"
}
}
deltas = append(deltas,
wsmsg.NewTableDelta(
uint32(blk.Num()),
v1DBOp,
step,
),
)
}
}
return deltas
}
func newDBRow(data []byte, tableName eos.TableName, abi *eos.ABI, payer string, needJSON bool, zlog *zap.Logger) *v1.DBRow {
row := &v1.DBRow{
Payer: payer,
}
if needJSON {
rowData, err := abi.DecodeTableRow(tableName, data)
if err == nil {
row.JSON = json.RawMessage(rowData)
} else {
zlog.Error("couldn't decode row", zap.Error(err))
row.Error = fmt.Sprintf("Couldn't json decode ROW: %s", err)
}
} else {
row.Hex = hex.EncodeToString(data)
}
return row
}
func fetchStateTableRows(ctx context.Context, stateClient pbstatedb.StateClient, request *pbstatedb.StreamTableRowsRequest) (ref *pbstatedb.StreamReference, out *wsmsg.TableSnapshot, err error) {
out = new(wsmsg.TableSnapshot)
ref, err = pbstatedb.ForEachTableRows(ctx, stateClient, request, func(row *pbstatedb.TableRowResponse) error {
out.Data.Rows = append(out.Data.Rows, []byte(row.Json))
return nil
})
return
}
type tableDeltaHandler struct {
msg *wsmsg.GetTableRows
emitter Emitter
ctx context.Context
zlog *zap.Logger
getABIFunc func() *eos.ABI
}
func newTableDeltaHandler(ctx context.Context, msg *wsmsg.GetTableRows, emitter Emitter, zlog *zap.Logger, getABIFunc func() *eos.ABI) *tableDeltaHandler {
return &tableDeltaHandler{msg: msg, emitter: emitter, ctx: ctx, zlog: zlog, getABIFunc: getABIFunc}
}
func (h *tableDeltaHandler) ProcessBlock(block *bstream.Block, obj interface{}) error {
fObj := obj.(*forkable.ForkableObject)
if fObj.Step == forkable.StepNew || fObj.Step == forkable.StepUndo || fObj.Step == forkable.StepRedo {
abi := h.getABIFunc()
if abi == nil {
return fmt.Errorf("expected a none nil abi")
}
deltas := tableDeltasFromBlock(block, h.msg, h.getABIFunc(), fObj.Step, h.zlog)
for _, d := range deltas {
metrics.DocumentResponseCounter.Inc()
h.emitter.EmitReply(h.ctx, h.msg, d)
}
}
return nil
}