forked from qtumproject/janus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eth_getBlockByNumber.go
97 lines (86 loc) · 2.83 KB
/
eth_getBlockByNumber.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
package transformer
import (
"context"
"math/big"
"github.com/kaonone/eth-rpc-gate/pkg/eth"
"github.com/kaonone/eth-rpc-gate/pkg/kaon"
"github.com/labstack/echo"
)
// ProxyETHGetBlockByNumber implements ETHProxy
type ProxyETHGetBlockByNumber struct {
*kaon.Kaon
}
func (p *ProxyETHGetBlockByNumber) Method() string {
return "eth_getBlockByNumber"
}
func (p *ProxyETHGetBlockByNumber) Request(rpcReq *eth.JSONRPCRequest, c echo.Context) (interface{}, *eth.JSONRPCError) {
req := new(eth.GetBlockByNumberRequest)
if err := unmarshalRequest(rpcReq.Params, req); err != nil {
// TODO: Correct error code?
return nil, eth.NewInvalidParamsError(err.Error())
}
return p.request(c.Request().Context(), req)
}
func (p *ProxyETHGetBlockByNumber) request(ctx context.Context, req *eth.GetBlockByNumberRequest) (*eth.GetBlockByNumberResponse, *eth.JSONRPCError) {
blockNum, err := getBlockNumberByRawParam(ctx, p.Kaon, req.BlockNumber, false)
if err != nil {
return nil, eth.NewCallbackError("couldn't get block number by parameter")
}
blockHash, jsonErr := proxyETHGetBlockByHash(ctx, p, p.Kaon, blockNum)
if jsonErr != nil {
return nil, eth.NewInvalidParamsError(jsonErr.Message())
}
if blockHash == nil {
return nil, nil
}
var (
getBlockByHashReq = ð.GetBlockByHashRequest{
BlockHash: string(*blockHash),
FullTransaction: req.FullTransaction,
}
proxy = &ProxyETHGetBlockByHash{Kaon: p.Kaon}
)
block, jsonErr := proxy.request(ctx, getBlockByHashReq)
if jsonErr != nil {
p.GetDebugLogger().Log("function", p.Method(), "msg", "couldn't get block by hash", "jsonErr", jsonErr.Message())
return nil, eth.NewCallbackError("couldn't get block by hash")
}
if blockNum != nil {
p.GetDebugLogger().Log("function", p.Method(), "request", string(req.BlockNumber), "msg", "Successfully got block by number", "result", blockNum.String())
}
return block, nil
}
func proxyETHGetBlockByHash(ctx context.Context, p ETHProxy, q *kaon.Kaon, blockNum *big.Int) (*kaon.GetBlockHashResponse, *eth.JSONRPCError) {
// Attempt to get the block hash from Kaon
resp, err := q.GetBlockHash(ctx, blockNum)
if err != nil {
// Handle specific known errors
if err == kaon.ErrInvalidParameter {
// block doesn't exist; return null as per ETH RPC spec
/**
{
"jsonrpc": "2.0",
"id": 1234,
"result": null
}
**/
q.GetDebugLogger().Log(
"function", p.Method(),
"request", blockNum.String(),
"msg", "Unknown block",
"error", err.Error(),
)
return nil, nil
}
// Catch-all for any other unknown errors
q.GetDebugLogger().Log(
"function", p.Method(),
"request", blockNum.String(),
"msg", "Unexpected error occurred while getting block hash",
"error", err.Error(),
)
return nil, eth.NewCallbackError("unexpected error: " + err.Error())
}
// Successfully retrieved block hash
return &resp, nil
}