Skip to content

Commit

Permalink
reintroduce rollupId, but as a config in the constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
gupadhyaya committed Oct 1, 2024
1 parent a98e57c commit b30eec5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 37 deletions.
50 changes: 34 additions & 16 deletions test/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package test
import (
"bytes"
"context"
"encoding/hex"
"errors"
"math"
"sync"
Expand All @@ -11,6 +12,8 @@ import (
"github.com/rollkit/go-sequencing"
)

var ErrInvalidRollupId = errors.New("invalid rollup id")

Check failure on line 15 in test/dummy.go

View workflow job for this annotation

GitHub Actions / lint / golangci-lint

exported: exported var ErrInvalidRollupId should have comment or be unexported (revive)

// TransactionQueue is a queue of transactions
type TransactionQueue struct {
queue []sequencing.Tx
Expand Down Expand Up @@ -54,21 +57,26 @@ func (tq *TransactionQueue) GetNextBatch(maxBytes uint64) *sequencing.Batch {
maxBytes = math.MaxUint64
}

for {
for batchSize > 0 {
batch = tq.queue[:batchSize]
blobSize := totalBytes(batch)
if uint64(blobSize) <= maxBytes {
break
}
batchSize = batchSize - 1
}
if batchSize == 0 {
// No transactions can fit within maxBytes
return &sequencing.Batch{Transactions: nil}
}

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

// DummySequencer is a dummy sequencer for testing that serves a single rollup
type DummySequencer struct {
rollupId []byte
tq *TransactionQueue
lastBatchHash []byte
lastBatchHashMutex sync.RWMutex
Expand All @@ -79,26 +87,29 @@ type DummySequencer struct {

// SubmitRollupTransaction implements sequencing.Sequencer.
func (d *DummySequencer) SubmitRollupTransaction(ctx context.Context, req sequencing.SubmitRollupTransactionRequest) (*sequencing.SubmitRollupTransactionResponse, error) {
if !d.isValid(req.RollupId) {
return nil, ErrInvalidRollupId
}
d.tq.AddTransaction(req.Tx)
return &sequencing.SubmitRollupTransactionResponse{}, nil
}

// GetNextBatch implements sequencing.Sequencer.
func (d *DummySequencer) GetNextBatch(ctx context.Context, req sequencing.GetNextBatchRequest) (*sequencing.GetNextBatchResponse, error) {
if !d.isValid(req.RollupId) {
return nil, ErrInvalidRollupId
}
now := time.Now()
d.lastBatchHashMutex.RLock()
lastBatchHash := d.lastBatchHash
d.lastBatchHashMutex.RUnlock()
if lastBatchHash == nil {
if req.LastBatchHash != nil {
return nil, errors.New("lastBatch is supposed to be nil")
}
} else if req.LastBatchHash == nil {

if lastBatchHash == nil && req.LastBatchHash != nil {
return nil, errors.New("lastBatch is supposed to be nil")
} else if lastBatchHash != nil && req.LastBatchHash == nil {
return nil, errors.New("lastBatch is not supposed to be nil")
} else {
if !bytes.Equal(lastBatchHash, req.LastBatchHash) {
return nil, errors.New("supplied lastBatch does not match with sequencer last batch")
}
} else if !bytes.Equal(lastBatchHash, req.LastBatchHash) {
return nil, errors.New("supplied lastBatch does not match with sequencer last batch")
}

batch := d.tq.GetNextBatch(req.MaxBytes)
Expand All @@ -118,26 +129,33 @@ func (d *DummySequencer) GetNextBatch(ctx context.Context, req sequencing.GetNex
d.lastBatchHashMutex.Unlock()

d.seenBatchesMutex.Lock()
d.seenBatches[string(h)] = struct{}{}
d.seenBatches[hex.EncodeToString(h)] = struct{}{}
d.seenBatchesMutex.Unlock()
return batchRes, nil
}

// VerifyBatch implements sequencing.Sequencer.
func (d *DummySequencer) VerifyBatch(ctx context.Context, req sequencing.VerifyBatchRequest) (*sequencing.VerifyBatchResponse, error) {
if !d.isValid(req.RollupId) {
return nil, ErrInvalidRollupId
}
d.seenBatchesMutex.Lock()
defer d.seenBatchesMutex.Unlock()
for batchHash := range d.seenBatches {
if bytes.Equal([]byte(batchHash), req.BatchHash) {
return &sequencing.VerifyBatchResponse{Status: true}, nil
}
key := hex.EncodeToString(req.BatchHash)
if _, exists := d.seenBatches[key]; exists {
return &sequencing.VerifyBatchResponse{Status: true}, nil
}
return &sequencing.VerifyBatchResponse{Status: false}, nil
}

func (d *DummySequencer) isValid(rollupId []byte) bool {
return bytes.Equal(d.rollupId, rollupId)
}

// NewDummySequencer creates a new DummySequencer
func NewDummySequencer() *DummySequencer {
func NewDummySequencer(rollupId []byte) *DummySequencer {
return &DummySequencer{
rollupId: rollupId,
tq: NewTransactionQueue(),
seenBatches: make(map[string]struct{}, 0),
}
Expand Down
40 changes: 19 additions & 21 deletions test/dummy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ func TestTransactionQueue_GetNextBatch(t *testing.T) {
}

func TestDummySequencer_SubmitRollupTransaction(t *testing.T) {
sequencer := NewDummySequencer()

// Define a test rollup ID and transaction
rollupId := []byte("test_rollup_id")
tx := []byte("test_transaction")
sequencer := NewDummySequencer(rollupId)

// Submit a transaction
req := sequencing.SubmitRollupTransactionRequest{
Expand All @@ -68,11 +67,10 @@ func TestDummySequencer_SubmitRollupTransaction(t *testing.T) {
}

func TestDummySequencer_SubmitEmptyTransaction(t *testing.T) {
sequencer := NewDummySequencer()

// Define a test rollup ID and an empty transaction
rollupId := []byte("test_rollup_id")
tx := []byte("")
sequencer := NewDummySequencer(rollupId)

// Submit an empty transaction
req := sequencing.SubmitRollupTransactionRequest{
Expand All @@ -92,13 +90,12 @@ func TestDummySequencer_SubmitEmptyTransaction(t *testing.T) {
}

func TestDummySequencer_SubmitMultipleTransactions(t *testing.T) {
sequencer := NewDummySequencer()

// Define a test rollup ID and multiple transactions
rollupId := []byte("test_rollup_id")
tx1 := []byte("transaction_1")
tx2 := []byte("transaction_2")
tx3 := []byte("transaction_3")
sequencer := NewDummySequencer(rollupId)

// Submit multiple transactions
req1 := sequencing.SubmitRollupTransactionRequest{
Expand Down Expand Up @@ -130,11 +127,10 @@ func TestDummySequencer_SubmitMultipleTransactions(t *testing.T) {
}

func TestDummySequencer_GetNextBatch(t *testing.T) {
sequencer := NewDummySequencer()

// Add a transaction to the queue
rollupId := []byte("test_rollup_id")
tx := []byte("test_transaction")
sequencer := NewDummySequencer(rollupId)
req := sequencing.SubmitRollupTransactionRequest{
RollupId: rollupId,
Tx: tx,
Expand Down Expand Up @@ -162,11 +158,11 @@ func TestDummySequencer_GetNextBatch(t *testing.T) {

// Test retrieving a batch with no transactions
func TestDummySequencer_GetNextBatch_NoTransactions(t *testing.T) {
sequencer := NewDummySequencer()

rollupId := []byte("test_rollup_id")
sequencer := NewDummySequencer(rollupId)
// Attempt to retrieve a batch with no transactions in the queue
getBatchReq := sequencing.GetNextBatchRequest{
RollupId: []byte("test_rollup_id"),
RollupId: rollupId,
LastBatchHash: nil,
}
batchResp, err := sequencer.GetNextBatch(context.Background(), getBatchReq)
Expand All @@ -179,10 +175,9 @@ func TestDummySequencer_GetNextBatch_NoTransactions(t *testing.T) {
}

func TestDummySequencer_GetNextBatch_LastBatchHashMismatch(t *testing.T) {
sequencer := NewDummySequencer()

// Submit a transaction
rollupId := []byte("test_rollup_id")
sequencer := NewDummySequencer(rollupId)
tx := []byte("test_transaction")
req := sequencing.SubmitRollupTransactionRequest{
RollupId: rollupId,
Expand All @@ -205,10 +200,9 @@ func TestDummySequencer_GetNextBatch_LastBatchHashMismatch(t *testing.T) {

// Test retrieving a batch with maxBytes limit
func TestDummySequencer_GetNextBatch_MaxBytesLimit(t *testing.T) {
sequencer := NewDummySequencer()

// Define a test rollup ID and multiple transactions
rollupId := []byte("test_rollup_id")
sequencer := NewDummySequencer(rollupId)
tx1 := []byte("transaction_1")
tx2 := []byte("transaction_2")
tx3 := []byte("transaction_3")
Expand Down Expand Up @@ -270,10 +264,9 @@ func TestDummySequencer_GetNextBatch_MaxBytesLimit(t *testing.T) {
}

func TestDummySequencer_VerifyBatch(t *testing.T) {
sequencer := NewDummySequencer()

// Add and retrieve a batch
rollupId := []byte("test_rollup_id")
sequencer := NewDummySequencer(rollupId)
tx := []byte("test_transaction")
req := sequencing.SubmitRollupTransactionRequest{
RollupId: rollupId,
Expand All @@ -294,6 +287,7 @@ func TestDummySequencer_VerifyBatch(t *testing.T) {
assert.NoError(t, err)
// Verify the batch using the batch hash
verifyReq := sequencing.VerifyBatchRequest{
RollupId: rollupId,
BatchHash: batchHash,
}
verifyResp, err := sequencer.VerifyBatch(context.Background(), verifyReq)
Expand All @@ -304,11 +298,13 @@ func TestDummySequencer_VerifyBatch(t *testing.T) {
}

func TestDummySequencer_VerifyEmptyBatch(t *testing.T) {
sequencer := NewDummySequencer()
rollupId := []byte("test_rollup_id")
sequencer := NewDummySequencer(rollupId)

// Create a request with an empty batch hash
emptyBatchHash := []byte{}
verifyReq := sequencing.VerifyBatchRequest{
RollupId: rollupId,
BatchHash: emptyBatchHash,
}

Expand All @@ -321,10 +317,9 @@ func TestDummySequencer_VerifyEmptyBatch(t *testing.T) {
}

func TestDummySequencer_VerifyBatchWithMultipleTransactions(t *testing.T) {
sequencer := NewDummySequencer()

// Define a test rollup ID and multiple transactions
rollupId := []byte("test_rollup_id")
sequencer := NewDummySequencer(rollupId)
tx1 := []byte("transaction_1")
tx2 := []byte("transaction_2")

Expand Down Expand Up @@ -355,6 +350,7 @@ func TestDummySequencer_VerifyBatchWithMultipleTransactions(t *testing.T) {
assert.NoError(t, err)
// Verify the batch using the batch hash
verifyReq := sequencing.VerifyBatchRequest{
RollupId: rollupId,
BatchHash: batchHash,
}
verifyResp, err := sequencer.VerifyBatch(context.Background(), verifyReq)
Expand All @@ -365,10 +361,12 @@ func TestDummySequencer_VerifyBatchWithMultipleTransactions(t *testing.T) {
}

func TestDummySequencer_VerifyBatch_NotFound(t *testing.T) {
sequencer := NewDummySequencer()
rollupId := []byte("test_rollup_id")
sequencer := NewDummySequencer(rollupId)

// Try verifying a batch with an invalid hash
verifyReq := sequencing.VerifyBatchRequest{
RollupId: rollupId,
BatchHash: []byte("invalid_hash"),
}
verifyResp, err := sequencer.VerifyBatch(context.Background(), verifyReq)
Expand Down

0 comments on commit b30eec5

Please sign in to comment.