Skip to content

Commit aded1ca

Browse files
committed
core/types,catalyst,engine: add new engine api methods for prague and 6110 deposit type
1 parent 4f4f9d8 commit aded1ca

File tree

8 files changed

+275
-7
lines changed

8 files changed

+275
-7
lines changed

beacon/engine/types.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var (
3535
PayloadV1 PayloadVersion = 0x1
3636
PayloadV2 PayloadVersion = 0x2
3737
PayloadV3 PayloadVersion = 0x3
38+
PayloadV4 PayloadVersion = 0x4
3839
)
3940

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

8082
// JSON type overrides for executableData.
@@ -229,6 +231,11 @@ func ExecutableDataToBlock(params ExecutableData, versionedHashes []common.Hash,
229231
h := types.DeriveSha(types.Withdrawals(params.Withdrawals), trie.NewStackTrie(nil))
230232
withdrawalsRoot = &h
231233
}
234+
var depositsRoot *common.Hash
235+
if params.Deposits != nil {
236+
h := types.DeriveSha(types.Deposits(params.Deposits), trie.NewStackTrie(nil))
237+
depositsRoot = &h
238+
}
232239
header := &types.Header{
233240
ParentHash: params.ParentHash,
234241
UncleHash: types.EmptyUncleHash,
@@ -249,8 +256,10 @@ func ExecutableDataToBlock(params ExecutableData, versionedHashes []common.Hash,
249256
ExcessBlobGas: params.ExcessBlobGas,
250257
BlobGasUsed: params.BlobGasUsed,
251258
ParentBeaconRoot: beaconRoot,
259+
DepositsHash: depositsRoot,
252260
}
253-
block := types.NewBlockWithHeader(header).WithBody(txs, nil /* uncles */).WithWithdrawals(params.Withdrawals)
261+
262+
block := types.NewBlockWithHeader(header).WithBody2(&types.Body{Transactions: txs, Uncles: nil, Withdrawals: params.Withdrawals, Deposits: params.Deposits})
254263
if block.Hash() != params.BlockHash {
255264
return nil, fmt.Errorf("blockhash mismatch, want %x, got %x", params.BlockHash, block.Hash())
256265
}
@@ -278,6 +287,7 @@ func BlockToExecutableData(block *types.Block, fees *big.Int, sidecars []*types.
278287
Withdrawals: block.Withdrawals(),
279288
BlobGasUsed: block.BlobGasUsed(),
280289
ExcessBlobGas: block.ExcessBlobGas(),
290+
Deposits: block.Deposits(),
281291
}
282292
bundle := BlobsBundleV1{
283293
Commitments: make([]hexutil.Bytes, 0),

core/types/block.go

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ type Header struct {
9393

9494
// ParentBeaconRoot was added by EIP-4788 and is ignored in legacy headers.
9595
ParentBeaconRoot *common.Hash `json:"parentBeaconBlockRoot" rlp:"optional"`
96+
97+
// DepositsHash was added by EIP-6110 and is ignored in legacy headers.
98+
DepositsHash *common.Hash `json:"depositsRoot" rlp:"optional"`
9699
}
97100

98101
// field type overrides for gencodec
@@ -171,6 +174,7 @@ type Body struct {
171174
Transactions []*Transaction
172175
Uncles []*Header
173176
Withdrawals []*Withdrawal `rlp:"optional"`
177+
Deposits []*Deposit `rlp:"optional"`
174178
}
175179

176180
// Block represents an Ethereum block.
@@ -195,6 +199,7 @@ type Block struct {
195199
uncles []*Header
196200
transactions Transactions
197201
withdrawals Withdrawals
202+
deposits Deposits
198203

199204
// caches
200205
hash atomic.Pointer[common.Hash]
@@ -212,6 +217,7 @@ type extblock struct {
212217
Txs []*Transaction
213218
Uncles []*Header
214219
Withdrawals []*Withdrawal `rlp:"optional"`
220+
Deposits []*Deposit `rlp:"optional"`
215221
}
216222

217223
// NewBlock creates a new block. The input data is copied, changes to header and to the
@@ -272,6 +278,30 @@ func NewBlockWithWithdrawals(header *Header, txs []*Transaction, uncles []*Heade
272278
return b.WithWithdrawals(withdrawals)
273279
}
274280

281+
// NewBlockWithWithdrawals creates a new block with withdrawals. The input data is copied,
282+
// changes to header and to the field values will not affect the block.
283+
//
284+
// The values of TxHash, UncleHash, ReceiptHash and Bloom in header are ignored and set to
285+
// values derived from the given txs, uncles and receipts.
286+
func NewBlockWithDeposits(header *Header, txs []*Transaction, uncles []*Header, receipts []*Receipt, withdrawals []*Withdrawal, deposits []*Deposit, hasher TrieHasher) *Block {
287+
b := NewBlockWithWithdrawals(header, txs, uncles, receipts, withdrawals, hasher)
288+
289+
if deposits == nil {
290+
b.header.DepositsHash = nil
291+
} else if len(deposits) == 0 {
292+
b.header.DepositsHash = &EmptyWithdrawalsHash
293+
} else {
294+
h := DeriveSha(Deposits(deposits), hasher)
295+
b.header.DepositsHash = &h
296+
}
297+
298+
b = b.WithWithdrawals(withdrawals)
299+
300+
// TODO(matt): copy this
301+
b.deposits = deposits
302+
return b
303+
}
304+
275305
// CopyHeader creates a deep copy of a block header.
276306
func CopyHeader(h *Header) *Header {
277307
cpy := *h
@@ -304,6 +334,10 @@ func CopyHeader(h *Header) *Header {
304334
cpy.ParentBeaconRoot = new(common.Hash)
305335
*cpy.ParentBeaconRoot = *h.ParentBeaconRoot
306336
}
337+
if h.DepositsHash != nil {
338+
cpy.DepositsHash = new(common.Hash)
339+
*cpy.DepositsHash = *h.DepositsHash
340+
}
307341
return &cpy
308342
}
309343

@@ -332,7 +366,7 @@ func (b *Block) EncodeRLP(w io.Writer) error {
332366
// Body returns the non-header content of the block.
333367
// Note the returned data is not an independent copy.
334368
func (b *Block) Body() *Body {
335-
return &Body{b.transactions, b.uncles, b.withdrawals}
369+
return &Body{b.transactions, b.uncles, b.withdrawals, b.deposits}
336370
}
337371

338372
// Accessors for body data. These do not return a copy because the content
@@ -341,6 +375,7 @@ func (b *Block) Body() *Body {
341375
func (b *Block) Uncles() []*Header { return b.uncles }
342376
func (b *Block) Transactions() Transactions { return b.transactions }
343377
func (b *Block) Withdrawals() Withdrawals { return b.withdrawals }
378+
func (b *Block) Deposits() Deposits { return b.deposits }
344379

345380
func (b *Block) Transaction(hash common.Hash) *Transaction {
346381
for _, transaction := range b.transactions {
@@ -468,6 +503,23 @@ func (b *Block) WithBody(transactions []*Transaction, uncles []*Header) *Block {
468503
return block
469504
}
470505

506+
func (b *Block) WithBody2(body *Body) *Block {
507+
block := &Block{
508+
header: b.header,
509+
transactions: make([]*Transaction, len(body.Transactions)),
510+
uncles: make([]*Header, len(body.Uncles)),
511+
withdrawals: make([]*Withdrawal, len(body.Withdrawals)),
512+
deposits: make([]*Deposit, len(body.Deposits)),
513+
}
514+
copy(block.transactions, body.Transactions)
515+
for i := range body.Uncles {
516+
block.uncles[i] = CopyHeader(body.Uncles[i])
517+
}
518+
copy(block.withdrawals, body.Withdrawals)
519+
copy(block.deposits, body.Deposits)
520+
return block
521+
}
522+
471523
// WithWithdrawals returns a copy of the block containing the given withdrawals.
472524
func (b *Block) WithWithdrawals(withdrawals []*Withdrawal) *Block {
473525
block := &Block{

core/types/deposit.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2024 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package types
18+
19+
import (
20+
"bytes"
21+
"reflect"
22+
23+
"github.com/ethereum/go-ethereum/common"
24+
"github.com/ethereum/go-ethereum/common/hexutil"
25+
"github.com/ethereum/go-ethereum/rlp"
26+
)
27+
28+
//go:generate go run github.com/fjl/gencodec -type Deposit -field-override depositMarshaling -out gen_deposit_json.go
29+
30+
// Deposit contians EIP-6110 deposit data.
31+
type Deposit struct {
32+
PublicKey BLSPublicKey `json:"pubkey"`
33+
WithdrawalCredentials common.Hash `json:"withdrawalCredentials"`
34+
Amount uint64 `json:"amount"` // in gwei
35+
Signature BLSSignature `json:"signature"`
36+
Index uint64 `json:"index"`
37+
}
38+
39+
type depositMarshaling struct {
40+
PublicKey hexutil.Bytes
41+
WithdrawalCredentials hexutil.Bytes
42+
Amount hexutil.Uint64
43+
Signature hexutil.Bytes
44+
Index hexutil.Uint64
45+
}
46+
47+
// Deposit implements DerivableList for withdrawals.
48+
type Deposits []*Deposit
49+
50+
// Len returns the length of s.
51+
func (s Deposits) Len() int { return len(s) }
52+
53+
// EncodeIndex encodes the i'th deposit to s.
54+
func (s Deposits) EncodeIndex(i int, w *bytes.Buffer) {
55+
rlp.Encode(w, s[i])
56+
}
57+
58+
// misc bls types
59+
////
60+
61+
var (
62+
pubkeyT = reflect.TypeOf(BLSPublicKey{})
63+
sigT = reflect.TypeOf(BLSSignature{})
64+
)
65+
66+
type BLSPublicKey [48]byte
67+
68+
// UnmarshalJSON parses a hash in hex syntax.
69+
func (h *BLSPublicKey) UnmarshalJSON(input []byte) error {
70+
return hexutil.UnmarshalFixedJSON(pubkeyT, input, h[:])
71+
}
72+
73+
// MarshalText returns the hex representation of h.
74+
func (h BLSPublicKey) MarshalText() ([]byte, error) {
75+
return hexutil.Bytes(h[:]).MarshalText()
76+
}
77+
78+
type BLSSignature [96]byte
79+
80+
// UnmarshalJSON parses a hash in hex syntax.
81+
func (h *BLSSignature) UnmarshalJSON(input []byte) error {
82+
return hexutil.UnmarshalFixedJSON(sigT, input, h[:])
83+
}
84+
85+
// MarshalText returns the hex representation of h.
86+
func (h BLSSignature) MarshalText() ([]byte, error) {
87+
return hexutil.Bytes(h[:]).MarshalText()
88+
}

core/types/gen_deposit_json.go

Lines changed: 70 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/types/gen_header_json.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)