Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: (x/gov) store an index of proposals that are in voting period #13576

Merged
merged 48 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
63c6057
(x/gov) feat: move parts of Proposal into separate storages
facundomedica Oct 18, 2022
d8219fc
fix some
facundomedica Oct 18, 2022
cb12f0e
fix others
facundomedica Oct 18, 2022
5514486
fix unit tests
facundomedica Oct 18, 2022
1d62ba4
fix unit tests
facundomedica Oct 18, 2022
4b97f7b
fix conflicts
facundomedica Oct 18, 2022
d9364d4
fix conflicts
facundomedica Oct 18, 2022
8e838f5
progress
facundomedica Oct 18, 2022
3fccb66
do not overwrite messages
facundomedica Oct 18, 2022
e2bef95
remove change
facundomedica Oct 18, 2022
82998ef
simplify methods
facundomedica Oct 19, 2022
da271d5
static data
facundomedica Oct 19, 2022
601c411
fix tests
facundomedica Oct 19, 2022
162353c
fix tests
facundomedica Oct 19, 2022
04e9308
fix tests
facundomedica Oct 19, 2022
d62d0c1
rollback some changes
facundomedica Oct 19, 2022
733f286
rollback some changes
facundomedica Oct 19, 2022
d7918b7
progress
facundomedica Oct 19, 2022
d9d339f
progress
facundomedica Oct 19, 2022
1f32225
progress
facundomedica Oct 19, 2022
b5b2ada
progress
facundomedica Oct 19, 2022
37af9c4
progress
facundomedica Oct 19, 2022
1a9a443
progress
facundomedica Oct 19, 2022
2edde57
add delete
facundomedica Oct 19, 2022
f4a128f
fix tests
facundomedica Oct 19, 2022
3f8936e
use SetProposalWithoutContents whenever possible
facundomedica Oct 19, 2022
46b087c
add migrations
facundomedica Oct 19, 2022
59dc8a7
fix godoc
facundomedica Oct 19, 2022
d7c8cd1
gofumpt
facundomedica Oct 19, 2022
78dbf6c
Merge branch 'main' into facu/gov-storage
facundomedica Oct 19, 2022
ece934d
add changelog
facundomedica Oct 19, 2022
c3af4cf
rolling back changes
facundomedica Oct 24, 2022
59434a7
rolling back changes
facundomedica Oct 24, 2022
2d07421
progress
facundomedica Oct 24, 2022
4ecb17c
progress
facundomedica Oct 24, 2022
c20b48f
progress
facundomedica Oct 24, 2022
dd38f3d
progress
facundomedica Oct 24, 2022
b6e24af
fix tests
facundomedica Oct 24, 2022
ecc8917
fix cl
facundomedica Oct 24, 2022
fc8b9d0
fix cl
facundomedica Oct 24, 2022
cd2ae66
fix
facundomedica Oct 24, 2022
ff62538
fix test error
facundomedica Oct 24, 2022
e23a08b
Merge branch 'main' into facu/gov-storage
facundomedica Oct 25, 2022
e42c114
add store key in readme
facundomedica Oct 26, 2022
97e54bd
add store key in readme
facundomedica Oct 26, 2022
0bda41c
Merge branch 'main' into facu/gov-storage
facundomedica Oct 26, 2022
8707966
Merge branch 'facu/gov-storage' of https://github.com/cosmos/cosmos-s…
facundomedica Oct 26, 2022
aa1ed8b
Merge branch 'main' into facu/gov-storage
facundomedica Oct 26, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
887 changes: 717 additions & 170 deletions api/cosmos/gov/v1/gov.pulsar.go

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions proto/cosmos/gov/v1/gov.proto
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ message Proposal {
string metadata = 10;
}

message ProposalMessages {
repeated google.protobuf.Any messages = 1;
}

// ProposalStatus enumerates the valid statuses of a proposal.
enum ProposalStatus {
// PROPOSAL_STATUS_UNSPECIFIED defines the default proposal status.
Expand Down
16 changes: 8 additions & 8 deletions x/gov/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ func EndBlocker(ctx sdk.Context, keeper *keeper.Keeper) {
// the handlers fails, no state mutation is written and the error
// message is logged.
cacheCtx, writeCache := ctx.CacheContext()
messages, err := proposal.GetMsgs()
if err == nil {
for idx, msg = range messages {
handler := keeper.Router().Handler(msg)
_, err = handler(cacheCtx, msg)
if err != nil {
break
}
messages := keeper.GetProposalMessages(ctx, proposal.GetId())
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed

var err error
for idx, msg = range messages {
handler := keeper.Router().Handler(msg)
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
_, err = handler(cacheCtx, msg)
if err != nil {
break
}
}

Expand Down
41 changes: 40 additions & 1 deletion x/gov/keeper/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
sdktx "github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/x/gov/types"
v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
)
Expand Down Expand Up @@ -72,12 +73,13 @@ func (keeper Keeper) SubmitProposal(ctx sdk.Context, messages []sdk.Msg, metadat
submitTime := ctx.BlockHeader().Time
depositPeriod := keeper.GetParams(ctx).MaxDepositPeriod

proposal, err := v1.NewProposal(messages, proposalID, metadata, submitTime, submitTime.Add(*depositPeriod))
proposal, err := v1.NewProposal(proposalID, metadata, submitTime, submitTime.Add(*depositPeriod))
if err != nil {
return v1.Proposal{}, err
}

keeper.SetProposal(ctx, proposal)
keeper.SetProposalMessages(ctx, proposalID, messages)
keeper.InsertInactiveProposalQueue(ctx, proposalID, *proposal.DepositEndTime)
keeper.SetProposalID(ctx, proposalID+1)

Expand Down Expand Up @@ -125,6 +127,43 @@ func (keeper Keeper) SetProposal(ctx sdk.Context, proposal v1.Proposal) {
store.Set(types.ProposalKey(proposal.Id), bz)
}

func (keeper Keeper) SetProposalMessages(ctx sdk.Context, proposalID uint64, messages []sdk.Msg) {
msgs, err := sdktx.SetMsgs(messages)
if err != nil {
panic(err)
}

bz, err := keeper.cdc.Marshal(&v1.ProposalMessages{Messages: msgs})
if err != nil {
panic(err)
}

store := ctx.KVStore(keeper.storeKey)
store.Set(types.ProposalMessagesKey(proposalID), bz)
}

func (keeper Keeper) GetProposalMessages(ctx sdk.Context, proposalID uint64) []sdk.Msg {
store := ctx.KVStore(keeper.storeKey)

bz := store.Get(types.ProposalMessagesKey(proposalID))
if bz == nil {
return nil
}

var propMsgs v1.ProposalMessages
err := keeper.cdc.Unmarshal(bz, &propMsgs)
if err != nil {
panic(err)
}

msgs, err := sdktx.GetMsgs(propMsgs.Messages, "sdk.Msg")
if err != nil {
panic(err)
}

return msgs
}

// DeleteProposal deletes a proposal from store.
// Panics if the proposal doesn't exist.
func (keeper Keeper) DeleteProposal(ctx sdk.Context, proposalID uint64) {
Expand Down
3 changes: 2 additions & 1 deletion x/gov/keeper/proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (suite *KeeperTestSuite) TestGetProposalsFiltered() {

for _, s := range status {
for i := 0; i < 50; i++ {
p, err := v1.NewProposal(TestProposal, proposalID, "", time.Now(), time.Now())
p, err := v1.NewProposal(proposalID, "", time.Now(), time.Now())
suite.Require().NoError(err)

p.Status = s
Expand All @@ -107,6 +107,7 @@ func (suite *KeeperTestSuite) TestGetProposalsFiltered() {
}

suite.govKeeper.SetProposal(suite.ctx, p)
suite.govKeeper.SetProposalMessages(suite.ctx, proposalID, TestProposal)
proposalID++
}
}
Expand Down
5 changes: 5 additions & 0 deletions x/gov/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var (
ActiveProposalQueuePrefix = []byte{0x01}
InactiveProposalQueuePrefix = []byte{0x02}
ProposalIDKey = []byte{0x03}
ProposalMessagesKeyPrefix = []byte{0x04}

DepositsKeyPrefix = []byte{0x10}

Expand Down Expand Up @@ -69,6 +70,10 @@ func ProposalKey(proposalID uint64) []byte {
return append(ProposalsKeyPrefix, GetProposalIDBytes(proposalID)...)
}

func ProposalMessagesKey(proposalID uint64) []byte {
return append(ProposalMessagesKeyPrefix, GetProposalIDBytes(proposalID)...)
}

// ActiveProposalByTimeKey gets the active proposal queue key by endTime
func ActiveProposalByTimeKey(endTime time.Time) []byte {
return append(ActiveProposalQueuePrefix, sdk.FormatTimeBytes(endTime)...)
Expand Down
346 changes: 264 additions & 82 deletions x/gov/types/v1/gov.pb.go

Large diffs are not rendered by default.

14 changes: 5 additions & 9 deletions x/gov/types/v1/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,12 @@ const (
)

// NewProposal creates a new Proposal instance
func NewProposal(messages []sdk.Msg, id uint64, metadata string, submitTime, depositEndTime time.Time) (Proposal, error) {
msgs, err := sdktx.SetMsgs(messages)
if err != nil {
return Proposal{}, err
}

func NewProposal(id uint64, metadata string, submitTime, depositEndTime time.Time) (Proposal, error) {
tally := EmptyTallyResult()

p := Proposal{
Id: id,
Messages: msgs,
Id: id,
// Messages: msgs, // TODO: Remove
Metadata: metadata,
Status: StatusDepositPeriod,
FinalTallyResult: &tally,
Expand All @@ -44,7 +39,8 @@ func NewProposal(messages []sdk.Msg, id uint64, metadata string, submitTime, dep
return p, nil
}

// GetMessages returns the proposal messages
// Deprecated: GetMessages returns the proposal messages
// Use keeper.GetProposalMessages to get a stored proposal's messages.
func (p Proposal) GetMsgs() ([]sdk.Msg, error) {
return sdktx.GetMsgs(p.Messages, "sdk.MsgProposal")
}
Expand Down