diff --git a/CHANGELOG.md b/CHANGELOG.md index 202e227b86..a106b4fbbe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (refactor) [\#493](https://github.com/line/lbm-sdk/pull/493) restructure x/consortium * (server/grpc) [\#526](https://github.com/line/lbm-sdk/pull/526) add index field into TxResponse * (cli) [\#535](https://github.com/line/lbm-sdk/pull/536) updated ostracon to v1.0.5; `unsafe-reset-all` command has been moved to the `ostracon` sub-command. +* (x/foundation) [\#597](https://github.com/line/lbm-sdk/pull/597) tidy up x/foundation ### Bug Fixes * (x/wasm) [\#453](https://github.com/line/lbm-sdk/pull/453) modify wasm grpc query api path diff --git a/x/foundation/authz_test.go b/x/foundation/authz_test.go index fa3342f24c..88b01fc893 100644 --- a/x/foundation/authz_test.go +++ b/x/foundation/authz_test.go @@ -9,14 +9,14 @@ import ( ) func TestReceiveFromTreasuryAuthorization(t *testing.T) { - testCases := map[string]struct{ - msg sdk.Msg - valid bool + testCases := map[string]struct { + msg sdk.Msg + valid bool accept bool }{ "valid": { - msg: &foundation.MsgWithdrawFromTreasury{}, - valid: true, + msg: &foundation.MsgWithdrawFromTreasury{}, + valid: true, accept: true, }, "msg mismatch": { diff --git a/x/foundation/client/cli/tx.go b/x/foundation/client/cli/tx.go index 9f3dcaaa50..9016f3a403 100644 --- a/x/foundation/client/cli/tx.go +++ b/x/foundation/client/cli/tx.go @@ -461,7 +461,7 @@ Example of the content of policy-json: "threshold": "10", "windows": { "voting_period": "24h", - "min_execution_period": "0s", + "min_execution_period": "0s" } } `, diff --git a/x/foundation/client/testutil/query.go b/x/foundation/client/testutil/query.go index 52a37cff0a..bb15af708c 100644 --- a/x/foundation/client/testutil/query.go +++ b/x/foundation/client/testutil/query.go @@ -57,7 +57,7 @@ func (s *IntegrationTestSuite) TestNewQueryCmdParams() { s.Require().NoError(err) var actual foundation.QueryParamsResponse - s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(out.Bytes(), &actual), out.String()) + s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual), out.String()) s.Require().Equal(tc.expected, &actual) }) } @@ -525,7 +525,7 @@ func (s *IntegrationTestSuite) TestNewQueryCmdGrants() { s.Require().NoError(err) var actual foundation.QueryGrantsResponse - s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(out.Bytes(), &actual), out.String()) + s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual), out.String()) s.Require().Equal(tc.expected, len(actual.Authorizations)) }) } diff --git a/x/foundation/client/testutil/suite.go b/x/foundation/client/testutil/suite.go index 1676061cf0..9e0953ab4e 100644 --- a/x/foundation/client/testutil/suite.go +++ b/x/foundation/client/testutil/suite.go @@ -36,7 +36,7 @@ type IntegrationTestSuite struct { var commonArgs = []string{ fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100))).String()), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100)))), } func NewIntegrationTestSuite(cfg network.Config) *IntegrationTestSuite { @@ -185,7 +185,7 @@ func (s *IntegrationTestSuite) submitProposal(msg sdk.Msg, try bool) uint64 { for _, e := range events { if e.Type == proposalEvent.Type { var proposal foundation.Proposal - err := s.cfg.Codec.UnmarshalJSON([]byte(e.Attributes[0].Value), &proposal) + err := val.ClientCtx.Codec.UnmarshalJSON([]byte(e.Attributes[0].Value), &proposal) s.Require().NoError(err) return proposal.Id diff --git a/x/foundation/client/testutil/tx.go b/x/foundation/client/testutil/tx.go index 8cfa44ea22..df75d21fae 100644 --- a/x/foundation/client/testutil/tx.go +++ b/x/foundation/client/testutil/tx.go @@ -17,10 +17,10 @@ func (s *IntegrationTestSuite) TestNewProposalCmdUpdateFoundationParams() { val := s.network.Validators[0] commonFlags := []string{ - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)))), } testCases := []struct { @@ -49,15 +49,14 @@ func (s *IntegrationTestSuite) TestNewProposalCmdUpdateFoundationParams() { s.Run(tc.name, func() { cmd := cli.NewProposalCmdUpdateFoundationParams() - clientCtx := val.ClientCtx flags.AddTxFlagsToCmd(cmd) - out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args) + out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, tc.args) if tc.expectErr { s.Require().Error(err) } else { s.Require().NoError(err) - s.Require().NoError(s.cfg.Codec.UnmarshalJSON(out.Bytes(), tc.respType), out.String()) + s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), tc.respType), out) txResp := tc.respType.(*sdk.TxResponse) s.Require().Equal(tc.expectedCode, txResp.Code) @@ -70,10 +69,10 @@ func (s *IntegrationTestSuite) TestNewProposalCmdUpdateValidatorAuths() { val := s.network.Validators[0] commonFlags := []string{ - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)))), } testCases := []struct { @@ -137,15 +136,14 @@ func (s *IntegrationTestSuite) TestNewProposalCmdUpdateValidatorAuths() { s.Run(tc.name, func() { cmd := cli.NewProposalCmdUpdateValidatorAuths() - clientCtx := val.ClientCtx flags.AddTxFlagsToCmd(cmd) - out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args) + out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, tc.args) if tc.expectErr { s.Require().Error(err) } else { s.Require().NoError(err) - s.Require().NoError(s.cfg.Codec.UnmarshalJSON(out.Bytes(), tc.respType), out.String()) + s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), tc.respType), out) txResp := tc.respType.(*sdk.TxResponse) s.Require().Equal(tc.expectedCode, txResp.Code) @@ -157,10 +155,10 @@ func (s *IntegrationTestSuite) TestNewProposalCmdUpdateValidatorAuths() { func (s *IntegrationTestSuite) TestNewTxCmdFundTreasury() { val := s.network.Validators[0] commonArgs := []string{ - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)))), } testCases := map[string]struct { @@ -203,8 +201,8 @@ func (s *IntegrationTestSuite) TestNewTxCmdFundTreasury() { s.Require().NoError(err) var res sdk.TxResponse - s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res), out.String()) - s.Require().EqualValues(0, res.Code, out.String()) + s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res), out) + s.Require().EqualValues(0, res.Code, out) }) } } @@ -212,10 +210,10 @@ func (s *IntegrationTestSuite) TestNewTxCmdFundTreasury() { func (s *IntegrationTestSuite) TestNewTxCmdWithdrawFromTreasury() { val := s.network.Validators[0] commonArgs := []string{ - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)))), } testCases := map[string]struct { @@ -261,8 +259,8 @@ func (s *IntegrationTestSuite) TestNewTxCmdWithdrawFromTreasury() { s.Require().NoError(err) var res sdk.TxResponse - s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(out.Bytes(), &res), out.String()) - s.Require().EqualValues(0, res.Code, out.String()) + s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res), out) + s.Require().EqualValues(0, res.Code, out) }) } } @@ -270,10 +268,10 @@ func (s *IntegrationTestSuite) TestNewTxCmdWithdrawFromTreasury() { func (s *IntegrationTestSuite) TestNewTxCmdUpdateMembers() { val := s.network.Validators[0] commonArgs := []string{ - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)))), } updates := `[{"address":"%s", "participating":%t}]` @@ -317,8 +315,8 @@ func (s *IntegrationTestSuite) TestNewTxCmdUpdateMembers() { s.Require().NoError(err) var res sdk.TxResponse - s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(out.Bytes(), &res), out.String()) - s.Require().EqualValues(0, res.Code, out.String()) + s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res), out) + s.Require().EqualValues(0, res.Code, out) }) } } @@ -326,14 +324,14 @@ func (s *IntegrationTestSuite) TestNewTxCmdUpdateMembers() { func (s *IntegrationTestSuite) TestNewTxCmdUpdateDecisionPolicy() { val := s.network.Validators[0] commonArgs := []string{ - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)))), } doMarshal := func(policy foundation.DecisionPolicy) string { - bz, err := s.cfg.Codec.MarshalInterfaceJSON(policy) + bz, err := val.ClientCtx.Codec.MarshalInterfaceJSON(policy) s.Require().NoError(err) return string(bz) } @@ -387,8 +385,8 @@ func (s *IntegrationTestSuite) TestNewTxCmdUpdateDecisionPolicy() { s.Require().NoError(err) var res sdk.TxResponse - s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(out.Bytes(), &res), out.String()) - s.Require().EqualValues(0, res.Code, out.String()) + s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res), out) + s.Require().EqualValues(0, res.Code, out) }) } } @@ -396,10 +394,10 @@ func (s *IntegrationTestSuite) TestNewTxCmdUpdateDecisionPolicy() { func (s *IntegrationTestSuite) TestNewTxCmdSubmitProposal() { val := s.network.Validators[0] commonArgs := []string{ - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)))), } proposers := `["%s"]` @@ -454,8 +452,8 @@ func (s *IntegrationTestSuite) TestNewTxCmdSubmitProposal() { s.Require().NoError(err) var res sdk.TxResponse - s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(out.Bytes(), &res), out.String()) - s.Require().EqualValues(0, res.Code, out.String()) + s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res), out) + s.Require().EqualValues(0, res.Code, out) }) } } @@ -463,10 +461,10 @@ func (s *IntegrationTestSuite) TestNewTxCmdSubmitProposal() { func (s *IntegrationTestSuite) TestNewTxCmdWithdrawProposal() { val := s.network.Validators[0] commonArgs := []string{ - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)))), } testCases := map[string]struct { @@ -510,8 +508,8 @@ func (s *IntegrationTestSuite) TestNewTxCmdWithdrawProposal() { s.Require().NoError(err) var res sdk.TxResponse - s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(out.Bytes(), &res), out.String()) - s.Require().EqualValues(0, res.Code, out.String()) + s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res), out) + s.Require().EqualValues(0, res.Code, out) }) } } @@ -519,10 +517,10 @@ func (s *IntegrationTestSuite) TestNewTxCmdWithdrawProposal() { func (s *IntegrationTestSuite) TestNewTxCmdVote() { val := s.network.Validators[0] commonArgs := []string{ - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)))), } id := s.submitProposal(&foundation.MsgWithdrawFromTreasury{ @@ -576,8 +574,8 @@ func (s *IntegrationTestSuite) TestNewTxCmdVote() { s.Require().NoError(err) var res sdk.TxResponse - s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(out.Bytes(), &res), out.String()) - s.Require().EqualValues(0, res.Code, out.String()) + s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res), out) + s.Require().EqualValues(0, res.Code, out) }) } } @@ -585,10 +583,10 @@ func (s *IntegrationTestSuite) TestNewTxCmdVote() { func (s *IntegrationTestSuite) TestNewTxCmdExec() { val := s.network.Validators[0] commonArgs := []string{ - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)))), } testCases := map[string]struct { @@ -631,8 +629,8 @@ func (s *IntegrationTestSuite) TestNewTxCmdExec() { s.Require().NoError(err) var res sdk.TxResponse - s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(out.Bytes(), &res), out.String()) - s.Require().EqualValues(0, res.Code, out.String()) + s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res), out) + s.Require().EqualValues(0, res.Code, out) }) } } @@ -640,10 +638,10 @@ func (s *IntegrationTestSuite) TestNewTxCmdExec() { func (s *IntegrationTestSuite) TestNewTxCmdLeaveFoundation() { val := s.network.Validators[0] commonArgs := []string{ - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)))), } testCases := map[string]struct { @@ -682,8 +680,8 @@ func (s *IntegrationTestSuite) TestNewTxCmdLeaveFoundation() { s.Require().NoError(err) var res sdk.TxResponse - s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(out.Bytes(), &res), out.String()) - s.Require().EqualValues(0, res.Code, out.String()) + s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res), out) + s.Require().EqualValues(0, res.Code, out) }) } } @@ -691,14 +689,14 @@ func (s *IntegrationTestSuite) TestNewTxCmdLeaveFoundation() { func (s *IntegrationTestSuite) TestNewTxCmdGrant() { val := s.network.Validators[0] commonArgs := []string{ - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)))), } doMarshal := func(authorization foundation.Authorization) string { - bz, err := s.cfg.Codec.MarshalInterfaceJSON(authorization) + bz, err := val.ClientCtx.Codec.MarshalInterfaceJSON(authorization) s.Require().NoError(err) return string(bz) } @@ -745,8 +743,8 @@ func (s *IntegrationTestSuite) TestNewTxCmdGrant() { s.Require().NoError(err) var res sdk.TxResponse - s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(out.Bytes(), &res), out.String()) - s.Require().EqualValues(0, res.Code, out.String()) + s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res), out) + s.Require().EqualValues(0, res.Code, out) }) } } @@ -754,10 +752,10 @@ func (s *IntegrationTestSuite) TestNewTxCmdGrant() { func (s *IntegrationTestSuite) TestNewTxCmdRevoke() { val := s.network.Validators[0] commonArgs := []string{ - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)))), } testCases := map[string]struct { @@ -803,8 +801,8 @@ func (s *IntegrationTestSuite) TestNewTxCmdRevoke() { s.Require().NoError(err) var res sdk.TxResponse - s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(out.Bytes(), &res), out.String()) - s.Require().EqualValues(0, res.Code, out.String()) + s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res), out) + s.Require().EqualValues(0, res.Code, out) }) } } diff --git a/x/foundation/foundation.go b/x/foundation/foundation.go index ee0b9e579d..f96979da86 100644 --- a/x/foundation/foundation.go +++ b/x/foundation/foundation.go @@ -136,7 +136,7 @@ func (t *TallyResult) Add(option VoteOption) error { case VOTE_OPTION_NO_WITH_VETO: t.NoWithVetoCount = t.NoWithVetoCount.Add(weight) default: - return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "unknown vote option %s", option.String()) + return sdkerrors.ErrInvalidRequest.Wrapf("unknown vote option %s", option) } return nil diff --git a/x/foundation/foundation_test.go b/x/foundation/foundation_test.go index f1046ec9d2..c6727d065d 100644 --- a/x/foundation/foundation_test.go +++ b/x/foundation/foundation_test.go @@ -45,35 +45,35 @@ func TestTallyResult(t *testing.T) { func TestThresholdDecisionPolicy(t *testing.T) { config := foundation.DefaultConfig() - testCases := map[string]struct{ - threshold sdk.Dec - votingPeriod time.Duration + testCases := map[string]struct { + threshold sdk.Dec + votingPeriod time.Duration minExecutionPeriod time.Duration - validBasic bool - valid bool + validBasic bool + valid bool }{ "valid policy": { - threshold: config.MinThreshold, - votingPeriod: time.Hour, + threshold: config.MinThreshold, + votingPeriod: time.Hour, minExecutionPeriod: config.MaxExecutionPeriod + time.Hour - time.Nanosecond, - validBasic: true, - valid: true, + validBasic: true, + valid: true, }, "invalid policy (basic)": { - threshold: config.MinThreshold, + threshold: config.MinThreshold, minExecutionPeriod: config.MaxExecutionPeriod - time.Nanosecond, }, "invalid policy": { - threshold: config.MinThreshold.Sub(sdk.SmallestDec()), - votingPeriod: time.Hour, + threshold: config.MinThreshold.Sub(sdk.SmallestDec()), + votingPeriod: time.Hour, minExecutionPeriod: config.MaxExecutionPeriod + time.Hour - time.Nanosecond, - validBasic: true, + validBasic: true, }, "invalid policy (windows)": { - threshold: config.MinThreshold, - votingPeriod: time.Hour, + threshold: config.MinThreshold, + votingPeriod: time.Hour, minExecutionPeriod: config.MaxExecutionPeriod + time.Hour, - validBasic: true, + validBasic: true, }, } @@ -81,7 +81,7 @@ func TestThresholdDecisionPolicy(t *testing.T) { policy := foundation.ThresholdDecisionPolicy{ Threshold: tc.threshold, Windows: &foundation.DecisionPolicyWindows{ - VotingPeriod: tc.votingPeriod, + VotingPeriod: tc.votingPeriod, MinExecutionPeriod: tc.minExecutionPeriod, }, } @@ -115,47 +115,47 @@ func TestThresholdDecisionPolicyAllow(t *testing.T) { require.NoError(t, policy.Validate(config)) require.Equal(t, time.Hour, policy.GetVotingPeriod()) - testCases := map[string]struct{ + testCases := map[string]struct { sinceSubmission time.Duration - totalWeight sdk.Dec - tally foundation.TallyResult - valid bool - final bool - allow bool + totalWeight sdk.Dec + tally foundation.TallyResult + valid bool + final bool + allow bool }{ "allow": { sinceSubmission: policy.Windows.MinExecutionPeriod, - totalWeight: policy.Threshold, - tally: foundation.NewTallyResult(policy.Threshold, sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), - valid: true, - final: true, - allow: true, + totalWeight: policy.Threshold, + tally: foundation.NewTallyResult(policy.Threshold, sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), + valid: true, + final: true, + allow: true, }, "allow (member size < threshold)": { sinceSubmission: policy.Windows.MinExecutionPeriod, - totalWeight: config.MinThreshold, - tally: foundation.NewTallyResult(config.MinThreshold, sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), - valid: true, - final: true, - allow: true, + totalWeight: config.MinThreshold, + tally: foundation.NewTallyResult(config.MinThreshold, sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), + valid: true, + final: true, + allow: true, }, "not final": { sinceSubmission: policy.Windows.MinExecutionPeriod, - totalWeight: policy.Threshold, - tally: foundation.NewTallyResult(policy.Threshold.Sub(sdk.OneDec()), sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), - valid: true, + totalWeight: policy.Threshold, + tally: foundation.NewTallyResult(policy.Threshold.Sub(sdk.OneDec()), sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), + valid: true, }, "deny": { sinceSubmission: policy.Windows.MinExecutionPeriod, - totalWeight: policy.Threshold.Add(sdk.OneDec()), - tally: foundation.NewTallyResult(sdk.ZeroDec(), sdk.OneDec(), sdk.OneDec(), sdk.ZeroDec()), - valid: true, - final: true, + totalWeight: policy.Threshold.Add(sdk.OneDec()), + tally: foundation.NewTallyResult(sdk.ZeroDec(), sdk.OneDec(), sdk.OneDec(), sdk.ZeroDec()), + valid: true, + final: true, }, "too early": { sinceSubmission: policy.Windows.MinExecutionPeriod - time.Nanosecond, - totalWeight: policy.Threshold, - tally: foundation.NewTallyResult(policy.Threshold, sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), + totalWeight: policy.Threshold, + tally: foundation.NewTallyResult(policy.Threshold, sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), }, } @@ -177,35 +177,35 @@ func TestThresholdDecisionPolicyAllow(t *testing.T) { func TestPercentageDecisionPolicy(t *testing.T) { config := foundation.DefaultConfig() - testCases := map[string]struct{ - percentage sdk.Dec - votingPeriod time.Duration + testCases := map[string]struct { + percentage sdk.Dec + votingPeriod time.Duration minExecutionPeriod time.Duration - validBasic bool - valid bool + validBasic bool + valid bool }{ "valid policy": { - percentage: config.MinPercentage, - votingPeriod: time.Hour, + percentage: config.MinPercentage, + votingPeriod: time.Hour, minExecutionPeriod: config.MaxExecutionPeriod + time.Hour - time.Nanosecond, - validBasic: true, - valid: true, + validBasic: true, + valid: true, }, "invalid policy (basic)": { - percentage: config.MinPercentage, + percentage: config.MinPercentage, minExecutionPeriod: config.MaxExecutionPeriod - time.Nanosecond, }, "invalid policy": { - percentage: config.MinPercentage.Sub(sdk.SmallestDec()), - votingPeriod: time.Hour, + percentage: config.MinPercentage.Sub(sdk.SmallestDec()), + votingPeriod: time.Hour, minExecutionPeriod: config.MaxExecutionPeriod + time.Hour - time.Nanosecond, - validBasic: true, + validBasic: true, }, "invalid policy (windows)": { - percentage: config.MinPercentage, - votingPeriod: time.Hour, + percentage: config.MinPercentage, + votingPeriod: time.Hour, minExecutionPeriod: config.MaxExecutionPeriod + time.Hour, - validBasic: true, + validBasic: true, }, } @@ -213,7 +213,7 @@ func TestPercentageDecisionPolicy(t *testing.T) { policy := foundation.PercentageDecisionPolicy{ Percentage: tc.percentage, Windows: &foundation.DecisionPolicyWindows{ - VotingPeriod: tc.votingPeriod, + VotingPeriod: tc.votingPeriod, MinExecutionPeriod: tc.minExecutionPeriod, }, } @@ -248,47 +248,47 @@ func TestPercentageDecisionPolicyAllow(t *testing.T) { require.Equal(t, time.Hour, policy.GetVotingPeriod()) totalWeight := sdk.NewDec(10) - testCases := map[string]struct{ + testCases := map[string]struct { sinceSubmission time.Duration - tally foundation.TallyResult - valid bool - final bool - allow bool + tally foundation.TallyResult + valid bool + final bool + allow bool }{ "allow": { sinceSubmission: policy.Windows.MinExecutionPeriod, - tally: foundation.NewTallyResult(sdk.NewDec(8), sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), - valid: true, - final: true, - allow: true, + tally: foundation.NewTallyResult(sdk.NewDec(8), sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), + valid: true, + final: true, + allow: true, }, "allow (abstain)": { sinceSubmission: policy.Windows.MinExecutionPeriod, - tally: foundation.NewTallyResult(sdk.NewDec(4), sdk.NewDec(5), sdk.ZeroDec(), sdk.ZeroDec()), - valid: true, - final: true, - allow: true, + tally: foundation.NewTallyResult(sdk.NewDec(4), sdk.NewDec(5), sdk.ZeroDec(), sdk.ZeroDec()), + valid: true, + final: true, + allow: true, }, "not final": { sinceSubmission: policy.Windows.MinExecutionPeriod, - tally: foundation.NewTallyResult(sdk.ZeroDec(), sdk.NewDec(5), sdk.NewDec(1), sdk.ZeroDec()), - valid: true, + tally: foundation.NewTallyResult(sdk.ZeroDec(), sdk.NewDec(5), sdk.NewDec(1), sdk.ZeroDec()), + valid: true, }, "deny": { sinceSubmission: policy.Windows.MinExecutionPeriod, - tally: foundation.NewTallyResult(sdk.ZeroDec(), sdk.ZeroDec(), sdk.NewDec(3), sdk.ZeroDec()), - valid: true, - final: true, + tally: foundation.NewTallyResult(sdk.ZeroDec(), sdk.ZeroDec(), sdk.NewDec(3), sdk.ZeroDec()), + valid: true, + final: true, }, "deny (all abstain)": { sinceSubmission: policy.Windows.MinExecutionPeriod, - tally: foundation.NewTallyResult(sdk.ZeroDec(), sdk.NewDec(10), sdk.ZeroDec(), sdk.ZeroDec()), - valid: true, - final: true, + tally: foundation.NewTallyResult(sdk.ZeroDec(), sdk.NewDec(10), sdk.ZeroDec(), sdk.ZeroDec()), + valid: true, + final: true, }, "too early": { sinceSubmission: policy.Windows.MinExecutionPeriod - time.Nanosecond, - tally: foundation.NewTallyResult(sdk.NewDec(8), sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), + tally: foundation.NewTallyResult(sdk.NewDec(8), sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), }, } diff --git a/x/foundation/genesis_test.go b/x/foundation/genesis_test.go index 582c7e4ba5..db87c2ffc5 100644 --- a/x/foundation/genesis_test.go +++ b/x/foundation/genesis_test.go @@ -22,20 +22,19 @@ func TestValidateGenesis(t *testing.T) { return sdk.BytesToAccAddress(secp256k1.GenPrivKey().PubKey().Address()) } - testCases := map[string]struct{ - data foundation.GenesisState + testCases := map[string]struct { + data foundation.GenesisState valid bool }{ "minimal": { - data: foundation.GenesisState{ - }, + data: foundation.GenesisState{}, valid: true, }, "members": { data: foundation.GenesisState{ Members: []foundation.Member{ { - Address: createAddress().String(), + Address: createAddress().String(), Participating: true, }, }, @@ -46,7 +45,7 @@ func TestValidateGenesis(t *testing.T) { data: foundation.GenesisState{ Foundation: &foundation.FoundationInfo{ Operator: createAddress().String(), - Version: 1, + Version: 1, }, }, valid: true, @@ -73,7 +72,7 @@ func TestValidateGenesis(t *testing.T) { data: foundation.GenesisState{ Members: []foundation.Member{ { - Address: "invalid-address", + Address: "invalid-address", Participating: true, }, }, @@ -83,21 +82,20 @@ func TestValidateGenesis(t *testing.T) { data: foundation.GenesisState{ Foundation: &foundation.FoundationInfo{ Operator: "invalid-address", - Version: 1, + Version: 1, }, }, }, "invalid foundation version": { data: foundation.GenesisState{ - Foundation: &foundation.FoundationInfo{ - }, + Foundation: &foundation.FoundationInfo{}, }, }, "invalid decision policy": { data: foundation.GenesisState{ Foundation: foundation.FoundationInfo{ Operator: createAddress().String(), - Version: 1, + Version: 1, }.WithDecisionPolicy(&foundation.ThresholdDecisionPolicy{ Windows: &foundation.DecisionPolicyWindows{}, }), @@ -107,7 +105,7 @@ func TestValidateGenesis(t *testing.T) { data: foundation.GenesisState{ Proposals: []foundation.Proposal{ { - Id: 1, + Id: 1, FoundationVersion: 1, }, }, @@ -117,12 +115,12 @@ func TestValidateGenesis(t *testing.T) { data: foundation.GenesisState{ Proposals: []foundation.Proposal{ *foundation.Proposal{ - Id: 1, + Id: 1, Proposers: []string{createAddress().String()}, }.WithMsgs([]sdk.Msg{&foundation.MsgWithdrawFromTreasury{ Operator: createAddress().String(), - To: createAddress().String(), - Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())), + To: createAddress().String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())), }}), }, }, @@ -131,8 +129,8 @@ func TestValidateGenesis(t *testing.T) { data: foundation.GenesisState{ Proposals: []foundation.Proposal{ { - Id: 1, - Proposers: []string{createAddress().String()}, + Id: 1, + Proposers: []string{createAddress().String()}, FoundationVersion: 1, }, }, @@ -142,22 +140,22 @@ func TestValidateGenesis(t *testing.T) { data: foundation.GenesisState{ Proposals: []foundation.Proposal{ *foundation.Proposal{ - Id: 1, - Proposers: []string{createAddress().String()}, + Id: 1, + Proposers: []string{createAddress().String()}, FoundationVersion: 1, }.WithMsgs([]sdk.Msg{&foundation.MsgWithdrawFromTreasury{ Operator: createAddress().String(), - To: createAddress().String(), - Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())), + To: createAddress().String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())), }}), *foundation.Proposal{ - Id: 1, - Proposers: []string{createAddress().String()}, + Id: 1, + Proposers: []string{createAddress().String()}, FoundationVersion: 1, }.WithMsgs([]sdk.Msg{&foundation.MsgWithdrawFromTreasury{ Operator: createAddress().String(), - To: createAddress().String(), - Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())), + To: createAddress().String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())), }}), }, }, @@ -167,8 +165,8 @@ func TestValidateGenesis(t *testing.T) { Votes: []foundation.Vote{ { ProposalId: 1, - Voter: createAddress().String(), - Option: foundation.VOTE_OPTION_YES, + Voter: createAddress().String(), + Option: foundation.VOTE_OPTION_YES, }, }, }, @@ -177,20 +175,20 @@ func TestValidateGenesis(t *testing.T) { data: foundation.GenesisState{ Proposals: []foundation.Proposal{ *foundation.Proposal{ - Id: 1, - Proposers: []string{createAddress().String()}, + Id: 1, + Proposers: []string{createAddress().String()}, FoundationVersion: 1, }.WithMsgs([]sdk.Msg{&foundation.MsgWithdrawFromTreasury{ Operator: createAddress().String(), - To: createAddress().String(), - Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())), + To: createAddress().String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())), }}), }, Votes: []foundation.Vote{ { ProposalId: 1, - Voter: "invalid-address", - Option: foundation.VOTE_OPTION_YES, + Voter: "invalid-address", + Option: foundation.VOTE_OPTION_YES, }, }, }, @@ -199,19 +197,19 @@ func TestValidateGenesis(t *testing.T) { data: foundation.GenesisState{ Proposals: []foundation.Proposal{ *foundation.Proposal{ - Id: 1, - Proposers: []string{createAddress().String()}, + Id: 1, + Proposers: []string{createAddress().String()}, FoundationVersion: 1, }.WithMsgs([]sdk.Msg{&foundation.MsgWithdrawFromTreasury{ Operator: createAddress().String(), - To: createAddress().String(), - Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())), + To: createAddress().String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())), }}), }, Votes: []foundation.Vote{ { ProposalId: 1, - Voter: createAddress().String(), + Voter: createAddress().String(), }, }, }, diff --git a/x/foundation/keeper/abci_test.go b/x/foundation/keeper/abci_test.go new file mode 100644 index 0000000000..9990d41510 --- /dev/null +++ b/x/foundation/keeper/abci_test.go @@ -0,0 +1,28 @@ +package keeper_test + +import ( + sdk "github.com/line/lbm-sdk/types" + "github.com/line/lbm-sdk/x/foundation" + "github.com/line/lbm-sdk/x/foundation/keeper" +) + +func (s *KeeperTestSuite) TestBeginBlocker() { + ctx, _ := s.ctx.CacheContext() + + s.keeper.SetParams(ctx, &foundation.Params{ + Enabled: true, + FoundationTax: sdk.MustNewDecFromStr("0.5"), + }) + + before := s.keeper.GetTreasury(ctx) + s.Require().Equal(1, len(before)) + s.Require().Equal(s.balance, before[0].Amount) + + // collect + keeper.BeginBlocker(ctx, s.keeper) + + after := s.keeper.GetTreasury(ctx) + s.Require().Equal(1, len(after)) + // s.balance + s.balance * 0.5 + s.Require().Equal(s.balance.Add(s.balance.Quo(sdk.NewInt(2))), after[0].Amount) +} diff --git a/x/foundation/keeper/exec.go b/x/foundation/keeper/exec.go index 2f5d464d13..24ed3d6556 100644 --- a/x/foundation/keeper/exec.go +++ b/x/foundation/keeper/exec.go @@ -29,7 +29,7 @@ func (k Keeper) Exec(ctx sdk.Context, proposalID uint64) error { if proposal.Status != foundation.PROPOSAL_STATUS_SUBMITTED && proposal.Status != foundation.PROPOSAL_STATUS_CLOSED { - return sdkerrors.ErrInvalidRequest.Wrapf("not possible with proposal status: %s", proposal.Status.String()) + return sdkerrors.ErrInvalidRequest.Wrapf("not possible with proposal status: %s", proposal.Status) } if proposal.Status == foundation.PROPOSAL_STATUS_SUBMITTED { @@ -59,15 +59,17 @@ func (k Keeper) Exec(ctx sdk.Context, proposalID uint64) error { if proposal.ExecutorResult == foundation.PROPOSAL_EXECUTOR_RESULT_SUCCESS { k.pruneProposal(ctx, *proposal) } else { - if err := k.setProposal(ctx, *proposal); err != nil { - return err - } + k.setProposal(ctx, *proposal) } - return ctx.EventManager().EmitTypedEvent(&foundation.EventExec{ + if err := ctx.EventManager().EmitTypedEvent(&foundation.EventExec{ ProposalId: proposal.Id, Result: proposal.ExecutorResult, - }) + }); err != nil { + panic(err) + } + + return nil } // doExecuteMsgs routes the messages to the registered handlers. diff --git a/x/foundation/keeper/exec_test.go b/x/foundation/keeper/exec_test.go index f4c8499be9..7a0e54c036 100644 --- a/x/foundation/keeper/exec_test.go +++ b/x/foundation/keeper/exec_test.go @@ -1,21 +1,21 @@ package keeper_test func (s *KeeperTestSuite) TestExec() { - testCases := map[string]struct{ + testCases := map[string]struct { proposalID uint64 - valid bool + valid bool }{ "valid exec": { proposalID: s.votedProposal, - valid: true, + valid: true, }, "not enough votes": { proposalID: s.activeProposal, - valid: true, + valid: true, }, "invalid msg in proposal": { proposalID: s.invalidProposal, - valid: true, + valid: true, }, "aborted proposal": { proposalID: s.abortedProposal, diff --git a/x/foundation/keeper/genesis.go b/x/foundation/keeper/genesis.go index 1052edf491..854556231c 100644 --- a/x/foundation/keeper/genesis.go +++ b/x/foundation/keeper/genesis.go @@ -93,9 +93,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, sk foundation.StakingKeeper, data * } } - if err := k.setFoundationInfo(ctx, *info); err != nil { - return err - } + k.setFoundationInfo(ctx, *info) k.setPreviousProposalID(ctx, data.PreviousProposalId) @@ -104,9 +102,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, sk foundation.StakingKeeper, data * return err } - if err := k.setProposal(ctx, proposal); err != nil { - return err - } + k.setProposal(ctx, proposal) k.addProposalToVPEndQueue(ctx, proposal) } @@ -115,15 +111,11 @@ func (k Keeper) InitGenesis(ctx sdk.Context, sk foundation.StakingKeeper, data * return err } - if err := k.setVote(ctx, vote); err != nil { - return err - } + k.setVote(ctx, vote) } for _, ga := range authorizations { - if err := k.setAuthorization(ctx, ga.Granter, sdk.AccAddress(ga.Grantee), ga.GetAuthorization()); err != nil { - return err - } + k.setAuthorization(ctx, ga.Granter, sdk.AccAddress(ga.Grantee), ga.GetAuthorization()) } return nil diff --git a/x/foundation/keeper/genesis_test.go b/x/foundation/keeper/genesis_test.go index 070258f78c..7afa21a31a 100644 --- a/x/foundation/keeper/genesis_test.go +++ b/x/foundation/keeper/genesis_test.go @@ -4,25 +4,24 @@ import ( sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/x/foundation" - "github.com/line/lbm-sdk/x/stakingplus" govtypes "github.com/line/lbm-sdk/x/gov/types" + "github.com/line/lbm-sdk/x/stakingplus" ) func (s *KeeperTestSuite) TestImportExportGenesis() { - testCases := map[string]struct{ - init *foundation.GenesisState - valid bool + testCases := map[string]struct { + init *foundation.GenesisState + valid bool export *foundation.GenesisState }{ "minimal": { - init: &foundation.GenesisState{ - }, + init: &foundation.GenesisState{}, valid: true, export: &foundation.GenesisState{ Params: foundation.DefaultParams(), Foundation: foundation.FoundationInfo{ - Operator: s.keeper.GetAdmin(s.ctx).String(), - Version: 1, + Operator: s.keeper.GetAdmin(s.ctx).String(), + Version: 1, TotalWeight: sdk.ZeroDec(), }.WithDecisionPolicy(foundation.DefaultDecisionPolicy(foundation.DefaultConfig())), }, @@ -30,19 +29,19 @@ func (s *KeeperTestSuite) TestImportExportGenesis() { "enabled with no create validator grantees": { init: &foundation.GenesisState{ Params: &foundation.Params{ - Enabled: true, + Enabled: true, FoundationTax: sdk.ZeroDec(), }, }, valid: true, export: &foundation.GenesisState{ Params: &foundation.Params{ - Enabled: true, + Enabled: true, FoundationTax: sdk.ZeroDec(), }, Foundation: foundation.FoundationInfo{ - Operator: s.keeper.GetAdmin(s.ctx).String(), - Version: 1, + Operator: s.keeper.GetAdmin(s.ctx).String(), + Version: 1, TotalWeight: sdk.ZeroDec(), }.WithDecisionPolicy(foundation.DefaultDecisionPolicy(foundation.DefaultConfig())), }, @@ -51,7 +50,7 @@ func (s *KeeperTestSuite) TestImportExportGenesis() { init: &foundation.GenesisState{ Members: []foundation.Member{ { - Address: s.members[0].String(), + Address: s.members[0].String(), Participating: true, }, }, @@ -60,13 +59,13 @@ func (s *KeeperTestSuite) TestImportExportGenesis() { export: &foundation.GenesisState{ Params: foundation.DefaultParams(), Foundation: foundation.FoundationInfo{ - Operator: s.keeper.GetAdmin(s.ctx).String(), - Version: 1, + Operator: s.keeper.GetAdmin(s.ctx).String(), + Version: 1, TotalWeight: sdk.OneDec(), }.WithDecisionPolicy(foundation.DefaultDecisionPolicy(foundation.DefaultConfig())), Members: []foundation.Member{ { - Address: s.members[0].String(), + Address: s.members[0].String(), Participating: true, }, }, @@ -76,20 +75,20 @@ func (s *KeeperTestSuite) TestImportExportGenesis() { init: &foundation.GenesisState{ Proposals: []foundation.Proposal{ *foundation.Proposal{ - Id: 1, - Proposers: []string{s.members[0].String()}, + Id: 1, + Proposers: []string{s.members[0].String()}, FoundationVersion: 1, }.WithMsgs([]sdk.Msg{&foundation.MsgWithdrawFromTreasury{ Operator: s.operator.String(), - To: s.stranger.String(), - Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance)), + To: s.stranger.String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance)), }}), }, Votes: []foundation.Vote{ { ProposalId: 1, - Voter: s.members[0].String(), - Option: foundation.VOTE_OPTION_YES, + Voter: s.members[0].String(), + Option: foundation.VOTE_OPTION_YES, }, }, }, @@ -97,32 +96,32 @@ func (s *KeeperTestSuite) TestImportExportGenesis() { export: &foundation.GenesisState{ Params: foundation.DefaultParams(), Foundation: foundation.FoundationInfo{ - Operator: s.keeper.GetAdmin(s.ctx).String(), - Version: 1, + Operator: s.keeper.GetAdmin(s.ctx).String(), + Version: 1, TotalWeight: sdk.ZeroDec(), }.WithDecisionPolicy(foundation.DefaultDecisionPolicy(foundation.DefaultConfig())), Proposals: []foundation.Proposal{ *foundation.Proposal{ - Id: 1, - Proposers: []string{s.members[0].String()}, + Id: 1, + Proposers: []string{s.members[0].String()}, FoundationVersion: 1, FinalTallyResult: foundation.TallyResult{ - YesCount: sdk.ZeroDec(), - NoCount: sdk.ZeroDec(), - AbstainCount: sdk.ZeroDec(), + YesCount: sdk.ZeroDec(), + NoCount: sdk.ZeroDec(), + AbstainCount: sdk.ZeroDec(), NoWithVetoCount: sdk.ZeroDec(), }, }.WithMsgs([]sdk.Msg{&foundation.MsgWithdrawFromTreasury{ Operator: s.operator.String(), - To: s.stranger.String(), - Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance)), + To: s.stranger.String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance)), }}), }, Votes: []foundation.Vote{ { ProposalId: 1, - Voter: s.members[0].String(), - Option: foundation.VOTE_OPTION_YES, + Voter: s.members[0].String(), + Option: foundation.VOTE_OPTION_YES, }, }, }, @@ -140,8 +139,8 @@ func (s *KeeperTestSuite) TestImportExportGenesis() { export: &foundation.GenesisState{ Params: foundation.DefaultParams(), Foundation: foundation.FoundationInfo{ - Operator: s.keeper.GetAdmin(s.ctx).String(), - Version: 1, + Operator: s.keeper.GetAdmin(s.ctx).String(), + Version: 1, TotalWeight: sdk.ZeroDec(), }.WithDecisionPolicy(foundation.DefaultDecisionPolicy(foundation.DefaultConfig())), Authorizations: []foundation.GrantAuthorization{ @@ -167,8 +166,8 @@ func (s *KeeperTestSuite) TestImportExportGenesis() { export: &foundation.GenesisState{ Params: foundation.DefaultParams(), Foundation: foundation.FoundationInfo{ - Operator: s.keeper.GetAdmin(s.ctx).String(), - Version: 1, + Operator: s.keeper.GetAdmin(s.ctx).String(), + Version: 1, TotalWeight: sdk.OneDec(), }.WithDecisionPolicy(foundation.DefaultDecisionPolicy(foundation.DefaultConfig())), Authorizations: []foundation.GrantAuthorization{ @@ -180,9 +179,9 @@ func (s *KeeperTestSuite) TestImportExportGenesis() { }), }, Members: []foundation.Member{{ - Address: s.stranger.String(), + Address: s.stranger.String(), Participating: true, - Metadata: "genesis member", + Metadata: "genesis member", }}, }, }, @@ -190,9 +189,9 @@ func (s *KeeperTestSuite) TestImportExportGenesis() { init: &foundation.GenesisState{ Members: []foundation.Member{ { - Address: s.members[0].String(), + Address: s.members[0].String(), Participating: true, - Metadata: string(make([]rune, 256)), + Metadata: string(make([]rune, 256)), }, }, }, @@ -201,14 +200,14 @@ func (s *KeeperTestSuite) TestImportExportGenesis() { init: &foundation.GenesisState{ Proposals: []foundation.Proposal{ *foundation.Proposal{ - Id: 1, - Metadata: string(make([]rune, 256)), - Proposers: []string{s.members[0].String()}, + Id: 1, + Metadata: string(make([]rune, 256)), + Proposers: []string{s.members[0].String()}, FoundationVersion: 1, }.WithMsgs([]sdk.Msg{&foundation.MsgWithdrawFromTreasury{ Operator: s.operator.String(), - To: s.stranger.String(), - Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance)), + To: s.stranger.String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance)), }}), }, }, @@ -217,21 +216,21 @@ func (s *KeeperTestSuite) TestImportExportGenesis() { init: &foundation.GenesisState{ Proposals: []foundation.Proposal{ *foundation.Proposal{ - Id: 1, - Proposers: []string{s.members[0].String()}, + Id: 1, + Proposers: []string{s.members[0].String()}, FoundationVersion: 1, }.WithMsgs([]sdk.Msg{&foundation.MsgWithdrawFromTreasury{ Operator: s.operator.String(), - To: s.stranger.String(), - Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance)), + To: s.stranger.String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance)), }}), }, Votes: []foundation.Vote{ { ProposalId: 1, - Voter: s.members[0].String(), - Option: foundation.VOTE_OPTION_YES, - Metadata: string(make([]rune, 256)), + Voter: s.members[0].String(), + Option: foundation.VOTE_OPTION_YES, + Metadata: string(make([]rune, 256)), }, }, }, @@ -245,7 +244,7 @@ func (s *KeeperTestSuite) TestImportExportGenesis() { err := foundation.ValidateGenesis(*tc.init) s.Require().NoError(err) - + err = s.keeper.InitGenesis(ctx, s.app.StakingKeeper, tc.init) if !tc.valid { s.Require().Error(err) diff --git a/x/foundation/keeper/grant.go b/x/foundation/keeper/grant.go index 2ba44d0388..b9f12d86d8 100644 --- a/x/foundation/keeper/grant.go +++ b/x/foundation/keeper/grant.go @@ -11,9 +11,7 @@ func (k Keeper) Grant(ctx sdk.Context, granter string, grantee sdk.AccAddress, a return sdkerrors.ErrInvalidRequest.Wrapf("authorization for %s already exists", authorization.MsgTypeURL()) } - if err := k.setAuthorization(ctx, granter, grantee, authorization); err != nil { - return err - } + k.setAuthorization(ctx, granter, grantee, authorization) any, err := foundation.SetAuthorization(authorization) if err != nil { @@ -24,7 +22,7 @@ func (k Keeper) Grant(ctx sdk.Context, granter string, grantee sdk.AccAddress, a Grantee: grantee.String(), Authorization: any, }); err != nil { - return err + panic(err) } return nil @@ -41,7 +39,7 @@ func (k Keeper) Revoke(ctx sdk.Context, granter string, grantee sdk.AccAddress, Grantee: grantee.String(), MsgTypeUrl: msgTypeURL, }); err != nil { - return err + panic(err) } return nil @@ -57,23 +55,21 @@ func (k Keeper) GetAuthorization(ctx sdk.Context, granter string, grantee sdk.Ac var auth foundation.Authorization if err := k.cdc.UnmarshalInterface(bz, &auth); err != nil { - return nil, err + panic(err) } return auth, nil } -func (k Keeper) setAuthorization(ctx sdk.Context, granter string, grantee sdk.AccAddress, authorization foundation.Authorization) error { +func (k Keeper) setAuthorization(ctx sdk.Context, granter string, grantee sdk.AccAddress, authorization foundation.Authorization) { store := ctx.KVStore(k.storeKey) key := grantKey(grantee, authorization.MsgTypeURL(), granter) bz, err := k.cdc.MarshalInterface(authorization) if err != nil { - return err + panic(err) } store.Set(key, bz) - - return nil } func (k Keeper) deleteAuthorization(ctx sdk.Context, granter string, grantee sdk.AccAddress, msgTypeURL string) { @@ -97,9 +93,7 @@ func (k Keeper) Accept(ctx sdk.Context, granter string, grantee sdk.AccAddress, if resp.Delete { k.deleteAuthorization(ctx, granter, grantee, msgTypeURL) } else if resp.Updated != nil { - if err := k.setAuthorization(ctx, granter, grantee, resp.Updated); err != nil { - return err - } + k.setAuthorization(ctx, granter, grantee, resp.Updated) } if !resp.Accept { diff --git a/x/foundation/keeper/grant_test.go b/x/foundation/keeper/grant_test.go index fc098b9d8c..1c2f568134 100644 --- a/x/foundation/keeper/grant_test.go +++ b/x/foundation/keeper/grant_test.go @@ -8,17 +8,17 @@ import ( func (s *KeeperTestSuite) TestGrant() { testCases := map[string]struct { grantee sdk.AccAddress - auth foundation.Authorization - valid bool + auth foundation.Authorization + valid bool }{ "valid authz": { grantee: s.members[0], - auth: &foundation.ReceiveFromTreasuryAuthorization{}, - valid: true, + auth: &foundation.ReceiveFromTreasuryAuthorization{}, + valid: true, }, "override attempt": { grantee: s.stranger, - auth: &foundation.ReceiveFromTreasuryAuthorization{}, + auth: &foundation.ReceiveFromTreasuryAuthorization{}, }, } @@ -39,17 +39,17 @@ func (s *KeeperTestSuite) TestGrant() { func (s *KeeperTestSuite) TestRevoke() { testCases := map[string]struct { grantee sdk.AccAddress - url string - valid bool + url string + valid bool }{ "valid url": { grantee: s.stranger, - url: foundation.ReceiveFromTreasuryAuthorization{}.MsgTypeURL(), - valid: true, + url: foundation.ReceiveFromTreasuryAuthorization{}.MsgTypeURL(), + valid: true, }, "grant not found": { grantee: s.members[0], - url: foundation.ReceiveFromTreasuryAuthorization{}.MsgTypeURL(), + url: foundation.ReceiveFromTreasuryAuthorization{}.MsgTypeURL(), }, } diff --git a/x/foundation/keeper/grpc_query.go b/x/foundation/keeper/grpc_query.go index 90b33e84a7..46b6955f5e 100644 --- a/x/foundation/keeper/grpc_query.go +++ b/x/foundation/keeper/grpc_query.go @@ -206,16 +206,16 @@ func (s queryServer) Grants(c context.Context, req *foundation.QueryGrantsReques _, err := query.Paginate(grantStore, req.Pagination, func(key []byte, value []byte) error { var authorization foundation.Authorization if err := s.keeper.cdc.UnmarshalInterface(value, &authorization); err != nil { - return err + panic(err) } msg, ok := authorization.(proto.Message) if !ok { - return sdkerrors.ErrInvalidType.Wrapf("can't proto marshal %T", msg) + panic(sdkerrors.ErrInvalidType.Wrapf("can't proto marshal %T", msg)) } any, err := codectypes.NewAnyWithValue(msg) if err != nil { - return err + panic(err) } authorizations = append(authorizations, any) @@ -236,16 +236,16 @@ func (s queryServer) Grants(c context.Context, req *foundation.QueryGrantsReques pageRes, err := query.Paginate(grantStore, req.Pagination, func(key []byte, value []byte) error { var authorization foundation.Authorization if err := s.keeper.cdc.UnmarshalInterface(value, &authorization); err != nil { - return err + panic(err) } msg, ok := authorization.(proto.Message) if !ok { - return sdkerrors.ErrInvalidType.Wrapf("can't proto marshal %T", msg) + panic(sdkerrors.ErrInvalidType.Wrapf("can't proto marshal %T", msg)) } any, err := codectypes.NewAnyWithValue(msg) if err != nil { - return err + panic(err) } authorizations = append(authorizations, any) diff --git a/x/foundation/keeper/handler.go b/x/foundation/keeper/handler.go index 2f15e8d396..8c7a467189 100644 --- a/x/foundation/keeper/handler.go +++ b/x/foundation/keeper/handler.go @@ -19,7 +19,7 @@ func NewProposalHandler(k Keeper) govtypes.Handler { case *foundation.UpdateValidatorAuthsProposal: return k.handleUpdateValidatorAuthsProposal(ctx, c) default: - return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized foundation proposal content type: %T", c) + return sdkerrors.ErrUnknownRequest.Wrapf("unrecognized foundation proposal content type: %T", c) } } } diff --git a/x/foundation/keeper/keeper_test.go b/x/foundation/keeper/keeper_test.go index c10584bca5..70dac3ae6c 100644 --- a/x/foundation/keeper/keeper_test.go +++ b/x/foundation/keeper/keeper_test.go @@ -11,11 +11,12 @@ import ( "github.com/line/lbm-sdk/crypto/keys/secp256k1" "github.com/line/lbm-sdk/simapp" sdk "github.com/line/lbm-sdk/types" + authtypes "github.com/line/lbm-sdk/x/auth/types" "github.com/line/lbm-sdk/x/foundation" "github.com/line/lbm-sdk/x/foundation/keeper" - "github.com/line/lbm-sdk/x/stakingplus" govtypes "github.com/line/lbm-sdk/x/gov/types" minttypes "github.com/line/lbm-sdk/x/mint/types" + "github.com/line/lbm-sdk/x/stakingplus" ) var ( @@ -40,19 +41,19 @@ func TestCleanup(t *testing.T) { type KeeperTestSuite struct { suite.Suite - ctx sdk.Context + ctx sdk.Context - app *simapp.SimApp - keeper keeper.Keeper + app *simapp.SimApp + keeper keeper.Keeper queryServer foundation.QueryServer - msgServer foundation.MsgServer + msgServer foundation.MsgServer operator sdk.AccAddress - members []sdk.AccAddress + members []sdk.AccAddress stranger sdk.AccAddress - activeProposal uint64 - votedProposal uint64 + activeProposal uint64 + votedProposal uint64 abortedProposal uint64 invalidProposal uint64 @@ -83,6 +84,7 @@ func (s *KeeperTestSuite) SetupTest() { holders := []sdk.AccAddress{ s.stranger, s.app.AccountKeeper.GetModuleAccount(s.ctx, foundation.TreasuryName).GetAddress(), + s.app.AccountKeeper.GetModuleAccount(s.ctx, authtypes.FeeCollectorName).GetAddress(), } for _, holder := range holders { amount := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance)) @@ -103,7 +105,7 @@ func (s *KeeperTestSuite) SetupTest() { updates := make([]foundation.Member, len(s.members)) for i, member := range s.members { updates[i] = foundation.Member{ - Address: member.String(), + Address: member.String(), Participating: true, } } @@ -114,16 +116,16 @@ func (s *KeeperTestSuite) SetupTest() { s.activeProposal, err = s.keeper.SubmitProposal(s.ctx, []string{s.members[0].String()}, "", []sdk.Msg{ &foundation.MsgWithdrawFromTreasury{ Operator: s.operator.String(), - To: s.stranger.String(), - Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance)), + To: s.stranger.String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance)), }, }) s.Require().NoError(err) for _, member := range s.members[1:] { err := s.keeper.Vote(s.ctx, foundation.Vote{ ProposalId: s.activeProposal, - Voter: member.String(), - Option: foundation.VOTE_OPTION_YES, + Voter: member.String(), + Option: foundation.VOTE_OPTION_YES, }) s.Require().NoError(err) } @@ -132,16 +134,16 @@ func (s *KeeperTestSuite) SetupTest() { s.votedProposal, err = s.keeper.SubmitProposal(s.ctx, []string{s.members[0].String()}, "", []sdk.Msg{ &foundation.MsgWithdrawFromTreasury{ Operator: s.operator.String(), - To: s.stranger.String(), - Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance)), + To: s.stranger.String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance)), }, }) s.Require().NoError(err) for _, member := range s.members { err := s.keeper.Vote(s.ctx, foundation.Vote{ ProposalId: s.votedProposal, - Voter: member.String(), - Option: foundation.VOTE_OPTION_YES, + Voter: member.String(), + Option: foundation.VOTE_OPTION_YES, }) s.Require().NoError(err) } @@ -150,8 +152,8 @@ func (s *KeeperTestSuite) SetupTest() { s.abortedProposal, err = s.keeper.SubmitProposal(s.ctx, []string{s.members[0].String()}, "", []sdk.Msg{ &foundation.MsgWithdrawFromTreasury{ Operator: s.operator.String(), - To: s.stranger.String(), - Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance)), + To: s.stranger.String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance)), }, }) s.Require().NoError(err) @@ -162,16 +164,16 @@ func (s *KeeperTestSuite) SetupTest() { s.invalidProposal, err = s.keeper.SubmitProposal(s.ctx, []string{s.members[0].String()}, "", []sdk.Msg{ &foundation.MsgWithdrawFromTreasury{ Operator: s.operator.String(), - To: s.stranger.String(), - Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance.Add(sdk.OneInt()))), + To: s.stranger.String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance.Add(sdk.OneInt()))), }, }) s.Require().NoError(err) for _, member := range s.members { err := s.keeper.Vote(s.ctx, foundation.Vote{ ProposalId: s.invalidProposal, - Voter: member.String(), - Option: foundation.VOTE_OPTION_YES, + Voter: member.String(), + Option: foundation.VOTE_OPTION_YES, }) s.Require().NoError(err) } diff --git a/x/foundation/keeper/member.go b/x/foundation/keeper/member.go index 63a6eda23d..7deea3a436 100644 --- a/x/foundation/keeper/member.go +++ b/x/foundation/keeper/member.go @@ -42,16 +42,10 @@ func (k Keeper) GetFoundationInfo(ctx sdk.Context) foundation.FoundationInfo { return info } -func (k Keeper) setFoundationInfo(ctx sdk.Context, info foundation.FoundationInfo) error { - bz, err := k.cdc.Marshal(&info) - if err != nil { - return err - } - +func (k Keeper) setFoundationInfo(ctx sdk.Context, info foundation.FoundationInfo) { store := ctx.KVStore(k.storeKey) + bz := k.cdc.MustMarshal(&info) store.Set(foundationInfoKey, bz) - - return nil } func (k Keeper) UpdateMembers(ctx sdk.Context, members []foundation.Member) error { @@ -97,27 +91,21 @@ func (k Keeper) GetMember(ctx sdk.Context, address sdk.AccAddress) (*foundation. key := memberKey(address) bz := store.Get(key) if len(bz) == 0 { - return nil, sdkerrors.Wrapf(sdkerrors.ErrNotFound, "No such member: %s", address.String()) + return nil, sdkerrors.ErrNotFound.Wrapf("No such member: %s", address) } var member foundation.Member - if err := k.cdc.Unmarshal(bz, &member); err != nil { - return nil, err - } + k.cdc.MustUnmarshal(bz, &member) + return &member, nil } -func (k Keeper) setMember(ctx sdk.Context, member foundation.Member) error { - bz, err := k.cdc.Marshal(&member) - if err != nil { - return err - } - +func (k Keeper) setMember(ctx sdk.Context, member foundation.Member) { store := ctx.KVStore(k.storeKey) key := memberKey(sdk.AccAddress(member.Address)) - store.Set(key, bz) - return nil + bz := k.cdc.MustMarshal(&member) + store.Set(key, bz) } func (k Keeper) deleteMember(ctx sdk.Context, address sdk.AccAddress) { @@ -163,9 +151,7 @@ func (k Keeper) UpdateOperator(ctx sdk.Context, operator sdk.AccAddress) error { } info.Operator = operator.String() - if err := k.setFoundationInfo(ctx, info); err != nil { - return err - } + k.setFoundationInfo(ctx, info) return nil } diff --git a/x/foundation/keeper/member_test.go b/x/foundation/keeper/member_test.go index 20d99812c4..077d880874 100644 --- a/x/foundation/keeper/member_test.go +++ b/x/foundation/keeper/member_test.go @@ -9,9 +9,9 @@ import ( func (s *KeeperTestSuite) TestUpdateDecisionPolicy() { config := foundation.DefaultConfig() - testCases := map[string]struct{ + testCases := map[string]struct { policy foundation.DecisionPolicy - valid bool + valid bool }{ "threshold policy (valid)": { policy: &foundation.ThresholdDecisionPolicy{ @@ -34,7 +34,7 @@ func (s *KeeperTestSuite) TestUpdateDecisionPolicy() { policy: &foundation.ThresholdDecisionPolicy{ Threshold: config.MinThreshold, Windows: &foundation.DecisionPolicyWindows{ - VotingPeriod: time.Hour, + VotingPeriod: time.Hour, MinExecutionPeriod: time.Hour + config.MaxExecutionPeriod, }, }, @@ -60,7 +60,7 @@ func (s *KeeperTestSuite) TestUpdateDecisionPolicy() { policy: &foundation.PercentageDecisionPolicy{ Percentage: config.MinPercentage, Windows: &foundation.DecisionPolicyWindows{ - VotingPeriod: time.Hour, + VotingPeriod: time.Hour, MinExecutionPeriod: time.Hour + config.MaxExecutionPeriod, }, }, @@ -82,14 +82,14 @@ func (s *KeeperTestSuite) TestUpdateDecisionPolicy() { } func (s *KeeperTestSuite) TestUpdateMembers() { - testCases := map[string]struct{ + testCases := map[string]struct { updates []foundation.Member - valid bool + valid bool }{ "add a new member": { updates: []foundation.Member{ { - Address: s.stranger.String(), + Address: s.stranger.String(), Participating: true, }, }, @@ -113,9 +113,9 @@ func (s *KeeperTestSuite) TestUpdateMembers() { "long metadata": { updates: []foundation.Member{ { - Address: s.stranger.String(), + Address: s.stranger.String(), Participating: true, - Metadata: string(make([]rune, 256)), + Metadata: string(make([]rune, 256)), }, }, }, @@ -136,13 +136,13 @@ func (s *KeeperTestSuite) TestUpdateMembers() { } func (s *KeeperTestSuite) TestUpdateOperator() { - testCases := map[string]struct{ + testCases := map[string]struct { operator sdk.AccAddress - valid bool + valid bool }{ "valid new operator": { operator: s.stranger, - valid: true, + valid: true, }, "already the operator": { operator: s.operator, diff --git a/x/foundation/keeper/msg_server.go b/x/foundation/keeper/msg_server.go index 16bd587a9f..5157fc62cf 100644 --- a/x/foundation/keeper/msg_server.go +++ b/x/foundation/keeper/msg_server.go @@ -42,7 +42,7 @@ func (s msgServer) FundTreasury(c context.Context, req *foundation.MsgFundTreasu From: req.From, Amount: req.Amount, }); err != nil { - return nil, err + panic(err) } return &foundation.MsgFundTreasuryResponse{}, nil @@ -68,7 +68,7 @@ func (s msgServer) WithdrawFromTreasury(c context.Context, req *foundation.MsgWi To: req.To, Amount: req.Amount, }); err != nil { - return nil, err + panic(err) } return &foundation.MsgWithdrawFromTreasuryResponse{}, nil @@ -88,7 +88,7 @@ func (s msgServer) UpdateMembers(c context.Context, req *foundation.MsgUpdateMem if err := ctx.EventManager().EmitTypedEvent(&foundation.EventUpdateMembers{ MemberUpdates: req.MemberUpdates, }); err != nil { - return nil, err + panic(err) } return &foundation.MsgUpdateMembersResponse{}, nil @@ -111,7 +111,7 @@ func (s msgServer) UpdateDecisionPolicy(c context.Context, req *foundation.MsgUp return nil, err } if err := ctx.EventManager().EmitTypedEvent(event); err != nil { - return nil, err + panic(err) } return &foundation.MsgUpdateDecisionPolicyResponse{}, nil @@ -129,11 +129,14 @@ func (s msgServer) SubmitProposal(c context.Context, req *foundation.MsgSubmitPr return nil, err } - proposal, _ := s.keeper.GetProposal(ctx, id) + proposal, err := s.keeper.GetProposal(ctx, id) + if err != nil { + panic(err) + } if err := ctx.EventManager().EmitTypedEvent(&foundation.EventSubmitProposal{ Proposal: *proposal, }); err != nil { - return nil, err + panic(err) } // Try to execute proposal immediately @@ -187,7 +190,7 @@ func (s msgServer) WithdrawProposal(c context.Context, req *foundation.MsgWithdr if err := ctx.EventManager().EmitTypedEvent(&foundation.EventWithdrawProposal{ ProposalId: id, }); err != nil { - return nil, err + panic(err) } return &foundation.MsgWithdrawProposalResponse{}, nil @@ -251,7 +254,7 @@ func (s msgServer) LeaveFoundation(c context.Context, req *foundation.MsgLeaveFo if err := ctx.EventManager().EmitTypedEvent(&foundation.EventLeaveFoundation{ Address: req.Address, }); err != nil { - return nil, err + panic(err) } return &foundation.MsgLeaveFoundationResponse{}, nil diff --git a/x/foundation/keeper/msg_server_test.go b/x/foundation/keeper/msg_server_test.go index 7b56fc8417..701c20109c 100644 --- a/x/foundation/keeper/msg_server_test.go +++ b/x/foundation/keeper/msg_server_test.go @@ -9,11 +9,11 @@ import ( func (s *KeeperTestSuite) TestMsgFundTreasury() { testCases := map[string]struct { amount sdk.Int - valid bool + valid bool }{ "valid request": { amount: s.balance, - valid: true, + valid: true, }, "insufficient funds": { amount: s.balance.Add(sdk.OneInt()), @@ -25,8 +25,8 @@ func (s *KeeperTestSuite) TestMsgFundTreasury() { ctx, _ := s.ctx.CacheContext() req := &foundation.MsgFundTreasury{ - From: s.stranger.String(), - Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, tc.amount)), + From: s.stranger.String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, tc.amount)), } res, err := s.msgServer.FundTreasury(sdk.WrapSDKContext(ctx), req) if !tc.valid { @@ -42,30 +42,30 @@ func (s *KeeperTestSuite) TestMsgFundTreasury() { func (s *KeeperTestSuite) TestMsgWithdrawFromTreasury() { testCases := map[string]struct { operator sdk.AccAddress - to sdk.AccAddress - amount sdk.Int - valid bool + to sdk.AccAddress + amount sdk.Int + valid bool }{ "valid request": { operator: s.operator, - to: s.stranger, - amount: s.balance, - valid: true, + to: s.stranger, + amount: s.balance, + valid: true, }, "operator not authorized": { operator: s.stranger, - to: s.stranger, - amount: s.balance, + to: s.stranger, + amount: s.balance, }, "receiver not authorized": { operator: s.operator, - to: s.members[0], - amount: s.balance, + to: s.members[0], + amount: s.balance, }, "insufficient funds": { operator: s.operator, - to: s.stranger, - amount: s.balance.Add(sdk.OneInt()), + to: s.stranger, + amount: s.balance.Add(sdk.OneInt()), }, } @@ -75,8 +75,8 @@ func (s *KeeperTestSuite) TestMsgWithdrawFromTreasury() { req := &foundation.MsgWithdrawFromTreasury{ Operator: tc.operator.String(), - To: tc.to.String(), - Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, tc.amount)), + To: tc.to.String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, tc.amount)), } res, err := s.msgServer.WithdrawFromTreasury(sdk.WrapSDKContext(ctx), req) if !tc.valid { @@ -92,14 +92,14 @@ func (s *KeeperTestSuite) TestMsgWithdrawFromTreasury() { func (s *KeeperTestSuite) TestMsgUpdateDecisionPolicy() { testCases := map[string]struct { operator sdk.AccAddress - policy foundation.DecisionPolicy - valid bool + policy foundation.DecisionPolicy + valid bool }{ "valid request": { operator: s.operator, policy: &foundation.ThresholdDecisionPolicy{ Threshold: foundation.DefaultConfig().MinThreshold, - Windows: &foundation.DecisionPolicyWindows{}, + Windows: &foundation.DecisionPolicyWindows{}, }, valid: true, }, @@ -107,14 +107,14 @@ func (s *KeeperTestSuite) TestMsgUpdateDecisionPolicy() { operator: s.stranger, policy: &foundation.ThresholdDecisionPolicy{ Threshold: foundation.DefaultConfig().MinThreshold, - Windows: &foundation.DecisionPolicyWindows{}, + Windows: &foundation.DecisionPolicyWindows{}, }, }, "low threshold": { operator: s.operator, policy: &foundation.ThresholdDecisionPolicy{ Threshold: sdk.OneDec(), - Windows: &foundation.DecisionPolicyWindows{}, + Windows: &foundation.DecisionPolicyWindows{}, }, }, } @@ -143,8 +143,8 @@ func (s *KeeperTestSuite) TestMsgUpdateDecisionPolicy() { func (s *KeeperTestSuite) TestMsgUpdateMembers() { testCases := map[string]struct { operator sdk.AccAddress - member foundation.Member - valid bool + member foundation.Member + valid bool }{ "valid request": { operator: s.operator, @@ -172,7 +172,7 @@ func (s *KeeperTestSuite) TestMsgUpdateMembers() { ctx, _ := s.ctx.CacheContext() req := &foundation.MsgUpdateMembers{ - Operator: tc.operator.String(), + Operator: tc.operator.String(), MemberUpdates: []foundation.Member{tc.member}, } res, err := s.msgServer.UpdateMembers(sdk.WrapSDKContext(ctx), req) @@ -194,17 +194,17 @@ func (s *KeeperTestSuite) TestMsgSubmitProposal() { testCases := map[string]struct { proposers []string - metadata string - msg sdk.Msg - exec foundation.Exec - valid bool + metadata string + msg sdk.Msg + exec foundation.Exec + valid bool }{ "valid request (submit)": { proposers: members, msg: &foundation.MsgWithdrawFromTreasury{ Operator: s.operator.String(), - To: s.stranger.String(), - Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance)), + To: s.stranger.String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance)), }, valid: true, }, @@ -212,36 +212,36 @@ func (s *KeeperTestSuite) TestMsgSubmitProposal() { proposers: members, msg: &foundation.MsgWithdrawFromTreasury{ Operator: s.operator.String(), - To: s.stranger.String(), - Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance)), + To: s.stranger.String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance)), }, - exec: foundation.Exec_EXEC_TRY, + exec: foundation.Exec_EXEC_TRY, valid: true, }, "valid request (submit & unable to reach quorum)": { proposers: []string{members[0]}, msg: &foundation.MsgWithdrawFromTreasury{ Operator: s.operator.String(), - To: s.stranger.String(), - Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance)), + To: s.stranger.String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance)), }, - exec: foundation.Exec_EXEC_TRY, + exec: foundation.Exec_EXEC_TRY, valid: true, }, "not a member": { proposers: []string{s.stranger.String()}, msg: &foundation.MsgWithdrawFromTreasury{ Operator: s.operator.String(), - To: s.stranger.String(), - Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance)), + To: s.stranger.String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance)), }, }, "unauthorized msg": { proposers: []string{members[0]}, msg: &foundation.MsgWithdrawFromTreasury{ Operator: s.stranger.String(), - To: s.stranger.String(), - Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance)), + To: s.stranger.String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, s.balance)), }, }, } @@ -252,8 +252,8 @@ func (s *KeeperTestSuite) TestMsgSubmitProposal() { req := &foundation.MsgSubmitProposal{ Proposers: tc.proposers, - Metadata: tc.metadata, - Exec: tc.exec, + Metadata: tc.metadata, + Exec: tc.exec, } err := req.SetMsgs([]sdk.Msg{tc.msg}) s.Require().NoError(err) @@ -272,25 +272,25 @@ func (s *KeeperTestSuite) TestMsgSubmitProposal() { func (s *KeeperTestSuite) TestMsgWithdrawProposal() { testCases := map[string]struct { proposalID uint64 - address sdk.AccAddress - valid bool + address sdk.AccAddress + valid bool }{ "valid request (proposer)": { proposalID: s.activeProposal, - address: s.members[0], - valid: true, + address: s.members[0], + valid: true, }, "valid request (operator)": { proposalID: s.activeProposal, - address: s.operator, - valid: true, + address: s.operator, + valid: true, }, "not authorized": { address: s.stranger, }, "inactive proposal": { proposalID: s.abortedProposal, - address: s.members[0], + address: s.members[0], }, } @@ -300,7 +300,7 @@ func (s *KeeperTestSuite) TestMsgWithdrawProposal() { req := &foundation.MsgWithdrawProposal{ ProposalId: tc.proposalID, - Address: tc.address.String(), + Address: tc.address.String(), } res, err := s.msgServer.WithdrawProposal(sdk.WrapSDKContext(ctx), req) if !tc.valid { @@ -316,29 +316,29 @@ func (s *KeeperTestSuite) TestMsgWithdrawProposal() { func (s *KeeperTestSuite) TestMsgVote() { testCases := map[string]struct { proposalID uint64 - voter sdk.AccAddress - msg sdk.Msg - exec foundation.Exec - valid bool + voter sdk.AccAddress + msg sdk.Msg + exec foundation.Exec + valid bool }{ "valid request (vote)": { proposalID: s.activeProposal, - voter: s.members[0], - valid: true, + voter: s.members[0], + valid: true, }, "valid request (vote & execute)": { proposalID: s.activeProposal, - voter: s.members[0], - exec: foundation.Exec_EXEC_TRY, - valid: true, + voter: s.members[0], + exec: foundation.Exec_EXEC_TRY, + valid: true, }, "not authorized": { proposalID: s.activeProposal, - voter: s.stranger, + voter: s.stranger, }, "already voted": { proposalID: s.votedProposal, - voter: s.members[0], + voter: s.members[0], }, } @@ -348,9 +348,9 @@ func (s *KeeperTestSuite) TestMsgVote() { req := &foundation.MsgVote{ ProposalId: tc.proposalID, - Voter: tc.voter.String(), - Option: foundation.VOTE_OPTION_YES, - Exec: tc.exec, + Voter: tc.voter.String(), + Option: foundation.VOTE_OPTION_YES, + Exec: tc.exec, } res, err := s.msgServer.Vote(sdk.WrapSDKContext(ctx), req) if !tc.valid { @@ -366,22 +366,22 @@ func (s *KeeperTestSuite) TestMsgVote() { func (s *KeeperTestSuite) TestMsgExec() { testCases := map[string]struct { proposalID uint64 - signer sdk.AccAddress - valid bool + signer sdk.AccAddress + valid bool }{ "valid request (execute)": { proposalID: s.votedProposal, - signer: s.members[0], - valid: true, + signer: s.members[0], + valid: true, }, "valid request (not finalized)": { proposalID: s.activeProposal, - signer: s.members[0], - valid: true, + signer: s.members[0], + valid: true, }, "not authorized": { proposalID: s.votedProposal, - signer: s.stranger, + signer: s.stranger, }, } @@ -391,7 +391,7 @@ func (s *KeeperTestSuite) TestMsgExec() { req := &foundation.MsgExec{ ProposalId: tc.proposalID, - Signer: tc.signer.String(), + Signer: tc.signer.String(), } res, err := s.msgServer.Exec(sdk.WrapSDKContext(ctx), req) if !tc.valid { @@ -407,11 +407,11 @@ func (s *KeeperTestSuite) TestMsgExec() { func (s *KeeperTestSuite) TestMsgLeaveFoundation() { testCases := map[string]struct { address sdk.AccAddress - valid bool + valid bool }{ "valid request": { address: s.members[0], - valid: true, + valid: true, }, "not authorized": { address: s.stranger, @@ -438,21 +438,21 @@ func (s *KeeperTestSuite) TestMsgLeaveFoundation() { func (s *KeeperTestSuite) TestMsgGrant() { testCases := map[string]struct { - operator sdk.AccAddress + operator sdk.AccAddress authorization foundation.Authorization - valid bool + valid bool }{ "valid request": { - operator: s.operator, + operator: s.operator, authorization: &foundation.ReceiveFromTreasuryAuthorization{}, - valid: true, + valid: true, }, "not authorized": { - operator: s.stranger, + operator: s.stranger, authorization: &foundation.ReceiveFromTreasuryAuthorization{}, }, "wrong granter": { - operator: s.operator, + operator: s.operator, authorization: &stakingplus.CreateValidatorAuthorization{}, }, } @@ -463,7 +463,7 @@ func (s *KeeperTestSuite) TestMsgGrant() { req := &foundation.MsgGrant{ Operator: tc.operator.String(), - Grantee: s.operator.String(), + Grantee: s.operator.String(), } err := req.SetAuthorization(tc.authorization) s.Require().NoError(err) @@ -481,30 +481,30 @@ func (s *KeeperTestSuite) TestMsgGrant() { func (s *KeeperTestSuite) TestMsgRevoke() { testCases := map[string]struct { - operator sdk.AccAddress - grantee sdk.AccAddress + operator sdk.AccAddress + grantee sdk.AccAddress msgTypeURL string - valid bool + valid bool }{ "valid request": { - operator: s.operator, - grantee: s.stranger, + operator: s.operator, + grantee: s.stranger, msgTypeURL: foundation.ReceiveFromTreasuryAuthorization{}.MsgTypeURL(), - valid: true, + valid: true, }, "no grant": { - operator: s.operator, - grantee: s.members[0], + operator: s.operator, + grantee: s.members[0], msgTypeURL: foundation.ReceiveFromTreasuryAuthorization{}.MsgTypeURL(), }, "not authorized": { - operator: s.stranger, - grantee: s.stranger, + operator: s.stranger, + grantee: s.stranger, msgTypeURL: foundation.ReceiveFromTreasuryAuthorization{}.MsgTypeURL(), }, "wrong granter": { - operator: s.operator, - grantee: s.stranger, + operator: s.operator, + grantee: s.stranger, msgTypeURL: stakingplus.CreateValidatorAuthorization{}.MsgTypeURL(), }, } @@ -514,8 +514,8 @@ func (s *KeeperTestSuite) TestMsgRevoke() { ctx, _ := s.ctx.CacheContext() req := &foundation.MsgRevoke{ - Operator: tc.operator.String(), - Grantee: tc.grantee.String(), + Operator: tc.operator.String(), + Grantee: tc.grantee.String(), MsgTypeUrl: tc.msgTypeURL, } res, err := s.msgServer.Revoke(sdk.WrapSDKContext(ctx), req) diff --git a/x/foundation/keeper/proposal.go b/x/foundation/keeper/proposal.go index c98a5fafbd..931e86897a 100644 --- a/x/foundation/keeper/proposal.go +++ b/x/foundation/keeper/proposal.go @@ -20,9 +20,13 @@ func (k Keeper) handleUpdateFoundationParamsProposal(ctx sdk.Context, p *foundat k.Cleanup(ctx) } - return ctx.EventManager().EmitTypedEvent(&foundation.EventUpdateFoundationParams{ + if err := ctx.EventManager().EmitTypedEvent(&foundation.EventUpdateFoundationParams{ Params: params, - }) + }); err != nil { + panic(err) + } + + return nil } // handleUpdateValidatorAuthsProposal is a handler for update validator auths proposal @@ -95,9 +99,7 @@ func (k Keeper) SubmitProposal(ctx sdk.Context, proposers []string, metadata str return 0, err } - if err := k.setProposal(ctx, proposal); err != nil { - return 0, err - } + k.setProposal(ctx, proposal) k.addProposalToVPEndQueue(ctx, proposal) return id, nil @@ -111,7 +113,7 @@ func (k Keeper) WithdrawProposal(ctx sdk.Context, proposalID uint64) error { // Ensure the proposal can be withdrawn. if proposal.Status != foundation.PROPOSAL_STATUS_SUBMITTED { - return sdkerrors.ErrInvalidRequest.Wrapf("cannot withdraw a proposal with the status of %s", proposal.Status.String()) + return sdkerrors.ErrInvalidRequest.Wrapf("cannot withdraw a proposal with the status of %s", proposal.Status) } proposal.Status = foundation.PROPOSAL_STATUS_WITHDRAWN @@ -209,9 +211,7 @@ func (k Keeper) UpdateTallyOfVPEndProposals(ctx sdk.Context) { panic(err) } - if err := k.setProposal(ctx, proposal); err != nil { - panic(err) - } + k.setProposal(ctx, proposal) return false }) @@ -222,27 +222,21 @@ func (k Keeper) GetProposal(ctx sdk.Context, id uint64) (*foundation.Proposal, e key := proposalKey(id) bz := store.Get(key) if len(bz) == 0 { - return nil, sdkerrors.Wrapf(sdkerrors.ErrNotFound, "No proposal for id: %d", id) + return nil, sdkerrors.ErrNotFound.Wrapf("No proposal for id: %d", id) } var proposal foundation.Proposal - if err := k.cdc.Unmarshal(bz, &proposal); err != nil { - return nil, err - } + k.cdc.MustUnmarshal(bz, &proposal) + return &proposal, nil } -func (k Keeper) setProposal(ctx sdk.Context, proposal foundation.Proposal) error { - bz, err := k.cdc.Marshal(&proposal) - if err != nil { - return err - } - +func (k Keeper) setProposal(ctx sdk.Context, proposal foundation.Proposal) { store := ctx.KVStore(k.storeKey) key := proposalKey(proposal.Id) - store.Set(key, bz) - return nil + bz := k.cdc.MustMarshal(&proposal) + store.Set(key, bz) } func (k Keeper) deleteProposal(ctx sdk.Context, proposalID uint64) { diff --git a/x/foundation/keeper/proposal_test.go b/x/foundation/keeper/proposal_test.go index 61017df1c1..c322f0ee62 100644 --- a/x/foundation/keeper/proposal_test.go +++ b/x/foundation/keeper/proposal_test.go @@ -10,8 +10,8 @@ import ( sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/x/foundation" "github.com/line/lbm-sdk/x/foundation/keeper" - "github.com/line/lbm-sdk/x/stakingplus" govtypes "github.com/line/lbm-sdk/x/gov/types" + "github.com/line/lbm-sdk/x/stakingplus" ) func newParams(enabled bool) *foundation.Params { @@ -89,36 +89,36 @@ func TestProposalHandler(t *testing.T) { } func (s *KeeperTestSuite) TestSubmitProposal() { - testCases := map[string]struct{ + testCases := map[string]struct { proposers []string - metadata string - msg sdk.Msg - valid bool + metadata string + msg sdk.Msg + valid bool }{ "valid proposal": { proposers: []string{s.members[0].String()}, msg: &foundation.MsgWithdrawFromTreasury{ Operator: s.operator.String(), - To: s.stranger.String(), - Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())), + To: s.stranger.String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())), }, valid: true, }, "long metadata": { proposers: []string{s.members[0].String()}, - metadata: string(make([]rune, 256)), + metadata: string(make([]rune, 256)), msg: &foundation.MsgWithdrawFromTreasury{ Operator: s.operator.String(), - To: s.stranger.String(), - Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())), + To: s.stranger.String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())), }, }, "unauthorized msg": { proposers: []string{s.members[0].String()}, msg: &foundation.MsgWithdrawFromTreasury{ Operator: s.stranger.String(), - To: s.stranger.String(), - Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())), + To: s.stranger.String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())), }, }, } @@ -138,12 +138,12 @@ func (s *KeeperTestSuite) TestSubmitProposal() { } func (s *KeeperTestSuite) TestWithdrawProposal() { - testCases := map[string]struct{ - id uint64 + testCases := map[string]struct { + id uint64 valid bool }{ "valid proposal": { - id: s.activeProposal, + id: s.activeProposal, valid: true, }, "not active": { diff --git a/x/foundation/keeper/treasury_test.go b/x/foundation/keeper/treasury_test.go index 75cf46c8e3..cd29a6cd9d 100644 --- a/x/foundation/keeper/treasury_test.go +++ b/x/foundation/keeper/treasury_test.go @@ -7,11 +7,11 @@ import ( func (s *KeeperTestSuite) TestFundTreasury() { testCases := map[string]struct { amount sdk.Int - valid bool + valid bool }{ "valid amount": { amount: s.balance, - valid: true, + valid: true, }, "insufficient coins": { amount: s.balance.Add(sdk.OneInt()), @@ -36,11 +36,11 @@ func (s *KeeperTestSuite) TestFundTreasury() { func (s *KeeperTestSuite) TestWithDrawFromTreasury() { testCases := map[string]struct { amount sdk.Int - valid bool + valid bool }{ "valid amount": { amount: s.balance, - valid: true, + valid: true, }, "insufficient coins": { amount: s.balance.Add(sdk.OneInt()), diff --git a/x/foundation/keeper/vote.go b/x/foundation/keeper/vote.go index 99025eb549..bade705088 100644 --- a/x/foundation/keeper/vote.go +++ b/x/foundation/keeper/vote.go @@ -13,7 +13,7 @@ func (k Keeper) Vote(ctx sdk.Context, vote foundation.Vote) error { // Make sure that a voter hasn't already voted. if k.hasVote(ctx, vote.ProposalId, sdk.AccAddress(vote.Voter)) { - return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "Already voted: %s", vote.Voter) + return sdkerrors.ErrInvalidRequest.Wrapf("Already voted: %s", vote.Voter) } proposal, err := k.GetProposal(ctx, vote.ProposalId) @@ -23,21 +23,22 @@ func (k Keeper) Vote(ctx sdk.Context, vote foundation.Vote) error { // Ensure that we can still accept votes for this proposal. if proposal.Status != foundation.PROPOSAL_STATUS_SUBMITTED { - return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "not possible with proposal status: %s", proposal.Status.String()) + return sdkerrors.ErrInvalidRequest.Wrapf("not possible with proposal status: %s", proposal.Status) } if !ctx.BlockTime().Before(proposal.VotingPeriodEnd) { - return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "voting period has ended already") + return sdkerrors.ErrInvalidRequest.Wrap("voting period has ended already") } vote.SubmitTime = ctx.BlockTime() + k.setVote(ctx, vote) - if err := k.setVote(ctx, vote); err != nil { - return err + if err := ctx.EventManager().EmitTypedEvent(&foundation.EventVote{ + Vote: vote, + }); err != nil { + panic(err) } - return ctx.EventManager().EmitTypedEvent(&foundation.EventVote{ - Vote: vote, - }) + return nil } func (k Keeper) hasVote(ctx sdk.Context, proposalID uint64, voter sdk.AccAddress) bool { @@ -51,27 +52,21 @@ func (k Keeper) GetVote(ctx sdk.Context, proposalID uint64, voter sdk.AccAddress key := voteKey(proposalID, voter) bz := store.Get(key) if len(bz) == 0 { - return nil, sdkerrors.Wrapf(sdkerrors.ErrNotFound, "No vote for proposal %d: %s", proposalID, voter) + return nil, sdkerrors.ErrNotFound.Wrapf("No vote for proposal %d: %s", proposalID, voter) } var vote foundation.Vote - if err := k.cdc.Unmarshal(bz, &vote); err != nil { - return nil, err - } + k.cdc.MustUnmarshal(bz, &vote) + return &vote, nil } -func (k Keeper) setVote(ctx sdk.Context, vote foundation.Vote) error { - bz, err := k.cdc.Marshal(&vote) - if err != nil { - return err - } - +func (k Keeper) setVote(ctx sdk.Context, vote foundation.Vote) { store := ctx.KVStore(k.storeKey) key := voteKey(vote.ProposalId, sdk.AccAddress(vote.Voter)) - store.Set(key, bz) - return nil + bz := k.cdc.MustMarshal(&vote) + store.Set(key, bz) } func (k Keeper) iterateVotes(ctx sdk.Context, proposalID uint64, fn func(vote foundation.Vote) (stop bool)) { diff --git a/x/foundation/keeper/vote_test.go b/x/foundation/keeper/vote_test.go index 00f3600701..dd0cfdd7ef 100644 --- a/x/foundation/keeper/vote_test.go +++ b/x/foundation/keeper/vote_test.go @@ -8,47 +8,47 @@ import ( ) func (s *KeeperTestSuite) TestVote() { - testCases := map[string]struct{ + testCases := map[string]struct { proposalID uint64 - voter sdk.AccAddress - option foundation.VoteOption - metadata string - after time.Duration - valid bool + voter sdk.AccAddress + option foundation.VoteOption + metadata string + after time.Duration + valid bool }{ "vote yes": { proposalID: s.activeProposal, - voter: s.members[0], - option: foundation.VOTE_OPTION_YES, - valid: true, + voter: s.members[0], + option: foundation.VOTE_OPTION_YES, + valid: true, }, "vote no": { proposalID: s.activeProposal, - voter: s.members[0], - option: foundation.VOTE_OPTION_NO, - valid: true, + voter: s.members[0], + option: foundation.VOTE_OPTION_NO, + valid: true, }, "already voted": { proposalID: s.votedProposal, - voter: s.members[0], - option: foundation.VOTE_OPTION_YES, + voter: s.members[0], + option: foundation.VOTE_OPTION_YES, }, "inactive proposal": { proposalID: s.abortedProposal, - voter: s.members[0], - option: foundation.VOTE_OPTION_YES, + voter: s.members[0], + option: foundation.VOTE_OPTION_YES, }, "long metadata": { proposalID: s.activeProposal, - voter: s.members[0], - option: foundation.VOTE_OPTION_YES, - metadata: string(make([]rune, 256)), + voter: s.members[0], + option: foundation.VOTE_OPTION_YES, + metadata: string(make([]rune, 256)), }, "voting too late": { proposalID: s.activeProposal, - voter: s.members[0], - option: foundation.VOTE_OPTION_YES, - after: s.keeper.GetFoundationInfo(s.ctx).GetDecisionPolicy().GetVotingPeriod(), + voter: s.members[0], + option: foundation.VOTE_OPTION_YES, + after: s.keeper.GetFoundationInfo(s.ctx).GetDecisionPolicy().GetVotingPeriod(), }, } @@ -59,9 +59,9 @@ func (s *KeeperTestSuite) TestVote() { vote := foundation.Vote{ ProposalId: tc.proposalID, - Voter: tc.voter.String(), - Option: tc.option, - Metadata: tc.metadata, + Voter: tc.voter.String(), + Option: tc.option, + Metadata: tc.metadata, } err := s.keeper.Vote(ctx, vote) if tc.valid { diff --git a/x/foundation/msgs_test.go b/x/foundation/msgs_test.go index 5485606a22..7b9e1c21f7 100644 --- a/x/foundation/msgs_test.go +++ b/x/foundation/msgs_test.go @@ -18,28 +18,28 @@ func TestMsgFundTreasury(t *testing.T) { } testCases := map[string]struct { - from sdk.AccAddress - amount sdk.Int - valid bool + from sdk.AccAddress + amount sdk.Int + valid bool }{ "valid msg": { - from: addrs[0], - amount: sdk.OneInt(), - valid: true, + from: addrs[0], + amount: sdk.OneInt(), + valid: true, }, "empty from": { - amount: sdk.OneInt(), + amount: sdk.OneInt(), }, "zero amount": { - from: addrs[0], - amount: sdk.ZeroInt(), - }, + from: addrs[0], + amount: sdk.ZeroInt(), + }, } for name, tc := range testCases { msg := foundation.MsgFundTreasury{ - From: tc.from.String(), - Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, tc.amount)), + From: tc.from.String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, tc.amount)), } require.Equal(t, []sdk.AccAddress{tc.from}, msg.GetSigners(), name) @@ -61,36 +61,36 @@ func TestMsgWithdrawFromTreasury(t *testing.T) { testCases := map[string]struct { operator sdk.AccAddress - to sdk.AccAddress - amount sdk.Int - valid bool + to sdk.AccAddress + amount sdk.Int + valid bool }{ "valid msg": { operator: addrs[0], - to: addrs[1], - amount: sdk.OneInt(), - valid: true, + to: addrs[1], + amount: sdk.OneInt(), + valid: true, }, "empty operator": { - to: addrs[1], - amount: sdk.OneInt(), + to: addrs[1], + amount: sdk.OneInt(), }, "empty to": { operator: addrs[0], - amount: sdk.OneInt(), + amount: sdk.OneInt(), }, "zero amount": { operator: addrs[0], - to: addrs[1], - amount: sdk.ZeroInt(), + to: addrs[1], + amount: sdk.ZeroInt(), }, } for name, tc := range testCases { msg := foundation.MsgWithdrawFromTreasury{ Operator: tc.operator.String(), - To: tc.to.String(), - Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, tc.amount)), + To: tc.to.String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, tc.amount)), } require.Equal(t, []sdk.AccAddress{tc.operator}, msg.GetSigners(), name) @@ -112,26 +112,26 @@ func TestMsgUpdateMembers(t *testing.T) { testCases := map[string]struct { operator sdk.AccAddress - members []foundation.Member - valid bool + members []foundation.Member + valid bool }{ "valid msg": { operator: addrs[0], members: []foundation.Member{{ - Address: addrs[1].String(), + Address: addrs[1].String(), Participating: true, }}, - valid: true, + valid: true, }, "empty operator": { members: []foundation.Member{{ - Address: addrs[1].String(), + Address: addrs[1].String(), Participating: true, }}, }, "empty members": { operator: addrs[0], - members: []foundation.Member{}, + members: []foundation.Member{}, }, "empty member address": { operator: addrs[0], @@ -143,7 +143,7 @@ func TestMsgUpdateMembers(t *testing.T) { operator: addrs[0], members: []foundation.Member{ { - Address: addrs[1].String(), + Address: addrs[1].String(), Participating: true, }, { @@ -155,7 +155,7 @@ func TestMsgUpdateMembers(t *testing.T) { for name, tc := range testCases { msg := foundation.MsgUpdateMembers{ - Operator: tc.operator.String(), + Operator: tc.operator.String(), MemberUpdates: tc.members, } @@ -178,8 +178,8 @@ func TestMsgUpdateDecisionPolicy(t *testing.T) { testCases := map[string]struct { operator sdk.AccAddress - policy foundation.DecisionPolicy - valid bool + policy foundation.DecisionPolicy + valid bool }{ "valid threshold policy": { operator: addrs[0], @@ -189,7 +189,7 @@ func TestMsgUpdateDecisionPolicy(t *testing.T) { VotingPeriod: time.Hour, }, }, - valid: true, + valid: true, }, "valid percentage policy": { operator: addrs[0], @@ -199,7 +199,7 @@ func TestMsgUpdateDecisionPolicy(t *testing.T) { VotingPeriod: time.Hour, }, }, - valid: true, + valid: true, }, "empty operator": { policy: &foundation.ThresholdDecisionPolicy{ @@ -225,8 +225,7 @@ func TestMsgUpdateDecisionPolicy(t *testing.T) { operator: addrs[0], policy: &foundation.ThresholdDecisionPolicy{ Threshold: sdk.NewDec(3), - Windows: &foundation.DecisionPolicyWindows{ - }, + Windows: &foundation.DecisionPolicyWindows{}, }, }, "invalid percentage": { @@ -268,41 +267,41 @@ func TestMsgSubmitProposal(t *testing.T) { testCases := map[string]struct { proposers []sdk.AccAddress - msgs []sdk.Msg - exec foundation.Exec - valid bool + msgs []sdk.Msg + exec foundation.Exec + valid bool }{ "valid msg": { proposers: []sdk.AccAddress{addrs[0]}, msgs: []sdk.Msg{&foundation.MsgWithdrawFromTreasury{ Operator: addrs[1].String(), - To: addrs[2].String(), - Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())), + To: addrs[2].String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())), }}, - valid: true, + valid: true, }, "empty proposers": { proposers: []sdk.AccAddress{}, msgs: []sdk.Msg{&foundation.MsgWithdrawFromTreasury{ Operator: addrs[1].String(), - To: addrs[2].String(), - Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())), + To: addrs[2].String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())), }}, }, "invalid proposer": { proposers: []sdk.AccAddress{""}, msgs: []sdk.Msg{&foundation.MsgWithdrawFromTreasury{ Operator: addrs[1].String(), - To: addrs[2].String(), - Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())), + To: addrs[2].String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())), }}, }, "duplicate proposers": { proposers: []sdk.AccAddress{addrs[0], addrs[0]}, msgs: []sdk.Msg{&foundation.MsgWithdrawFromTreasury{ Operator: addrs[1].String(), - To: addrs[2].String(), - Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())), + To: addrs[2].String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())), }}, }, "empty msgs": { @@ -310,14 +309,14 @@ func TestMsgSubmitProposal(t *testing.T) { }, "invalid msg": { proposers: []sdk.AccAddress{addrs[0]}, - msgs: []sdk.Msg{&foundation.MsgWithdrawFromTreasury{}}, + msgs: []sdk.Msg{&foundation.MsgWithdrawFromTreasury{}}, }, "invalid exec": { proposers: []sdk.AccAddress{addrs[0]}, msgs: []sdk.Msg{&foundation.MsgWithdrawFromTreasury{ Operator: addrs[1].String(), - To: addrs[2].String(), - Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())), + To: addrs[2].String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())), }}, exec: -1, }, @@ -331,7 +330,7 @@ func TestMsgSubmitProposal(t *testing.T) { msg := foundation.MsgSubmitProposal{ Proposers: proposers, - Exec: tc.exec, + Exec: tc.exec, } err := msg.SetMsgs(tc.msgs) require.NoError(t, err, name) @@ -354,12 +353,12 @@ func TestMsgWithdrawProposal(t *testing.T) { } testCases := map[string]struct { - id uint64 + id uint64 address sdk.AccAddress valid bool }{ "valid msg": { - id: 1, + id: 1, address: addrs[0], valid: true, }, @@ -374,7 +373,7 @@ func TestMsgWithdrawProposal(t *testing.T) { for name, tc := range testCases { msg := foundation.MsgWithdrawProposal{ ProposalId: tc.id, - Address: tc.address.String(), + Address: tc.address.String(), } require.Equal(t, []sdk.AccAddress{tc.address}, msg.GetSigners(), name) @@ -395,49 +394,49 @@ func TestMsgVote(t *testing.T) { } testCases := map[string]struct { - id uint64 - voter sdk.AccAddress + id uint64 + voter sdk.AccAddress option foundation.VoteOption - exec foundation.Exec - valid bool + exec foundation.Exec + valid bool }{ "valid msg": { - id: 1, - voter: addrs[0], + id: 1, + voter: addrs[0], option: foundation.VOTE_OPTION_YES, - valid: true, + valid: true, }, "empty proposal id": { - voter: addrs[0], + voter: addrs[0], option: foundation.VOTE_OPTION_YES, }, "empty voter": { - id: 1, + id: 1, option: foundation.VOTE_OPTION_YES, }, "empty option": { - id: 1, + id: 1, voter: addrs[0], }, "invalid option": { - id: 1, - voter: addrs[0], + id: 1, + voter: addrs[0], option: -1, }, "invalid exec": { - id: 1, - voter: addrs[0], + id: 1, + voter: addrs[0], option: foundation.VOTE_OPTION_YES, - exec: -1, + exec: -1, }, } for name, tc := range testCases { msg := foundation.MsgVote{ ProposalId: tc.id, - Voter: tc.voter.String(), - Option: tc.option, - Exec: tc.exec, + Voter: tc.voter.String(), + Option: tc.option, + Exec: tc.exec, } require.Equal(t, []sdk.AccAddress{tc.voter}, msg.GetSigners(), name) @@ -458,14 +457,14 @@ func TestMsgExec(t *testing.T) { } testCases := map[string]struct { - id uint64 + id uint64 signer sdk.AccAddress - valid bool + valid bool }{ "valid msg": { - id: 1, + id: 1, signer: addrs[0], - valid: true, + valid: true, }, "empty proposal id": { signer: addrs[0], @@ -478,7 +477,7 @@ func TestMsgExec(t *testing.T) { for name, tc := range testCases { msg := foundation.MsgExec{ ProposalId: tc.id, - Signer: tc.signer.String(), + Signer: tc.signer.String(), } require.Equal(t, []sdk.AccAddress{tc.signer}, msg.GetSigners(), name) @@ -506,8 +505,7 @@ func TestMsgLeaveFoundation(t *testing.T) { address: addrs[0], valid: true, }, - "empty address": { - }, + "empty address": {}, } for name, tc := range testCases { @@ -533,35 +531,35 @@ func TestMsgGrant(t *testing.T) { } testCases := map[string]struct { - operator sdk.AccAddress - grantee sdk.AccAddress + operator sdk.AccAddress + grantee sdk.AccAddress authorization foundation.Authorization - valid bool + valid bool }{ "valid msg": { - operator: addrs[0], - grantee: addrs[1], + operator: addrs[0], + grantee: addrs[1], authorization: &foundation.ReceiveFromTreasuryAuthorization{}, - valid: true, + valid: true, }, "empty operator": { - grantee: addrs[1], + grantee: addrs[1], authorization: &foundation.ReceiveFromTreasuryAuthorization{}, }, "empty grantee": { - operator: addrs[0], + operator: addrs[0], authorization: &foundation.ReceiveFromTreasuryAuthorization{}, }, "empty authorization": { operator: addrs[0], - grantee: addrs[1], + grantee: addrs[1], }, } for name, tc := range testCases { msg := foundation.MsgGrant{ Operator: tc.operator.String(), - Grantee: tc.grantee.String(), + Grantee: tc.grantee.String(), } if tc.authorization != nil { msg.SetAuthorization(tc.authorization) @@ -585,35 +583,35 @@ func TestMsgRevoke(t *testing.T) { } testCases := map[string]struct { - operator sdk.AccAddress - grantee sdk.AccAddress + operator sdk.AccAddress + grantee sdk.AccAddress msgTypeURL string - valid bool + valid bool }{ "valid msg": { - operator: addrs[0], - grantee: addrs[1], + operator: addrs[0], + grantee: addrs[1], msgTypeURL: foundation.ReceiveFromTreasuryAuthorization{}.MsgTypeURL(), - valid: true, + valid: true, }, "empty operator": { - grantee: addrs[1], + grantee: addrs[1], msgTypeURL: foundation.ReceiveFromTreasuryAuthorization{}.MsgTypeURL(), }, "empty grantee": { - operator: addrs[0], + operator: addrs[0], msgTypeURL: foundation.ReceiveFromTreasuryAuthorization{}.MsgTypeURL(), }, "empty url": { operator: addrs[0], - grantee: addrs[1], + grantee: addrs[1], }, } for name, tc := range testCases { msg := foundation.MsgRevoke{ - Operator: tc.operator.String(), - Grantee: tc.grantee.String(), + Operator: tc.operator.String(), + Grantee: tc.grantee.String(), MsgTypeUrl: tc.msgTypeURL, } diff --git a/x/foundation/proposal.go b/x/foundation/proposal.go index de6cff1529..48ef76f01c 100644 --- a/x/foundation/proposal.go +++ b/x/foundation/proposal.go @@ -36,7 +36,7 @@ func (p *UpdateFoundationParamsProposal) ProposalType() string { func (p *UpdateFoundationParamsProposal) ValidateBasic() error { params := p.Params if params.Enabled { - return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "cannot enable foundation") + return sdkerrors.ErrInvalidRequest.Wrap("cannot enable foundation") } if err := validateRatio(params.FoundationTax, "tax rate"); err != nil { @@ -68,7 +68,7 @@ func (p *UpdateValidatorAuthsProposal) ProposalType() string { return Proposal func (p *UpdateValidatorAuthsProposal) ValidateBasic() error { if len(p.Auths) == 0 { - return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "empty auths") + return sdkerrors.ErrInvalidRequest.Wrap("empty auths") } usedAddrs := map[string]bool{} @@ -78,7 +78,7 @@ func (p *UpdateValidatorAuthsProposal) ValidateBasic() error { return err } if usedAddrs[addr] { - return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "multiple auths for same validator: %s", addr) + return sdkerrors.ErrInvalidRequest.Wrapf("multiple auths for same validator: %s", addr) } usedAddrs[addr] = true } diff --git a/x/foundation/proposal_test.go b/x/foundation/proposal_test.go index 0892cbf24a..c5c75e0d1d 100644 --- a/x/foundation/proposal_test.go +++ b/x/foundation/proposal_test.go @@ -13,15 +13,15 @@ import ( func TestUpdateFoundationParamsProposal(t *testing.T) { testCases := map[string]struct { params *foundation.Params - valid bool + valid bool }{ "valid proposal": { params: foundation.DefaultParams(), - valid: true, + valid: true, }, "attempt to enable foundation": { params: &foundation.Params{ - Enabled: true, + Enabled: true, FoundationTax: sdk.ZeroDec(), }, }, @@ -64,7 +64,7 @@ func TestUpdateValidatorAuthsProposal(t *testing.T) { OperatorAddress: addrs[0].ToValAddress().String(), CreationAllowed: true, }}, - valid: true, + valid: true, }, "empty auths": { updates: []foundation.ValidatorAuth{},