Skip to content

Commit

Permalink
feat: update governance params types
Browse files Browse the repository at this point in the history
  • Loading branch information
RiccardoM committed Jun 26, 2023
1 parent 28e6c90 commit 76737a3
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 382 deletions.
144 changes: 37 additions & 107 deletions database/gov.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package database
import (
"encoding/json"
"fmt"
"strings"
"time"

"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
"github.com/cosmos/gogoproto/proto"

govtypesv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
"github.com/lib/pq"

Expand All @@ -20,74 +18,26 @@ import (

// SaveGovParams saves the given x/gov parameters inside the database
func (db *Db) SaveGovParams(params *types.GovParams) error {

depositParamsBz, err := json.Marshal(&params.DepositParams)
paramsBz, err := json.Marshal(&params.Params)
if err != nil {
return fmt.Errorf("error while marshaling deposit params: %s", err)
}

votingParamsBz, err := json.Marshal(&params.VotingParams)
if err != nil {
return fmt.Errorf("error while marshaling voting params: %s", err)
}

tallyingParams, err := json.Marshal(&params.TallyParams)
if err != nil {
return fmt.Errorf("error while marshaling tally params: %s", err)
}

stmt := `
INSERT INTO gov_params(deposit_params, voting_params, tally_params, height)
VALUES ($1, $2, $3, $4)
INSERT INTO gov_params(params, height)
VALUES ($1, $2)
ON CONFLICT (one_row_id) DO UPDATE
SET deposit_params = excluded.deposit_params,
voting_params = excluded.voting_params,
tally_params = excluded.tally_params,
SET params = excluded.params,
height = excluded.height
WHERE gov_params.height <= excluded.height`
_, err = db.SQL.Exec(stmt, string(depositParamsBz), string(votingParamsBz), string(tallyingParams), params.Height)
_, err = db.SQL.Exec(stmt, string(paramsBz), params.Height)
if err != nil {
return fmt.Errorf("error while storing gov params: %s", err)
}

return nil
}

// SaveGenesisGovParams saves the genesis x/gov parameters inside the database
func (db *Db) SaveGenesisGovParams(params *types.GenesisGovParams) error {

depositParamsBz, err := json.Marshal(&params.DepositParams)
if err != nil {
return fmt.Errorf("error while marshaling genesis deposit params: %s", err)
}

votingParamsBz, err := json.Marshal(&params.VotingParams)
if err != nil {
return fmt.Errorf("error while marshaling genesis voting params: %s", err)
}

tallyingParams, err := json.Marshal(&params.TallyParams)
if err != nil {
return fmt.Errorf("error while marshaling genesis tally params: %s", err)
}

stmt := `
INSERT INTO gov_params(deposit_params, voting_params, tally_params, height)
VALUES ($1, $2, $3, $4)
ON CONFLICT (one_row_id) DO UPDATE
SET deposit_params = excluded.deposit_params,
voting_params = excluded.voting_params,
tally_params = excluded.tally_params,
height = excluded.height
WHERE gov_params.height <= excluded.height`
_, err = db.SQL.Exec(stmt, string(depositParamsBz), string(votingParamsBz), string(tallyingParams), params.Height)
if err != nil {
return fmt.Errorf("error while storing genesis gov params: %s", err)
}

return nil
}

// GetGovParams returns the most recent governance parameters
func (db *Db) GetGovParams() (*types.GovParams, error) {
var rows []dbtypes.GovParamsRow
Expand All @@ -102,28 +52,13 @@ func (db *Db) GetGovParams() (*types.GovParams, error) {

row := rows[0]

var depositParams types.DepositParams
err = json.Unmarshal([]byte(row.DepositParams), &depositParams)
var params govtypesv1.Params
err = json.Unmarshal([]byte(row.Params), &params)
if err != nil {
return nil, err
}

var votingParams types.VotingParams
err = json.Unmarshal([]byte(row.VotingParams), &votingParams)
if err != nil {
return nil, err
}

var tallyParams types.TallyParams
err = json.Unmarshal([]byte(row.TallyParams), &tallyParams)
if err != nil {
return nil, err
}

return types.NewGovParams(
votingParams, depositParams, tallyParams,
row.Height,
), nil
return types.NewGovParams(&params, row.Height), nil
}

// --------------------------------------------------------------------------------------------------------------------
Expand All @@ -138,7 +73,7 @@ func (db *Db) SaveProposals(proposals []types.Proposal) error {

proposalsQuery := `
INSERT INTO proposal(
id, title, description, content, proposer_address, proposal_route, proposal_type, status,
id, title, description, metadata, content, proposer_address, status,
submit_time, deposit_end_time, voting_start_time, voting_end_time
) VALUES`
var proposalsParams []interface{}
Expand All @@ -152,31 +87,23 @@ INSERT INTO proposal(
proposalsQuery += fmt.Sprintf("($%d,$%d,$%d,$%d,$%d,$%d,$%d,$%d,$%d,$%d,$%d,$%d),",
vi+1, vi+2, vi+3, vi+4, vi+5, vi+6, vi+7, vi+8, vi+9, vi+10, vi+11, vi+12)

// Encode the content properly
protoContent, ok := proposal.Content.(proto.Message)
if !ok {
return fmt.Errorf("invalid proposal content types: %T", proposal.Content)
}

anyContent, err := codectypes.NewAnyWithValue(protoContent)
if err != nil {
return fmt.Errorf("error while wrapping proposal proto content: %s", err)
}

var jsonMessages []string
var protoCodec codec.ProtoCodec
contentBz, err := protoCodec.MarshalJSON(anyContent)
if err != nil {
return fmt.Errorf("error while marshaling proposal content: %s", err)
for _, msg := range proposal.Messages {
contentBz, err := protoCodec.MarshalJSON(msg)
if err != nil {
return fmt.Errorf("error while marshaling proposal content: %s", err)
}
jsonMessages = append(jsonMessages, string(contentBz))
}

proposalsParams = append(proposalsParams,
proposal.ProposalID,
proposal.Content.GetTitle(),
proposal.Content.GetDescription(),
string(contentBz),
proposal.ID,
proposal.Title,
proposal.Summary,
proposal.Metadata,
fmt.Sprintf("[%s]", strings.Join(jsonMessages, ",")),
proposal.Proposer,
proposal.ProposalRoute,
proposal.ProposalType,
proposal.Status,
proposal.SubmitTime,
proposal.DepositEndTime,
Expand Down Expand Up @@ -216,23 +143,26 @@ func (db *Db) GetProposal(id uint64) (types.Proposal, error) {

row := rows[0]

var contentAny codectypes.Any
err = db.Cdc.UnmarshalJSON([]byte(row.Content), &contentAny)
if err != nil {
return types.Proposal{}, err
}
trimContent := strings.TrimPrefix(row.Content, "{")
trimContent = strings.TrimPrefix(trimContent, "}")
jsonMessages := strings.Split(trimContent, ",")

var content govtypesv1beta1.Content
err = db.Cdc.UnpackAny(&contentAny, &content)
if err != nil {
return types.Proposal{}, err
var messages []*codectypes.Any
for _, jsonMessage := range jsonMessages {
var msg codectypes.Any
err = db.Cdc.UnmarshalJSON([]byte(jsonMessage), &msg)
if err != nil {
return types.Proposal{}, err
}
messages = append(messages, &msg)
}

proposal := types.NewProposal(
row.ProposalID,
row.ProposalRoute,
row.ProposalType,
content,
row.Title,
row.Description,
row.Metadata,
messages,
row.Status,
row.SubmitTime,
row.DepositEndTime,
Expand Down
22 changes: 11 additions & 11 deletions database/gov_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func (suite *DbTestSuite) TestBigDipperDb_UpdateProposal() {
timestamp2 := time.Date(2020, 1, 1, 01, 00, 00, 000, time.UTC)

update := types.NewProposalUpdate(
proposal.ProposalID,
proposal.ID,
govtypesv1.StatusPassed.String(),
timestamp1,
timestamp2,
Expand All @@ -319,7 +319,7 @@ func (suite *DbTestSuite) TestBigDipperDb_UpdateProposal() {
suite.Require().NoError(err)

expected := dbtypes.NewProposalRow(
proposal.ProposalID,
proposal.ID,
proposal.ProposalRoute,
proposal.ProposalType,
proposal.Content.GetTitle(),
Expand Down Expand Up @@ -365,9 +365,9 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveDeposits() {
timestamp3 := time.Date(2020, 1, 1, 17, 00, 00, 000, time.UTC)

deposit := []types.Deposit{
types.NewDeposit(proposal.ProposalID, depositor.String(), amount, timestamp1, 10),
types.NewDeposit(proposal.ProposalID, depositor2.String(), amount2, timestamp2, 10),
types.NewDeposit(proposal.ProposalID, depositor3.String(), amount3, timestamp3, 10),
types.NewDeposit(proposal.ID, depositor.String(), amount, timestamp1, 10),
types.NewDeposit(proposal.ID, depositor2.String(), amount2, timestamp2, 10),
types.NewDeposit(proposal.ID, depositor3.String(), amount3, timestamp3, 10),
}

err := suite.database.SaveDeposits(deposit)
Expand All @@ -393,9 +393,9 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveDeposits() {
amount3 = sdk.NewCoins(sdk.NewCoin("desmos", sdk.NewInt(30)))

deposit = []types.Deposit{
types.NewDeposit(proposal.ProposalID, depositor.String(), amount, timestamp1, 9),
types.NewDeposit(proposal.ProposalID, depositor2.String(), amount2, timestamp2, 10),
types.NewDeposit(proposal.ProposalID, depositor3.String(), amount3, timestamp3, 11),
types.NewDeposit(proposal.ID, depositor.String(), amount, timestamp1, 9),
types.NewDeposit(proposal.ID, depositor2.String(), amount2, timestamp2, 10),
types.NewDeposit(proposal.ID, depositor3.String(), amount3, timestamp3, 11),
}

err = suite.database.SaveDeposits(deposit)
Expand Down Expand Up @@ -432,7 +432,7 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveVote() {
err := suite.database.SaveVote(vote)
suite.Require().NoError(err)

expected := dbtypes.NewVoteRow(int64(proposal.ProposalID), voter.String(), govtypesv1.OptionYes.String(), timestamp, 1)
expected := dbtypes.NewVoteRow(int64(proposal.ID), voter.String(), govtypesv1.OptionYes.String(), timestamp, 1)

var result []dbtypes.VoteRow
err = suite.database.SQL.Select(&result, `SELECT * FROM proposal_vote`)
Expand All @@ -456,7 +456,7 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveVote() {
err = suite.database.SaveVote(vote)
suite.Require().NoError(err)

expected = dbtypes.NewVoteRow(int64(proposal.ProposalID), voter.String(), govtypesv1.OptionAbstain.String(), timestamp, 1)
expected = dbtypes.NewVoteRow(int64(proposal.ID), voter.String(), govtypesv1.OptionAbstain.String(), timestamp, 1)

result = []dbtypes.VoteRow{}
err = suite.database.Sqlx.Select(&result, `SELECT * FROM proposal_vote`)
Expand All @@ -469,7 +469,7 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveVote() {
err = suite.database.SaveVote(vote)
suite.Require().NoError(err)

expected = dbtypes.NewVoteRow(int64(proposal.ProposalID), voter.String(), govtypesv1.OptionNoWithVeto.String(), timestamp, 2)
expected = dbtypes.NewVoteRow(int64(proposal.ID), voter.String(), govtypesv1.OptionNoWithVeto.String(), timestamp, 2)

result = []dbtypes.VoteRow{}
err = suite.database.Sqlx.Select(&result, `SELECT * FROM proposal_vote`)
Expand Down
21 changes: 9 additions & 12 deletions database/schema/08-gov.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
CREATE TABLE gov_params
(
one_row_id BOOLEAN NOT NULL DEFAULT TRUE PRIMARY KEY,
deposit_params JSONB NOT NULL,
voting_params JSONB NOT NULL,
tally_params JSONB NOT NULL,
height BIGINT NOT NULL,
one_row_id BOOLEAN NOT NULL DEFAULT TRUE PRIMARY KEY,
params JSONB NOT NULL,
height BIGINT NOT NULL,
CHECK (one_row_id)
);

Expand All @@ -13,9 +11,8 @@ CREATE TABLE proposal
id INTEGER NOT NULL PRIMARY KEY,
title TEXT NOT NULL,
description TEXT NOT NULL,
metadata TEXT NOT NULL,
content JSONB NOT NULL,
proposal_route TEXT NOT NULL,
proposal_type TEXT NOT NULL,
submit_time TIMESTAMP NOT NULL,
deposit_end_time TIMESTAMP,
voting_start_time TIMESTAMP,
Expand All @@ -28,7 +25,7 @@ CREATE INDEX proposal_proposer_address_index ON proposal (proposer_address);
CREATE TABLE proposal_deposit
(
proposal_id INTEGER NOT NULL REFERENCES proposal (id),
depositor_address TEXT REFERENCES account (address),
depositor_address TEXT REFERENCES account (address),
amount COIN[],
timestamp TIMESTAMP,
height BIGINT NOT NULL,
Expand All @@ -54,10 +51,10 @@ CREATE INDEX proposal_vote_height_index ON proposal_vote (height);
CREATE TABLE proposal_tally_result
(
proposal_id INTEGER REFERENCES proposal (id) PRIMARY KEY,
yes TEXT NOT NULL,
abstain TEXT NOT NULL,
no TEXT NOT NULL,
no_with_veto TEXT NOT NULL,
yes TEXT NOT NULL,
abstain TEXT NOT NULL,
no TEXT NOT NULL,
no_with_veto TEXT NOT NULL,
height BIGINT NOT NULL,
CONSTRAINT unique_tally_result UNIQUE (proposal_id)
);
Expand Down
Loading

0 comments on commit 76737a3

Please sign in to comment.