From c6954760907680ab3f492f518d58d3d90237bed2 Mon Sep 17 00:00:00 2001 From: CHAMI Rachid Date: Fri, 11 Aug 2023 14:56:29 +0200 Subject: [PATCH] fix: end exclusive data commitment range fix (#1058) (#1060) ## Description Cherry pick https://github.com/celestiaorg/celestia-core/pull/1058 to main #### 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 --------- ## Description _Please add a description of the changes that this PR introduces and the files that are the most critical to review._ If this PR is non-trivial/large/complex, please ensure that you have either created an issue that the team's had a chance to respond to, or had some discussion with the team prior to submitting substantial pull requests. The team can be reached via GitHub Discussions or the Cosmos Network Discord server in the #cometbft channel. GitHub Discussions is preferred over Discord as it allows us to keep track of conversations topically. https://github.com/cometbft/cometbft/discussions If the work in this PR is not aligned with the team's current priorities, please be advised that it may take some time before it is merged - especially if it has not yet been discussed with the team. See the project board for the team's current priorities: https://github.com/orgs/cometbft/projects/1 --> --- #### 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