Skip to content

Commit

Permalink
Merge pull request #581 from oasisprotocol/andrew7234/block-api-hash
Browse files Browse the repository at this point in the history
query block by hash
  • Loading branch information
Andrew7234 authored Dec 6, 2023
2 parents d7c9b0d + 9aaec19 commit 56c1471
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 123 deletions.
12 changes: 12 additions & 0 deletions api/spec/v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,12 @@ paths:
format: date-time
description: A filter on maximum block time, exclusive.
example: *iso_timestamp_2
- in: query
name: hash
schema:
type: string
description: The block hash of the block to return.
example: *block_hash_1
responses:
'200':
description: A JSON object containing a list of consensus blocks.
Expand Down Expand Up @@ -815,6 +821,12 @@ paths:
format: date-time
description: A filter on maximum block time, exclusive.
example: *iso_timestamp_2
- in: query
name: hash
schema:
type: string
description: The block hash of the block to return.
example: *block_hash_1
responses:
'200':
description: A JSON object containing a list of Runtime blocks.
Expand Down
117 changes: 0 additions & 117 deletions storage/client/api.go

This file was deleted.

25 changes: 25 additions & 0 deletions storage/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"database/sql"
"fmt"
"math/big"
"strings"
"time"

"github.com/dgraph-io/ristretto"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"

"github.com/oasisprotocol/oasis-core/go/common/crypto/hash"
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
oasisErrors "github.com/oasisprotocol/oasis-core/go/common/errors"
oasisConfig "github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
Expand Down Expand Up @@ -244,13 +246,18 @@ func (c *StorageClient) Status(ctx context.Context) (*Status, error) {

// Blocks returns a list of consensus blocks.
func (c *StorageClient) Blocks(ctx context.Context, r apiTypes.GetConsensusBlocksParams) (*BlockList, error) {
hash, err := canonicalizedHash(r.Hash)
if err != nil {
return nil, wrapError(err)
}
res, err := c.withTotalCount(
ctx,
queries.Blocks,
r.From,
r.To,
r.After,
r.Before,
hash,
r.Limit,
r.Offset,
)
Expand All @@ -277,6 +284,19 @@ func (c *StorageClient) Blocks(ctx context.Context, r apiTypes.GetConsensusBlock
return &bs, nil
}

func canonicalizedHash(input *string) (*string, error) {
if input == nil {
return nil, nil
}
sanitized, _ := strings.CutPrefix(*input, "0x")
var h hash.Hash
if err := h.UnmarshalHex(sanitized); err != nil {
return nil, fmt.Errorf("invalid hash: %w", err)
}
s := h.String()
return &s, nil
}

// Block returns a consensus block. This endpoint is cached.
func (c *StorageClient) Block(ctx context.Context, height int64) (*Block, error) {
// Check cache
Expand Down Expand Up @@ -1096,6 +1116,10 @@ func (c *StorageClient) Validators(ctx context.Context, p apiTypes.GetConsensusV

// RuntimeBlocks returns a list of runtime blocks.
func (c *StorageClient) RuntimeBlocks(ctx context.Context, p apiTypes.GetRuntimeBlocksParams) (*RuntimeBlockList, error) {
hash, err := canonicalizedHash(p.Hash)
if err != nil {
return nil, wrapError(err)
}
res, err := c.withTotalCount(
ctx,
queries.RuntimeBlocks,
Expand All @@ -1104,6 +1128,7 @@ func (c *StorageClient) RuntimeBlocks(ctx context.Context, p apiTypes.GetRuntime
p.To,
p.After,
p.Before,
hash,
p.Limit,
p.Offset,
)
Expand Down
14 changes: 8 additions & 6 deletions storage/client/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ const (
WHERE ($1::bigint IS NULL OR height >= $1::bigint) AND
($2::bigint IS NULL OR height <= $2::bigint) AND
($3::timestamptz IS NULL OR time >= $3::timestamptz) AND
($4::timestamptz IS NULL OR time < $4::timestamptz)
($4::timestamptz IS NULL OR time < $4::timestamptz) AND
($5::text IS NULL OR block_hash = $5::text)
ORDER BY height DESC
LIMIT $5::bigint
OFFSET $6::bigint`
LIMIT $6::bigint
OFFSET $7::bigint`

Block = `
SELECT height, block_hash, time, num_txs
Expand Down Expand Up @@ -290,10 +291,11 @@ const (
($2::bigint IS NULL OR round >= $2::bigint) AND
($3::bigint IS NULL OR round <= $3::bigint) AND
($4::timestamptz IS NULL OR timestamp >= $4::timestamptz) AND
($5::timestamptz IS NULL OR timestamp < $5::timestamptz)
($5::timestamptz IS NULL OR timestamp < $5::timestamptz) AND
($6::text IS NULL OR block_hash = $6::text)
ORDER BY round DESC
LIMIT $6::bigint
OFFSET $7::bigint`
LIMIT $7::bigint
OFFSET $8::bigint`

RuntimeBlock = `
SELECT round, block_hash, timestamp, num_transactions, size, gas_used
Expand Down
3 changes: 3 additions & 0 deletions storage/client/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package client

import (
api "github.com/oasisprotocol/nexus/api/v1/types"
"github.com/oasisprotocol/nexus/common"
)

type BigInt = common.BigInt

// Status is the storage response for GetStatus.
type Status = api.Status

Expand Down

0 comments on commit 56c1471

Please sign in to comment.