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 26 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"
ecocreditcore "github.com/regen-network/regen-ledger/x/ecocredit/client/core"
)

func setCustomModuleBasics() []module.AppModuleBasic {
return []module.AppModuleBasic{
gov.NewAppModuleBasic(
paramsclient.ProposalHandler, distrclient.ProposalHandler,
upgradeclient.ProposalHandler, upgradeclient.CancelProposalHandler,
ecocreditcore.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;
}
18 changes: 0 additions & 18 deletions x/ecocredit/core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,21 +420,3 @@ func (b BatchInfo) Validate() error {

return nil
}

// Validate performs a basic validation of credit type
func (c CreditType) Validate() error {
if err := ValidateCreditTypeAbbreviation(c.Abbreviation); err != nil {
return err
}
if len(c.Name) == 0 {
return sdkerrors.ErrInvalidRequest.Wrap("name cannot be empty")
}
if len(c.Unit) == 0 {
return sdkerrors.ErrInvalidRequest.Wrap("unit cannot be empty")
}
if c.Precision != PRECISION {
return sdkerrors.ErrInvalidRequest.Wrapf("credit type precision is currently locked to %d", PRECISION)
}

return nil
}
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
28 changes: 28 additions & 0 deletions x/ecocredit/core/msg_credit_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package core

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

const (
maxCreditTypeNameLength = 75
)

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.Name) > maxCreditTypeNameLength {
return sdkerrors.ErrInvalidRequest.Wrapf("credit type name cannot exceed %d characters", maxCreditTypeNameLength)
}
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)
}
})
}
}
97 changes: 97 additions & 0 deletions x/ecocredit/core/msg_credit_type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package core

import (
"fmt"
"strings"
"testing"

"gotest.tools/v3/assert"
)

func TestCreditType_Validate(t *testing.T) {
type fields struct {
Abbreviation string
Name string
Unit string
Precision uint32
}
tests := []struct {
name string
fields fields
errMsg string
}{
{
name: "valid",
fields: fields{
Abbreviation: "C",
Name: "carbon",
Unit: "ton",
Precision: 6,
},
},
{
name: "invalid: no abbreviation",
fields: fields{
Abbreviation: "",
},
errMsg: "credit type abbreviation must be 1-3 uppercase latin letters",
},
{
name: "invalid: abbreviation too long",
fields: fields{
Abbreviation: "CARB",
},
errMsg: "credit type abbreviation must be 1-3 uppercase latin letters",
},
{
name: "invalid no name",
fields: fields{
Abbreviation: "C",
Name: "",
},
errMsg: "name cannot be empty",
},
{
name: "invalid name too long name",
fields: fields{
Abbreviation: "C",
Name: strings.Repeat("x", maxCreditTypeNameLength+1),
},
errMsg: fmt.Sprintf("credit type name cannot exceed %d characters", maxCreditTypeNameLength),
},
{
name: "invalid no unit",
fields: fields{
Abbreviation: "C",
Name: "carbon",
},
errMsg: "unit cannot be empty",
},
{
name: "invalid precision",
fields: fields{
Abbreviation: "C",
Name: "carbon",
Unit: "ton",
Precision: 3,
},
errMsg: "credit type precision is currently locked to 6",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := CreditType{
Abbreviation: tt.fields.Abbreviation,
Name: tt.fields.Name,
Unit: tt.fields.Unit,
Precision: tt.fields.Precision,
}
err := m.Validate()
if len(tt.errMsg) != 0 {
assert.ErrorContains(t, err, tt.errMsg)
} else {
assert.NilError(t, err)
}
})
}
}
Loading