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: refactor gashub module #105

Merged
merged 13 commits into from
Feb 16, 2023
3 changes: 2 additions & 1 deletion baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re

ctx := app.getContextForTx(mode, txBytes)
ms := ctx.MultiStore()
gInfo.MinGasPrices = ctx.MinGasPrices().String()

// only run the tx if there is block gas remaining
if mode == runTxModeDeliver && ctx.BlockGasMeter().IsOutOfGas() {
Expand All @@ -657,7 +658,7 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re
err, result = processRecovery(r, recoveryMW), nil
}

gInfo = sdk.GasInfo{GasWanted: gasWanted, GasUsed: ctx.GasMeter().GasConsumed()}
gInfo = sdk.GasInfo{GasWanted: gasWanted, GasUsed: ctx.GasMeter().GasConsumed(), MinGasPrices: app.minGasPrices.String()}
}()

blockGasConsumed := false
Expand Down
19 changes: 18 additions & 1 deletion client/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,30 @@ func BroadcastTx(clientCtx client.Context, txf Factory, msgs ...sdk.Msg) error {
}

if txf.SimulateAndExecute() || clientCtx.Simulate {
_, adjusted, err := CalculateGas(clientCtx, txf, msgs...)
gInfo, adjusted, err := CalculateGas(clientCtx, txf, msgs...)
if err != nil {
return err
}

txf = txf.WithGas(adjusted)
_, _ = fmt.Fprintf(os.Stderr, "%s\n", GasEstimateResponse{GasEstimate: txf.Gas()})

parsedGasPrices, err := sdk.ParseCoinsNormalized(gInfo.GasInfo.MinGasPrices)
if err != nil {
return err
}
if parsedGasPrices == nil {
return fmt.Errorf("empty gas prices")
}

// we only accept one type coin
gasPrice := parsedGasPrices[0]
unclezoro marked this conversation as resolved.
Show resolved Hide resolved
fees := make(sdk.Coins, 1)
gasLimit := sdk.NewInt(int64(adjusted))
fee := gasPrice.Amount.Mul(gasLimit)
fees[0] = sdk.NewCoin(gasPrice.Denom, fee)

txf = txf.WithFees(fees.String())
}

if clientCtx.Simulate {
Expand Down
3 changes: 3 additions & 0 deletions proto/cosmos/base/abci/v1beta1/abci.proto
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ message GasInfo {

// GasUsed is the amount of gas actually consumed.
uint64 gas_used = 2;

// MinGasPrices are the min gas prices.
string min_gas_prices = 3;
unclezoro marked this conversation as resolved.
Show resolved Hide resolved
}

// Result is the union of ResponseFormat and ResponseCheckTx.
Expand Down
12 changes: 12 additions & 0 deletions proto/cosmos/gashub/v1alpha1/event.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Since: cosmos-sdk 0.43
syntax = "proto3";
package cosmos.gashub.v1alpha1;

option go_package = "github.com/cosmos/cosmos-sdk/x/gashub/types";

// EventUpdateMsgGasParams is emitted when update a msg's gas params
message EventUpdateMsgGasParams {
unclezoro marked this conversation as resolved.
Show resolved Hide resolved
string msg_type_url = 1;
string from_value = 2;
string to_value = 3;
}
80 changes: 36 additions & 44 deletions proto/cosmos/gashub/v1alpha1/gashub.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,40 @@ message Params {
option (gogoproto.equal) = true;
option (gogoproto.goproto_stringer) = false;

uint64 max_tx_size = 1 [(gogoproto.customname) = "MaxTxSize"];
uint64 min_gas_per_byte = 2 [(gogoproto.customname) = "MinGasPerByte"];
uint64 msg_grant_fixed_gas = 3 [(gogoproto.customname) = "MsgGrantFixedGas"];
uint64 msg_grant_per_item_gas = 4 [(gogoproto.customname) = "MsgGrantPerItemGas"];
uint64 msg_revoke_gas = 5 [(gogoproto.customname) = "MsgRevokeGas"];
uint64 msg_exec_gas = 6 [(gogoproto.customname) = "MsgExecGas"];
uint64 msg_send_gas = 7 [(gogoproto.customname) = "MsgSendGas"];
uint64 msg_multi_send_fixed_gas = 8 [(gogoproto.customname) = "MsgMultiSendFixedGas"];
uint64 msg_multi_send_per_item_gas = 9 [(gogoproto.customname) = "MsgMultiSendPerItemGas"];
uint64 msg_withdraw_delegator_reward_gas = 10 [(gogoproto.customname) = "MsgWithdrawDelegatorRewardGas"];
uint64 msg_withdraw_validator_commission_gas = 11 [(gogoproto.customname) = "MsgWithdrawValidatorCommissionGas"];
uint64 msg_set_withdraw_address_gas = 12 [(gogoproto.customname) = "MsgSetWithdrawAddressGas"];
uint64 msg_fund_community_pool_gas = 13 [(gogoproto.customname) = "MsgFundCommunityPoolGas"];
uint64 msg_grant_allowance_fixed_gas = 14 [(gogoproto.customname) = "MsgGrantAllowanceFixedGas"];
uint64 msg_grant_allowance_per_item_gas = 15 [(gogoproto.customname) = "MsgGrantAllowancePerItemGas"];
uint64 msg_revoke_allowance_gas = 16 [(gogoproto.customname) = "MsgRevokeAllowanceGas"];
uint64 msg_submit_proposal_gas = 17 [(gogoproto.customname) = "MsgSubmitProposalGas"];
uint64 msg_vote_gas = 18 [(gogoproto.customname) = "MsgVoteGas"];
uint64 msg_vote_weighted_gas = 19 [(gogoproto.customname) = "MsgVoteWeightedGas"];
uint64 msg_deposit_gas = 20 [(gogoproto.customname) = "MsgDepositGas"];
uint64 msg_unjail_gas = 21 [(gogoproto.customname) = "MsgUnjailGas"];
uint64 msg_impeach_gas = 22 [(gogoproto.customname) = "MsgImpeachGas"];
uint64 msg_edit_validator_gas = 23 [(gogoproto.customname) = "MsgEditValidatorGas"];
uint64 msg_delegate_gas = 24 [(gogoproto.customname) = "MsgDelegateGas"];
uint64 msg_undelegate_gas = 25 [(gogoproto.customname) = "MsgUndelegateGas"];
uint64 msg_begin_redelegate_gas = 26 [(gogoproto.customname) = "MsgBeginRedelegateGas"];
uint64 msg_cancel_unbonding_delegation_gas = 27 [(gogoproto.customname) = "MsgCancelUnbondingDelegationGas"];
uint64 msg_create_validator_gas = 28 [(gogoproto.customname) = "MsgCreateValidatorGas"];
uint64 msg_claim_gas = 29 [(gogoproto.customname) = "MsgClaimGas"];
uint64 msg_transfer_out_gas = 30 [(gogoproto.customname) = "MsgTransferOutGas"];
uint64 msg_create_storage_provider_gas = 31 [(gogoproto.customname) = "MsgCreateStorageProviderGas"];
uint64 msg_edit_storage_provider_gas = 32 [(gogoproto.customname) = "MsgEditStorageProviderGas"];
uint64 msg_sp_deposit_gas = 33 [(gogoproto.customname) = "MsgSpDepositGas"];
uint64 msg_storage_create_bucket_gas = 34 [(gogoproto.customname) = "MsgStorageCreateBucket"];
uint64 msg_storage_delete_bucket_gas = 35 [(gogoproto.customname) = "MsgStorageDeleteBucket"];
uint64 msg_storage_create_object_gas = 36 [(gogoproto.customname) = "MsgStorageCreateObject"];
uint64 msg_storage_delete_object_gas = 37 [(gogoproto.customname) = "MsgStorageDeleteObject"];
uint64 msg_storage_seal_object_gas = 38 [(gogoproto.customname) = "MsgStorageSealObject"];
uint64 msg_storage_copy_object_gas = 39 [(gogoproto.customname) = "MsgStorageCopyObject"];
uint64 msg_storage_reject_seal_object_gas = 40 [(gogoproto.customname) = "MsgStorageRejectSealObject"];
uint64 msg_storage_create_group_gas = 41 [(gogoproto.customname) = "MsgStorageCreateGroup"];
uint64 msg_storage_delete_group_gas = 42 [(gogoproto.customname) = "MsgStorageDeleteGroup"];
uint64 msg_storage_leave_group_gas = 43 [(gogoproto.customname) = "MsgStorageLeaveGroup"];
uint64 msg_storage_update_group_member_gas = 44 [(gogoproto.customname) = "MsgStorageUpdateGroupMember"];
uint64 max_tx_size = 1 [(gogoproto.customname) = "MaxTxSize"];
uint64 min_gas_per_byte = 2 [(gogoproto.customname) = "MinGasPerByte"];

repeated MsgGasParams msg_gas_params_set = 3 [(gogoproto.customname) = "MsgGasParamsSet"];
}

// MsgGasParams defines gas for a msg type
message MsgGasParams {
option (gogoproto.equal) = true;

string msg_type_url = 1 [(gogoproto.customname) = "MsgTypeUrl"];
// gas_params is the oneof that represents either fixed_gas_params or dynamic_gas_params
oneof gas_params {
owen-reorg marked this conversation as resolved.
Show resolved Hide resolved
// fixed_type specifies fixed type gas params.
FixedGasParams fixed_type = 2;
// grant_type specifies dynamic type gas params for msg/grant.
DynamicGasParams grant_type = 3;
// grant_type specifies dynamic type gas params for msg/multiSend.
DynamicGasParams multi_send_type = 4;
// grant_type specifies dynamic type gas params for msg/grantAllowance.
DynamicGasParams grant_allowance_type = 5;
}
// FixedGasParams defines the parameters for fixed gas type.
message FixedGasParams {
option (gogoproto.equal) = true;

uint64 fixed_gas = 1 [(gogoproto.customname) = "FixedGas"];
}

// DynamicGasParams defines the parameters for dynamic gas type.
message DynamicGasParams {
option (gogoproto.equal) = true;

uint64 fixed_gas = 1 [(gogoproto.customname) = "FixedGas"];
uint64 gas_per_item = 2 [(gogoproto.customname) = "GasPerItem"];
}
}
30 changes: 30 additions & 0 deletions proto/cosmos/gashub/v1alpha1/tx.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
syntax = "proto3";
package cosmos.gashub.v1alpha1;

import "gogoproto/gogo.proto";
import "cosmos/gashub/v1alpha1/gashub.proto";
import "cosmos_proto/cosmos.proto";
import "cosmos/msg/v1/msg.proto";

option go_package = "github.com/cosmos/cosmos-sdk/x/gashub/types";

// Msg defines the gashub Msg service.
service Msg {
// UpdateMsgGasParams defines a method for updating msg gas params.
rpc UpdateMsgGasParams(MsgUpdateMsgGasParams) returns (MsgUpdateMsgGasParamsResponse);
}

// MsgUpdateMsgGasParams represents a message to update msg gas params.
message MsgUpdateMsgGasParams {
// NOTE: The params should be updated by the gov module account after the proposal passes.
option (cosmos.msg.v1.signer) = "sender";

option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

string from = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
repeated MsgGasParams new_params_set = 2;
}

// MsgUpdateMsgGasParamsResponse defines the Msg/UpdateMsgGasParams response type.
message MsgUpdateMsgGasParamsResponse {}
Loading