Skip to content

Commit 7104199

Browse files
committed
return error when no batch
1 parent c8163df commit 7104199

2 files changed

Lines changed: 63 additions & 7 deletions

File tree

block/manager.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ const DAIncludedHeightKey = "da included height"
8080
// dataHashForEmptyTxs to be used while only syncing headers from DA and no p2p to get the Data for no txs scenarios, the syncing can proceed without getting stuck forever.
8181
var dataHashForEmptyTxs = []byte{110, 52, 11, 156, 255, 179, 122, 152, 156, 165, 68, 230, 187, 120, 10, 44, 120, 144, 29, 63, 179, 55, 56, 118, 133, 17, 163, 6, 23, 175, 160, 29}
8282

83+
// ErrNoBatch indicate no batch is available for creating block
84+
var ErrNoBatch = errors.New("no batch to process")
85+
8386
// NewHeaderEvent is used to pass header and DA height to headerInCh
8487
type NewHeaderEvent struct {
8588
Header *types.SignedHeader
@@ -989,18 +992,17 @@ func (m *Manager) getSignature(header types.Header) (*types.Signature, error) {
989992
return &signature, nil
990993
}
991994

992-
func (m *Manager) getTxsFromBatch() (cmtypes.Txs, time.Time) {
995+
func (m *Manager) getTxsFromBatch() (cmtypes.Txs, *time.Time, error) {
993996
batch := m.bq.Next()
994997
if batch == nil {
995-
// this excludes empty batches
996-
// empty batches still gets the timestamp from the sequencer
997-
return make(cmtypes.Txs, 0), time.Now()
998+
// batch is nil when there is nothing to process
999+
return nil, nil, ErrNoBatch
9981000
}
9991001
txs := make(cmtypes.Txs, 0, len(batch.Transactions))
10001002
for _, tx := range batch.Transactions {
10011003
txs = append(txs, tx)
10021004
}
1003-
return txs, batch.Time
1005+
return txs, &batch.Time, nil
10041006
}
10051007

10061008
func (m *Manager) publishBlock(ctx context.Context) error {
@@ -1063,8 +1065,11 @@ func (m *Manager) publishBlock(ctx context.Context) error {
10631065
return fmt.Errorf("failed to load extended commit for height %d: %w", height, err)
10641066
}
10651067

1066-
txs, timestamp := m.getTxsFromBatch()
1067-
header, data, err = m.createBlock(newHeight, lastSignature, lastHeaderHash, extendedCommit, txs, timestamp)
1068+
txs, timestamp, err := m.getTxsFromBatch()
1069+
if err != nil {
1070+
return fmt.Errorf("failed to get transactions from batch: %w", err)
1071+
}
1072+
header, data, err = m.createBlock(newHeight, lastSignature, lastHeaderHash, extendedCommit, txs, *timestamp)
10681073
if err != nil {
10691074
return err
10701075
}

block/manager_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030

3131
goDA "github.com/rollkit/go-da"
3232
goDATest "github.com/rollkit/go-da/test"
33+
"github.com/rollkit/go-sequencing"
3334

3435
seqGRPC "github.com/rollkit/go-sequencing/proxy/grpc"
3536
"github.com/rollkit/rollkit/config"
@@ -920,3 +921,53 @@ func TestNormalAggregationLoop(t *testing.T) {
920921
// Wait for the function to complete or timeout
921922
<-ctx.Done()
922923
}
924+
925+
func TestGetTxsFromBatch_NoBatch(t *testing.T) {
926+
// Mocking a manager with an empty batch queue
927+
m := &Manager{
928+
bq: &BatchQueue{queue: nil}, // No batch available
929+
}
930+
931+
// Call the method and assert the results
932+
txs, timestamp, err := m.getTxsFromBatch()
933+
934+
// Assertions
935+
assert.Nil(t, txs, "Transactions should be nil when no batch exists")
936+
assert.Nil(t, timestamp, "Timestamp should be nil when no batch exists")
937+
assert.Equal(t, ErrNoBatch, err, "Expected ErrNoBatch error")
938+
}
939+
940+
func TestGetTxsFromBatch_EmptyBatch(t *testing.T) {
941+
// Mocking a manager with an empty batch
942+
m := &Manager{
943+
bq: &BatchQueue{queue: []BatchWithTime{
944+
{Batch: &sequencing.Batch{Transactions: nil}, Time: time.Now()},
945+
}},
946+
}
947+
948+
// Call the method and assert the results
949+
txs, timestamp, err := m.getTxsFromBatch()
950+
951+
// Assertions
952+
require.NoError(t, err, "Expected no error for empty batch")
953+
assert.Empty(t, txs, "Transactions should be empty when batch has no transactions")
954+
assert.NotNil(t, timestamp, "Timestamp should not be nil for empty batch")
955+
}
956+
957+
func TestGetTxsFromBatch_ValidBatch(t *testing.T) {
958+
// Mocking a manager with a valid batch
959+
m := &Manager{
960+
bq: &BatchQueue{queue: []BatchWithTime{
961+
{Batch: &sequencing.Batch{Transactions: [][]byte{[]byte("tx1"), []byte("tx2")}}, Time: time.Now()},
962+
}},
963+
}
964+
965+
// Call the method and assert the results
966+
txs, timestamp, err := m.getTxsFromBatch()
967+
968+
// Assertions
969+
require.NoError(t, err, "Expected no error for valid batch")
970+
assert.Len(t, txs, 2, "Expected 2 transactions")
971+
assert.NotNil(t, timestamp, "Timestamp should not be nil for valid batch")
972+
assert.Equal(t, cmtypes.Txs{cmtypes.Tx([]byte("tx1")), cmtypes.Tx([]byte("tx2"))}, txs, "Transactions do not match")
973+
}

0 commit comments

Comments
 (0)