Skip to content

Commit 7eccd9f

Browse files
EvanJRichardprotolambdaroberto-bayardo
committed
Pull in basic types from op-service/eth package.
Co-authored-by: protolambda <[email protected]> Co-authored-by: Roberto Bayardo <[email protected]>
1 parent 6f57e9c commit 7eccd9f

File tree

1 file changed

+67
-4
lines changed

1 file changed

+67
-4
lines changed

op-service/eth/types.go

+67-4
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import (
55
"fmt"
66
"math/big"
77
"reflect"
8+
"strconv"
89

910
"github.com/ethereum/go-ethereum/beacon/engine"
1011
"github.com/ethereum/go-ethereum/common"
1112
"github.com/ethereum/go-ethereum/common/hexutil"
1213
"github.com/ethereum/go-ethereum/core/types"
14+
"github.com/ethereum/go-ethereum/rlp"
1315
"github.com/ethereum/go-ethereum/trie"
1416
"github.com/holiman/uint256"
1517
)
@@ -142,8 +144,7 @@ type ExecutionPayload struct {
142144
ExtraData BytesMax32 `json:"extraData"`
143145
BaseFeePerGas Uint256Quantity `json:"baseFeePerGas"`
144146
BlockHash common.Hash `json:"blockHash"`
145-
// nil if not present, pre-shanghai
146-
Withdrawals *types.Withdrawals `json:"withdrawals,omitempty"`
147+
Withdrawals *Withdrawals `json:"withdrawals,omitempty"`
147148
// Array of transaction objects, each object is a byte list (DATA) representing
148149
// TransactionType || TransactionPayload or LegacyTransaction as defined in EIP-2718
149150
Transactions []Data `json:"transactions"`
@@ -237,7 +238,7 @@ func BlockAsPayload(bl *types.Block, canyonForkTime *uint64) (*ExecutionPayload,
237238
}
238239

239240
if canyonForkTime != nil && uint64(payload.Timestamp) >= *canyonForkTime {
240-
payload.Withdrawals = &types.Withdrawals{}
241+
payload.Withdrawals = &Withdrawals{}
241242
}
242243

243244
return payload, nil
@@ -251,7 +252,7 @@ type PayloadAttributes struct {
251252
// suggested value for the coinbase field of the new payload
252253
SuggestedFeeRecipient common.Address `json:"suggestedFeeRecipient"`
253254
// Withdrawals to include into the block -- should be nil or empty depending on Shanghai enablement
254-
Withdrawals *types.Withdrawals `json:"withdrawals,omitempty"`
255+
Withdrawals *Withdrawals `json:"withdrawals,omitempty"`
255256
// Transactions to force into the block (always at the start of the transactions list).
256257
Transactions []Data `json:"transactions,omitempty"`
257258
// NoTxPool to disable adding any transactions from the transaction-pool.
@@ -317,3 +318,65 @@ type SystemConfig struct {
317318
GasLimit uint64 `json:"gasLimit"`
318319
// More fields can be added for future SystemConfig versions.
319320
}
321+
322+
// Withdrawal represents a validator withdrawal from the consensus layer.
323+
// https://github.com/ethereum/consensus-specs/blob/dev/specs/capella/beacon-chain.md#withdrawal
324+
type Withdrawal struct {
325+
Index uint64 `json:"index"` // monotonically increasing identifier issued by consensus layer
326+
Validator uint64 `json:"validatorIndex"` // index of validator associated with withdrawal
327+
Address common.Address `json:"address"` // target address for withdrawn ether
328+
Amount uint64 `json:"amount"` // value of withdrawal in Gwei
329+
}
330+
331+
// Withdrawals implements DerivableList for withdrawals.
332+
type Withdrawals []Withdrawal
333+
334+
// Len returns the length of s.
335+
func (s Withdrawals) Len() int { return len(s) }
336+
337+
// EncodeIndex encodes the i'th withdrawal to w. Note that this does not check for errors
338+
// because we assume that *Withdrawal will only ever contain valid withdrawals that were either
339+
// constructed by decoding or via public API in this package.
340+
func (s Withdrawals) EncodeIndex(i int, w *bytes.Buffer) {
341+
_ = rlp.Encode(w, s[i])
342+
}
343+
344+
type Bytes48 [48]byte
345+
346+
func (b *Bytes48) UnmarshalJSON(text []byte) error {
347+
return hexutil.UnmarshalFixedJSON(reflect.TypeOf(b), text, b[:])
348+
}
349+
350+
func (b *Bytes48) UnmarshalText(text []byte) error {
351+
return hexutil.UnmarshalFixedText("Bytes32", text, b[:])
352+
}
353+
354+
func (b Bytes48) MarshalText() ([]byte, error) {
355+
return hexutil.Bytes(b[:]).MarshalText()
356+
}
357+
358+
func (b Bytes48) String() string {
359+
return hexutil.Encode(b[:])
360+
}
361+
362+
// TerminalString implements log.TerminalStringer, formatting a string for console
363+
// output during logging.
364+
func (b Bytes48) TerminalString() string {
365+
return fmt.Sprintf("%x..%x", b[:3], b[45:])
366+
}
367+
368+
type Uint64String uint64
369+
370+
func (v Uint64String) MarshalText() (out []byte, err error) {
371+
out = strconv.AppendUint(out, uint64(v), 10)
372+
return
373+
}
374+
375+
func (v *Uint64String) UnmarshalText(b []byte) error {
376+
n, err := strconv.ParseUint(string(b), 0, 64)
377+
if err != nil {
378+
return err
379+
}
380+
*v = Uint64String(n)
381+
return nil
382+
}

0 commit comments

Comments
 (0)