This repository was archived by the owner on May 2, 2025. It is now read-only.
generated from evstack/template-da-repo
-
Notifications
You must be signed in to change notification settings - Fork 2
feat: adding multi-rollup sequencer for the purpose of testing #18
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3134457
adding multi-rollup sequencer for the purpose of testing
gupadhyaya 6eb03f5
lint
gupadhyaya 1fd77fe
Merge branch 'main' into multi_rollup
gupadhyaya c328383
minor fix
gupadhyaya 27d57ea
fix tests and checks
gupadhyaya 67c8e69
DeepEqual
gupadhyaya cb5ec7e
more deepequal
gupadhyaya 5ce114a
adding multi-rollup test
gupadhyaya dae98a5
add test
gupadhyaya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,126 @@ | ||
package test | ||
|
||
import ( | ||
"context" | ||
"encoding/hex" | ||
"fmt" | ||
"reflect" | ||
"sync" | ||
"time" | ||
|
||
"github.com/rollkit/go-sequencing" | ||
) | ||
|
||
// MultiRollupSequencer is a sequencer for testing that serves multiple rollups | ||
type MultiRollupSequencer struct { | ||
rollups map[string]*RollupData | ||
rollupsMutex sync.RWMutex | ||
} | ||
|
||
// RollupData holds the data for a specific rollup, including its transaction queue, last batch hash, and seen batches. | ||
type RollupData struct { | ||
tq *TransactionQueue | ||
lastBatchHash []byte | ||
lastBatchHashMutex sync.RWMutex | ||
|
||
seenBatches map[string]struct{} | ||
seenBatchesMutex sync.Mutex | ||
gupadhyaya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// SubmitRollupTransaction implements sequencing.Sequencer. | ||
func (d *MultiRollupSequencer) SubmitRollupTransaction(ctx context.Context, req sequencing.SubmitRollupTransactionRequest) (*sequencing.SubmitRollupTransactionResponse, error) { | ||
rollup, err := d.getOrCreateRollup(req.RollupId) | ||
if err != nil { | ||
return nil, err | ||
} | ||
rollup.tq.AddTransaction(req.Tx) | ||
return &sequencing.SubmitRollupTransactionResponse{}, nil | ||
} | ||
gupadhyaya marked this conversation as resolved.
Show resolved
Hide resolved
gupadhyaya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// GetNextBatch implements sequencing.Sequencer. | ||
func (d *MultiRollupSequencer) GetNextBatch(ctx context.Context, req sequencing.GetNextBatchRequest) (*sequencing.GetNextBatchResponse, error) { | ||
rollup, err := d.getOrCreateRollup(req.RollupId) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
now := time.Now() | ||
rollup.lastBatchHashMutex.RLock() | ||
lastBatchHash := rollup.lastBatchHash | ||
rollup.lastBatchHashMutex.RUnlock() | ||
|
||
if !reflect.DeepEqual(lastBatchHash, req.LastBatchHash) { | ||
return nil, fmt.Errorf("batch hash mismatch: lastBatchHash = %x, req.LastBatchHash = %x", lastBatchHash, req.LastBatchHash) | ||
} | ||
|
||
batch := rollup.tq.GetNextBatch(req.MaxBytes) | ||
batchRes := &sequencing.GetNextBatchResponse{Batch: batch, Timestamp: now} | ||
// If there are no transactions, return empty batch without updating the last batch hash | ||
if len(batch.Transactions) == 0 { | ||
return batchRes, nil | ||
} | ||
|
||
h, err := batch.Hash() | ||
if err != nil { | ||
return nil, err | ||
} | ||
gupadhyaya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
rollup.lastBatchHashMutex.Lock() | ||
rollup.lastBatchHash = h | ||
rollup.lastBatchHashMutex.Unlock() | ||
|
||
rollup.seenBatchesMutex.Lock() | ||
rollup.seenBatches[hex.EncodeToString(h)] = struct{}{} | ||
rollup.seenBatchesMutex.Unlock() | ||
return batchRes, nil | ||
} | ||
gupadhyaya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// VerifyBatch implements sequencing.Sequencer. | ||
func (d *MultiRollupSequencer) VerifyBatch(ctx context.Context, req sequencing.VerifyBatchRequest) (*sequencing.VerifyBatchResponse, error) { | ||
rollup, err := d.getOrCreateRollup(req.RollupId) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rollup.seenBatchesMutex.Lock() | ||
defer rollup.seenBatchesMutex.Unlock() | ||
key := hex.EncodeToString(req.BatchHash) | ||
if _, exists := rollup.seenBatches[key]; exists { | ||
return &sequencing.VerifyBatchResponse{Status: true}, nil | ||
} | ||
return &sequencing.VerifyBatchResponse{Status: false}, nil | ||
} | ||
gupadhyaya marked this conversation as resolved.
Show resolved
Hide resolved
gupadhyaya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// getOrCreateRollup returns the RollupData for a given rollupId, creating it if necessary. | ||
func (d *MultiRollupSequencer) getOrCreateRollup(rollupId []byte) (*RollupData, error) { | ||
rollupKey := hex.EncodeToString(rollupId) | ||
|
||
d.rollupsMutex.Lock() | ||
defer d.rollupsMutex.Unlock() | ||
rollup, exists := d.rollups[rollupKey] | ||
if exists { | ||
return rollup, nil | ||
} | ||
|
||
// Double-check existence after acquiring write lock | ||
if rollup, exists := d.rollups[rollupKey]; exists { | ||
return rollup, nil | ||
} | ||
|
||
// Create a new RollupData if it doesn't exist | ||
rollup = &RollupData{ | ||
tq: NewTransactionQueue(), | ||
seenBatches: make(map[string]struct{}, 0), | ||
} | ||
d.rollups[rollupKey] = rollup | ||
return rollup, nil | ||
} | ||
gupadhyaya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// NewMultiRollupSequencer creates a new MultiRollupSequencer | ||
func NewMultiRollupSequencer() *MultiRollupSequencer { | ||
return &MultiRollupSequencer{ | ||
rollups: make(map[string]*RollupData), | ||
} | ||
} | ||
|
||
var _ sequencing.Sequencer = &MultiRollupSequencer{} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.