From 3e85a7bd69775cdddcca60b0f02e278ad199e725 Mon Sep 17 00:00:00 2001 From: Sunny Aggarwal Date: Tue, 30 Oct 2018 17:01:41 -0700 Subject: [PATCH 01/16] switched gov proposals queue to use iterators --- PENDING.md | 1 + client/lcd/lcd_test.go | 12 ++-- types/utils.go | 8 +++ x/gov/endblocker_test.go | 141 +++++++++++++++++++++------------------ x/gov/handler.go | 72 ++++++-------------- x/gov/keeper.go | 118 +++++++++++--------------------- x/gov/keeper_keys.go | 42 +++++++++++- x/gov/keeper_test.go | 65 ++++++------------ x/gov/proposals.go | 28 ++++++-- 9 files changed, 234 insertions(+), 253 deletions(-) diff --git a/PENDING.md b/PENDING.md index 11c13ea75f00..232d28f63129 100644 --- a/PENDING.md +++ b/PENDING.md @@ -40,6 +40,7 @@ IMPROVEMENTS * Gaia CLI (`gaiacli`) * Gaia + - #2637 [x/gov] Switched inactive and active proposal queues to an iterator based queue * SDK - \#2573 [x/distribution] add accum invariance diff --git a/client/lcd/lcd_test.go b/client/lcd/lcd_test.go index d3140e5b8d71..5aebd0da0987 100644 --- a/client/lcd/lcd_test.go +++ b/client/lcd/lcd_test.go @@ -629,7 +629,7 @@ func TestSubmitProposal(t *testing.T) { require.Equal(t, uint32(0), resultTx.DeliverTx.Code) var proposalID int64 - cdc.UnmarshalBinaryBare(resultTx.DeliverTx.GetData(), &proposalID) + cdc.MustUnmarshalBinary(resultTx.DeliverTx.GetData(), &proposalID) // query proposal proposal := getProposal(t, port, proposalID) @@ -651,7 +651,7 @@ func TestDeposit(t *testing.T) { require.Equal(t, uint32(0), resultTx.DeliverTx.Code) var proposalID int64 - cdc.UnmarshalBinaryBare(resultTx.DeliverTx.GetData(), &proposalID) + cdc.MustUnmarshalBinary(resultTx.DeliverTx.GetData(), &proposalID) // query proposal proposal := getProposal(t, port, proposalID) @@ -685,7 +685,7 @@ func TestVote(t *testing.T) { require.Equal(t, uint32(0), resultTx.DeliverTx.Code) var proposalID int64 - cdc.UnmarshalBinaryBare(resultTx.DeliverTx.GetData(), &proposalID) + cdc.MustUnmarshalBinary(resultTx.DeliverTx.GetData(), &proposalID) // query proposal proposal := getProposal(t, port, proposalID) @@ -733,17 +733,17 @@ func TestProposalsQuery(t *testing.T) { // Addr1 proposes (and deposits) proposals #1 and #2 resultTx := doSubmitProposal(t, port, seeds[0], names[0], passwords[0], addrs[0], 5) var proposalID1 int64 - cdc.UnmarshalBinaryBare(resultTx.DeliverTx.GetData(), &proposalID1) + cdc.MustUnmarshalBinary(resultTx.DeliverTx.GetData(), &proposalID1) tests.WaitForHeight(resultTx.Height+1, port) resultTx = doSubmitProposal(t, port, seeds[0], names[0], passwords[0], addrs[0], 5) var proposalID2 int64 - cdc.UnmarshalBinaryBare(resultTx.DeliverTx.GetData(), &proposalID2) + cdc.MustUnmarshalBinary(resultTx.DeliverTx.GetData(), &proposalID2) tests.WaitForHeight(resultTx.Height+1, port) // Addr2 proposes (and deposits) proposals #3 resultTx = doSubmitProposal(t, port, seeds[1], names[1], passwords[1], addrs[1], 5) var proposalID3 int64 - cdc.UnmarshalBinaryBare(resultTx.DeliverTx.GetData(), &proposalID3) + cdc.MustUnmarshalBinary(resultTx.DeliverTx.GetData(), &proposalID3) tests.WaitForHeight(resultTx.Height+1, port) // Addr2 deposits on proposals #2 & #3 diff --git a/types/utils.go b/types/utils.go index 10ec854728ef..db6851f80d8f 100644 --- a/types/utils.go +++ b/types/utils.go @@ -1,6 +1,7 @@ package types import ( + "encoding/binary" "encoding/json" "time" @@ -36,6 +37,13 @@ func MustSortJSON(toSortJSON []byte) []byte { return js } +// marshals int64 to a bigendian byte slice so it can be sorted +func Int64ToSortableBytes(i int64) []byte { + b := make([]byte, 8) + binary.BigEndian.PutUint64(b, uint64(i)) + return b +} + // Slight modification of the RFC3339Nano but it right pads all zeros and drops the time zone info const SortableTimeFormat = "2006-01-02T15:04:05.000000000" diff --git a/x/gov/endblocker_test.go b/x/gov/endblocker_test.go index 27eff15f6443..9f7803264273 100644 --- a/x/gov/endblocker_test.go +++ b/x/gov/endblocker_test.go @@ -16,35 +16,40 @@ func TestTickExpiredDepositPeriod(t *testing.T) { ctx := mapp.BaseApp.NewContext(false, abci.Header{}) govHandler := NewHandler(keeper) - require.Nil(t, keeper.InactiveProposalQueuePeek(ctx)) - require.False(t, shouldPopInactiveProposalQueue(ctx, keeper)) + inactiveQueue := keeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + require.False(t, inactiveQueue.Valid()) + inactiveQueue.Close() newProposalMsg := NewMsgSubmitProposal("Test", "test", ProposalTypeText, addrs[0], sdk.Coins{sdk.NewInt64Coin("steak", 5)}) res := govHandler(ctx, newProposalMsg) require.True(t, res.IsOK()) - EndBlocker(ctx, keeper) - require.NotNil(t, keeper.InactiveProposalQueuePeek(ctx)) - require.False(t, shouldPopInactiveProposalQueue(ctx, keeper)) + inactiveQueue = keeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + require.False(t, inactiveQueue.Valid()) + inactiveQueue.Close() newHeader := ctx.BlockHeader() newHeader.Time = ctx.BlockHeader().Time.Add(time.Duration(1) * time.Second) ctx = ctx.WithBlockHeader(newHeader) - EndBlocker(ctx, keeper) - require.NotNil(t, keeper.InactiveProposalQueuePeek(ctx)) - require.False(t, shouldPopInactiveProposalQueue(ctx, keeper)) + inactiveQueue = keeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + require.False(t, inactiveQueue.Valid()) + inactiveQueue.Close() newHeader = ctx.BlockHeader() newHeader.Time = ctx.BlockHeader().Time.Add(keeper.GetDepositProcedure(ctx).MaxDepositPeriod) ctx = ctx.WithBlockHeader(newHeader) - require.NotNil(t, keeper.InactiveProposalQueuePeek(ctx)) - require.True(t, shouldPopInactiveProposalQueue(ctx, keeper)) + inactiveQueue = keeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + require.True(t, inactiveQueue.Valid()) + inactiveQueue.Close() + EndBlocker(ctx, keeper) - require.Nil(t, keeper.InactiveProposalQueuePeek(ctx)) - require.False(t, shouldPopInactiveProposalQueue(ctx, keeper)) + + inactiveQueue = keeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + require.False(t, inactiveQueue.Valid()) + inactiveQueue.Close() } func TestTickMultipleExpiredDepositPeriod(t *testing.T) { @@ -53,25 +58,26 @@ func TestTickMultipleExpiredDepositPeriod(t *testing.T) { ctx := mapp.BaseApp.NewContext(false, abci.Header{}) govHandler := NewHandler(keeper) - require.Nil(t, keeper.InactiveProposalQueuePeek(ctx)) - require.False(t, shouldPopInactiveProposalQueue(ctx, keeper)) + inactiveQueue := keeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + require.False(t, inactiveQueue.Valid()) + inactiveQueue.Close() newProposalMsg := NewMsgSubmitProposal("Test", "test", ProposalTypeText, addrs[0], sdk.Coins{sdk.NewInt64Coin("steak", 5)}) res := govHandler(ctx, newProposalMsg) require.True(t, res.IsOK()) - EndBlocker(ctx, keeper) - require.NotNil(t, keeper.InactiveProposalQueuePeek(ctx)) - require.False(t, shouldPopInactiveProposalQueue(ctx, keeper)) + inactiveQueue = keeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + require.False(t, inactiveQueue.Valid()) + inactiveQueue.Close() newHeader := ctx.BlockHeader() newHeader.Time = ctx.BlockHeader().Time.Add(time.Duration(2) * time.Second) ctx = ctx.WithBlockHeader(newHeader) - EndBlocker(ctx, keeper) - require.NotNil(t, keeper.InactiveProposalQueuePeek(ctx)) - require.False(t, shouldPopInactiveProposalQueue(ctx, keeper)) + inactiveQueue = keeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + require.False(t, inactiveQueue.Valid()) + inactiveQueue.Close() newProposalMsg2 := NewMsgSubmitProposal("Test2", "test2", ProposalTypeText, addrs[1], sdk.Coins{sdk.NewInt64Coin("steak", 5)}) res = govHandler(ctx, newProposalMsg2) @@ -81,21 +87,25 @@ func TestTickMultipleExpiredDepositPeriod(t *testing.T) { newHeader.Time = ctx.BlockHeader().Time.Add(keeper.GetDepositProcedure(ctx).MaxDepositPeriod).Add(time.Duration(-1) * time.Second) ctx = ctx.WithBlockHeader(newHeader) - require.NotNil(t, keeper.InactiveProposalQueuePeek(ctx)) - require.True(t, shouldPopInactiveProposalQueue(ctx, keeper)) + inactiveQueue = keeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + require.True(t, inactiveQueue.Valid()) + inactiveQueue.Close() EndBlocker(ctx, keeper) - require.NotNil(t, keeper.InactiveProposalQueuePeek(ctx)) - require.False(t, shouldPopInactiveProposalQueue(ctx, keeper)) + inactiveQueue = keeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + require.False(t, inactiveQueue.Valid()) + inactiveQueue.Close() newHeader = ctx.BlockHeader() newHeader.Time = ctx.BlockHeader().Time.Add(time.Duration(5) * time.Second) ctx = ctx.WithBlockHeader(newHeader) - require.NotNil(t, keeper.InactiveProposalQueuePeek(ctx)) - require.True(t, shouldPopInactiveProposalQueue(ctx, keeper)) + inactiveQueue = keeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + require.True(t, inactiveQueue.Valid()) + inactiveQueue.Close() EndBlocker(ctx, keeper) - require.Nil(t, keeper.InactiveProposalQueuePeek(ctx)) - require.False(t, shouldPopInactiveProposalQueue(ctx, keeper)) + inactiveQueue = keeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + require.False(t, inactiveQueue.Valid()) + inactiveQueue.Close() } func TestTickPassedDepositPeriod(t *testing.T) { @@ -104,45 +114,39 @@ func TestTickPassedDepositPeriod(t *testing.T) { ctx := mapp.BaseApp.NewContext(false, abci.Header{}) govHandler := NewHandler(keeper) - require.Nil(t, keeper.InactiveProposalQueuePeek(ctx)) - require.False(t, shouldPopInactiveProposalQueue(ctx, keeper)) - require.Nil(t, keeper.ActiveProposalQueuePeek(ctx)) - require.False(t, shouldPopActiveProposalQueue(ctx, keeper)) + inactiveQueue := keeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + require.False(t, inactiveQueue.Valid()) + inactiveQueue.Close() + activeQueue := keeper.ActiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + require.False(t, activeQueue.Valid()) + activeQueue.Close() newProposalMsg := NewMsgSubmitProposal("Test", "test", ProposalTypeText, addrs[0], sdk.Coins{sdk.NewInt64Coin("steak", 5)}) res := govHandler(ctx, newProposalMsg) require.True(t, res.IsOK()) var proposalID int64 - keeper.cdc.UnmarshalBinaryBare(res.Data, &proposalID) + keeper.cdc.MustUnmarshalBinary(res.Data, &proposalID) - EndBlocker(ctx, keeper) - require.NotNil(t, keeper.InactiveProposalQueuePeek(ctx)) - require.False(t, shouldPopInactiveProposalQueue(ctx, keeper)) + inactiveQueue = keeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + require.False(t, inactiveQueue.Valid()) + inactiveQueue.Close() newHeader := ctx.BlockHeader() newHeader.Time = ctx.BlockHeader().Time.Add(time.Duration(1) * time.Second) ctx = ctx.WithBlockHeader(newHeader) - EndBlocker(ctx, keeper) - require.NotNil(t, keeper.InactiveProposalQueuePeek(ctx)) - require.False(t, shouldPopInactiveProposalQueue(ctx, keeper)) + inactiveQueue = keeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + require.False(t, inactiveQueue.Valid()) + inactiveQueue.Close() newDepositMsg := NewMsgDeposit(addrs[1], proposalID, sdk.Coins{sdk.NewInt64Coin("steak", 5)}) res = govHandler(ctx, newDepositMsg) require.True(t, res.IsOK()) - require.NotNil(t, keeper.InactiveProposalQueuePeek(ctx)) - require.True(t, shouldPopInactiveProposalQueue(ctx, keeper)) - require.NotNil(t, keeper.ActiveProposalQueuePeek(ctx)) - - EndBlocker(ctx, keeper) - - require.Nil(t, keeper.InactiveProposalQueuePeek(ctx)) - require.False(t, shouldPopInactiveProposalQueue(ctx, keeper)) - require.NotNil(t, keeper.ActiveProposalQueuePeek(ctx)) - require.False(t, shouldPopActiveProposalQueue(ctx, keeper)) - + activeQueue = keeper.ActiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + require.False(t, activeQueue.Valid()) + activeQueue.Close() } func TestTickPassedVotingPeriod(t *testing.T) { @@ -152,17 +156,19 @@ func TestTickPassedVotingPeriod(t *testing.T) { ctx := mapp.BaseApp.NewContext(false, abci.Header{}) govHandler := NewHandler(keeper) - require.Nil(t, keeper.InactiveProposalQueuePeek(ctx)) - require.False(t, shouldPopInactiveProposalQueue(ctx, keeper)) - require.Nil(t, keeper.ActiveProposalQueuePeek(ctx)) - require.False(t, shouldPopActiveProposalQueue(ctx, keeper)) + inactiveQueue := keeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + require.False(t, inactiveQueue.Valid()) + inactiveQueue.Close() + activeQueue := keeper.ActiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + require.False(t, activeQueue.Valid()) + activeQueue.Close() newProposalMsg := NewMsgSubmitProposal("Test", "test", ProposalTypeText, addrs[0], sdk.Coins{sdk.NewInt64Coin("steak", 5)}) res := govHandler(ctx, newProposalMsg) require.True(t, res.IsOK()) var proposalID int64 - keeper.cdc.UnmarshalBinaryBare(res.Data, &proposalID) + keeper.cdc.MustUnmarshalBinary(res.Data, &proposalID) newHeader := ctx.BlockHeader() newHeader.Time = ctx.BlockHeader().Time.Add(time.Duration(1) * time.Second) @@ -172,24 +178,27 @@ func TestTickPassedVotingPeriod(t *testing.T) { res = govHandler(ctx, newDepositMsg) require.True(t, res.IsOK()) - EndBlocker(ctx, keeper) - newHeader = ctx.BlockHeader() - newHeader.Time = ctx.BlockHeader().Time.Add(keeper.GetDepositProcedure(ctx).MaxDepositPeriod).Add(keeper.GetDepositProcedure(ctx).MaxDepositPeriod) + newHeader.Time = ctx.BlockHeader().Time.Add(keeper.GetDepositProcedure(ctx).MaxDepositPeriod).Add(keeper.GetVotingProcedure(ctx).VotingPeriod) ctx = ctx.WithBlockHeader(newHeader) - require.True(t, shouldPopActiveProposalQueue(ctx, keeper)) + inactiveQueue = keeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + require.False(t, inactiveQueue.Valid()) + inactiveQueue.Close() + + activeQueue = keeper.ActiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + require.True(t, activeQueue.Valid()) + var activeProposalID int64 + keeper.cdc.UnmarshalBinary(activeQueue.Value(), &activeProposalID) + require.Equal(t, StatusVotingPeriod, keeper.GetProposal(ctx, activeProposalID).GetStatus()) depositsIterator := keeper.GetDeposits(ctx, proposalID) require.True(t, depositsIterator.Valid()) depositsIterator.Close() - require.Equal(t, StatusVotingPeriod, keeper.GetProposal(ctx, proposalID).GetStatus()) + activeQueue.Close() EndBlocker(ctx, keeper) - require.Nil(t, keeper.ActiveProposalQueuePeek(ctx)) - depositsIterator = keeper.GetDeposits(ctx, proposalID) - require.False(t, depositsIterator.Valid()) - depositsIterator.Close() - require.Equal(t, StatusRejected, keeper.GetProposal(ctx, proposalID).GetStatus()) - require.True(t, keeper.GetProposal(ctx, proposalID).GetTallyResult().Equals(EmptyTallyResult())) + activeQueue = keeper.ActiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + require.False(t, activeQueue.Valid()) + activeQueue.Close() } diff --git a/x/gov/handler.go b/x/gov/handler.go index dc1dbfb10e40..8274ee1986e5 100644 --- a/x/gov/handler.go +++ b/x/gov/handler.go @@ -33,7 +33,7 @@ func handleMsgSubmitProposal(ctx sdk.Context, keeper Keeper, msg MsgSubmitPropos return err.Result() } - proposalIDBytes := keeper.cdc.MustMarshalBinaryBare(proposal.GetProposalID()) + proposalIDBytes := keeper.cdc.MustMarshalBinaryLengthPrefixed(proposal.GetProposalID()) resTags := sdk.NewTags( tags.Action, tags.ActionSubmitProposal, @@ -102,40 +102,35 @@ func EndBlocker(ctx sdk.Context, keeper Keeper) (resTags sdk.Tags) { resTags = sdk.NewTags() - // Delete proposals that haven't met minDeposit - for shouldPopInactiveProposalQueue(ctx, keeper) { - inactiveProposal := keeper.InactiveProposalQueuePop(ctx) - if inactiveProposal.GetStatus() != StatusDepositPeriod { - continue - } + inactiveIterator := keeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + for ; inactiveIterator.Valid(); inactiveIterator.Next() { + var proposalID int64 + keeper.cdc.MustUnmarshalBinaryLengthPrefixed(inactiveIterator.Value(), &proposalID) + inactiveProposal := keeper.GetProposal(ctx, proposalID) + keeper.RefundDeposits(ctx, proposalID) + keeper.DeleteProposal(ctx, proposalID) - proposalIDBytes := keeper.cdc.MustMarshalBinaryBare(inactiveProposal.GetProposalID()) - keeper.DeleteProposal(ctx, inactiveProposal) resTags = resTags.AppendTag(tags.Action, tags.ActionProposalDropped) - resTags = resTags.AppendTag(tags.ProposalID, proposalIDBytes) + resTags = resTags.AppendTag(tags.ProposalID, []byte(string(proposalID))) logger.Info( - fmt.Sprintf("proposal %d (%s) didn't meet minimum deposit of %v steak (had only %v steak); deleted", + fmt.Sprintf("proposal %d (%s) didn't meet minimum deposit of %s (had only %s); deleted", inactiveProposal.GetProposalID(), inactiveProposal.GetTitle(), - keeper.GetDepositProcedure(ctx).MinDeposit.AmountOf("steak"), - inactiveProposal.GetTotalDeposit().AmountOf("steak"), + keeper.GetDepositProcedure(ctx).MinDeposit, + inactiveProposal.GetTotalDeposit(), ), ) } + inactiveIterator.Close() - // Check if earliest Active Proposal ended voting period yet - for shouldPopActiveProposalQueue(ctx, keeper) { - activeProposal := keeper.ActiveProposalQueuePop(ctx) - - proposalStartTime := activeProposal.GetVotingStartTime() - votingPeriod := keeper.GetVotingProcedure(ctx).VotingPeriod - if ctx.BlockHeader().Time.Before(proposalStartTime.Add(votingPeriod)) { - continue - } - + activeIterator := keeper.ActiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) + for ; activeIterator.Valid(); activeIterator.Next() { + var proposalID int64 + keeper.cdc.MustUnmarshalBinaryLengthPrefixed(activeIterator.Value(), &proposalID) + activeProposal := keeper.GetProposal(ctx, proposalID) passes, tallyResults := tally(ctx, keeper, activeProposal) - proposalIDBytes := keeper.cdc.MustMarshalBinaryBare(activeProposal.GetProposalID()) + var action []byte if passes { keeper.RefundDeposits(ctx, activeProposal.GetProposalID()) @@ -149,37 +144,14 @@ func EndBlocker(ctx sdk.Context, keeper Keeper) (resTags sdk.Tags) { activeProposal.SetTallyResult(tallyResults) keeper.SetProposal(ctx, activeProposal) + keeper.RemoveFromActiveProposalQueue(ctx, activeProposal.GetVotingEndTime(), activeProposal.GetProposalID()) + logger.Info(fmt.Sprintf("proposal %d (%s) tallied; passed: %v", activeProposal.GetProposalID(), activeProposal.GetTitle(), passes)) resTags = resTags.AppendTag(tags.Action, action) - resTags = resTags.AppendTag(tags.ProposalID, proposalIDBytes) + resTags = resTags.AppendTag(tags.ProposalID, []byte(string(proposalID))) } return resTags } -func shouldPopInactiveProposalQueue(ctx sdk.Context, keeper Keeper) bool { - depositProcedure := keeper.GetDepositProcedure(ctx) - peekProposal := keeper.InactiveProposalQueuePeek(ctx) - - if peekProposal == nil { - return false - } else if peekProposal.GetStatus() != StatusDepositPeriod { - return true - } else if !ctx.BlockHeader().Time.Before(peekProposal.GetSubmitTime().Add(depositProcedure.MaxDepositPeriod)) { - return true - } - return false -} - -func shouldPopActiveProposalQueue(ctx sdk.Context, keeper Keeper) bool { - votingProcedure := keeper.GetVotingProcedure(ctx) - peekProposal := keeper.ActiveProposalQueuePeek(ctx) - - if peekProposal == nil { - return false - } else if !ctx.BlockHeader().Time.Before(peekProposal.GetVotingStartTime().Add(votingProcedure.VotingPeriod)) { - return true - } - return false -} diff --git a/x/gov/keeper.go b/x/gov/keeper.go index a3e3cda2534c..ebe359161a73 100644 --- a/x/gov/keeper.go +++ b/x/gov/keeper.go @@ -1,6 +1,8 @@ package gov import ( + "time" + codec "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/bank" @@ -92,8 +94,12 @@ func (keeper Keeper) NewTextProposal(ctx sdk.Context, title string, description TotalDeposit: sdk.Coins{}, SubmitTime: ctx.BlockHeader().Time, } + + depositPeriod := keeper.GetDepositProcedure(ctx).MaxDepositPeriod + proposal.SetDepositEndTime(proposal.GetSubmitTime().Add(depositPeriod)) + keeper.SetProposal(ctx, proposal) - keeper.InactiveProposalQueuePush(ctx, proposal) + keeper.InsertInactiveProposalQueue(ctx, proposal.GetDepositEndTime(), proposalID) return proposal } @@ -119,9 +125,12 @@ func (keeper Keeper) SetProposal(ctx sdk.Context, proposal Proposal) { } // Implements sdk.AccountKeeper. -func (keeper Keeper) DeleteProposal(ctx sdk.Context, proposal Proposal) { +func (keeper Keeper) DeleteProposal(ctx sdk.Context, proposalID int64) { store := ctx.KVStore(keeper.storeKey) - store.Delete(KeyProposal(proposal.GetProposalID())) + proposal := keeper.GetProposal(ctx, proposalID) + keeper.RemoveFromInactiveProposalQueue(ctx, proposal.GetDepositEndTime(), proposalID) + keeper.RemoveFromActiveProposalQueue(ctx, proposal.GetVotingEndTime(), proposalID) + store.Delete(KeyProposal(proposalID)) } // Get Proposal from store by ProposalID @@ -216,9 +225,13 @@ func (keeper Keeper) peekCurrentProposalID(ctx sdk.Context) (proposalID int64, e func (keeper Keeper) activateVotingPeriod(ctx sdk.Context, proposal Proposal) { proposal.SetVotingStartTime(ctx.BlockHeader().Time) + votingPeriod := keeper.GetVotingProcedure(ctx).VotingPeriod + proposal.SetVotingEndTime(proposal.GetVotingStartTime().Add(votingPeriod)) proposal.SetStatus(StatusVotingPeriod) keeper.SetProposal(ctx, proposal) - keeper.ActiveProposalQueuePush(ctx, proposal) + + keeper.RemoveFromInactiveProposalQueue(ctx, proposal.GetDepositEndTime(), proposal.GetProposalID()) + keeper.InsertActiveProposalQueue(ctx, proposal.GetVotingEndTime(), proposal.GetProposalID()) } // ===================================================== @@ -426,93 +439,40 @@ func (keeper Keeper) DeleteDeposits(ctx sdk.Context, proposalID int64) { // ===================================================== // ProposalQueues -func (keeper Keeper) getActiveProposalQueue(ctx sdk.Context) ProposalQueue { +// Returns an iterator for all the proposals in the Active Queue that expire by endTime +func (keeper Keeper) ActiveProposalQueueIterator(ctx sdk.Context, endTime time.Time) sdk.Iterator { store := ctx.KVStore(keeper.storeKey) - bz := store.Get(KeyActiveProposalQueue) - if bz == nil { - return nil - } - - var proposalQueue ProposalQueue - keeper.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &proposalQueue) - - return proposalQueue + return store.Iterator(PrefixActiveProposalQueue, sdk.PrefixEndBytes(ActiveProposalQueueTimePrefix(endTime))) } -func (keeper Keeper) setActiveProposalQueue(ctx sdk.Context, proposalQueue ProposalQueue) { +// Inserts a ProposalID into the active proposal queue at endTime +func (keeper Keeper) InsertActiveProposalQueue(ctx sdk.Context, endTime time.Time, proposalID int64) { store := ctx.KVStore(keeper.storeKey) - bz := keeper.cdc.MustMarshalBinaryLengthPrefixed(proposalQueue) - store.Set(KeyActiveProposalQueue, bz) -} - -// Return the Proposal at the front of the ProposalQueue -func (keeper Keeper) ActiveProposalQueuePeek(ctx sdk.Context) Proposal { - proposalQueue := keeper.getActiveProposalQueue(ctx) - if len(proposalQueue) == 0 { - return nil - } - return keeper.GetProposal(ctx, proposalQueue[0]) -} - -// Remove and return a Proposal from the front of the ProposalQueue -func (keeper Keeper) ActiveProposalQueuePop(ctx sdk.Context) Proposal { - proposalQueue := keeper.getActiveProposalQueue(ctx) - if len(proposalQueue) == 0 { - return nil - } - frontElement, proposalQueue := proposalQueue[0], proposalQueue[1:] - keeper.setActiveProposalQueue(ctx, proposalQueue) - return keeper.GetProposal(ctx, frontElement) -} - -// Add a proposalID to the back of the ProposalQueue -func (keeper Keeper) ActiveProposalQueuePush(ctx sdk.Context, proposal Proposal) { - proposalQueue := append(keeper.getActiveProposalQueue(ctx), proposal.GetProposalID()) - keeper.setActiveProposalQueue(ctx, proposalQueue) + bz := keeper.cdc.MustMarshalBinaryLengthPrefixed(proposalID) + store.Set(ActiveProposalQueueProposalKey(endTime, proposalID), bz) } -func (keeper Keeper) getInactiveProposalQueue(ctx sdk.Context) ProposalQueue { +// removes a proposalID from the Active Proposal Queue +func (keeper Keeper) RemoveFromActiveProposalQueue(ctx sdk.Context, endTime time.Time, proposalID int64) { store := ctx.KVStore(keeper.storeKey) - bz := store.Get(KeyInactiveProposalQueue) - if bz == nil { - return nil - } - - var proposalQueue ProposalQueue - - keeper.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &proposalQueue) - - return proposalQueue + store.Set(ActiveProposalQueueProposalKey(endTime, proposalID), nil) } -func (keeper Keeper) setInactiveProposalQueue(ctx sdk.Context, proposalQueue ProposalQueue) { +// Returns an iterator for all the proposals in the Inactive Queue that expire by endTime +func (keeper Keeper) InactiveProposalQueueIterator(ctx sdk.Context, endTime time.Time) sdk.Iterator { store := ctx.KVStore(keeper.storeKey) - bz := keeper.cdc.MustMarshalBinaryLengthPrefixed(proposalQueue) - store.Set(KeyInactiveProposalQueue, bz) + return store.Iterator(PrefixInactiveProposalQueue, sdk.PrefixEndBytes(InactiveProposalQueueTimePrefix(endTime))) } -// Return the Proposal at the front of the ProposalQueue -func (keeper Keeper) InactiveProposalQueuePeek(ctx sdk.Context) Proposal { - proposalQueue := keeper.getInactiveProposalQueue(ctx) - if len(proposalQueue) == 0 { - return nil - } - return keeper.GetProposal(ctx, proposalQueue[0]) -} - -// Remove and return a Proposal from the front of the ProposalQueue -func (keeper Keeper) InactiveProposalQueuePop(ctx sdk.Context) Proposal { - proposalQueue := keeper.getInactiveProposalQueue(ctx) - if len(proposalQueue) == 0 { - return nil - } - frontElement, proposalQueue := proposalQueue[0], proposalQueue[1:] - keeper.setInactiveProposalQueue(ctx, proposalQueue) - return keeper.GetProposal(ctx, frontElement) +// Inserts a ProposalID into the inactive proposal queue at endTime +func (keeper Keeper) InsertInactiveProposalQueue(ctx sdk.Context, endTime time.Time, proposalID int64) { + store := ctx.KVStore(keeper.storeKey) + bz := keeper.cdc.MustMarshalBinaryLengthPrefixed(proposalID) + store.Set(InactiveProposalQueueProposalKey(endTime, proposalID), bz) } -// Add a proposalID to the back of the ProposalQueue -func (keeper Keeper) InactiveProposalQueuePush(ctx sdk.Context, proposal Proposal) { - proposalQueue := append(keeper.getInactiveProposalQueue(ctx), proposal.GetProposalID()) - keeper.setInactiveProposalQueue(ctx, proposalQueue) +// removes a proposalID from the Inactive Proposal Queue +func (keeper Keeper) RemoveFromInactiveProposalQueue(ctx sdk.Context, endTime time.Time, proposalID int64) { + store := ctx.KVStore(keeper.storeKey) + store.Set(InactiveProposalQueueProposalKey(endTime, proposalID), nil) } diff --git a/x/gov/keeper_keys.go b/x/gov/keeper_keys.go index 7b1bf43f2f77..57f38fdd6b29 100644 --- a/x/gov/keeper_keys.go +++ b/x/gov/keeper_keys.go @@ -1,7 +1,9 @@ package gov import ( + "bytes" "fmt" + "time" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -10,9 +12,9 @@ import ( // Key for getting a the next available proposalID from the store var ( - KeyNextProposalID = []byte("newProposalID") - KeyActiveProposalQueue = []byte("activeProposalQueue") - KeyInactiveProposalQueue = []byte("inactiveProposalQueue") + KeyNextProposalID = []byte("newProposalID") + PrefixActiveProposalQueue = []byte("activeProposalQueue") + PrefixInactiveProposalQueue = []byte("inactiveProposalQueue") ) // Key for getting a specific proposal from the store @@ -39,3 +41,37 @@ func KeyDepositsSubspace(proposalID int64) []byte { func KeyVotesSubspace(proposalID int64) []byte { return []byte(fmt.Sprintf("votes:%d:", proposalID)) } + +// Returns the key for a proposalID in the activeProposalQueue +func ActiveProposalQueueTimePrefix(endTime time.Time) []byte { + return bytes.Join([][]byte{ + PrefixActiveProposalQueue, + sdk.FormatTimeBytes(endTime), + }, []byte("/")) +} + +// Returns the key for a proposalID in the activeProposalQueue +func ActiveProposalQueueProposalKey(endTime time.Time, proposalID int64) []byte { + return bytes.Join([][]byte{ + PrefixActiveProposalQueue, + sdk.FormatTimeBytes(endTime), + sdk.Int64ToSortableBytes(proposalID), + }, []byte("/")) +} + +// Returns the key for a proposalID in the activeProposalQueue +func InactiveProposalQueueTimePrefix(endTime time.Time) []byte { + return bytes.Join([][]byte{ + PrefixInactiveProposalQueue, + sdk.FormatTimeBytes(endTime), + }, []byte("/")) +} + +// Returns the key for a proposalID in the activeProposalQueue +func InactiveProposalQueueProposalKey(endTime time.Time, proposalID int64) []byte { + return bytes.Join([][]byte{ + PrefixInactiveProposalQueue, + sdk.FormatTimeBytes(endTime), + sdk.Int64ToSortableBytes(proposalID), + }, []byte("/")) +} diff --git a/x/gov/keeper_test.go b/x/gov/keeper_test.go index 0152c50bbd4c..b3a7c0623298 100644 --- a/x/gov/keeper_test.go +++ b/x/gov/keeper_test.go @@ -47,12 +47,17 @@ func TestActivateVotingPeriod(t *testing.T) { proposal := keeper.NewTextProposal(ctx, "Test", "description", ProposalTypeText) require.True(t, proposal.GetVotingStartTime().Equal(time.Time{})) - require.Nil(t, keeper.ActiveProposalQueuePeek(ctx)) keeper.activateVotingPeriod(ctx, proposal) require.True(t, proposal.GetVotingStartTime().Equal(ctx.BlockHeader().Time)) - require.Equal(t, proposal.GetProposalID(), keeper.ActiveProposalQueuePeek(ctx).GetProposalID()) + + activeIterator := keeper.ActiveProposalQueueIterator(ctx, proposal.GetVotingEndTime()) + require.True(t, activeIterator.Valid()) + var proposalID int64 + keeper.cdc.UnmarshalBinary(activeIterator.Value(), &proposalID) + require.Equal(t, proposalID, proposal.GetProposalID()) + activeIterator.Close() } func TestDeposits(t *testing.T) { @@ -79,7 +84,6 @@ func TestDeposits(t *testing.T) { deposit, found := keeper.GetDeposit(ctx, proposalID, addrs[1]) require.False(t, found) require.True(t, keeper.GetProposal(ctx, proposalID).GetVotingStartTime().Equal(time.Time{})) - require.Nil(t, keeper.ActiveProposalQueuePeek(ctx)) // Check first deposit err, votingStarted := keeper.AddDeposit(ctx, proposalID, addrs[0], fourSteak) @@ -116,8 +120,6 @@ func TestDeposits(t *testing.T) { // Check that proposal moved to voting period require.True(t, keeper.GetProposal(ctx, proposalID).GetVotingStartTime().Equal(ctx.BlockHeader().Time)) - require.NotNil(t, keeper.ActiveProposalQueuePeek(ctx)) - require.Equal(t, proposalID, keeper.ActiveProposalQueuePeek(ctx).GetProposalID()) // Test deposit iterator depositsIterator := keeper.GetDeposits(ctx, proposalID) @@ -207,44 +209,21 @@ func TestProposalQueues(t *testing.T) { ctx := mapp.BaseApp.NewContext(false, abci.Header{}) mapp.InitChainer(ctx, abci.RequestInitChain{}) - require.Nil(t, keeper.InactiveProposalQueuePeek(ctx)) - require.Nil(t, keeper.ActiveProposalQueuePeek(ctx)) - // create test proposals proposal := keeper.NewTextProposal(ctx, "Test", "description", ProposalTypeText) - proposal2 := keeper.NewTextProposal(ctx, "Test2", "description", ProposalTypeText) - proposal3 := keeper.NewTextProposal(ctx, "Test3", "description", ProposalTypeText) - proposal4 := keeper.NewTextProposal(ctx, "Test4", "description", ProposalTypeText) - - // test pushing to inactive proposal queue - keeper.InactiveProposalQueuePush(ctx, proposal) - keeper.InactiveProposalQueuePush(ctx, proposal2) - keeper.InactiveProposalQueuePush(ctx, proposal3) - keeper.InactiveProposalQueuePush(ctx, proposal4) - - // test peeking and popping from inactive proposal queue - require.Equal(t, keeper.InactiveProposalQueuePeek(ctx).GetProposalID(), proposal.GetProposalID()) - require.Equal(t, keeper.InactiveProposalQueuePop(ctx).GetProposalID(), proposal.GetProposalID()) - require.Equal(t, keeper.InactiveProposalQueuePeek(ctx).GetProposalID(), proposal2.GetProposalID()) - require.Equal(t, keeper.InactiveProposalQueuePop(ctx).GetProposalID(), proposal2.GetProposalID()) - require.Equal(t, keeper.InactiveProposalQueuePeek(ctx).GetProposalID(), proposal3.GetProposalID()) - require.Equal(t, keeper.InactiveProposalQueuePop(ctx).GetProposalID(), proposal3.GetProposalID()) - require.Equal(t, keeper.InactiveProposalQueuePeek(ctx).GetProposalID(), proposal4.GetProposalID()) - require.Equal(t, keeper.InactiveProposalQueuePop(ctx).GetProposalID(), proposal4.GetProposalID()) - - // test pushing to active proposal queue - keeper.ActiveProposalQueuePush(ctx, proposal) - keeper.ActiveProposalQueuePush(ctx, proposal2) - keeper.ActiveProposalQueuePush(ctx, proposal3) - keeper.ActiveProposalQueuePush(ctx, proposal4) - - // test peeking and popping from active proposal queue - require.Equal(t, keeper.ActiveProposalQueuePeek(ctx).GetProposalID(), proposal.GetProposalID()) - require.Equal(t, keeper.ActiveProposalQueuePop(ctx).GetProposalID(), proposal.GetProposalID()) - require.Equal(t, keeper.ActiveProposalQueuePeek(ctx).GetProposalID(), proposal2.GetProposalID()) - require.Equal(t, keeper.ActiveProposalQueuePop(ctx).GetProposalID(), proposal2.GetProposalID()) - require.Equal(t, keeper.ActiveProposalQueuePeek(ctx).GetProposalID(), proposal3.GetProposalID()) - require.Equal(t, keeper.ActiveProposalQueuePop(ctx).GetProposalID(), proposal3.GetProposalID()) - require.Equal(t, keeper.ActiveProposalQueuePeek(ctx).GetProposalID(), proposal4.GetProposalID()) - require.Equal(t, keeper.ActiveProposalQueuePop(ctx).GetProposalID(), proposal4.GetProposalID()) + + inactiveIterator := keeper.InactiveProposalQueueIterator(ctx, proposal.GetDepositEndTime()) + require.True(t, inactiveIterator.Valid()) + var proposalID int64 + keeper.cdc.UnmarshalBinary(inactiveIterator.Value(), &proposalID) + require.Equal(t, proposalID, proposal.GetProposalID()) + inactiveIterator.Close() + + keeper.activateVotingPeriod(ctx, proposal) + + activeIterator := keeper.ActiveProposalQueueIterator(ctx, proposal.GetVotingEndTime()) + require.True(t, activeIterator.Valid()) + keeper.cdc.UnmarshalBinary(activeIterator.Value(), &proposalID) + require.Equal(t, proposalID, proposal.GetProposalID()) + activeIterator.Close() } diff --git a/x/gov/proposals.go b/x/gov/proposals.go index 9d1ba860adae..1d9d562a6b25 100644 --- a/x/gov/proposals.go +++ b/x/gov/proposals.go @@ -34,11 +34,17 @@ type Proposal interface { GetSubmitTime() time.Time SetSubmitTime(time.Time) + GetDepositEndTime() time.Time + SetDepositEndTime(time.Time) + GetTotalDeposit() sdk.Coins SetTotalDeposit(sdk.Coins) GetVotingStartTime() time.Time SetVotingStartTime(time.Time) + + GetVotingEndTime() time.Time + SetVotingEndTime(time.Time) } // checks if two proposals are equal @@ -68,10 +74,12 @@ type TextProposal struct { Status ProposalStatus `json:"proposal_status"` // Status of the Proposal {Pending, Active, Passed, Rejected} TallyResult TallyResult `json:"tally_result"` // Result of Tallys - SubmitTime time.Time `json:"submit_time"` // Height of the block where TxGovSubmitProposal was included - TotalDeposit sdk.Coins `json:"total_deposit"` // Current deposit on this proposal. Initial value is set at InitialDeposit + SubmitTime time.Time `json:"submit_time"` // Time of the block where TxGovSubmitProposal was included + DepositEndTime time.Time `json:"deposit_end_time"` // Time that the Proposal would expire if deposit amount isn't met + TotalDeposit sdk.Coins `json:"total_deposit"` // Current deposit on this proposal. Initial value is set at InitialDeposit - VotingStartTime time.Time `json:"voting_start_time"` // Height of the block where MinDeposit was reached. -1 if MinDeposit is not reached + VotingStartTime time.Time `json:"voting_start_time"` // Time of the block where MinDeposit was reached. -1 if MinDeposit is not reached + VotingEndTime time.Time `json:"voting_end_time"` // Time that the VotingPeriod for this proposal will end and votes will be tallied } // Implements Proposal Interface @@ -92,12 +100,20 @@ func (tp TextProposal) GetTallyResult() TallyResult { return tp.T func (tp *TextProposal) SetTallyResult(tallyResult TallyResult) { tp.TallyResult = tallyResult } func (tp TextProposal) GetSubmitTime() time.Time { return tp.SubmitTime } func (tp *TextProposal) SetSubmitTime(submitTime time.Time) { tp.SubmitTime = submitTime } -func (tp TextProposal) GetTotalDeposit() sdk.Coins { return tp.TotalDeposit } -func (tp *TextProposal) SetTotalDeposit(totalDeposit sdk.Coins) { tp.TotalDeposit = totalDeposit } -func (tp TextProposal) GetVotingStartTime() time.Time { return tp.VotingStartTime } +func (tp TextProposal) GetDepositEndTime() time.Time { return tp.DepositEndTime } +func (tp *TextProposal) SetDepositEndTime(depositEndTime time.Time) { + tp.DepositEndTime = depositEndTime +} +func (tp TextProposal) GetTotalDeposit() sdk.Coins { return tp.TotalDeposit } +func (tp *TextProposal) SetTotalDeposit(totalDeposit sdk.Coins) { tp.TotalDeposit = totalDeposit } +func (tp TextProposal) GetVotingStartTime() time.Time { return tp.VotingStartTime } func (tp *TextProposal) SetVotingStartTime(votingStartTime time.Time) { tp.VotingStartTime = votingStartTime } +func (tp TextProposal) GetVotingEndTime() time.Time { return tp.VotingEndTime } +func (tp *TextProposal) SetVotingEndTime(votingEndTime time.Time) { + tp.VotingEndTime = votingEndTime +} //----------------------------------------------------------- // ProposalQueue From d212b18c053d800cd890ac0985e7fc74f16dd041 Mon Sep 17 00:00:00 2001 From: Sunny Aggarwal Date: Tue, 30 Oct 2018 17:17:42 -0700 Subject: [PATCH 02/16] update gov spec --- docs/spec/governance/state.md | 45 +++++++++++------------------------ 1 file changed, 14 insertions(+), 31 deletions(-) diff --git a/docs/spec/governance/state.md b/docs/spec/governance/state.md index 5b577cec3918..6335ddad68cc 100644 --- a/docs/spec/governance/state.md +++ b/docs/spec/governance/state.md @@ -96,11 +96,13 @@ type Proposal struct { Type ProposalType // Type of proposal. Initial set {PlainTextProposal, SoftwareUpgradeProposal} TotalDeposit sdk.Coins // Current deposit on this proposal. Initial value is set at InitialDeposit Deposits []Deposit // List of deposits on the proposal - SubmitTime time.Time // Time of the block where TxGovSubmitProposal was included - Submitter sdk.Address // Address of the submitter + SubmitTime time.Time // Time of the block where TxGovSubmitProposal was included + DepositEndTime time.Time // Time that the DepositPeriod of a proposal would expire + Submitter sdk.Address // Address of the submitter VotingStartTime time.Time // Time of the block where MinDeposit was reached. time.Time{} if MinDeposit is not reached - CurrentStatus ProposalStatus // Current status of the proposal + VotingEndTime time.Time // Time of the block that the VotingPeriod for a proposal will end. + CurrentStatus ProposalStatus // Current status of the proposal YesVotes sdk.Dec NoVotes sdk.Dec @@ -134,47 +136,29 @@ For pseudocode purposes, here are the two function we will use to read or write **Store:** * `ProposalProcessingQueue`: A queue `queue[proposalID]` containing all the - `ProposalIDs` of proposals that reached `MinDeposit`. Each round, the oldest - element of `ProposalProcessingQueue` is checked during `BeginBlock` to see if - `CurrentTime == VotingStartTime + activeProcedure.VotingPeriod`. If it is, - then the application tallies the votes, compute the votes of each validator and checks if every validator in the valdiator set have voted - and, if not, applies `GovernancePenalty`. If the proposal is accepted, deposits are refunded. - After that proposal is ejected from `ProposalProcessingQueue` and the next element of the queue is evaluated. + `ProposalIDs` of proposals that reached `MinDeposit`. Each `EndBlock`, all the proposals + that have reached the end of their voting period are processed. + To process a finished proposal, the application tallies the votes, compute the votes of + each validator and checks if every validator in the valdiator set have voted. + If the proposal is accepted, deposits are refunded. And the pseudocode for the `ProposalProcessingQueue`: ```go in EndBlock do - - checkProposal() // First call of the recursive function - - - // Recursive function. First call in BeginBlock - func checkProposal() - proposalID = ProposalProcessingQueue.Peek() - if (proposalID == nil) - return - proposal = load(Governance, ) // proposal is a const key votingProcedure = load(GlobalParams, 'VotingProcedure') + + for finishedProposalID in GetAllFinishedProposalIDs(block.Time) + proposal = load(Governance, ) // proposal is a const key - if (CurrentTime == proposal.VotingStartTime + votingProcedure.VotingPeriod && proposal.CurrentStatus == ProposalStatusActive) - - // End of voting period, tally - - ProposalProcessingQueue.pop() - validators = - - - Keeper.getAllValidators() + validators = Keeper.getAllValidators() tmpValMap := map(sdk.Address)ValidatorGovInfo // Initiate mapping at 0. Validators that remain at 0 at the end of tally will be punished for each validator in validators tmpValMap(validator).Minus = 0 - - // Tally voterIterator = rangeQuery(Governance, ) //return all the addresses that voted on the proposal for each (voterAddress, vote) in voterIterator @@ -212,5 +196,4 @@ And the pseudocode for the `ProposalProcessingQueue`: proposal.CurrentStatus = ProposalStatusRejected store(Governance, , proposal) - checkProposal() ``` From 00c2534e6a613e8c1a9559daaa719b3f9b646e1e Mon Sep 17 00:00:00 2001 From: Sunny Aggarwal Date: Fri, 2 Nov 2018 18:58:11 -0700 Subject: [PATCH 03/16] address chris and bez comments --- types/utils.go | 4 ++-- x/gov/keeper_keys.go | 14 ++++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/types/utils.go b/types/utils.go index db6851f80d8f..289aef472f43 100644 --- a/types/utils.go +++ b/types/utils.go @@ -37,8 +37,8 @@ func MustSortJSON(toSortJSON []byte) []byte { return js } -// marshals int64 to a bigendian byte slice so it can be sorted -func Int64ToSortableBytes(i int64) []byte { +// Int64ToBigEndian - marshals int64 to a bigendian byte slice so it can be sorted +func Int64ToBigEndian(i int64) []byte { b := make([]byte, 8) binary.BigEndian.PutUint64(b, uint64(i)) return b diff --git a/x/gov/keeper_keys.go b/x/gov/keeper_keys.go index 57f38fdd6b29..b171961f99c0 100644 --- a/x/gov/keeper_keys.go +++ b/x/gov/keeper_keys.go @@ -12,6 +12,8 @@ import ( // Key for getting a the next available proposalID from the store var ( + KeyDelimiter = []byte("/") + KeyNextProposalID = []byte("newProposalID") PrefixActiveProposalQueue = []byte("activeProposalQueue") PrefixInactiveProposalQueue = []byte("inactiveProposalQueue") @@ -47,7 +49,7 @@ func ActiveProposalQueueTimePrefix(endTime time.Time) []byte { return bytes.Join([][]byte{ PrefixActiveProposalQueue, sdk.FormatTimeBytes(endTime), - }, []byte("/")) + }, KeyDelimiter) } // Returns the key for a proposalID in the activeProposalQueue @@ -55,8 +57,8 @@ func ActiveProposalQueueProposalKey(endTime time.Time, proposalID int64) []byte return bytes.Join([][]byte{ PrefixActiveProposalQueue, sdk.FormatTimeBytes(endTime), - sdk.Int64ToSortableBytes(proposalID), - }, []byte("/")) + sdk.Int64ToBigEndian(proposalID), + }, KeyDelimiter) } // Returns the key for a proposalID in the activeProposalQueue @@ -64,7 +66,7 @@ func InactiveProposalQueueTimePrefix(endTime time.Time) []byte { return bytes.Join([][]byte{ PrefixInactiveProposalQueue, sdk.FormatTimeBytes(endTime), - }, []byte("/")) + }, KeyDelimiter) } // Returns the key for a proposalID in the activeProposalQueue @@ -72,6 +74,6 @@ func InactiveProposalQueueProposalKey(endTime time.Time, proposalID int64) []byt return bytes.Join([][]byte{ PrefixInactiveProposalQueue, sdk.FormatTimeBytes(endTime), - sdk.Int64ToSortableBytes(proposalID), - }, []byte("/")) + sdk.Int64ToBigEndian(proposalID), + }, KeyDelimiter) } From 46f62d530e49f9ac8de1e501e10fc78c74654f46 Mon Sep 17 00:00:00 2001 From: Sunny Aggarwal Date: Mon, 5 Nov 2018 15:16:36 -0800 Subject: [PATCH 04/16] update proposal.Equal --- x/gov/proposals.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/x/gov/proposals.go b/x/gov/proposals.go index 1d9d562a6b25..b0cc0024b18e 100644 --- a/x/gov/proposals.go +++ b/x/gov/proposals.go @@ -56,8 +56,10 @@ func ProposalEqual(proposalA Proposal, proposalB Proposal) bool { proposalA.GetStatus() == proposalB.GetStatus() && proposalA.GetTallyResult().Equals(proposalB.GetTallyResult()) && proposalA.GetSubmitTime().Equal(proposalB.GetSubmitTime()) && + proposalA.GetDepositEndTime().Equal(proposalB.GetDepositEndTime() &&) proposalA.GetTotalDeposit().IsEqual(proposalB.GetTotalDeposit()) && - proposalA.GetVotingStartTime().Equal(proposalB.GetVotingStartTime()) { + proposalA.GetVotingStartTime().Equal(proposalB.GetVotingStartTime()) && + proposalA.GetVotingEndTime().Equal(proposalB.GetVotingEndTime()) { return true } return false From b9b52c3fe29105d47a0832a292f7dc91b885ffa0 Mon Sep 17 00:00:00 2001 From: Sunny Aggarwal Date: Mon, 5 Nov 2018 15:38:15 -0800 Subject: [PATCH 05/16] Amino api change --- x/gov/endblocker_test.go | 6 +++--- x/gov/keeper_test.go | 6 +++--- x/gov/proposals.go | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/x/gov/endblocker_test.go b/x/gov/endblocker_test.go index 9f7803264273..068aa55ffc37 100644 --- a/x/gov/endblocker_test.go +++ b/x/gov/endblocker_test.go @@ -126,7 +126,7 @@ func TestTickPassedDepositPeriod(t *testing.T) { res := govHandler(ctx, newProposalMsg) require.True(t, res.IsOK()) var proposalID int64 - keeper.cdc.MustUnmarshalBinary(res.Data, &proposalID) + keeper.cdc.MustUnmarshalBinaryLengthPrefixed(res.Data, &proposalID) inactiveQueue = keeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) require.False(t, inactiveQueue.Valid()) @@ -168,7 +168,7 @@ func TestTickPassedVotingPeriod(t *testing.T) { res := govHandler(ctx, newProposalMsg) require.True(t, res.IsOK()) var proposalID int64 - keeper.cdc.MustUnmarshalBinary(res.Data, &proposalID) + keeper.cdc.MustUnmarshalBinaryLengthPrefixed(res.Data, &proposalID) newHeader := ctx.BlockHeader() newHeader.Time = ctx.BlockHeader().Time.Add(time.Duration(1) * time.Second) @@ -189,7 +189,7 @@ func TestTickPassedVotingPeriod(t *testing.T) { activeQueue = keeper.ActiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) require.True(t, activeQueue.Valid()) var activeProposalID int64 - keeper.cdc.UnmarshalBinary(activeQueue.Value(), &activeProposalID) + keeper.cdc.UnmarshalBinaryLengthPrefixed(activeQueue.Value(), &activeProposalID) require.Equal(t, StatusVotingPeriod, keeper.GetProposal(ctx, activeProposalID).GetStatus()) depositsIterator := keeper.GetDeposits(ctx, proposalID) require.True(t, depositsIterator.Valid()) diff --git a/x/gov/keeper_test.go b/x/gov/keeper_test.go index b3a7c0623298..b428be1ebf94 100644 --- a/x/gov/keeper_test.go +++ b/x/gov/keeper_test.go @@ -55,7 +55,7 @@ func TestActivateVotingPeriod(t *testing.T) { activeIterator := keeper.ActiveProposalQueueIterator(ctx, proposal.GetVotingEndTime()) require.True(t, activeIterator.Valid()) var proposalID int64 - keeper.cdc.UnmarshalBinary(activeIterator.Value(), &proposalID) + keeper.cdc.UnmarshalBinaryLengthPrefixed(activeIterator.Value(), &proposalID) require.Equal(t, proposalID, proposal.GetProposalID()) activeIterator.Close() } @@ -215,7 +215,7 @@ func TestProposalQueues(t *testing.T) { inactiveIterator := keeper.InactiveProposalQueueIterator(ctx, proposal.GetDepositEndTime()) require.True(t, inactiveIterator.Valid()) var proposalID int64 - keeper.cdc.UnmarshalBinary(inactiveIterator.Value(), &proposalID) + keeper.cdc.UnmarshalBinaryLengthPrefixed(inactiveIterator.Value(), &proposalID) require.Equal(t, proposalID, proposal.GetProposalID()) inactiveIterator.Close() @@ -223,7 +223,7 @@ func TestProposalQueues(t *testing.T) { activeIterator := keeper.ActiveProposalQueueIterator(ctx, proposal.GetVotingEndTime()) require.True(t, activeIterator.Valid()) - keeper.cdc.UnmarshalBinary(activeIterator.Value(), &proposalID) + keeper.cdc.UnmarshalBinaryLengthPrefixed(activeIterator.Value(), &proposalID) require.Equal(t, proposalID, proposal.GetProposalID()) activeIterator.Close() } diff --git a/x/gov/proposals.go b/x/gov/proposals.go index b0cc0024b18e..4e21c8a0e25d 100644 --- a/x/gov/proposals.go +++ b/x/gov/proposals.go @@ -56,7 +56,7 @@ func ProposalEqual(proposalA Proposal, proposalB Proposal) bool { proposalA.GetStatus() == proposalB.GetStatus() && proposalA.GetTallyResult().Equals(proposalB.GetTallyResult()) && proposalA.GetSubmitTime().Equal(proposalB.GetSubmitTime()) && - proposalA.GetDepositEndTime().Equal(proposalB.GetDepositEndTime() &&) + proposalA.GetDepositEndTime().Equal(proposalB.GetDepositEndTime()) && proposalA.GetTotalDeposit().IsEqual(proposalB.GetTotalDeposit()) && proposalA.GetVotingStartTime().Equal(proposalB.GetVotingStartTime()) && proposalA.GetVotingEndTime().Equal(proposalB.GetVotingEndTime()) { From 6d6c5403dfef58b665204bc9be1616339202c2b3 Mon Sep 17 00:00:00 2001 From: Sunny Aggarwal Date: Mon, 5 Nov 2018 15:55:12 -0800 Subject: [PATCH 06/16] addressed comments --- client/lcd/lcd_test.go | 12 ++++++------ types/utils.go | 6 +++--- x/gov/keeper_keys.go | 4 ++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/client/lcd/lcd_test.go b/client/lcd/lcd_test.go index 5aebd0da0987..8c75cdc7a1d6 100644 --- a/client/lcd/lcd_test.go +++ b/client/lcd/lcd_test.go @@ -629,7 +629,7 @@ func TestSubmitProposal(t *testing.T) { require.Equal(t, uint32(0), resultTx.DeliverTx.Code) var proposalID int64 - cdc.MustUnmarshalBinary(resultTx.DeliverTx.GetData(), &proposalID) + cdc.MustUnmarshalBinaryLengthPrefixed(resultTx.DeliverTx.GetData(), &proposalID) // query proposal proposal := getProposal(t, port, proposalID) @@ -651,7 +651,7 @@ func TestDeposit(t *testing.T) { require.Equal(t, uint32(0), resultTx.DeliverTx.Code) var proposalID int64 - cdc.MustUnmarshalBinary(resultTx.DeliverTx.GetData(), &proposalID) + cdc.MustUnmarshalBinaryLengthPrefixed(resultTx.DeliverTx.GetData(), &proposalID) // query proposal proposal := getProposal(t, port, proposalID) @@ -685,7 +685,7 @@ func TestVote(t *testing.T) { require.Equal(t, uint32(0), resultTx.DeliverTx.Code) var proposalID int64 - cdc.MustUnmarshalBinary(resultTx.DeliverTx.GetData(), &proposalID) + cdc.MustUnmarshalBinaryLengthPrefixed(resultTx.DeliverTx.GetData(), &proposalID) // query proposal proposal := getProposal(t, port, proposalID) @@ -733,17 +733,17 @@ func TestProposalsQuery(t *testing.T) { // Addr1 proposes (and deposits) proposals #1 and #2 resultTx := doSubmitProposal(t, port, seeds[0], names[0], passwords[0], addrs[0], 5) var proposalID1 int64 - cdc.MustUnmarshalBinary(resultTx.DeliverTx.GetData(), &proposalID1) + cdc.MustUnmarshalBinaryLengthPrefixed(resultTx.DeliverTx.GetData(), &proposalID1) tests.WaitForHeight(resultTx.Height+1, port) resultTx = doSubmitProposal(t, port, seeds[0], names[0], passwords[0], addrs[0], 5) var proposalID2 int64 - cdc.MustUnmarshalBinary(resultTx.DeliverTx.GetData(), &proposalID2) + cdc.MustUnmarshalBinaryLengthPrefixed(resultTx.DeliverTx.GetData(), &proposalID2) tests.WaitForHeight(resultTx.Height+1, port) // Addr2 proposes (and deposits) proposals #3 resultTx = doSubmitProposal(t, port, seeds[1], names[1], passwords[1], addrs[1], 5) var proposalID3 int64 - cdc.MustUnmarshalBinary(resultTx.DeliverTx.GetData(), &proposalID3) + cdc.MustUnmarshalBinaryLengthPrefixed(resultTx.DeliverTx.GetData(), &proposalID3) tests.WaitForHeight(resultTx.Height+1, port) // Addr2 deposits on proposals #2 & #3 diff --git a/types/utils.go b/types/utils.go index 289aef472f43..91cdcf9b0384 100644 --- a/types/utils.go +++ b/types/utils.go @@ -37,10 +37,10 @@ func MustSortJSON(toSortJSON []byte) []byte { return js } -// Int64ToBigEndian - marshals int64 to a bigendian byte slice so it can be sorted -func Int64ToBigEndian(i int64) []byte { +// Uint64ToBigEndian - marshals uint64 to a bigendian byte slice so it can be sorted +func Uint64ToBigEndian(i uint64) []byte { b := make([]byte, 8) - binary.BigEndian.PutUint64(b, uint64(i)) + binary.BigEndian.PutUint64(b, i) return b } diff --git a/x/gov/keeper_keys.go b/x/gov/keeper_keys.go index b171961f99c0..bb78ed9b17d2 100644 --- a/x/gov/keeper_keys.go +++ b/x/gov/keeper_keys.go @@ -57,7 +57,7 @@ func ActiveProposalQueueProposalKey(endTime time.Time, proposalID int64) []byte return bytes.Join([][]byte{ PrefixActiveProposalQueue, sdk.FormatTimeBytes(endTime), - sdk.Int64ToBigEndian(proposalID), + sdk.Uint64ToBigEndian(uint64(proposalID)), }, KeyDelimiter) } @@ -74,6 +74,6 @@ func InactiveProposalQueueProposalKey(endTime time.Time, proposalID int64) []byt return bytes.Join([][]byte{ PrefixInactiveProposalQueue, sdk.FormatTimeBytes(endTime), - sdk.Int64ToBigEndian(proposalID), + sdk.Uint64ToBigEndian(uint64(proposalID)), }, KeyDelimiter) } From c48f8f0e177f7517fa9c81ec5a183f3d4fb74b22 Mon Sep 17 00:00:00 2001 From: Sunny Aggarwal Date: Mon, 5 Nov 2018 21:40:21 -0800 Subject: [PATCH 07/16] switched proposalID to uint64 --- client/lcd/lcd_test.go | 26 +++++++++--------- client/utils/rest.go | 14 ++++++++++ cmd/gaia/app/sim_test.go | 2 +- cmd/gaia/cli_test/cli_test.go | 7 ++--- x/gov/client/cli/tx.go | 18 ++++++------- x/gov/client/rest/rest.go | 18 ++++++------- x/gov/depositsvotes.go | 4 +-- x/gov/endblocker_test.go | 6 ++--- x/gov/errors.go | 8 +++--- x/gov/genesis.go | 4 +-- x/gov/handler.go | 4 +-- x/gov/keeper.go | 50 +++++++++++++++++------------------ x/gov/keeper_keys.go | 18 ++++++------- x/gov/keeper_test.go | 6 ++--- x/gov/msgs.go | 8 +++--- x/gov/msgs_test.go | 6 ++--- x/gov/proposals.go | 12 ++++----- x/gov/queryable.go | 20 +++++++------- x/gov/simulation/msgs.go | 8 +++--- 19 files changed, 127 insertions(+), 112 deletions(-) diff --git a/client/lcd/lcd_test.go b/client/lcd/lcd_test.go index 8c75cdc7a1d6..92167346d2c1 100644 --- a/client/lcd/lcd_test.go +++ b/client/lcd/lcd_test.go @@ -628,7 +628,7 @@ func TestSubmitProposal(t *testing.T) { require.Equal(t, uint32(0), resultTx.CheckTx.Code) require.Equal(t, uint32(0), resultTx.DeliverTx.Code) - var proposalID int64 + var proposalID uint64 cdc.MustUnmarshalBinaryLengthPrefixed(resultTx.DeliverTx.GetData(), &proposalID) // query proposal @@ -650,7 +650,7 @@ func TestDeposit(t *testing.T) { require.Equal(t, uint32(0), resultTx.CheckTx.Code) require.Equal(t, uint32(0), resultTx.DeliverTx.Code) - var proposalID int64 + var proposalID uint64 cdc.MustUnmarshalBinaryLengthPrefixed(resultTx.DeliverTx.GetData(), &proposalID) // query proposal @@ -684,7 +684,7 @@ func TestVote(t *testing.T) { require.Equal(t, uint32(0), resultTx.CheckTx.Code) require.Equal(t, uint32(0), resultTx.DeliverTx.Code) - var proposalID int64 + var proposalID uint64 cdc.MustUnmarshalBinaryLengthPrefixed(resultTx.DeliverTx.GetData(), &proposalID) // query proposal @@ -732,17 +732,17 @@ func TestProposalsQuery(t *testing.T) { // Addr1 proposes (and deposits) proposals #1 and #2 resultTx := doSubmitProposal(t, port, seeds[0], names[0], passwords[0], addrs[0], 5) - var proposalID1 int64 + var proposalID1 uint64 cdc.MustUnmarshalBinaryLengthPrefixed(resultTx.DeliverTx.GetData(), &proposalID1) tests.WaitForHeight(resultTx.Height+1, port) resultTx = doSubmitProposal(t, port, seeds[0], names[0], passwords[0], addrs[0], 5) - var proposalID2 int64 + var proposalID2 uint64 cdc.MustUnmarshalBinaryLengthPrefixed(resultTx.DeliverTx.GetData(), &proposalID2) tests.WaitForHeight(resultTx.Height+1, port) // Addr2 proposes (and deposits) proposals #3 resultTx = doSubmitProposal(t, port, seeds[1], names[1], passwords[1], addrs[1], 5) - var proposalID3 int64 + var proposalID3 uint64 cdc.MustUnmarshalBinaryLengthPrefixed(resultTx.DeliverTx.GetData(), &proposalID3) tests.WaitForHeight(resultTx.Height+1, port) @@ -1230,7 +1230,7 @@ func getValidatorRedelegations(t *testing.T, port string, validatorAddr sdk.ValA // ============= Governance Module ================ -func getProposal(t *testing.T, port string, proposalID int64) gov.Proposal { +func getProposal(t *testing.T, port string, proposalID uint64) gov.Proposal { res, body := Request(t, port, "GET", fmt.Sprintf("/gov/proposals/%d", proposalID), nil) require.Equal(t, http.StatusOK, res.StatusCode, body) var proposal gov.Proposal @@ -1239,7 +1239,7 @@ func getProposal(t *testing.T, port string, proposalID int64) gov.Proposal { return proposal } -func getDeposits(t *testing.T, port string, proposalID int64) []gov.Deposit { +func getDeposits(t *testing.T, port string, proposalID uint64) []gov.Deposit { res, body := Request(t, port, "GET", fmt.Sprintf("/gov/proposals/%d/deposits", proposalID), nil) require.Equal(t, http.StatusOK, res.StatusCode, body) var deposits []gov.Deposit @@ -1248,7 +1248,7 @@ func getDeposits(t *testing.T, port string, proposalID int64) []gov.Deposit { return deposits } -func getDeposit(t *testing.T, port string, proposalID int64, depositerAddr sdk.AccAddress) gov.Deposit { +func getDeposit(t *testing.T, port string, proposalID uint64, depositerAddr sdk.AccAddress) gov.Deposit { res, body := Request(t, port, "GET", fmt.Sprintf("/gov/proposals/%d/deposits/%s", proposalID, depositerAddr), nil) require.Equal(t, http.StatusOK, res.StatusCode, body) var deposit gov.Deposit @@ -1257,7 +1257,7 @@ func getDeposit(t *testing.T, port string, proposalID int64, depositerAddr sdk.A return deposit } -func getVote(t *testing.T, port string, proposalID int64, voterAddr sdk.AccAddress) gov.Vote { +func getVote(t *testing.T, port string, proposalID uint64, voterAddr sdk.AccAddress) gov.Vote { res, body := Request(t, port, "GET", fmt.Sprintf("/gov/proposals/%d/votes/%s", proposalID, voterAddr), nil) require.Equal(t, http.StatusOK, res.StatusCode, body) var vote gov.Vote @@ -1266,7 +1266,7 @@ func getVote(t *testing.T, port string, proposalID int64, voterAddr sdk.AccAddre return vote } -func getVotes(t *testing.T, port string, proposalID int64) []gov.Vote { +func getVotes(t *testing.T, port string, proposalID uint64) []gov.Vote { res, body := Request(t, port, "GET", fmt.Sprintf("/gov/proposals/%d/votes", proposalID), nil) require.Equal(t, http.StatusOK, res.StatusCode, body) var votes []gov.Vote @@ -1358,7 +1358,7 @@ func doSubmitProposal(t *testing.T, port, seed, name, password string, proposerA return results } -func doDeposit(t *testing.T, port, seed, name, password string, proposerAddr sdk.AccAddress, proposalID int64, amount int64) (resultTx ctypes.ResultBroadcastTxCommit) { +func doDeposit(t *testing.T, port, seed, name, password string, proposerAddr sdk.AccAddress, proposalID uint64, amount int64) (resultTx ctypes.ResultBroadcastTxCommit) { acc := getAccount(t, port, proposerAddr) accnum := acc.GetAccountNumber() @@ -1388,7 +1388,7 @@ func doDeposit(t *testing.T, port, seed, name, password string, proposerAddr sdk return results } -func doVote(t *testing.T, port, seed, name, password string, proposerAddr sdk.AccAddress, proposalID int64) (resultTx ctypes.ResultBroadcastTxCommit) { +func doVote(t *testing.T, port, seed, name, password string, proposerAddr sdk.AccAddress, proposalID uint64) (resultTx ctypes.ResultBroadcastTxCommit) { // get the account to get the sequence acc := getAccount(t, port, proposerAddr) accnum := acc.GetAccountNumber() diff --git a/client/utils/rest.go b/client/utils/rest.go index e9d948514720..53c6186921db 100644 --- a/client/utils/rest.go +++ b/client/utils/rest.go @@ -65,6 +65,20 @@ func ParseInt64OrReturnBadRequest(w http.ResponseWriter, s string) (n int64, ok return n, true } +// ParseUint64OrReturnBadRequest converts s to a uint64 value. +func ParseUint64OrReturnBadRequest(w http.ResponseWriter, s string) (n uint64, ok bool) { + var err error + + n, err = strconv.ParseUint(s, 10, 64) + if err != nil { + err := fmt.Errorf("'%s' is not a valid uint64", s) + WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + return n, false + } + + return n, true +} + // ParseFloat64OrReturnBadRequest converts s to a float64 value. It returns a // default value, defaultIfEmpty, if the string is empty. func ParseFloat64OrReturnBadRequest(w http.ResponseWriter, s string, defaultIfEmpty float64) (n float64, ok bool) { diff --git a/cmd/gaia/app/sim_test.go b/cmd/gaia/app/sim_test.go index 0216616a88ac..80cbd7d0b333 100644 --- a/cmd/gaia/app/sim_test.go +++ b/cmd/gaia/app/sim_test.go @@ -70,7 +70,7 @@ func appStateFn(r *rand.Rand, accs []simulation.Account) json.RawMessage { // Random genesis states govGenesis := gov.GenesisState{ - StartingProposalID: int64(r.Intn(100)), + StartingProposalID: uint64(r.Intn(100)), DepositProcedure: gov.DepositProcedure{ MinDeposit: sdk.Coins{sdk.NewInt64Coin("steak", int64(r.Intn(1e3)))}, MaxDepositPeriod: time.Duration(r.Intn(2*172800)) * time.Second, diff --git a/cmd/gaia/cli_test/cli_test.go b/cmd/gaia/cli_test/cli_test.go index c80b597c75e0..aae8491c5dac 100644 --- a/cmd/gaia/cli_test/cli_test.go +++ b/cmd/gaia/cli_test/cli_test.go @@ -5,13 +5,14 @@ package clitest import ( "encoding/json" "fmt" - "github.com/tendermint/tendermint/types" "io/ioutil" "os" "path" "path/filepath" "testing" + "github.com/tendermint/tendermint/types" + "github.com/stretchr/testify/require" abci "github.com/tendermint/tendermint/abci/types" @@ -348,7 +349,7 @@ func TestGaiaCLISubmitProposal(t *testing.T) { require.Equal(t, int64(45), fooAcc.GetCoins().AmountOf("steak").Int64()) proposal1 := executeGetProposal(t, fmt.Sprintf("gaiacli query proposal --proposal-id=1 --output=json %v", flags)) - require.Equal(t, int64(1), proposal1.GetProposalID()) + require.Equal(t, uint64(1), proposal1.GetProposalID()) require.Equal(t, gov.StatusDepositPeriod, proposal1.GetStatus()) proposalsQuery, _ = tests.ExecuteT(t, fmt.Sprintf("gaiacli query proposals %v", flags), "") @@ -391,7 +392,7 @@ func TestGaiaCLISubmitProposal(t *testing.T) { fooAcc = executeGetAccount(t, fmt.Sprintf("gaiacli query account %s %v", fooAddr, flags)) require.Equal(t, int64(35), fooAcc.GetCoins().AmountOf("steak").Int64()) proposal1 = executeGetProposal(t, fmt.Sprintf("gaiacli query proposal --proposal-id=1 --output=json %v", flags)) - require.Equal(t, int64(1), proposal1.GetProposalID()) + require.Equal(t, uint64(1), proposal1.GetProposalID()) require.Equal(t, gov.StatusVotingPeriod, proposal1.GetStatus()) voteStr := fmt.Sprintf("gaiacli tx vote %v", flags) diff --git a/x/gov/client/cli/tx.go b/x/gov/client/cli/tx.go index 4b23002bd74f..31072c54a292 100644 --- a/x/gov/client/cli/tx.go +++ b/x/gov/client/cli/tx.go @@ -170,7 +170,7 @@ func GetCmdDeposit(cdc *codec.Codec) *cobra.Command { return err } - proposalID := viper.GetInt64(flagProposalID) + proposalID := uint64(viper.GetInt64(flagProposalID)) amount, err := sdk.ParseCoins(viper.GetString(flagDeposit)) if err != nil { @@ -215,7 +215,7 @@ func GetCmdVote(cdc *codec.Codec) *cobra.Command { return err } - proposalID := viper.GetInt64(flagProposalID) + proposalID := uint64(viper.GetInt64(flagProposalID)) option := viper.GetString(flagOption) byteVoteOption, err := gov.VoteOptionFromString(client.NormalizeVoteOption(option)) @@ -256,7 +256,7 @@ func GetCmdQueryProposal(queryRoute string, cdc *codec.Codec) *cobra.Command { Short: "Query details of a single proposal", RunE: func(cmd *cobra.Command, args []string) error { cliCtx := context.NewCLIContext().WithCodec(cdc) - proposalID := viper.GetInt64(flagProposalID) + proposalID := uint64(viper.GetInt64(flagProposalID)) params := gov.QueryProposalParams{ ProposalID: proposalID, @@ -291,7 +291,7 @@ func GetCmdQueryProposals(queryRoute string, cdc *codec.Codec) *cobra.Command { bechDepositerAddr := viper.GetString(flagDepositer) bechVoterAddr := viper.GetString(flagVoter) strProposalStatus := viper.GetString(flagStatus) - latestProposalsIDs := viper.GetInt64(flagLatestProposalIDs) + latestProposalsIDs := uint64(viper.GetInt64(flagLatestProposalIDs)) params := gov.QueryProposalsParams{ NumLatestProposals: latestProposalsIDs, @@ -368,7 +368,7 @@ func GetCmdQueryVote(queryRoute string, cdc *codec.Codec) *cobra.Command { Short: "Query details of a single vote", RunE: func(cmd *cobra.Command, args []string) error { cliCtx := context.NewCLIContext().WithCodec(cdc) - proposalID := viper.GetInt64(flagProposalID) + proposalID := uint64(viper.GetInt64(flagProposalID)) voterAddr, err := sdk.AccAddressFromBech32(viper.GetString(flagVoter)) if err != nil { @@ -407,7 +407,7 @@ func GetCmdQueryVotes(queryRoute string, cdc *codec.Codec) *cobra.Command { Short: "Query votes on a proposal", RunE: func(cmd *cobra.Command, args []string) error { cliCtx := context.NewCLIContext().WithCodec(cdc) - proposalID := viper.GetInt64(flagProposalID) + proposalID := uint64(viper.GetInt64(flagProposalID)) params := gov.QueryVotesParams{ ProposalID: proposalID, @@ -440,7 +440,7 @@ func GetCmdQueryDeposit(queryRoute string, cdc *codec.Codec) *cobra.Command { Short: "Query details of a deposit", RunE: func(cmd *cobra.Command, args []string) error { cliCtx := context.NewCLIContext().WithCodec(cdc) - proposalID := viper.GetInt64(flagProposalID) + proposalID := uint64(viper.GetInt64(flagProposalID)) depositerAddr, err := sdk.AccAddressFromBech32(viper.GetString(flagDepositer)) if err != nil { @@ -479,7 +479,7 @@ func GetCmdQueryDeposits(queryRoute string, cdc *codec.Codec) *cobra.Command { Short: "Query deposits on a proposal", RunE: func(cmd *cobra.Command, args []string) error { cliCtx := context.NewCLIContext().WithCodec(cdc) - proposalID := viper.GetInt64(flagProposalID) + proposalID := uint64(viper.GetInt64(flagProposalID)) params := gov.QueryDepositsParams{ ProposalID: proposalID, @@ -511,7 +511,7 @@ func GetCmdQueryTally(queryRoute string, cdc *codec.Codec) *cobra.Command { Short: "Get the tally of a proposal vote", RunE: func(cmd *cobra.Command, args []string) error { cliCtx := context.NewCLIContext().WithCodec(cdc) - proposalID := viper.GetInt64(flagProposalID) + proposalID := uint64(viper.GetInt64(flagProposalID)) params := gov.QueryTallyParams{ ProposalID: proposalID, diff --git a/x/gov/client/rest/rest.go b/x/gov/client/rest/rest.go index 842edfdacb45..f5a92d110737 100644 --- a/x/gov/client/rest/rest.go +++ b/x/gov/client/rest/rest.go @@ -104,7 +104,7 @@ func depositHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerF return } - proposalID, ok := utils.ParseInt64OrReturnBadRequest(w, strProposalID) + proposalID, ok := utils.ParseUint64OrReturnBadRequest(w, strProposalID) if !ok { return } @@ -143,7 +143,7 @@ func voteHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc return } - proposalID, ok := utils.ParseInt64OrReturnBadRequest(w, strProposalID) + proposalID, ok := utils.ParseUint64OrReturnBadRequest(w, strProposalID) if !ok { return } @@ -188,7 +188,7 @@ func queryProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Ha return } - proposalID, ok := utils.ParseInt64OrReturnBadRequest(w, strProposalID) + proposalID, ok := utils.ParseUint64OrReturnBadRequest(w, strProposalID) if !ok { return } @@ -218,7 +218,7 @@ func queryDepositsHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Ha vars := mux.Vars(r) strProposalID := vars[RestProposalID] - proposalID, ok := utils.ParseInt64OrReturnBadRequest(w, strProposalID) + proposalID, ok := utils.ParseUint64OrReturnBadRequest(w, strProposalID) if !ok { return } @@ -255,7 +255,7 @@ func queryDepositHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Han return } - proposalID, ok := utils.ParseInt64OrReturnBadRequest(w, strProposalID) + proposalID, ok := utils.ParseUint64OrReturnBadRequest(w, strProposalID) if !ok { return } @@ -319,7 +319,7 @@ func queryVoteHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Handle return } - proposalID, ok := utils.ParseInt64OrReturnBadRequest(w, strProposalID) + proposalID, ok := utils.ParseUint64OrReturnBadRequest(w, strProposalID) if !ok { return } @@ -386,7 +386,7 @@ func queryVotesOnProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) return } - proposalID, ok := utils.ParseInt64OrReturnBadRequest(w, strProposalID) + proposalID, ok := utils.ParseUint64OrReturnBadRequest(w, strProposalID) if !ok { return } @@ -447,7 +447,7 @@ func queryProposalsWithParameterFn(cdc *codec.Codec, cliCtx context.CLIContext) params.ProposalStatus = proposalStatus } if len(strNumLatest) != 0 { - numLatest, ok := utils.ParseInt64OrReturnBadRequest(w, strNumLatest) + numLatest, ok := utils.ParseUint64OrReturnBadRequest(w, strNumLatest) if !ok { return } @@ -484,7 +484,7 @@ func queryTallyOnProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) return } - proposalID, ok := utils.ParseInt64OrReturnBadRequest(w, strProposalID) + proposalID, ok := utils.ParseUint64OrReturnBadRequest(w, strProposalID) if !ok { return } diff --git a/x/gov/depositsvotes.go b/x/gov/depositsvotes.go index d1179023fa4b..7a6b043e6367 100644 --- a/x/gov/depositsvotes.go +++ b/x/gov/depositsvotes.go @@ -11,7 +11,7 @@ import ( // Vote type Vote struct { Voter sdk.AccAddress `json:"voter"` // address of the voter - ProposalID int64 `json:"proposal_id"` // proposalID of the proposal + ProposalID uint64 `json:"proposal_id"` // proposalID of the proposal Option VoteOption `json:"option"` // option from OptionSet chosen by the voter } @@ -29,7 +29,7 @@ func (voteA Vote) Empty() bool { // Deposit type Deposit struct { Depositer sdk.AccAddress `json:"depositer"` // Address of the depositer - ProposalID int64 `json:"proposal_id"` // proposalID of the proposal + ProposalID uint64 `json:"proposal_id"` // proposalID of the proposal Amount sdk.Coins `json:"amount"` // Deposit amount } diff --git a/x/gov/endblocker_test.go b/x/gov/endblocker_test.go index 068aa55ffc37..07b3e89b6c07 100644 --- a/x/gov/endblocker_test.go +++ b/x/gov/endblocker_test.go @@ -125,7 +125,7 @@ func TestTickPassedDepositPeriod(t *testing.T) { res := govHandler(ctx, newProposalMsg) require.True(t, res.IsOK()) - var proposalID int64 + var proposalID uint64 keeper.cdc.MustUnmarshalBinaryLengthPrefixed(res.Data, &proposalID) inactiveQueue = keeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) @@ -167,7 +167,7 @@ func TestTickPassedVotingPeriod(t *testing.T) { res := govHandler(ctx, newProposalMsg) require.True(t, res.IsOK()) - var proposalID int64 + var proposalID uint64 keeper.cdc.MustUnmarshalBinaryLengthPrefixed(res.Data, &proposalID) newHeader := ctx.BlockHeader() @@ -188,7 +188,7 @@ func TestTickPassedVotingPeriod(t *testing.T) { activeQueue = keeper.ActiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) require.True(t, activeQueue.Valid()) - var activeProposalID int64 + var activeProposalID uint64 keeper.cdc.UnmarshalBinaryLengthPrefixed(activeQueue.Value(), &activeProposalID) require.Equal(t, StatusVotingPeriod, keeper.GetProposal(ctx, activeProposalID).GetStatus()) depositsIterator := keeper.GetDeposits(ctx, proposalID) diff --git a/x/gov/errors.go b/x/gov/errors.go index cd1cf8a17715..e803d080eacd 100644 --- a/x/gov/errors.go +++ b/x/gov/errors.go @@ -26,19 +26,19 @@ const ( //---------------------------------------- // Error constructors -func ErrUnknownProposal(codespace sdk.CodespaceType, proposalID int64) sdk.Error { +func ErrUnknownProposal(codespace sdk.CodespaceType, proposalID uint64) sdk.Error { return sdk.NewError(codespace, CodeUnknownProposal, fmt.Sprintf("Unknown proposal with id %d", proposalID)) } -func ErrInactiveProposal(codespace sdk.CodespaceType, proposalID int64) sdk.Error { +func ErrInactiveProposal(codespace sdk.CodespaceType, proposalID uint64) sdk.Error { return sdk.NewError(codespace, CodeInactiveProposal, fmt.Sprintf("Inactive proposal with id %d", proposalID)) } -func ErrAlreadyActiveProposal(codespace sdk.CodespaceType, proposalID int64) sdk.Error { +func ErrAlreadyActiveProposal(codespace sdk.CodespaceType, proposalID uint64) sdk.Error { return sdk.NewError(codespace, CodeAlreadyActiveProposal, fmt.Sprintf("Proposal %d has been already active", proposalID)) } -func ErrAlreadyFinishedProposal(codespace sdk.CodespaceType, proposalID int64) sdk.Error { +func ErrAlreadyFinishedProposal(codespace sdk.CodespaceType, proposalID uint64) sdk.Error { return sdk.NewError(codespace, CodeAlreadyFinishedProposal, fmt.Sprintf("Proposal %d has already passed its voting period", proposalID)) } diff --git a/x/gov/genesis.go b/x/gov/genesis.go index 58273c8e849b..e7023848acea 100644 --- a/x/gov/genesis.go +++ b/x/gov/genesis.go @@ -8,13 +8,13 @@ import ( // GenesisState - all staking state that must be provided at genesis type GenesisState struct { - StartingProposalID int64 `json:"starting_proposalID"` + StartingProposalID uint64 `json:"starting_proposalID"` DepositProcedure DepositProcedure `json:"deposit_period"` VotingProcedure VotingProcedure `json:"voting_period"` TallyingProcedure TallyingProcedure `json:"tallying_procedure"` } -func NewGenesisState(startingProposalID int64, dp DepositProcedure, vp VotingProcedure, tp TallyingProcedure) GenesisState { +func NewGenesisState(startingProposalID uint64, dp DepositProcedure, vp VotingProcedure, tp TallyingProcedure) GenesisState { return GenesisState{ StartingProposalID: startingProposalID, DepositProcedure: dp, diff --git a/x/gov/handler.go b/x/gov/handler.go index 8274ee1986e5..5b4628f01640 100644 --- a/x/gov/handler.go +++ b/x/gov/handler.go @@ -104,7 +104,7 @@ func EndBlocker(ctx sdk.Context, keeper Keeper) (resTags sdk.Tags) { inactiveIterator := keeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) for ; inactiveIterator.Valid(); inactiveIterator.Next() { - var proposalID int64 + var proposalID uint64 keeper.cdc.MustUnmarshalBinaryLengthPrefixed(inactiveIterator.Value(), &proposalID) inactiveProposal := keeper.GetProposal(ctx, proposalID) keeper.RefundDeposits(ctx, proposalID) @@ -126,7 +126,7 @@ func EndBlocker(ctx sdk.Context, keeper Keeper) (resTags sdk.Tags) { activeIterator := keeper.ActiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) for ; activeIterator.Valid(); activeIterator.Next() { - var proposalID int64 + var proposalID uint64 keeper.cdc.MustUnmarshalBinaryLengthPrefixed(activeIterator.Value(), &proposalID) activeProposal := keeper.GetProposal(ctx, proposalID) passes, tallyResults := tally(ctx, keeper, activeProposal) diff --git a/x/gov/keeper.go b/x/gov/keeper.go index ebe359161a73..e1acaa5d85e1 100644 --- a/x/gov/keeper.go +++ b/x/gov/keeper.go @@ -104,7 +104,7 @@ func (keeper Keeper) NewTextProposal(ctx sdk.Context, title string, description } // Get Proposal from store by ProposalID -func (keeper Keeper) GetProposal(ctx sdk.Context, proposalID int64) Proposal { +func (keeper Keeper) GetProposal(ctx sdk.Context, proposalID uint64) Proposal { store := ctx.KVStore(keeper.storeKey) bz := store.Get(KeyProposal(proposalID)) if bz == nil { @@ -125,7 +125,7 @@ func (keeper Keeper) SetProposal(ctx sdk.Context, proposal Proposal) { } // Implements sdk.AccountKeeper. -func (keeper Keeper) DeleteProposal(ctx sdk.Context, proposalID int64) { +func (keeper Keeper) DeleteProposal(ctx sdk.Context, proposalID uint64) { store := ctx.KVStore(keeper.storeKey) proposal := keeper.GetProposal(ctx, proposalID) keeper.RemoveFromInactiveProposalQueue(ctx, proposal.GetDepositEndTime(), proposalID) @@ -134,7 +134,7 @@ func (keeper Keeper) DeleteProposal(ctx sdk.Context, proposalID int64) { } // Get Proposal from store by ProposalID -func (keeper Keeper) GetProposalsFiltered(ctx sdk.Context, voterAddr sdk.AccAddress, depositerAddr sdk.AccAddress, status ProposalStatus, numLatest int64) []Proposal { +func (keeper Keeper) GetProposalsFiltered(ctx sdk.Context, voterAddr sdk.AccAddress, depositerAddr sdk.AccAddress, status ProposalStatus, numLatest uint64) []Proposal { maxProposalID, err := keeper.peekCurrentProposalID(ctx) if err != nil { @@ -143,7 +143,7 @@ func (keeper Keeper) GetProposalsFiltered(ctx sdk.Context, voterAddr sdk.AccAddr matchingProposals := []Proposal{} - if numLatest <= 0 { + if numLatest == 0 { numLatest = maxProposalID } @@ -178,7 +178,7 @@ func (keeper Keeper) GetProposalsFiltered(ctx sdk.Context, voterAddr sdk.AccAddr return matchingProposals } -func (keeper Keeper) setInitialProposalID(ctx sdk.Context, proposalID int64) sdk.Error { +func (keeper Keeper) setInitialProposalID(ctx sdk.Context, proposalID uint64) sdk.Error { store := ctx.KVStore(keeper.storeKey) bz := store.Get(KeyNextProposalID) if bz != nil { @@ -190,7 +190,7 @@ func (keeper Keeper) setInitialProposalID(ctx sdk.Context, proposalID int64) sdk } // Get the last used proposal ID -func (keeper Keeper) GetLastProposalID(ctx sdk.Context) (proposalID int64) { +func (keeper Keeper) GetLastProposalID(ctx sdk.Context) (proposalID uint64) { proposalID, err := keeper.peekCurrentProposalID(ctx) if err != nil { return 0 @@ -200,11 +200,11 @@ func (keeper Keeper) GetLastProposalID(ctx sdk.Context) (proposalID int64) { } // Gets the next available ProposalID and increments it -func (keeper Keeper) getNewProposalID(ctx sdk.Context) (proposalID int64, err sdk.Error) { +func (keeper Keeper) getNewProposalID(ctx sdk.Context) (proposalID uint64, err sdk.Error) { store := ctx.KVStore(keeper.storeKey) bz := store.Get(KeyNextProposalID) if bz == nil { - return -1, ErrInvalidGenesis(keeper.codespace, "InitialProposalID never set") + return 0, ErrInvalidGenesis(keeper.codespace, "InitialProposalID never set") } keeper.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &proposalID) bz = keeper.cdc.MustMarshalBinaryLengthPrefixed(proposalID + 1) @@ -213,11 +213,11 @@ func (keeper Keeper) getNewProposalID(ctx sdk.Context) (proposalID int64, err sd } // Peeks the next available ProposalID without incrementing it -func (keeper Keeper) peekCurrentProposalID(ctx sdk.Context) (proposalID int64, err sdk.Error) { +func (keeper Keeper) peekCurrentProposalID(ctx sdk.Context) (proposalID uint64, err sdk.Error) { store := ctx.KVStore(keeper.storeKey) bz := store.Get(KeyNextProposalID) if bz == nil { - return -1, ErrInvalidGenesis(keeper.codespace, "InitialProposalID never set") + return 0, ErrInvalidGenesis(keeper.codespace, "InitialProposalID never set") } keeper.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &proposalID) return proposalID, nil @@ -280,7 +280,7 @@ func (keeper Keeper) setTallyingProcedure(ctx sdk.Context, tallyingProcedure Tal // Votes // Adds a vote on a specific proposal -func (keeper Keeper) AddVote(ctx sdk.Context, proposalID int64, voterAddr sdk.AccAddress, option VoteOption) sdk.Error { +func (keeper Keeper) AddVote(ctx sdk.Context, proposalID uint64, voterAddr sdk.AccAddress, option VoteOption) sdk.Error { proposal := keeper.GetProposal(ctx, proposalID) if proposal == nil { return ErrUnknownProposal(keeper.codespace, proposalID) @@ -304,7 +304,7 @@ func (keeper Keeper) AddVote(ctx sdk.Context, proposalID int64, voterAddr sdk.Ac } // Gets the vote of a specific voter on a specific proposal -func (keeper Keeper) GetVote(ctx sdk.Context, proposalID int64, voterAddr sdk.AccAddress) (Vote, bool) { +func (keeper Keeper) GetVote(ctx sdk.Context, proposalID uint64, voterAddr sdk.AccAddress) (Vote, bool) { store := ctx.KVStore(keeper.storeKey) bz := store.Get(KeyVote(proposalID, voterAddr)) if bz == nil { @@ -315,19 +315,19 @@ func (keeper Keeper) GetVote(ctx sdk.Context, proposalID int64, voterAddr sdk.Ac return vote, true } -func (keeper Keeper) setVote(ctx sdk.Context, proposalID int64, voterAddr sdk.AccAddress, vote Vote) { +func (keeper Keeper) setVote(ctx sdk.Context, proposalID uint64, voterAddr sdk.AccAddress, vote Vote) { store := ctx.KVStore(keeper.storeKey) bz := keeper.cdc.MustMarshalBinaryLengthPrefixed(vote) store.Set(KeyVote(proposalID, voterAddr), bz) } // Gets all the votes on a specific proposal -func (keeper Keeper) GetVotes(ctx sdk.Context, proposalID int64) sdk.Iterator { +func (keeper Keeper) GetVotes(ctx sdk.Context, proposalID uint64) sdk.Iterator { store := ctx.KVStore(keeper.storeKey) return sdk.KVStorePrefixIterator(store, KeyVotesSubspace(proposalID)) } -func (keeper Keeper) deleteVote(ctx sdk.Context, proposalID int64, voterAddr sdk.AccAddress) { +func (keeper Keeper) deleteVote(ctx sdk.Context, proposalID uint64, voterAddr sdk.AccAddress) { store := ctx.KVStore(keeper.storeKey) store.Delete(KeyVote(proposalID, voterAddr)) } @@ -336,7 +336,7 @@ func (keeper Keeper) deleteVote(ctx sdk.Context, proposalID int64, voterAddr sdk // Deposits // Gets the deposit of a specific depositer on a specific proposal -func (keeper Keeper) GetDeposit(ctx sdk.Context, proposalID int64, depositerAddr sdk.AccAddress) (Deposit, bool) { +func (keeper Keeper) GetDeposit(ctx sdk.Context, proposalID uint64, depositerAddr sdk.AccAddress) (Deposit, bool) { store := ctx.KVStore(keeper.storeKey) bz := store.Get(KeyDeposit(proposalID, depositerAddr)) if bz == nil { @@ -347,7 +347,7 @@ func (keeper Keeper) GetDeposit(ctx sdk.Context, proposalID int64, depositerAddr return deposit, true } -func (keeper Keeper) setDeposit(ctx sdk.Context, proposalID int64, depositerAddr sdk.AccAddress, deposit Deposit) { +func (keeper Keeper) setDeposit(ctx sdk.Context, proposalID uint64, depositerAddr sdk.AccAddress, deposit Deposit) { store := ctx.KVStore(keeper.storeKey) bz := keeper.cdc.MustMarshalBinaryLengthPrefixed(deposit) store.Set(KeyDeposit(proposalID, depositerAddr), bz) @@ -355,7 +355,7 @@ func (keeper Keeper) setDeposit(ctx sdk.Context, proposalID int64, depositerAddr // Adds or updates a deposit of a specific depositer on a specific proposal // Activates voting period when appropriate -func (keeper Keeper) AddDeposit(ctx sdk.Context, proposalID int64, depositerAddr sdk.AccAddress, depositAmount sdk.Coins) (sdk.Error, bool) { +func (keeper Keeper) AddDeposit(ctx sdk.Context, proposalID uint64, depositerAddr sdk.AccAddress, depositAmount sdk.Coins) (sdk.Error, bool) { // Checks to see if proposal exists proposal := keeper.GetProposal(ctx, proposalID) if proposal == nil { @@ -399,13 +399,13 @@ func (keeper Keeper) AddDeposit(ctx sdk.Context, proposalID int64, depositerAddr } // Gets all the deposits on a specific proposal -func (keeper Keeper) GetDeposits(ctx sdk.Context, proposalID int64) sdk.Iterator { +func (keeper Keeper) GetDeposits(ctx sdk.Context, proposalID uint64) sdk.Iterator { store := ctx.KVStore(keeper.storeKey) return sdk.KVStorePrefixIterator(store, KeyDepositsSubspace(proposalID)) } // Returns and deletes all the deposits on a specific proposal -func (keeper Keeper) RefundDeposits(ctx sdk.Context, proposalID int64) { +func (keeper Keeper) RefundDeposits(ctx sdk.Context, proposalID uint64) { store := ctx.KVStore(keeper.storeKey) depositsIterator := keeper.GetDeposits(ctx, proposalID) @@ -425,7 +425,7 @@ func (keeper Keeper) RefundDeposits(ctx sdk.Context, proposalID int64) { } // Deletes all the deposits on a specific proposal without refunding them -func (keeper Keeper) DeleteDeposits(ctx sdk.Context, proposalID int64) { +func (keeper Keeper) DeleteDeposits(ctx sdk.Context, proposalID uint64) { store := ctx.KVStore(keeper.storeKey) depositsIterator := keeper.GetDeposits(ctx, proposalID) @@ -446,14 +446,14 @@ func (keeper Keeper) ActiveProposalQueueIterator(ctx sdk.Context, endTime time.T } // Inserts a ProposalID into the active proposal queue at endTime -func (keeper Keeper) InsertActiveProposalQueue(ctx sdk.Context, endTime time.Time, proposalID int64) { +func (keeper Keeper) InsertActiveProposalQueue(ctx sdk.Context, endTime time.Time, proposalID uint64) { store := ctx.KVStore(keeper.storeKey) bz := keeper.cdc.MustMarshalBinaryLengthPrefixed(proposalID) store.Set(ActiveProposalQueueProposalKey(endTime, proposalID), bz) } // removes a proposalID from the Active Proposal Queue -func (keeper Keeper) RemoveFromActiveProposalQueue(ctx sdk.Context, endTime time.Time, proposalID int64) { +func (keeper Keeper) RemoveFromActiveProposalQueue(ctx sdk.Context, endTime time.Time, proposalID uint64) { store := ctx.KVStore(keeper.storeKey) store.Set(ActiveProposalQueueProposalKey(endTime, proposalID), nil) } @@ -465,14 +465,14 @@ func (keeper Keeper) InactiveProposalQueueIterator(ctx sdk.Context, endTime time } // Inserts a ProposalID into the inactive proposal queue at endTime -func (keeper Keeper) InsertInactiveProposalQueue(ctx sdk.Context, endTime time.Time, proposalID int64) { +func (keeper Keeper) InsertInactiveProposalQueue(ctx sdk.Context, endTime time.Time, proposalID uint64) { store := ctx.KVStore(keeper.storeKey) bz := keeper.cdc.MustMarshalBinaryLengthPrefixed(proposalID) store.Set(InactiveProposalQueueProposalKey(endTime, proposalID), bz) } // removes a proposalID from the Inactive Proposal Queue -func (keeper Keeper) RemoveFromInactiveProposalQueue(ctx sdk.Context, endTime time.Time, proposalID int64) { +func (keeper Keeper) RemoveFromInactiveProposalQueue(ctx sdk.Context, endTime time.Time, proposalID uint64) { store := ctx.KVStore(keeper.storeKey) store.Set(InactiveProposalQueueProposalKey(endTime, proposalID), nil) } diff --git a/x/gov/keeper_keys.go b/x/gov/keeper_keys.go index bb78ed9b17d2..3cb13de5a050 100644 --- a/x/gov/keeper_keys.go +++ b/x/gov/keeper_keys.go @@ -20,27 +20,27 @@ var ( ) // Key for getting a specific proposal from the store -func KeyProposal(proposalID int64) []byte { +func KeyProposal(proposalID uint64) []byte { return []byte(fmt.Sprintf("proposals:%d", proposalID)) } // Key for getting a specific deposit from the store -func KeyDeposit(proposalID int64, depositerAddr sdk.AccAddress) []byte { +func KeyDeposit(proposalID uint64, depositerAddr sdk.AccAddress) []byte { return []byte(fmt.Sprintf("deposits:%d:%d", proposalID, depositerAddr)) } // Key for getting a specific vote from the store -func KeyVote(proposalID int64, voterAddr sdk.AccAddress) []byte { +func KeyVote(proposalID uint64, voterAddr sdk.AccAddress) []byte { return []byte(fmt.Sprintf("votes:%d:%d", proposalID, voterAddr)) } // Key for getting all deposits on a proposal from the store -func KeyDepositsSubspace(proposalID int64) []byte { +func KeyDepositsSubspace(proposalID uint64) []byte { return []byte(fmt.Sprintf("deposits:%d:", proposalID)) } // Key for getting all votes on a proposal from the store -func KeyVotesSubspace(proposalID int64) []byte { +func KeyVotesSubspace(proposalID uint64) []byte { return []byte(fmt.Sprintf("votes:%d:", proposalID)) } @@ -53,11 +53,11 @@ func ActiveProposalQueueTimePrefix(endTime time.Time) []byte { } // Returns the key for a proposalID in the activeProposalQueue -func ActiveProposalQueueProposalKey(endTime time.Time, proposalID int64) []byte { +func ActiveProposalQueueProposalKey(endTime time.Time, proposalID uint64) []byte { return bytes.Join([][]byte{ PrefixActiveProposalQueue, sdk.FormatTimeBytes(endTime), - sdk.Uint64ToBigEndian(uint64(proposalID)), + sdk.Uint64ToBigEndian(proposalID), }, KeyDelimiter) } @@ -70,10 +70,10 @@ func InactiveProposalQueueTimePrefix(endTime time.Time) []byte { } // Returns the key for a proposalID in the activeProposalQueue -func InactiveProposalQueueProposalKey(endTime time.Time, proposalID int64) []byte { +func InactiveProposalQueueProposalKey(endTime time.Time, proposalID uint64) []byte { return bytes.Join([][]byte{ PrefixInactiveProposalQueue, sdk.FormatTimeBytes(endTime), - sdk.Uint64ToBigEndian(uint64(proposalID)), + sdk.Uint64ToBigEndian(proposalID), }, KeyDelimiter) } diff --git a/x/gov/keeper_test.go b/x/gov/keeper_test.go index b428be1ebf94..2ff1344b7843 100644 --- a/x/gov/keeper_test.go +++ b/x/gov/keeper_test.go @@ -36,7 +36,7 @@ func TestIncrementProposalNumber(t *testing.T) { keeper.NewTextProposal(ctx, "Test", "description", ProposalTypeText) proposal6 := keeper.NewTextProposal(ctx, "Test", "description", ProposalTypeText) - require.Equal(t, int64(6), proposal6.GetProposalID()) + require.Equal(t, uint64(6), proposal6.GetProposalID()) } func TestActivateVotingPeriod(t *testing.T) { @@ -54,7 +54,7 @@ func TestActivateVotingPeriod(t *testing.T) { activeIterator := keeper.ActiveProposalQueueIterator(ctx, proposal.GetVotingEndTime()) require.True(t, activeIterator.Valid()) - var proposalID int64 + var proposalID uint64 keeper.cdc.UnmarshalBinaryLengthPrefixed(activeIterator.Value(), &proposalID) require.Equal(t, proposalID, proposal.GetProposalID()) activeIterator.Close() @@ -214,7 +214,7 @@ func TestProposalQueues(t *testing.T) { inactiveIterator := keeper.InactiveProposalQueueIterator(ctx, proposal.GetDepositEndTime()) require.True(t, inactiveIterator.Valid()) - var proposalID int64 + var proposalID uint64 keeper.cdc.UnmarshalBinaryLengthPrefixed(inactiveIterator.Value(), &proposalID) require.Equal(t, proposalID, proposal.GetProposalID()) inactiveIterator.Close() diff --git a/x/gov/msgs.go b/x/gov/msgs.go index 847c04ab0d7d..b8325d81cae6 100644 --- a/x/gov/msgs.go +++ b/x/gov/msgs.go @@ -84,12 +84,12 @@ func (msg MsgSubmitProposal) GetSigners() []sdk.AccAddress { //----------------------------------------------------------- // MsgDeposit type MsgDeposit struct { - ProposalID int64 `json:"proposal_id"` // ID of the proposal + ProposalID uint64 `json:"proposal_id"` // ID of the proposal Depositer sdk.AccAddress `json:"depositer"` // Address of the depositer Amount sdk.Coins `json:"amount"` // Coins to add to the proposal's deposit } -func NewMsgDeposit(depositer sdk.AccAddress, proposalID int64, amount sdk.Coins) MsgDeposit { +func NewMsgDeposit(depositer sdk.AccAddress, proposalID uint64, amount sdk.Coins) MsgDeposit { return MsgDeposit{ ProposalID: proposalID, Depositer: depositer, @@ -145,12 +145,12 @@ func (msg MsgDeposit) GetSigners() []sdk.AccAddress { //----------------------------------------------------------- // MsgVote type MsgVote struct { - ProposalID int64 `json:"proposal_id"` // ID of the proposal + ProposalID uint64 `json:"proposal_id"` // ID of the proposal Voter sdk.AccAddress `json:"voter"` // address of the voter Option VoteOption `json:"option"` // option from OptionSet chosen by the voter } -func NewMsgVote(voter sdk.AccAddress, proposalID int64, option VoteOption) MsgVote { +func NewMsgVote(voter sdk.AccAddress, proposalID uint64, option VoteOption) MsgVote { return MsgVote{ ProposalID: proposalID, Voter: voter, diff --git a/x/gov/msgs_test.go b/x/gov/msgs_test.go index 6d2b8bb38804..bdc273dcd667 100644 --- a/x/gov/msgs_test.go +++ b/x/gov/msgs_test.go @@ -53,13 +53,12 @@ func TestMsgSubmitProposal(t *testing.T) { func TestMsgDeposit(t *testing.T) { _, addrs, _, _ := mock.CreateGenAccounts(1, sdk.Coins{}) tests := []struct { - proposalID int64 + proposalID uint64 depositerAddr sdk.AccAddress depositAmount sdk.Coins expectPass bool }{ {0, addrs[0], coinsPos, true}, - {-1, addrs[0], coinsPos, false}, {1, sdk.AccAddress{}, coinsPos, false}, {1, addrs[0], coinsZero, true}, {1, addrs[0], coinsNeg, false}, @@ -80,13 +79,12 @@ func TestMsgDeposit(t *testing.T) { func TestMsgVote(t *testing.T) { _, addrs, _, _ := mock.CreateGenAccounts(1, sdk.Coins{}) tests := []struct { - proposalID int64 + proposalID uint64 voterAddr sdk.AccAddress option VoteOption expectPass bool }{ {0, addrs[0], OptionYes, true}, - {-1, addrs[0], OptionYes, false}, {0, sdk.AccAddress{}, OptionYes, false}, {0, addrs[0], OptionNo, true}, {0, addrs[0], OptionNoWithVeto, true}, diff --git a/x/gov/proposals.go b/x/gov/proposals.go index 4e21c8a0e25d..e943fe1169ab 100644 --- a/x/gov/proposals.go +++ b/x/gov/proposals.go @@ -13,8 +13,8 @@ import ( //----------------------------------------------------------- // Proposal interface type Proposal interface { - GetProposalID() int64 - SetProposalID(int64) + GetProposalID() uint64 + SetProposalID(uint64) GetTitle() string SetTitle(string) @@ -68,7 +68,7 @@ func ProposalEqual(proposalA Proposal, proposalB Proposal) bool { //----------------------------------------------------------- // Text Proposals type TextProposal struct { - ProposalID int64 `json:"proposal_id"` // ID of the proposal + ProposalID uint64 `json:"proposal_id"` // ID of the proposal Title string `json:"title"` // Title of the proposal Description string `json:"description"` // Description of the proposal ProposalType ProposalKind `json:"proposal_type"` // Type of proposal. Initial set {PlainTextProposal, SoftwareUpgradeProposal} @@ -88,8 +88,8 @@ type TextProposal struct { var _ Proposal = (*TextProposal)(nil) // nolint -func (tp TextProposal) GetProposalID() int64 { return tp.ProposalID } -func (tp *TextProposal) SetProposalID(proposalID int64) { tp.ProposalID = proposalID } +func (tp TextProposal) GetProposalID() uint64 { return tp.ProposalID } +func (tp *TextProposal) SetProposalID(proposalID uint64) { tp.ProposalID = proposalID } func (tp TextProposal) GetTitle() string { return tp.Title } func (tp *TextProposal) SetTitle(title string) { tp.Title = title } func (tp TextProposal) GetDescription() string { return tp.Description } @@ -119,7 +119,7 @@ func (tp *TextProposal) SetVotingEndTime(votingEndTime time.Time) { //----------------------------------------------------------- // ProposalQueue -type ProposalQueue []int64 +type ProposalQueue []uint64 //----------------------------------------------------------- // ProposalKind diff --git a/x/gov/queryable.go b/x/gov/queryable.go index ae7c6d9c55a9..380490f96cb1 100644 --- a/x/gov/queryable.go +++ b/x/gov/queryable.go @@ -42,7 +42,7 @@ func NewQuerier(keeper Keeper) sdk.Querier { // Params for query 'custom/gov/proposal' type QueryProposalParams struct { - ProposalID int64 + ProposalID uint64 } // nolint: unparam @@ -67,7 +67,7 @@ func queryProposal(ctx sdk.Context, path []string, req abci.RequestQuery, keeper // Params for query 'custom/gov/deposit' type QueryDepositParams struct { - ProposalID int64 + ProposalID uint64 Depositer sdk.AccAddress } @@ -89,7 +89,7 @@ func queryDeposit(ctx sdk.Context, path []string, req abci.RequestQuery, keeper // Params for query 'custom/gov/vote' type QueryVoteParams struct { - ProposalID int64 + ProposalID uint64 Voter sdk.AccAddress } @@ -111,7 +111,7 @@ func queryVote(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Kee // Params for query 'custom/gov/deposits' type QueryDepositsParams struct { - ProposalID int64 + ProposalID uint64 } // nolint: unparam @@ -139,7 +139,7 @@ func queryDeposits(ctx sdk.Context, path []string, req abci.RequestQuery, keeper // Params for query 'custom/gov/votes' type QueryVotesParams struct { - ProposalID int64 + ProposalID uint64 } // nolint: unparam @@ -171,7 +171,7 @@ type QueryProposalsParams struct { Voter sdk.AccAddress Depositer sdk.AccAddress ProposalStatus ProposalStatus - NumLatestProposals int64 + NumLatestProposals uint64 } // nolint: unparam @@ -193,19 +193,21 @@ func queryProposals(ctx sdk.Context, path []string, req abci.RequestQuery, keepe // Params for query 'custom/gov/tally' type QueryTallyParams struct { - ProposalID int64 + ProposalID uint64 } // nolint: unparam func queryTally(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) (res []byte, err sdk.Error) { // TODO: Dependant on #1914 - var proposalID int64 - err2 := keeper.cdc.UnmarshalJSON(req.Data, proposalID) + var params QueryTallyParams + err2 := keeper.cdc.UnmarshalJSON(req.Data, ¶ms) if err2 != nil { return nil, sdk.ErrUnknownRequest(sdk.AppendMsgToErr("incorrectly formatted request data", err2.Error())) } + proposalID := params.ProposalID + proposal := keeper.GetProposal(ctx, proposalID) if proposal == nil { return nil, ErrUnknownProposal(DefaultCodespace, proposalID) diff --git a/x/gov/simulation/msgs.go b/x/gov/simulation/msgs.go index d5ff1881e470..443b9ee2bfe8 100644 --- a/x/gov/simulation/msgs.go +++ b/x/gov/simulation/msgs.go @@ -155,11 +155,11 @@ func SimulateMsgDeposit(k gov.Keeper, sk stake.Keeper) simulation.Operation { // SimulateMsgVote // nolint: unparam func SimulateMsgVote(k gov.Keeper, sk stake.Keeper) simulation.Operation { - return operationSimulateMsgVote(k, sk, simulation.Account{}, -1) + return operationSimulateMsgVote(k, sk, simulation.Account{}, 0) } // nolint: unparam -func operationSimulateMsgVote(k gov.Keeper, sk stake.Keeper, acc simulation.Account, proposalID int64) simulation.Operation { +func operationSimulateMsgVote(k gov.Keeper, sk stake.Keeper, acc simulation.Account, proposalID uint64) simulation.Operation { return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) (action string, fOp []simulation.FutureOperation, err error) { if acc.Equals(simulation.Account{}) { acc = simulation.RandomAcc(r, accs) @@ -200,12 +200,12 @@ func randomDeposit(r *rand.Rand) sdk.Coins { } // Pick a random proposal ID -func randomProposalID(r *rand.Rand, k gov.Keeper, ctx sdk.Context) (proposalID int64, ok bool) { +func randomProposalID(r *rand.Rand, k gov.Keeper, ctx sdk.Context) (proposalID uint64, ok bool) { lastProposalID := k.GetLastProposalID(ctx) if lastProposalID < 1 { return 0, false } - proposalID = int64(r.Intn(int(lastProposalID))) + proposalID = uint64(r.Intn(int(lastProposalID))) return proposalID, true } From b853c97f9391abe90f2242a6071cf64dc65d1d33 Mon Sep 17 00:00:00 2001 From: Sunny Aggarwal Date: Mon, 5 Nov 2018 22:04:10 -0800 Subject: [PATCH 08/16] renamed Gov Procedures to Params --- cmd/gaia/app/sim_test.go | 6 ++-- x/gov/endblocker_test.go | 6 ++-- x/gov/genesis.go | 40 ++++++++++----------- x/gov/handler.go | 2 +- x/gov/keeper.go | 58 +++++++++++++++--------------- x/gov/{procedures.go => params.go} | 6 ++-- x/gov/simulation/msgs.go | 2 +- x/gov/tally.go | 6 ++-- 8 files changed, 63 insertions(+), 63 deletions(-) rename x/gov/{procedures.go => params.go} (91%) diff --git a/cmd/gaia/app/sim_test.go b/cmd/gaia/app/sim_test.go index 80cbd7d0b333..0c6d35cb413d 100644 --- a/cmd/gaia/app/sim_test.go +++ b/cmd/gaia/app/sim_test.go @@ -71,14 +71,14 @@ func appStateFn(r *rand.Rand, accs []simulation.Account) json.RawMessage { // Random genesis states govGenesis := gov.GenesisState{ StartingProposalID: uint64(r.Intn(100)), - DepositProcedure: gov.DepositProcedure{ + DepositParams: gov.DepositParams{ MinDeposit: sdk.Coins{sdk.NewInt64Coin("steak", int64(r.Intn(1e3)))}, MaxDepositPeriod: time.Duration(r.Intn(2*172800)) * time.Second, }, - VotingProcedure: gov.VotingProcedure{ + VotingParams: gov.VotingParams{ VotingPeriod: time.Duration(r.Intn(2*172800)) * time.Second, }, - TallyingProcedure: gov.TallyingProcedure{ + TallyParams: gov.TallyParams{ Threshold: sdk.NewDecWithPrec(5, 1), Veto: sdk.NewDecWithPrec(334, 3), GovernancePenalty: sdk.NewDecWithPrec(1, 2), diff --git a/x/gov/endblocker_test.go b/x/gov/endblocker_test.go index 07b3e89b6c07..535cf036a5f9 100644 --- a/x/gov/endblocker_test.go +++ b/x/gov/endblocker_test.go @@ -38,7 +38,7 @@ func TestTickExpiredDepositPeriod(t *testing.T) { inactiveQueue.Close() newHeader = ctx.BlockHeader() - newHeader.Time = ctx.BlockHeader().Time.Add(keeper.GetDepositProcedure(ctx).MaxDepositPeriod) + newHeader.Time = ctx.BlockHeader().Time.Add(keeper.GetDepositParams(ctx).MaxDepositPeriod) ctx = ctx.WithBlockHeader(newHeader) inactiveQueue = keeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) @@ -84,7 +84,7 @@ func TestTickMultipleExpiredDepositPeriod(t *testing.T) { require.True(t, res.IsOK()) newHeader = ctx.BlockHeader() - newHeader.Time = ctx.BlockHeader().Time.Add(keeper.GetDepositProcedure(ctx).MaxDepositPeriod).Add(time.Duration(-1) * time.Second) + newHeader.Time = ctx.BlockHeader().Time.Add(keeper.GetDepositParams(ctx).MaxDepositPeriod).Add(time.Duration(-1) * time.Second) ctx = ctx.WithBlockHeader(newHeader) inactiveQueue = keeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) @@ -179,7 +179,7 @@ func TestTickPassedVotingPeriod(t *testing.T) { require.True(t, res.IsOK()) newHeader = ctx.BlockHeader() - newHeader.Time = ctx.BlockHeader().Time.Add(keeper.GetDepositProcedure(ctx).MaxDepositPeriod).Add(keeper.GetVotingProcedure(ctx).VotingPeriod) + newHeader.Time = ctx.BlockHeader().Time.Add(keeper.GetDepositParams(ctx).MaxDepositPeriod).Add(keeper.GetVotingParams(ctx).VotingPeriod) ctx = ctx.WithBlockHeader(newHeader) inactiveQueue = keeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time) diff --git a/x/gov/genesis.go b/x/gov/genesis.go index e7023848acea..db35f68c8c4e 100644 --- a/x/gov/genesis.go +++ b/x/gov/genesis.go @@ -8,18 +8,18 @@ import ( // GenesisState - all staking state that must be provided at genesis type GenesisState struct { - StartingProposalID uint64 `json:"starting_proposalID"` - DepositProcedure DepositProcedure `json:"deposit_period"` - VotingProcedure VotingProcedure `json:"voting_period"` - TallyingProcedure TallyingProcedure `json:"tallying_procedure"` + StartingProposalID uint64 `json:"starting_proposalID"` + DepositParams DepositParams `json:"deposit_params"` + VotingParams VotingParams `json:"voting_params"` + TallyParams TallyParams `json:"tally_params"` } -func NewGenesisState(startingProposalID uint64, dp DepositProcedure, vp VotingProcedure, tp TallyingProcedure) GenesisState { +func NewGenesisState(startingProposalID uint64, dp DepositParams, vp VotingParams, tp TallyParams) GenesisState { return GenesisState{ StartingProposalID: startingProposalID, - DepositProcedure: dp, - VotingProcedure: vp, - TallyingProcedure: tp, + DepositParams: dp, + VotingParams: vp, + TallyParams: tp, } } @@ -27,14 +27,14 @@ func NewGenesisState(startingProposalID uint64, dp DepositProcedure, vp VotingPr func DefaultGenesisState() GenesisState { return GenesisState{ StartingProposalID: 1, - DepositProcedure: DepositProcedure{ + DepositParams: DepositParams{ MinDeposit: sdk.Coins{sdk.NewInt64Coin("steak", 10)}, MaxDepositPeriod: time.Duration(172800) * time.Second, }, - VotingProcedure: VotingProcedure{ + VotingParams: VotingParams{ VotingPeriod: time.Duration(172800) * time.Second, }, - TallyingProcedure: TallyingProcedure{ + TallyParams: TallyParams{ Threshold: sdk.NewDecWithPrec(5, 1), Veto: sdk.NewDecWithPrec(334, 3), GovernancePenalty: sdk.NewDecWithPrec(1, 2), @@ -49,22 +49,22 @@ func InitGenesis(ctx sdk.Context, k Keeper, data GenesisState) { // TODO: Handle this with #870 panic(err) } - k.setDepositProcedure(ctx, data.DepositProcedure) - k.setVotingProcedure(ctx, data.VotingProcedure) - k.setTallyingProcedure(ctx, data.TallyingProcedure) + k.setDepositParams(ctx, data.DepositParams) + k.setVotingParams(ctx, data.VotingParams) + k.setTallyParams(ctx, data.TallyParams) } // WriteGenesis - output genesis parameters func WriteGenesis(ctx sdk.Context, k Keeper) GenesisState { startingProposalID, _ := k.getNewProposalID(ctx) - depositProcedure := k.GetDepositProcedure(ctx) - votingProcedure := k.GetVotingProcedure(ctx) - tallyingProcedure := k.GetTallyingProcedure(ctx) + depositParams := k.GetDepositParams(ctx) + votingParams := k.GetVotingParams(ctx) + tallyParams := k.GetTallyParams(ctx) return GenesisState{ StartingProposalID: startingProposalID, - DepositProcedure: depositProcedure, - VotingProcedure: votingProcedure, - TallyingProcedure: tallyingProcedure, + DepositParams: depositParams, + VotingParams: votingParams, + TallyParams: tallyParams, } } diff --git a/x/gov/handler.go b/x/gov/handler.go index 5b4628f01640..68a9c7b719d5 100644 --- a/x/gov/handler.go +++ b/x/gov/handler.go @@ -117,7 +117,7 @@ func EndBlocker(ctx sdk.Context, keeper Keeper) (resTags sdk.Tags) { fmt.Sprintf("proposal %d (%s) didn't meet minimum deposit of %s (had only %s); deleted", inactiveProposal.GetProposalID(), inactiveProposal.GetTitle(), - keeper.GetDepositProcedure(ctx).MinDeposit, + keeper.GetDepositParams(ctx).MinDeposit, inactiveProposal.GetTotalDeposit(), ), ) diff --git a/x/gov/keeper.go b/x/gov/keeper.go index e1acaa5d85e1..1158167fa92d 100644 --- a/x/gov/keeper.go +++ b/x/gov/keeper.go @@ -16,17 +16,17 @@ const ( // Parameter store key var ( - ParamStoreKeyDepositProcedure = []byte("depositprocedure") - ParamStoreKeyVotingProcedure = []byte("votingprocedure") - ParamStoreKeyTallyingProcedure = []byte("tallyingprocedure") + ParamStoreKeyDepositParams = []byte("depositparams") + ParamStoreKeyVotingParams = []byte("votingparams") + ParamStoreKeyTallyParams = []byte("tallyparams") ) // Type declaration for parameters func ParamTypeTable() params.TypeTable { return params.NewTypeTable( - ParamStoreKeyDepositProcedure, DepositProcedure{}, - ParamStoreKeyVotingProcedure, VotingProcedure{}, - ParamStoreKeyTallyingProcedure, TallyingProcedure{}, + ParamStoreKeyDepositParams, DepositParams{}, + ParamStoreKeyVotingParams, VotingParams{}, + ParamStoreKeyTallyParams, TallyParams{}, ) } @@ -95,7 +95,7 @@ func (keeper Keeper) NewTextProposal(ctx sdk.Context, title string, description SubmitTime: ctx.BlockHeader().Time, } - depositPeriod := keeper.GetDepositProcedure(ctx).MaxDepositPeriod + depositPeriod := keeper.GetDepositParams(ctx).MaxDepositPeriod proposal.SetDepositEndTime(proposal.GetSubmitTime().Add(depositPeriod)) keeper.SetProposal(ctx, proposal) @@ -225,7 +225,7 @@ func (keeper Keeper) peekCurrentProposalID(ctx sdk.Context) (proposalID uint64, func (keeper Keeper) activateVotingPeriod(ctx sdk.Context, proposal Proposal) { proposal.SetVotingStartTime(ctx.BlockHeader().Time) - votingPeriod := keeper.GetVotingProcedure(ctx).VotingPeriod + votingPeriod := keeper.GetVotingParams(ctx).VotingPeriod proposal.SetVotingEndTime(proposal.GetVotingStartTime().Add(votingPeriod)) proposal.SetStatus(StatusVotingPeriod) keeper.SetProposal(ctx, proposal) @@ -235,45 +235,45 @@ func (keeper Keeper) activateVotingPeriod(ctx sdk.Context, proposal Proposal) { } // ===================================================== -// Procedures +// Params -// Returns the current Deposit Procedure from the global param store +// Returns the current DepositParams from the global param store // nolint: errcheck -func (keeper Keeper) GetDepositProcedure(ctx sdk.Context) DepositProcedure { - var depositProcedure DepositProcedure - keeper.paramSpace.Get(ctx, ParamStoreKeyDepositProcedure, &depositProcedure) - return depositProcedure +func (keeper Keeper) GetDepositParams(ctx sdk.Context) DepositParams { + var depositParams DepositParams + keeper.paramSpace.Get(ctx, ParamStoreKeyDepositParams, &depositParams) + return depositParams } // Returns the current Voting Procedure from the global param store // nolint: errcheck -func (keeper Keeper) GetVotingProcedure(ctx sdk.Context) VotingProcedure { - var votingProcedure VotingProcedure - keeper.paramSpace.Get(ctx, ParamStoreKeyVotingProcedure, &votingProcedure) - return votingProcedure +func (keeper Keeper) GetVotingParams(ctx sdk.Context) VotingParams { + var votingParams VotingParams + keeper.paramSpace.Get(ctx, ParamStoreKeyVotingParams, &votingParams) + return votingParams } // Returns the current Tallying Procedure from the global param store // nolint: errcheck -func (keeper Keeper) GetTallyingProcedure(ctx sdk.Context) TallyingProcedure { - var tallyingProcedure TallyingProcedure - keeper.paramSpace.Get(ctx, ParamStoreKeyTallyingProcedure, &tallyingProcedure) - return tallyingProcedure +func (keeper Keeper) GetTallyParams(ctx sdk.Context) TallyParams { + var tallyParams TallyParams + keeper.paramSpace.Get(ctx, ParamStoreKeyTallyParams, &tallyParams) + return tallyParams } // nolint: errcheck -func (keeper Keeper) setDepositProcedure(ctx sdk.Context, depositProcedure DepositProcedure) { - keeper.paramSpace.Set(ctx, ParamStoreKeyDepositProcedure, &depositProcedure) +func (keeper Keeper) setDepositParams(ctx sdk.Context, depositParams DepositParams) { + keeper.paramSpace.Set(ctx, ParamStoreKeyDepositParams, &depositParams) } // nolint: errcheck -func (keeper Keeper) setVotingProcedure(ctx sdk.Context, votingProcedure VotingProcedure) { - keeper.paramSpace.Set(ctx, ParamStoreKeyVotingProcedure, &votingProcedure) +func (keeper Keeper) setVotingParams(ctx sdk.Context, votingParams VotingParams) { + keeper.paramSpace.Set(ctx, ParamStoreKeyVotingParams, &votingParams) } // nolint: errcheck -func (keeper Keeper) setTallyingProcedure(ctx sdk.Context, tallyingProcedure TallyingProcedure) { - keeper.paramSpace.Set(ctx, ParamStoreKeyTallyingProcedure, &tallyingProcedure) +func (keeper Keeper) setTallyParams(ctx sdk.Context, tallyParams TallyParams) { + keeper.paramSpace.Set(ctx, ParamStoreKeyTallyParams, &tallyParams) } // ===================================================== @@ -380,7 +380,7 @@ func (keeper Keeper) AddDeposit(ctx sdk.Context, proposalID uint64, depositerAdd // Check if deposit tipped proposal into voting period // Active voting period if so activatedVotingPeriod := false - if proposal.GetStatus() == StatusDepositPeriod && proposal.GetTotalDeposit().IsGTE(keeper.GetDepositProcedure(ctx).MinDeposit) { + if proposal.GetStatus() == StatusDepositPeriod && proposal.GetTotalDeposit().IsGTE(keeper.GetDepositParams(ctx).MinDeposit) { keeper.activateVotingPeriod(ctx, proposal) activatedVotingPeriod = true } diff --git a/x/gov/procedures.go b/x/gov/params.go similarity index 91% rename from x/gov/procedures.go rename to x/gov/params.go index e453add791c9..4d8126d14137 100644 --- a/x/gov/procedures.go +++ b/x/gov/params.go @@ -7,19 +7,19 @@ import ( ) // Procedure around Deposits for governance -type DepositProcedure struct { +type DepositParams struct { MinDeposit sdk.Coins `json:"min_deposit"` // Minimum deposit for a proposal to enter voting period. MaxDepositPeriod time.Duration `json:"max_deposit_period"` // Maximum period for Atom holders to deposit on a proposal. Initial value: 2 months } // Procedure around Tallying votes in governance -type TallyingProcedure struct { +type TallyParams struct { Threshold sdk.Dec `json:"threshold"` // Minimum propotion of Yes votes for proposal to pass. Initial value: 0.5 Veto sdk.Dec `json:"veto"` // Minimum value of Veto votes to Total votes ratio for proposal to be vetoed. Initial value: 1/3 GovernancePenalty sdk.Dec `json:"governance_penalty"` // Penalty if validator does not vote } // Procedure around Voting in governance -type VotingProcedure struct { +type VotingParams struct { VotingPeriod time.Duration `json:"voting_period"` // Length of the voting period. } diff --git a/x/gov/simulation/msgs.go b/x/gov/simulation/msgs.go index 443b9ee2bfe8..bba834a3c8bf 100644 --- a/x/gov/simulation/msgs.go +++ b/x/gov/simulation/msgs.go @@ -64,7 +64,7 @@ func SimulateSubmittingVotingAndSlashingForProposal(k gov.Keeper, sk stake.Keepe whoVotes := r.Perm(len(accs)) // didntVote := whoVotes[numVotes:] whoVotes = whoVotes[:numVotes] - votingPeriod := k.GetVotingProcedure(ctx).VotingPeriod + votingPeriod := k.GetVotingParams(ctx).VotingPeriod fops := make([]simulation.FutureOperation, numVotes+1) for i := 0; i < numVotes; i++ { whenVote := ctx.BlockHeader().Time.Add(time.Duration(r.Int63n(int64(votingPeriod.Seconds()))) * time.Second) diff --git a/x/gov/tally.go b/x/gov/tally.go index e839bbe039c0..d66237762ead 100644 --- a/x/gov/tally.go +++ b/x/gov/tally.go @@ -84,7 +84,7 @@ func tally(ctx sdk.Context, keeper Keeper, proposal Proposal) (passes bool, tall totalVotingPower = totalVotingPower.Add(votingPower) } - tallyingProcedure := keeper.GetTallyingProcedure(ctx) + tallyParams := keeper.GetTallyParams(ctx) tallyResults = TallyResult{ Yes: results[OptionYes], @@ -98,11 +98,11 @@ func tally(ctx sdk.Context, keeper Keeper, proposal Proposal) (passes bool, tall return false, tallyResults } // If more than 1/3 of voters veto, proposal fails - if results[OptionNoWithVeto].Quo(totalVotingPower).GT(tallyingProcedure.Veto) { + if results[OptionNoWithVeto].Quo(totalVotingPower).GT(tallyParams.Veto) { return false, tallyResults } // If more than 1/2 of non-abstaining voters vote Yes, proposal passes - if results[OptionYes].Quo(totalVotingPower.Sub(results[OptionAbstain])).GT(tallyingProcedure.Threshold) { + if results[OptionYes].Quo(totalVotingPower.Sub(results[OptionAbstain])).GT(tallyParams.Threshold) { return true, tallyResults } // If more than 1/2 of non-abstaining voters vote No, proposal fails From 1d3e54dfb417f662af8f7c1259b50f9080fc3d96 Mon Sep 17 00:00:00 2001 From: Sunny Aggarwal Date: Mon, 5 Nov 2018 22:14:36 -0800 Subject: [PATCH 09/16] s/ActiveProposalQueueProposalKey/KeyActiveProposalQueueProposal/g --- x/gov/keeper.go | 12 ++++++------ x/gov/keeper_keys.go | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/x/gov/keeper.go b/x/gov/keeper.go index 1158167fa92d..472ab3340e61 100644 --- a/x/gov/keeper.go +++ b/x/gov/keeper.go @@ -442,37 +442,37 @@ func (keeper Keeper) DeleteDeposits(ctx sdk.Context, proposalID uint64) { // Returns an iterator for all the proposals in the Active Queue that expire by endTime func (keeper Keeper) ActiveProposalQueueIterator(ctx sdk.Context, endTime time.Time) sdk.Iterator { store := ctx.KVStore(keeper.storeKey) - return store.Iterator(PrefixActiveProposalQueue, sdk.PrefixEndBytes(ActiveProposalQueueTimePrefix(endTime))) + return store.Iterator(PrefixActiveProposalQueue, sdk.PrefixEndBytes(PrefixActiveProposalQueueTime(endTime))) } // Inserts a ProposalID into the active proposal queue at endTime func (keeper Keeper) InsertActiveProposalQueue(ctx sdk.Context, endTime time.Time, proposalID uint64) { store := ctx.KVStore(keeper.storeKey) bz := keeper.cdc.MustMarshalBinaryLengthPrefixed(proposalID) - store.Set(ActiveProposalQueueProposalKey(endTime, proposalID), bz) + store.Set(KeyActiveProposalQueueProposal(endTime, proposalID), bz) } // removes a proposalID from the Active Proposal Queue func (keeper Keeper) RemoveFromActiveProposalQueue(ctx sdk.Context, endTime time.Time, proposalID uint64) { store := ctx.KVStore(keeper.storeKey) - store.Set(ActiveProposalQueueProposalKey(endTime, proposalID), nil) + store.Set(KeyActiveProposalQueueProposal(endTime, proposalID), nil) } // Returns an iterator for all the proposals in the Inactive Queue that expire by endTime func (keeper Keeper) InactiveProposalQueueIterator(ctx sdk.Context, endTime time.Time) sdk.Iterator { store := ctx.KVStore(keeper.storeKey) - return store.Iterator(PrefixInactiveProposalQueue, sdk.PrefixEndBytes(InactiveProposalQueueTimePrefix(endTime))) + return store.Iterator(PrefixInactiveProposalQueue, sdk.PrefixEndBytes(PrefixInactiveProposalQueueTime(endTime))) } // Inserts a ProposalID into the inactive proposal queue at endTime func (keeper Keeper) InsertInactiveProposalQueue(ctx sdk.Context, endTime time.Time, proposalID uint64) { store := ctx.KVStore(keeper.storeKey) bz := keeper.cdc.MustMarshalBinaryLengthPrefixed(proposalID) - store.Set(InactiveProposalQueueProposalKey(endTime, proposalID), bz) + store.Set(KeyInactiveProposalQueueProposal(endTime, proposalID), bz) } // removes a proposalID from the Inactive Proposal Queue func (keeper Keeper) RemoveFromInactiveProposalQueue(ctx sdk.Context, endTime time.Time, proposalID uint64) { store := ctx.KVStore(keeper.storeKey) - store.Set(InactiveProposalQueueProposalKey(endTime, proposalID), nil) + store.Set(KeyInactiveProposalQueueProposal(endTime, proposalID), nil) } diff --git a/x/gov/keeper_keys.go b/x/gov/keeper_keys.go index 3cb13de5a050..158a0bcc846b 100644 --- a/x/gov/keeper_keys.go +++ b/x/gov/keeper_keys.go @@ -45,7 +45,7 @@ func KeyVotesSubspace(proposalID uint64) []byte { } // Returns the key for a proposalID in the activeProposalQueue -func ActiveProposalQueueTimePrefix(endTime time.Time) []byte { +func PrefixActiveProposalQueueTime(endTime time.Time) []byte { return bytes.Join([][]byte{ PrefixActiveProposalQueue, sdk.FormatTimeBytes(endTime), @@ -53,7 +53,7 @@ func ActiveProposalQueueTimePrefix(endTime time.Time) []byte { } // Returns the key for a proposalID in the activeProposalQueue -func ActiveProposalQueueProposalKey(endTime time.Time, proposalID uint64) []byte { +func KeyActiveProposalQueueProposal(endTime time.Time, proposalID uint64) []byte { return bytes.Join([][]byte{ PrefixActiveProposalQueue, sdk.FormatTimeBytes(endTime), @@ -62,7 +62,7 @@ func ActiveProposalQueueProposalKey(endTime time.Time, proposalID uint64) []byte } // Returns the key for a proposalID in the activeProposalQueue -func InactiveProposalQueueTimePrefix(endTime time.Time) []byte { +func PrefixInactiveProposalQueueTime(endTime time.Time) []byte { return bytes.Join([][]byte{ PrefixInactiveProposalQueue, sdk.FormatTimeBytes(endTime), @@ -70,7 +70,7 @@ func InactiveProposalQueueTimePrefix(endTime time.Time) []byte { } // Returns the key for a proposalID in the activeProposalQueue -func InactiveProposalQueueProposalKey(endTime time.Time, proposalID uint64) []byte { +func KeyInactiveProposalQueueProposal(endTime time.Time, proposalID uint64) []byte { return bytes.Join([][]byte{ PrefixInactiveProposalQueue, sdk.FormatTimeBytes(endTime), From f456c04dee3936d42b6ec866c3bd8890ef8914d8 Mon Sep 17 00:00:00 2001 From: Sunny Aggarwal Date: Mon, 5 Nov 2018 22:30:06 -0800 Subject: [PATCH 10/16] address comments --- docs/spec/governance/state.md | 16 +++++++--------- docs/spec/governance/transactions.md | 20 +++++--------------- x/gov/keeper_keys.go | 2 +- 3 files changed, 13 insertions(+), 25 deletions(-) diff --git a/docs/spec/governance/state.md b/docs/spec/governance/state.md index 6335ddad68cc..ca130bd4c374 100644 --- a/docs/spec/governance/state.md +++ b/docs/spec/governance/state.md @@ -98,11 +98,11 @@ type Proposal struct { Deposits []Deposit // List of deposits on the proposal SubmitTime time.Time // Time of the block where TxGovSubmitProposal was included DepositEndTime time.Time // Time that the DepositPeriod of a proposal would expire - Submitter sdk.Address // Address of the submitter + Submitter sdk.AccAddress // Address of the submitter - VotingStartTime time.Time // Time of the block where MinDeposit was reached. time.Time{} if MinDeposit is not reached - VotingEndTime time.Time // Time of the block that the VotingPeriod for a proposal will end. - CurrentStatus ProposalStatus // Current status of the proposal + VotingStartTime time.Time // Time of the block where MinDeposit was reached. time.Time{} if MinDeposit is not reached + VotingEndTime time.Time // Time of the block that the VotingPeriod for a proposal will end. + CurrentStatus ProposalStatus // Current status of the proposal YesVotes sdk.Dec NoVotes sdk.Dec @@ -146,18 +146,16 @@ And the pseudocode for the `ProposalProcessingQueue`: ```go in EndBlock do - - votingProcedure = load(GlobalParams, 'VotingProcedure') for finishedProposalID in GetAllFinishedProposalIDs(block.Time) proposal = load(Governance, ) // proposal is a const key validators = Keeper.getAllValidators() - tmpValMap := map(sdk.Address)ValidatorGovInfo + tmpValMap := map(sdk.AccAddress)ValidatorGovInfo - // Initiate mapping at 0. Validators that remain at 0 at the end of tally will be punished + // Initiate mapping at 0. This is the amount of shares of the validator's vote that will be overridden by their delegator's votes for each validator in validators - tmpValMap(validator).Minus = 0 + tmpValMap(validator.OperatorAddr).Minus = 0 // Tally voterIterator = rangeQuery(Governance, ) //return all the addresses that voted on the proposal diff --git a/docs/spec/governance/transactions.md b/docs/spec/governance/transactions.md index 5ce91284fdc1..f3636facda75 100644 --- a/docs/spec/governance/transactions.md +++ b/docs/spec/governance/transactions.md @@ -45,6 +45,8 @@ upon receiving txGovSubmitProposal from sender do if (txGovSubmitProposal.Type != ProposalTypePlainText) OR (txGovSubmitProposal.Type != ProposalTypeSoftwareUpgrade) sender.AtomBalance -= initialDeposit.Atoms + + depositProcedure = load(GlobalParams, 'DepositProcedure') proposalID = generate new proposalID proposal = NewProposal() @@ -53,27 +55,15 @@ upon receiving txGovSubmitProposal from sender do proposal.Description = txGovSubmitProposal.Description proposal.Type = txGovSubmitProposal.Type proposal.TotalDeposit = initialDeposit - proposal.SubmitBlock = CurrentBlock + proposal.SubmitTime = + proposal.DepositEndTime = .Add(depositProcedure.MaxDepositPeriod) proposal.Deposits.append({initialDeposit, sender}) proposal.Submitter = sender proposal.YesVotes = 0 proposal.NoVotes = 0 proposal.NoWithVetoVotes = 0 proposal.AbstainVotes = 0 - - depositProcedure = load(GlobalParams, 'DepositProcedure') - - if (initialDeposit < depositProcedure.MinDeposit) - // MinDeposit is not reached - - proposal.CurrentStatus = ProposalStatusOpen - - else - // MinDeposit is reached - - proposal.CurrentStatus = ProposalStatusActive - proposal.VotingStartBlock = CurrentBlock - ProposalProcessingQueue.push(proposalID) + proposal.CurrentStatus = ProposalStatusOpen store(Proposals, , proposal) // Store proposal in Proposals mapping return proposalID diff --git a/x/gov/keeper_keys.go b/x/gov/keeper_keys.go index 158a0bcc846b..8a3324fd1835 100644 --- a/x/gov/keeper_keys.go +++ b/x/gov/keeper_keys.go @@ -12,7 +12,7 @@ import ( // Key for getting a the next available proposalID from the store var ( - KeyDelimiter = []byte("/") + KeyDelimiter = []byte(":") KeyNextProposalID = []byte("newProposalID") PrefixActiveProposalQueue = []byte("activeProposalQueue") From ed5640704870c71808ec91f394f1db4c5457c6c8 Mon Sep 17 00:00:00 2001 From: Sunny Aggarwal Date: Mon, 5 Nov 2018 22:57:21 -0800 Subject: [PATCH 11/16] numLatestProposals -> Limit --- x/gov/client/cli/tx.go | 28 ++++++++++++++-------------- x/gov/client/rest/rest.go | 10 +++++----- x/gov/queryable.go | 10 +++++----- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/x/gov/client/cli/tx.go b/x/gov/client/cli/tx.go index 31072c54a292..455bd58db55e 100644 --- a/x/gov/client/cli/tx.go +++ b/x/gov/client/cli/tx.go @@ -21,17 +21,17 @@ import ( ) const ( - flagProposalID = "proposal-id" - flagTitle = "title" - flagDescription = "description" - flagProposalType = "type" - flagDeposit = "deposit" - flagVoter = "voter" - flagOption = "option" - flagDepositer = "depositer" - flagStatus = "status" - flagLatestProposalIDs = "latest" - flagProposal = "proposal" + flagProposalID = "proposal-id" + flagTitle = "title" + flagDescription = "description" + flagProposalType = "type" + flagDeposit = "deposit" + flagVoter = "voter" + flagOption = "option" + flagDepositer = "depositer" + flagStatus = "status" + flagNumLimit = "limit" + flagProposal = "proposal" ) type proposal struct { @@ -291,10 +291,10 @@ func GetCmdQueryProposals(queryRoute string, cdc *codec.Codec) *cobra.Command { bechDepositerAddr := viper.GetString(flagDepositer) bechVoterAddr := viper.GetString(flagVoter) strProposalStatus := viper.GetString(flagStatus) - latestProposalsIDs := uint64(viper.GetInt64(flagLatestProposalIDs)) + numLimit := uint64(viper.GetInt64(flagNumLimit)) params := gov.QueryProposalsParams{ - NumLatestProposals: latestProposalsIDs, + Limit: numLimit, } if len(bechDepositerAddr) != 0 { @@ -352,7 +352,7 @@ func GetCmdQueryProposals(queryRoute string, cdc *codec.Codec) *cobra.Command { }, } - cmd.Flags().String(flagLatestProposalIDs, "", "(optional) limit to latest [number] proposals. Defaults to all proposals") + cmd.Flags().String(flagNumLimit, "", "(optional) limit to latest [number] proposals. Defaults to all proposals") cmd.Flags().String(flagDepositer, "", "(optional) filter by proposals deposited on by depositer") cmd.Flags().String(flagVoter, "", "(optional) filter by proposals voted on by voted") cmd.Flags().String(flagStatus, "", "(optional) filter proposals by proposal status, status: deposit_period/voting_period/passed/rejected") diff --git a/x/gov/client/rest/rest.go b/x/gov/client/rest/rest.go index f5a92d110737..f477f2ff9a29 100644 --- a/x/gov/client/rest/rest.go +++ b/x/gov/client/rest/rest.go @@ -22,7 +22,7 @@ const ( RestDepositer = "depositer" RestVoter = "voter" RestProposalStatus = "status" - RestNumLatest = "latest" + RestNumLimit = "limit" storeName = "gov" ) @@ -416,7 +416,7 @@ func queryProposalsWithParameterFn(cdc *codec.Codec, cliCtx context.CLIContext) bechVoterAddr := r.URL.Query().Get(RestVoter) bechDepositerAddr := r.URL.Query().Get(RestDepositer) strProposalStatus := r.URL.Query().Get(RestProposalStatus) - strNumLatest := r.URL.Query().Get(RestNumLatest) + strNumLimit := r.URL.Query().Get(RestNumLimit) params := gov.QueryProposalsParams{} @@ -446,12 +446,12 @@ func queryProposalsWithParameterFn(cdc *codec.Codec, cliCtx context.CLIContext) } params.ProposalStatus = proposalStatus } - if len(strNumLatest) != 0 { - numLatest, ok := utils.ParseUint64OrReturnBadRequest(w, strNumLatest) + if len(strNumLimit) != 0 { + numLimit, ok := utils.ParseUint64OrReturnBadRequest(w, strNumLimit) if !ok { return } - params.NumLatestProposals = numLatest + params.Limit = numLimit } bz, err := cdc.MarshalJSON(params) diff --git a/x/gov/queryable.go b/x/gov/queryable.go index 380490f96cb1..3e70d43aa055 100644 --- a/x/gov/queryable.go +++ b/x/gov/queryable.go @@ -168,10 +168,10 @@ func queryVotes(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Ke // Params for query 'custom/gov/proposals' type QueryProposalsParams struct { - Voter sdk.AccAddress - Depositer sdk.AccAddress - ProposalStatus ProposalStatus - NumLatestProposals uint64 + Voter sdk.AccAddress + Depositer sdk.AccAddress + ProposalStatus ProposalStatus + Limit uint64 } // nolint: unparam @@ -182,7 +182,7 @@ func queryProposals(ctx sdk.Context, path []string, req abci.RequestQuery, keepe return nil, sdk.ErrUnknownRequest(sdk.AppendMsgToErr("incorrectly formatted request data", err2.Error())) } - proposals := keeper.GetProposalsFiltered(ctx, params.Voter, params.Depositer, params.ProposalStatus, params.NumLatestProposals) + proposals := keeper.GetProposalsFiltered(ctx, params.Voter, params.Depositer, params.ProposalStatus, params.Limit) bz, err2 := codec.MarshalJSONIndent(keeper.cdc, proposals) if err2 != nil { From 4d2bbd397c62ddc57cc6206774eac59d57003662 Mon Sep 17 00:00:00 2001 From: Sunny Aggarwal Date: Mon, 5 Nov 2018 23:35:56 -0800 Subject: [PATCH 12/16] delete key properly and close activeIterator --- x/gov/handler.go | 1 + x/gov/keeper.go | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/x/gov/handler.go b/x/gov/handler.go index 68a9c7b719d5..279c4cc6e63a 100644 --- a/x/gov/handler.go +++ b/x/gov/handler.go @@ -152,6 +152,7 @@ func EndBlocker(ctx sdk.Context, keeper Keeper) (resTags sdk.Tags) { resTags = resTags.AppendTag(tags.Action, action) resTags = resTags.AppendTag(tags.ProposalID, []byte(string(proposalID))) } + activeIterator.Close() return resTags } diff --git a/x/gov/keeper.go b/x/gov/keeper.go index 472ab3340e61..7ed8f9431a40 100644 --- a/x/gov/keeper.go +++ b/x/gov/keeper.go @@ -455,7 +455,7 @@ func (keeper Keeper) InsertActiveProposalQueue(ctx sdk.Context, endTime time.Tim // removes a proposalID from the Active Proposal Queue func (keeper Keeper) RemoveFromActiveProposalQueue(ctx sdk.Context, endTime time.Time, proposalID uint64) { store := ctx.KVStore(keeper.storeKey) - store.Set(KeyActiveProposalQueueProposal(endTime, proposalID), nil) + store.Delete(KeyActiveProposalQueueProposal(endTime, proposalID)) } // Returns an iterator for all the proposals in the Inactive Queue that expire by endTime @@ -474,5 +474,5 @@ func (keeper Keeper) InsertInactiveProposalQueue(ctx sdk.Context, endTime time.T // removes a proposalID from the Inactive Proposal Queue func (keeper Keeper) RemoveFromInactiveProposalQueue(ctx sdk.Context, endTime time.Time, proposalID uint64) { store := ctx.KVStore(keeper.storeKey) - store.Set(KeyInactiveProposalQueueProposal(endTime, proposalID), nil) + store.Delete(KeyInactiveProposalQueueProposal(endTime, proposalID)) } From ed2e1c8c500d5619a2e5608d43aa54857f6870f6 Mon Sep 17 00:00:00 2001 From: Sunny Aggarwal Date: Tue, 6 Nov 2018 11:35:46 -0800 Subject: [PATCH 13/16] fixed staking invariant breakage because of gov deposits --- cmd/gaia/app/sim_test.go | 2 +- x/gov/keeper.go | 10 ++++++--- x/gov/simulation/msgs.go | 39 +++++++++++------------------------- x/gov/simulation/sim_test.go | 8 ++++---- 4 files changed, 24 insertions(+), 35 deletions(-) diff --git a/cmd/gaia/app/sim_test.go b/cmd/gaia/app/sim_test.go index 0c6d35cb413d..431c5bc41550 100644 --- a/cmd/gaia/app/sim_test.go +++ b/cmd/gaia/app/sim_test.go @@ -166,7 +166,7 @@ func testAndRunTxs(app *GaiaApp) []simulation.WeightedOperation { {50, distrsim.SimulateMsgWithdrawDelegatorReward(app.accountKeeper, app.distrKeeper)}, {50, distrsim.SimulateMsgWithdrawValidatorRewardsAll(app.accountKeeper, app.distrKeeper)}, {5, govsim.SimulateSubmittingVotingAndSlashingForProposal(app.govKeeper, app.stakeKeeper)}, - {100, govsim.SimulateMsgDeposit(app.govKeeper, app.stakeKeeper)}, + {100, govsim.SimulateMsgDeposit(app.govKeeper)}, {100, stakesim.SimulateMsgCreateValidator(app.accountKeeper, app.stakeKeeper)}, {5, stakesim.SimulateMsgEditValidator(app.stakeKeeper)}, {100, stakesim.SimulateMsgDelegate(app.accountKeeper, app.stakeKeeper)}, diff --git a/x/gov/keeper.go b/x/gov/keeper.go index 7ed8f9431a40..1940d5074400 100644 --- a/x/gov/keeper.go +++ b/x/gov/keeper.go @@ -7,6 +7,8 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/bank" "github.com/cosmos/cosmos-sdk/x/params" + + "github.com/tendermint/tendermint/crypto" ) // Parameter store default namestore @@ -19,6 +21,8 @@ var ( ParamStoreKeyDepositParams = []byte("depositparams") ParamStoreKeyVotingParams = []byte("votingparams") ParamStoreKeyTallyParams = []byte("tallyparams") + + DepositedCoinsAccAddr = sdk.AccAddress(crypto.AddressHash([]byte("govDepositedCoins"))) ) // Type declaration for parameters @@ -367,8 +371,8 @@ func (keeper Keeper) AddDeposit(ctx sdk.Context, proposalID uint64, depositerAdd return ErrAlreadyFinishedProposal(keeper.codespace, proposalID), false } - // Subtract coins from depositer's account - _, _, err := keeper.ck.SubtractCoins(ctx, depositerAddr, depositAmount) + // Send coins from depositer's account to DepositedCoinsAccAddr account + _, err := keeper.ck.SendCoins(ctx, depositerAddr, DepositedCoinsAccAddr, depositAmount) if err != nil { return err, false } @@ -413,7 +417,7 @@ func (keeper Keeper) RefundDeposits(ctx sdk.Context, proposalID uint64) { deposit := &Deposit{} keeper.cdc.MustUnmarshalBinaryLengthPrefixed(depositsIterator.Value(), deposit) - _, _, err := keeper.ck.AddCoins(ctx, deposit.Depositer, deposit.Amount) + _, err := keeper.ck.SendCoins(ctx, DepositedCoinsAccAddr, deposit.Depositer, deposit.Amount) if err != nil { panic("should not happen") } diff --git a/x/gov/simulation/msgs.go b/x/gov/simulation/msgs.go index bba834a3c8bf..0eadc7febb6b 100644 --- a/x/gov/simulation/msgs.go +++ b/x/gov/simulation/msgs.go @@ -50,7 +50,7 @@ func SimulateSubmittingVotingAndSlashingForProposal(k gov.Keeper, sk stake.Keepe if err != nil { return "", nil, err } - action, ok := simulateHandleMsgSubmitProposal(msg, sk, handler, ctx, event) + action, ok := simulateHandleMsgSubmitProposal(msg, handler, ctx, event) // don't schedule votes if proposal failed if !ok { return action, nil, nil @@ -68,7 +68,7 @@ func SimulateSubmittingVotingAndSlashingForProposal(k gov.Keeper, sk stake.Keepe fops := make([]simulation.FutureOperation, numVotes+1) for i := 0; i < numVotes; i++ { whenVote := ctx.BlockHeader().Time.Add(time.Duration(r.Int63n(int64(votingPeriod.Seconds()))) * time.Second) - fops[i] = simulation.FutureOperation{BlockTime: whenVote, Op: operationSimulateMsgVote(k, sk, accs[whoVotes[i]], proposalID)} + fops[i] = simulation.FutureOperation{BlockTime: whenVote, Op: operationSimulateMsgVote(k, accs[whoVotes[i]], proposalID)} } // 3) Make an operation to ensure slashes were done correctly. (Really should be a future invariant) // TODO: Find a way to check if a validator was slashed other than just checking their balance a block @@ -80,7 +80,7 @@ func SimulateSubmittingVotingAndSlashingForProposal(k gov.Keeper, sk stake.Keepe // SimulateMsgSubmitProposal simulates a msg Submit Proposal // Note: Currently doesn't ensure that the proposal txt is in JSON form -func SimulateMsgSubmitProposal(k gov.Keeper, sk stake.Keeper) simulation.Operation { +func SimulateMsgSubmitProposal(k gov.Keeper) simulation.Operation { handler := gov.NewHandler(k) return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) (action string, fOps []simulation.FutureOperation, err error) { sender := simulation.RandomAcc(r, accs) @@ -88,22 +88,14 @@ func SimulateMsgSubmitProposal(k gov.Keeper, sk stake.Keeper) simulation.Operati if err != nil { return "", nil, err } - action, _ = simulateHandleMsgSubmitProposal(msg, sk, handler, ctx, event) + action, _ = simulateHandleMsgSubmitProposal(msg, handler, ctx, event) return action, nil, nil } } -func simulateHandleMsgSubmitProposal(msg gov.MsgSubmitProposal, sk stake.Keeper, handler sdk.Handler, ctx sdk.Context, event func(string)) (action string, ok bool) { - ctx, write := ctx.CacheContext() - result := handler(ctx, msg) - ok = result.IsOK() - if ok { - // Update pool to keep invariants - pool := sk.GetPool(ctx) - pool.LooseTokens = pool.LooseTokens.Sub(sdk.NewDecFromInt(msg.InitialDeposit.AmountOf(denom))) - sk.SetPool(ctx, pool) - write() - } +func simulateHandleMsgSubmitProposal(msg gov.MsgSubmitProposal, handler sdk.Handler, ctx sdk.Context, event func(string)) (action string, ok bool) { + ctx, _ = ctx.CacheContext() + handler(ctx, msg) event(fmt.Sprintf("gov/MsgSubmitProposal/%v", ok)) action = fmt.Sprintf("TestMsgSubmitProposal: ok %v, msg %s", ok, msg.GetSignBytes()) return @@ -125,7 +117,7 @@ func simulationCreateMsgSubmitProposal(r *rand.Rand, sender simulation.Account) } // SimulateMsgDeposit -func SimulateMsgDeposit(k gov.Keeper, sk stake.Keeper) simulation.Operation { +func SimulateMsgDeposit(k gov.Keeper) simulation.Operation { return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) (action string, fOp []simulation.FutureOperation, err error) { acc := simulation.RandomAcc(r, accs) proposalID, ok := randomProposalID(r, k, ctx) @@ -137,15 +129,8 @@ func SimulateMsgDeposit(k gov.Keeper, sk stake.Keeper) simulation.Operation { if msg.ValidateBasic() != nil { return "", nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) } - ctx, write := ctx.CacheContext() + ctx, _ = ctx.CacheContext() result := gov.NewHandler(k)(ctx, msg) - if result.IsOK() { - // Update pool to keep invariants - pool := sk.GetPool(ctx) - pool.LooseTokens = pool.LooseTokens.Sub(sdk.NewDecFromInt(deposit.AmountOf(denom))) - sk.SetPool(ctx, pool) - write() - } event(fmt.Sprintf("gov/MsgDeposit/%v", result.IsOK())) action = fmt.Sprintf("TestMsgDeposit: ok %v, msg %s", result.IsOK(), msg.GetSignBytes()) return action, nil, nil @@ -154,12 +139,12 @@ func SimulateMsgDeposit(k gov.Keeper, sk stake.Keeper) simulation.Operation { // SimulateMsgVote // nolint: unparam -func SimulateMsgVote(k gov.Keeper, sk stake.Keeper) simulation.Operation { - return operationSimulateMsgVote(k, sk, simulation.Account{}, 0) +func SimulateMsgVote(k gov.Keeper) simulation.Operation { + return operationSimulateMsgVote(k, simulation.Account{}, 0) } // nolint: unparam -func operationSimulateMsgVote(k gov.Keeper, sk stake.Keeper, acc simulation.Account, proposalID uint64) simulation.Operation { +func operationSimulateMsgVote(k gov.Keeper, acc simulation.Account, proposalID uint64) simulation.Operation { return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) (action string, fOp []simulation.FutureOperation, err error) { if acc.Equals(simulation.Account{}) { acc = simulation.RandomAcc(r, accs) diff --git a/x/gov/simulation/sim_test.go b/x/gov/simulation/sim_test.go index a222c19a5274..5b6068c577a4 100644 --- a/x/gov/simulation/sim_test.go +++ b/x/gov/simulation/sim_test.go @@ -59,9 +59,9 @@ func TestGovWithRandomMessages(t *testing.T) { simulation.Simulate( t, mapp.BaseApp, appStateFn, []simulation.WeightedOperation{ - {2, SimulateMsgSubmitProposal(govKeeper, stakeKeeper)}, - {3, SimulateMsgDeposit(govKeeper, stakeKeeper)}, - {20, SimulateMsgVote(govKeeper, stakeKeeper)}, + {2, SimulateMsgSubmitProposal(govKeeper)}, + {3, SimulateMsgDeposit(govKeeper)}, + {20, SimulateMsgVote(govKeeper)}, }, []simulation.RandSetup{ setup, }, []simulation.Invariant{ @@ -75,7 +75,7 @@ func TestGovWithRandomMessages(t *testing.T) { t, mapp.BaseApp, appStateFn, []simulation.WeightedOperation{ {10, SimulateSubmittingVotingAndSlashingForProposal(govKeeper, stakeKeeper)}, - {5, SimulateMsgDeposit(govKeeper, stakeKeeper)}, + {5, SimulateMsgDeposit(govKeeper)}, }, []simulation.RandSetup{ setup, }, []simulation.Invariant{ From ec396f2e8cd295d3fdbc46f5a823595d3622adbd Mon Sep 17 00:00:00 2001 From: Jack Zampolin Date: Tue, 6 Nov 2018 17:05:13 -0800 Subject: [PATCH 14/16] Fix dependancy issue and update lockfile --- Gopkg.lock | 8 ++++---- Gopkg.toml | 3 +-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index f9be7bd97a9e..0e9681ed39eb 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -165,13 +165,12 @@ version = "v1.2.0" [[projects]] - digest = "1:c0d19ab64b32ce9fe5cf4ddceba78d5bc9807f0016db6b1183599da3dcc24d10" + digest = "1:ea40c24cdbacd054a6ae9de03e62c5f252479b96c716375aace5c120d68647c8" name = "github.com/hashicorp/hcl" packages = [ ".", "hcl/ast", "hcl/parser", - "hcl/printer", "hcl/scanner", "hcl/strconv", "hcl/token", @@ -435,7 +434,7 @@ version = "v0.11.1" [[projects]] - digest = "1:92d7d1678577fd1a6f3348168cef87880bbc710ef5f4e9a1216f45c56567d734" + digest = "1:395820b381043b9d2204e181ddf0f9147397c4a7b8f5dc3162de4cfcddf4589a" name = "github.com/tendermint/tendermint" packages = [ "abci/client", @@ -501,7 +500,8 @@ "version", ] pruneopts = "UT" - revision = "ebee4377b15f2958b08994485375dd2ee8a649ac" + revision = "03e42d2e3866f01a00625f608e3bbfaeb30690de" + version = "v0.26.1-rc0" [[projects]] digest = "1:7886f86064faff6f8d08a3eb0e8c773648ff5a2e27730831e2bfbf07467f6666" diff --git a/Gopkg.toml b/Gopkg.toml index a66574ea62f8..5726008858c4 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -36,7 +36,7 @@ [[override]] name = "github.com/tendermint/tendermint" - revision = "ebee4377b15f2958b08994485375dd2ee8a649ac" # TODO replace w/ 0.26.1 + version = "v0.26.1-rc0" # TODO replace w/ 0.26.1 ## deps without releases: @@ -84,4 +84,3 @@ [prune] go-tests = true unused-packages = true - From 56d2e225a6849f1c2207fc95ea386599af1d2a31 Mon Sep 17 00:00:00 2001 From: Sunny Aggarwal Date: Tue, 6 Nov 2018 18:08:24 -0800 Subject: [PATCH 15/16] fix cli uint64 --- cmd/gaia/cli_test/cli_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/gaia/cli_test/cli_test.go b/cmd/gaia/cli_test/cli_test.go index aae8491c5dac..bc9151200c43 100644 --- a/cmd/gaia/cli_test/cli_test.go +++ b/cmd/gaia/cli_test/cli_test.go @@ -414,12 +414,12 @@ func TestGaiaCLISubmitProposal(t *testing.T) { tests.WaitForNextNBlocksTM(2, port) vote := executeGetVote(t, fmt.Sprintf("gaiacli query vote --proposal-id=1 --voter=%s --output=json %v", fooAddr, flags)) - require.Equal(t, int64(1), vote.ProposalID) + require.Equal(t, uint64(1), vote.ProposalID) require.Equal(t, gov.OptionYes, vote.Option) votes := executeGetVotes(t, fmt.Sprintf("gaiacli query votes --proposal-id=1 --output=json %v", flags)) require.Len(t, votes, 1) - require.Equal(t, int64(1), votes[0].ProposalID) + require.Equal(t, uint64(1), votes[0].ProposalID) require.Equal(t, gov.OptionYes, votes[0].Option) proposalsQuery, _ = tests.ExecuteT(t, fmt.Sprintf("gaiacli query proposals --status=DepositPeriod %v", flags), "") @@ -439,7 +439,7 @@ func TestGaiaCLISubmitProposal(t *testing.T) { executeWrite(t, spStr, app.DefaultKeyPass) tests.WaitForNextNBlocksTM(2, port) - proposalsQuery, _ = tests.ExecuteT(t, fmt.Sprintf("gaiacli query proposals --latest=1 %v", flags), "") + proposalsQuery, _ = tests.ExecuteT(t, fmt.Sprintf("gaiacli query proposals --limit=1 %v", flags), "") require.Equal(t, " 2 - Apples", proposalsQuery) } From f921df7b09b49a3376f2fbe0acac1e6f7b3792c3 Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Tue, 6 Nov 2018 23:26:37 -0800 Subject: [PATCH 16/16] Send burned deposits to BurnedDepositCoinsAccAddr --- x/gov/keeper.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/x/gov/keeper.go b/x/gov/keeper.go index 1940d5074400..48d9000d8187 100644 --- a/x/gov/keeper.go +++ b/x/gov/keeper.go @@ -22,7 +22,8 @@ var ( ParamStoreKeyVotingParams = []byte("votingparams") ParamStoreKeyTallyParams = []byte("tallyparams") - DepositedCoinsAccAddr = sdk.AccAddress(crypto.AddressHash([]byte("govDepositedCoins"))) + DepositedCoinsAccAddr = sdk.AccAddress(crypto.AddressHash([]byte("govDepositedCoins"))) + BurnedDepositCoinsAccAddr = sdk.AccAddress(crypto.AddressHash([]byte("govBurnedDepositCoins"))) ) // Type declaration for parameters @@ -434,6 +435,14 @@ func (keeper Keeper) DeleteDeposits(ctx sdk.Context, proposalID uint64) { depositsIterator := keeper.GetDeposits(ctx, proposalID) for ; depositsIterator.Valid(); depositsIterator.Next() { + deposit := &Deposit{} + keeper.cdc.MustUnmarshalBinaryLengthPrefixed(depositsIterator.Value(), deposit) + + _, err := keeper.ck.SendCoins(ctx, DepositedCoinsAccAddr, BurnedDepositCoinsAccAddr, deposit.Amount) + if err != nil { + panic("should not happen") + } + store.Delete(depositsIterator.Key()) }