Skip to content

Commit

Permalink
Add some missing EIP-7685 wirings (#12157)
Browse files Browse the repository at this point in the history
Cherry pick #10415
  • Loading branch information
yperbasis authored Oct 1, 2024
1 parent 55e55db commit 7af0551
Show file tree
Hide file tree
Showing 20 changed files with 155 additions and 37 deletions.
1 change: 1 addition & 0 deletions cmd/evm/internal/t8ntool/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type stEnv struct {
Withdrawals []*types.Withdrawal `json:"withdrawals,omitempty"`
WithdrawalsHash *libcommon.Hash `json:"withdrawalsRoot,omitempty"`
Requests []*types.Request `json:"requests,omitempty"`
RequestsRoot *libcommon.Hash `json:"requestsRoot,omitempty"`
}

type stEnvMarshaling struct {
Expand Down
6 changes: 6 additions & 0 deletions cmd/evm/internal/t8ntool/gen_stenv.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion cmd/evm/internal/t8ntool/transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,11 @@ func Main(ctx *cli.Context) error {
}

if chainConfig.IsShanghai(prestate.Env.Timestamp) && prestate.Env.Withdrawals == nil {
return NewError(ErrorVMConfig, errors.New("Shanghai config but missing 'withdrawals' in env section"))
return NewError(ErrorVMConfig, errors.New("shanghai config but missing 'withdrawals' in env section"))
}

if chainConfig.IsPrague(prestate.Env.Timestamp) && prestate.Env.Requests == nil {
return NewError(ErrorVMConfig, errors.New("prague config but missing 'requests' in env section"))
}

isMerged := chainConfig.TerminalTotalDifficulty != nil && chainConfig.TerminalTotalDifficulty.BitLen() == 0
Expand Down Expand Up @@ -599,6 +603,7 @@ func NewHeader(env stEnv) *types.Header {

header.UncleHash = env.UncleHash
header.WithdrawalsHash = env.WithdrawalsHash
header.RequestsRoot = env.RequestsRoot

return &header
}
Expand Down
4 changes: 4 additions & 0 deletions consensus/clique/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ func (c *Clique) verifyHeader(chain consensus.ChainHeaderReader, header *types.H
return consensus.ErrUnexpectedWithdrawals
}

if header.RequestsRoot != nil {
return consensus.ErrUnexpectedRequests
}

// All basic checks passed, verify cascading fields
return c.verifyCascadingFields(chain, header, parents)
}
Expand Down
3 changes: 3 additions & 0 deletions consensus/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@ var (

// ErrUnexpectedWithdrawals is returned if a pre-Shanghai block has withdrawals.
ErrUnexpectedWithdrawals = errors.New("unexpected withdrawals")

// ErrUnexpectedRequests is returned if a pre-Prague block has EIP-7685 requests.
ErrUnexpectedRequests = errors.New("unexpected requests")
)
4 changes: 4 additions & 0 deletions consensus/ethash/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ func VerifyHeaderBasics(chain consensus.ChainHeaderReader, header, parent *types
return consensus.ErrUnexpectedWithdrawals
}

if header.RequestsRoot != nil {
return consensus.ErrUnexpectedRequests
}

// If all checks passed, validate any special fields for hard forks
if err := misc.VerifyDAOHeaderExtraData(chain.Config(), header); err != nil {
return err
Expand Down
11 changes: 10 additions & 1 deletion consensus/merge/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,23 @@ func (s *Merge) verifyHeader(chain consensus.ChainHeaderReader, header, parent *
if !chain.Config().IsCancun(header.Time) {
return misc.VerifyAbsenceOfCancunHeaderFields(header)
}

if err := misc.VerifyPresenceOfCancunHeaderFields(header); err != nil {
return err
}
expectedExcessBlobGas := misc.CalcExcessBlobGas(chain.Config(), parent)
if *header.ExcessBlobGas != expectedExcessBlobGas {
return fmt.Errorf("invalid excessBlobGas: have %d, want %d", *header.ExcessBlobGas, expectedExcessBlobGas)
}

// Verify existence / non-existence of requestsRoot
prague := chain.Config().IsPrague(header.Time)
if prague && header.RequestsRoot == nil {
return fmt.Errorf("missing requestsRoot")
}
if !prague && header.RequestsRoot != nil {
return consensus.ErrUnexpectedRequests
}

return nil
}

Expand Down
34 changes: 30 additions & 4 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ func (r RawBlock) AsBlock() (*Block, error) {
b := &Block{header: r.Header}
b.uncles = r.Body.Uncles
b.withdrawals = r.Body.Withdrawals
b.requests = r.Body.Requests

txs := make([]Transaction, len(r.Body.Transactions))
for i, tx := range r.Body.Transactions {
Expand Down Expand Up @@ -1049,7 +1050,7 @@ func NewBlock(header *Header, txs []Transaction, uncles []*Header, receipts []*R
if requests == nil {
b.header.RequestsRoot = nil
} else if len(requests) == 0 {
b.header.RequestsRoot = &EmptyRootHash // TODO(racytech): is this correct?
b.header.RequestsRoot = &EmptyRootHash
b.requests = make(Requests, len(requests))
} else {
h := DeriveSha(Requests(requests))
Expand Down Expand Up @@ -1305,7 +1306,7 @@ func (b *Block) SendersToTxs(senders []libcommon.Address) {
// RawBody creates a RawBody based on the block. It is not very efficient, so
// will probably be removed in favour of RawBlock. Also it panics
func (b *Block) RawBody() *RawBody {
br := &RawBody{Transactions: make([][]byte, len(b.transactions)), Uncles: b.uncles, Withdrawals: b.withdrawals}
br := &RawBody{Transactions: make([][]byte, len(b.transactions)), Uncles: b.uncles, Withdrawals: b.withdrawals, Requests: b.requests}
for i, tx := range b.transactions {
var err error
br.Transactions[i], err = rlp.EncodeToBytes(tx)
Expand All @@ -1318,7 +1319,7 @@ func (b *Block) RawBody() *RawBody {

// RawBody creates a RawBody based on the body.
func (b *Body) RawBody() *RawBody {
br := &RawBody{Transactions: make([][]byte, len(b.Transactions)), Uncles: b.Uncles, Withdrawals: b.Withdrawals}
br := &RawBody{Transactions: make([][]byte, len(b.Transactions)), Uncles: b.Uncles, Withdrawals: b.Withdrawals, Requests: b.Requests}
for i, tx := range b.Transactions {
var err error
br.Transactions[i], err = rlp.EncodeToBytes(tx)
Expand Down Expand Up @@ -1347,7 +1348,7 @@ func (b *Block) SanityCheck() error {
return b.header.SanityCheck()
}

// HashCheck checks that transactions, receipts, uncles and withdrawals hashes are correct.
// HashCheck checks that transactions, receipts, uncles, withdrawals, and requests hashes are correct.
func (b *Block) HashCheck() error {
if hash := DeriveSha(b.Transactions()); hash != b.TxHash() {
return fmt.Errorf("block has invalid transaction hash: have %x, exp: %x", hash, b.TxHash())
Expand Down Expand Up @@ -1377,6 +1378,20 @@ func (b *Block) HashCheck() error {
if hash := DeriveSha(b.Withdrawals()); hash != *b.WithdrawalsHash() {
return fmt.Errorf("block has invalid withdrawals hash: have %x, exp: %x", hash, b.WithdrawalsHash())
}

if b.RequestsRoot() == nil {
if b.Requests() != nil {
return errors.New("header missing RequestsRoot")
}
return nil
}
if b.Requests() == nil {
return errors.New("body missing Requests")
}
if hash := DeriveSha(b.Requests()); hash != *b.RequestsRoot() {
return fmt.Errorf("block has invalid requests root: have %x, exp: %x", hash, b.RequestsRoot())
}

return nil
}

Expand Down Expand Up @@ -1433,6 +1448,15 @@ func (b *Block) Copy() *Block {
}
}

var requests []*Request
if b.requests != nil {
requests = make([]*Request, 0, len(b.requests))
for _, request := range b.requests {
rCopy := *request
requests = append(requests, &rCopy)
}
}

var hashValue atomic.Value
if value := b.hash.Load(); value != nil {
hash := value.(libcommon.Hash)
Expand All @@ -1450,6 +1474,7 @@ func (b *Block) Copy() *Block {
uncles: uncles,
transactions: CopyTxs(b.transactions),
withdrawals: withdrawals,
requests: requests,
hash: hashValue,
size: sizeValue,
}
Expand All @@ -1465,6 +1490,7 @@ func (b *Block) WithSeal(header *Header) *Block {
transactions: b.transactions,
uncles: b.uncles,
withdrawals: b.withdrawals,
requests: b.requests,
}
}

Expand Down
6 changes: 6 additions & 0 deletions core/types/gen_header_json.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions eth/protocols/eth/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,18 +356,19 @@ type BlockBodiesRLPPacket66 struct {
BlockBodiesRLPPacket
}

// Unpack retrieves the transactions, uncles, and withdrawals from the range packet and returns
// Unpack retrieves the transactions, uncles, withdrawals, and requests from the range packet and returns
// them in a split flat format that's more consistent with the internal data structures.
func (p *BlockRawBodiesPacket) Unpack() ([][][]byte, [][]*types.Header, []types.Withdrawals) {
func (p *BlockRawBodiesPacket) Unpack() ([][][]byte, [][]*types.Header, []types.Withdrawals, []types.Requests) {
var (
txSet = make([][][]byte, len(*p))
uncleSet = make([][]*types.Header, len(*p))
withdrawalSet = make([]types.Withdrawals, len(*p))
requestSet = make([]types.Requests, len(*p))
)
for i, body := range *p {
txSet[i], uncleSet[i], withdrawalSet[i] = body.Transactions, body.Uncles, body.Withdrawals
txSet[i], uncleSet[i], withdrawalSet[i], requestSet[i] = body.Transactions, body.Uncles, body.Withdrawals, body.Requests
}
return txSet, uncleSet, withdrawalSet
return txSet, uncleSet, withdrawalSet, requestSet
}

// GetReceiptsPacket represents a block receipts query.
Expand Down
2 changes: 2 additions & 0 deletions eth/stagedsync/exec3.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ Loop:
GetHashFn: getHashFn,
EvmBlockContext: blockContext,
Withdrawals: b.Withdrawals(),
Requests: b.Requests(),
}
if txIndex >= 0 && txIndex < len(txs) {
txTask.Tx = txs[txIndex]
Expand Down Expand Up @@ -1080,6 +1081,7 @@ func reconstituteStep(last bool,
GetHashFn: getHashFn,
EvmBlockContext: blockContext,
Withdrawals: b.Withdrawals(),
Requests: b.Requests(),
}
if txIndex >= 0 && txIndex < len(txs) {
txTask.Tx = txs[txIndex]
Expand Down
6 changes: 3 additions & 3 deletions p2p/sentry/sentry_multi_client/sentry_multi_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,12 +585,12 @@ func (cs *MultiClient) blockBodies66(ctx context.Context, inreq *proto_sentry.In
if err := rlp.DecodeBytes(inreq.Data, &request); err != nil {
return fmt.Errorf("decode BlockBodiesPacket66: %w", err)
}
txs, uncles, withdrawals := request.BlockRawBodiesPacket.Unpack()
if len(txs) == 0 && len(uncles) == 0 && len(withdrawals) == 0 {
txs, uncles, withdrawals, requests := request.BlockRawBodiesPacket.Unpack()
if len(txs) == 0 && len(uncles) == 0 && len(withdrawals) == 0 && len(requests) == 0 {
// No point processing empty response
return nil
}
cs.Bd.DeliverBodies(txs, uncles, withdrawals, uint64(len(inreq.Data)), sentry.ConvertH512ToPeerID(inreq.PeerId))
cs.Bd.DeliverBodies(txs, uncles, withdrawals, requests, uint64(len(inreq.Data)), sentry.ConvertH512ToPeerID(inreq.PeerId))
return nil
}

Expand Down
12 changes: 12 additions & 0 deletions polygon/bor/bor.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,10 @@ func ValidateHeaderUnusedFields(header *types.Header) error {
return consensus.ErrUnexpectedWithdrawals
}

if header.RequestsRoot != nil {
return consensus.ErrUnexpectedRequests
}

return misc.VerifyAbsenceOfCancunHeaderFields(header)
}

Expand Down Expand Up @@ -984,6 +988,10 @@ func (c *Bor) Finalize(config *chain.Config, header *types.Header, state *state.
return nil, nil, consensus.ErrUnexpectedWithdrawals
}

if requests != nil || header.RequestsRoot != nil {
return nil, nil, consensus.ErrUnexpectedRequests
}

if isSprintStart(headerNumber, c.config.CalculateSprintLength(headerNumber)) {
cx := statefull.ChainContext{Chain: chain, Bor: c}

Expand Down Expand Up @@ -1050,6 +1058,10 @@ func (c *Bor) FinalizeAndAssemble(chainConfig *chain.Config, header *types.Heade
return nil, nil, nil, consensus.ErrUnexpectedWithdrawals
}

if requests != nil || header.RequestsRoot != nil {
return nil, nil, nil, consensus.ErrUnexpectedRequests
}

if isSprintStart(headerNumber, c.config.CalculateSprintLength(headerNumber)) {
cx := statefull.ChainContext{Chain: chain, Bor: c}

Expand Down
4 changes: 4 additions & 0 deletions tests/block_test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ type btHeader struct {
BlobGasUsed *uint64
ExcessBlobGas *uint64
ParentBeaconBlockRoot *libcommon.Hash
RequestsRoot *libcommon.Hash
}

type btHeaderMarshaling struct {
Expand Down Expand Up @@ -303,6 +304,9 @@ func validateHeader(h *btHeader, h2 *types.Header) error {
if !reflect.DeepEqual(h.ParentBeaconBlockRoot, h2.ParentBeaconBlockRoot) {
return fmt.Errorf("parentBeaconBlockRoot: want: %v have: %v", h.ParentBeaconBlockRoot, h2.ParentBeaconBlockRoot)
}
if !reflect.DeepEqual(h.RequestsRoot, h2.RequestsRoot) {
return fmt.Errorf("requestsRoot: want: %v have: %v", h.RequestsRoot, h2.RequestsRoot)
}
return nil
}

Expand Down
6 changes: 6 additions & 0 deletions tests/gen_btheader.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7af0551

Please sign in to comment.