mirror of https://github.com/ethereum/go-ethereum
all: implement flat deposit requests encoding (#30425)
This implements recent changes to EIP-7685, EIP-6110, and execution-apis. --------- Co-authored-by: lightclient <lightclient@protonmail.com> Co-authored-by: Shude Li <islishude@gmail.com>pull/30563/head
parent
f8ac95e56f
commit
2936b41514
@ -1,70 +0,0 @@ |
||||
// 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 |
||||
} |
@ -1,157 +0,0 @@ |
||||
// 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