Skip to content

Commit

Permalink
analyzer/consensus: Support ParametersChange proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrus committed Nov 30, 2023
1 parent f642372 commit 246651f
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 23 deletions.
18 changes: 16 additions & 2 deletions analyzer/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,8 @@ func (m *processor) queueSubmissions(batch *storage.QueryBatch, data *governance
}

for _, submission := range data.ProposalSubmissions {
if submission.Content.Upgrade != nil {
switch {
case submission.Content.Upgrade != nil:
batch.Queue(queries.ConsensusProposalSubmissionInsert,
submission.ID,
submission.Submitter.String(),
Expand All @@ -905,7 +906,7 @@ func (m *processor) queueSubmissions(batch *storage.QueryBatch, data *governance
submission.CreatedAt,
submission.ClosesAt,
)
} else if submission.Content.CancelUpgrade != nil {
case submission.Content.CancelUpgrade != nil:
batch.Queue(queries.ConsensusProposalSubmissionCancelInsert,
submission.ID,
submission.Submitter.String(),
Expand All @@ -915,6 +916,19 @@ func (m *processor) queueSubmissions(batch *storage.QueryBatch, data *governance
submission.CreatedAt,
submission.ClosesAt,
)
case submission.Content.ChangeParameters != nil:
batch.Queue(queries.ConsensusProposalSubmissionChangeParametersInsert,
submission.ID,
submission.Submitter.String(),
submission.State.String(),
submission.Deposit.String(),
submission.Content.ChangeParameters.Module,
[]byte(submission.Content.ChangeParameters.Changes),
submission.CreatedAt,
submission.ClosesAt,
)
default:
m.logger.Warn("unknown proposal content type", "proposal_id", submission.ID, "content", submission.Content)
}
}

Expand Down
35 changes: 28 additions & 7 deletions analyzer/consensus/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package consensus

import (
"encoding/hex"
"encoding/json"
"fmt"
"sort"
Expand Down Expand Up @@ -206,7 +207,7 @@ VALUES

// There might be no runtime_nodes to insert; create a query only if there are.
if queryRt != "" {
queryRt = `INSERT INTO chain.runtime_nodes(runtime_id, node_id)
queryRt = `INSERT INTO chain.runtime_nodes(runtime_id, node_id)
VALUES
` + queryRt + ";"
queries = append(queries, queryRt)
Expand Down Expand Up @@ -472,14 +473,15 @@ func (mg *GenesisProcessor) addGovernanceBackendMigrations(document *genesis.Doc

if len(document.Governance.Proposals) > 0 {
// TODO: Extract `executed` for proposal.
query := `INSERT INTO chain.proposals (id, submitter, state, deposit, handler, cp_target_version, rhp_target_version, rcp_target_version, upgrade_epoch, cancels, created_at, closes_at, invalid_votes)
query := `INSERT INTO chain.proposals (id, submitter, state, deposit, handler, cp_target_version, rhp_target_version, rcp_target_version, upgrade_epoch, cancels, parameters_change_module, parameters_change, created_at, closes_at, invalid_votes)
VALUES
`

for i, proposal := range document.Governance.Proposals {
if proposal.Content.Upgrade != nil {
switch {
case proposal.Content.Upgrade != nil:
query += fmt.Sprintf(
"\t(%d, '%s', '%s', %d, '%s', '%s', '%s', '%s', %d, %s, %d, %d, %d)",
"\t(%d, '%s', '%s', %d, '%s', '%s', '%s', '%s', %d, NULL, NULL, NULL, %d, %d, %d)",
proposal.ID,
proposal.Submitter.String(),
proposal.State.String(),
Expand All @@ -489,24 +491,43 @@ VALUES
proposal.Content.Upgrade.Target.RuntimeHostProtocol.String(),
proposal.Content.Upgrade.Target.RuntimeCommitteeProtocol.String(),
proposal.Content.Upgrade.Epoch,
"null",
// 1 hardcoded NULL for the proposal.Content.CancelUpgrade.ProposalID field.
// 2 hardcoded NULLs for the proposal.Content.ChangeParameters fields.
proposal.CreatedAt,
proposal.ClosesAt,
proposal.InvalidVotes,
)
} else if proposal.Content.CancelUpgrade != nil {
case proposal.Content.CancelUpgrade != nil:
query += fmt.Sprintf(
"\t(%d, '%s', '%s', %d, NULL, NULL, NULL, NULL, NULL, %d, %d, %d, %d)",
"\t(%d, '%s', '%s', %d, NULL, NULL, NULL, NULL, NULL, %d, NULL, NULL, %d, %d, %d)",
proposal.ID,
proposal.Submitter.String(),
proposal.State.String(),
proposal.Deposit.ToBigInt(),
// 5 hardcoded NULLs for the proposal.Content.Upgrade fields.
proposal.Content.CancelUpgrade.ProposalID,
// 2 hardcoded NULLs for the proposal.Content.ChangeParameters fields.
proposal.CreatedAt,
proposal.ClosesAt,
proposal.InvalidVotes,
)
case proposal.Content.ChangeParameters != nil:
query += fmt.Sprintf(
"\t(%d, '%s', '%s', %d, NULL, NULL, NULL, NULL, NULL, NULL, '%s', decode('%s', 'hex'), %d, %d, %d)",
proposal.ID,
proposal.Submitter.String(),
proposal.State.String(),
proposal.Deposit.ToBigInt(),
// 5 hardcoded NULLs for the proposal.Content.Upgrade fields.
// 1 hardocded NULL for the proposal.Content.CancelUpgrade.ProposalID field.
proposal.Content.ChangeParameters.Module,
hex.EncodeToString(proposal.Content.ChangeParameters.Changes),
proposal.CreatedAt,
proposal.ClosesAt,
proposal.InvalidVotes,
)
default:
mg.logger.Warn("unknown proposal content type", "proposal_id", proposal.ID, "content", proposal.Content)
}

if i != len(document.Governance.Proposals)-1 {
Expand Down
14 changes: 9 additions & 5 deletions analyzer/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ var (
height >= $2 AND ($3 <= 0 OR height <= $3) AND
processed_time IS NOT NULL
)
SELECT
SELECT
-- Whether the processed subrange is a contiguous range that starts at the input range.
COALESCE(
(COUNT(*) = MAX(height) - MIN(height) + 1) AND MIN(height) = $2,
Expand Down Expand Up @@ -162,9 +162,9 @@ var (
INSERT INTO chain.latest_node_heights (layer, height)
VALUES
($1, $2)
ON CONFLICT (layer)
DO UPDATE
SET
ON CONFLICT (layer)
DO UPDATE
SET
height = excluded.height`

ConsensusBlockInsert = `
Expand Down Expand Up @@ -396,6 +396,10 @@ var (
INSERT INTO chain.proposals (id, submitter, state, deposit, cancels, created_at, closes_at)
VALUES ($1, $2, $3, $4, $5, $6, $7)`

ConsensusProposalSubmissionChangeParametersInsert = `
INSERT INTO chain.proposals (id, submitter, state, deposit, parameters_change_module, parameters_change, created_at, closes_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`

ConsensusProposalExecutionsUpdate = `
UPDATE chain.proposals
SET executed = true
Expand Down
21 changes: 16 additions & 5 deletions api/spec/v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1241,7 +1241,7 @@ components:
format: int64
description: |
The height of the most recent indexed block. Compare with latest_node_block to measure
how far behind Nexus is from the chain.
how far behind Nexus is from the chain.
example: *block_height_1
latest_node_block:
type: integer
Expand Down Expand Up @@ -2040,6 +2040,17 @@ components:
description: |
The proposal to cancel, if this proposal proposes
cancelling an existing proposal.
parameters_change_module:
type: string
description: |
The name of the module whose parameters are to be changed
by this 'parameters_change' proposal.
parameters_change:
type: string
format: byte
description: |
The base64 encoded raw cbor representing the updated parameters
which are to be changed by this 'parameters_change' proposal.
created_at:
type: integer
format: int64
Expand Down Expand Up @@ -2577,7 +2588,7 @@ components:
type: string
enum:
- ERC20
- ERC721
- ERC721
# Desired support in the future, among others:
# - ERC1155
description: |
Expand Down Expand Up @@ -2647,8 +2658,8 @@ components:
is_verified:
type: boolean
description: |
Whether the contract has been successfully verified by Sourcify.
Additional information on verified contracts is available via
Whether the contract has been successfully verified by Sourcify.
Additional information on verified contracts is available via
the `/{runtime}/accounts/{address}` endpoint.
example: false

Expand Down Expand Up @@ -2721,7 +2732,7 @@ components:
type: integer
format: uint64
example: 4184

TxVolumeList:
type: object
required: [window_size_seconds, windows]
Expand Down
4 changes: 4 additions & 0 deletions storage/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,8 @@ func (c *StorageClient) Proposals(ctx context.Context, p apiTypes.GetConsensusPr
&p.Target.RuntimeCommitteeProtocol,
&p.Epoch,
&p.Cancels,
&p.ParametersChangeModule,
&p.ParametersChange,
&p.CreatedAt,
&p.ClosesAt,
&invalidVotesNum,
Expand Down Expand Up @@ -979,6 +981,8 @@ func (c *StorageClient) Proposal(ctx context.Context, proposalID uint64) (*Propo
&p.Target.RuntimeCommitteeProtocol,
&p.Epoch,
&p.Cancels,
&p.ParametersChangeModule,
&p.ParametersChange,
&p.CreatedAt,
&p.ClosesAt,
&p.InvalidVotes,
Expand Down
8 changes: 4 additions & 4 deletions storage/client/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (
LIMIT 1`

NodeHeight = `
SELECT height
SELECT height
FROM chain.latest_node_heights
WHERE layer=$1`

Expand Down Expand Up @@ -230,7 +230,7 @@ const (

Proposals = `
SELECT id, submitter, state, deposit, handler, cp_target_version, rhp_target_version, rcp_target_version,
upgrade_epoch, cancels, created_at, closes_at, invalid_votes
upgrade_epoch, cancels, parameters_change_module, parameters_change, created_at, closes_at, invalid_votes
FROM chain.proposals
WHERE ($1::text IS NULL OR submitter = $1::text) AND
($2::text IS NULL OR state = $2::text)
Expand All @@ -240,7 +240,7 @@ const (

Proposal = `
SELECT id, submitter, state, deposit, handler, cp_target_version, rhp_target_version, rcp_target_version,
upgrade_epoch, cancels, created_at, closes_at, invalid_votes
upgrade_epoch, cancels, parameters_change_module, parameters_change, created_at, closes_at, invalid_votes
FROM chain.proposals
WHERE id = $1::bigint`

Expand Down Expand Up @@ -472,7 +472,7 @@ const (
(tokens.runtime = $1) AND
($2::oasis_addr IS NULL OR tokens.token_address = $2::oasis_addr) AND
($3::text IS NULL OR tokens.token_name ILIKE '%' || $3 || '%' OR tokens.symbol ILIKE '%' || $3 || '%') AND
tokens.token_type IS NOT NULL AND -- exclude token _candidates_ that we haven't inspected yet
tokens.token_type IS NOT NULL AND -- exclude token _candidates_ that we haven't inspected yet
tokens.token_type != 0 -- exclude unknown-type tokens; they're often just contracts that emitted Transfer events but don't expose the token ticker, name, balance etc.
ORDER BY num_holders DESC, contract_addr
LIMIT $4::bigint
Expand Down
5 changes: 5 additions & 0 deletions storage/migrations/01_consensus.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ CREATE TABLE chain.proposals
-- If this proposal cancels an existing proposal.
cancels UINT63 REFERENCES chain.proposals(id) DEFAULT NULL,

-- Added in 25_consensus_parameters_change_proposals.up.sql
-- If this proposal is a "ChangeParameters" proposal.
-- parameters_change_module TEXT,
-- parameters_change BYTEA,

created_at UINT63 NOT NULL, -- EpochTime, i.e. number of epochs since base epoch
closes_at UINT63 NOT NULL, -- EpochTime, i.e. number of epochs since base epoch
invalid_votes UINT_NUMERIC NOT NULL DEFAULT 0 -- uint64 in go; because the value might conceivably be >2^63, we use UINT_NUMERIC over UINT63 here.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
BEGIN;

ALTER TABLE chain.proposals
ADD COLUMN parameters_change_module TEXT,
ADD COLUMN parameters_change BYTEA;

COMMIT;

0 comments on commit 246651f

Please sign in to comment.