From 2e476fd75772e087ac4c62fded838254de12646a Mon Sep 17 00:00:00 2001 From: SpaghettiOverload Date: Sun, 22 Oct 2023 12:04:41 +0300 Subject: [PATCH 1/2] fix: EOF on DB update deleted proposal from chain --- database/gov.go | 34 ++++++---------------------------- 1 file changed, 6 insertions(+), 28 deletions(-) diff --git a/database/gov.go b/database/gov.go index ffcefc3cc..660db8398 100644 --- a/database/gov.go +++ b/database/gov.go @@ -7,7 +7,6 @@ import ( "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" upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" "github.com/lib/pq" @@ -131,7 +130,9 @@ INSERT INTO proposal( // GetProposal returns the proposal with the given id, or nil if not found func (db *Db) GetProposal(id uint64) (types.Proposal, error) { + var proposal types.Proposal var rows []*dbtypes.ProposalRow + err := db.SQL.Select(&rows, `SELECT * FROM proposal WHERE id = $1`, id) if err != nil { return types.Proposal{}, err @@ -142,34 +143,11 @@ func (db *Db) GetProposal(id uint64) (types.Proposal, error) { } row := rows[0] + proposal.ID = row.ProposalID + proposal.Status = row.Status + proposal.VotingStartTime = dbtypes.NullTimeToTime(row.VotingStartTime) + proposal.VotingEndTime = dbtypes.NullTimeToTime(row.VotingEndTime) - trimContent := strings.TrimPrefix(row.Content, "{") - trimContent = strings.TrimPrefix(trimContent, "}") - jsonMessages := strings.Split(trimContent, ",") - - 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.Title, - row.Description, - row.Metadata, - messages, - row.Status, - row.SubmitTime, - row.DepositEndTime, - dbtypes.NullTimeToTime(row.VotingStartTime), - dbtypes.NullTimeToTime(row.VotingEndTime), - row.Proposer, - ) return proposal, nil } From baee78e1271973604f00c9b61d3663d081b0245a Mon Sep 17 00:00:00 2001 From: SpaghettiOverload Date: Mon, 23 Oct 2023 00:10:10 +0300 Subject: [PATCH 2/2] add: GetProposalForUpdate to separate concerns --- database/gov.go | 57 +++++++++++++++++++++++++++++++---- modules/gov/utils_proposal.go | 8 ++--- 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/database/gov.go b/database/gov.go index 660db8398..ee87aaa60 100644 --- a/database/gov.go +++ b/database/gov.go @@ -7,6 +7,7 @@ import ( "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" upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" "github.com/lib/pq" @@ -130,9 +131,7 @@ INSERT INTO proposal( // GetProposal returns the proposal with the given id, or nil if not found func (db *Db) GetProposal(id uint64) (types.Proposal, error) { - var proposal types.Proposal var rows []*dbtypes.ProposalRow - err := db.SQL.Select(&rows, `SELECT * FROM proposal WHERE id = $1`, id) if err != nil { return types.Proposal{}, err @@ -143,14 +142,60 @@ func (db *Db) GetProposal(id uint64) (types.Proposal, error) { } row := rows[0] - proposal.ID = row.ProposalID - proposal.Status = row.Status - proposal.VotingStartTime = dbtypes.NullTimeToTime(row.VotingStartTime) - proposal.VotingEndTime = dbtypes.NullTimeToTime(row.VotingEndTime) + trimContent := strings.TrimPrefix(row.Content, "{") + trimContent = strings.TrimPrefix(trimContent, "}") + jsonMessages := strings.Split(trimContent, ",") + + 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.Title, + row.Description, + row.Metadata, + messages, + row.Status, + row.SubmitTime, + row.DepositEndTime, + dbtypes.NullTimeToTime(row.VotingStartTime), + dbtypes.NullTimeToTime(row.VotingEndTime), + row.Proposer, + ) return proposal, nil } +// GetProposalForUpdate returns a proposal with data required by NewProposalUpdate type +func (db *Db) GetProposalForUpdate(id uint64) (types.ProposalUpdate, error) { + var proposalForUpdate types.ProposalUpdate + var rows []*dbtypes.ProposalRow + + err := db.SQL.Select(&rows, `SELECT status, voting_start_time, voting_end_time FROM proposal WHERE id = $1`, id) + if err != nil { + return types.ProposalUpdate{}, err + } + + if len(rows) == 0 { + return types.ProposalUpdate{}, nil + } + + row := rows[0] + proposalForUpdate.ProposalID = id + proposalForUpdate.Status = row.Status + proposalForUpdate.VotingStartTime = dbtypes.NullTimeToTime(row.VotingStartTime) + proposalForUpdate.VotingEndTime = dbtypes.NullTimeToTime(row.VotingEndTime) + + return proposalForUpdate, nil +} + // GetOpenProposalsIds returns all the ids of the proposals that are in deposit or voting period at the given block time func (db *Db) GetOpenProposalsIds(blockTime time.Time) ([]uint64, error) { var ids []uint64 diff --git a/modules/gov/utils_proposal.go b/modules/gov/utils_proposal.go index 5649ee621..3784ee08c 100644 --- a/modules/gov/utils_proposal.go +++ b/modules/gov/utils_proposal.go @@ -82,17 +82,17 @@ func (m *Module) UpdateProposalStakingPoolSnapshot(height int64, blockVals *tmct // updateDeletedProposalStatus updates the proposal having the given id by setting its status // to the one that represents a deleted proposal func (m *Module) updateDeletedProposalStatus(id uint64) error { - stored, err := m.db.GetProposal(id) + proposalForUpdate, err := m.db.GetProposalForUpdate(id) if err != nil { return err } return m.db.UpdateProposal( types.NewProposalUpdate( - stored.ID, + proposalForUpdate.ProposalID, types.ProposalStatusInvalid, - stored.VotingStartTime, - stored.VotingEndTime, + proposalForUpdate.VotingStartTime, + proposalForUpdate.VotingEndTime, ), ) }