From 12c6d454655d3112b974660efd9a529521a4b8bd Mon Sep 17 00:00:00 2001 From: CHAMI Rachid Date: Thu, 10 Aug 2023 16:44:44 +0200 Subject: [PATCH] fix: end exclusive data commitment range fix (#1058) ## Description Fixes https://github.com/celestiaorg/orchestrator-relayer/issues/432 #### PR checklist - [ ] Tests written/updated - [ ] Changelog entry added in `.changelog` (we use [unclog](https://github.com/informalsystems/unclog) to manage our changelog) - [ ] Updated relevant documentation (`docs/` or `spec/`) and code comments --------- Co-authored-by: Rootul P --- rpc/core/blocks.go | 5 +++-- rpc/core/blocks_test.go | 11 ++++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/rpc/core/blocks.go b/rpc/core/blocks.go index ceb20040d4..09ebe391c2 100644 --- a/rpc/core/blocks.go +++ b/rpc/core/blocks.go @@ -351,9 +351,10 @@ func validateDataCommitmentRange(start uint64, end uint64) error { if start >= end { return fmt.Errorf("last block is smaller than first block") } - if end > uint64(env.BlockStore.Height()) { + // the data commitment range is end exclusive + if end > uint64(env.BlockStore.Height())+1 { return fmt.Errorf( - "last block %d is higher than current chain height %d", + "end block %d is higher than current chain height %d", end, env.BlockStore.Height(), ) diff --git a/rpc/core/blocks_test.go b/rpc/core/blocks_test.go index aa50930c3e..00193876a5 100644 --- a/rpc/core/blocks_test.go +++ b/rpc/core/blocks_test.go @@ -168,6 +168,11 @@ func TestDataCommitmentResults(t *testing.T) { {0, 1000, false}, {0, 10, false}, {10, 8, false}, + // to test the end exclusive support for ranges. + // the end block could be equal to (height+1), but the data commitment would only + // take up to height. So we should be able to send request having end block equal + // to (height+1). + {int(env.BlockStore.Height()) - 100, int(env.BlockStore.Height()) + 1, true}, } for i, tc := range testCases { @@ -334,10 +339,10 @@ func (indexer mockBlockIndexer) Search(ctx context.Context, _ *query.Query) ([]i return results, nil } -// randomBlocks generates a set of random blocks up to the provided height. +// randomBlocks generates a set of random blocks up to (and including) the provided height. func randomBlocks(height int64) []*types.Block { - blocks := make([]*types.Block, height) - for i := int64(0); i < height; i++ { + blocks := make([]*types.Block, height+1) + for i := int64(0); i <= height; i++ { blocks[i] = randomBlock(i) } return blocks