From 7630eb7de088f0cdd732a9fe6bfc878d751ca814 Mon Sep 17 00:00:00 2001 From: Luca Palla Date: Tue, 8 Jul 2025 17:59:55 +0200 Subject: [PATCH] handle cancel proposal msg --- database/gov.go | 40 +++++++++++++++++++++++++++++++++++++++ modules/gov/handle_msg.go | 33 ++++++++++++++++++++++++++++---- 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/database/gov.go b/database/gov.go index efa5dd87b..0f7f7a9cb 100644 --- a/database/gov.go +++ b/database/gov.go @@ -490,3 +490,43 @@ func (db *Db) TruncateSoftwareUpgradePlan(height int64) error { return nil } + +// DeleteProposalAndRelated delete a proposal and its related rows +func (db *Db) DeleteProposalAndRelated(proposalID uint64) error { + // Delete software upgrade plan + _, err := db.SQL.Exec(`DELETE FROM software_upgrade_plan WHERE proposal_id = $1`, proposalID) + if err != nil { + return fmt.Errorf("error while deleting software_upgrade_plan: %s", err) + } + // Delete proposal_validator_status_snapshot + _, err = db.SQL.Exec(`DELETE FROM proposal_validator_status_snapshot WHERE proposal_id = $1`, proposalID) + if err != nil { + return fmt.Errorf("error while deleting proposal_validator_status_snapshot: %s", err) + } + // Delete proposal_staking_pool_snapshot + _, err = db.SQL.Exec(`DELETE FROM proposal_staking_pool_snapshot WHERE proposal_id = $1`, proposalID) + if err != nil { + return fmt.Errorf("error while deleting proposal_staking_pool_snapshot: %s", err) + } + // Delete proposal_tally_result + _, err = db.SQL.Exec(`DELETE FROM proposal_tally_result WHERE proposal_id = $1`, proposalID) + if err != nil { + return fmt.Errorf("error while deleting proposal_tally_result: %s", err) + } + // Delete proposal_vote + _, err = db.SQL.Exec(`DELETE FROM proposal_vote WHERE proposal_id = $1`, proposalID) + if err != nil { + return fmt.Errorf("error while deleting proposal_vote: %s", err) + } + // Delete proposal_deposit + _, err = db.SQL.Exec(`DELETE FROM proposal_deposit WHERE proposal_id = $1`, proposalID) + if err != nil { + return fmt.Errorf("error while deleting proposal_deposit: %s", err) + } + // Delete la proposal + _, err = db.SQL.Exec(`DELETE FROM proposal WHERE id = $1`, proposalID) + if err != nil { + return fmt.Errorf("error while deleting proposal: %s", err) + } + return nil +} diff --git a/modules/gov/handle_msg.go b/modules/gov/handle_msg.go index c6e310062..09df71260 100644 --- a/modules/gov/handle_msg.go +++ b/modules/gov/handle_msg.go @@ -26,6 +26,7 @@ var msgFilter = map[string]bool{ "/cosmos.gov.v1.MsgDeposit": true, "/cosmos.gov.v1.MsgVote": true, "/cosmos.gov.v1.MsgVoteWeighted": true, + "/cosmos.gov.v1.MsgCancelProposal": true, "/cosmos.gov.v1beta1.MsgSubmitProposal": true, "/cosmos.gov.v1beta1.MsgDeposit": true, @@ -53,11 +54,11 @@ func (m *Module) HandleMsg(index int, msg juno.Message, tx *juno.Transaction) er return m.handleSubmitProposalEvent(tx, cosmosMsg.Proposer, eventutils.FindEventsByMsgIndex(sdk.StringifyEvents(tx.Events), index)) case "/cosmos.gov.v1beta1.MsgSubmitProposal": cosmosMsg := utils.UnpackMessage(m.cdc, msg.GetBytes(), &govtypesv1beta1.MsgSubmitProposal{}) - + // Legacy proposal have raw log filled, and no msg_index inside the events. - if (tx.RawLog != "" && len(tx.Logs) > 0) { + if tx.RawLog != "" && len(tx.Logs) > 0 { events := tx.Logs[index].Events - return m.handleSubmitProposalEvent(tx, cosmosMsg.Proposer, events); + return m.handleSubmitProposalEvent(tx, cosmosMsg.Proposer, events) } return m.handleSubmitProposalEvent(tx, cosmosMsg.Proposer, eventutils.FindEventsByMsgIndex(sdk.StringifyEvents(tx.Events), index)) @@ -81,8 +82,11 @@ func (m *Module) HandleMsg(index int, msg juno.Message, tx *juno.Transaction) er return m.handleVoteEvent(tx, cosmosMsg.Voter, eventutils.FindEventsByMsgIndex(sdk.StringifyEvents(tx.Events), index)) case "/cosmos.gov.v1beta1.MsgVoteWeighted": cosmosMsg := utils.UnpackMessage(m.cdc, msg.GetBytes(), &govtypesv1beta1.MsgVoteWeighted{}) - return m.handleVoteEvent(tx, cosmosMsg.Voter, eventutils.FindEventsByMsgIndex(sdk.StringifyEvents(tx.Events), index)) + + case "/cosmos.gov.v1.MsgCancelProposal": + cosmosMsg := utils.UnpackMessage(m.cdc, msg.GetBytes(), &govtypesv1.MsgCancelProposal{}) + return m.handleCancelProposalEvent(tx, cosmosMsg.ProposalId, eventutils.FindEventsByMsgIndex(sdk.StringifyEvents(tx.Events), index)) } return nil @@ -220,3 +224,24 @@ func (m *Module) handleVoteEvent(tx *juno.Transaction, voter string, events sdk. // update tally result for given proposal return m.UpdateProposalTallyResult(proposalID, int64(tx.Height)) } + +// handleCancelProposalEvent handle the removing of proposal and the index reset +func (m *Module) handleCancelProposalEvent(tx *juno.Transaction, proposalID uint64, _ sdk.StringEvents) error { + // Check if cancelProposal transaction had success + // We read this information from success' column in transactions' table + + var success bool + row := m.db.SQL.QueryRow(`SELECT success FROM transaction WHERE hash = $1`, tx.TxHash) + err := row.Scan(&success) + if err != nil { + return fmt.Errorf("error while checking transaction success: %s", err) + } + if success { + err := m.db.DeleteProposalAndRelated(proposalID) + + if err != nil { + return fmt.Errorf("error while deleting proposal and related data: %s", err) + } + } + return nil +}