Skip to content

Commit

Permalink
refactor: update sequencing methods to use go-sequencing v0.2.0
Browse files Browse the repository at this point in the history
Refactored methods to use request/response structures for better readability and maintainability. Adjusted tests accordingly.
  • Loading branch information
tzdybal committed Oct 3, 2024
1 parent 5b30e6f commit c63a9ba
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 39 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.23.1

require (
github.com/rollkit/go-da v0.7.1-0.20240927084534-0289b27dbd20
github.com/rollkit/go-sequencing v0.0.0-20240924073851-5e3b8522a072
github.com/rollkit/go-sequencing v0.2.0
github.com/stretchr/testify v1.9.0
)

Expand All @@ -21,7 +21,7 @@ require (
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.17.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240827150818-7e3bb234dfed // indirect
google.golang.org/grpc v1.67.0 // indirect
google.golang.org/grpc v1.67.1 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ github.com/rollkit/go-da v0.7.1-0.20240927084534-0289b27dbd20 h1:MkNGLP+SIC4MiA0
github.com/rollkit/go-da v0.7.1-0.20240927084534-0289b27dbd20/go.mod h1:3eHWK5gkv8lhwq6bjOZOi82WwHyS2B9rQOlUrE1GGws=
github.com/rollkit/go-sequencing v0.0.0-20240924073851-5e3b8522a072 h1:ryzQ53hpEZ/tzsweDkEFUP3ovFcmi0IBye4JhCNIs+8=
github.com/rollkit/go-sequencing v0.0.0-20240924073851-5e3b8522a072/go.mod h1:s/3XzHYeY+bximgM8PDdQmoe9aWBSOa4NDQmBxpMJ4A=
github.com/rollkit/go-sequencing v0.1.0 h1:LL6QT5pekydPLxeU8ncRoRlkdbJpM8fcsu+Uw15H4sE=
github.com/rollkit/go-sequencing v0.1.0/go.mod h1:s/3XzHYeY+bximgM8PDdQmoe9aWBSOa4NDQmBxpMJ4A=
github.com/rollkit/go-sequencing v0.2.0 h1:IrEA29p07zPDbqY29AzI+q2zUqm7QACAoR+/PBpexvw=
github.com/rollkit/go-sequencing v0.2.0/go.mod h1:P/cQXTw3rWpPqhqnCwKzlkS39XM8ugmyf2u63twBgG8=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
Expand All @@ -33,6 +37,8 @@ google.golang.org/genproto/googleapis/rpc v0.0.0-20240827150818-7e3bb234dfed h1:
google.golang.org/genproto/googleapis/rpc v0.0.0-20240827150818-7e3bb234dfed/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU=
google.golang.org/grpc v1.67.0 h1:IdH9y6PF5MPSdAntIcpjQ+tXO41pcQsfZV2RxtQgVcw=
google.golang.org/grpc v1.67.0/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA=
google.golang.org/grpc v1.67.1 h1:zWnc1Vrcno+lHZCOofnIMvycFcc0QRGIzm9dhnDX68E=
google.golang.org/grpc v1.67.1/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
54 changes: 22 additions & 32 deletions sequencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package based_sequencer

import (
"context"
"crypto/sha256"
"errors"
"fmt"
"strings"
Expand Down Expand Up @@ -38,87 +37,78 @@ func NewSequencer(da da.DA) *BasedSequencer {
var _ sequencing.Sequencer = (*BasedSequencer)(nil)

// SubmitRollupTransaction submits a transaction directly to DA, as a single blob.
func (seq *BasedSequencer) SubmitRollupTransaction(ctx context.Context, rollupId sequencing.RollupId, tx sequencing.Tx) error {
_, err := seq.da.Submit(ctx, []da.Blob{tx}, 0, rollupId)
func (seq *BasedSequencer) SubmitRollupTransaction(ctx context.Context, req sequencing.SubmitRollupTransactionRequest) (*sequencing.SubmitRollupTransactionResponse, error) {
_, err := seq.da.Submit(ctx, []da.Blob{req.Tx}, 0, req.RollupId)
if err != nil {
return err
return nil, err
}
return nil
return &sequencing.SubmitRollupTransactionResponse{}, nil
}

// GetNextBatch reads data from namespace in DA and builds transactions batches.
func (seq *BasedSequencer) GetNextBatch(ctx context.Context, lastBatchHash sequencing.Hash) (*sequencing.Batch, time.Time, error) {
func (seq *BasedSequencer) GetNextBatch(ctx context.Context, req sequencing.GetNextBatchRequest) (*sequencing.GetNextBatchResponse, error) {
// optimistic case - we already know the hash after lastBatchHash
nextBatchHash, err := seq.store.GetNextHash(ctx, lastBatchHash)
nextBatchHash, err := seq.store.GetNextHash(ctx, req.LastBatchHash)
if err == nil {
height, err := seq.store.GetHashMapping(ctx, nextBatchHash)
if err != nil {
return nil, time.Time{}, fmt.Errorf("store contents inconsistent: %w", err)
return nil, fmt.Errorf("store contents inconsistent: %w", err)
}
return seq.getBatchByHeight(ctx, height)
}

// if there is no indexing information in store, it's not an error
if !errors.Is(err, ds.ErrNotFound) {
return nil, time.Time{}, err
return nil, err
}

// we need to search for next batch
for ctx.Err() == nil {
batch, ts, err := seq.getBatchByHeight(ctx, seq.daHeight.Load())
resp, err := seq.getBatchByHeight(ctx, seq.daHeight.Load())
if err != nil {
if strings.Contains(err.Error(), "given height is from the future") {
time.Sleep(100 * time.Millisecond) // TODO(tzdybal): this needs to be configurable
continue
}
return nil, time.Time{}, err
return nil, err
}

// TODO(tzdybal): extract method
hash, err := BatchHash(batch)
hash, err := resp.Batch.Hash()
if err != nil {
return nil, time.Time{}, err
return nil, err
}
if err = seq.store.SetNextHash(ctx, lastBatchHash, hash); err != nil {
return nil, time.Time{}, err
if err = seq.store.SetNextHash(ctx, req.LastBatchHash, hash); err != nil {
return nil, err
}
if err = seq.store.SetHashMapping(ctx, hash, seq.daHeight.Load()); err != nil {
return nil, time.Time{}, err
return nil, err
}
seq.daHeight.Add(1)
return batch, ts, err
return resp, err
}

return nil, time.Time{}, ctx.Err()
return nil, ctx.Err()
}

// VerifyBatch ensures data-availability of a batch in DA.
func (seq *BasedSequencer) VerifyBatch(ctx context.Context, batchHash sequencing.Hash) (bool, error) {
func (seq *BasedSequencer) VerifyBatch(ctx context.Context, req sequencing.VerifyBatchRequest) (*sequencing.VerifyBatchResponse, error) {
//TODO implement me
panic("implement me")
}

func BatchHash(batch *sequencing.Batch) (sequencing.Hash, error) {
batchBytes, err := batch.Marshal()
if err != nil {
return nil, err
}
h := sha256.Sum256(batchBytes)
return h[:], nil
}

func (seq *BasedSequencer) getBatchByHeight(ctx context.Context, height uint64) (*sequencing.Batch, time.Time, error) {
func (seq *BasedSequencer) getBatchByHeight(ctx context.Context, height uint64) (*sequencing.GetNextBatchResponse, error) {
// TODO(tzdybal): this needs to be a field
namespace := []byte("test namespace")
result, err := seq.da.GetIDs(ctx, height, namespace)
if err != nil {
return nil, time.Time{}, err
return nil, err
}

blobs, err := seq.da.Get(ctx, result.IDs, namespace)
if err != nil {
return nil, time.Time{}, err
return nil, err
}

return &sequencing.Batch{Transactions: blobs}, result.Timestamp, nil
return &sequencing.GetNextBatchResponse{Batch: &sequencing.Batch{Transactions: blobs}, Timestamp: result.Timestamp}, nil
}
12 changes: 7 additions & 5 deletions sequencer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ func TestSubmitRollupTransaction(t *testing.T) {
require.Contains(t, string(blobs[0]), testTx)
}).Return(nil, nil).Once()

err := seq.SubmitRollupTransaction(context.Background(), sequencing.RollupId(rollupId), sequencing.Tx(testTx))
resp, err := seq.SubmitRollupTransaction(context.Background(),
sequencing.SubmitRollupTransactionRequest{RollupId: sequencing.RollupId(rollupId), Tx: sequencing.Tx(testTx)})
require.NoError(t, err)
require.NotNil(t, resp)

mockDA.AssertExpectations(t)
}
Expand Down Expand Up @@ -97,14 +99,14 @@ func TestGetNextBatch(t *testing.T) {

var lastBatchHash sequencing.Hash
for i := 0; i < len(batchIds); i++ {
batch, ts, err := seq.GetNextBatch(ctx, lastBatchHash)
resp, err := seq.GetNextBatch(ctx, sequencing.GetNextBatchRequest{LastBatchHash: lastBatchHash})
require.NoError(t, err)
require.NotNil(t, batch)
require.NotNil(t, resp)
require.NotEmpty(t, ts)
require.NoError(t, err)
expected := makeBatch(batchIds[i])
require.Equal(t, expected, batch.Transactions)
lastBatchHash, err = BatchHash(batch)
require.Equal(t, expected, resp.Batch.Transactions)
lastBatchHash, err = resp.Batch.Hash()
require.NoError(t, err)
}

Expand Down

0 comments on commit c63a9ba

Please sign in to comment.