forked from dcb9/janus
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy patheth_getBlockByNumber.go
81 lines (72 loc) · 2.41 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
package transformer
import (
"context"
"math/big"
"github.com/labstack/echo"
"github.com/qtumproject/janus/pkg/eth"
"github.com/qtumproject/janus/pkg/qtum"
)
// ProxyETHGetBlockByNumber implements ETHProxy
type ProxyETHGetBlockByNumber struct {
*qtum.Qtum
}
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.Qtum, req.BlockNumber, false)
if err != nil {
return nil, eth.NewCallbackError("couldn't get block number by parameter")
}
blockHash, jsonErr := proxyETHGetBlockByHash(ctx, p, p.Qtum, blockNum)
if jsonErr != nil {
return nil, jsonErr
}
if blockHash == nil {
return nil, nil
}
var (
getBlockByHashReq = ð.GetBlockByHashRequest{
BlockHash: string(*blockHash),
FullTransaction: req.FullTransaction,
}
proxy = &ProxyETHGetBlockByHash{Qtum: p.Qtum}
)
block, jsonErr := proxy.request(ctx, getBlockByHashReq)
if jsonErr != nil {
p.GetDebugLogger().Log("function", p.Method(), "msg", "couldn't get block by hash", "err", err)
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
}
// Properly handle unknown blocks
func proxyETHGetBlockByHash(ctx context.Context, p ETHProxy, q *qtum.Qtum, blockNum *big.Int) (*qtum.GetBlockHashResponse, eth.JSONRPCError) {
resp, err := q.GetBlockHash(ctx, blockNum)
if err != nil {
if err == qtum.ErrInvalidParameter {
// block doesn't exist, ETH rpc returns null
/**
{
"jsonrpc": "2.0",
"id": 1234,
"result": null
}
**/
q.GetDebugLogger().Log("function", p.Method(), "request", blockNum.String(), "msg", "Unknown block")
return nil, nil
}
return nil, eth.NewCallbackError("couldn't get block hash")
}
return &resp, nil
}