Skip to content

Commit 7f695ad

Browse files
committed
Add min/max round parameters to GET /v2/blocks
1 parent e0bcf13 commit 7f695ad

File tree

8 files changed

+92
-15
lines changed

8 files changed

+92
-15
lines changed

api/converter_utils.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,16 +800,37 @@ func (si *ServerImplementation) blockParamsToBlockFilter(params generated.Search
800800

801801
// Integer
802802
filter.Limit = min(uintOrDefaultValue(params.Limit, si.opts.DefaultBlocksLimit), si.opts.MaxBlocksLimit)
803+
// If min/max are mixed up
804+
//
805+
// This check is performed here instead of in validateBlockFilter because
806+
// when converting params into a filter, the next token is merged with params.MinRound.
807+
if params.MinRound != nil && params.MaxRound != nil && *params.MinRound > *params.MaxRound {
808+
errorArr = append(errorArr, errInvalidRoundMinMax)
809+
}
810+
filter.MaxRound = params.MaxRound
811+
filter.MinRound = params.MinRound
803812

804813
// String
805814
if params.Next != nil {
806815
n, err := idb.DecodeBlockRowNext(*params.Next)
807816
if err != nil {
808817
errorArr = append(errorArr, fmt.Sprintf("%s: %v", errUnableToParseNext, err))
809818
}
810-
filter.MinRound = max(filter.MinRound, n+1)
819+
// Set the MinRound
820+
if filter.MinRound == nil {
821+
filter.MinRound = uint64Ptr(n + 1)
822+
} else {
823+
filter.MinRound = uint64Ptr(max(*filter.MinRound, n+1))
824+
}
811825
}
812826

827+
// If there were any errorArr while setting up the BlockFilter, return now.
828+
if len(errorArr) > 0 {
829+
err = errors.New("invalid input: " + strings.Join(errorArr, ", "))
830+
831+
// clear out the intermediates.
832+
filter = idb.BlockFilter{}
833+
}
813834
return
814835
}
815836

api/generated/v2/routes.go

Lines changed: 22 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/generated/v2/types.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/handlers.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ type ServerImplementation struct {
4848
func validateBlockFilter(filter *idb.BlockFilter) error {
4949
var errorArr = make([]string, 0)
5050

51+
if (filter.MaxRound != nil && *filter.MaxRound > math.MaxInt64) ||
52+
(filter.MinRound != nil && *filter.MinRound > math.MaxInt64) {
53+
errorArr = append(errorArr, errValueExceedingInt64)
54+
}
55+
5156
if len(errorArr) > 0 {
5257
return errors.New("invalid input: " + strings.Join(errorArr, ", "))
5358
}

api/indexer.oas2.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,12 @@
872872
},
873873
{
874874
"$ref": "#/parameters/next"
875+
},
876+
{
877+
"$ref": "#/parameters/min-round"
878+
},
879+
{
880+
"$ref": "#/parameters/max-round"
875881
}
876882
],
877883
"responses": {

api/indexer.oas3.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4823,6 +4823,22 @@
48234823
"schema": {
48244824
"type": "string"
48254825
}
4826+
},
4827+
{
4828+
"description": "Include results at or after the specified min-round.",
4829+
"in": "query",
4830+
"name": "min-round",
4831+
"schema": {
4832+
"type": "integer"
4833+
}
4834+
},
4835+
{
4836+
"description": "Include results at or before the specified max-round.",
4837+
"in": "query",
4838+
"name": "max-round",
4839+
"schema": {
4840+
"type": "integer"
4841+
}
48264842
}
48274843
],
48284844
"responses": {

idb/idb.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ type GetBlockOptions struct {
231231
// BlockFilter is a parameter object with all the block filter options.
232232
type BlockFilter struct {
233233
Limit uint64
234-
MinRound uint64
234+
MaxRound *uint64
235+
MinRound *uint64
235236
}
236237

237238
// TransactionFilter is a parameter object with all the transaction filter options.

idb/postgres/postgres.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -725,14 +725,22 @@ func buildTransactionQuery(tf idb.TransactionFilter) (query string, whereArgs []
725725

726726
func buildBlockQuery(bf idb.BlockFilter) (query string, whereArgs []interface{}, err error) {
727727

728+
// Compute the terms in the WHERE clause
728729
whereParts := make([]string, 0)
729730
whereArgs = make([]interface{}, 0)
730-
partNumber := 1
731+
{
732+
partNumber := 1
731733

732-
if bf.MinRound != 0 {
733-
whereParts = append(whereParts, fmt.Sprintf("round >= $%d", partNumber))
734-
whereArgs = append(whereArgs, bf.MinRound)
735-
partNumber++
734+
if bf.MaxRound != nil {
735+
whereParts = append(whereParts, fmt.Sprintf("round <= $%d", partNumber))
736+
whereArgs = append(whereArgs, *bf.MaxRound)
737+
partNumber++
738+
}
739+
if bf.MinRound != nil {
740+
whereParts = append(whereParts, fmt.Sprintf("round >= $%d", partNumber))
741+
whereArgs = append(whereArgs, *bf.MinRound)
742+
partNumber++
743+
}
736744
}
737745

738746
// SELECT, FROM

0 commit comments

Comments
 (0)