-
Notifications
You must be signed in to change notification settings - Fork 17
/
multiepoch-getBlock.go
537 lines (498 loc) · 15.9 KB
/
multiepoch-getBlock.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
package main
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
"runtime"
"sort"
"sync"
"github.com/gagliardetto/solana-go"
"github.com/ipfs/go-cid"
"github.com/ipld/go-car/util"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
jsoniter "github.com/json-iterator/go"
"github.com/rpcpool/yellowstone-faithful/compactindexsized"
"github.com/rpcpool/yellowstone-faithful/ipld/ipldbindcode"
"github.com/rpcpool/yellowstone-faithful/slottools"
solanablockrewards "github.com/rpcpool/yellowstone-faithful/solana-block-rewards"
"github.com/rpcpool/yellowstone-faithful/tooling"
"github.com/sourcegraph/jsonrpc2"
"golang.org/x/sync/errgroup"
"k8s.io/klog/v2"
)
var fasterJson = jsoniter.ConfigCompatibleWithStandardLibrary
type MyContextKey string
const requestIDKey = MyContextKey("requestID")
func setRequestIDToContext(ctx context.Context, id string) context.Context {
return context.WithValue(ctx, requestIDKey, id)
}
func getRequestIDFromContext(ctx context.Context) string {
id, ok := ctx.Value(requestIDKey).(string)
if !ok {
return ""
}
return id
}
func (multi *MultiEpoch) handleGetBlock(ctx context.Context, conn *requestContext, req *jsonrpc2.Request) (*jsonrpc2.Error, error) {
tim := newTimer(getRequestIDFromContext(ctx))
params, err := parseGetBlockRequest(req.Params)
if err != nil {
return &jsonrpc2.Error{
Code: jsonrpc2.CodeInvalidParams,
Message: "Invalid params",
}, fmt.Errorf("failed to parse params: %w", err)
}
if err := params.Validate(); err != nil {
return &jsonrpc2.Error{
Code: jsonrpc2.CodeInvalidParams,
Message: err.Error(),
}, fmt.Errorf("failed to validate params: %w", err)
}
tim.time("parseGetBlockRequest")
slot := params.Slot
// find the epoch that contains the requested slot
epochNumber := slottools.CalcEpochForSlot(slot)
epochHandler, err := multi.GetEpoch(epochNumber)
if err != nil {
return &jsonrpc2.Error{
Code: CodeNotFound,
Message: fmt.Sprintf("Epoch %d is not available", epochNumber),
}, fmt.Errorf("failed to get epoch %d: %w", epochNumber, err)
}
block, blockCid, err := epochHandler.GetBlock(WithSubrapghPrefetch(ctx, true), slot)
if err != nil {
if errors.Is(err, compactindexsized.ErrNotFound) {
return &jsonrpc2.Error{
Code: CodeNotFound,
Message: fmt.Sprintf("Slot %d was skipped, or missing in long-term storage", slot),
}, err
} else {
return &jsonrpc2.Error{
Code: jsonrpc2.CodeInternalError,
Message: "Failed to get block",
}, fmt.Errorf("failed to get block: %w", err)
}
}
// set the headers:
{
conn.ctx.Response.Header.Set("DAG-Root-CID", blockCid.String())
}
tim.time("GetBlock")
{
prefetcherFromCar := func() error {
parentIsInPreviousEpoch := slottools.CalcEpochForSlot(uint64(block.Meta.Parent_slot)) != slottools.CalcEpochForSlot(slot)
if slot == 0 {
parentIsInPreviousEpoch = true
}
if slot > 1 && block.Meta.Parent_slot == 0 {
parentIsInPreviousEpoch = true
}
var blockCid, parentBlockCid cid.Cid
wg := new(errgroup.Group)
wg.Go(func() (err error) {
blockCid, err = epochHandler.FindCidFromSlot(ctx, slot)
if err != nil {
return err
}
return nil
})
wg.Go(func() (err error) {
if parentIsInPreviousEpoch {
return nil
}
parentBlockCid, err = epochHandler.FindCidFromSlot(ctx, uint64(block.Meta.Parent_slot))
if err != nil {
return err
}
return nil
})
err = wg.Wait()
if err != nil {
return err
}
if slot == 0 {
klog.V(4).Infof("car start to slot(0)::%s", blockCid)
} else {
klog.V(4).Infof(
"slot(%d)::%s to slot(%d)::%s",
uint64(block.Meta.Parent_slot),
parentBlockCid,
slot,
blockCid,
)
}
{
var blockOffset, parentOffset uint64
wg := new(errgroup.Group)
wg.Go(func() (err error) {
offsetAndSize, err := epochHandler.FindOffsetAndSizeFromCid(ctx, blockCid)
if err != nil {
return err
}
blockOffset = offsetAndSize.Offset
return nil
})
wg.Go(func() (err error) {
if parentIsInPreviousEpoch {
// get car file header size
parentOffset = epochHandler.carHeaderSize
return nil
}
offsetAndSize, err := epochHandler.FindOffsetAndSizeFromCid(ctx, parentBlockCid)
if err != nil {
return err
}
parentOffset = offsetAndSize.Offset
return nil
})
err = wg.Wait()
if err != nil {
return err
}
length := blockOffset - parentOffset
MiB := uint64(1024 * 1024)
maxPrefetchSize := MiB * 10 // let's cap prefetching size
if length > maxPrefetchSize {
length = maxPrefetchSize
}
start := parentOffset
klog.V(4).Infof("prefetching CAR: start=%d length=%d (parent_offset=%d)", start, length, parentOffset)
carSection, err := epochHandler.ReadAtFromCar(ctx, start, length)
if err != nil {
return err
}
dr := bytes.NewReader(carSection)
br := bufio.NewReader(dr)
gotCid, data, err := util.ReadNode(br)
if err != nil {
return fmt.Errorf("failed to read first node: %w", err)
}
if !parentIsInPreviousEpoch && !gotCid.Equals(parentBlockCid) {
return fmt.Errorf("CID mismatch: expected %s, got %s", parentBlockCid, gotCid)
}
epochHandler.GetCache().PutRawCarObject(gotCid, data)
for {
gotCid, data, err = util.ReadNode(br)
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return fmt.Errorf("failed to read node: %w", err)
}
if gotCid.Equals(blockCid) {
break
}
epochHandler.GetCache().PutRawCarObject(gotCid, data)
}
}
return nil
}
if epochHandler.lassieFetcher == nil {
err := prefetcherFromCar()
if err != nil {
klog.Errorf("failed to prefetch from car: %v", err)
}
}
}
blocktime := uint64(block.Meta.Blocktime)
allTransactionNodes := make([][]*ipldbindcode.Transaction, len(block.Entries))
mu := &sync.Mutex{}
var lastEntryHash solana.Hash
{
wg := new(errgroup.Group)
wg.SetLimit(runtime.NumCPU() * 2)
// get entries from the block
for entryIndex, entry := range block.Entries {
entryIndex := entryIndex
entryCid := entry.(cidlink.Link).Cid
wg.Go(func() error {
// get the entry by CID
entryNode, err := epochHandler.GetEntryByCid(ctx, entryCid)
if err != nil {
klog.Errorf("failed to decode Entry: %v", err)
return err
}
if entryIndex == len(block.Entries)-1 {
lastEntryHash = solana.HashFromBytes(entryNode.Hash)
}
twg := new(errgroup.Group)
twg.SetLimit(runtime.NumCPU())
// get the transactions from the entry
allTransactionNodes[entryIndex] = make([]*ipldbindcode.Transaction, len(entryNode.Transactions))
for txI := range entryNode.Transactions {
txI := txI
tx := entryNode.Transactions[txI]
twg.Go(func() error {
// get the transaction by CID
tcid := tx.(cidlink.Link).Cid
txNode, err := epochHandler.GetTransactionByCid(ctx, tcid)
if err != nil {
klog.Errorf("failed to decode Transaction %s: %v", tcid, err)
return nil
}
mu.Lock()
allTransactionNodes[entryIndex][txI] = txNode
mu.Unlock()
return nil
})
}
return twg.Wait()
})
}
err = wg.Wait()
if err != nil {
return &jsonrpc2.Error{
Code: jsonrpc2.CodeInternalError,
Message: "Internal error",
}, fmt.Errorf("failed to get entries: %v", err)
}
}
tim.time("get entries")
var allTransactions []GetTransactionResponse
var rewards any
hasRewards := !block.Rewards.(cidlink.Link).Cid.Equals(DummyCID)
if *params.Options.Rewards && hasRewards {
rewardsNode, err := epochHandler.GetRewardsByCid(ctx, block.Rewards.(cidlink.Link).Cid)
if err != nil {
return &jsonrpc2.Error{
Code: jsonrpc2.CodeInternalError,
Message: "Internal error",
}, fmt.Errorf("failed to decode Rewards: %v", err)
}
rewardsBuf, err := tooling.LoadDataFromDataFrames(&rewardsNode.Data, epochHandler.GetDataFrameByCid)
if err != nil {
return &jsonrpc2.Error{
Code: jsonrpc2.CodeInternalError,
Message: "Internal error",
}, fmt.Errorf("failed to load Rewards dataFrames: %v", err)
}
uncompressedRewards, err := tooling.DecompressZstd(rewardsBuf)
if err != nil {
return &jsonrpc2.Error{
Code: jsonrpc2.CodeInternalError,
Message: "Internal error",
}, fmt.Errorf("failed to decompress Rewards: %v", err)
}
// try decoding as protobuf
actualRewards, err := solanablockrewards.ParseRewards(uncompressedRewards)
if err != nil {
// TODO: add support for legacy rewards format
fmt.Println("Rewards are not protobuf: " + err.Error())
} else {
{
// encode rewards as JSON, then decode it as a map
buf, err := fasterJson.Marshal(actualRewards)
if err != nil {
return &jsonrpc2.Error{
Code: jsonrpc2.CodeInternalError,
Message: "Internal error",
}, fmt.Errorf("failed to encode rewards: %v", err)
}
var m map[string]any
err = fasterJson.Unmarshal(buf, &m)
if err != nil {
return &jsonrpc2.Error{
Code: jsonrpc2.CodeInternalError,
Message: "Internal error",
}, fmt.Errorf("failed to decode rewards: %v", err)
}
if _, ok := m["rewards"]; ok {
// iter over rewards as an array of maps, and add a "commission" field to each = nil
rewardsAsArray := m["rewards"].([]any)
for _, reward := range rewardsAsArray {
rewardAsMap := reward.(map[string]any)
if _, ok := rewardAsMap["commission"]; !ok {
rewardAsMap["commission"] = nil
}
// if the commission field is a string, convert it to a float
if asString, ok := rewardAsMap["commission"].(string); ok {
rewardAsMap["commission"] = asFloat(asString)
}
// if no lamports field, add it and set it to 0
if _, ok := rewardAsMap["lamports"]; !ok {
rewardAsMap["lamports"] = uint64(0)
}
// if it has a post_balance field, convert it to postBalance
if _, ok := rewardAsMap["post_balance"]; ok {
rewardAsMap["postBalance"] = rewardAsMap["post_balance"]
delete(rewardAsMap, "post_balance")
}
// if it has a reward_type field, convert it to rewardType
if _, ok := rewardAsMap["reward_type"]; ok {
rewardAsMap["rewardType"] = rewardAsMap["reward_type"]
delete(rewardAsMap, "reward_type")
// if it's a float, convert to int and use rentTypeToString
if asFloat, ok := rewardAsMap["rewardType"].(float64); ok {
rewardAsMap["rewardType"] = rewardTypeToString(int(asFloat))
}
}
}
rewards = rewardsAsArray
// sort.Slice(rewardsAsArray, func(i, j int) bool {
// // sort by rewardType, then by pubkey
// if rewardTypeStringToInt(rewardsAsArray[i].(map[string]any)["rewardType"].(string)) != rewardTypeStringToInt(rewardsAsArray[j].(map[string]any)["rewardType"].(string)) {
// return rewardTypeStringToInt(rewardsAsArray[i].(map[string]any)["rewardType"].(string)) > rewardTypeStringToInt(rewardsAsArray[j].(map[string]any)["rewardType"].(string))
// }
// return bytes.Compare(solana.MPK(rewardsAsArray[i].(map[string]any)["pubkey"].(string)).Bytes(), solana.MPK(rewardsAsArray[j].(map[string]any)["pubkey"].(string)).Bytes()) < 0
// })
} else {
klog.Errorf("did not find rewards field in rewards")
rewards = make([]any, 0)
}
}
}
} else {
rewards = make([]any, 0)
}
tim.time("get rewards")
{
for _, transactionNode := range mergeTxNodeSlices(allTransactionNodes) {
var txResp GetTransactionResponse
// response.Slot = uint64(transactionNode.Slot)
// if blocktime != 0 {
// response.Blocktime = &blocktime
// }
{
pos, ok := transactionNode.GetPositionIndex()
if ok {
txResp.Position = uint64(pos)
}
tx, meta, err := parseTransactionAndMetaFromNode(transactionNode, epochHandler.GetDataFrameByCid)
if err != nil {
return &jsonrpc2.Error{
Code: jsonrpc2.CodeInternalError,
Message: "Internal error",
}, fmt.Errorf("failed to decode transaction: %v", err)
}
txResp.Signatures = tx.Signatures
if tx.Message.IsVersioned() {
txResp.Version = tx.Message.GetVersion() - 1
} else {
txResp.Version = "legacy"
}
encodedTx, encodedMeta, err := encodeTransactionResponseBasedOnWantedEncoding(*params.Options.Encoding, tx, meta)
if err != nil {
return &jsonrpc2.Error{
Code: jsonrpc2.CodeInternalError,
Message: "Internal error",
}, fmt.Errorf("failed to encode transaction: %v", err)
}
txResp.Transaction = encodedTx
txResp.Meta = encodedMeta
}
allTransactions = append(allTransactions, txResp)
}
}
sort.Slice(allTransactions, func(i, j int) bool {
return allTransactions[i].Position < allTransactions[j].Position
})
tim.time("get transactions")
var blockResp GetBlockResponse
blockResp.Transactions = allTransactions
if blocktime != 0 {
blockResp.BlockTime = &blocktime
}
blockResp.Blockhash = lastEntryHash.String()
blockResp.ParentSlot = uint64(block.Meta.Parent_slot)
blockResp.Rewards = rewards
if slot == 0 {
genesis := epochHandler.GetGenesis()
if genesis != nil {
blockZeroBlocktime := uint64(genesis.Config.CreationTime.Unix())
blockResp.BlockTime = &blockZeroBlocktime
}
blockResp.ParentSlot = uint64(0)
zeroBlockHeight := uint64(0)
blockResp.BlockHeight = &zeroBlockHeight
blockZeroBlockHash := lastEntryHash.String()
blockResp.PreviousBlockhash = &blockZeroBlockHash // NOTE: this is what solana RPC does. Should it be nil instead? Or should it be the genesis hash?
}
{
blockHeight, ok := block.GetBlockHeight()
if ok {
blockResp.BlockHeight = &blockHeight
}
}
{
// get parent slot
parentSlot := uint64(block.Meta.Parent_slot)
if (parentSlot != 0 || slot == 1) && slottools.CalcEpochForSlot(parentSlot) == epochNumber {
// NOTE: if the parent is in the same epoch, we can get it from the same epoch handler as the block;
// otherwise, we need to get it from the previous epoch (TODO: implement this)
parentBlock, _, err := epochHandler.GetBlock(WithSubrapghPrefetch(ctx, false), parentSlot)
if err != nil {
return &jsonrpc2.Error{
Code: jsonrpc2.CodeInternalError,
Message: "Internal error",
}, fmt.Errorf("failed to get/decode block: %v", err)
}
if len(parentBlock.Entries) > 0 {
lastEntryCidOfParent := parentBlock.Entries[len(parentBlock.Entries)-1]
parentEntryNode, err := epochHandler.GetEntryByCid(ctx, lastEntryCidOfParent.(cidlink.Link).Cid)
if err != nil {
return &jsonrpc2.Error{
Code: jsonrpc2.CodeInternalError,
Message: "Internal error",
}, fmt.Errorf("failed to decode Entry: %v", err)
}
parentEntryHash := solana.HashFromBytes(parentEntryNode.Hash).String()
blockResp.PreviousBlockhash = &parentEntryHash
}
} else {
if slot != 0 {
klog.V(4).Infof("parent slot is in a different epoch, not implemented yet (can't get previousBlockhash)")
}
}
}
tim.time("get parent block")
{
if len(blockResp.Transactions) == 0 {
blockResp.Transactions = make([]GetTransactionResponse, 0)
}
if blockResp.Rewards == nil || len(blockResp.Rewards.([]any)) == 0 {
blockResp.Rewards = make([]any, 0)
}
}
err = conn.Reply(
ctx,
req.ID,
blockResp,
func(m map[string]any) map[string]any {
transactions, ok := m["transactions"].([]any)
if !ok {
return m
}
for i := range transactions {
transaction, ok := transactions[i].(map[string]any)
if !ok {
continue
}
transactions[i] = adaptTransactionMetaToExpectedOutput(transaction)
}
return m
},
)
tim.time("reply")
if err != nil {
return nil, fmt.Errorf("failed to reply: %w", err)
}
return nil, nil
}
func asFloat(s string) float64 {
var f float64
_, err := fmt.Sscanf(s, "%f", &f)
if err != nil {
panic(err)
}
return f
}
func mergeTxNodeSlices(slices [][]*ipldbindcode.Transaction) []*ipldbindcode.Transaction {
var out []*ipldbindcode.Transaction
for _, slice := range slices {
out = append(out, slice...)
}
return out
}