Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat(x/ecocredit): credit type gov handler #1015

Merged
merged 31 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ddfe66c
chore: gen api
technicallyty Apr 11, 2022
6393445
wip: proposal handler
technicallyty Apr 12, 2022
2dd5ecc
wip: wires
technicallyty Apr 12, 2022
0c0ddbd
feat: final gov handler
technicallyty Apr 13, 2022
b04bb2e
chore: cleanup dead code
technicallyty Apr 13, 2022
195b51e
chore: cleanup code
technicallyty Apr 13, 2022
bd10a55
Merge branch 'master' into ty/1005-credit_type_gov_handler
technicallyty Apr 13, 2022
6d2f020
chore: fix app error
technicallyty Apr 13, 2022
c2e72a9
chore: add dummy REST proposal handler
technicallyty Apr 13, 2022
8690a08
chore: regen mocks
technicallyty Apr 13, 2022
f99cd38
chore: not invalid request
technicallyty Apr 13, 2022
0fcb39a
chore: clean test
technicallyty Apr 13, 2022
9cb5970
refactor: move proposal handler code into server/core
technicallyty Apr 13, 2022
e7ab09c
chore: add tests for new types
technicallyty Apr 13, 2022
e10b59b
refactor: use setCustomModuleBasics
technicallyty Apr 13, 2022
834d836
fix: restore setCustomKeepers
technicallyty Apr 13, 2022
8f40361
Update x/ecocredit/server/expected_keepers.go
technicallyty Apr 19, 2022
af4f04f
chore: assert credit type name length
technicallyty Apr 19, 2022
975f386
refactor: NewCreditType -> AddCreditType
technicallyty Apr 19, 2022
7cafa8d
Merge branch 'ty/1005-credit_type_gov_handler' of https://github.com/…
technicallyty Apr 19, 2022
567325b
Update app/stable_appconfig.go
technicallyty Apr 19, 2022
549d53e
chore: import alias
technicallyty Apr 19, 2022
d6bf5be
Merge branch 'ty/1005-credit_type_gov_handler' of https://github.com/…
technicallyty Apr 19, 2022
8dfbe96
Merge branch 'master' into ty/1005-credit_type_gov_handler
technicallyty Apr 19, 2022
eb1b2f5
fix: remove duplicate credit type validate method
technicallyty Apr 19, 2022
fa42e6d
chore: remove proposal cli method
technicallyty Apr 19, 2022
dddd634
Revert "chore: remove proposal cli method"
technicallyty Apr 20, 2022
582fec6
chore: revert cli removal
technicallyty Apr 20, 2022
3f57cab
Merge branch 'master' into ty/1005-credit_type_gov_handler
technicallyty Apr 21, 2022
eaaf563
chore: update error type
technicallyty Apr 21, 2022
00001e9
Merge branch 'master' into ty/1005-credit_type_gov_handler
technicallyty Apr 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
703 changes: 673 additions & 30 deletions api/regen/ecocredit/v1/types.pulsar.go

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"

"github.com/CosmWasm/wasmd/x/wasm"

"github.com/cosmos/ibc-go/v2/modules/apps/transfer"
ibctransferkeeper "github.com/cosmos/ibc-go/v2/modules/apps/transfer/keeper"
ibctransfertypes "github.com/cosmos/ibc-go/v2/modules/apps/transfer/types"
Expand Down Expand Up @@ -98,6 +97,7 @@ import (
"github.com/regen-network/regen-ledger/x/ecocredit"
"github.com/regen-network/regen-ledger/x/ecocredit/basket"
ecocreditmodule "github.com/regen-network/regen-ledger/x/ecocredit/module"
ecoServer "github.com/regen-network/regen-ledger/x/ecocredit/server/core"

// unnamed import of statik for swagger UI support
_ "github.com/regen-network/regen-ledger/v3/client/docs/statik"
Expand Down Expand Up @@ -376,13 +376,6 @@ func NewRegenApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest
)
app.AuthzKeeper = authzKeeper

app.setCustomKeepers(bApp, keys, appCodec, govRouter, homePath, appOpts, wasmOpts)

app.GovKeeper = govkeeper.NewKeeper(
appCodec, keys[govtypes.StoreKey], app.GetSubspace(govtypes.ModuleName), app.AccountKeeper, app.BankKeeper,
&stakingKeeper, govRouter,
)

// register custom modules here
app.smm = setCustomModules(app, interfaceRegistry)
ecocreditModule := ecocreditmodule.NewModule(
Expand All @@ -404,6 +397,14 @@ func NewRegenApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest
}
app.smm.RegisterInvariants(&app.CrisisKeeper)

govRouter.AddRoute(ecocredit.RouterKey, ecoServer.NewCreditTypeProposalHandler(ecocreditModule.Keeper))
app.GovKeeper = govkeeper.NewKeeper(
appCodec, keys[govtypes.StoreKey], app.GetSubspace(govtypes.ModuleName), app.AccountKeeper, app.BankKeeper,
&stakingKeeper, govRouter,
)

app.setCustomKeepers(bApp, keys, appCodec, govRouter, homePath, appOpts, wasmOpts)

var skipGenesisInvariants = cast.ToBool(appOpts.Get(crisis.FlagSkipGenesisInvariants))

app.mm = module.NewManager(
Expand Down
2 changes: 2 additions & 0 deletions app/experimental_appconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

moduletypes "github.com/regen-network/regen-ledger/types/module"
"github.com/regen-network/regen-ledger/types/module/server"
"github.com/regen-network/regen-ledger/x/ecocredit/client/core"
group "github.com/regen-network/regen-ledger/x/group/module"
)

Expand All @@ -40,6 +41,7 @@ func setCustomModuleBasics() []module.AppModuleBasic {
wasmclient.ProposalHandlers,
paramsclient.ProposalHandler, distrclient.ProposalHandler,
upgradeclient.ProposalHandler, upgradeclient.CancelProposalHandler,
core.CreditTypeProposalHandler,
)...,
),
wasm.AppModuleBasic{},
Expand Down
10 changes: 6 additions & 4 deletions app/stable_appconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ package app

import (
"github.com/CosmWasm/wasmd/x/wasm"
distrclient "github.com/cosmos/cosmos-sdk/x/distribution/client"
"github.com/cosmos/cosmos-sdk/x/gov"
paramsclient "github.com/cosmos/cosmos-sdk/x/params/client"
upgradeclient "github.com/cosmos/cosmos-sdk/x/upgrade/client"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
Expand All @@ -16,21 +20,19 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
distrclient "github.com/cosmos/cosmos-sdk/x/distribution/client"
"github.com/cosmos/cosmos-sdk/x/gov"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
paramsclient "github.com/cosmos/cosmos-sdk/x/params/client"
paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper"
upgradeclient "github.com/cosmos/cosmos-sdk/x/upgrade/client"

"github.com/regen-network/regen-ledger/types/module/server"
"github.com/regen-network/regen-ledger/x/ecocredit/client/core"
technicallyty marked this conversation as resolved.
Show resolved Hide resolved
)

func setCustomModuleBasics() []module.AppModuleBasic {
return []module.AppModuleBasic{
gov.NewAppModuleBasic(
paramsclient.ProposalHandler, distrclient.ProposalHandler,
upgradeclient.ProposalHandler, upgradeclient.CancelProposalHandler,
core.CreditTypeProposalHandler,
),
}
}
Expand Down
15 changes: 15 additions & 0 deletions proto/regen/ecocredit/v1/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,18 @@ message OriginTx {
// the id of a transaction based on a type (tx id, serial number)
string id = 2;
}

// CreditTypeProposal is a gov Content type for adding a credit type.
message CreditTypeProposal {
technicallyty marked this conversation as resolved.
Show resolved Hide resolved
option (gogoproto.goproto_stringer) = false;

// title is the title of the proposal.
string title = 1;

// description is the description of the proposal.
string description = 2;

// credit_type is the credit type to be added to the network if the proposal
// passes.
CreditType credit_type = 3;
}
78 changes: 78 additions & 0 deletions x/ecocredit/client/core/proposal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package core

import (
"fmt"
"strconv"

govclient "github.com/cosmos/cosmos-sdk/x/gov/client"
"github.com/cosmos/cosmos-sdk/x/gov/client/rest"
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov/client/cli"
"github.com/cosmos/cosmos-sdk/x/gov/types"

"github.com/regen-network/regen-ledger/x/ecocredit/core"
)

var CreditTypeProposalHandler = govclient.NewProposalHandler(TxCreditTypeProposalCmd, func(context client.Context) rest.ProposalRESTHandler {
return rest.ProposalRESTHandler{ // TODO: what to do here??
aaronc marked this conversation as resolved.
Show resolved Hide resolved
SubRoute: "",
Handler: nil,
}
})

func TxCreditTypeProposalCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "credit-type-proposal [proposal-title] [proposal-description] [credit_type_abbreviation] [credit_type_name] [units] [precision] [flags]",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a lot of positional arguments. I think we shouldn't even implement this and just let people submit a JSON proposal. I don't expect this to be called more than 5-10 times ever in production

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is that how most folks submit proposals? i could change it to a single argument (json file)

Copy link
Member

@ryanchristo ryanchristo Apr 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think aaron is referring to the gov command:

regen tx gov submit-proposal --proposal="path/to/proposal.json" --from mykey

Where proposal.json includes:

{
  "title": "Add Biochar Credit Type",
  "description": "Add Biochar credit type to the list of allowed credit types",
  "type": "CreditTypeProposal",
  "credit_type": {
    "abbreviation": "BC",
    "name": "biochar",
    "unit": "tonnes of organic carbon",
    "precision": 6,
  },
  "deposit": "10regen"
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if the above works and it should be tested before removing the command implemented here. If not, I think a json or yaml file might be nice.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok didn't know of that one. removed the cli command - fa42e6d

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didnt see your last comment 🥲, wasn't able to get this working. maybe we can take a look tomorrows standup? can just revert the removal if we cant get it going

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. Let's try tomorrow and then revert if we can't get it going.

Args: cobra.ExactArgs(6),
Short: "Submit a proposal for a new credit type",
Long: "Submit a proposal to add a new credit type. The credit type abbreviation and name MUST be unique, else " +
"the proposal will fail upon execution. Units are measurements units (i.e. metric tonne). Precision is how " +
"many decimal places are allowed in the credits.",
Example: `regen tx gov submit-proposal credit-type-proposal "Add Biodiversity Type" "A biodiversity type would be great..." BIO biodiversity "sq. meters" 3`,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
title, desc, abbrev, name, units, precisionStr := args[0], args[1], args[2], args[3], args[4], args[5]
precisionU64, err := strconv.ParseUint(precisionStr, 10, 32)
if err != nil {
return fmt.Errorf("invalid precision %s: %w", precisionStr, err)
}
proposal := core.CreditTypeProposal{
Title: title,
Description: desc,
CreditType: &core.CreditType{
Abbreviation: abbrev,
Name: name,
Unit: units,
Precision: uint32(precisionU64),
},
}
if err := proposal.ValidateBasic(); err != nil {
return fmt.Errorf("invalid proposal: %w", err)
}

depositStr, err := cmd.Flags().GetString(cli.FlagDeposit)
if err != nil {
return err
}
deposit, err := sdk.ParseCoinsNormalized(depositStr)
if err != nil {
return err
}
var content types.Content = &proposal
msg, err := types.NewMsgSubmitProposal(content, deposit, clientCtx.GetFromAddress())
if err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
cmd.Flags().String(cli.FlagDeposit, "", "deposit of proposal")
return cmd
}
2 changes: 2 additions & 0 deletions x/ecocredit/client/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/regen-network/regen-ledger/types"
"github.com/regen-network/regen-ledger/x/ecocredit"
basketcli "github.com/regen-network/regen-ledger/x/ecocredit/client/basket"
corecli "github.com/regen-network/regen-ledger/x/ecocredit/client/core"
marketplacecli "github.com/regen-network/regen-ledger/x/ecocredit/client/marketplace"
"github.com/regen-network/regen-ledger/x/ecocredit/core"
)
Expand All @@ -45,6 +46,7 @@ func TxCmd(name string) *cobra.Command {
TxUpdateClassIssuersCmd(),
TxUpdateClassAdminCmd(),
TxCreateProject(),
corecli.TxCreditTypeProposalCmd(),
basketcli.TxCreateBasket(),
basketcli.TxPutInBasket(),
basketcli.TxTakeFromBasket(),
Expand Down
1 change: 1 addition & 0 deletions x/ecocredit/core/msg_create_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/legacy/legacytx"

"github.com/regen-network/regen-ledger/x/ecocredit"
)

Expand Down
21 changes: 21 additions & 0 deletions x/ecocredit/core/msg_credit_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package core

import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

func (m CreditType) Validate() error {
if err := ValidateCreditTypeAbbreviation(m.Abbreviation); err != nil {
ryanchristo marked this conversation as resolved.
Show resolved Hide resolved
return err
}
if len(m.Name) == 0 {
return sdkerrors.ErrInvalidRequest.Wrap("name cannot be empty")
}
ryanchristo marked this conversation as resolved.
Show resolved Hide resolved
if len(m.Unit) == 0 {
return sdkerrors.ErrInvalidRequest.Wrap("unit cannot be empty")
}
Comment on lines +21 to +23
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think #429 would be nice to have alongside these updates but this could be added as a followup.

if m.Precision != PRECISION {
return sdkerrors.ErrInvalidRequest.Wrapf("credit type precision is currently locked to %d", PRECISION)
}
return nil
}
42 changes: 42 additions & 0 deletions x/ecocredit/core/msg_credit_type_proposal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package core

import (
"fmt"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"

"github.com/regen-network/regen-ledger/x/ecocredit"
)

var _ govtypes.Content = &CreditTypeProposal{}

const (
ProposalType = "CreditTypeProposal"
)

func init() {
govtypes.RegisterProposalType(ProposalType)
}

func (m *CreditTypeProposal) ProposalRoute() string { return ecocredit.RouterKey }

func (m *CreditTypeProposal) ProposalType() string { return ProposalType }

func (m *CreditTypeProposal) ValidateBasic() error {
if m.CreditType == nil {
return sdkerrors.ErrInvalidRequest.Wrap("credit type cannot be nil")
}
if err := m.CreditType.Validate(); err != nil {
return err
}
return govtypes.ValidateAbstract(m)
}
ryanchristo marked this conversation as resolved.
Show resolved Hide resolved

func (m *CreditTypeProposal) String() string {
return fmt.Sprintf(`Credit Type Proposal:
Title: %s
Description: %s
Credit Type: %v
`, m.Title, m.Description, m.CreditType)
}
54 changes: 54 additions & 0 deletions x/ecocredit/core/msg_credit_type_proposal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package core

import (
"testing"

"gotest.tools/v3/assert"
)

func TestCreditTypeProposal_ValidateBasic(t *testing.T) {
validCreditType := &CreditType{"C", "carbon", "carbon ton", 6}
type fields struct {
Title string
Description string
CreditType *CreditType
}
tests := []struct {
name string
fields fields
errMsg string
}{
{
name: "valid",
fields: fields{
Title: "hello",
Description: "world",
CreditType: validCreditType,
},
},
{
name: "invalid: nil credit type",
fields: fields{
Title: "hi",
Description: "hello",
CreditType: nil,
},
errMsg: "credit type cannot be nil",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := &CreditTypeProposal{
Title: tt.fields.Title,
Description: tt.fields.Description,
CreditType: tt.fields.CreditType,
}
err := m.ValidateBasic()
if len(tt.errMsg) != 0 {
assert.ErrorContains(t, err, tt.errMsg)
} else {
assert.NilError(t, err)
}
})
}
}
Loading