Skip to content

Commit

Permalink
fix: fix logic for getting latest signed batch
Browse files Browse the repository at this point in the history
  • Loading branch information
hacheigriega committed Nov 26, 2024
1 parent 8e4856c commit 54d9e95
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
34 changes: 34 additions & 0 deletions x/batching/keeper/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/sedaprotocol/seda-chain/app/abci"
"github.com/sedaprotocol/seda-chain/x/batching/types"
)

Expand Down Expand Up @@ -100,6 +101,39 @@ func (k Keeper) GetLatestBatch(ctx context.Context) (types.Batch, error) {
return k.GetBatchByBatchNumber(ctx, currentBatchNum)
}

// GetLatestSignedBatch returns the latest batch whose signatures have
// been collected.
func (k Keeper) GetLatestSignedBatch(ctx sdk.Context) (types.Batch, error) {
currentBatchNum, err := k.currentBatchNumber.Peek(ctx)
if err != nil {
return types.Batch{}, err
}
if currentBatchNum == collections.DefaultSequenceStart {
return types.Batch{}, types.ErrBatchingHasNotStarted
}
batch, err := k.GetBatchByBatchNumber(ctx, currentBatchNum)
if err != nil {
return types.Batch{}, err
}

if batch.BlockHeight > ctx.BlockHeight()+abci.BlockOffsetCollectPhase {
currentBatchNum--
for currentBatchNum > collections.DefaultSequenceStart {
batch, err = k.GetBatchByBatchNumber(ctx, currentBatchNum)
if err != nil {
return types.Batch{}, err
}
if batch.BlockHeight <= ctx.BlockHeight()+abci.BlockOffsetCollectPhase {
break
} else if currentBatchNum == collections.DefaultSequenceStart {
return types.Batch{}, types.ErrNoSignedBatches
}
currentBatchNum--
}
}
return batch, nil
}

// IterateBatches iterates over the batches and performs a given
// callback function.
func (k Keeper) IterateBatches(ctx sdk.Context, callback func(types.Batch) (stop bool)) error {
Expand Down
2 changes: 1 addition & 1 deletion x/batching/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (q Querier) Batch(c context.Context, req *types.QueryBatchRequest) (*types.
var batch types.Batch
var err error
if req.BatchNumber == 0 {
batch, err = q.Keeper.GetBatchForHeight(ctx, ctx.BlockHeight()+abci.BlockOffsetCollectPhase)
batch, err = q.Keeper.GetLatestSignedBatch(ctx)
} else {
batch, err = q.Keeper.GetBatchByBatchNumber(ctx, req.BatchNumber)
}
Expand Down
1 change: 1 addition & 0 deletions x/batching/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ var (
ErrBatchAlreadyExists = errors.Register("batching", 4, "batch already exists at the given block height")
ErrInvalidPublicKey = errors.Register("batching", 5, "invalid public key")
ErrNoBatchingUpdate = errors.Register("batching", 6, "no change from previous data result and validator roots")
ErrNoSignedBatches = errors.Register("batching", 7, "there is no signed batch yet")
)

0 comments on commit 54d9e95

Please sign in to comment.