Skip to content

Commit

Permalink
refactor[l2geth]: queue origin type (#975)
Browse files Browse the repository at this point in the history
* refactor: queueOrigin type

* Convert queueOrigin to uint8 in encode

* Add changeset

* Regenerate json marshall

* style: combine lines

* Add Stringer for QueueOrigin

* Turn QueueOrigin into uint8

* l2geth: gen tx meta fix

* l2geth: gen tx meta fix

* lint

Co-authored-by: Mark Tyneway <mark.tyneway@gmail.com>
  • Loading branch information
karlfloersch and tynes authored Jun 2, 2021
1 parent c54de60 commit 0887367
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 85 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-planets-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@eth-optimism/l2geth': patch
---

Update queueOrigin type
2 changes: 1 addition & 1 deletion l2geth/accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ func (m callmsg) Data() []byte { return m.CallMsg.Data }

func (m callmsg) L1MessageSender() *common.Address { return m.CallMsg.L1MessageSender }
func (m callmsg) L1BlockNumber() *big.Int { return m.CallMsg.L1BlockNumber }
func (m callmsg) QueueOrigin() *big.Int { return m.CallMsg.QueueOrigin }
func (m callmsg) QueueOrigin() types.QueueOrigin { return m.CallMsg.QueueOrigin }

// filterBackend implements filters.Backend to support filtering for logs without
// taking bloom-bits acceleration structures into account.
Expand Down
6 changes: 2 additions & 4 deletions l2geth/core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type Message interface {
Data() []byte
L1MessageSender() *common.Address
L1BlockNumber() *big.Int
QueueOrigin() *big.Int
QueueOrigin() types.QueueOrigin
}

// IntrinsicGas computes the 'intrinsic gas' for a message with the given data.
Expand Down Expand Up @@ -184,9 +184,7 @@ func (st *StateTransition) preCheck() error {
if nonce < st.msg.Nonce() {
if vm.UsingOVM {
// The nonce never increments for L1ToL2 txs
qo := st.msg.QueueOrigin()
l1ToL2 := uint64(types.QueueOriginL1ToL2)
if qo != nil && qo.Uint64() == l1ToL2 {
if st.msg.QueueOrigin() == types.QueueOriginL1ToL2 {
return st.buyGas()
}
}
Expand Down
28 changes: 4 additions & 24 deletions l2geth/core/state_transition_ovm.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func toExecutionManagerRun(evm *vm.EVM, msg Message) (Message, error) {
tx := ovmTransaction{
evm.Context.Time,
msg.L1BlockNumber(),
uint8(msg.QueueOrigin().Uint64()),
uint8(msg.QueueOrigin()),
*msg.L1MessageSender(),
*msg.To(),
big.NewInt(int64(msg.Gas())),
Expand Down Expand Up @@ -73,8 +73,7 @@ func AsOvmMessage(tx *types.Transaction, signer types.Signer, decompressor commo
// sequencer entrypoint. The calldata is expected to be in the
// correct format when deserialized from the EVM events, see
// rollup/sync_service.go.
qo := msg.QueueOrigin()
if qo != nil && qo.Uint64() == uint64(types.QueueOriginL1ToL2) {
if msg.QueueOrigin() == types.QueueOriginL1ToL2 {
return msg, nil
}

Expand Down Expand Up @@ -104,7 +103,7 @@ func EncodeSimulatedMessage(msg Message, timestamp, blockNumber *big.Int, execut
tx := ovmTransaction{
timestamp,
blockNumber,
uint8(msg.QueueOrigin().Uint64()),
uint8(msg.QueueOrigin()),
*msg.L1MessageSender(),
*to,
big.NewInt(int64(msg.Gas())),
Expand Down Expand Up @@ -139,11 +138,6 @@ func modMessage(
data []byte,
gasLimit uint64,
) (Message, error) {
queueOrigin, err := getQueueOrigin(msg.QueueOrigin())
if err != nil {
return nil, err
}

outmsg := types.NewMessage(
from,
to,
Expand All @@ -155,22 +149,8 @@ func modMessage(
false,
msg.L1MessageSender(),
msg.L1BlockNumber(),
queueOrigin,
msg.QueueOrigin(),
)

return outmsg, nil
}

func getQueueOrigin(
queueOrigin *big.Int,
) (types.QueueOrigin, error) {
if queueOrigin.Cmp(big.NewInt(0)) == 0 {
return types.QueueOriginSequencer, nil
} else if queueOrigin.Cmp(big.NewInt(1)) == 0 {
return types.QueueOriginL1ToL2, nil
} else if queueOrigin.Cmp(big.NewInt(2)) == 0 {
return types.QueueOriginL1ToL2, nil
} else {
return types.QueueOriginSequencer, fmt.Errorf("invalid queue origin: %d", queueOrigin)
}
}
8 changes: 4 additions & 4 deletions l2geth/core/types/gen_tx_json.go

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

26 changes: 23 additions & 3 deletions l2geth/core/types/gen_tx_meta_json.go

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

20 changes: 8 additions & 12 deletions l2geth/core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,12 @@ func (tx *Transaction) DecodeRLP(s *rlp.Stream) error {

// MarshalJSON encodes the web3 RPC transaction format.
func (tx *Transaction) MarshalJSON() ([]byte, error) {
return tx.data.TransactionMarshalJSON()
return tx.data.MarshalJSON()
}

// UnmarshalJSON decodes the web3 RPC transaction format.
func (tx *Transaction) UnmarshalJSON(input []byte) error {
err := tx.data.TransactionUnmarshalJSON(input)
err := tx.data.UnmarshalJSON(input)
if err != nil {
return err
}
Expand Down Expand Up @@ -254,13 +254,9 @@ func (tx *Transaction) L1BlockNumber() *big.Int {
return &l1BlockNumber
}

// QueueOrigin returns the Queue Origin of the transaction if it exists.
func (tx *Transaction) QueueOrigin() *big.Int {
if tx.meta.QueueOrigin == nil {
return nil
}
queueOrigin := *tx.meta.QueueOrigin
return &queueOrigin
// QueueOrigin returns the Queue Origin of the transaction
func (tx *Transaction) QueueOrigin() QueueOrigin {
return tx.meta.QueueOrigin
}

// Hash hashes the RLP encoding of tx.
Expand Down Expand Up @@ -519,7 +515,7 @@ type Message struct {

l1MessageSender *common.Address
l1BlockNumber *big.Int
queueOrigin *big.Int
queueOrigin QueueOrigin
}

func NewMessage(from common.Address, to *common.Address, nonce uint64, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte, checkNonce bool, l1MessageSender *common.Address, l1BlockNumber *big.Int, queueOrigin QueueOrigin) Message {
Expand All @@ -535,7 +531,7 @@ func NewMessage(from common.Address, to *common.Address, nonce uint64, amount *b

l1BlockNumber: l1BlockNumber,
l1MessageSender: l1MessageSender,
queueOrigin: big.NewInt(int64(queueOrigin)),
queueOrigin: queueOrigin,
}
}

Expand All @@ -550,4 +546,4 @@ func (m Message) CheckNonce() bool { return m.checkNonce }

func (m Message) L1MessageSender() *common.Address { return m.l1MessageSender }
func (m Message) L1BlockNumber() *big.Int { return m.l1BlockNumber }
func (m Message) QueueOrigin() *big.Int { return m.queueOrigin }
func (m Message) QueueOrigin() QueueOrigin { return m.queueOrigin }
29 changes: 18 additions & 11 deletions l2geth/core/types/transaction_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,32 @@ import (
"github.com/ethereum/go-ethereum/common"
)

type QueueOrigin int64
type QueueOrigin uint8

const (
// Possible `queue_origin` values
QueueOriginSequencer QueueOrigin = 0
QueueOriginL1ToL2 QueueOrigin = 1
)

func (q QueueOrigin) String() string {
switch q {
case QueueOriginSequencer:
return "sequencer"
case QueueOriginL1ToL2:
return "l1"
default:
return ""
}
}

//go:generate gencodec -type TransactionMeta -out gen_tx_meta_json.go

type TransactionMeta struct {
L1BlockNumber *big.Int `json:"l1BlockNumber"`
L1Timestamp uint64 `json:"l1Timestamp"`
L1MessageSender *common.Address `json:"l1MessageSender" gencodec:"required"`
QueueOrigin *big.Int `json:"queueOrigin" gencodec:"required"`
QueueOrigin QueueOrigin `json:"queueOrigin" gencodec:"required"`
// The canonical transaction chain index
Index *uint64 `json:"index" gencodec:"required"`
// The queue index, nil for queue origin sequencer transactions
Expand All @@ -40,7 +51,7 @@ func NewTransactionMeta(l1BlockNumber *big.Int, l1timestamp uint64, l1MessageSen
L1BlockNumber: l1BlockNumber,
L1Timestamp: l1timestamp,
L1MessageSender: l1MessageSender,
QueueOrigin: big.NewInt(int64(queueOrigin)),
QueueOrigin: queueOrigin,
Index: index,
QueueIndex: queueIndex,
RawTransaction: rawTransaction,
Expand Down Expand Up @@ -83,7 +94,7 @@ func TxMetaDecode(input []byte) (*TransactionMeta, error) {
}
if !isNullValue(qo) {
queueOrigin := new(big.Int).SetBytes(qo)
meta.QueueOrigin = queueOrigin
meta.QueueOrigin = QueueOrigin(queueOrigin.Uint64())
}

l, err := common.ReadVarBytes(b, 0, 1024, "L1Timestamp")
Expand Down Expand Up @@ -146,13 +157,9 @@ func TxMetaEncode(meta *TransactionMeta) []byte {
}

queueOrigin := meta.QueueOrigin
if queueOrigin == nil {
common.WriteVarBytes(b, 0, getNullValue())
} else {
q := new(bytes.Buffer)
binary.Write(q, binary.LittleEndian, queueOrigin.Bytes())
common.WriteVarBytes(b, 0, q.Bytes())
}
q := new(bytes.Buffer)
binary.Write(q, binary.LittleEndian, queueOrigin)
common.WriteVarBytes(b, 0, q.Bytes())

l := new(bytes.Buffer)
binary.Write(l, binary.LittleEndian, &meta.L1Timestamp)
Expand Down
9 changes: 1 addition & 8 deletions l2geth/core/types/transaction_meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,7 @@ func isTxMetaEqual(meta1 *TransactionMeta, meta2 *TransactionMeta) bool {
}
}

if meta1.QueueOrigin == nil || meta2.QueueOrigin == nil {
// Note: this only works because it is the final comparison
if meta1.QueueOrigin == nil && meta2.QueueOrigin == nil {
return true
}
}

if !bytes.Equal(meta1.QueueOrigin.Bytes(), meta2.QueueOrigin.Bytes()) {
if meta1.QueueOrigin != meta2.QueueOrigin {
return false
}

Expand Down
2 changes: 1 addition & 1 deletion l2geth/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ type CallMsg struct {

L1MessageSender *common.Address
L1BlockNumber *big.Int
QueueOrigin *big.Int
QueueOrigin types.QueueOrigin
}

// A ContractCaller provides contract calls, essentially transactions that are executed by
Expand Down
12 changes: 3 additions & 9 deletions l2geth/internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1364,14 +1364,8 @@ func newRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber
if meta.L1BlockNumber != nil {
result.L1BlockNumber = (*hexutil.Big)(meta.L1BlockNumber)
}
if meta.QueueOrigin != nil {
switch meta.QueueOrigin.Uint64() {
case uint64(types.QueueOriginSequencer):
result.QueueOrigin = "sequencer"
case uint64(types.QueueOriginL1ToL2):
result.QueueOrigin = "l1"
}
}

result.QueueOrigin = fmt.Sprint(meta.QueueOrigin)

if meta.Index != nil {
index := (hexutil.Uint64)(*meta.Index)
Expand Down Expand Up @@ -2168,7 +2162,7 @@ func (api *PrivateDebugAPI) IngestTransactions(txs []*RPCTransaction) error {
L1BlockNumber: l1BlockNumber,
L1Timestamp: l1Timestamp,
L1MessageSender: tx.L1TxOrigin,
QueueOrigin: big.NewInt(int64(queueOrigin)),
QueueOrigin: queueOrigin,
Index: (*uint64)(tx.Index),
QueueIndex: (*uint64)(tx.QueueIndex),
RawTransaction: rawTransaction,
Expand Down
2 changes: 1 addition & 1 deletion l2geth/miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ func (w *worker) commitNewTx(tx *types.Transaction) error {
// transactions as the timestamp cannot be malleated
if parent.Time() > tx.L1Timestamp() {
log.Error("Monotonicity violation", "index", num)
if tx.QueueOrigin().Uint64() == uint64(types.QueueOriginSequencer) {
if tx.QueueOrigin() == types.QueueOriginSequencer {
tx.SetL1Timestamp(parent.Time())
prev := parent.Transactions()
if len(prev) == 1 {
Expand Down
Loading

0 comments on commit 0887367

Please sign in to comment.