From 4a32f0b1b99ea42ff9ae3d4136e86b4801c9197d Mon Sep 17 00:00:00 2001 From: gupadhyaya Date: Mon, 19 Aug 2024 11:19:02 +0400 Subject: [PATCH] accept nil, but handle it --- proxy/grpc/server.go | 14 ++++---------- serialization.go | 13 ++----------- test/dummy.go | 4 ++-- 3 files changed, 8 insertions(+), 23 deletions(-) diff --git a/proxy/grpc/server.go b/proxy/grpc/server.go index ab44c4e..7be1747 100644 --- a/proxy/grpc/server.go +++ b/proxy/grpc/server.go @@ -53,11 +53,8 @@ func (s *proxyInputSrv) SubmitRollupTransaction(ctx context.Context, req *pbseq. // GetNextBatch returns the next batch of transactions from sequencer to rollup. func (s *proxyOutputSrv) GetNextBatch(ctx context.Context, req *pbseq.Batch) (*pbseq.Batch, error) { - var lastBatch *sequencing.Batch - if req.Transactions != nil { - lastBatch = &sequencing.Batch{} - lastBatch.FromProto(req) - } + lastBatch := &sequencing.Batch{} + lastBatch.FromProto(req) batch, err := s.SequencerOutput.GetNextBatch(ctx, lastBatch) if err != nil { return nil, err @@ -70,11 +67,8 @@ func (s *proxyOutputSrv) GetNextBatch(ctx context.Context, req *pbseq.Batch) (*p // VerifyBatch verifies a batch of transactions received from the sequencer. func (s *proxyVerificationSrv) VerifyBatch(ctx context.Context, req *pbseq.Batch) (*pbseq.VerificationResponse, error) { - var batch *sequencing.Batch - if req.Transactions != nil { - batch = &sequencing.Batch{} - batch.FromProto(req) - } + batch := &sequencing.Batch{} + batch.FromProto(req) ok, err := s.BatchVerifier.VerifyBatch(ctx, batch) if err != nil { return nil, err diff --git a/serialization.go b/serialization.go index 91c78e1..0b65a65 100644 --- a/serialization.go +++ b/serialization.go @@ -6,11 +6,7 @@ import ( // ToProto serializes a batch to a protobuf message. func (batch *Batch) ToProto() *pbseq.Batch { - var txs [][]byte - if batch != nil { - txs = batch.Transactions - } - return &pbseq.Batch{Transactions: txsToByteSlices(txs)} + return &pbseq.Batch{Transactions: txsToByteSlices(batch.Transactions)} } // FromProto deserializes a batch from a protobuf message. @@ -38,12 +34,7 @@ func byteSlicesToTxs(bytes [][]byte) []Tx { // Marshal serializes a batch to a byte slice. func (batch *Batch) Marshal() ([]byte, error) { - var b []byte - if batch == nil { - return b, nil - } - pb := batch.ToProto() - return pb.Marshal() + return batch.ToProto().Marshal() } // Unmarshal deserializes a batch from a byte slice. diff --git a/test/dummy.go b/test/dummy.go index 86fa85a..335cfb1 100644 --- a/test/dummy.go +++ b/test/dummy.go @@ -74,10 +74,10 @@ func (d *DummySequencer) SubmitRollupTransaction(ctx context.Context, rollupId [ // GetNextBatch implements sequencing.Sequencer. func (d *DummySequencer) GetNextBatch(ctx context.Context, lastBatch *sequencing.Batch) (*sequencing.Batch, error) { if d.lastBatchHash == nil { - if lastBatch != nil { + if lastBatch.Transactions != nil { return nil, errors.New("lastBatch is supposed to be nil") } - } else if lastBatch == nil { + } else if lastBatch.Transactions == nil { return nil, errors.New("lastBatch is not supposed to be nil") } else { lastBatchBytes, err := lastBatch.Marshal()