Skip to content

Commit

Permalink
feat: deserialize L2 TxMeta in ethclient
Browse files Browse the repository at this point in the history
  • Loading branch information
cfromknecht committed Dec 13, 2021
1 parent 996230d commit 2d192f5
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 1 deletion.
14 changes: 14 additions & 0 deletions l2geth/core/types/transaction_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package types
import (
"bytes"
"encoding/binary"
"fmt"
"math/big"

"github.com/ethereum-optimism/optimism/l2geth/common"
Expand All @@ -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 {
Expand Down
33 changes: 32 additions & 1 deletion l2geth/ethclient/ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"`
Expand All @@ -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)
}

Expand Down
75 changes: 75 additions & 0 deletions l2geth/ethclient/gen_rpc_tx_meta_json.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2d192f5

Please sign in to comment.