2020-06-29 23:39:35 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2022-01-08 00:39:27 +00:00
|
|
|
"context"
|
2020-06-29 23:39:35 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-04-05 23:21:59 +00:00
|
|
|
errorsmod "cosmossdk.io/errors"
|
2022-01-08 00:39:27 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2020-06-29 23:39:35 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2022-01-08 00:39:27 +00:00
|
|
|
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
|
2020-06-29 23:39:35 +00:00
|
|
|
|
2024-05-01 03:17:24 +00:00
|
|
|
"github.com/0glabs/0g-chain/x/committee/types"
|
2020-06-29 23:39:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Note: QueryProposer is copied in from the gov module
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultPage = 1
|
|
|
|
defaultLimit = 30 // should be consistent with tendermint/tendermint/rpc/core/pipe.go:19
|
|
|
|
)
|
|
|
|
|
|
|
|
// Proposer contains metadata of a governance proposal used for querying a proposer.
|
|
|
|
type Proposer struct {
|
|
|
|
ProposalID uint64 `json:"proposal_id" yaml:"proposal_id"`
|
|
|
|
Proposer string `json:"proposer" yaml:"proposer"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewProposer returns a new Proposer given id and proposer
|
|
|
|
func NewProposer(proposalID uint64, proposer string) Proposer {
|
|
|
|
return Proposer{proposalID, proposer}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p Proposer) String() string {
|
|
|
|
return fmt.Sprintf("Proposal with ID %d was proposed by %s", p.ProposalID, p.Proposer)
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryProposer will query for a proposer of a governance proposal by ID.
|
2022-01-08 00:39:27 +00:00
|
|
|
func QueryProposer(cliCtx client.Context, proposalID uint64) (Proposer, error) {
|
2020-06-29 23:39:35 +00:00
|
|
|
events := []string{
|
|
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, types.TypeMsgSubmitProposal),
|
|
|
|
fmt.Sprintf("%s.%s='%s'", types.EventTypeProposalSubmit, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", proposalID))),
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: SearchTxs is used to facilitate the txs query which does not currently
|
|
|
|
// support configurable pagination.
|
2022-01-08 00:39:27 +00:00
|
|
|
searchResult, err := authtx.QueryTxsByEvents(cliCtx, events, defaultPage, defaultLimit, "")
|
2020-06-29 23:39:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return Proposer{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, info := range searchResult.Txs {
|
2022-01-08 00:39:27 +00:00
|
|
|
for _, msg := range info.GetTx().GetMsgs() {
|
2020-06-29 23:39:35 +00:00
|
|
|
// there should only be a single proposal under the given conditions
|
2022-01-08 00:39:27 +00:00
|
|
|
if subMsg, ok := msg.(*types.MsgSubmitProposal); ok {
|
|
|
|
return NewProposer(proposalID, subMsg.Proposer), nil
|
2020-06-29 23:39:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Proposer{}, fmt.Errorf("failed to find the proposer for proposalID %d", proposalID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryProposalByID returns a proposal from state if present or fallbacks to searching old blocks
|
2022-01-08 00:39:27 +00:00
|
|
|
func QueryProposalByID(cliCtx client.Context, cdc *codec.LegacyAmino, queryRoute string, proposalID uint64) (*types.Proposal, int64, error) {
|
2020-06-29 23:39:35 +00:00
|
|
|
bz, err := cdc.MarshalJSON(types.NewQueryProposalParams(proposalID))
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryProposal), bz)
|
|
|
|
|
|
|
|
if err == nil {
|
2022-01-08 00:39:27 +00:00
|
|
|
var proposal types.Proposal
|
2020-06-29 23:39:35 +00:00
|
|
|
cdc.MustUnmarshalJSON(res, &proposal)
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
return &proposal, height, nil
|
2020-06-29 23:39:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: !errors.Is(err, types.ErrUnknownProposal) does not work here
|
|
|
|
if err != nil && !strings.Contains(err.Error(), "proposal not found") {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, height, err = cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryNextProposalID), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var nextProposalID uint64
|
|
|
|
cdc.MustUnmarshalJSON(res, &nextProposalID)
|
|
|
|
|
|
|
|
if proposalID >= nextProposalID {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, 0, errorsmod.Wrapf(types.ErrUnknownProposal, "%d", proposalID)
|
2020-06-29 23:39:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
events := []string{
|
|
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, types.TypeMsgSubmitProposal),
|
|
|
|
fmt.Sprintf("%s.%s='%s'", types.EventTypeProposalSubmit, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", proposalID))),
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
searchResult, err := authtx.QueryTxsByEvents(cliCtx, events, defaultPage, defaultLimit, "")
|
2020-06-29 23:39:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, info := range searchResult.Txs {
|
2022-01-08 00:39:27 +00:00
|
|
|
for _, msg := range info.GetTx().GetMsgs() {
|
|
|
|
if subMsg, ok := msg.(*types.MsgSubmitProposal); ok {
|
2020-06-29 23:39:35 +00:00
|
|
|
deadline, err := calculateDeadline(cliCtx, cdc, queryRoute, subMsg.CommitteeID, info.Height)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
proposal, err := types.NewProposal(subMsg.GetPubProposal(), proposalID, subMsg.CommitteeID, deadline)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
return &proposal, height, nil
|
2020-06-29 23:39:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, 0, errorsmod.Wrapf(types.ErrUnknownProposal, "%d", proposalID)
|
2020-06-29 23:39:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// calculateDeadline returns the proposal deadline for a committee and block height
|
2022-01-08 00:39:27 +00:00
|
|
|
func calculateDeadline(cliCtx client.Context, cdc *codec.LegacyAmino, queryRoute string, committeeID uint64, blockHeight int64) (time.Time, error) {
|
2020-06-29 23:39:35 +00:00
|
|
|
var deadline time.Time
|
|
|
|
|
|
|
|
bz, err := cdc.MarshalJSON(types.NewQueryCommitteeParams(committeeID))
|
|
|
|
if err != nil {
|
|
|
|
return deadline, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryCommittee), bz)
|
|
|
|
if err != nil {
|
|
|
|
return deadline, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var committee types.Committee
|
|
|
|
err = cdc.UnmarshalJSON(res, &committee)
|
|
|
|
if err != nil {
|
|
|
|
return deadline, err
|
|
|
|
}
|
|
|
|
|
|
|
|
node, err := cliCtx.GetNode()
|
|
|
|
if err != nil {
|
|
|
|
return deadline, err
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
resultBlock, err := node.Block(context.Background(), &blockHeight)
|
2020-06-29 23:39:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return deadline, err
|
|
|
|
}
|
|
|
|
|
2021-06-07 16:08:03 +00:00
|
|
|
deadline = resultBlock.Block.Header.Time.Add(committee.GetProposalDuration())
|
2020-06-29 23:39:35 +00:00
|
|
|
return deadline, nil
|
|
|
|
}
|