Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.

Commit 211bb09

Browse files
committed
Prevent uint256 overflow during conversion (#45)
* Prevent uint256 overflow during conversion * add shanghai checks * remove unused functions * move validation to ValidatePayload
1 parent 8117040 commit 211bb09

File tree

4 files changed

+50
-35
lines changed

4 files changed

+50
-35
lines changed

builder/builder.go

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -166,18 +166,18 @@ func (b *Builder) submitBellatrixBlock(block *types.Block, blockValue *big.Int,
166166
if b.dryRun {
167167
err = b.validator.ValidateBuilderSubmissionV1(&blockvalidation.BuilderBlockValidationRequest{BuilderSubmitBlockRequest: blockSubmitReq, RegisteredGasLimit: vd.GasLimit})
168168
if err != nil {
169-
log.Error("could not validate block", "err", err)
169+
log.Error("could not validate bellatrix block", "err", err)
170170
}
171171
} else {
172172
go b.ds.ConsumeBuiltBlock(block, blockValue, ordersClosedAt, sealedAt, commitedBundles, allBundles, &blockBidMsg)
173173
err = b.relay.SubmitBlock(&blockSubmitReq, vd)
174174
if err != nil {
175-
log.Error("could not submit block", "err", err, "#commitedBundles", len(commitedBundles))
175+
log.Error("could not submit bellatrix block", "err", err, "#commitedBundles", len(commitedBundles))
176176
return err
177177
}
178178
}
179179

180-
log.Info("submitted block", "slot", blockBidMsg.Slot, "value", blockBidMsg.Value.String(), "parent", blockBidMsg.ParentHash, "hash", block.Hash(), "#commitedBundles", len(commitedBundles))
180+
log.Info("submitted bellatrix block", "slot", blockBidMsg.Slot, "value", blockBidMsg.Value.String(), "parent", blockBidMsg.ParentHash, "hash", block.Hash(), "#commitedBundles", len(commitedBundles))
181181

182182
return nil
183183
}
@@ -190,10 +190,9 @@ func (b *Builder) submitCapellaBlock(block *types.Block, blockValue *big.Int, or
190190
return err
191191
}
192192

193-
value := new(boostTypes.U256Str)
194-
err = value.FromBig(blockValue)
195-
if err != nil {
196-
log.Error("could not set block value", "err", err)
193+
value, overflow := uint256.FromBig(blockValue)
194+
if overflow {
195+
log.Error("could not set block value due to value overflow")
197196
return err
198197
}
199198

@@ -206,10 +205,14 @@ func (b *Builder) submitCapellaBlock(block *types.Block, blockValue *big.Int, or
206205
ProposerFeeRecipient: bellatrix.ExecutionAddress(vd.FeeRecipient),
207206
GasLimit: executableData.ExecutionPayload.GasLimit,
208207
GasUsed: executableData.ExecutionPayload.GasUsed,
209-
Value: uint256.NewInt(value.BigInt().Uint64()),
208+
Value: value,
210209
}
211210

212-
boostBidTrace := convertBidTrace(blockBidMsg)
211+
boostBidTrace, err := convertBidTrace(blockBidMsg)
212+
if err != nil {
213+
log.Error("could not convert bid trace", "err", err)
214+
return err
215+
}
213216

214217
signature, err := boostTypes.SignMessage(&blockBidMsg, b.builderSigningDomain, b.builderSecretKey)
215218
if err != nil {
@@ -223,12 +226,21 @@ func (b *Builder) submitCapellaBlock(block *types.Block, blockValue *big.Int, or
223226
ExecutionPayload: payload,
224227
}
225228

226-
go b.ds.ConsumeBuiltBlock(block, blockValue, ordersClosedAt, sealedAt, commitedBundles, allBundles, &boostBidTrace)
227-
err = b.relay.SubmitBlockCapella(&blockSubmitReq, vd)
228-
if err != nil {
229-
log.Error("could not submit block", "err", err, "#commitedBundles", len(commitedBundles))
230-
return err
229+
if b.dryRun {
230+
err = b.validator.ValidateBuilderSubmissionV2(&blockvalidation.BuilderBlockValidationRequestV2{SubmitBlockRequest: blockSubmitReq, RegisteredGasLimit: vd.GasLimit})
231+
if err != nil {
232+
log.Error("could not validate block for capella", "err", err)
233+
}
234+
} else {
235+
go b.ds.ConsumeBuiltBlock(block, blockValue, ordersClosedAt, sealedAt, commitedBundles, allBundles, &boostBidTrace)
236+
err = b.relay.SubmitBlockCapella(&blockSubmitReq, vd)
237+
if err != nil {
238+
log.Error("could not submit capella block", "err", err, "#commitedBundles", len(commitedBundles))
239+
return err
240+
}
231241
}
242+
243+
log.Info("submitted capella block", "slot", blockBidMsg.Slot, "value", blockBidMsg.Value.String(), "parent", blockBidMsg.ParentHash, "hash", block.Hash(), "#commitedBundles", len(commitedBundles))
232244
return nil
233245
}
234246

@@ -444,7 +456,13 @@ func executableDataToCapellaExecutionPayload(data *engine.ExecutableData) (*cape
444456
}, nil
445457
}
446458

447-
func convertBidTrace(bidTrace apiv1.BidTrace) boostTypes.BidTrace {
459+
func convertBidTrace(bidTrace apiv1.BidTrace) (boostTypes.BidTrace, error) {
460+
value := new(boostTypes.U256Str)
461+
err := value.FromBig(bidTrace.Value.ToBig())
462+
if err != nil {
463+
return boostTypes.BidTrace{}, err
464+
}
465+
448466
return boostTypes.BidTrace{
449467
Slot: bidTrace.Slot,
450468
ParentHash: boostTypes.Hash(bidTrace.ParentHash),
@@ -454,6 +472,6 @@ func convertBidTrace(bidTrace apiv1.BidTrace) boostTypes.BidTrace {
454472
ProposerFeeRecipient: boostTypes.Address(bidTrace.ProposerFeeRecipient),
455473
GasLimit: bidTrace.GasLimit,
456474
GasUsed: bidTrace.GasUsed,
457-
Value: boostTypes.IntToU256(bidTrace.Value.Uint64()),
458-
}
475+
Value: *value,
476+
}, nil
459477
}

core/block_validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
8181
// Withdrawals are not allowed prior to shanghai fork
8282
return fmt.Errorf("withdrawals present in block body")
8383
}
84-
84+
8585
if !v.bc.HasBlockAndState(block.ParentHash(), block.NumberU64()-1) {
8686
if !v.bc.HasBlock(block.ParentHash(), block.NumberU64()-1) {
8787
return consensus.ErrUnknownAncestor

core/blockchain.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2532,6 +2532,20 @@ func (bc *BlockChain) ValidatePayload(block *types.Block, feeRecipient common.Ad
25322532
return err
25332533
}
25342534

2535+
if bc.Config().IsShanghai(header.Time) {
2536+
if header.WithdrawalsHash == nil {
2537+
return fmt.Errorf("withdrawals hash is missing")
2538+
}
2539+
// withdrawals hash and withdrawals validated later in ValidateBody
2540+
} else {
2541+
if header.WithdrawalsHash != nil {
2542+
return fmt.Errorf("withdrawals hash present before shanghai")
2543+
}
2544+
if block.Withdrawals() != nil {
2545+
return fmt.Errorf("withdrawals list present in block body before shanghai")
2546+
}
2547+
}
2548+
25352549
if err := bc.validator.ValidateBody(block); err != nil {
25362550
return err
25372551
}

eth/block-validation/api.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"github.com/ethereum/go-ethereum/log"
1919
"github.com/ethereum/go-ethereum/node"
2020
"github.com/ethereum/go-ethereum/rpc"
21-
"github.com/ethereum/go-ethereum/trie"
2221

2322
boostTypes "github.com/flashbots/go-boost-utils/types"
2423
)
@@ -67,22 +66,6 @@ func (a *AccessVerifier) verifyTransactions(signer types.Signer, txs types.Trans
6766
return nil
6867
}
6968

70-
func verifyWithdrawals(withdrawals types.Withdrawals, expectedWithdrawalsRoot common.Hash, isShanghai bool) error {
71-
if !isShanghai {
72-
// Reject payload attributes with withdrawals before shanghai
73-
if withdrawals != nil {
74-
return errors.New("withdrawals before shanghai")
75-
}
76-
return nil
77-
}
78-
79-
withdrawalsRoot := types.DeriveSha(types.Withdrawals(withdrawals), trie.NewStackTrie(nil))
80-
if withdrawalsRoot != expectedWithdrawalsRoot {
81-
return fmt.Errorf("withdrawals mismatch")
82-
}
83-
return nil
84-
}
85-
8669
func NewAccessVerifierFromFile(path string) (*AccessVerifier, error) {
8770
bytes, err := os.ReadFile(path)
8871
if err != nil {

0 commit comments

Comments
 (0)