forked from dfuse-io/dkafka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabidecoder.go
302 lines (268 loc) · 8.29 KB
/
abidecoder.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
package dkafka
import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"math"
"os"
"strconv"
"strings"
pbabicodec "github.com/dfuse-io/dfuse-eosio/pb/dfuse/eosio/abicodec/v1"
pbcodec "github.com/dfuse-io/dfuse-eosio/pb/dfuse/eosio/codec/v1"
"github.com/eoscanada/eos-go"
pbbstream "github.com/streamingfast/pbgo/dfuse/bstream/v1"
"go.uber.org/zap"
)
type ABI struct {
*eos.ABI
AbiBlockNum uint32
}
type ABICodec interface {
IsNOOP() bool
DecodeDBOp(in *pbcodec.DBOp, blockNum uint32) (*decodedDBOp, error)
GetCodec(name string, blockNum uint32) (Codec, error)
UpdateABI(blockNum uint32, step pbbstream.ForkStep, trxID string, actionTrace *pbcodec.ActionTrace) error
}
type JsonABICodec struct {
*ABIDecoder
codec Codec
account string
}
func (c *JsonABICodec) GetCodec(name string, blockNum uint32) (Codec, error) {
return c.codec, nil
}
func (c *JsonABICodec) UpdateABI(_ uint32, _ pbbstream.ForkStep, _ string, _ *pbcodec.ActionTrace) error {
return nil
}
func NewJsonABICodec(
decoder *ABIDecoder,
account string,
) ABICodec {
return &JsonABICodec{
decoder,
NewJSONCodec(),
account,
}
}
// MessageSchemaSupplier is a function that return the specific message schema
// of a given entity (i.e. table or action)
type MessageSchemaSupplier = func(string, *ABI) (MessageSchema, error)
// ABIDecoder legacy abi codec does not support schema registry
type ABIDecoder struct {
overrides map[string]*ABI
abiCodecCli pbabicodec.DecoderClient
abisCache map[string]*ABI
context context.Context
}
func (a *ABIDecoder) IsNOOP() bool {
return a.overrides == nil && a.abiCodecCli == nil
}
func ParseABIFileSpecs(specs []string) (abiFileSpecs map[string]string, err error) {
abiFileSpecs = make(map[string]string)
for _, ext := range specs {
account, abiPath, err := ParseABIFileSpec(ext)
if err != nil {
break
}
abiFileSpecs[account] = abiPath
}
return
}
func ParseABIFileSpec(spec string) (account string, abiPath string, err error) {
kv := strings.SplitN(spec, ":", 2)
if len(kv) != 2 {
err = fmt.Errorf("invalid value for local ABI file: %s", spec)
} else {
account = kv[0]
abiPath = kv[1]
}
return
}
// LoadABIFiles will load ABIs for different accounts from JSON files
func LoadABIFiles(abiFiles map[string]string) (map[string]*ABI, error) {
out := make(map[string]*ABI)
for contract, abiFile := range abiFiles {
abi, err := LoadABIFile(abiFile)
if err != nil {
return nil, fmt.Errorf("reading abi file %s: %w", abiFile, err)
}
out[contract] = abi
}
return out, nil
}
func LoadABIFile(abiFile string) (*ABI, error) {
kv := strings.SplitN(abiFile, ":", 2) //[abiFilePath] - [abiFilePath, abiNumber]
var abiPath = abiFile
var abiBlockNum = uint64(0)
if len(kv) == 2 {
abiPath = kv[0]
var err error
abiBlockNum, err = strconv.ParseUint(kv[1], 10, 32)
if err != nil {
return nil, err
}
}
f, err := os.Open(abiPath)
if err == nil {
defer f.Close()
eosAbi, err := eos.NewABI(f)
abi := &ABI{eosAbi, uint32(abiBlockNum)}
return abi, err
}
return nil, err
}
func NewABIDecoder(
overrides map[string]*ABI,
abiCodecCli pbabicodec.DecoderClient,
context context.Context,
) *ABIDecoder {
return &ABIDecoder{
overrides: overrides,
abiCodecCli: abiCodecCli,
abisCache: make(map[string]*ABI),
context: context,
}
}
type decodedDBOp struct {
*pbcodec.DBOp
NewJSON map[string]interface{} `json:"new_json,omitempty"`
OldJSON map[string]interface{} `json:"old_json,omitempty"`
}
func (dbOp *decodedDBOp) asMap(dbOpRecordName string, dbOpIndex int) map[string]interface{} {
asMap := newDBOpBasic(dbOp.DBOp, dbOpIndex)
addOptional(&asMap, "old_json", dbOp.OldJSON)
addOptional(&asMap, "new_json", dbOp.NewJSON)
// addOptionalRecord(&asMap, "old_json", dbOpRecordName, dbOp.OldJSON)
// addOptionalRecord(&asMap, "new_json", dbOpRecordName, dbOp.NewJSON)
return asMap
}
func addOptional(m *map[string]interface{}, key string, value map[string]interface{}) {
if len(value) > 0 {
(*m)[key] = value
}
}
// func addOptionalRecord(m *map[string]interface{}, key string, rType string, value map[string]interface{}) {
// if len(value) > 0 {
// addOptional(m, key, map[string]interface{}{rType: value})
// }
// }
func (a *ABIDecoder) abi(contract string, blockNum uint32, forceRefresh bool) (*ABI, error) {
if a.overrides != nil {
if abi, ok := a.overrides[contract]; ok {
return abi, nil
}
}
if a.abiCodecCli == nil {
return nil, fmt.Errorf("unable to get abi for contract %q", contract)
}
if !forceRefresh {
if abiObj, ok := a.abisCache[contract]; ok {
if abiObj.AbiBlockNum < blockNum {
return abiObj, nil
}
}
}
zlog.Info("ABIDecoder.abi(...) => call onReload()", zap.String("contract", contract), zap.Uint32("block_num", blockNum), zap.Bool("force_refresh", forceRefresh))
resp, err := a.abiCodecCli.GetAbi(a.context, &pbabicodec.GetAbiRequest{
Account: contract,
AtBlockNum: blockNum,
})
if err != nil {
return nil, fmt.Errorf("unable to get abi for contract %q: %w", contract, err)
}
var eosAbi *eos.ABI
err = json.Unmarshal([]byte(resp.JsonPayload), &eosAbi)
if err != nil {
return nil, fmt.Errorf("unable to decode abi for contract %q: %w", contract, err)
}
var abi = ABI{eosAbi, resp.AbiBlockNum}
zlog.Info("new ABI loaded", zap.String("contract", contract), zap.Uint32("block_num", blockNum), zap.Uint32("abi_block_num", abi.AbiBlockNum))
// store abi in cache for late uses
a.abisCache[contract] = &abi
return &abi, nil
}
func (a *ABIDecoder) decodeDBOp(op *decodedDBOp, blockNum uint32, forceRefresh bool) error {
abi, err := a.abi(op.Code, blockNum, forceRefresh)
if err != nil {
return fmt.Errorf("decoding dbop in block %d: %w", blockNum, err)
}
tableDef := abi.TableForName(eos.TableName(op.TableName))
if tableDef == nil {
return fmt.Errorf("table %s not present in ABI for contract %s", op.TableName, op.Code)
}
if len(op.NewData) > 0 {
asMap, err := abi.DecodeTableRowTypedNative(tableDef.Type, op.NewData)
if err != nil {
return fmt.Errorf("decode row: %w", err)
}
op.NewJSON = asMap
}
if len(op.OldData) > 0 {
asMap, err := abi.DecodeTableRowTypedNative(tableDef.Type, op.OldData)
if err != nil {
return fmt.Errorf("decode row: %w", err)
}
op.OldJSON = asMap
}
return nil
}
func (a *ABIDecoder) DecodeDBOp(in *pbcodec.DBOp, blockNum uint32) (decoded *decodedDBOp, err error) {
decoded = &decodedDBOp{DBOp: in}
err = a.decodeDBOp(decoded, blockNum, false)
if err != nil {
err = a.decodeDBOp(decoded, blockNum, true) //force refreshing ABI from cache
}
return
}
func (a *ABIDecoder) DecodeDBOps(in []*pbcodec.DBOp, blockNum uint32) (decodedDBOps []*decodedDBOp, err error) {
for _, op := range in {
decoded := &decodedDBOp{DBOp: op}
decodedDBOps = append(decodedDBOps, decoded)
}
if a.IsNOOP() {
return
}
var errors []error
for _, op := range decodedDBOps {
err := a.decodeDBOp(op, blockNum, false)
if err != nil {
err = a.decodeDBOp(op, blockNum, true) //force refreshing ABI from cache
}
if err != nil {
errors = append(errors, err)
}
}
if len(errors) > 0 {
errorStr := ""
for _, e := range errors {
errorStr = fmt.Sprintf("%s; %s", errorStr, e.Error())
}
err = fmt.Errorf(errorStr)
}
return
}
func DecodeABI(trxID string, account string, hexData string) (abi *eos.ABI, err error) {
if hexData == "" {
zlog.Warn("empty ABI in 'setabi' action", zap.String("account", account), zap.String("transaction_id", trxID))
return nil, fmt.Errorf("empty ABI in 'setabi' action, account: %s, trx: %s", account, trxID)
}
abiData, err := hex.DecodeString(hexData)
if err != nil {
zlog.Error("failed to hex decode abi string", zap.String("account", account), zap.String("transaction_id", trxID), zap.Error(err))
return nil, fmt.Errorf("failed to hex decode abi string, account: %s, trx: %s, error: %w", account, trxID, err)
}
err = eos.UnmarshalBinary(abiData, &abi)
if err != nil {
abiHexCutAt := math.Min(50, float64(len(hexData)))
zlog.Error("failed to unmarshal abi from binary",
zap.String("account", account),
zap.String("transaction_id", trxID),
zap.String("abi_hex_prefix", hexData[0:int(abiHexCutAt)]),
zap.Error(err),
)
return nil, fmt.Errorf("failed to unmarshal abi from binary, account: %s, trx: %s, error: %w", account, trxID, err)
}
zlog.Debug("setting new abi", zap.String("account", account), zap.String("transaction_id", trxID))
return
}