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