Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode transactions in RLP format #24

Merged
merged 3 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions arbos/parse_l2.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package arbos
import (
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"io"
Expand All @@ -16,6 +15,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"
"github.com/offchainlabs/nitro/arbos/arbostypes"
"github.com/offchainlabs/nitro/arbos/util"
"github.com/offchainlabs/nitro/util/arbmath"
Expand Down Expand Up @@ -199,7 +199,7 @@ func parseL2Message(rd io.Reader, poster common.Address, timestamp uint64, reque
continue
}
newTx := new(types.Transaction)
if err := json.Unmarshal(nextMsg, &newTx); err != nil {
if err := newTx.UnmarshalBinary(nextMsg); err != nil {
return nil, err
}
segments = append(segments, newTx)
Expand Down Expand Up @@ -237,7 +237,7 @@ func parseEspressoMsg(rd io.Reader) ([]espressoTypes.Bytes, *arbostypes.Espresso
}
if jst == nil {
j := new(arbostypes.EspressoBlockJustification)
if err := json.Unmarshal(nextMsg, &j); err != nil {
if err := rlp.DecodeBytes(nextMsg, &j); err != nil {
return nil, nil, err
}
jst = j
Expand Down Expand Up @@ -465,23 +465,18 @@ func parseBatchPostingReportMessage(rd io.Reader, chainId *big.Int, msgBatchGasC
// messageFromEspresso serializes raw data from the espresso block into an arbitrum message,
// including malformed and invalid transactions.
// This allows validators to rebuild a block and check the espresso commitment.
//
// Note that the raw data is actually in JSON format, which can result in a larger size than necessary.
// Storing it in L1 call data would lead to some waste. However, for the sake of this Proof of Concept,
// this is deemed acceptable. Addtionally, after we finish the integration, there is no need to store
// message in L1.
func MessageFromEspresso(header *arbostypes.L1IncomingMessageHeader, txes []espressoTypes.Bytes, jst *arbostypes.EspressoBlockJustification) (arbostypes.L1IncomingMessage, error) {
var l2Message []byte

l2Message = append(l2Message, L2MessageKind_EspressoTx)
jstJson, err := json.Marshal(jst)
jstBin, err := rlp.EncodeToBytes(jst)
if err != nil {
return arbostypes.L1IncomingMessage{}, err
}
sizeBuf := make([]byte, 8)
binary.BigEndian.PutUint64(sizeBuf, uint64(len(jstJson)))
binary.BigEndian.PutUint64(sizeBuf, uint64(len(jstBin)))
l2Message = append(l2Message, sizeBuf...)
l2Message = append(l2Message, jstJson...)
l2Message = append(l2Message, jstBin...)
for _, tx := range txes {
binary.BigEndian.PutUint64(sizeBuf, uint64(len(tx)))
l2Message = append(l2Message, sizeBuf...)
Expand Down
3 changes: 1 addition & 2 deletions execution/gethexec/espresso_sequencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package gethexec

import (
"context"
"encoding/json"
"time"

"github.com/offchainlabs/nitro/util/stopwaiter"
Expand Down Expand Up @@ -122,7 +121,7 @@ func (s *EspressoSequencer) Start(ctxIn context.Context) error {

// Required methods for the TransactionPublisher interface
func (s *EspressoSequencer) PublishTransaction(parentCtx context.Context, tx *types.Transaction, options *arbitrum_types.ConditionalOptions) error {
var txnBytes, err = json.Marshal(tx)
var txnBytes, err = tx.MarshalBinary()
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions execution/gethexec/executionengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package gethexec
import (
"context"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"sync"
Expand Down Expand Up @@ -295,7 +294,7 @@ func (s *ExecutionEngine) SequenceTransactionsEspresso(
txes := types.Transactions{}
for _, tx := range rawTxes {
var out types.Transaction
if err := json.Unmarshal(tx, &out); err != nil {
if err := out.UnmarshalBinary(tx); err != nil {
log.Warn("Malformed tx is found")
continue
}
Expand Down
4 changes: 2 additions & 2 deletions system_tests/espresso_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ func onlyMalformedTxs(t *testing.T) [][]byte {
// Two valid transactions and two invalid transactions with invalid nonces
func userTxs(t *testing.T, l2Info *BlockchainTestInfo) [][]byte {
tx1 := l2Info.PrepareTx("Faucet", "Owner", 3e7, big.NewInt(1e16), nil)
tx1Bin, err := json.Marshal(tx1)
tx1Bin, err := tx1.MarshalBinary()
if err != nil {
panic(err)
}
tx2 := l2Info.PrepareTx("Owner", "Faucet", 3e7, big.NewInt(1e16), nil)
tx2Bin, err := json.Marshal(tx2)
tx2Bin, err := tx2.MarshalBinary()
if err != nil {
panic(err)
}
Expand Down
Loading