Skip to content

Commit

Permalink
all: begin using 7685 request objects
Browse files Browse the repository at this point in the history
  • Loading branch information
lightclient committed Apr 23, 2024
1 parent 9618592 commit b6ebd84
Show file tree
Hide file tree
Showing 14 changed files with 269 additions and 73 deletions.
17 changes: 11 additions & 6 deletions beacon/engine/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ type ExecutableData struct {
Withdrawals []*types.Withdrawal `json:"withdrawals"`
BlobGasUsed *uint64 `json:"blobGasUsed"`
ExcessBlobGas *uint64 `json:"excessBlobGas"`
Deposits []*types.Deposit `json:"depositReceipts"`
Deposits types.Deposits `json:"depositRequests"`
}

// JSON type overrides for executableData.
Expand Down Expand Up @@ -231,10 +231,15 @@ func ExecutableDataToBlock(params ExecutableData, versionedHashes []common.Hash,
h := types.DeriveSha(types.Withdrawals(params.Withdrawals), trie.NewStackTrie(nil))
withdrawalsRoot = &h
}
var depositsRoot *common.Hash

var (
requestsHash *common.Hash
requests types.Requests
)
if params.Deposits != nil {
h := types.DeriveSha(types.Deposits(params.Deposits), trie.NewStackTrie(nil))
depositsRoot = &h
requests = params.Deposits.Requests()
h := types.DeriveSha(requests, trie.NewStackTrie(nil))
requestsHash = &h
}
header := &types.Header{
ParentHash: params.ParentHash,
Expand All @@ -256,10 +261,10 @@ func ExecutableDataToBlock(params ExecutableData, versionedHashes []common.Hash,
ExcessBlobGas: params.ExcessBlobGas,
BlobGasUsed: params.BlobGasUsed,
ParentBeaconRoot: beaconRoot,
DepositsHash: depositsRoot,
RequestsHash: requestsHash,
}

block := types.NewBlockWithHeader(header).WithBody2(&types.Body{Transactions: txs, Uncles: nil, Withdrawals: params.Withdrawals, Deposits: params.Deposits})
block := types.NewBlockWithHeader(header).WithBody2(&types.Body{Transactions: txs, Uncles: nil, Withdrawals: params.Withdrawals, Requests: requests})
if block.Hash() != params.BlockHash {
return nil, fmt.Errorf("blockhash mismatch, want %x, got %x", params.BlockHash, block.Hash())
}
Expand Down
2 changes: 1 addition & 1 deletion consensus/beacon/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea
header.Root = state.IntermediateRoot(true)

// Assemble and return the final block.
return types.NewBlockWithDeposits(header, body.Transactions, body.Uncles, receipts, body.Withdrawals, body.Deposits, trie.NewStackTrie(nil)), nil
return types.NewBlockWithRequests(header, body.Transactions, body.Uncles, receipts, body.Withdrawals, body.Requests, trie.NewStackTrie(nil)), nil
}

// Seal generates a new sealing request for the given input block and pushes
Expand Down
10 changes: 5 additions & 5 deletions core/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.StateD
if receiptSha != header.ReceiptHash {
return fmt.Errorf("invalid receipt root hash (remote: %x local: %x)", header.ReceiptHash, receiptSha)
}
// Validate the parsed deposits match the expected header value.
if header.DepositsHash != nil {
depositSha := types.DeriveSha(res.Deposits, trie.NewStackTrie(nil))
if depositSha != *header.DepositsHash {
return fmt.Errorf("invalid deposit root hash (remote: %x local: %x)", *header.DepositsHash, depositSha)
// Validate the parsed requests match the expected header value.
if header.RequestsHash != nil {
depositSha := types.DeriveSha(res.Requests, trie.NewStackTrie(nil))
if depositSha != *header.RequestsHash {
return fmt.Errorf("invalid deposit root hash (remote: %x local: %x)", *header.RequestsHash, depositSha)
}
}
// Validate the state root against the received state root and throw
Expand Down
6 changes: 3 additions & 3 deletions core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,18 +346,18 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse
gen(i, b)
}

var deposits types.Deposits
var requests types.Requests
if config.IsPrague(b.header.Number, b.header.Time) {
for _, r := range b.receipts {
d, err := ParseDepositLogs(r.Logs)
if err != nil {
panic(fmt.Sprintf("failed to parse deposit log: %v", err))
}
deposits = append(deposits, d...)
requests = append(requests, d...)
}
}

body := types.Body{Transactions: b.txs, Uncles: b.uncles, Withdrawals: b.withdrawals, Deposits: deposits}
body := types.Body{Transactions: b.txs, Uncles: b.uncles, Withdrawals: b.withdrawals, Requests: requests}
block, err := b.engine.FinalizeAndAssemble(cm, b.header, statedb, &body, b.receipts)
if err != nil {
panic(err)
Expand Down
6 changes: 3 additions & 3 deletions core/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ func ReadBlock(db ethdb.Reader, hash common.Hash, number uint64) *types.Block {
if body == nil {
return nil
}
return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles).WithWithdrawals(body.Withdrawals).WithDeposits(body.Deposits)
return types.NewBlockWithHeader(header).WithBody2(body)
}

// WriteBlock serializes a block into the database, header and body separately.
Expand Down Expand Up @@ -843,7 +843,7 @@ func ReadBadBlock(db ethdb.Reader, hash common.Hash) *types.Block {
}
for _, bad := range badBlocks {
if bad.Header.Hash() == hash {
return types.NewBlockWithHeader(bad.Header).WithBody(bad.Body.Transactions, bad.Body.Uncles).WithWithdrawals(bad.Body.Withdrawals)
return types.NewBlockWithHeader(bad.Header).WithBody2(bad.Body)
}
}
return nil
Expand All @@ -862,7 +862,7 @@ func ReadAllBadBlocks(db ethdb.Reader) []*types.Block {
}
var blocks []*types.Block
for _, bad := range badBlocks {
blocks = append(blocks, types.NewBlockWithHeader(bad.Header).WithBody(bad.Body.Transactions, bad.Body.Uncles).WithWithdrawals(bad.Body.Withdrawals).WithDeposits(bad.Body.Deposits))
blocks = append(blocks, types.NewBlockWithHeader(bad.Header).WithBody2(bad.Body))
}
return blocks
}
Expand Down
14 changes: 7 additions & 7 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
if len(withdrawals) > 0 && !p.config.IsShanghai(block.Number(), block.Time()) {
return nil, errors.New("withdrawals before shanghai")
}
// Read deposits if Prague is enabled.
var deposits types.Deposits
// Read requests if Prague is enabled.
var requests types.Requests
if p.config.IsPrague(block.Number(), block.Time()) {
deposits, err = ParseDepositLogs(allLogs)
requests, err = ParseDepositLogs(allLogs)
if err != nil {
return nil, err
}
Expand All @@ -115,7 +115,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg

return &ProcessResult{
Receipts: receipts,
Deposits: deposits,
Requests: requests,
Logs: allLogs,
GasUsed: *usedGas,
}, nil
Expand Down Expand Up @@ -220,15 +220,15 @@ func ProcessBeaconBlockRoot(beaconRoot common.Hash, vmenv *vm.EVM, statedb *stat

// ParseDepositLogs extracts the EIP-6110 deposit values from logs emitted by
// BeaconDepositContract.
func ParseDepositLogs(logs []*types.Log) (types.Deposits, error) {
var deposits types.Deposits
func ParseDepositLogs(logs []*types.Log) (types.Requests, error) {
var deposits types.Requests
for _, log := range logs {
if log.Address == params.BeaconDepositContractAddress {
d, err := types.UnpackIntoDeposit(log.Data)
if err != nil {
return nil, fmt.Errorf("unable to parse deposit data: %v", err)
}
deposits = append(deposits, d)
deposits = append(deposits, types.NewRequest(d))
}
}
return deposits, nil
Expand Down
2 changes: 1 addition & 1 deletion core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type Processor interface {
// ProcessResult contains the values computed by Process.
type ProcessResult struct {
Receipts types.Receipts
Deposits types.Deposits
Requests types.Requests
Logs []*types.Log
GasUsed uint64
}
75 changes: 42 additions & 33 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io"
"math/big"
"reflect"
"slices"
"sync/atomic"
"time"

Expand Down Expand Up @@ -94,8 +95,8 @@ type Header struct {
// ParentBeaconRoot was added by EIP-4788 and is ignored in legacy headers.
ParentBeaconRoot *common.Hash `json:"parentBeaconBlockRoot" rlp:"optional"`

// DepositsHash was added by EIP-6110 and is ignored in legacy headers.
DepositsHash *common.Hash `json:"depositsRoot" rlp:"optional"`
// RequestsHash was added by EIP-7685 and is ignored in legacy headers.
RequestsHash *common.Hash `json:"requestsRoot" rlp:"optional"`
}

// field type overrides for gencodec
Expand Down Expand Up @@ -174,7 +175,7 @@ type Body struct {
Transactions []*Transaction
Uncles []*Header
Withdrawals []*Withdrawal `rlp:"optional"`
Deposits []*Deposit `rlp:"optional"`
Requests []*Request `rlp:"optional"`
}

// Block represents an Ethereum block.
Expand All @@ -199,7 +200,7 @@ type Block struct {
uncles []*Header
transactions Transactions
withdrawals Withdrawals
deposits Deposits
requests Requests

// caches
hash atomic.Pointer[common.Hash]
Expand All @@ -217,7 +218,7 @@ type extblock struct {
Txs []*Transaction
Uncles []*Header
Withdrawals []*Withdrawal `rlp:"optional"`
Deposits []*Deposit `rlp:"optional"`
Requests []*Request `rlp:"optional"`
}

// NewBlock creates a new block. The input data is copied, changes to header and to the
Expand Down Expand Up @@ -278,27 +279,24 @@ func NewBlockWithWithdrawals(header *Header, txs []*Transaction, uncles []*Heade
return b.WithWithdrawals(withdrawals)
}

// NewBlockWithDeposits creates a new block with withdrawals. The input data is copied,
// NewBlockWithRequests creates a new block with withdrawals. The input data is copied,
// changes to header and to the field values will not affect the block.
//
// The values of TxHash, UncleHash, ReceiptHash and Bloom in header are ignored and set to
// values derived from the given txs, uncles and receipts.
func NewBlockWithDeposits(header *Header, txs []*Transaction, uncles []*Header, receipts []*Receipt, withdrawals []*Withdrawal, deposits []*Deposit, hasher TrieHasher) *Block {
func NewBlockWithRequests(header *Header, txs Transactions, uncles []*Header, receipts Receipts, withdrawals Withdrawals, requests Requests, hasher TrieHasher) *Block {
b := NewBlockWithWithdrawals(header, txs, uncles, receipts, withdrawals, hasher)

if deposits == nil {
b.header.DepositsHash = nil
} else if len(deposits) == 0 {
b.header.DepositsHash = &EmptyWithdrawalsHash
if requests == nil {
b.header.RequestsHash = nil
} else if len(requests) == 0 {
b.header.RequestsHash = &EmptyWithdrawalsHash
} else {
h := DeriveSha(Deposits(deposits), hasher)
b.header.DepositsHash = &h
h := DeriveSha(requests, hasher)
b.header.RequestsHash = &h
}

b = b.WithWithdrawals(withdrawals)

// TODO(matt): copy this
b.deposits = deposits
b.requests = slices.Clone(requests)
return b
}

Expand Down Expand Up @@ -334,9 +332,9 @@ func CopyHeader(h *Header) *Header {
cpy.ParentBeaconRoot = new(common.Hash)
*cpy.ParentBeaconRoot = *h.ParentBeaconRoot
}
if h.DepositsHash != nil {
cpy.DepositsHash = new(common.Hash)
*cpy.DepositsHash = *h.DepositsHash
if h.RequestsHash != nil {
cpy.RequestsHash = new(common.Hash)
*cpy.RequestsHash = *h.RequestsHash
}
return &cpy
}
Expand Down Expand Up @@ -366,7 +364,7 @@ func (b *Block) EncodeRLP(w io.Writer) error {
// Body returns the non-header content of the block.
// Note the returned data is not an independent copy.
func (b *Block) Body() *Body {
return &Body{b.transactions, b.uncles, b.withdrawals, b.deposits}
return &Body{b.transactions, b.uncles, b.withdrawals, b.requests}
}

// Accessors for body data. These do not return a copy because the content
Expand All @@ -375,7 +373,17 @@ func (b *Block) Body() *Body {
func (b *Block) Uncles() []*Header { return b.uncles }
func (b *Block) Transactions() Transactions { return b.transactions }
func (b *Block) Withdrawals() Withdrawals { return b.withdrawals }
func (b *Block) Deposits() Deposits { return b.deposits }
func (b *Block) Requests() Requests { return b.requests }

func (b *Block) Deposits() Deposits {
var deps Deposits
for _, r := range b.requests {
if d, ok := r.inner.(*Deposit); ok {
deps = append(deps, d)
}
}
return deps
}

func (b *Block) Transaction(hash common.Hash) *Transaction {
for _, transaction := range b.transactions {
Expand Down Expand Up @@ -506,17 +514,18 @@ func (b *Block) WithBody(transactions []*Transaction, uncles []*Header) *Block {
func (b *Block) WithBody2(body *Body) *Block {
block := &Block{
header: b.header,
transactions: make([]*Transaction, len(body.Transactions)),
transactions: slices.Clone(body.Transactions),
uncles: make([]*Header, len(body.Uncles)),
withdrawals: make([]*Withdrawal, len(body.Withdrawals)),
deposits: make([]*Deposit, len(body.Deposits)),
}
copy(block.transactions, body.Transactions)
for i := range body.Uncles {
block.uncles[i] = CopyHeader(body.Uncles[i])
}
copy(block.withdrawals, body.Withdrawals)
copy(block.deposits, body.Deposits)
if body.Withdrawals != nil {
block.withdrawals = slices.Clone(body.Withdrawals)
}
if body.Requests != nil {
block.requests = slices.Clone(body.Requests)
}
return block
}

Expand All @@ -534,17 +543,17 @@ func (b *Block) WithWithdrawals(withdrawals []*Withdrawal) *Block {
return block
}

// WithDeposits returns a copy of the block containing the given deposits.
func (b *Block) WithDeposits(deposits []*Deposit) *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,
}
if deposits != nil {
block.deposits = make([]*Deposit, len(deposits))
copy(block.deposits, deposits)
if requests != nil {
block.requests = make([]*Request, len(requests))
copy(block.requests, requests)
}
return block
}
Expand Down
24 changes: 23 additions & 1 deletion core/types/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type depositUnpacking struct {
Index []byte
}

// Deposit implements DerivableList for withdrawals.
// Deposits implements DerivableList for requests.
type Deposits []*Deposit

// Len returns the length of s.
Expand All @@ -67,6 +67,15 @@ func (s Deposits) EncodeIndex(i int, w *bytes.Buffer) {
rlp.Encode(w, s[i])
}

// Requests creates a deep copy of each deposit and returns a slice of the
// deposits as Request objects.
func (s Deposits) Requests() (reqs Requests) {
for _, d := range s {
reqs = append(reqs, NewRequest(d))
}
return
}

var (
// DepositABI is an ABI instance of beacon chain deposit events.
DepositABI = abi.ABI{Events: map[string]abi.Event{"DepositEvent": depositEvent}}
Expand Down Expand Up @@ -96,6 +105,19 @@ func UnpackIntoDeposit(data []byte) (*Deposit, error) {
return &d, nil
}

func (d *Deposit) requestType() byte { return DepositRequestType }
func (d *Deposit) encode(b *bytes.Buffer) error { return rlp.Encode(b, d) }
func (d *Deposit) decode(input []byte) error { return rlp.DecodeBytes(input, d) }
func (d *Deposit) copy() RequestData {
return &Deposit{
PublicKey: d.PublicKey,
WithdrawalCredentials: d.WithdrawalCredentials,
Amount: d.Amount,
Signature: d.Signature,
Index: d.Index,
}
}

// misc bls types
////

Expand Down

0 comments on commit b6ebd84

Please sign in to comment.