Skip to content

Commit

Permalink
BEP-466: Make the block format compatible with EIP-7685
Browse files Browse the repository at this point in the history
  • Loading branch information
buddh0 committed Nov 28, 2024
1 parent 2ae712b commit 9185f29
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 15 deletions.
1 change: 0 additions & 1 deletion consensus/parlia/parlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,6 @@ func (p *Parlia) verifyHeader(chain consensus.ChainHeaderReader, header *types.H
return fmt.Errorf("invalid RequestsHash, have %#x, expected nil", header.ParentBeaconRoot)
}
} else {
// TODO(Nathan): need a BEP to define this and `Requests` in struct Body
if !header.EmptyRequestsHash() {
return errors.New("header has wrong RequestsHash")
}
Expand Down
17 changes: 13 additions & 4 deletions core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type BlockGen struct {
receipts []*types.Receipt
uncles []*types.Header
withdrawals []*types.Withdrawal
requests []*types.Request

engine consensus.Engine

Expand Down Expand Up @@ -337,6 +338,9 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse
if b.header.EmptyWithdrawalsHash() {
b.withdrawals = make([]*types.Withdrawal, 0)
}
if b.header.EmptyRequestsHash() {
b.requests = make([]*types.Request, 0)
}

// Set the difficulty for clique block. The chain maker doesn't have access
// to a chain, so the difficulty will be left unset (nil). Set it here to the
Expand Down Expand Up @@ -601,11 +605,16 @@ func (cm *chainMaker) makeHeader(parent *types.Block, state *state.StateDB, engi
excessBlobGas := eip4844.CalcExcessBlobGas(parentExcessBlobGas, parentBlobGasUsed)
header.ExcessBlobGas = &excessBlobGas
header.BlobGasUsed = new(uint64)
if cm.config.Parlia != nil {
header.WithdrawalsHash = &types.EmptyWithdrawalsHash
}
if cm.config.Parlia == nil || cm.config.IsBohr(header.Number, header.Time) {
if cm.config.Parlia == nil {
header.ParentBeaconRoot = new(common.Hash)
} else {
header.WithdrawalsHash = &types.EmptyWithdrawalsHash
if cm.config.IsBohr(header.Number, header.Time) {
header.ParentBeaconRoot = new(common.Hash)
if cm.config.IsPrague(header.Number, header.Time) {
header.RequestsHash = &types.EmptyRequestsHash
}
}
}
}
return header
Expand Down
8 changes: 5 additions & 3 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,9 +481,11 @@ func (g *Genesis) toBlockWithRoot(root common.Hash) *types.Block {
)
if conf := g.Config; conf != nil {
num := big.NewInt(int64(g.Number))
if conf.Parlia == nil && conf.IsShanghai(num, g.Timestamp) {
head.WithdrawalsHash = &types.EmptyWithdrawalsHash
withdrawals = make([]*types.Withdrawal, 0)
if conf.IsShanghai(num, g.Timestamp) {
if conf.Parlia == nil {
head.WithdrawalsHash = &types.EmptyWithdrawalsHash
withdrawals = make([]*types.Withdrawal, 0)
}
}
if conf.IsCancun(num, g.Timestamp) {
if conf.Parlia != nil {
Expand Down
17 changes: 17 additions & 0 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,23 @@ func (b *Block) WithWithdrawals(withdrawals []*Withdrawal) *Block {
return block
}

// WithRequests returns a copy of the block containing the given requests.
func (b *Block) WithRequests(requests []*Request) *Block {
block := &Block{
header: b.header,
transactions: b.transactions,
uncles: b.uncles,
withdrawals: b.withdrawals,
witness: b.witness,
sidecars: b.sidecars,
}
if requests != nil {
block.requests = make([]*Request, len(requests))
copy(block.requests, requests)
}
return block
}

// WithSidecars returns a block containing the given blobs.
func (b *Block) WithSidecars(sidecars BlobSidecars) *Block {
block := &Block{
Expand Down
3 changes: 3 additions & 0 deletions eth/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -1355,6 +1355,9 @@ func (d *Downloader) importBlockResults(results []*fetchResult) error {
blocks := make([]*types.Block, len(results))
for i, result := range results {
blocks[i] = types.NewBlockWithHeader(result.Header).WithBody(result.body()).WithSidecars(result.Sidecars)
if blocks[i].Header().EmptyRequestsHash() {
blocks[i] = blocks[i].WithRequests(make([]*types.Request, 0))
}
}
// Downloaded blocks are always regarded as trusted after the
// transition. Because the downloaded chain is guided by the
Expand Down
6 changes: 6 additions & 0 deletions eth/fetcher/block_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,9 @@ func (f *BlockFetcher) loop() {
if block.Header().EmptyWithdrawalsHash() {
block = block.WithWithdrawals(make([]*types.Withdrawal, 0))
}
if block.Header().EmptyRequestsHash() {
block = block.WithRequests(make([]*types.Request, 0))
}
block.ReceivedAt = task.time

complete = append(complete, block)
Expand Down Expand Up @@ -916,6 +919,9 @@ func (f *BlockFetcher) importBlocks(op *blockOrHeaderInject) {
if block.Header().EmptyWithdrawalsHash() {
block = block.WithWithdrawals(make([]*types.Withdrawal, 0))
}
if block.Header().EmptyRequestsHash() {
block = block.WithRequests(make([]*types.Request, 0))
}

defer func() { f.done <- hash }()
// Quickly validate the header and propagate the block if it passes
Expand Down
7 changes: 5 additions & 2 deletions eth/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,14 @@ func newHandler(config *handlerConfig) (*handler, error) {

broadcastBlockWithCheck := func(block *types.Block, propagate bool) {
if propagate {
if !(block.Header().WithdrawalsHash == nil && block.Withdrawals() == nil) &&
!(block.Header().EmptyWithdrawalsHash() && block.Withdrawals() != nil && len(block.Withdrawals()) == 0) {
if block.Header().WithdrawalsHash == nil && block.Withdrawals() != nil {
log.Error("Propagated block has invalid withdrawals")
return
}
if block.Header().RequestsHash == nil && block.Requests() != nil {
log.Error("Propagated block has invalid requests")
return
}
if err := core.IsDataAvailable(h.chain, block); err != nil {
log.Error("Propagating block with invalid sidecars", "number", block.Number(), "hash", block.Hash(), "err", err)
return
Expand Down
19 changes: 14 additions & 5 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,9 @@ func (w *worker) updateSnapshot(env *environment) {
if env.header.EmptyWithdrawalsHash() {
body.Withdrawals = make([]*types.Withdrawal, 0)
}
if env.header.EmptyRequestsHash() {
body.Requests = make([]*types.Request, 0)
}
w.snapshotBlock = types.NewBlock(
env.header,
&body,
Expand Down Expand Up @@ -1024,13 +1027,16 @@ func (w *worker) prepareWork(genParams *generateParams, witness bool) (*environm
}
header.BlobGasUsed = new(uint64)
header.ExcessBlobGas = &excessBlobGas
if w.chainConfig.Parlia != nil {
header.WithdrawalsHash = &types.EmptyWithdrawalsHash
}
if w.chainConfig.Parlia == nil {
header.ParentBeaconRoot = genParams.beaconRoot
} else if w.chainConfig.IsBohr(header.Number, header.Time) {
header.ParentBeaconRoot = new(common.Hash)
} else {
header.WithdrawalsHash = &types.EmptyWithdrawalsHash
if w.chainConfig.IsBohr(header.Number, header.Time) {
header.ParentBeaconRoot = new(common.Hash)
if w.chainConfig.IsPrague(header.Number, header.Time) {
header.RequestsHash = &types.EmptyRequestsHash
}
}
}
}
// Could potentially happen if starting to mine in an odd state.
Expand Down Expand Up @@ -1429,6 +1435,9 @@ func (w *worker) commit(env *environment, interval func(), update bool, start ti
if env.header.EmptyWithdrawalsHash() {
body.Withdrawals = make([]*types.Withdrawal, 0)
}
if env.header.EmptyRequestsHash() {
body.Requests = make([]*types.Request, 0)
}
block, receipts, err := w.engine.FinalizeAndAssemble(w.chain, types.CopyHeader(env.header), env.state, &body, env.receipts)
if err != nil {
return err
Expand Down

0 comments on commit 9185f29

Please sign in to comment.