This repository has been archived by the owner on Oct 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 139
/
api.go
327 lines (285 loc) · 10.7 KB
/
api.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package blockvalidation
import (
"encoding/json"
"errors"
"fmt"
"math/big"
"os"
builderApiBellatrix "github.com/attestantio/go-builder-client/api/bellatrix"
builderApiCapella "github.com/attestantio/go-builder-client/api/capella"
builderApiDeneb "github.com/attestantio/go-builder-client/api/deneb"
builderApiV1 "github.com/attestantio/go-builder-client/api/v1"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/ethereum/go-ethereum/beacon/engine"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto/kzg4844"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/eth/tracers/logger"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/rpc"
)
type BlacklistedAddresses []common.Address
type AccessVerifier struct {
blacklistedAddresses map[common.Address]struct{}
}
func (a *AccessVerifier) verifyTraces(tracer *logger.AccessListTracer) error {
log.Trace("x", "tracer.AccessList()", tracer.AccessList())
for _, accessTuple := range tracer.AccessList() {
// TODO: should we ignore common.Address{}?
if _, found := a.blacklistedAddresses[accessTuple.Address]; found {
log.Info("bundle accesses blacklisted address", "address", accessTuple.Address)
return fmt.Errorf("blacklisted address %s in execution trace", accessTuple.Address.String())
}
}
return nil
}
func (a *AccessVerifier) isBlacklisted(addr common.Address) error {
if _, present := a.blacklistedAddresses[addr]; present {
return fmt.Errorf("transaction from blacklisted address %s", addr.String())
}
return nil
}
func (a *AccessVerifier) verifyTransactions(signer types.Signer, txs types.Transactions) error {
for _, tx := range txs {
from, err := types.Sender(signer, tx)
if err == nil {
if _, present := a.blacklistedAddresses[from]; present {
return fmt.Errorf("transaction from blacklisted address %s", from.String())
}
}
to := tx.To()
if to != nil {
if _, present := a.blacklistedAddresses[*to]; present {
return fmt.Errorf("transaction to blacklisted address %s", to.String())
}
}
}
return nil
}
func NewAccessVerifierFromFile(path string) (*AccessVerifier, error) {
bytes, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var ba BlacklistedAddresses
if err := json.Unmarshal(bytes, &ba); err != nil {
return nil, err
}
blacklistedAddresses := make(map[common.Address]struct{}, len(ba))
for _, address := range ba {
blacklistedAddresses[address] = struct{}{}
}
return &AccessVerifier{
blacklistedAddresses: blacklistedAddresses,
}, nil
}
type BlockValidationConfig struct {
BlacklistSourceFilePath string
// If set to true, proposer payment is calculated as a balance difference of the fee recipient.
UseBalanceDiffProfit bool
// If set to true, withdrawals to the fee recipient are excluded from the balance difference.
ExcludeWithdrawals bool
}
// Register adds catalyst APIs to the full node.
func Register(stack *node.Node, backend *eth.Ethereum, cfg BlockValidationConfig) error {
var accessVerifier *AccessVerifier
if cfg.BlacklistSourceFilePath != "" {
var err error
accessVerifier, err = NewAccessVerifierFromFile(cfg.BlacklistSourceFilePath)
if err != nil {
return err
}
}
stack.RegisterAPIs([]rpc.API{
{
Namespace: "flashbots",
Service: NewBlockValidationAPI(backend, accessVerifier, cfg.UseBalanceDiffProfit, cfg.ExcludeWithdrawals),
},
})
return nil
}
type BlockValidationAPI struct {
eth *eth.Ethereum
accessVerifier *AccessVerifier
// If set to true, proposer payment is calculated as a balance difference of the fee recipient.
useBalanceDiffProfit bool
// If set to true, withdrawals to the fee recipient are excluded from the balance delta.
excludeWithdrawals bool
}
// NewConsensusAPI creates a new consensus api for the given backend.
// The underlying blockchain needs to have a valid terminal total difficulty set.
func NewBlockValidationAPI(eth *eth.Ethereum, accessVerifier *AccessVerifier, useBalanceDiffProfit, excludeWithdrawals bool) *BlockValidationAPI {
return &BlockValidationAPI{
eth: eth,
accessVerifier: accessVerifier,
useBalanceDiffProfit: useBalanceDiffProfit,
excludeWithdrawals: excludeWithdrawals,
}
}
type BuilderBlockValidationRequest struct {
builderApiBellatrix.SubmitBlockRequest
RegisteredGasLimit uint64 `json:"registered_gas_limit,string"`
}
func (api *BlockValidationAPI) ValidateBuilderSubmissionV1(params *BuilderBlockValidationRequest) error {
// no longer supported endpoint
if params.ExecutionPayload == nil {
return errors.New("nil execution payload")
}
payload := params.ExecutionPayload
block, err := engine.ExecutionPayloadV1ToBlock(payload)
if err != nil {
return err
}
return api.validateBlock(block, params.Message, params.RegisteredGasLimit)
}
type BuilderBlockValidationRequestV2 struct {
builderApiCapella.SubmitBlockRequest
RegisteredGasLimit uint64 `json:"registered_gas_limit,string"`
}
func (r *BuilderBlockValidationRequestV2) UnmarshalJSON(data []byte) error {
params := &struct {
RegisteredGasLimit uint64 `json:"registered_gas_limit,string"`
}{}
err := json.Unmarshal(data, params)
if err != nil {
return err
}
r.RegisteredGasLimit = params.RegisteredGasLimit
blockRequest := new(builderApiCapella.SubmitBlockRequest)
err = json.Unmarshal(data, &blockRequest)
if err != nil {
return err
}
r.SubmitBlockRequest = *blockRequest
return nil
}
func (api *BlockValidationAPI) ValidateBuilderSubmissionV2(params *BuilderBlockValidationRequestV2) error {
// TODO: fuzztest, make sure the validation is sound
// TODO: handle context!
if params.ExecutionPayload == nil {
return errors.New("nil execution payload")
}
payload := params.ExecutionPayload
block, err := engine.ExecutionPayloadV2ToBlock(payload)
if err != nil {
return err
}
return api.validateBlock(block, params.Message, params.RegisteredGasLimit)
}
type BuilderBlockValidationRequestV3 struct {
builderApiDeneb.SubmitBlockRequest
ParentBeaconBlockRoot common.Hash `json:"parent_beacon_block_root"`
RegisteredGasLimit uint64 `json:"registered_gas_limit,string"`
}
func (r *BuilderBlockValidationRequestV3) UnmarshalJSON(data []byte) error {
params := &struct {
ParentBeaconBlockRoot common.Hash `json:"parent_beacon_block_root"`
RegisteredGasLimit uint64 `json:"registered_gas_limit,string"`
}{}
err := json.Unmarshal(data, params)
if err != nil {
return err
}
r.RegisteredGasLimit = params.RegisteredGasLimit
r.ParentBeaconBlockRoot = params.ParentBeaconBlockRoot
blockRequest := new(builderApiDeneb.SubmitBlockRequest)
err = json.Unmarshal(data, &blockRequest)
if err != nil {
return err
}
r.SubmitBlockRequest = *blockRequest
return nil
}
func (api *BlockValidationAPI) ValidateBuilderSubmissionV3(params *BuilderBlockValidationRequestV3) error {
// TODO: fuzztest, make sure the validation is sound
payload := params.ExecutionPayload
blobsBundle := params.BlobsBundle
log.Info("blobs bundle", "blobs", len(blobsBundle.Blobs), "commits", len(blobsBundle.Commitments), "proofs", len(blobsBundle.Proofs))
block, err := engine.ExecutionPayloadV3ToBlock(payload, blobsBundle, params.ParentBeaconBlockRoot)
if err != nil {
return err
}
err = api.validateBlock(block, params.Message, params.RegisteredGasLimit)
if err != nil {
log.Error("invalid payload", "hash", block.Hash, "number", block.NumberU64(), "parentHash", block.ParentHash, "err", err)
return err
}
err = validateBlobsBundle(block.Transactions(), blobsBundle)
if err != nil {
log.Error("invalid blobs bundle", "err", err)
return err
}
return nil
}
func (api *BlockValidationAPI) validateBlock(block *types.Block, msg *builderApiV1.BidTrace, registeredGasLimit uint64) error {
if msg.ParentHash != phase0.Hash32(block.ParentHash()) {
return fmt.Errorf("incorrect ParentHash %s, expected %s", msg.ParentHash.String(), block.ParentHash().String())
}
if msg.BlockHash != phase0.Hash32(block.Hash()) {
return fmt.Errorf("incorrect BlockHash %s, expected %s", msg.BlockHash.String(), block.Hash().String())
}
if msg.GasLimit != block.GasLimit() {
return fmt.Errorf("incorrect GasLimit %d, expected %d", msg.GasLimit, block.GasLimit())
}
if msg.GasUsed != block.GasUsed() {
return fmt.Errorf("incorrect GasUsed %d, expected %d", msg.GasUsed, block.GasUsed())
}
feeRecipient := common.BytesToAddress(msg.ProposerFeeRecipient[:])
expectedProfit := msg.Value.ToBig()
var vmconfig vm.Config
var tracer *logger.AccessListTracer = nil
if api.accessVerifier != nil {
if err := api.accessVerifier.isBlacklisted(block.Coinbase()); err != nil {
return err
}
if err := api.accessVerifier.isBlacklisted(feeRecipient); err != nil {
return err
}
if err := api.accessVerifier.verifyTransactions(types.LatestSigner(api.eth.BlockChain().Config()), block.Transactions()); err != nil {
return err
}
isPostMerge := true // the call is PoS-native
precompiles := vm.ActivePrecompiles(api.eth.APIBackend.ChainConfig().Rules(new(big.Int).SetUint64(block.NumberU64()), isPostMerge, block.Time()))
tracer = logger.NewAccessListTracer(nil, common.Address{}, common.Address{}, precompiles)
vmconfig = vm.Config{Tracer: tracer}
}
err := api.eth.BlockChain().ValidatePayload(block, feeRecipient, expectedProfit, registeredGasLimit, vmconfig, api.useBalanceDiffProfit, api.excludeWithdrawals)
if err != nil {
return err
}
if api.accessVerifier != nil && tracer != nil {
if err := api.accessVerifier.verifyTraces(tracer); err != nil {
return err
}
}
log.Info("validated block", "hash", block.Hash(), "number", block.NumberU64(), "parentHash", block.ParentHash())
return nil
}
func validateBlobsBundle(txs types.Transactions, blobsBundle *builderApiDeneb.BlobsBundle) error {
var hashes []common.Hash
for _, tx := range txs {
hashes = append(hashes, tx.BlobHashes()...)
}
blobs := blobsBundle.Blobs
commits := blobsBundle.Commitments
proofs := blobsBundle.Proofs
if len(blobs) != len(hashes) {
return fmt.Errorf("invalid number of %d blobs compared to %d blob hashes", len(blobs), len(hashes))
}
if len(commits) != len(hashes) {
return fmt.Errorf("invalid number of %d blob commitments compared to %d blob hashes", len(commits), len(hashes))
}
if len(proofs) != len(hashes) {
return fmt.Errorf("invalid number of %d blob proofs compared to %d blob hashes", len(proofs), len(hashes))
}
for i := range blobs {
if err := kzg4844.VerifyBlobProof(kzg4844.Blob(blobs[i]), kzg4844.Commitment(commits[i]), kzg4844.Proof(proofs[i])); err != nil {
return fmt.Errorf("invalid blob %d: %v", i, err)
}
}
log.Info("validated blobs bundle", "blobs", len(blobs), "commits", len(commits), "proofs", len(proofs))
return nil
}