Skip to content

Commit

Permalink
feat: support gov v1 proposals (#610)
Browse files Browse the repository at this point in the history
## Description

This PR aims at updating the codebase in order to properlty support
`x/gov` new `v1` proposal types and inner workings.

---

### Author Checklist

*All items are required. Please add a note to the item if the item is
not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type
prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json)
in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch
- [ ] provided a link to the relevant issue or specification
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go
code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable
and please add
your handle next to the items reviewed if you only reviewed selected
items.*

I have...

- [ ] confirmed the correct [type
prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json)
in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)

---------

Co-authored-by: Magic Cat <37407870+MonikaCat@users.noreply.github.com>
Co-authored-by: Magic Cat <pusz.monika@gmail.com>
  • Loading branch information
3 people committed Jun 29, 2023
1 parent a724b93 commit 67a1737
Show file tree
Hide file tree
Showing 25 changed files with 811 additions and 649 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ name: Lint
on:
pull_request:
push:
branches:
- chains/*
- cosmos/*

jobs:
GolangCI:
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ name: Tests
on:
pull_request:
push:
branches:
- chains/*
- cosmos/*

jobs:
Cleanup-runs:
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Unreleased
- ([\#610](https://github.com/forbole/bdjuno/pull/610)) Add support for gov `v1` proposals


## Version v4.0.0
## Notes
This version is thought to be used with Cosmos SDK `v0.47.x`.
Expand Down
3 changes: 1 addition & 2 deletions cmd/parse/gov/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
modulestypes "github.com/forbole/bdjuno/v4/modules/types"

govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govtypesv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
parsecmdtypes "github.com/forbole/juno/v5/cmd/parse/types"
"github.com/forbole/juno/v5/parser"
"github.com/forbole/juno/v5/types/config"
Expand Down Expand Up @@ -116,7 +115,7 @@ func refreshProposalDetails(parseCtx *parser.Context, proposalID uint64, govModu

// Handle the MsgSubmitProposal messages
for index, msg := range tx.GetMsgs() {
if _, ok := msg.(*govtypesv1beta1.MsgSubmitProposal); !ok {
if _, ok := msg.(*govtypesv1.MsgSubmitProposal); !ok {
continue
}

Expand Down
156 changes: 43 additions & 113 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)
return fmt.Errorf("error while marshalling gov 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)
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)
var params govtypesv1.Params
err = json.Unmarshal([]byte(row.Params), &params)
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 @@ -148,35 +83,27 @@ INSERT INTO proposal(
accounts = append(accounts, types.NewAccount(proposal.Proposer))

// Prepare the proposal query
vi := i * 12
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)
}
vi := i * 11
proposalsQuery += fmt.Sprintf("($%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)

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 marshalling proposal msg: %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,28 +143,31 @@ 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,
row.VotingStartTime,
row.VotingEndTime,
dbtypes.NullTimeToTime(row.VotingStartTime),
dbtypes.NullTimeToTime(row.VotingEndTime),
row.Proposer,
)
return proposal, nil
Expand Down
Loading

0 comments on commit 67a1737

Please sign in to comment.