Skip to content

Commit

Permalink
Merge pull request #760 from oasisprotocol/ptrus/feature/validator-si…
Browse files Browse the repository at this point in the history
…gned-blocks

api/consensus/validators: Expose validator signed blocks info
  • Loading branch information
ptrus authored Oct 10, 2024
2 parents a42f1e4 + 1e7c373 commit daf42bf
Show file tree
Hide file tree
Showing 9 changed files with 860 additions and 3 deletions.
1 change: 1 addition & 0 deletions .changelog/760.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
api/consensus/valudators: Expose validator signed blocks info
20 changes: 20 additions & 0 deletions api/spec/v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2080,9 +2080,29 @@ components:
description: Commission rate.
current_commission_bound:
allOf: [$ref: '#/components/schemas/ValidatorCommissionBound']
signed_blocks:
type: array
description: An array containing details of the last 100 consensus blocks, indicating whether each block was signed by the validator. Only available when querying a single validator.
items:
allOf: [$ref: '#/components/schemas/ValidatorSignedBlock']
description: |
An validator registered at the consensus layer.
ValidatorSignedBlock:
type: object
required: [height, signed]
properties:
height:
type: integer
format: int64
description: The block height.
example: *block_height_1
signed:
type: boolean
description: Whether the validator signed the block.
description: |
Information whether a block was signed by the validator.
Escrow:
type: object
properties:
Expand Down
25 changes: 24 additions & 1 deletion storage/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1252,7 +1252,7 @@ func (c *StorageClient) Validators(ctx context.Context, p apiTypes.GetConsensusV
}
var schedule staking.CommissionSchedule
var logoUrl *string
if err := res.rows.Scan(
if err = res.rows.Scan(
&v.EntityID,
&v.EntityAddress,
&v.NodeID,
Expand Down Expand Up @@ -1303,6 +1303,29 @@ func (c *StorageClient) Validators(ctx context.Context, p apiTypes.GetConsensusV
vs.Validators = append(vs.Validators, v)
}

// When querying for a single validator, include the detailed block sign data for last 100 blocks.
if address != nil && len(vs.Validators) == 1 {
rows, err := c.db.Query(ctx, queries.ValidatorLast100BlocksSigned, vs.Validators[0].EntityID)
if err != nil {
return nil, wrapError(err)
}
defer rows.Close()

signedBlocks := []ValidatorSignedBlock{}
for rows.Next() {
var height int64
var signed bool
if err = rows.Scan(
&height,
&signed,
); err != nil {
return nil, wrapError(err)
}
signedBlocks = append(signedBlocks, ValidatorSignedBlock{Height: height, Signed: signed})
}
vs.Validators[0].SignedBlocks = &signedBlocks
}

return &vs, nil
}

Expand Down
6 changes: 6 additions & 0 deletions storage/client/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,12 @@ const (
JOIN chain.accounts AS accts ON entities.address = accts.address)
, 0) AS total_staked_balance`

ValidatorLast100BlocksSigned = `
SELECT height, COALESCE($1 = ANY(signer_entity_ids), FALSE)
FROM chain.blocks
ORDER BY height DESC
LIMIT 100`

ValidatorsData = `
WITH
-- Find all self-delegations for all accounts with active delegations.
Expand Down
3 changes: 3 additions & 0 deletions storage/client/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ type ValidatorMedia = api.ValidatorMedia
// ValidatorCommissionBound is the commission bound for a validator.
type ValidatorCommissionBound = api.ValidatorCommissionBound

// ValidatorSignedBlock is the information weather a validator has signed a specific block.
type ValidatorSignedBlock = api.ValidatorSignedBlock

// ValidatorHistory is the storage response for GetValidatorHistory.
type ValidatorHistory = api.ValidatorHistory

Expand Down
Loading

0 comments on commit daf42bf

Please sign in to comment.