forked from ethereum-optimism/optimism
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref impl: Add simple batch serialization
- Loading branch information
1 parent
2f89ca1
commit 05b1bad
Showing
3 changed files
with
109 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters