Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

refactor: remove query by events for x/gov deposits #9519

Merged
merged 47 commits into from
Jul 30, 2021
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
71040f4
refactor: remove query by events for deposits
atheeshp Jun 15, 2021
9449fb4
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/re…
atheeshp Jun 15, 2021
098d86f
revert norace
atheeshp Jun 15, 2021
322c6ac
fix lint
atheeshp Jun 15, 2021
45bdadf
fix test
atheeshp Jun 15, 2021
296b61f
update refund logic
atheeshp Jun 21, 2021
19ddfa2
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/re…
atheeshp Jun 21, 2021
d3f21e9
fix tests
atheeshp Jun 21, 2021
e59ac57
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/re…
atheeshp Jun 21, 2021
c1a790b
fix tests
atheeshp Jun 21, 2021
330dcbc
update invariants
atheeshp Jun 23, 2021
0df64f1
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/re…
atheeshp Jun 23, 2021
e7eb3df
fix tests
atheeshp Jun 24, 2021
9f9e2dc
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/re…
atheeshp Jun 24, 2021
e9c236f
Merge branch 'master' into atheesh/remove-query-events
atheeshp Jun 24, 2021
57cd024
Merge branch 'master' into atheesh/remove-query-events
atheeshp Jun 25, 2021
201c8b8
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/re…
atheeshp Jun 28, 2021
4189b99
remove commented code
atheeshp Jun 28, 2021
32f9844
Merge branch 'atheesh/remove-query-events' of github.com:cosmos/cosmo…
atheeshp Jun 29, 2021
ff75352
update invariants
atheeshp Jun 29, 2021
d049857
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/re…
atheeshp Jun 29, 2021
ebf9ef4
update tests
atheeshp Jun 30, 2021
968e102
fix conflicts
atheeshp Jun 30, 2021
0b96594
updated docs
atheeshp Jun 30, 2021
e199caa
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/re…
atheeshp Jul 5, 2021
b9127b8
address review changes
atheeshp Jul 5, 2021
b6f95f9
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/re…
atheeshp Jul 6, 2021
b7d97cd
fix lint
atheeshp Jul 6, 2021
95be8f4
review changes
atheeshp Jul 6, 2021
8c8549d
fix lint
atheeshp Jul 6, 2021
34d653e
Update x/gov/abci.go
atheeshp Jul 12, 2021
ff99ffb
Merge branch 'master' into atheesh/remove-query-events
atheeshp Jul 12, 2021
837f224
Merge branch 'master' into atheesh/remove-query-events
atheeshp Jul 16, 2021
0b47c1e
update active proposals endblocker
atheeshp Jul 16, 2021
8645610
revert
atheeshp Jul 16, 2021
8ae0f05
update tests
atheeshp Jul 22, 2021
7af89fe
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/re…
atheeshp Jul 22, 2021
d4461f3
remove log
atheeshp Jul 22, 2021
41fb2cc
update changelog
atheeshp Jul 22, 2021
d95df5f
update docs
atheeshp Jul 22, 2021
68a7be1
Merge branch 'master' into atheesh/remove-query-events
atheeshp Jul 26, 2021
8cd4653
Merge branch 'master' into atheesh/remove-query-events
atheeshp Jul 26, 2021
23dc6f5
Merge branch 'master' into atheesh/remove-query-events
robert-zaremba Jul 27, 2021
20913a1
Update x/gov/spec/01_concepts.md
atheeshp Jul 27, 2021
8d499c5
Merge branch 'master' into atheesh/remove-query-events
atheeshp Jul 27, 2021
3b0079f
Merge branch 'master' into atheesh/remove-query-events
atheeshp Jul 29, 2021
e26d3c4
Merge branch 'master' into atheesh/remove-query-events
robert-zaremba Jul 30, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
the Tx Factory as methods.
* (x/slashing) [\#9458](https://github.com/cosmos/cosmos-sdk/pull/9458) Coins burned from slashing is now returned from Slash function and included in Slash event.
* [\#9246](https://github.com/cosmos/cosmos-sdk/pull/9246) The `New` method for the network package now returns an error.
* [\#9519](https://github.com/cosmos/cosmos-sdk/pull/9519) `DeleteDeposits` now renamed to `DeleteAndBurnDeposits`, also now deposits won't be removed from state for completed proposals unless a proposal is vetoed.
* (codec) [\#9521](https://github.com/cosmos/cosmos-sdk/pull/9521) Removed deprecated `clientCtx.JSONCodec` from `client.Context`.
* (codec) [\#9521](https://github.com/cosmos/cosmos-sdk/pull/9521) Rename `EncodingConfig.Marshaler` to `Codec`.

Expand Down
6 changes: 2 additions & 4 deletions x/gov/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func EndBlocker(ctx sdk.Context, keeper keeper.Keeper) {
// delete inactive proposal from store and its deposits
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
keeper.IterateInactiveProposalsQueue(ctx, ctx.BlockHeader().Time, func(proposal types.Proposal) bool {
keeper.DeleteProposal(ctx, proposal.ProposalId)
keeper.DeleteDeposits(ctx, proposal.ProposalId)
keeper.DeleteAndBurnDeposits(ctx, proposal.ProposalId)
atheeshp marked this conversation as resolved.
Show resolved Hide resolved

// called when proposal become inactive
keeper.AfterProposalFailedMinDeposit(ctx, proposal.ProposalId)
Expand Down Expand Up @@ -49,9 +49,7 @@ func EndBlocker(ctx sdk.Context, keeper keeper.Keeper) {

passes, burnDeposits, tallyResults := keeper.Tally(ctx, proposal)

if burnDeposits {
keeper.DeleteDeposits(ctx, proposal.ProposalId)
} else {
if !burnDeposits {
keeper.RefundDeposits(ctx, proposal.ProposalId)
}

Expand Down
37 changes: 2 additions & 35 deletions x/gov/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,31 +366,14 @@ $ %s query gov deposit 1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk

// check to see if the proposal is in the store
ctx := cmd.Context()
proposalRes, err := queryClient.Proposal(
_, err = queryClient.Proposal(
ctx,
&types.QueryProposalRequest{ProposalId: proposalID},
)
if err != nil {
return fmt.Errorf("failed to fetch proposal-id %d: %s", proposalID, err)
}

depositorAddr, err := sdk.AccAddressFromBech32(args[1])
if err != nil {
return err
}

var deposit types.Deposit
propStatus := proposalRes.Proposal.Status
if !(propStatus == types.StatusVotingPeriod || propStatus == types.StatusDepositPeriod) {
params := types.NewQueryDepositParams(proposalID, depositorAddr)
resByTxQuery, err := gcutils.QueryDepositByTxQuery(clientCtx, params)
if err != nil {
return err
}
clientCtx.Codec.MustUnmarshalJSON(resByTxQuery, &deposit)
return clientCtx.PrintProto(&deposit)
}

res, err := queryClient.Deposit(
ctx,
&types.QueryDepositRequest{ProposalId: proposalID, Depositor: args[1]},
Expand Down Expand Up @@ -439,30 +422,14 @@ $ %s query gov deposits 1

// check to see if the proposal is in the store
ctx := cmd.Context()
proposalRes, err := queryClient.Proposal(
_, err = queryClient.Proposal(
ctx,
&types.QueryProposalRequest{ProposalId: proposalID},
)
if err != nil {
return fmt.Errorf("failed to fetch proposal-id %d: %s", proposalID, err)
}

propStatus := proposalRes.GetProposal().Status
if !(propStatus == types.StatusVotingPeriod || propStatus == types.StatusDepositPeriod) {
params := types.NewQueryProposalParams(proposalID)
resByTxQuery, err := gcutils.QueryDepositsByTxQuery(clientCtx, params)
if err != nil {
return err
}

var dep types.Deposits
// TODO migrate to use JSONCodec (implement MarshalJSONArray
// or wrap lists of proto.Message in some other message)
clientCtx.LegacyAmino.MustUnmarshalJSON(resByTxQuery, &dep)

return clientCtx.PrintObjectLegacy(dep)
}

pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
Expand Down
147 changes: 72 additions & 75 deletions x/gov/client/testutil/deposits.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import (
type DepositTestSuite struct {
suite.Suite

cfg network.Config
network *network.Network
fees string
cfg network.Config
network *network.Network
deposits sdk.Coins
proposalIDs []string
}

func NewDepositTestSuite(cfg network.Config) *DepositTestSuite {
Expand All @@ -34,86 +35,82 @@ func (s *DepositTestSuite) SetupSuite() {

_, err = s.network.WaitForHeight(1)
s.Require().NoError(err)
s.fees = sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(20))).String()

val := s.network.Validators[0]

deposits := sdk.Coins{
sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(0)),
sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens.Sub(sdk.NewInt(50))),
sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens),
}
s.deposits = deposits

// create 3 proposals for testing
for i := 0; i < len(deposits); i++ {
var exactArgs []string
id := i + 1

if !deposits[i].IsZero() {
exactArgs = append(exactArgs, fmt.Sprintf("--%s=%s", cli.FlagDeposit, deposits[i]))
}

_, err := MsgSubmitProposal(
val.ClientCtx,
val.Address.String(),
fmt.Sprintf("Text Proposal %d", id),
"Where is the title!?",
types.ProposalTypeText,
exactArgs...,
)

s.Require().NoError(err)
_, err = s.network.WaitForHeight(1)
s.Require().NoError(err)
s.proposalIDs = append(s.proposalIDs, fmt.Sprintf("%d", id))
}
}

func (s *DepositTestSuite) TearDownSuite() {
s.T().Log("tearing down test suite")
s.network.Cleanup()
}

func (s *DepositTestSuite) TestQueryDepositsInitialDeposit() {
val := s.network.Validators[0]
clientCtx := val.ClientCtx
initialDeposit := sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens.Sub(sdk.NewInt(20))).String()

// create a proposal with deposit
_, err := MsgSubmitProposal(val.ClientCtx, val.Address.String(),
"Text Proposal 1", "Where is the title!?", types.ProposalTypeText,
fmt.Sprintf("--%s=%s", cli.FlagDeposit, initialDeposit))
s.Require().NoError(err)

// deposit more amount
_, err = MsgDeposit(clientCtx, val.Address.String(), "1", sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(50)).String())
s.Require().NoError(err)

// waiting for voting period to end
time.Sleep(20 * time.Second)

// query deposit & verify initial deposit
deposit := s.queryDeposit(val, "1", false)
s.Require().Equal(deposit.Amount.String(), initialDeposit)

// query deposits
deposits := s.queryDeposits(val, "1", false)
s.Require().Equal(len(deposits), 2)
// verify initial deposit
s.Require().Equal(deposits[0].Amount.String(), initialDeposit)
}

func (s *DepositTestSuite) TestQueryDepositsWithoutInitialDeposit() {
val := s.network.Validators[0]
clientCtx := val.ClientCtx

// create a proposal without deposit
_, err := MsgSubmitProposal(val.ClientCtx, val.Address.String(),
"Text Proposal 2", "Where is the title!?", types.ProposalTypeText)
s.Require().NoError(err)
proposalID := s.proposalIDs[0]

// deposit amount
_, err = MsgDeposit(clientCtx, val.Address.String(), "2", sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens.Add(sdk.NewInt(50))).String())
depositAmount := sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens.Add(sdk.NewInt(50))).String()
_, err := MsgDeposit(clientCtx, val.Address.String(), proposalID, depositAmount)
s.Require().NoError(err)

// waiting for voting period to end
time.Sleep(20 * time.Second)
_, err = s.network.WaitForHeight(2)
s.Require().NoError(err)

// query deposit
deposit := s.queryDeposit(val, "2", false)
s.Require().Equal(deposit.Amount.String(), sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens.Add(sdk.NewInt(50))).String())
deposit := s.queryDeposit(val, proposalID, false, "")
s.Require().NotNil(deposit)
s.Require().Equal(deposit.Amount.String(), depositAmount)

// query deposits
deposits := s.queryDeposits(val, "2", false)
s.Require().Equal(len(deposits), 1)
deposits := s.queryDeposits(val, proposalID, false, "")
s.Require().NotNil(deposits)
s.Require().Len(deposits.Deposits, 1)
// verify initial deposit
s.Require().Equal(deposits[0].Amount.String(), sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens.Add(sdk.NewInt(50))).String())
s.Require().Equal(deposits.Deposits[0].Amount.String(), depositAmount)
}

func (s *DepositTestSuite) TestQueryProposalNotEnoughDeposits() {
val := s.network.Validators[0]
clientCtx := val.ClientCtx
initialDeposit := sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens.Sub(sdk.NewInt(2000))).String()

// create a proposal with deposit
_, err := MsgSubmitProposal(val.ClientCtx, val.Address.String(),
"Text Proposal 3", "Where is the title!?", types.ProposalTypeText,
fmt.Sprintf("--%s=%s", cli.FlagDeposit, initialDeposit))
s.Require().NoError(err)
proposalID := s.proposalIDs[1]

// query proposal
args := []string{"3", fmt.Sprintf("--%s=json", tmcli.OutputFlag)}
args := []string{proposalID, fmt.Sprintf("--%s=json", tmcli.OutputFlag)}
cmd := cli.GetCmdQueryProposal()
_, err = clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
_, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
s.Require().NoError(err)

// waiting for deposit period to end
Expand All @@ -122,23 +119,18 @@ func (s *DepositTestSuite) TestQueryProposalNotEnoughDeposits() {
// query proposal
_, err = clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
s.Require().Error(err)
s.Require().Contains(err.Error(), "proposal 3 doesn't exist")
s.Require().Contains(err.Error(), fmt.Sprintf("proposal %s doesn't exist", proposalID))
}

func (s *DepositTestSuite) TestRejectedProposalDeposits() {
val := s.network.Validators[0]
clientCtx := val.ClientCtx
initialDeposit := sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens)

// create a proposal with deposit
_, err := MsgSubmitProposal(clientCtx, val.Address.String(),
"Text Proposal 4", "Where is the title!?", types.ProposalTypeText,
fmt.Sprintf("--%s=%s", cli.FlagDeposit, initialDeposit))
s.Require().NoError(err)
initialDeposit := s.deposits[2]
proposalID := s.proposalIDs[2]

// query deposits
var deposits types.QueryDepositsResponse
args := []string{"4", fmt.Sprintf("--%s=json", tmcli.OutputFlag)}
args := []string{proposalID, fmt.Sprintf("--%s=json", tmcli.OutputFlag)}
cmd := cli.GetCmdQueryDeposits()
out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, args)
s.Require().NoError(err)
Expand All @@ -148,48 +140,53 @@ func (s *DepositTestSuite) TestRejectedProposalDeposits() {
s.Require().Equal(deposits.Deposits[0].Amount.String(), sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens).String())

// vote
_, err = MsgVote(clientCtx, val.Address.String(), "4", "no")
_, err = MsgVote(clientCtx, val.Address.String(), proposalID, "no")
s.Require().NoError(err)

time.Sleep(20 * time.Second)
_, err = s.network.WaitForHeight(3)
s.Require().NoError(err)

args = []string{"4", fmt.Sprintf("--%s=json", tmcli.OutputFlag)}
args = []string{proposalID, fmt.Sprintf("--%s=json", tmcli.OutputFlag)}
cmd = cli.GetCmdQueryProposal()
_, err = clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
s.Require().NoError(err)

// query deposits
depositsRes := s.queryDeposits(val, "4", false)
s.Require().Equal(len(depositsRes), 1)
depositsRes := s.queryDeposits(val, proposalID, false, "")
s.Require().NotNil(depositsRes)
s.Require().Len(depositsRes.Deposits, 1)
// verify initial deposit
s.Require().Equal(depositsRes[0].Amount.String(), initialDeposit.String())

s.Require().Equal(depositsRes.Deposits[0].Amount.String(), initialDeposit.String())
}

func (s *DepositTestSuite) queryDeposits(val *network.Validator, proposalID string, exceptErr bool) types.Deposits {
func (s *DepositTestSuite) queryDeposits(val *network.Validator, proposalID string, exceptErr bool, message string) *types.QueryDepositsResponse {
args := []string{proposalID, fmt.Sprintf("--%s=json", tmcli.OutputFlag)}
var depositsRes types.Deposits
var depositsRes *types.QueryDepositsResponse
cmd := cli.GetCmdQueryDeposits()
out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, args)

if exceptErr {
s.Require().Error(err)
s.Require().Contains(err.Error(), message)
return nil
}

s.Require().NoError(err)
s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(out.Bytes(), &depositsRes))
return depositsRes
}

func (s *DepositTestSuite) queryDeposit(val *network.Validator, proposalID string, exceptErr bool) *types.Deposit {
func (s *DepositTestSuite) queryDeposit(val *network.Validator, proposalID string, exceptErr bool, message string) *types.Deposit {
args := []string{proposalID, val.Address.String(), fmt.Sprintf("--%s=json", tmcli.OutputFlag)}
var depositRes types.Deposit
var depositRes *types.Deposit
cmd := cli.GetCmdQueryDeposit()
out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, args)
if exceptErr {
s.Require().Error(err)
s.Require().Contains(err.Error(), message)
return nil
}
s.Require().NoError(err)
s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(out.Bytes(), &depositRes))
return &depositRes
return depositRes
}
4 changes: 2 additions & 2 deletions x/gov/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func InitGenesis(ctx sdk.Context, ak types.AccountKeeper, bk types.BankKeeper, k
}

// check if total deposits equals balance, if it doesn't panic because there were export/import errors
if !balance.IsEqual(totalDeposits) {
panic(fmt.Sprintf("expected module account was %s but we got %s", balance.String(), totalDeposits.String()))
if totalDeposits.IsAllGT(balance) {
panic(fmt.Sprintf("expected module account balance to be less than %s but got %s", totalDeposits.String(), balance.String()))
}
}

Expand Down
9 changes: 3 additions & 6 deletions x/gov/keeper/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func (keeper Keeper) GetDeposits(ctx sdk.Context, proposalID uint64) (deposits t
return
}

// DeleteDeposits deletes all the deposits on a specific proposal without refunding them
func (keeper Keeper) DeleteDeposits(ctx sdk.Context, proposalID uint64) {
// DeleteAndBurnDeposits deletes and burn all the deposits on a specific proposal.
func (keeper Keeper) DeleteAndBurnDeposits(ctx sdk.Context, proposalID uint64) {
store := ctx.KVStore(keeper.storeKey)

keeper.IterateDeposits(ctx, proposalID, func(deposit types.Deposit) bool {
Expand Down Expand Up @@ -166,10 +166,8 @@ func (keeper Keeper) AddDeposit(ctx sdk.Context, proposalID uint64, depositorAdd
return activatedVotingPeriod, nil
}

// RefundDeposits refunds and deletes all the deposits on a specific proposal
// RefundDeposits refunds all the deposits on a specific proposal.
func (keeper Keeper) RefundDeposits(ctx sdk.Context, proposalID uint64) {
store := ctx.KVStore(keeper.storeKey)

keeper.IterateDeposits(ctx, proposalID, func(deposit types.Deposit) bool {
depositor, err := sdk.AccAddressFromBech32(deposit.Depositor)
if err != nil {
Expand All @@ -181,7 +179,6 @@ func (keeper Keeper) RefundDeposits(ctx sdk.Context, proposalID uint64) {
panic(err)
}

store.Delete(types.DepositKey(proposalID, depositor))
return false
})
}
10 changes: 7 additions & 3 deletions x/gov/keeper/deposit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,18 @@ func TestDeposits(t *testing.T) {
require.Equal(t, fourStake, deposit.Amount)
app.GovKeeper.RefundDeposits(ctx, proposalID)
deposit, found = app.GovKeeper.GetDeposit(ctx, proposalID, TestAddrs[1])
require.False(t, found)
require.True(t, found) // deposits will be refunded but not deleted from store
require.Equal(t, addr0Initial, app.BankKeeper.GetAllBalances(ctx, TestAddrs[0]))
require.Equal(t, addr1Initial, app.BankKeeper.GetAllBalances(ctx, TestAddrs[1]))

// Test delete deposits
// Test delete and burn deposits
proposal, err = app.GovKeeper.SubmitProposal(ctx, tp)
require.NoError(t, err)
proposalID = proposal.ProposalId
_, err = app.GovKeeper.AddDeposit(ctx, proposalID, TestAddrs[0], fourStake)
require.NoError(t, err)
app.GovKeeper.DeleteDeposits(ctx, proposalID)
app.GovKeeper.DeleteAndBurnDeposits(ctx, proposalID)
deposits = app.GovKeeper.GetDeposits(ctx, proposalID)
require.Len(t, deposits, 0)
require.Equal(t, addr0Initial.Sub(fourStake), app.BankKeeper.GetAllBalances(ctx, TestAddrs[0]))
}
Loading