Skip to content

Commit

Permalink
core/types,catalyst,engine: add new engine api methods for prague and…
Browse files Browse the repository at this point in the history
… 6110 deposit type
  • Loading branch information
lightclient committed May 14, 2024
1 parent 5b3e3cd commit 67fe7af
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 8 deletions.
11 changes: 10 additions & 1 deletion beacon/engine/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var (
PayloadV1 PayloadVersion = 0x1
PayloadV2 PayloadVersion = 0x2
PayloadV3 PayloadVersion = 0x3
PayloadV4 PayloadVersion = 0x4
)

//go:generate go run github.com/fjl/gencodec -type PayloadAttributes -field-override payloadAttributesMarshaling -out gen_blockparams.go
Expand Down Expand Up @@ -75,6 +76,7 @@ type ExecutableData struct {
Withdrawals []*types.Withdrawal `json:"withdrawals"`
BlobGasUsed *uint64 `json:"blobGasUsed"`
ExcessBlobGas *uint64 `json:"excessBlobGas"`
Deposits []*types.Deposit `json:"depositReceipts"`
}

// JSON type overrides for executableData.
Expand Down Expand Up @@ -229,6 +231,11 @@ func ExecutableDataToBlock(params ExecutableData, versionedHashes []common.Hash,
h := types.DeriveSha(types.Withdrawals(params.Withdrawals), trie.NewStackTrie(nil))
withdrawalsRoot = &h
}
var depositsRoot *common.Hash
if params.Deposits != nil {
h := types.DeriveSha(types.Deposits(params.Deposits), trie.NewStackTrie(nil))
depositsRoot = &h
}
header := &types.Header{
ParentHash: params.ParentHash,
UncleHash: types.EmptyUncleHash,
Expand All @@ -249,8 +256,9 @@ func ExecutableDataToBlock(params ExecutableData, versionedHashes []common.Hash,
ExcessBlobGas: params.ExcessBlobGas,
BlobGasUsed: params.BlobGasUsed,
ParentBeaconRoot: beaconRoot,
DepositsHash: depositsRoot,
}
block := types.NewBlockWithHeader(header).WithBody(types.Body{Transactions: txs, Uncles: nil, Withdrawals: params.Withdrawals})
block := types.NewBlockWithHeader(header).WithBody(types.Body{Transactions: txs, Uncles: nil, Withdrawals: params.Withdrawals, Deposits: params.Deposits})
if block.Hash() != params.BlockHash {
return nil, fmt.Errorf("blockhash mismatch, want %x, got %x", params.BlockHash, block.Hash())
}
Expand Down Expand Up @@ -278,6 +286,7 @@ func BlockToExecutableData(block *types.Block, fees *big.Int, sidecars []*types.
Withdrawals: block.Withdrawals(),
BlobGasUsed: block.BlobGasUsed(),
ExcessBlobGas: block.ExcessBlobGas(),
Deposits: block.Deposits(),
}
bundle := BlobsBundleV1{
Commitments: make([]hexutil.Bytes, 0),
Expand Down
13 changes: 12 additions & 1 deletion core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ 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"`
}

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

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

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

// NewBlock creates a new block. The input data is copied, changes to header and to the
Expand Down Expand Up @@ -302,6 +308,10 @@ 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
}
return &cpy
}

Expand Down Expand Up @@ -330,7 +340,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}
return &Body{b.transactions, b.uncles, b.withdrawals, b.deposits}
}

// Accessors for body data. These do not return a copy because the content
Expand All @@ -339,6 +349,7 @@ 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) Transaction(hash common.Hash) *Transaction {
for _, transaction := range b.transactions {
Expand Down
88 changes: 88 additions & 0 deletions core/types/deposit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright 2024 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package types

import (
"bytes"
"reflect"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/rlp"
)

//go:generate go run github.com/fjl/gencodec -type Deposit -field-override depositMarshaling -out gen_deposit_json.go

// Deposit contians EIP-6110 deposit data.
type Deposit struct {
PublicKey BLSPublicKey `json:"pubkey"`
WithdrawalCredentials common.Hash `json:"withdrawalCredentials"`
Amount uint64 `json:"amount"` // in gwei
Signature BLSSignature `json:"signature"`
Index uint64 `json:"index"`
}

type depositMarshaling struct {
PublicKey hexutil.Bytes
WithdrawalCredentials hexutil.Bytes
Amount hexutil.Uint64
Signature hexutil.Bytes
Index hexutil.Uint64
}

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

// Len returns the length of s.
func (s Deposits) Len() int { return len(s) }

// EncodeIndex encodes the i'th deposit to s.
func (s Deposits) EncodeIndex(i int, w *bytes.Buffer) {
rlp.Encode(w, s[i])
}

// misc bls types
////

var (
pubkeyT = reflect.TypeOf(BLSPublicKey{})
sigT = reflect.TypeOf(BLSSignature{})
)

type BLSPublicKey [48]byte

// UnmarshalJSON parses a hash in hex syntax.
func (h *BLSPublicKey) UnmarshalJSON(input []byte) error {
return hexutil.UnmarshalFixedJSON(pubkeyT, input, h[:])
}

// MarshalText returns the hex representation of h.
func (h BLSPublicKey) MarshalText() ([]byte, error) {
return hexutil.Bytes(h[:]).MarshalText()
}

type BLSSignature [96]byte

// UnmarshalJSON parses a hash in hex syntax.
func (h *BLSSignature) UnmarshalJSON(input []byte) error {
return hexutil.UnmarshalFixedJSON(sigT, input, h[:])
}

// MarshalText returns the hex representation of h.
func (h BLSSignature) MarshalText() ([]byte, error) {
return hexutil.Bytes(h[:]).MarshalText()
}
70 changes: 70 additions & 0 deletions core/types/gen_deposit_json.go

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

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.

18 changes: 13 additions & 5 deletions core/types/gen_header_rlp.go

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

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")

// EmptyDepositsHash is the known hash of the empty deposits set.
EmptyDepositsHash = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")

// EmptyVerkleHash is the known hash of an empty verkle trie.
EmptyVerkleHash = common.Hash{}
)
Expand Down
Loading

0 comments on commit 67fe7af

Please sign in to comment.