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: Add gas per-loop-iteration in ecocredit messages (#519) #526

Merged
merged 5 commits into from
Sep 16, 2021
Merged
Changes from 3 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
19 changes: 16 additions & 3 deletions x/ecocredit/server/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ 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 All @@ -29,7 +33,7 @@ func (s serverImpl) CreateClass(goCtx context.Context, req *ecocredit.MsgCreateC

var params ecocredit.Params
s.paramSpace.GetParamSet(ctx.Context, &params)
if params.AllowlistEnabled && !s.isCreatorAllowListed(params.AllowedClassCreators, adminAddress) {
if params.AllowlistEnabled && !s.isCreatorAllowListed(ctx, params.AllowedClassCreators, adminAddress) {
return nil, fmt.Errorf("%s is not allowed to create credit classes", adminAddress.String())
}

Expand Down Expand Up @@ -179,6 +183,8 @@ func (s serverImpl) CreateBatch(goCtx context.Context, req *ecocredit.MsgCreateB
if err != nil {
return nil, err
}

ctx.GasMeter().ConsumeGas(gasCostPerIteration, "batch issuance")
}

setDecimal(store, TradableSupplyKey(batchDenom), tradableSupply)
Expand Down Expand Up @@ -308,6 +314,8 @@ func (s serverImpl) Send(goCtx context.Context, req *ecocredit.MsgSend) (*ecocre
if err != nil {
return nil, err
}

ctx.GasMeter().ConsumeGas(gasCostPerIteration, "send ecocredits")
}

return &ecocredit.MsgSendResponse{}, nil
Expand Down Expand Up @@ -355,6 +363,8 @@ func (s serverImpl) Retire(goCtx context.Context, req *ecocredit.MsgRetire) (*ec
if err != nil {
return nil, err
}

ctx.GasMeter().ConsumeGas(gasCostPerIteration, "retire ecocredits")
}

return &ecocredit.MsgRetireResponse{}, nil
Expand Down Expand Up @@ -442,6 +452,8 @@ func (s serverImpl) Cancel(goCtx context.Context, req *ecocredit.MsgCancel) (*ec
if err != nil {
return nil, err
}

ctx.GasMeter().ConsumeGas(gasCostPerIteration, "cancel ecocredits")
}

return &ecocredit.MsgCancelResponse{}, nil
Expand Down Expand Up @@ -510,9 +522,10 @@ func (s serverImpl) getBatchPrecision(ctx types.Context, denom batchDenomT) (uin
return classInfo.CreditType.Precision, nil
}

// Checks if the given address is in the allowlist of credit class designers
func (s serverImpl) isCreatorAllowListed(allowlist []string, designer sdk.Address) bool {
// Checks if the given address is in the allowlist of credit class creators
func (s serverImpl) isCreatorAllowListed(ctx sdk.Context, allowlist []string, designer sdk.Address) bool {
for _, addr := range allowlist {
ctx.GasMeter().ConsumeGas(gasCostPerIteration, "credit class creators allowlist")
allowListedAddr, _ := sdk.AccAddressFromBech32(addr)
if designer.Equals(allowListedAddr) {
return true
Expand Down