diff --git a/database/gov.go b/database/gov.go index c04e2e64f1..f77f6e94be 100644 --- a/database/gov.go +++ b/database/gov.go @@ -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" @@ -20,32 +18,19 @@ import ( // SaveGovParams saves the given x/gov parameters inside the database func (db *Db) SaveGovParams(params *types.GovParams) error { - - depositParamsBz, err := json.Marshal(¶ms.DepositParams) + paramsBz, err := json.Marshal(¶ms.Params) if err != nil { return fmt.Errorf("error while marshaling deposit params: %s", err) } - votingParamsBz, err := json.Marshal(¶ms.VotingParams) - if err != nil { - return fmt.Errorf("error while marshaling voting params: %s", err) - } - - tallyingParams, err := json.Marshal(¶ms.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) } @@ -53,41 +38,6 @@ WHERE gov_params.height <= excluded.height` return nil } -// SaveGenesisGovParams saves the genesis x/gov parameters inside the database -func (db *Db) SaveGenesisGovParams(params *types.GenesisGovParams) error { - - depositParamsBz, err := json.Marshal(¶ms.DepositParams) - if err != nil { - return fmt.Errorf("error while marshaling genesis deposit params: %s", err) - } - - votingParamsBz, err := json.Marshal(¶ms.VotingParams) - if err != nil { - return fmt.Errorf("error while marshaling genesis voting params: %s", err) - } - - tallyingParams, err := json.Marshal(¶ms.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 @@ -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), ¶ms) 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(¶ms, row.Height), nil } // -------------------------------------------------------------------------------------------------------------------- @@ -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{} @@ -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, @@ -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, diff --git a/database/gov_test.go b/database/gov_test.go index 14e0dedc98..1f80237136 100644 --- a/database/gov_test.go +++ b/database/gov_test.go @@ -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, @@ -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(), @@ -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) @@ -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) @@ -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`) @@ -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`) @@ -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`) diff --git a/database/schema/08-gov.sql b/database/schema/08-gov.sql index 3df6f75987..97967d9388 100644 --- a/database/schema/08-gov.sql +++ b/database/schema/08-gov.sql @@ -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) ); @@ -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, @@ -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, @@ -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) ); diff --git a/database/types/gov.go b/database/types/gov.go index c0670d15b6..0d48bd6f86 100644 --- a/database/types/gov.go +++ b/database/types/gov.go @@ -7,11 +7,9 @@ import ( // GovParamsRow represents a single row of the "gov_params" table type GovParamsRow struct { - OneRowID bool `db:"one_row_id"` - DepositParams string `db:"deposit_params"` - VotingParams string `db:"voting_params"` - TallyParams string `db:"tally_params"` - Height int64 `db:"height"` + OneRowID bool `db:"one_row_id"` + Params string `db:"params"` + Height int64 `db:"height"` } // -------------------------------------------------------------------------------------------------------------------- @@ -20,9 +18,8 @@ type GovParamsRow struct { type ProposalRow struct { Title string `db:"title"` Description string `db:"description"` + Metadata string `db:"metadata"` Content string `db:"content"` - ProposalRoute string `db:"proposal_route"` - ProposalType string `db:"proposal_type"` ProposalID uint64 `db:"id"` SubmitTime time.Time `db:"submit_time"` DepositEndTime time.Time `db:"deposit_end_time"` @@ -35,10 +32,9 @@ type ProposalRow struct { // NewProposalRow allows to easily create a new ProposalRow func NewProposalRow( proposalID uint64, - proposalRoute string, - proposalType string, title string, description string, + metadata string, content string, submitTime time.Time, depositEndTime time.Time, @@ -48,18 +44,17 @@ func NewProposalRow( status string, ) ProposalRow { return ProposalRow{ + ProposalID: proposalID, Title: title, Description: description, + Metadata: metadata, Content: content, - ProposalRoute: proposalRoute, - ProposalType: proposalType, - ProposalID: proposalID, + Status: status, SubmitTime: submitTime, DepositEndTime: depositEndTime, VotingStartTime: TimeToNullTime(votingStartTime), VotingEndTime: TimeToNullTime(votingEndTime), Proposer: proposer, - Status: status, } } @@ -67,8 +62,8 @@ func NewProposalRow( func (w ProposalRow) Equals(v ProposalRow) bool { return w.Title == v.Title && w.Description == v.Description && - w.ProposalRoute == v.ProposalRoute && - w.ProposalType == v.ProposalType && + w.Metadata == v.Metadata && + w.Content == v.Content && w.ProposalID == v.ProposalID && w.SubmitTime.Equal(v.SubmitTime) && w.DepositEndTime.Equal(v.DepositEndTime) && diff --git a/modules/gov/handle_genesis.go b/modules/gov/handle_genesis.go index 9ee4825065..8b52458fe3 100644 --- a/modules/gov/handle_genesis.go +++ b/modules/gov/handle_genesis.go @@ -9,7 +9,7 @@ import ( "github.com/forbole/bdjuno/v5/types" gov "github.com/cosmos/cosmos-sdk/x/gov/types" - govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" + govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" "github.com/rs/zerolog/log" ) @@ -31,12 +31,7 @@ func (m *Module) HandleGenesis(doc *tmtypes.GenesisDoc, appState map[string]json } // Save the params - err = m.db.SaveGenesisGovParams(types.NewGenesisGovParams( - types.NewGenesisVotingParams(&genStatev1beta1.VotingParams), - types.NewGenesisDepositParam(&genStatev1beta1.DepositParams), - types.NewGenesisTallyParams(&genStatev1beta1.TallyParams), - doc.InitialHeight, - )) + err = m.db.SaveGovParams(types.NewGovParams(genStatev1beta1.Params, doc.InitialHeight)) if err != nil { return fmt.Errorf("error while storing genesis governance params: %s", err) } @@ -53,29 +48,30 @@ func (m *Module) saveGenesisProposals(slice govtypesv1.Proposals, genDoc *tmtype for index, proposal := range slice { // Since it's not possible to get the proposer, set it to nil proposals[index] = types.NewProposal( - proposal.ProposalId, - proposal.ProposalRoute(), - proposal.ProposalType(), - proposal.GetContent(), + proposal.Id, + proposal.Title, + proposal.Summary, + proposal.Metadata, + proposal.Messages, proposal.Status.String(), - proposal.SubmitTime, - proposal.DepositEndTime, - &proposal.VotingStartTime, - &proposal.VotingEndTime, + *proposal.SubmitTime, + *proposal.DepositEndTime, + proposal.VotingStartTime, + proposal.VotingEndTime, "", ) tallyResults[index] = types.NewTallyResult( - proposal.ProposalId, - proposal.FinalTallyResult.Yes.String(), - proposal.FinalTallyResult.Abstain.String(), - proposal.FinalTallyResult.No.String(), - proposal.FinalTallyResult.NoWithVeto.String(), + proposal.Id, + proposal.FinalTallyResult.YesCount, + proposal.FinalTallyResult.AbstainCount, + proposal.FinalTallyResult.NoCount, + proposal.FinalTallyResult.NoWithVetoCount, genDoc.InitialHeight, ) deposits[index] = types.NewDeposit( - proposal.ProposalId, + proposal.Id, "", proposal.TotalDeposit, genDoc.GenesisTime, diff --git a/modules/gov/handle_msg.go b/modules/gov/handle_msg.go index fd94369ba1..09a44fe764 100644 --- a/modules/gov/handle_msg.go +++ b/modules/gov/handle_msg.go @@ -61,9 +61,10 @@ func (m *Module) handleMsgSubmitProposal(tx *juno.Tx, index int, msg *govtypesv1 // Store the proposal proposalObj := types.NewProposal( proposal.Id, - msg.GetContent().ProposalRoute(), - msg.GetContent().ProposalType(), - msg.GetContent(), + proposal.Title, + proposal.Summary, + proposal.Metadata, + msg.Messages, proposal.Status.String(), *proposal.SubmitTime, *proposal.DepositEndTime, diff --git a/modules/gov/source/local/source.go b/modules/gov/source/local/source.go index ab27028c68..f2ab28e751 100644 --- a/modules/gov/source/local/source.go +++ b/modules/gov/source/local/source.go @@ -73,8 +73,8 @@ func (s Source) TallyResult(height int64, proposalID uint64) (*govtypesv1.TallyR return res.Tally, nil } -// DepositParams implements govsource.Source -func (s Source) DepositParams(height int64) (*govtypesv1.DepositParams, error) { +// Params implements govsource.Source +func (s Source) Params(height int64) (*govtypesv1.Params, error) { ctx, err := s.LoadHeight(height) if err != nil { return nil, fmt.Errorf("error while loading height: %s", err) @@ -85,35 +85,5 @@ func (s Source) DepositParams(height int64) (*govtypesv1.DepositParams, error) { return nil, err } - return res.DepositParams, nil -} - -// VotingParams implements govsource.Source -func (s Source) VotingParams(height int64) (*govtypesv1.VotingParams, error) { - ctx, err := s.LoadHeight(height) - if err != nil { - return nil, fmt.Errorf("error while loading height: %s", err) - } - - res, err := s.queryClient.Params(sdk.WrapSDKContext(ctx), &govtypesv1.QueryParamsRequest{ParamsType: govtypesv1.ParamVoting}) - if err != nil { - return nil, err - } - - return res.VotingParams, nil -} - -// TallyParams implements govsource.Source -func (s Source) TallyParams(height int64) (*govtypesv1.TallyParams, error) { - ctx, err := s.LoadHeight(height) - if err != nil { - return nil, fmt.Errorf("error while loading height: %s", err) - } - - res, err := s.queryClient.Params(sdk.WrapSDKContext(ctx), &govtypesv1.QueryParamsRequest{ParamsType: govtypesv1.ParamTallying}) - if err != nil { - return nil, err - } - - return res.TallyParams, nil + return res.Params, nil } diff --git a/modules/gov/source/remote/source.go b/modules/gov/source/remote/source.go index 698967023f..3e179f4238 100644 --- a/modules/gov/source/remote/source.go +++ b/modules/gov/source/remote/source.go @@ -64,8 +64,8 @@ func (s Source) TallyResult(height int64, proposalID uint64) (*govtypesv1.TallyR return res.Tally, nil } -// DepositParams implements govsource.Source -func (s Source) DepositParams(height int64) (*govtypesv1.DepositParams, error) { +// Params implements govsource.Source +func (s Source) Params(height int64) (*govtypesv1.Params, error) { res, err := s.queryClient.Params( remote.GetHeightRequestContext(s.Ctx, height), &govtypesv1.QueryParamsRequest{ParamsType: govtypesv1.ParamDeposit}, @@ -74,31 +74,5 @@ func (s Source) DepositParams(height int64) (*govtypesv1.DepositParams, error) { return nil, err } - return res.DepositParams, nil -} - -// VotingParams implements govsource.Source -func (s Source) VotingParams(height int64) (*govtypesv1.VotingParams, error) { - res, err := s.queryClient.Params( - remote.GetHeightRequestContext(s.Ctx, height), - &govtypesv1.QueryParamsRequest{ParamsType: govtypesv1.ParamVoting}, - ) - if err != nil { - return nil, err - } - - return res.VotingParams, nil -} - -// TallyParams implements govsource.Source -func (s Source) TallyParams(height int64) (*govtypesv1.TallyParams, error) { - res, err := s.queryClient.Params( - remote.GetHeightRequestContext(s.Ctx, height), - &govtypesv1.QueryParamsRequest{ParamsType: govtypesv1.ParamTallying}, - ) - if err != nil { - return nil, err - } - - return res.TallyParams, nil + return res.Params, nil } diff --git a/modules/gov/source/source.go b/modules/gov/source/source.go index 37ea7ba576..b499e6d326 100644 --- a/modules/gov/source/source.go +++ b/modules/gov/source/source.go @@ -8,7 +8,5 @@ type Source interface { Proposal(height int64, id uint64) (*govtypesv1.Proposal, error) ProposalDeposit(height int64, id uint64, depositor string) (*govtypesv1.Deposit, error) TallyResult(height int64, proposalID uint64) (*govtypesv1.TallyResult, error) - DepositParams(height int64) (*govtypesv1.DepositParams, error) - VotingParams(height int64) (*govtypesv1.VotingParams, error) - TallyParams(height int64) (*govtypesv1.TallyParams, error) + Params(height int64) (*govtypesv1.Params, error) } diff --git a/modules/gov/utils_params.go b/modules/gov/utils_params.go index ef246e581a..037420ac40 100644 --- a/modules/gov/utils_params.go +++ b/modules/gov/utils_params.go @@ -13,25 +13,10 @@ func (m *Module) UpdateParams(height int64) error { log.Debug().Str("module", "gov").Int64("height", height). Msg("updating params") - depositParams, err := m.source.DepositParams(height) + params, err := m.source.Params(height) if err != nil { return fmt.Errorf("error while getting gov deposit params: %s", err) } - votingParams, err := m.source.VotingParams(height) - if err != nil { - return fmt.Errorf("error while getting gov voting params: %s", err) - } - - tallyParams, err := m.source.TallyParams(height) - if err != nil { - return fmt.Errorf("error while getting gov tally params: %s", err) - } - - return m.db.SaveGovParams(types.NewGovParams( - types.NewVotingParams(votingParams), - types.NewDepositParam(depositParams), - types.NewTallyParams(tallyParams), - height, - )) + return m.db.SaveGovParams(types.NewGovParams(params, height)) } diff --git a/modules/gov/utils_proposal.go b/modules/gov/utils_proposal.go index b8a7a13a4b..ef9a83194f 100644 --- a/modules/gov/utils_proposal.go +++ b/modules/gov/utils_proposal.go @@ -89,7 +89,7 @@ func (m *Module) updateDeletedProposalStatus(id uint64) error { return m.db.UpdateProposal( types.NewProposalUpdate( - stored.ProposalID, + stored.ID, types.ProposalStatusInvalid, stored.VotingStartTime, stored.VotingEndTime, diff --git a/types/gov.go b/types/gov.go index 04e9be8a27..a0ef2d83e0 100644 --- a/types/gov.go +++ b/types/gov.go @@ -3,125 +3,26 @@ package types import ( "time" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" - govtypesv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" - govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" - - "github.com/forbole/bdjuno/v5/modules/utils" ) const ( ProposalStatusInvalid = "PROPOSAL_STATUS_INVALID" ) -// DepositParams contains the data of the deposit parameters of the x/gov module -type DepositParams struct { - MinDeposit sdk.Coins `json:"min_deposit,omitempty" yaml:"min_deposit"` - MaxDepositPeriod int64 `json:"max_deposit_period,omitempty" yaml:"max_deposit_period"` -} - -// NewDepositParam allows to build a new DepositParams -func NewDepositParam(d *govtypesv1.DepositParams) DepositParams { - return DepositParams{ - MinDeposit: d.MinDeposit, - MaxDepositPeriod: d.MaxDepositPeriod.Nanoseconds(), - } -} - -// NewGenesisDepositParam allows to build a new DepositParams -func NewGenesisDepositParam(d *govtypesv1beta1.DepositParams) DepositParams { - return DepositParams{ - MinDeposit: d.MinDeposit, - MaxDepositPeriod: d.MaxDepositPeriod.Nanoseconds(), - } -} - -// VotingParams contains the voting parameters of the x/gov module -type VotingParams struct { - VotingPeriod int64 `json:"voting_period,omitempty" yaml:"voting_period"` -} - -// NewVotingParams allows to build a new VotingParams instance -func NewVotingParams(v *govtypesv1.VotingParams) VotingParams { - return VotingParams{ - VotingPeriod: v.VotingPeriod.Nanoseconds(), - } -} - -// NewGenesisVotingParams allows to build a new VotingParams instance -func NewGenesisVotingParams(v *govtypesv1beta1.VotingParams) VotingParams { - return VotingParams{ - VotingPeriod: v.VotingPeriod.Nanoseconds(), - } -} - // GovParams contains the data of the x/gov module parameters type GovParams struct { - DepositParams DepositParams `json:"deposit_params" yaml:"deposit_params"` - VotingParams VotingParams `json:"voting_params" yaml:"voting_params"` - TallyParams TallyParams `json:"tally_params" yaml:"tally_params"` - Height int64 `json:"height" ymal:"height"` + *govtypesv1.Params + Height int64 `json:"height" ymal:"height"` } -// GenesisGovParams contains the data of the x/gov module parameters -type GenesisGovParams struct { - DepositParams DepositParams `json:"deposit_params" yaml:"deposit_params"` - VotingParams VotingParams `json:"voting_params" yaml:"voting_params"` - TallyParams GenesisTallyParams `json:"tally_params" yaml:"tally_params"` - Height int64 `json:"height" ymal:"height"` -} - -// TallyParams contains the tally parameters of the x/gov module -type TallyParams struct { - Quorum string `json:"quorum,omitempty"` - Threshold string `json:"threshold,omitempty"` - VetoThreshold string `json:"veto_threshold,omitempty" yaml:"veto_threshold"` -} - -// GenesisTallyParams contains genesis tally parameters of the x/gov module -type GenesisTallyParams struct { - Quorum sdk.Dec `json:"quorum,omitempty"` - Threshold sdk.Dec `json:"threshold,omitempty"` - VetoThreshold sdk.Dec `json:"veto_threshold,omitempty" yaml:"veto_threshold"` -} - -// NewTallyParams allows to build a new TallyParams instance -func NewTallyParams(t *govtypesv1.TallyParams) TallyParams { - return TallyParams{ - Quorum: t.Quorum, - Threshold: t.Threshold, - VetoThreshold: t.VetoThreshold, - } -} - -// NewGenesisTallyParams allows to build a new GenesisTallyParams instance -func NewGenesisTallyParams(t *govtypesv1beta1.TallyParams) GenesisTallyParams { - return GenesisTallyParams{ - Quorum: t.Quorum, - Threshold: t.Threshold, - VetoThreshold: t.VetoThreshold, - } -} - -// NewGovParams allows to build a new GovParams instance -func NewGovParams(votingParams VotingParams, depositParams DepositParams, tallyParams TallyParams, height int64) *GovParams { +func NewGovParams(params *govtypesv1.Params, height int64) *GovParams { return &GovParams{ - DepositParams: depositParams, - VotingParams: votingParams, - TallyParams: tallyParams, - Height: height, - } -} - -// NewGenesisGovParams allows to build a new GenesisGovParams instance -func NewGenesisGovParams(votingParams VotingParams, depositParams DepositParams, tallyParams GenesisTallyParams, height int64) *GenesisGovParams { - return &GenesisGovParams{ - DepositParams: depositParams, - VotingParams: votingParams, - TallyParams: tallyParams, - Height: height, + Params: params, + Height: height, } } @@ -129,10 +30,11 @@ func NewGenesisGovParams(votingParams VotingParams, depositParams DepositParams, // Proposal represents a single governance proposal type Proposal struct { - ProposalRoute string - ProposalType string - ProposalID uint64 - Content govtypesv1beta1.Content + ID uint64 + Title string + Summary string + Metadata string + Messages []*codectypes.Any Status string SubmitTime time.Time DepositEndTime time.Time @@ -144,9 +46,10 @@ type Proposal struct { // NewProposal return a new Proposal instance func NewProposal( proposalID uint64, - proposalRoute string, - proposalType string, - content govtypesv1beta1.Content, + title string, + summary string, + metadata string, + messages []*codectypes.Any, status string, submitTime time.Time, depositEndTime time.Time, @@ -155,10 +58,11 @@ func NewProposal( proposer string, ) Proposal { return Proposal{ - Content: content, - ProposalRoute: proposalRoute, - ProposalType: proposalType, - ProposalID: proposalID, + ID: proposalID, + Title: title, + Summary: summary, + Metadata: metadata, + Messages: messages, Status: status, SubmitTime: submitTime, DepositEndTime: depositEndTime, @@ -168,20 +72,6 @@ func NewProposal( } } -// Equal tells whether p and other contain the same data -func (p Proposal) Equal(other Proposal) bool { - return p.ProposalRoute == other.ProposalRoute && - p.ProposalType == other.ProposalType && - p.ProposalID == other.ProposalID && - p.Content.String() == other.Content.String() && - p.Status == other.Status && - p.SubmitTime.Equal(other.SubmitTime) && - p.DepositEndTime.Equal(other.DepositEndTime) && - utils.AreTimesEqual(p.VotingStartTime, other.VotingStartTime) && - utils.AreTimesEqual(p.VotingEndTime, other.VotingEndTime) && - p.Proposer == other.Proposer -} - // ProposalUpdate contains the data that should be used when updating a governance proposal type ProposalUpdate struct { ProposalID uint64