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 Dec 10, 2024
1 parent 2849c07 commit f1892d8
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 22 deletions.
10 changes: 5 additions & 5 deletions consensus/parlia/parlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,11 +651,11 @@ func (p *Parlia) verifyHeader(chain consensus.ChainHeaderReader, header *types.H
if header.RequestsHash != nil {
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")
// }
} else {
if !header.EmptyRequestsHash() {
log.Info("verifyHeader", "header.EmptyRequestsHash", header.EmptyRequestsHash())
return errors.New("header has wrong RequestsHash")
}
}

// All basic checks passed, verify cascading fields
Expand Down
18 changes: 13 additions & 5 deletions core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,8 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse
}

var requests [][]byte
if config.IsPrague(b.header.Number, b.header.Time) {
isPrague := config.IsPrague(b.header.Number, b.header.Time)
if isPrague && config.Parlia == nil {
// EIP-6110 deposits
var blockLogs []*types.Log
for _, r := range b.receipts {
Expand All @@ -391,6 +392,8 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse
// EIP-7251 consolidations
consolidationRequests := ProcessConsolidationQueue(vmenv, statedb)
requests = append(requests, consolidationRequests)
} else if isPrague && config.Parlia != nil {
requests = make([][]byte, 0)
}
if requests != nil {
reqHash := types.CalcRequestsHash(requests)
Expand Down Expand Up @@ -612,11 +615,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 @@ -480,9 +480,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
5 changes: 4 additions & 1 deletion core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg

// Read requests if Prague is enabled.
var requests [][]byte
if p.config.IsPrague(block.Number(), block.Time()) {
isPrague := p.config.IsPrague(block.Number(), block.Time())
if isPrague && p.chain.config.Parlia == nil {
var allCommonLogs []*types.Log
for _, receipt := range receipts {
allCommonLogs = append(allCommonLogs, receipt.Logs...)
Expand All @@ -165,6 +166,8 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
// EIP-7251 consolidations
consolidationRequests := ProcessConsolidationQueue(vmenv, tracingStateDB)
requests = append(requests, consolidationRequests)
} else if isPrague && p.chain.config.Parlia != nil {
requests = make([][]byte, 0)
}

// Finalize the block, applying any consensus engine specific extras (e.g. block rewards)
Expand Down
13 changes: 10 additions & 3 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ func (h *Header) EmptyWithdrawalsHash() bool {
return h.WithdrawalsHash != nil && *h.WithdrawalsHash == EmptyWithdrawalsHash
}

// EmptyRequestsHash returns true if the RequestsHash is EmptyRequestsHash.
func (h *Header) EmptyRequestsHash() bool {
return h.RequestsHash != nil && *h.RequestsHash == EmptyRequestsHash
}

// Body is a simple (mutable, non-safe) data container for storing and moving
// a block's data contents (transactions and uncles) together.
type Body struct {
Expand Down Expand Up @@ -509,9 +514,11 @@ func CalcRequestsHash(requests [][]byte) common.Hash {
h1, h2 := sha256.New(), sha256.New()
var buf common.Hash
for _, item := range requests {
h1.Reset()
h1.Write(item)
h2.Write(h1.Sum(buf[:0]))
if len(item) > 1 { // skip items with only requestType and no data.
h1.Reset()
h1.Write(item)
h2.Write(h1.Sum(buf[:0]))
}
}
h2.Sum(buf[:0])
return buf
Expand Down
3 changes: 3 additions & 0 deletions core/types/hashes.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ var (
// EmptyWithdrawalsHash is the known hash of the empty withdrawal set.
EmptyWithdrawalsHash = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")

// EmptyRequestsHash is the known hash of the empty requests set.
EmptyRequestsHash = common.HexToHash("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")

// EmptyVerkleHash is the known hash of an empty verkle trie.
EmptyVerkleHash = common.Hash{}
)
Expand Down
13 changes: 8 additions & 5 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1025,13 +1025,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

0 comments on commit f1892d8

Please sign in to comment.