diff --git a/l2geth/core/types/transaction_meta.go b/l2geth/core/types/transaction_meta.go index 3fb6c6cf50ae..760bb6df43d5 100644 --- a/l2geth/core/types/transaction_meta.go +++ b/l2geth/core/types/transaction_meta.go @@ -7,6 +7,7 @@ package types import ( "bytes" "encoding/binary" + "fmt" "math/big" "github.com/ethereum-optimism/optimism/l2geth/common" @@ -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 type TransactionMeta struct { diff --git a/l2geth/ethclient/ethclient.go b/l2geth/ethclient/ethclient.go index 3c89ebc4a7ea..1a70ed71b75c 100644 --- a/l2geth/ethclient/ethclient.go +++ b/l2geth/ethclient/ethclient.go @@ -154,6 +154,13 @@ func (ec *Client) getBlock(ctx context.Context, method string, args ...interface if tx.From != nil { 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 } return types.NewBlockWithHeader(head).WithBody(txs, uncles), nil @@ -181,10 +188,31 @@ func (ec *Client) HeaderByNumber(ctx context.Context, number *big.Int) (*types.H } type rpcTransaction struct { - tx *types.Transaction + tx *types.Transaction + meta *rpcTransactionMeta 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 { BlockNumber *string `json:"blockNumber,omitempty"` BlockHash *common.Hash `json:"blockHash,omitempty"` @@ -195,6 +223,9 @@ func (tx *rpcTransaction) UnmarshalJSON(msg []byte) error { if err := json.Unmarshal(msg, &tx.tx); err != nil { return err } + if err := json.Unmarshal(msg, &tx.meta); err != nil { + return err + } return json.Unmarshal(msg, &tx.txExtraInfo) } diff --git a/l2geth/ethclient/gen_rpc_tx_meta_json.go b/l2geth/ethclient/gen_rpc_tx_meta_json.go new file mode 100644 index 000000000000..a167cbee1abf --- /dev/null +++ b/l2geth/ethclient/gen_rpc_tx_meta_json.go @@ -0,0 +1,75 @@ +// 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 +}