Commit 2d192f51 authored by Conner Fromknecht's avatar Conner Fromknecht

feat: deserialize L2 TxMeta in ethclient

parent 996230de
...@@ -7,6 +7,7 @@ package types ...@@ -7,6 +7,7 @@ package types
import ( import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"fmt"
"math/big" "math/big"
"github.com/ethereum-optimism/optimism/l2geth/common" "github.com/ethereum-optimism/optimism/l2geth/common"
...@@ -31,6 +32,19 @@ func (q QueueOrigin) String() string { ...@@ -31,6 +32,19 @@ func (q QueueOrigin) String() string {
} }
} }
func (q *QueueOrigin) UnmarshalJSON(b []byte) error {
switch string(b) {
case "\"sequencer\"":
*q = QueueOriginSequencer
return nil
case "\"l1\"":
*q = QueueOriginL1ToL2
return nil
default:
return fmt.Errorf("Unknown QueueOrigin: %q", b)
}
}
//go:generate gencodec -type TransactionMeta -out gen_tx_meta_json.go //go:generate gencodec -type TransactionMeta -out gen_tx_meta_json.go
type TransactionMeta struct { type TransactionMeta struct {
......
...@@ -154,6 +154,13 @@ func (ec *Client) getBlock(ctx context.Context, method string, args ...interface ...@@ -154,6 +154,13 @@ func (ec *Client) getBlock(ctx context.Context, method string, args ...interface
if tx.From != nil { if tx.From != nil {
setSenderFromServer(tx.tx, *tx.From, body.Hash) setSenderFromServer(tx.tx, *tx.From, body.Hash)
} }
meta := types.NewTransactionMeta(
tx.meta.L1BlockNumber, tx.meta.L1Timestamp,
tx.meta.L1MessageSender, tx.meta.QueueOrigin,
tx.meta.Index, tx.meta.QueueIndex,
tx.meta.RawTransaction,
)
tx.tx.SetTransactionMeta(meta)
txs[i] = tx.tx txs[i] = tx.tx
} }
return types.NewBlockWithHeader(head).WithBody(txs, uncles), nil return types.NewBlockWithHeader(head).WithBody(txs, uncles), nil
...@@ -181,10 +188,31 @@ func (ec *Client) HeaderByNumber(ctx context.Context, number *big.Int) (*types.H ...@@ -181,10 +188,31 @@ func (ec *Client) HeaderByNumber(ctx context.Context, number *big.Int) (*types.H
} }
type rpcTransaction struct { type rpcTransaction struct {
tx *types.Transaction tx *types.Transaction
meta *rpcTransactionMeta
txExtraInfo txExtraInfo
} }
//go:generate gencodec -type rpcTransactionMeta -field-override rpcTransactionMetaMarshaling -out gen_rpc_tx_meta_json.go
type rpcTransactionMeta struct {
L1BlockNumber *big.Int `json:"l1BlockNumber"`
L1Timestamp uint64 `json:"l1Timestamp"`
L1MessageSender *common.Address `json:"l1MessageSender"`
QueueOrigin types.QueueOrigin `json:"queueOrigin"`
Index *uint64 `json:"index"`
QueueIndex *uint64 `json:"queueIndex"`
RawTransaction []byte `json:"rawTransaction"`
}
type rpcTransactionMetaMarshaling struct {
L1BlockNumber *hexutil.Big
L1Timestamp hexutil.Uint64
Index *hexutil.Uint64
QueueIndex *hexutil.Uint64
RawTransaction hexutil.Bytes
}
type txExtraInfo struct { type txExtraInfo struct {
BlockNumber *string `json:"blockNumber,omitempty"` BlockNumber *string `json:"blockNumber,omitempty"`
BlockHash *common.Hash `json:"blockHash,omitempty"` BlockHash *common.Hash `json:"blockHash,omitempty"`
...@@ -195,6 +223,9 @@ func (tx *rpcTransaction) UnmarshalJSON(msg []byte) error { ...@@ -195,6 +223,9 @@ func (tx *rpcTransaction) UnmarshalJSON(msg []byte) error {
if err := json.Unmarshal(msg, &tx.tx); err != nil { if err := json.Unmarshal(msg, &tx.tx); err != nil {
return err return err
} }
if err := json.Unmarshal(msg, &tx.meta); err != nil {
return err
}
return json.Unmarshal(msg, &tx.txExtraInfo) return json.Unmarshal(msg, &tx.txExtraInfo)
} }
......
// Code generated by github.com/fjl/gencodec. DO NOT EDIT.
package ethclient
import (
"encoding/json"
"math/big"
"github.com/ethereum-optimism/optimism/l2geth/common"
"github.com/ethereum-optimism/optimism/l2geth/common/hexutil"
"github.com/ethereum-optimism/optimism/l2geth/core/types"
)
var _ = (*rpcTransactionMetaMarshaling)(nil)
// MarshalJSON marshals as JSON.
func (r rpcTransactionMeta) MarshalJSON() ([]byte, error) {
type rpcTransactionMeta struct {
L1BlockNumber *hexutil.Big `json:"l1BlockNumber"`
L1Timestamp hexutil.Uint64 `json:"l1Timestamp"`
L1MessageSender *common.Address `json:"l1MessageSender"`
QueueOrigin types.QueueOrigin `json:"queueOrigin"`
Index *hexutil.Uint64 `json:"index"`
QueueIndex *hexutil.Uint64 `json:"queueIndex"`
RawTransaction hexutil.Bytes `json:"rawTransaction"`
}
var enc rpcTransactionMeta
enc.L1BlockNumber = (*hexutil.Big)(r.L1BlockNumber)
enc.L1Timestamp = hexutil.Uint64(r.L1Timestamp)
enc.L1MessageSender = r.L1MessageSender
enc.QueueOrigin = r.QueueOrigin
enc.Index = (*hexutil.Uint64)(r.Index)
enc.QueueIndex = (*hexutil.Uint64)(r.QueueIndex)
enc.RawTransaction = r.RawTransaction
return json.Marshal(&enc)
}
// UnmarshalJSON unmarshals from JSON.
func (r *rpcTransactionMeta) UnmarshalJSON(input []byte) error {
type rpcTransactionMeta struct {
L1BlockNumber *hexutil.Big `json:"l1BlockNumber"`
L1Timestamp *hexutil.Uint64 `json:"l1Timestamp"`
L1MessageSender *common.Address `json:"l1MessageSender"`
QueueOrigin *types.QueueOrigin `json:"queueOrigin"`
Index *hexutil.Uint64 `json:"index"`
QueueIndex *hexutil.Uint64 `json:"queueIndex"`
RawTransaction *hexutil.Bytes `json:"rawTransaction"`
}
var dec rpcTransactionMeta
if err := json.Unmarshal(input, &dec); err != nil {
return err
}
if dec.L1BlockNumber != nil {
r.L1BlockNumber = (*big.Int)(dec.L1BlockNumber)
}
if dec.L1Timestamp != nil {
r.L1Timestamp = uint64(*dec.L1Timestamp)
}
if dec.L1MessageSender != nil {
r.L1MessageSender = dec.L1MessageSender
}
if dec.QueueOrigin != nil {
r.QueueOrigin = *dec.QueueOrigin
}
if dec.Index != nil {
r.Index = (*uint64)(dec.Index)
}
if dec.QueueIndex != nil {
r.QueueIndex = (*uint64)(dec.QueueIndex)
}
if dec.RawTransaction != nil {
r.RawTransaction = *dec.RawTransaction
}
return nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment