-
Notifications
You must be signed in to change notification settings - Fork 17
/
multiepoch-getBlockTime.go
60 lines (55 loc) · 1.57 KB
/
multiepoch-getBlockTime.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
package main
import (
"context"
"errors"
"fmt"
"github.com/rpcpool/yellowstone-faithful/compactindexsized"
"github.com/sourcegraph/jsonrpc2"
)
func (multi *MultiEpoch) handleGetBlockTime(ctx context.Context, conn *requestContext, req *jsonrpc2.Request) (*jsonrpc2.Error, error) {
blockNum, err := parseGetBlockTimeRequest(req.Params)
if err != nil {
return &jsonrpc2.Error{
Code: jsonrpc2.CodeInvalidParams,
Message: "Invalid params",
}, fmt.Errorf("failed to parse params: %w", err)
}
// find the epoch that contains the requested slot
epochNumber := CalcEpochForSlot(blockNum)
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, _, err := epochHandler.GetBlock(WithSubrapghPrefetch(ctx, false), blockNum)
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", blockNum),
}, err
} else {
return &jsonrpc2.Error{
Code: jsonrpc2.CodeInternalError,
Message: "Failed to get block",
}, fmt.Errorf("failed to get block: %w", err)
}
}
blockTime := uint64(block.Meta.Blocktime)
err = conn.ReplyRaw(
ctx,
req.ID,
func() any {
if blockTime != 0 {
return blockTime
}
return nil
}(),
)
if err != nil {
return nil, fmt.Errorf("failed to reply: %w", err)
}
return nil, nil
}