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: handling ecocredits/basket web grpc #789

Merged
merged 7 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
12 changes: 1 addition & 11 deletions app/stable_appconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,7 @@ func (app *RegenApp) registerUpgradeHandlers() {
return app.mm.RunMigrations(ctx, app.configurator, fromVM)
})

upgradeV22xName := "v2.2-upgrade"
app.UpgradeKeeper.SetUpgradeHandler(upgradeV22xName, func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
// get the params
ecoParams, ok := app.ParamsKeeper.GetSubspace(ecocredittypes.ModuleName)
if !ok {
panic(fmt.Sprintf("unable to upgrade: subspace %s not found", ecocredittypes.ModuleName))
}

// set basket creation fee to 1,0000 REGEN
ecoParams.Set(ctx, ecocredittypes.KeyBasketCreationFee, sdk.NewCoins(sdk.NewInt64Coin("uregen", 1e9)))

app.UpgradeKeeper.SetUpgradeHandler("v3.0.0", func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
return app.mm.RunMigrations(ctx, app.configurator, fromVM)
})

Expand Down
16 changes: 14 additions & 2 deletions x/ecocredit/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,23 @@ func (a Module) RegisterInterfaces(registry types.InterfaceRegistry) {

func (a Module) RegisterServices(configurator servermodule.Configurator) {
server.RegisterServices(configurator, a.paramSpace, a.accountKeeper, a.bankKeeper, a.distributionKeeper)

err := configurator.RegisterMigration(ecocredit.ModuleName, 1,
func(ctx sdk.Context) error {
// set basket creation fee to 1,000 REGEN
a.paramSpace.Set(ctx, ecocredit.KeyBasketCreationFee, sdk.NewCoins(sdk.NewInt64Coin("uregen", 1e9)))
return nil
})
if err != nil {
panic(fmt.Sprintf("failed to migrate x/ecocredit from version 1 to 2: %v", err))
}
}

//nolint:errcheck
func (a Module) RegisterGRPCGatewayRoutes(clientCtx sdkclient.Context, mux *runtime.ServeMux) {
ecocredit.RegisterQueryHandlerClient(context.Background(), mux, ecocredit.NewQueryClient(clientCtx))
ctx := context.Background()
ecocredit.RegisterQueryHandlerClient(ctx, mux, ecocredit.NewQueryClient(clientCtx))
baskettypes.RegisterQueryHandlerClient(ctx, mux, baskettypes.NewQueryClient(clientCtx))
}

func (a Module) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
Expand Down Expand Up @@ -154,7 +166,7 @@ func (a Module) GetTxCmd() *cobra.Command {
}

// ConsensusVersion implements AppModule/ConsensusVersion.
func (Module) ConsensusVersion() uint64 { return 1 }
func (Module) ConsensusVersion() uint64 { return 2 }

/**** DEPRECATED ****/
func (a Module) RegisterRESTRoutes(sdkclient.Context, *mux.Router) {}
Expand Down
4 changes: 4 additions & 0 deletions x/ecocredit/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ var (
KeyBasketCreationFee = []byte("BasketCreationFee")
)

// TODO: Revisit this once we have proper gas fee framework.
// Tracking issues https://github.com/cosmos/cosmos-sdk/issues/9054, https://github.com/cosmos/cosmos-sdk/discussions/9072
const GasCostPerIteration = uint64(10)

// TODO: remove after we open governance changes for precision
const (
PRECISION uint32 = 6
Expand Down
2 changes: 2 additions & 0 deletions x/ecocredit/server/basket/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ func (k Keeper) Put(ctx context.Context, req *baskettypes.MsgPut) (*baskettypes.

// keep track of the total amount of tokens to give to the depositor
amountReceived := sdk.NewInt(0)
sdkContext := sdk.UnwrapSDKContext(ctx)
for _, credit := range req.Credits {
sdkContext.GasMeter().ConsumeGas(ecocredit.GasCostPerIteration, "ecocredit/basket/MsgPut iteration")
// get credit batch info
res, err := k.ecocreditKeeper.BatchInfo(ctx, &ecocredit.QueryBatchInfoRequest{BatchDenom: credit.BatchDenom})
if err != nil {
Expand Down
11 changes: 5 additions & 6 deletions x/ecocredit/server/basket/take.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ import (
"context"
"fmt"

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

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/regen-network/regen-ledger/types/math"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

basketv1 "github.com/regen-network/regen-ledger/api/regen/ecocredit/basket/v1"
"github.com/regen-network/regen-ledger/types"
"github.com/regen-network/regen-ledger/types/math"
"github.com/regen-network/regen-ledger/x/ecocredit"
baskettypes "github.com/regen-network/regen-ledger/x/ecocredit/basket"
)

Expand Down Expand Up @@ -78,6 +76,7 @@ func (k Keeper) Take(ctx context.Context, msg *baskettypes.MsgTake) (*baskettype
return nil, err
}
it.Close()
sdkContext.GasMeter().ConsumeGas(ecocredit.GasCostPerIteration, "ecocredit/basket/MsgTake iteration")

balance, err := math.NewDecFromString(basketBalance.Balance)
if err != nil {
Expand Down
14 changes: 5 additions & 9 deletions x/ecocredit/server/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ import (
"github.com/regen-network/regen-ledger/x/ecocredit"
)

// TODO: Revisit this once we have proper gas fee framework.
// Tracking issues https://github.com/cosmos/cosmos-sdk/issues/9054, https://github.com/cosmos/cosmos-sdk/discussions/9072
const gasCostPerIteration = uint64(10)

// CreateClass creates a new class of ecocredit
//
// The admin is charged a fee for creating the class. This is controlled by
Expand Down Expand Up @@ -176,7 +172,7 @@ func (s serverImpl) CreateBatch(goCtx context.Context, req *ecocredit.MsgCreateB
return nil, err
}

ctx.GasMeter().ConsumeGas(gasCostPerIteration, "batch issuance")
ctx.GasMeter().ConsumeGas(ecocredit.GasCostPerIteration, "ecocredit/MsgCreateBatch iteration")
}

ecocredit.SetDecimal(store, ecocredit.TradableSupplyKey(batchDenom), tradableSupply)
Expand Down Expand Up @@ -308,7 +304,7 @@ func (s serverImpl) Send(goCtx context.Context, req *ecocredit.MsgSend) (*ecocre
return nil, err
}

ctx.GasMeter().ConsumeGas(gasCostPerIteration, "send ecocredits")
ctx.GasMeter().ConsumeGas(ecocredit.GasCostPerIteration, "ecocredit/MsgSend iteration")
}

return &ecocredit.MsgSendResponse{}, nil
Expand Down Expand Up @@ -357,7 +353,7 @@ func (s serverImpl) Retire(goCtx context.Context, req *ecocredit.MsgRetire) (*ec
return nil, err
}

ctx.GasMeter().ConsumeGas(gasCostPerIteration, "retire ecocredits")
ctx.GasMeter().ConsumeGas(ecocredit.GasCostPerIteration, "ecocredit/MsgRetire iteration")
}

return &ecocredit.MsgRetireResponse{}, nil
Expand Down Expand Up @@ -446,7 +442,7 @@ func (s serverImpl) Cancel(goCtx context.Context, req *ecocredit.MsgCancel) (*ec
return nil, err
}

ctx.GasMeter().ConsumeGas(gasCostPerIteration, "cancel ecocredits")
ctx.GasMeter().ConsumeGas(ecocredit.GasCostPerIteration, "ecocredit/MsgCancel iteration")
}

return &ecocredit.MsgCancelResponse{}, nil
Expand Down Expand Up @@ -569,7 +565,7 @@ func (s serverImpl) getBatchPrecision(ctx types.Context, denom ecocredit.BatchDe
// Checks if the given address is in the allowlist of credit class creators
func (s serverImpl) isCreatorAllowListed(ctx types.Context, allowlist []string, designer sdk.Address) bool {
for _, addr := range allowlist {
ctx.GasMeter().ConsumeGas(gasCostPerIteration, "credit class creators allowlist")
ctx.GasMeter().ConsumeGas(ecocredit.GasCostPerIteration, "ecocredit / credit class creators allowlist")
allowListedAddr, _ := sdk.AccAddressFromBech32(addr)
if designer.Equals(allowListedAddr) {
return true
Expand Down
5 changes: 3 additions & 2 deletions x/ecocredit/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,10 @@ func RegisterServices(
impl := newServer(configurator.ModuleKey(), paramSpace, accountKeeper, bankKeeper, distKeeper, configurator.Marshaler())
ecocredit.RegisterMsgServer(configurator.MsgServer(), impl)
ecocredit.RegisterQueryServer(configurator.QueryServer(), impl)
baskettypes.RegisterMsgServer(configurator.MsgServer(), impl.basketKeeper)
baskettypes.RegisterQueryServer(configurator.QueryServer(), impl.basketKeeper)

configurator.RegisterGenesisHandlers(impl.InitGenesis, impl.ExportGenesis)
configurator.RegisterWeightedOperationsHandler(impl.WeightedOperations)
configurator.RegisterInvariantsHandler(impl.RegisterInvariants)
baskettypes.RegisterMsgServer(configurator.MsgServer(), impl.basketKeeper)
baskettypes.RegisterQueryServer(configurator.QueryServer(), impl.basketKeeper)
}