Skip to content

Commit

Permalink
creating Batch type and serialization for better handling of the prot…
Browse files Browse the repository at this point in the history
…obuf
  • Loading branch information
gupadhyaya committed Jul 19, 2024
1 parent d5aeeb7 commit c47d0a7
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 267 deletions.
13 changes: 4 additions & 9 deletions proto/sequencing/sequencing.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,18 @@ message SubmitRollupTransactionResponse {
// SequencerOutput ...
service SequencerOutput {
// SubmitRollupTransaction ...
rpc GetNextBatch(BatchRequest) returns (BatchResponse) {}
rpc GetNextBatch(Batch) returns (Batch) {}
}

// BatchRequest provides the last batch while requesting for the next batch
message BatchRequest {
repeated bytes transactions = 1;
}

// BatchResponse contains the transaction batch that is last sequenced
message BatchResponse {
// Batch holds a list of transactions
message Batch {
repeated bytes transactions = 1;
}

// BatchVerifier
service BatchVerifier {
// VerifyBatch ...
rpc VerifyBatch(BatchRequest) returns (VerificationResponse) {}
rpc VerifyBatch(Batch) returns (VerificationResponse) {}
}

// VerificationResponse
Expand Down
19 changes: 9 additions & 10 deletions proxy/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package grpc
import (
"context"

Check failure on line 5 in proxy/grpc/client.go

View workflow job for this annotation

GitHub Actions / lint / golangci-lint

File is not `goimports`-ed with -local github.com/rollkit (goimports)
"github.com/rollkit/go-sequencing"
pbseq "github.com/rollkit/go-sequencing/types/pb/sequencing"
"google.golang.org/grpc"
)
Expand Down Expand Up @@ -50,21 +51,19 @@ func (c *Client) SubmitRollupTransaction(ctx context.Context, rollupId []byte, t
}

// GetNextBatch returns the next batch of transactions from sequencer to rollup.
func (c *Client) GetNextBatch(ctx context.Context, lastBatch [][]byte) ([][]byte, error) {
resp, err := c.SequencerOutputClient.GetNextBatch(ctx, &pbseq.BatchRequest{
Transactions: lastBatch,
})
func (c *Client) GetNextBatch(ctx context.Context, lastBatch sequencing.Batch) (sequencing.Batch, error) {
resp, err := c.SequencerOutputClient.GetNextBatch(ctx, lastBatch.ToProto())
if err != nil {
return nil, err
return sequencing.Batch{}, err
}
return resp.Transactions, nil
b := sequencing.Batch{}
b.FromProto(resp)
return b, nil
}

// VerifyBatch verifies a batch of transactions received from the sequencer.
func (c *Client) VerifyBatch(ctx context.Context, batch [][]byte) (bool, error) {
resp, err := c.BatchVerifierClient.VerifyBatch(ctx, &pbseq.BatchRequest{
Transactions: batch,
})
func (c *Client) VerifyBatch(ctx context.Context, batch sequencing.Batch) (bool, error) {
resp, err := c.BatchVerifierClient.VerifyBatch(ctx, batch.ToProto())
if err != nil {
return false, err
}
Expand Down
14 changes: 9 additions & 5 deletions proxy/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,20 @@ func (s *proxyInputSrv) SubmitRollupTransaction(ctx context.Context, req *pbseq.
return &pbseq.SubmitRollupTransactionResponse{}, nil
}

func (s *proxyOutputSrv) GetNextBatch(ctx context.Context, req *pbseq.BatchRequest) (*pbseq.BatchResponse, error) {
batch, err := s.SequencerOutput.GetNextBatch(ctx, req.Transactions)
func (s *proxyOutputSrv) GetNextBatch(ctx context.Context, req *pbseq.Batch) (*pbseq.Batch, error) {
lastBatch := sequencing.Batch{}
lastBatch.FromProto(req)
batch, err := s.SequencerOutput.GetNextBatch(ctx, lastBatch)
if err != nil {
return nil, err
}
return &pbseq.BatchResponse{Transactions: batch}, nil
return batch.ToProto(), nil
}

func (s *proxyVerificationSrv) VerifyBatch(ctx context.Context, req *pbseq.BatchRequest) (*pbseq.VerificationResponse, error) {
ok, err := s.BatchVerifier.VerifyBatch(ctx, req.Transactions)
func (s *proxyVerificationSrv) VerifyBatch(ctx context.Context, req *pbseq.Batch) (*pbseq.VerificationResponse, error) {
batch := sequencing.Batch{}
batch.FromProto(req)
ok, err := s.BatchVerifier.VerifyBatch(ctx, batch)
if err != nil {
return nil, err
}
Expand Down
9 changes: 7 additions & 2 deletions sequencing.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,22 @@ type SequencerOutput interface {
// GetNextBatch returns the next batch of transactions from sequencer to rollup
// lastBatch is the last batch of transactions received from the sequencer
// returns the next batch of transactions and an error if any from the sequencer
GetNextBatch(ctx context.Context, lastBatch []Tx) ([]Tx, error)
GetNextBatch(ctx context.Context, lastBatch Batch) (Batch, error)
}

// BatchVerifier provides a method for verifying a batch of transactions received from the sequencer
type BatchVerifier interface {
// VerifyBatch verifies a batch of transactions received from the sequencer
VerifyBatch(ctx context.Context, batch []Tx) (bool, error)
VerifyBatch(ctx context.Context, batch Batch) (bool, error)
}

// RollupId is a unique identifier for a rollup chain
type RollupId = []byte

// Tx is a rollup transaction
type Tx = []byte

// Batch is a collection of transactions
type Batch struct {
Transactions []Tx
}
45 changes: 45 additions & 0 deletions serialization.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package sequencing

import (
pbseq "github.com/rollkit/go-sequencing/types/pb/sequencing"
)

func (batch *Batch) ToProto() *pbseq.Batch {

Check warning on line 7 in serialization.go

View workflow job for this annotation

GitHub Actions / lint / golangci-lint

exported: exported method Batch.ToProto should have comment or be unexported (revive)
return &pbseq.Batch{Transactions: txsToByteSlices(batch.Transactions)}
}

func (batch *Batch) FromProto(pb *pbseq.Batch) {

Check warning on line 11 in serialization.go

View workflow job for this annotation

GitHub Actions / lint / golangci-lint

exported: exported method Batch.FromProto should have comment or be unexported (revive)
batch.Transactions = byteSlicesToTxs(pb.Transactions)
}

func txsToByteSlices(txs []Tx) [][]byte {
if txs == nil {
return nil
}
bytes := make([][]byte, len(txs))
copy(bytes, txs)
return bytes
}

func byteSlicesToTxs(bytes [][]byte) []Tx {
if len(bytes) == 0 {
return nil
}
txs := make([]Tx, len(bytes))
copy(txs, bytes)
return txs
}

func (batch *Batch) Marshal() ([]byte, error) {

Check warning on line 33 in serialization.go

View workflow job for this annotation

GitHub Actions / lint / golangci-lint

exported: exported method Batch.Marshal should have comment or be unexported (revive)
pb := batch.ToProto()
return pb.Marshal()
}

func (batch *Batch) Unmarshal(data []byte) error {

Check warning on line 38 in serialization.go

View workflow job for this annotation

GitHub Actions / lint / golangci-lint

exported: exported method Batch.Unmarshal should have comment or be unexported (revive)
var pb pbseq.Batch
if err := pb.Unmarshal(data); err != nil {
return err
}
batch.FromProto(&pb)
return nil
}
20 changes: 14 additions & 6 deletions test/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ func (tq *TransactionQueue) AddTransaction(tx sequencing.Tx) {
}

// GetBatch extracts a batch of transactions from the queue

Check warning on line 34 in test/dummy.go

View workflow job for this annotation

GitHub Actions / lint / golangci-lint

exported: comment on exported method TransactionQueue.GetNextBatch should be of the form "GetNextBatch ..." (revive)
func (tq *TransactionQueue) GetNextBatch() []sequencing.Tx {
func (tq *TransactionQueue) GetNextBatch() sequencing.Batch {
tq.mu.Lock()
defer tq.mu.Unlock()

batchSize := len(tq.queue)

batch := tq.queue[:batchSize]
tq.queue = tq.queue[batchSize:]
return batch
return sequencing.Batch{Transactions: batch}
}

// DummySequencer is a dummy sequencer for testing that serves a single rollup
Expand All @@ -67,16 +67,24 @@ func (d *DummySequencer) SubmitRollupTransaction(ctx context.Context, rollupId [
}

// GetNextBatch implements sequencing.Sequencer.
func (d *DummySequencer) GetNextBatch(ctx context.Context, lastBatch [][]byte) ([][]byte, error) {
func (d *DummySequencer) GetNextBatch(ctx context.Context, lastBatch sequencing.Batch) (sequencing.Batch, error) {
batch := d.tq.GetNextBatch()
d.lastBatchHash = hashSHA256(batch[0])
batchBytes, err := batch.Marshal()
if err != nil {
return sequencing.Batch{}, err
}
d.lastBatchHash = hashSHA256(batchBytes)
d.seenBatches[string(d.lastBatchHash)] = struct{}{}
return batch, nil
}

// VerifyBatch implements sequencing.Sequencer.
func (d *DummySequencer) VerifyBatch(ctx context.Context, batch [][]byte) (bool, error) {
hash := hashSHA256(batch[0])
func (d *DummySequencer) VerifyBatch(ctx context.Context, batch sequencing.Batch) (bool, error) {
batchBytes, err := batch.Marshal()
if err != nil {
return false, err
}
hash := hashSHA256(batchBytes)
_, ok := d.seenBatches[string(hash)]
return ok, nil
}
Expand Down
Loading

0 comments on commit c47d0a7

Please sign in to comment.