Skip to content

Commit

Permalink
ref impl: Add simple batch serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
trianglesphere authored and protolambda committed Mar 16, 2022
1 parent 2f89ca1 commit 05b1bad
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 12 deletions.
70 changes: 70 additions & 0 deletions opnode/rollup/derive/batch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package derive

import (
"bytes"
"errors"

"github.com/ethereum-optimism/optimistic-specs/opnode/l2"
"github.com/ethereum-optimism/optimistic-specs/opnode/rollup"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/rlp"
)

// Batch format
// first byte is type followed by bytstring.
//
// BatchV1Type := 0
// BatchV1Type ++ RLP([epoch, timestamp, transaction_list]

const (
BatchV1Type = iota
)

type BatchV1 struct {
Epoch uint64
Timestamp uint64
Transactions []hexutil.Bytes
}

type BatchData struct {
Epoch rollup.Epoch // aka l1 num
Timestamp uint64
// no feeRecipient address input, all fees go to a L2 contract
Transactions []l2.Data
}

func ParseBatch(data []byte) (BatchData, error) {
var v1 BatchV1
if err := v1.UnmarshalBinary(data); err != nil {
return BatchData{}, err
}
return BatchData{Epoch: rollup.Epoch(v1.Epoch), Timestamp: v1.Timestamp, Transactions: v1.Transactions}, nil
}

// // TODO: Is this needed?
// // EncodeRLP implements rlp.Encoder
// func (b *BatchV1) EncodeRLP(w io.Writer) error {
// return rlp.Encode(w, b)
// }

// MarshalBinary returns the canonical encoding of the batch.
func (b *BatchV1) MarshalBinary() ([]byte, error) {
var buf bytes.Buffer
buf.WriteByte(BatchV1Type)
err := rlp.Encode(&buf, b)
return buf.Bytes(), err
}

// // TODO: Is this needed?
// // DecodeRLP implements rlp.Decoder
// func (b *BatchV1) DecodeRLP(s *rlp.Stream) error {
// return s.Decode(b)
// }

// UnmarshalBinary decodes the canonical encoding of batch.
func (batch *BatchV1) UnmarshalBinary(b []byte) error {
if len(b) == 0 {
return errors.New("Batch too short")
}
return rlp.DecodeBytes(b[1:], batch)
}
32 changes: 32 additions & 0 deletions opnode/rollup/derive/batch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package derive

import (
"testing"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/stretchr/testify/assert"
)

func TestBatchRoundTrip(t *testing.T) {
batches := []BatchV1{
{
Epoch: 0,
Timestamp: 0,
Transactions: []hexutil.Bytes{},
},
{
Epoch: 1,
Timestamp: 1647026951,
Transactions: []hexutil.Bytes{[]byte{0, 0, 0}, []byte{0x76, 0xfd, 0x7c}},
},
}

for i, batch := range batches {
enc, err := batch.MarshalBinary()
assert.NoError(t, err)
var dec BatchV1
err = dec.UnmarshalBinary(enc)
assert.NoError(t, err)
assert.Equal(t, batch, dec, "Batch not equal test case %v", i)
}
}
19 changes: 7 additions & 12 deletions opnode/rollup/derive/payload_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,19 @@ func BatchesFromEVMTransactions(config *rollup.Config, txs []*types.Transaction)
if to := tx.To(); to != nil && *to == config.BatchInboxAddress {
seqDataSubmitter, err := l1Signer.Sender(tx)
if err != nil {
// TODO: log error
continue // bad signature, ignore
}
// some random L1 user might have sent a transaction to our batch inbox, ignore them
if seqDataSubmitter != config.BatchSenderAddress {
continue // not an authorized batch submitter, ignore
}
out = append(out, ParseBatches(tx.Data())...)
batch, err := ParseBatch(tx.Data())
if err != nil {
// TODO: log error
continue
}
out = append(out, batch)
}
}
return
Expand Down Expand Up @@ -328,14 +334,3 @@ func DeriveDeposits(l1Info L1Info, receipts []*types.Receipt) ([]l2.Data, error)
}
return encodedTxs, nil
}

type BatchData struct {
Epoch rollup.Epoch // aka l1 num
Timestamp uint64
// no feeRecipient address input, all fees go to a L2 contract
Transactions []l2.Data
}

func ParseBatches(data l2.Data) []BatchData {
return nil // TODO
}

0 comments on commit 05b1bad

Please sign in to comment.