mirror of https://github.com/ethereum/go-ethereum
all: implement EIP-6110, execution layer triggered deposits (#29431)
This PR implements EIP-6110: Supply validator deposits on chain. It also sketches out the base for Prague in the engine API types.pull/30390/merge
parent
de597af9c5
commit
dfd33c7792
@ -0,0 +1,103 @@ |
||||
// 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" |
||||
"encoding/binary" |
||||
"fmt" |
||||
|
||||
"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 contains EIP-6110 deposit data.
|
||||
type Deposit struct { |
||||
PublicKey [48]byte `json:"pubkey"` // public key of validator
|
||||
WithdrawalCredentials common.Hash `json:"withdrawalCredentials"` // beneficiary of the validator funds
|
||||
Amount uint64 `json:"amount"` // deposit size in Gwei
|
||||
Signature [96]byte `json:"signature"` // signature over deposit msg
|
||||
Index uint64 `json:"index"` // deposit count value
|
||||
} |
||||
|
||||
// field type overrides for gencodec
|
||||
type depositMarshaling struct { |
||||
PublicKey hexutil.Bytes |
||||
WithdrawalCredentials hexutil.Bytes |
||||
Amount hexutil.Uint64 |
||||
Signature hexutil.Bytes |
||||
Index hexutil.Uint64 |
||||
} |
||||
|
||||
// Deposits implements DerivableList for requests.
|
||||
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]) |
||||
} |
||||
|
||||
// UnpackIntoDeposit unpacks a serialized DepositEvent.
|
||||
func UnpackIntoDeposit(data []byte) (*Deposit, error) { |
||||
if len(data) != 576 { |
||||
return nil, fmt.Errorf("deposit wrong length: want 576, have %d", len(data)) |
||||
} |
||||
var d Deposit |
||||
// The ABI encodes the position of dynamic elements first. Since there are 5
|
||||
// elements, skip over the positional data. The first 32 bytes of dynamic
|
||||
// elements also encode their actual length. Skip over that value too.
|
||||
b := 32*5 + 32 |
||||
// PublicKey is the first element. ABI encoding pads values to 32 bytes, so
|
||||
// despite BLS public keys being length 48, the value length here is 64. Then
|
||||
// skip over the next length value.
|
||||
copy(d.PublicKey[:], data[b:b+48]) |
||||
b += 48 + 16 + 32 |
||||
// WithdrawalCredentials is 32 bytes. Read that value then skip over next
|
||||
// length.
|
||||
copy(d.WithdrawalCredentials[:], data[b:b+32]) |
||||
b += 32 + 32 |
||||
// Amount is 8 bytes, but it is padded to 32. Skip over it and the next
|
||||
// length.
|
||||
d.Amount = binary.LittleEndian.Uint64(data[b : b+8]) |
||||
b += 8 + 24 + 32 |
||||
// Signature is 96 bytes. Skip over it and the next length.
|
||||
copy(d.Signature[:], data[b:b+96]) |
||||
b += 96 + 32 |
||||
// Amount is 8 bytes.
|
||||
d.Index = binary.LittleEndian.Uint64(data[b : b+8]) |
||||
|
||||
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, |
||||
} |
||||
} |
@ -0,0 +1,93 @@ |
||||
// 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 ( |
||||
"encoding/binary" |
||||
"reflect" |
||||
"testing" |
||||
|
||||
"github.com/ethereum/go-ethereum/accounts/abi" |
||||
"github.com/ethereum/go-ethereum/common" |
||||
) |
||||
|
||||
var ( |
||||
depositABI = abi.ABI{Methods: map[string]abi.Method{"DepositEvent": depositEvent}} |
||||
bytesT, _ = abi.NewType("bytes", "", nil) |
||||
depositEvent = abi.NewMethod("DepositEvent", "DepositEvent", abi.Function, "", false, false, []abi.Argument{ |
||||
{Name: "pubkey", Type: bytesT, Indexed: false}, |
||||
{Name: "withdrawal_credentials", Type: bytesT, Indexed: false}, |
||||
{Name: "amount", Type: bytesT, Indexed: false}, |
||||
{Name: "signature", Type: bytesT, Indexed: false}, |
||||
{Name: "index", Type: bytesT, Indexed: false}}, nil, |
||||
) |
||||
) |
||||
|
||||
// FuzzUnpackIntoDeposit tries roundtrip packing and unpacking of deposit events.
|
||||
func FuzzUnpackIntoDeposit(f *testing.F) { |
||||
for _, tt := range []struct { |
||||
pubkey string |
||||
wxCred string |
||||
amount string |
||||
sig string |
||||
index string |
||||
}{ |
||||
{ |
||||
pubkey: "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111", |
||||
wxCred: "2222222222222222222222222222222222222222222222222222222222222222", |
||||
amount: "3333333333333333", |
||||
sig: "444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444", |
||||
index: "5555555555555555", |
||||
}, |
||||
} { |
||||
f.Add(common.FromHex(tt.pubkey), common.FromHex(tt.wxCred), common.FromHex(tt.amount), common.FromHex(tt.sig), common.FromHex(tt.index)) |
||||
} |
||||
|
||||
f.Fuzz(func(t *testing.T, p []byte, w []byte, a []byte, s []byte, i []byte) { |
||||
var ( |
||||
pubkey [48]byte |
||||
wxCred [32]byte |
||||
amount [8]byte |
||||
sig [96]byte |
||||
index [8]byte |
||||
) |
||||
copy(pubkey[:], p) |
||||
copy(wxCred[:], w) |
||||
copy(amount[:], a) |
||||
copy(sig[:], s) |
||||
copy(index[:], i) |
||||
|
||||
want := Deposit{ |
||||
PublicKey: pubkey, |
||||
WithdrawalCredentials: wxCred, |
||||
Amount: binary.LittleEndian.Uint64(amount[:]), |
||||
Signature: sig, |
||||
Index: binary.LittleEndian.Uint64(index[:]), |
||||
} |
||||
out, err := depositABI.Pack("DepositEvent", want.PublicKey[:], want.WithdrawalCredentials[:], amount[:], want.Signature[:], index[:]) |
||||
if err != nil { |
||||
t.Fatalf("error packing deposit: %v", err) |
||||
} |
||||
got, err := UnpackIntoDeposit(out[4:]) |
||||
if err != nil { |
||||
t.Errorf("error unpacking deposit: %v", err) |
||||
} |
||||
if !reflect.DeepEqual(want, *got) { |
||||
t.Errorf("roundtrip failed: want %v, got %v", want, got) |
||||
} |
||||
}) |
||||
} |
@ -0,0 +1,70 @@ |
||||
// Code generated by github.com/fjl/gencodec. DO NOT EDIT.
|
||||
|
||||
package types |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"errors" |
||||
|
||||
"github.com/ethereum/go-ethereum/common/hexutil" |
||||
) |
||||
|
||||
var _ = (*depositMarshaling)(nil) |
||||
|
||||
// MarshalJSON marshals as JSON.
|
||||
func (d Deposit) MarshalJSON() ([]byte, error) { |
||||
type Deposit struct { |
||||
PublicKey hexutil.Bytes `json:"pubkey"` |
||||
WithdrawalCredentials hexutil.Bytes `json:"withdrawalCredentials"` |
||||
Amount hexutil.Uint64 `json:"amount"` |
||||
Signature hexutil.Bytes `json:"signature"` |
||||
Index hexutil.Uint64 `json:"index"` |
||||
} |
||||
var enc Deposit |
||||
enc.PublicKey = d.PublicKey[:] |
||||
enc.WithdrawalCredentials = d.WithdrawalCredentials[:] |
||||
enc.Amount = hexutil.Uint64(d.Amount) |
||||
enc.Signature = d.Signature[:] |
||||
enc.Index = hexutil.Uint64(d.Index) |
||||
return json.Marshal(&enc) |
||||
} |
||||
|
||||
// UnmarshalJSON unmarshals from JSON.
|
||||
func (d *Deposit) UnmarshalJSON(input []byte) error { |
||||
type Deposit struct { |
||||
PublicKey *hexutil.Bytes `json:"pubkey"` |
||||
WithdrawalCredentials *hexutil.Bytes `json:"withdrawalCredentials"` |
||||
Amount *hexutil.Uint64 `json:"amount"` |
||||
Signature *hexutil.Bytes `json:"signature"` |
||||
Index *hexutil.Uint64 `json:"index"` |
||||
} |
||||
var dec Deposit |
||||
if err := json.Unmarshal(input, &dec); err != nil { |
||||
return err |
||||
} |
||||
if dec.PublicKey != nil { |
||||
if len(*dec.PublicKey) != len(d.PublicKey) { |
||||
return errors.New("field 'pubkey' has wrong length, need 48 items") |
||||
} |
||||
copy(d.PublicKey[:], *dec.PublicKey) |
||||
} |
||||
if dec.WithdrawalCredentials != nil { |
||||
if len(*dec.WithdrawalCredentials) != len(d.WithdrawalCredentials) { |
||||
return errors.New("field 'withdrawalCredentials' has wrong length, need 32 items") |
||||
} |
||||
copy(d.WithdrawalCredentials[:], *dec.WithdrawalCredentials) |
||||
} |
||||
if dec.Amount != nil { |
||||
d.Amount = uint64(*dec.Amount) |
||||
} |
||||
if dec.Signature != nil { |
||||
if len(*dec.Signature) != len(d.Signature) { |
||||
return errors.New("field 'signature' has wrong length, need 96 items") |
||||
} |
||||
copy(d.Signature[:], *dec.Signature) |
||||
} |
||||
if dec.Index != nil { |
||||
d.Index = uint64(*dec.Index) |
||||
} |
||||
return nil |
||||
} |
@ -0,0 +1,157 @@ |
||||
// 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" |
||||
"errors" |
||||
"fmt" |
||||
"io" |
||||
|
||||
"github.com/ethereum/go-ethereum/rlp" |
||||
) |
||||
|
||||
var ( |
||||
ErrRequestTypeNotSupported = errors.New("request type not supported") |
||||
errShortTypedRequest = errors.New("typed request too short") |
||||
) |
||||
|
||||
// Request types.
|
||||
const ( |
||||
DepositRequestType = 0x00 |
||||
) |
||||
|
||||
// Request is an EIP-7685 request object. It represents execution layer
|
||||
// triggered messages bound for the consensus layer.
|
||||
type Request struct { |
||||
inner RequestData |
||||
} |
||||
|
||||
// Type returns the EIP-7685 type of the request.
|
||||
func (r *Request) Type() byte { |
||||
return r.inner.requestType() |
||||
} |
||||
|
||||
// Inner returns the inner request data.
|
||||
func (r *Request) Inner() RequestData { |
||||
return r.inner |
||||
} |
||||
|
||||
// NewRequest creates a new request.
|
||||
func NewRequest(inner RequestData) *Request { |
||||
req := new(Request) |
||||
req.inner = inner.copy() |
||||
return req |
||||
} |
||||
|
||||
// Requests implements DerivableList for requests.
|
||||
type Requests []*Request |
||||
|
||||
// Len returns the length of s.
|
||||
func (s Requests) Len() int { return len(s) } |
||||
|
||||
// EncodeIndex encodes the i'th request to s.
|
||||
func (s Requests) EncodeIndex(i int, w *bytes.Buffer) { |
||||
s[i].encode(w) |
||||
} |
||||
|
||||
// RequestData is the underlying data of a request.
|
||||
type RequestData interface { |
||||
requestType() byte |
||||
encode(*bytes.Buffer) error |
||||
decode([]byte) error |
||||
copy() RequestData // creates a deep copy and initializes all fields
|
||||
} |
||||
|
||||
// EncodeRLP implements rlp.Encoder
|
||||
func (r *Request) EncodeRLP(w io.Writer) error { |
||||
buf := encodeBufferPool.Get().(*bytes.Buffer) |
||||
defer encodeBufferPool.Put(buf) |
||||
buf.Reset() |
||||
if err := r.encode(buf); err != nil { |
||||
return err |
||||
} |
||||
return rlp.Encode(w, buf.Bytes()) |
||||
} |
||||
|
||||
// encode writes the canonical encoding of a request to w.
|
||||
func (r *Request) encode(w *bytes.Buffer) error { |
||||
w.WriteByte(r.Type()) |
||||
return r.inner.encode(w) |
||||
} |
||||
|
||||
// MarshalBinary returns the canonical encoding of the request.
|
||||
func (r *Request) MarshalBinary() ([]byte, error) { |
||||
var buf bytes.Buffer |
||||
err := r.encode(&buf) |
||||
return buf.Bytes(), err |
||||
} |
||||
|
||||
// DecodeRLP implements rlp.Decoder
|
||||
func (r *Request) DecodeRLP(s *rlp.Stream) error { |
||||
kind, size, err := s.Kind() |
||||
switch { |
||||
case err != nil: |
||||
return err |
||||
case kind == rlp.List: |
||||
return fmt.Errorf("untyped request") |
||||
case kind == rlp.Byte: |
||||
return errShortTypedRequest |
||||
default: |
||||
// First read the request payload bytes into a temporary buffer.
|
||||
b, buf, err := getPooledBuffer(size) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
defer encodeBufferPool.Put(buf) |
||||
if err := s.ReadBytes(b); err != nil { |
||||
return err |
||||
} |
||||
// Now decode the inner request.
|
||||
inner, err := r.decode(b) |
||||
if err == nil { |
||||
r.inner = inner |
||||
} |
||||
return err |
||||
} |
||||
} |
||||
|
||||
// UnmarshalBinary decodes the canonical encoding of requests.
|
||||
func (r *Request) UnmarshalBinary(b []byte) error { |
||||
inner, err := r.decode(b) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
r.inner = inner |
||||
return nil |
||||
} |
||||
|
||||
// decode decodes a request from the canonical format.
|
||||
func (r *Request) decode(b []byte) (RequestData, error) { |
||||
if len(b) <= 1 { |
||||
return nil, errShortTypedRequest |
||||
} |
||||
var inner RequestData |
||||
switch b[0] { |
||||
case DepositRequestType: |
||||
inner = new(Deposit) |
||||
default: |
||||
return nil, ErrRequestTypeNotSupported |
||||
} |
||||
err := inner.decode(b[1:]) |
||||
return inner, err |
||||
} |
Loading…
Reference in new issue