Skip to content

Commit

Permalink
Add min/max time parameters to GET /v2/blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
agodnic committed Oct 16, 2024
1 parent 8e5e975 commit ecf2f87
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 11 deletions.
8 changes: 8 additions & 0 deletions api/converter_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,14 @@ func (si *ServerImplementation) blockParamsToBlockFilter(params generated.Search
}
}

// Time
if params.AfterTime != nil {
filter.AfterTime = *params.AfterTime
}
if params.BeforeTime != nil {
filter.BeforeTime = *params.BeforeTime
}

// If there were any errorArr while setting up the BlockFilter, return now.
if len(errorArr) > 0 {
err = errors.New("invalid input: " + strings.Join(errorArr, ", "))
Expand Down
1 change: 1 addition & 0 deletions api/error_messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
const (
errInvalidRoundAndMinMax = "cannot specify round and min-round/max-round"
errInvalidRoundMinMax = "min-round must be less than max-round"
errInvalidTimeMinMax = "after-time must be less than before-time"
errUnableToParseAddress = "unable to parse address"
errInvalidCreatorAddress = "found an invalid creator address"
errUnableToParseBase64 = "unable to parse base64 data"
Expand Down
30 changes: 22 additions & 8 deletions api/generated/v2/routes.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions api/generated/v2/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,17 @@ type ServerImplementation struct {
func validateBlockFilter(filter *idb.BlockFilter) error {
var errorArr = make([]string, 0)

// Int64 overflows
if (filter.MaxRound != nil && *filter.MaxRound > math.MaxInt64) ||
(filter.MinRound != nil && *filter.MinRound > math.MaxInt64) {
errorArr = append(errorArr, errValueExceedingInt64)
}

// Time
if !filter.AfterTime.IsZero() && !filter.BeforeTime.IsZero() && filter.AfterTime.After(filter.BeforeTime) {
errorArr = append(errorArr, errInvalidTimeMinMax)
}

if len(errorArr) > 0 {
return errors.New("invalid input: " + strings.Join(errorArr, ", "))
}
Expand Down
6 changes: 6 additions & 0 deletions api/indexer.oas2.json
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,12 @@
},
{
"$ref": "#/parameters/max-round"
},
{
"$ref": "#/parameters/before-time"
},
{
"$ref": "#/parameters/after-time"
}
],
"responses": {
Expand Down
22 changes: 22 additions & 0 deletions api/indexer.oas3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4839,6 +4839,28 @@
"schema": {
"type": "integer"
}
},
{
"description": "Include results before the given time. Must be an RFC 3339 formatted string.",
"in": "query",
"name": "before-time",
"schema": {
"format": "date-time",
"type": "string",
"x-algorand-format": "RFC3339 String"
},
"x-algorand-format": "RFC3339 String"
},
{
"description": "Include results after the given time. Must be an RFC 3339 formatted string.",
"in": "query",
"name": "after-time",
"schema": {
"format": "date-time",
"type": "string",
"x-algorand-format": "RFC3339 String"
},
"x-algorand-format": "RFC3339 String"
}
],
"responses": {
Expand Down
8 changes: 5 additions & 3 deletions idb/idb.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,11 @@ type GetBlockOptions struct {

// BlockFilter is a parameter object with all the block filter options.
type BlockFilter struct {
Limit uint64
MaxRound *uint64
MinRound *uint64
Limit uint64
MaxRound *uint64
MinRound *uint64
AfterTime time.Time
BeforeTime time.Time
}

// TransactionFilter is a parameter object with all the transaction filter options.
Expand Down
16 changes: 16 additions & 0 deletions idb/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,22 @@ func buildBlockQuery(bf idb.BlockFilter) (query string, whereArgs []interface{},
whereParts = append(whereParts, fmt.Sprintf("round >= $%d", partNumber))
whereArgs = append(whereArgs, *bf.MinRound)
}
if !bf.AfterTime.IsZero() {
partNumber++
whereParts = append(
whereParts,
fmt.Sprintf("round >= (SELECT tmp.round FROM block_header tmp WHERE tmp.realtime > $%d ORDER BY tmp.realtime ASC, tmp.round ASC LIMIT 1)", partNumber),
)
whereArgs = append(whereArgs, bf.AfterTime)
}
if !bf.BeforeTime.IsZero() {
partNumber++
whereParts = append(
whereParts,
fmt.Sprintf("round <= (SELECT tmp.round FROM block_header tmp WHERE tmp.realtime < $%d ORDER BY tmp.realtime DESC, tmp.round DESC LIMIT 1)", partNumber),
)
whereArgs = append(whereArgs, bf.BeforeTime)
}
}

// SELECT, FROM
Expand Down

0 comments on commit ecf2f87

Please sign in to comment.