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 all 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 @@ -54,6 +54,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (client/keys) [\#9407](https://github.com/cosmos/cosmos-sdk/pull/9601) Added `keys rename` CLI command and `Keyring.Rename` interface method to rename a key in the keyring.
* (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` renamed to `DeleteAndBurnDeposits`, `RefundDeposits` renamed to `RefundAndDeleteDeposits`
* (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`.
* [\#9418](https://github.com/cosmos/cosmos-sdk/pull/9418) `sdk.Msg`'s `GetSigners()` method updated to return `[]string`.
Expand Down
8 changes: 4 additions & 4 deletions x/gov/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ func EndBlocker(ctx sdk.Context, keeper keeper.Keeper) {

logger := keeper.Logger(ctx)

// delete inactive proposal from store and its deposits
// delete dead proposals from store and burn theirs deposits. A proposal is dead when it's inactive and didn't get enough deposit on time to get into voting phase.
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 @@ -50,9 +50,9 @@ func EndBlocker(ctx sdk.Context, keeper keeper.Keeper) {
passes, burnDeposits, tallyResults := keeper.Tally(ctx, proposal)

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

if passes {
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
2 changes: 0 additions & 2 deletions x/gov/client/testutil/cli_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build norace

package testutil

import (
Expand Down
158 changes: 89 additions & 69 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,98 @@ 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]

func (s *DepositTestSuite) TearDownSuite() {
s.T().Log("tearing down test suite")
s.network.Cleanup()
deposits := sdk.Coins{
sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(0)),
sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens.Sub(sdk.NewInt(50))),
}
s.deposits = deposits

// create 2 proposals for testing
for i := 0; i < len(deposits); i++ {
id := i + 1
deposit := deposits[i]

s.createProposal(val, deposit, id)
s.proposalIDs = append(s.proposalIDs, fmt.Sprintf("%d", id))
}
}

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()
func (s *DepositTestSuite) SetupNewSuite() {
s.T().Log("setting up new test suite")

// 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))
var err error
s.network, err = network.New(s.T(), s.T().TempDir(), s.cfg)
s.Require().NoError(err)

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

// waiting for voting period to end
time.Sleep(20 * time.Second)
func (s *DepositTestSuite) createProposal(val *network.Validator, initialDeposit sdk.Coin, id int) {
var exactArgs []string

// query deposit & verify initial deposit
deposit := s.queryDeposit(val, "1", false)
s.Require().Equal(deposit.Amount.String(), initialDeposit)
if !initialDeposit.IsZero() {
exactArgs = append(exactArgs, fmt.Sprintf("--%s=%s", cli.FlagDeposit, initialDeposit.String()))
}

// 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)
_, 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)
}

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

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,74 +135,81 @@ 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() {
// resetting state required (proposal is getting removed from state and proposalID is not in sequence)
s.TearDownSuite()
s.SetupNewSuite()

val := s.network.Validators[0]
clientCtx := val.ClientCtx
initialDeposit := sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens)
id := 1
proposalID := fmt.Sprintf("%d", id)

// 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)
s.createProposal(val, initialDeposit, id)

// 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)
s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(out.Bytes(), &deposits))
s.Require().Equal(len(deposits.Deposits), 1)
// verify initial deposit
s.Require().Equal(deposits.Deposits[0].Amount.String(), sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens).String())
s.Require().Equal(deposits.Deposits[0].Amount.String(), initialDeposit.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
}
8 changes: 4 additions & 4 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,8 +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
func (keeper Keeper) RefundDeposits(ctx sdk.Context, proposalID uint64) {
// RefundAndDeleteDeposits refunds and deletes all the deposits on a specific proposal.
func (keeper Keeper) RefundAndDeleteDeposits(ctx sdk.Context, proposalID uint64) {
store := ctx.KVStore(keeper.storeKey)

keeper.IterateDeposits(ctx, proposalID, func(deposit types.Deposit) bool {
Expand Down
Loading