-
Notifications
You must be signed in to change notification settings - Fork 0
/
execution.go
223 lines (183 loc) · 4.89 KB
/
execution.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package execution
import (
"context"
"encoding/json"
"errors"
"math/big"
"github.com/sirupsen/logrus"
)
var ErrUnsupportedGetBlockQuery = errors.New("unsupported get block query")
type Handler struct {
log logrus.FieldLogger
Cfg Config
storage *Storage
}
// NewHandler returns a new Handler instance.
func NewHandler(log logrus.FieldLogger, conf *Config) *Handler {
if err := conf.Validate(); err != nil {
log.Fatalf("invalid config: %s", err)
}
return &Handler{
log: log.WithField("module", "api/execution"),
Cfg: *conf,
storage: newStorage(log.WithField("module", "api/execution/storage")),
}
}
func (h *Handler) Start(ctx context.Context) {
h.storage.Start(ctx)
}
func (h *Handler) Request(ctx context.Context, id int, method string, params []*json.RawMessage) (*Response, error) {
resp := &Response{
ID: id,
JSONRPC: "2.0",
Result: false,
}
switch method {
case "eth_syncing":
resp.Result = false
case "engine_exchangeTransitionConfigurationV1":
resp.Result = ResultExchangeTransitionConfigurationV1{
TerminalTotalDifficulty: h.Cfg.TerminalTotalDifficulty,
TerminalBlockHash: h.Cfg.TerminalBlockHash,
TerminalBlockNumber: h.Cfg.TerminalBlockNumber,
}
case "engine_forkchoiceUpdatedV1":
result, err := h.forkChoiceUpdated(params)
if err != nil {
return nil, err
}
resp.Result = result
case "engine_forkchoiceUpdatedV2":
result, err := h.forkChoiceUpdated(params)
if err != nil {
return nil, err
}
resp.Result = result
case "engine_newPayloadV1":
result, err := h.newPayload(params)
if err != nil {
return nil, err
}
resp.Result = result
case "engine_newPayloadV2":
result, err := h.newPayload(params)
if err != nil {
return nil, err
}
resp.Result = result
case "engine_newPayloadV3":
result, err := h.newPayload(params)
if err != nil {
return nil, err
}
resp.Result = result
case "eth_getBlockByHash":
result, err := h.getBlockByHash(params)
if err != nil && err != ErrUnsupportedGetBlockQuery {
return nil, err
}
resp.Result = result
case "eth_getBlockByNumber":
result, err := h.getBlockByNumber(params)
if err != nil && err != ErrUnsupportedGetBlockQuery {
return nil, err
}
resp.Result = result
case "eth_chainId":
resp.Result = ResultChainID(h.Cfg.ChainID)
case "engine_exchangeCapabilities":
if len(params) < 1 || params[0] == nil {
return nil, errors.New("missing params")
}
var payload RequestParamsExchangeCapabilities
err := json.Unmarshal([]byte(*params[0]), &payload)
if err != nil {
return nil, err
}
resp.Result = ResultexchangeCapabilities(payload)
case "eth_call":
default:
h.log.WithField("method", method).Warn("unsupported method")
}
return resp, nil
}
func (h *Handler) forkChoiceUpdated(params []*json.RawMessage) (interface{}, error) {
if len(params) < 1 || params[0] == nil {
return nil, errors.New("missing params")
}
var forkchoiceState RequestParamsForkchoiceUpdatedV1
err := json.Unmarshal([]byte(*params[0]), &forkchoiceState)
if err != nil {
return nil, err
}
return ResultForkchoiceUpdatedV1{
PayloadStatus: ResultForkchoiceUpdatedV1PayloadStatus{
Status: "VALID",
LatestValidHash: forkchoiceState.HeadBlockHash,
ValidationError: "",
},
PayloadID: "0xa247243752eb10b4",
}, nil
}
func (h *Handler) newPayload(params []*json.RawMessage) (interface{}, error) {
if len(params) < 1 || params[0] == nil {
return nil, errors.New("missing params")
}
var payload RequestParamsNewPayloadV1
err := json.Unmarshal([]byte(*params[0]), &payload)
if err != nil {
return nil, err
}
h.storage.AddBlock(&payload, params[0])
return ResultNewPayloadV1{
Status: "VALID",
LatestValidHash: payload.BlockHash,
ValidationError: "",
}, nil
}
func (h *Handler) getBlockByHash(params []*json.RawMessage) (interface{}, error) {
if len(params) < 1 || params[0] == nil {
return nil, errors.New("missing params")
}
var query string
err := json.Unmarshal([]byte(*params[0]), &query)
if err != nil {
return nil, err
}
if query == "latest" {
block := h.storage.GetLatestBlock()
if block != nil {
return block.GetResult(), nil
}
} else {
block := h.storage.GetBlockByHash(query)
if block != nil {
return block.GetResult(), nil
}
}
return "{}", ErrUnsupportedGetBlockQuery
}
func (h *Handler) getBlockByNumber(params []*json.RawMessage) (interface{}, error) {
if len(params) < 1 || params[0] == nil {
return nil, errors.New("missing params")
}
var query string
err := json.Unmarshal([]byte(*params[0]), &query)
if err != nil {
return nil, err
}
if query == "latest" {
block := h.storage.GetLatestBlock()
if block != nil {
return block.GetResult(), nil
}
} else {
number := new(big.Int)
number.SetString(query[2:], 16)
block := h.storage.GetBlockByNumber(number)
if block != nil {
return block.GetResult(), nil
}
}
return nil, ErrUnsupportedGetBlockQuery
}