-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathdb.go
489 lines (400 loc) · 13.5 KB
/
db.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// 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"
"io/ioutil"
"math"
"path/filepath"
"strconv"
"strings"
"github.com/streamingfast/bstream"
"github.com/dfuse-io/dfuse-eosio/eosws/mdl"
pbcodec "github.com/dfuse-io/dfuse-eosio/pb/dfuse/eosio/codec/v1"
"github.com/dfuse-io/dfuse-eosio/trxdb"
v1 "github.com/dfuse-io/eosws-go/mdl/v1"
"github.com/streamingfast/logging"
"github.com/eoscanada/eos-go"
"github.com/streamingfast/kvdb"
"github.com/streamingfast/opaque"
"go.uber.org/zap"
)
type DB interface {
trxdb.DBReader
GetTransactionWithExpectedBlockID(ctx context.Context, id string, expectedBlockID string) (*pbcodec.TransactionLifecycle, error)
GetTransaction(ctx context.Context, id string) (*pbcodec.TransactionLifecycle, error)
GetTransactions(ctx context.Context, ids []string) ([]*pbcodec.TransactionLifecycle, error)
ListTransactionsForBlockID(ctx context.Context, blockId string, startKey string, limit int) (*mdl.TransactionList, error)
ListMostRecentTransactions(ctx context.Context, startKey string, limit int) (*mdl.TransactionList, error)
}
// TRXDB
type TRXDB struct {
trxdb.DBReader
chainDiscriminator func(blockID string) bool
}
func NewTRXDB(dbReader trxdb.DBReader) *TRXDB {
return &TRXDB{
DBReader: dbReader,
// TODO: implement real discriminator
chainDiscriminator: func(blockID string) bool {
return true
},
}
}
func (db *TRXDB) GetTransactionWithExpectedBlockID(ctx context.Context, id string, expectedBlockID string) (out *pbcodec.TransactionLifecycle, err error) {
evs, err := db.GetTransactionEvents(ctx, id)
if err != nil && err != kvdb.ErrNotFound {
return nil, err
}
if len(evs) == 0 {
return nil, DBTrxNotFoundError(ctx, id)
}
seenBlockID := false
for _, ev := range evs {
if ev.BlockId == expectedBlockID {
seenBlockID = true
break
}
}
if !seenBlockID {
return nil, DBTrxNotFoundError(ctx, id)
}
out = pbcodec.MergeTransactionEvents(evs, db.chainDiscriminator)
return
}
func (db *TRXDB) GetTransaction(ctx context.Context, id string) (out *pbcodec.TransactionLifecycle, err error) {
evs, err := db.GetTransactionEvents(ctx, id)
if err != nil && err != kvdb.ErrNotFound {
return nil, err
}
if len(evs) == 0 {
return nil, DBTrxNotFoundError(ctx, id)
}
out = pbcodec.MergeTransactionEvents(evs, db.chainDiscriminator)
return
}
func (db *TRXDB) GetTransactions(ctx context.Context, ids []string) (out []*pbcodec.TransactionLifecycle, err error) {
evs, err := db.GetTransactionEventsBatch(ctx, ids)
if err != nil {
return nil, err
}
for idx, ev := range evs {
trxID := ids[idx]
if len(ev) == 0 {
return nil, DBTrxNotFoundError(ctx, trxID)
}
out = append(out, pbcodec.MergeTransactionEvents(ev, db.chainDiscriminator))
}
return
}
func (db *TRXDB) ListTransactionsForBlockID(ctx context.Context, blockID string, startKey string, limit int) (*mdl.TransactionList, error) {
if limit < 1 {
return &mdl.TransactionList{
Cursor: opaqueCursor(startKey),
}, nil
}
block, err := db.GetBlock(ctx, blockID)
if err != nil {
return nil, err
}
var startTrxIndex uint16
if startKey != "" {
startTrxIndex, err = kvdb.FromHexUint16(startKey)
if err != nil {
return nil, err
}
}
var trxIDs []string
var nextTrxIndex uint16
allRefsHashes := append(block.ImplicitTransactionRefs.Hashes, block.TransactionTraceRefs.Hashes...)
for trxIndex, trxIDBytes := range allRefsHashes {
trxID := hex.EncodeToString(trxIDBytes)
if uint16(trxIndex) < startTrxIndex {
continue
}
nextTrxIndex = uint16(trxIndex)
// We add 1 to the limit to have more ids than requested since when we will
// actually retrieved the trx down below, we account for the `onblock` trx
// that will be left out (if in the ids list).
if len(trxIDs) >= limit+1 {
break
}
trxIDs = append(trxIDs, trxID)
}
trxList, err := db.GetTransactionEventsBatch(ctx, trxIDs)
if err != nil {
return nil, err
}
var lifecycles []*v1.TransactionLifecycle
for _, evs := range trxList {
if len(evs) == 0 {
return nil, fmt.Errorf("transactions list for block ID: a transaction was not found")
}
lc, err := mdl.ToV1TransactionLifecycle(pbcodec.MergeTransactionEvents(evs, db.chainDiscriminator))
if err != nil {
return nil, fmt.Errorf("transactions list for block ID: %w", err)
}
lifecycles = append(lifecycles, lc)
}
upperBound := limit
if len(lifecycles) < upperBound {
upperBound = len(lifecycles)
}
return &mdl.TransactionList{
Cursor: opaqueCursor(kvdb.HexUint16(nextTrxIndex)),
Transactions: lifecycles[0:upperBound],
}, nil
}
func (db *TRXDB) ListMostRecentTransactions(ctx context.Context, startKey string, limit int) (*mdl.TransactionList, error) {
if limit < 1 {
return &mdl.TransactionList{
Cursor: opaqueCursor(startKey),
}, nil
}
fromCursorKey := func(startKey string) (blockID string, trxIndex uint16, err error) {
chunks := strings.Split(startKey, ":")
if len(chunks) != 2 {
err = fmt.Errorf("invalid cursor key %q, expected two chunks separated by ':'", startKey)
return
}
blockID = chunks[0]
rawTrxIndex, err := strconv.ParseUint(chunks[1], 16, 16)
if err != nil {
err = fmt.Errorf("parsing transaction index %q: %s", chunks[1], err)
}
trxIndex = uint16(rawTrxIndex)
return
}
//var nextPrefix string
var nextBlockNum uint32
nextBlockNum = math.MaxUint32
var startBlockID string
var startTrxIndex uint16
if startKey != "" {
blockID, trxIndex, err := fromCursorKey(startKey)
if err != nil {
return nil, err
}
nextBlockNum = eos.BlockNum(blockID)
startBlockID = blockID
startTrxIndex = trxIndex
}
// TODO: fetch ListBlocks(nextPrefix, limit) BlockWithRefs
// on a les
// On fetch seulement la colonne `trxs:executed-ids`
// De ceux-là, on prends les `limit` premiers, et on forge le `NextCursor` à partir soit du prochain index, ou du prochain
var list [][]*pbcodec.TransactionEvent
type seenData struct {
transactionIndex uint16
blockID string
}
seenTrx := make(map[string]*seenData)
seenBlock := make(map[string]bool)
for {
if len(list) >= limit+1 /* +1 for the next cursor */ {
break
}
const blockCount = 5
blks, err := db.ListBlocks(ctx, nextBlockNum, blockCount) // for a max of 5 forked blocks
if err != nil {
return nil, fmt.Errorf("list blocks: %s", err)
}
for _, blk := range blks {
if len(list) > limit+1 {
break
}
if seenBlock[blk.Id] {
break
}
seenBlock[blk.Id] = true
if !db.chainDiscriminator(blk.Id) {
continue
}
// Accumulate transactions once the transaction
var fetchTransactions []string
allRefsHashes := append(blk.ImplicitTransactionRefs.Hashes, blk.TransactionTraceRefs.Hashes...)
for trxIndex := len(allRefsHashes) - 1; trxIndex >= 0; trxIndex-- {
trxIDBytes := allRefsHashes[trxIndex]
trxID := hex.EncodeToString(trxIDBytes)
if blk.Id == startBlockID && trxIndex > int(startTrxIndex) /* fetch one more, to know the next cursor */ {
continue
}
if _, found := seenTrx[trxID]; !found {
// save here the `blockID` + `trxIndex
fetchTransactions = append(fetchTransactions, trxID)
seenTrx[trxID] = &seenData{
transactionIndex: uint16(trxIndex),
blockID: blk.Id,
}
}
}
// FIXME: optimize this, as we don't need to get ALL the transactions if we know we're going to need only 25..
responses, err := db.GetTransactionEventsBatch(ctx, fetchTransactions)
if err != nil {
return nil, err
}
for _, resp := range responses {
if len(resp) != 0 {
list = append(list, resp)
//} else {return nil, fmt.Errorf("transaction not found: %q", fetchTransactions[idx]) // soft failing only
}
}
nextBlockNum = eos.BlockNum(blk.Id) - 1
//nextPrefix = kvdb.ReversedBlockID(kvdb.IncreaseBlockIDSuffix(blk.Id))
}
if len(blks) < blockCount {
// reached tip of history
break
}
}
out := &mdl.TransactionList{}
if len(list) > limit {
trxRowAtBoundary := list[limit]
seenData := seenTrx[trxRowAtBoundary[0].Id]
if seenData == nil {
return nil, fmt.Errorf("hmm, we haven't seen this but we added it? %s", trxRowAtBoundary[0].Id)
}
out.Cursor = opaqueCursor(seenData.blockID + ":" + kvdb.HexUint16(seenData.transactionIndex))
}
var keep [][]*pbcodec.TransactionEvent
if len(list) < limit {
keep = list
} else {
keep = list[:limit]
}
for _, events := range keep {
lc, err := mdl.ToV1TransactionLifecycle(pbcodec.MergeTransactionEvents(events, db.chainDiscriminator))
if err != nil {
return nil, fmt.Errorf("most recent transactions: %w", err)
}
out.Transactions = append(out.Transactions, lc)
}
return out, nil
}
func (db *TRXDB) GetBlock(ctx context.Context, id string) (out *pbcodec.BlockWithRefs, err error) {
out, err = db.DBReader.GetBlock(ctx, id)
if err == eos.ErrNotFound {
return nil, DBBlockNotFoundError(ctx, id)
}
return
}
func (db *TRXDB) GetBlockByNum(ctx context.Context, num uint32) (out []*pbcodec.BlockWithRefs, err error) {
out, err = db.DBReader.GetBlockByNum(ctx, num)
if err == kvdb.ErrNotFound {
return nil, DBBlockNotFoundError(ctx, strconv.FormatUint(uint64(num), 10))
}
if err != nil {
logging.Logger(ctx, zlog).Error("cannot get blocks by number", zap.Uint32("block_num", num), zap.Error(err))
}
return
}
func (db *TRXDB) GetAccount(ctx context.Context, name string) (account *pbcodec.AccountCreationRef, err error) {
account, err = db.DBReader.GetAccount(ctx, name)
if err == kvdb.ErrNotFound {
return nil, DBAccountNotFoundError(ctx, name)
}
if err != nil {
logging.Logger(ctx, zlog).Error("cannot get account", zap.String("account_name", name), zap.Error(err))
}
return
}
// MOCK MOCK
type MockDB struct {
trxdb.TimelineExplorer
trxdb.TransactionsReader
path string
}
func NewMockDB(path string) *MockDB {
return &MockDB{path: path}
}
func (db *MockDB) GetClosestIrreversibleIDAtBlockNum(ctx context.Context, num uint32) (out bstream.BlockRef, err error) {
return bstream.NewBlockRef("123", 123), nil
}
func (db *MockDB) GetIrreversibleIDAtBlockID(ctx context.Context, ID string) (out bstream.BlockRef, err error) {
return bstream.NewBlockRef("123", 123), nil
}
func (db *MockDB) GetLastWrittenBlockID(ctx context.Context) (string, error) {
return "0123456700000000000000000000000000000000000000000000000000000000", nil
}
func (db *MockDB) GetTransactionWithExpectedBlockID(ctx context.Context, id string, _ string) (out *pbcodec.TransactionLifecycle, err error) {
return db.GetTransaction(ctx, id)
}
func (db *MockDB) GetTransaction(ctx context.Context, id string) (out *pbcodec.TransactionLifecycle, err error) {
filename := filepath.Join(db.path, "transactions", fmt.Sprintf("%s.json", id))
err = readFromFile(filename, out)
return
}
func (db *MockDB) GetTransactions(ctx context.Context, ids []string) (out []*pbcodec.TransactionLifecycle, err error) {
for _, id := range ids {
filename := filepath.Join(db.path, "transactions", fmt.Sprintf("%s.json", id))
var el *pbcodec.TransactionLifecycle
err = readFromFile(filename, el)
out = append(out, el)
}
return
}
func (db *MockDB) ListMostRecentTransactions(ctx context.Context, startKey string, limit int) (*mdl.TransactionList, error) {
panic("Implement me!")
}
func (db *MockDB) ListTransactionsForBlockID(ctx context.Context, blockId string, startKey string, limit int) (*mdl.TransactionList, error) {
panic("Implement me!")
}
func (db *MockDB) GetBlock(ctx context.Context, id string) (out *pbcodec.BlockWithRefs, err error) {
filename := filepath.Join(db.path, "blocks", fmt.Sprintf("%s.json", id))
err = readFromFile(filename, out)
return
}
func (db *MockDB) ListBlocks(ctx context.Context, startBlockNum uint32, limit int) ([]*pbcodec.BlockWithRefs, error) {
panic("implement me")
}
func (db *MockDB) ListSiblingBlocks(ctx context.Context, blockNum uint32, spread uint32) ([]*pbcodec.BlockWithRefs, error) {
panic("implement me")
}
func (db *MockDB) GetAccount(ctx context.Context, name string) (out *pbcodec.AccountCreationRef, err error) {
return &pbcodec.AccountCreationRef{
Account: "eoscanadacom",
Creator: "bozo",
}, nil
}
func (db *MockDB) ListAccountNames(ctx context.Context) (out []string, err error) {
return []string{"eoscanadacom"}, nil
}
func (db *MockDB) GetBlockByNum(ctx context.Context, num uint32) (out []*pbcodec.BlockWithRefs, err error) {
filename := filepath.Join(db.path, "blocks", fmt.Sprintf("%s.json", fmt.Sprintf("%d", num)))
err = readFromFile(filename, out)
return
}
func readFromFile(filename string, out interface{}) (err error) {
buffer, err := ioutil.ReadFile(filename)
if err != nil {
return
}
err = json.Unmarshal(buffer, &out)
if err != nil {
return fmt.Errorf("unmarshal: %s", err)
}
return
}
func opaqueCursor(key string) string {
// The implementation always returns nil! So it's ok to panic here, if it changes and there is an error,
// change the code throughtout.
out, err := opaque.ToOpaque(key)
if err != nil {
panic(fmt.Errorf("unable to transform key %q to opaque cursor: %w", key, err))
}
return out
}