|
9 | 9 | "github.com/sirupsen/logrus" |
10 | 10 | ) |
11 | 11 |
|
| 12 | +var ErrUnsupportedGetBlockQuery = errors.New("unsupported get block query") |
| 13 | + |
12 | 14 | type Handler struct { |
13 | 15 | log logrus.FieldLogger |
14 | 16 | Cfg Config |
@@ -49,63 +51,126 @@ func (h *Handler) Request(ctx context.Context, id int, method string, params []* |
49 | 51 | TerminalBlockNumber: h.Cfg.TerminalBlockNumber, |
50 | 52 | } |
51 | 53 | case "engine_forkchoiceUpdatedV1": |
52 | | - if len(params) < 1 || params[0] == nil { |
53 | | - return nil, errors.New("missing params") |
| 54 | + result, err := h.forkChoiceUpdated(params) |
| 55 | + if err != nil { |
| 56 | + return nil, err |
54 | 57 | } |
55 | 58 |
|
56 | | - var forkchoiceState RequestParamsForkchoiceUpdatedV1 |
57 | | - |
58 | | - err := json.Unmarshal([]byte(*params[0]), &forkchoiceState) |
| 59 | + resp.Result = result |
| 60 | + case "engine_forkchoiceUpdatedV2": |
| 61 | + result, err := h.forkChoiceUpdated(params) |
59 | 62 | if err != nil { |
60 | 63 | return nil, err |
61 | 64 | } |
62 | 65 |
|
63 | | - resp.Result = ResultForkchoiceUpdatedV1{ |
64 | | - PayloadStatus: ResultForkchoiceUpdatedV1PayloadStatus{ |
65 | | - Status: "VALID", |
66 | | - LatestValidHash: forkchoiceState.HeadBlockHash, |
67 | | - ValidationError: "", |
68 | | - }, |
69 | | - PayloadID: "0xa247243752eb10b4", |
70 | | - } |
| 66 | + resp.Result = result |
71 | 67 | case "engine_newPayloadV1": |
72 | | - if len(params) < 1 || params[0] == nil { |
73 | | - return nil, errors.New("missing params") |
| 68 | + result, err := h.newPayload(params) |
| 69 | + if err != nil { |
| 70 | + return nil, err |
74 | 71 | } |
75 | 72 |
|
76 | | - var payload RequestParamsNewPayloadV1 |
77 | | - |
78 | | - err := json.Unmarshal([]byte(*params[0]), &payload) |
| 73 | + resp.Result = result |
| 74 | + case "engine_newPayloadV2": |
| 75 | + result, err := h.newPayload(params) |
79 | 76 | if err != nil { |
80 | 77 | return nil, err |
81 | 78 | } |
82 | 79 |
|
83 | | - h.latestBlock.UpdateToLatest(payload, params[0]) |
| 80 | + resp.Result = result |
| 81 | + case "eth_getBlockByNumber": |
| 82 | + result, err := h.getBlock(params) |
| 83 | + if err != nil && err != ErrUnsupportedGetBlockQuery { |
| 84 | + return nil, err |
| 85 | + } |
84 | 86 |
|
85 | | - resp.Result = ResultNewPayloadV1{ |
86 | | - Status: "VALID", |
87 | | - LatestValidHash: payload.BlockHash, |
88 | | - ValidationError: "", |
| 87 | + resp.Result = result |
| 88 | + case "eth_getBlockByHash": |
| 89 | + result, err := h.getBlock(params) |
| 90 | + if err != nil && err != ErrUnsupportedGetBlockQuery { |
| 91 | + return nil, err |
89 | 92 | } |
90 | | - case "eth_getBlockByNumber": |
| 93 | + |
| 94 | + resp.Result = result |
| 95 | + case "eth_chainId": |
| 96 | + resp.Result = ResultChainID(h.Cfg.ChainID) |
| 97 | + case "engine_exchangeCapabilities": |
91 | 98 | if len(params) < 1 || params[0] == nil { |
92 | 99 | return nil, errors.New("missing params") |
93 | 100 | } |
94 | 101 |
|
95 | | - var query string |
| 102 | + var payload RequestParamsExchangeCapabilities |
96 | 103 |
|
97 | | - err := json.Unmarshal([]byte(*params[0]), &query) |
| 104 | + err := json.Unmarshal([]byte(*params[0]), &payload) |
98 | 105 | if err != nil { |
99 | 106 | return nil, err |
100 | 107 | } |
101 | 108 |
|
102 | | - if query != "latest" { |
103 | | - resp.Result = ResultGetBlockByNumber(h.latestBlock.raw) |
104 | | - } |
105 | | - case "eth_chainId": |
106 | | - resp.Result = ResultChainID(h.Cfg.ChainID) |
| 109 | + resp.Result = ResultexchangeCapabilities(payload) |
107 | 110 | default: |
108 | 111 | } |
109 | 112 |
|
110 | 113 | return resp, nil |
111 | 114 | } |
| 115 | + |
| 116 | +func (h *Handler) forkChoiceUpdated(params []*json.RawMessage) (interface{}, error) { |
| 117 | + if len(params) < 1 || params[0] == nil { |
| 118 | + return nil, errors.New("missing params") |
| 119 | + } |
| 120 | + |
| 121 | + var forkchoiceState RequestParamsForkchoiceUpdatedV1 |
| 122 | + |
| 123 | + err := json.Unmarshal([]byte(*params[0]), &forkchoiceState) |
| 124 | + if err != nil { |
| 125 | + return nil, err |
| 126 | + } |
| 127 | + |
| 128 | + return ResultForkchoiceUpdatedV1{ |
| 129 | + PayloadStatus: ResultForkchoiceUpdatedV1PayloadStatus{ |
| 130 | + Status: "VALID", |
| 131 | + LatestValidHash: forkchoiceState.HeadBlockHash, |
| 132 | + ValidationError: "", |
| 133 | + }, |
| 134 | + PayloadID: "0xa247243752eb10b4", |
| 135 | + }, nil |
| 136 | +} |
| 137 | + |
| 138 | +func (h *Handler) newPayload(params []*json.RawMessage) (interface{}, error) { |
| 139 | + if len(params) < 1 || params[0] == nil { |
| 140 | + return nil, errors.New("missing params") |
| 141 | + } |
| 142 | + |
| 143 | + var payload RequestParamsNewPayloadV1 |
| 144 | + |
| 145 | + err := json.Unmarshal([]byte(*params[0]), &payload) |
| 146 | + if err != nil { |
| 147 | + return nil, err |
| 148 | + } |
| 149 | + |
| 150 | + h.latestBlock.UpdateToLatest(payload, params[0]) |
| 151 | + |
| 152 | + return ResultNewPayloadV1{ |
| 153 | + Status: "VALID", |
| 154 | + LatestValidHash: payload.BlockHash, |
| 155 | + ValidationError: "", |
| 156 | + }, nil |
| 157 | +} |
| 158 | + |
| 159 | +func (h *Handler) getBlock(params []*json.RawMessage) (interface{}, error) { |
| 160 | + if len(params) < 1 || params[0] == nil { |
| 161 | + return nil, errors.New("missing params") |
| 162 | + } |
| 163 | + |
| 164 | + var query string |
| 165 | + |
| 166 | + err := json.Unmarshal([]byte(*params[0]), &query) |
| 167 | + if err != nil { |
| 168 | + return nil, err |
| 169 | + } |
| 170 | + |
| 171 | + if query == "latest" { |
| 172 | + return ResultGetBlockByNumber(h.latestBlock.raw), nil |
| 173 | + } |
| 174 | + |
| 175 | + return nil, ErrUnsupportedGetBlockQuery |
| 176 | +} |
0 commit comments