Skip to content

Commit

Permalink
api/consensus/valudators: Expose validator signed blocks info
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrus committed Oct 2, 2024
1 parent 73df7db commit 771b898
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
22 changes: 21 additions & 1 deletion api/spec/v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1981,7 +1981,7 @@ components:

Validator:
type: object
required: [entity_address, entity_id, escrow, voting_power, active, start_date, rank, in_validator_set, current_rate, current_commission_bound]
required: [entity_address, entity_id, escrow, voting_power, active, start_date, rank, in_validator_set, current_rate, current_commission_bound, signed_blocks]
properties:
entity_address:
type: string
Expand Down Expand Up @@ -2032,9 +2032,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.
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 at which this block was signed.
example: *block_height_1
signed:
type: boolean
description: Whether the validator signed the block.
description: |
Information weather a block was signed by the validator.
Escrow:
type: object
properties:
Expand Down
37 changes: 36 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,41 @@ func (c *StorageClient) Validators(ctx context.Context, p apiTypes.GetConsensusV
vs.Validators = append(vs.Validators, v)
}

// Load validators blocks signed data for last 100 blocks.
rows, err := c.db.Query(ctx, queries.ValidatorsBlocksSigned)
if err != nil {
return nil, wrapError(err)
}
defer rows.Close()
for rows.Next() {
var height int64
var signersArr []string
if err = rows.Scan(
&height,
&signersArr,
); err != nil {
return nil, wrapError(err)
}
// Lookup map of signers.
signers := make(map[string]struct{})
for _, s := range signersArr {
signers[s] = struct{}{}
}

// Go through all validators for each height and mark weather it has singed the block.
for i, v := range vs.Validators {
if v.SignedBlocks == nil {
v.SignedBlocks = []ValidatorSignedBlock{}
}
_, exists := signers[v.EntityID]
v.SignedBlocks = append(v.SignedBlocks, ValidatorSignedBlock{
Height: height,
Signed: exists,
})
vs.Validators[i] = v
}
}

return &vs, nil
}

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

ValidatorsBlocksSigned = `
SELECT height, signer_entity_ids
FROM (
SELECT height, signer_entity_ids
FROM chain.blocks
ORDER BY height DESC
LIMIT 100
) AS subquery
ORDER BY height ASC`

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

0 comments on commit 771b898

Please sign in to comment.