Skip to content

Commit

Permalink
storage: query block proposer and signers
Browse files Browse the repository at this point in the history
  • Loading branch information
pro-wh committed Mar 18, 2024
1 parent 52e13e7 commit 003826c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
48 changes: 47 additions & 1 deletion storage/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"encoding/base64"
"encoding/json"
"fmt"
"math/big"
"strings"
Expand Down Expand Up @@ -252,6 +253,26 @@ func (c *StorageClient) Status(ctx context.Context) (*Status, error) {
return &s, nil
}

type nodeInfoRow struct {
NodeID *string
EntityID *string
EntityAddress *string
EntityMetadata *json.RawMessage
}

func nodeInfoFromRow(r nodeInfoRow) (apiTypes.NodeInfo, error) {
var entityMetadataAny any
if r.EntityMetadata != nil {
entityMetadataAny = *r.EntityMetadata
}
return apiTypes.NodeInfo{
EntityAddress: r.EntityAddress,
EntityId: r.EntityID,
EntityMetadata: &entityMetadataAny,
NodeId: r.NodeID,
}, nil
}

// Blocks returns a list of consensus blocks.
func (c *StorageClient) Blocks(ctx context.Context, r apiTypes.GetConsensusBlocksParams) (*BlockList, error) {
hash, err := canonicalizedHash(r.Hash)
Expand Down Expand Up @@ -281,10 +302,35 @@ func (c *StorageClient) Blocks(ctx context.Context, r apiTypes.GetConsensusBlock
}
for res.rows.Next() {
var b Block
if err := res.rows.Scan(&b.Height, &b.Hash, &b.Timestamp, &b.NumTransactions); err != nil {
var metadata json.RawMessage
var proposerRow nodeInfoRow
var signerRows []nodeInfoRow
if err := res.rows.Scan(
&b.Height,
&b.Hash,
&b.Timestamp,
&b.NumTransactions,
&metadata,
&proposerRow,
&signerRows,
); err != nil {
return nil, wrapError(err)
}
b.Timestamp = b.Timestamp.UTC()
proposer, err := nodeInfoFromRow(proposerRow)
if err != nil {
return nil, fmt.Errorf("converting block %d proposer: %w", b.Height, err)
}
b.Proposer = &proposer
signers := make([]apiTypes.NodeInfo, 0, len(signerRows))
for i, signerRow := range signerRows {
signer, err := nodeInfoFromRow(signerRow)
if err != nil {
return nil, fmt.Errorf("converting block %d signer %d: %w", b.Height, i, err)
}
signers = append(signers, signer)
}
b.Signers = &signers

bs.Blocks = append(bs.Blocks, b)
}
Expand Down
23 changes: 22 additions & 1 deletion storage/client/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,29 @@ const (
height,
block_hash,
time,
num_txs
num_txs,
metadata,
ROW(
proposer_node.id,
proposer_node.entity_id,
proposer_entity.address,
proposer_entity.meta
) AS proposer_node_info,
ARRAY(
SELECT
ROW(
signer_node.id,
signer_node.entity_id,
signer_entity.address,
signer_entity.meta
)
FROM UNNEST(signer_node_consensus_pubkey_addresses) AS signer_node_consensus_pubkey_address
LEFT JOIN chain.nodes AS signer_node ON signer_node.consensus_pubkey_address = signer_node_consensus_pubkey_address
LEFT JOIN chain.entities AS signer_entity ON signer_entity.id = signer_node.entity_id
) AS signer_node_infos
FROM chain.blocks
LEFT JOIN chain.nodes AS proposer_node ON proposer_node.consensus_pubkey_address = proposer_node_consensus_pubkey_address
LEFT JOIN chain.entities AS proposer_entity ON proposer_entity.id = proposer_node.entity_id
WHERE
($1::bigint IS NULL OR height >= $1::bigint) AND
($2::bigint IS NULL OR height <= $2::bigint) AND
Expand Down

0 comments on commit 003826c

Please sign in to comment.