-
Notifications
You must be signed in to change notification settings - Fork 17
/
multiepoch-getBlockTime.go
60 lines (56 loc) · 1.61 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"
"fmt"
"github.com/rpcpool/yellowstone-faithful/slottools"
"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 := slottools.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)
}
{
blocktimeIndex := epochHandler.GetBlocktimeIndex()
if blocktimeIndex != nil {
blockTime, err := blocktimeIndex.Get(blockNum)
if err != nil {
return &jsonrpc2.Error{
Code: CodeNotFound,
Message: fmt.Sprintf("Slot %d was skipped, or missing in long-term storage", blockNum),
}, fmt.Errorf("failed to get blocktime: %w", err)
}
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
} else {
return &jsonrpc2.Error{
Code: jsonrpc2.CodeInternalError,
Message: "Failed to get block",
}, fmt.Errorf("failed to get blocktime: blocktime index is nil")
}
}
}