From dd2bcce869f0993b7c7c6d57e076e066a1f31eba Mon Sep 17 00:00:00 2001 From: kbhat1 Date: Tue, 16 Apr 2024 22:43:57 -0400 Subject: [PATCH 01/13] Cosmos Gas Multiplier Params --- proto/cosmos/params/types/types.proto | 6 ++++++ types/context.go | 5 +++++ x/auth/ante/expected_keepers.go | 2 ++ x/params/keeper/keeper.go | 24 ++++++++++++++++++++++++ x/params/types/genesis.go | 13 ++++++++++++- x/params/types/params.go | 17 +++++++++++++++++ 6 files changed, 66 insertions(+), 1 deletion(-) diff --git a/proto/cosmos/params/types/types.proto b/proto/cosmos/params/types/types.proto index 9abb774fd..07d93652c 100644 --- a/proto/cosmos/params/types/types.proto +++ b/proto/cosmos/params/types/types.proto @@ -13,6 +13,12 @@ message FeesParams { [(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.DecCoins"]; } +message CosmosGasParams { + uint64 cosmos_gas_multiplier_numerator = 1; + uint64 cosmos_gas_multiplier_denominator = 2; +} + message GenesisState { FeesParams fees_params = 1 [(gogoproto.nullable) = false]; + CosmosGasParams cosmos_gas_params = 2 [(gogoproto.nullable) = false]; } diff --git a/types/context.go b/types/context.go index b2c467257..2111cb048 100644 --- a/types/context.go +++ b/types/context.go @@ -61,6 +61,11 @@ type Context struct { traceSpanContext context.Context } +type CosmosConsensusParams struct { + CosmosGasParams CosmosGasParams + TendermintConsensusParams *tmproto.ConsensusParams +} + // Proposed rename, not done to avoid API breakage type Request = Context diff --git a/x/auth/ante/expected_keepers.go b/x/auth/ante/expected_keepers.go index b4f1e4700..b7375a2fb 100644 --- a/x/auth/ante/expected_keepers.go +++ b/x/auth/ante/expected_keepers.go @@ -24,4 +24,6 @@ type FeegrantKeeper interface { type ParamsKeeper interface { SetFeesParams(ctx sdk.Context, feesParams paramtypes.FeesParams) GetFeesParams(ctx sdk.Context) paramtypes.FeesParams + SetCosmosGasParams(ctx sdk.Context, cosmosGasParams types.CosmosGasParams) + GetCosmosGasParams(ctx sdk.Context) types.CosmosGasParams } diff --git a/x/params/keeper/keeper.go b/x/params/keeper/keeper.go index 13db365fc..1fc98c6f0 100644 --- a/x/params/keeper/keeper.go +++ b/x/params/keeper/keeper.go @@ -59,6 +59,30 @@ func (k Keeper) GetFeesParams(ctx sdk.Context) types.FeesParams { return feesParams } +func (k Keeper) SetCosmosGasParams(ctx sdk.Context, cosmosGasParams types.CosmosGasParams) { + cosmosGasParams.Validate() + subspace, exist := k.GetSubspace(types.ModuleName) + if !exist { + panic("subspace params should exist") + } + subspace.Set(ctx, types.ParamStoreKeyCosmosGasParams, cosmosGasParams) +} + +func (k Keeper) GetCosmosGasParams(ctx sdk.Context) types.CosmosGasParams { + subspace, _ := k.GetSubspace(types.ModuleName) + + if !subspace.Has(ctx, types.ParamStoreKeyFeesParams) { + defaultParams := *types.DefaultFeesParams() + k.SetCosmosGasParams(ctx, defaultParams) + return defaultParams + } + + bz := subspace.GetRaw(ctx, types.ParamStoreKeyCosmosGasParams) + var cosmosGasParams types.CosmosGasParams + json.Unmarshal(bz, &cosmosGasParams) + return cosmosGasParams +} + // Logger returns a module-specific logger. func (k Keeper) Logger(ctx sdk.Context) log.Logger { return ctx.Logger().With("module", "x/"+proposal.ModuleName) diff --git a/x/params/types/genesis.go b/x/params/types/genesis.go index 9707c5840..2f1a8ccc6 100644 --- a/x/params/types/genesis.go +++ b/x/params/types/genesis.go @@ -12,13 +12,24 @@ func DefaultFeesParams() *FeesParams { } } +func DefaultCosmosGasParams() *CosmosGasParams { + return &CosmosGasParams{ + CosmosGasMultiplierNumerator: 1, + CosmosGasMultiplierDenominator: 1, + } +} + // DefaultGenesis returns the default Capability genesis state func DefaultGenesis() *GenesisState { return &GenesisState{ - FeesParams: *DefaultFeesParams(), + FeesParams: *DefaultFeesParams(), + CosmosGasParams: *DefaultCosmosGasParams(), } } func (gs GenesisState) Validate() error { + if err := gs.CosmosGasParams.Validate(); err != nil { + return err + } return gs.FeesParams.Validate() } diff --git a/x/params/types/params.go b/x/params/types/params.go index 49e0daea1..8723d6c5e 100644 --- a/x/params/types/params.go +++ b/x/params/types/params.go @@ -1,12 +1,14 @@ package types import ( + "errors" "fmt" sdk "github.com/cosmos/cosmos-sdk/types" ) var ParamStoreKeyFeesParams = []byte("FeesParams") +var ParamStoreKeyCosmosGasParams = []byte("CosmosGasParams") func NewFeesParams(minGasPrices sdk.DecCoins) FeesParams { return FeesParams{ @@ -41,3 +43,18 @@ func validateFeesParams(i interface{}) error { } return nil } + +func NewCosmosGasParams(multiplierNumerator uint64, multiplierDenominator uint64) CosmosGasParams { + return CosmosGasParams{ + CosmosGasMultiplierNumerator: multiplierNumerator, + CosmosGasMultiplierDenominator: multiplierDenominator, + } +} + +func (cg *CosmosGasParams) Validate() error { + if cg.CosmosGasMultiplierDenominator == 0 { + return errors.New("Cosmos Gas Multiplier Denominator can not be 0") + } + + return nil +} From 80ebe6eaf05957f8c27a7eb9933f22eeef6b3994 Mon Sep 17 00:00:00 2001 From: kbhat1 Date: Wed, 17 Apr 2024 07:55:25 -0400 Subject: [PATCH 02/13] proto gen --- x/params/types/types.pb.go | 325 ++++++++++++++++++++++++++++++++++--- 1 file changed, 302 insertions(+), 23 deletions(-) diff --git a/x/params/types/types.pb.go b/x/params/types/types.pb.go index 452b35311..daec2ec43 100644 --- a/x/params/types/types.pb.go +++ b/x/params/types/types.pb.go @@ -27,7 +27,7 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package // Defines fee params that are controlled through governance type FeesParams struct { - GlobalMinimumGasPrices github_com_cosmos_cosmos_sdk_types.DecCoins `protobuf:"bytes,1,rep,name=global_minimum_gas_prices,json=GlobalMinimumGasPrices,proto3,castrepeated=github.com/cosmos/cosmos-sdk/types.DecCoins" json:"global_minimum_gas_prices"` + GlobalMinimumGasPrices github_com_cosmos_cosmos_sdk_types.DecCoins `protobuf:"bytes,1,rep,name=global_minimum_gas_prices,json=globalMinimumGasPrices,proto3,castrepeated=github.com/cosmos/cosmos-sdk/types.DecCoins" json:"global_minimum_gas_prices"` } func (m *FeesParams) Reset() { *m = FeesParams{} } @@ -70,15 +70,68 @@ func (m *FeesParams) GetGlobalMinimumGasPrices() github_com_cosmos_cosmos_sdk_ty return nil } +type CosmosGasParams struct { + CosmosGasMultiplierNumerator uint64 `protobuf:"varint,1,opt,name=cosmos_gas_multiplier_numerator,json=cosmosGasMultiplierNumerator,proto3" json:"cosmos_gas_multiplier_numerator,omitempty"` + CosmosGasMultiplierDenominator uint64 `protobuf:"varint,2,opt,name=cosmos_gas_multiplier_denominator,json=cosmosGasMultiplierDenominator,proto3" json:"cosmos_gas_multiplier_denominator,omitempty"` +} + +func (m *CosmosGasParams) Reset() { *m = CosmosGasParams{} } +func (m *CosmosGasParams) String() string { return proto.CompactTextString(m) } +func (*CosmosGasParams) ProtoMessage() {} +func (*CosmosGasParams) Descriptor() ([]byte, []int) { + return fileDescriptor_56d782f42fecdb16, []int{1} +} +func (m *CosmosGasParams) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *CosmosGasParams) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_CosmosGasParams.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *CosmosGasParams) XXX_Merge(src proto.Message) { + xxx_messageInfo_CosmosGasParams.Merge(m, src) +} +func (m *CosmosGasParams) XXX_Size() int { + return m.Size() +} +func (m *CosmosGasParams) XXX_DiscardUnknown() { + xxx_messageInfo_CosmosGasParams.DiscardUnknown(m) +} + +var xxx_messageInfo_CosmosGasParams proto.InternalMessageInfo + +func (m *CosmosGasParams) GetCosmosGasMultiplierNumerator() uint64 { + if m != nil { + return m.CosmosGasMultiplierNumerator + } + return 0 +} + +func (m *CosmosGasParams) GetCosmosGasMultiplierDenominator() uint64 { + if m != nil { + return m.CosmosGasMultiplierDenominator + } + return 0 +} + type GenesisState struct { - FeesParams FeesParams `protobuf:"bytes,1,opt,name=fees_params,json=feesParams,proto3" json:"fees_params"` + FeesParams FeesParams `protobuf:"bytes,1,opt,name=fees_params,json=feesParams,proto3" json:"fees_params"` + CosmosGasParams CosmosGasParams `protobuf:"bytes,2,opt,name=cosmos_gas_params,json=cosmosGasParams,proto3" json:"cosmos_gas_params"` } func (m *GenesisState) Reset() { *m = GenesisState{} } func (m *GenesisState) String() string { return proto.CompactTextString(m) } func (*GenesisState) ProtoMessage() {} func (*GenesisState) Descriptor() ([]byte, []int) { - return fileDescriptor_56d782f42fecdb16, []int{1} + return fileDescriptor_56d782f42fecdb16, []int{2} } func (m *GenesisState) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -114,34 +167,49 @@ func (m *GenesisState) GetFeesParams() FeesParams { return FeesParams{} } +func (m *GenesisState) GetCosmosGasParams() CosmosGasParams { + if m != nil { + return m.CosmosGasParams + } + return CosmosGasParams{} +} + func init() { proto.RegisterType((*FeesParams)(nil), "cosmos.params.v1beta1.FeesParams") + proto.RegisterType((*CosmosGasParams)(nil), "cosmos.params.v1beta1.CosmosGasParams") proto.RegisterType((*GenesisState)(nil), "cosmos.params.v1beta1.GenesisState") } func init() { proto.RegisterFile("cosmos/params/types/types.proto", fileDescriptor_56d782f42fecdb16) } var fileDescriptor_56d782f42fecdb16 = []byte{ - // 294 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0x4f, 0xce, 0x2f, 0xce, - 0xcd, 0x2f, 0xd6, 0x2f, 0x48, 0x2c, 0x4a, 0xcc, 0x2d, 0xd6, 0x2f, 0xa9, 0x2c, 0x48, 0x85, 0x92, - 0x7a, 0x05, 0x45, 0xf9, 0x25, 0xf9, 0x42, 0xa2, 0x10, 0x05, 0x7a, 0x10, 0x05, 0x7a, 0x65, 0x86, - 0x49, 0xa9, 0x25, 0x89, 0x86, 0x52, 0x22, 0xe9, 0xf9, 0xe9, 0xf9, 0x60, 0x15, 0xfa, 0x20, 0x16, - 0x44, 0xb1, 0x94, 0x1c, 0xd4, 0xb4, 0xa4, 0xc4, 0xe2, 0x54, 0x7d, 0xa8, 0x52, 0xfd, 0xe4, 0xfc, - 0xcc, 0x3c, 0x88, 0xbc, 0xd2, 0x04, 0x46, 0x2e, 0x2e, 0xb7, 0xd4, 0xd4, 0xe2, 0x00, 0xb0, 0x61, - 0x42, 0x8d, 0x8c, 0x5c, 0xc2, 0xe9, 0x39, 0xf9, 0x49, 0x89, 0x39, 0xf1, 0xb9, 0x99, 0x79, 0x99, - 0xb9, 0xa5, 0xb9, 0xf1, 0x69, 0xa9, 0xa9, 0xc5, 0x12, 0x8c, 0x0a, 0xcc, 0x1a, 0xdc, 0x46, 0x32, - 0x7a, 0x50, 0xab, 0x41, 0xa6, 0xc1, 0x2c, 0xd6, 0x73, 0x49, 0x4d, 0x76, 0xce, 0xcf, 0xcc, 0x73, - 0x32, 0x3e, 0x71, 0x4f, 0x9e, 0x61, 0xd5, 0x7d, 0x79, 0xed, 0xf4, 0xcc, 0x92, 0x8c, 0xd2, 0x24, - 0xbd, 0xe4, 0xfc, 0x5c, 0x7d, 0xa8, 0xed, 0x10, 0x4a, 0xb7, 0x38, 0x25, 0x1b, 0xea, 0x13, 0xa8, - 0x9e, 0xe2, 0x20, 0x41, 0x88, 0x6d, 0xbe, 0x10, 0xcb, 0x40, 0x2e, 0x51, 0x8a, 0xe0, 0xe2, 0x71, - 0x4f, 0xcd, 0x4b, 0x2d, 0xce, 0x2c, 0x0e, 0x2e, 0x49, 0x2c, 0x49, 0x15, 0xf2, 0xe0, 0xe2, 0x06, - 0xb9, 0x21, 0x1e, 0xe2, 0x5f, 0x09, 0x46, 0x05, 0x46, 0x0d, 0x6e, 0x23, 0x45, 0x3d, 0xac, 0xa1, - 0xa0, 0x87, 0xf0, 0x8b, 0x13, 0x0b, 0xc8, 0x3d, 0x41, 0x5c, 0x69, 0x08, 0x11, 0xcf, 0x15, 0x8f, - 0xe4, 0x18, 0x4f, 0x3c, 0x92, 0x63, 0xbc, 0xf0, 0x48, 0x8e, 0xf1, 0xc1, 0x23, 0x39, 0xc6, 0x09, - 0x8f, 0xe5, 0x18, 0x2e, 0x3c, 0x96, 0x63, 0xb8, 0xf1, 0x58, 0x8e, 0x21, 0x0a, 0xbf, 0xbb, 0x2b, - 0x50, 0x22, 0x24, 0x89, 0x0d, 0x1c, 0x7c, 0xc6, 0x80, 0x00, 0x00, 0x00, 0xff, 0xff, 0xf1, 0xb8, - 0xa2, 0x52, 0xae, 0x01, 0x00, 0x00, + // 401 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x92, 0xbf, 0xae, 0xd3, 0x30, + 0x14, 0xc6, 0xe3, 0x52, 0x31, 0xb8, 0x48, 0x15, 0x11, 0xa0, 0x52, 0x55, 0xee, 0x9f, 0x01, 0x55, + 0x42, 0x38, 0x6a, 0xfb, 0x06, 0x6d, 0xa1, 0x74, 0x28, 0xaa, 0xca, 0x82, 0x58, 0x22, 0x27, 0x75, + 0x83, 0x45, 0x1c, 0x47, 0xb1, 0x83, 0xe0, 0x1d, 0x18, 0xd8, 0x19, 0x59, 0x10, 0x23, 0x4f, 0xd1, + 0xb1, 0x23, 0x13, 0xa0, 0xf4, 0x45, 0x50, 0x6c, 0x87, 0xde, 0x5b, 0xf5, 0xde, 0x25, 0x89, 0x9c, + 0xef, 0xfc, 0xce, 0xe7, 0xf3, 0x1d, 0xd8, 0x0d, 0x85, 0xe4, 0x42, 0x7a, 0x29, 0xc9, 0x08, 0x97, + 0x9e, 0xfa, 0x94, 0x52, 0xfb, 0xc4, 0x69, 0x26, 0x94, 0x70, 0x1f, 0x1a, 0x01, 0x36, 0x02, 0xfc, + 0x61, 0x14, 0x50, 0x45, 0x46, 0xed, 0x07, 0x91, 0x88, 0x84, 0x56, 0x78, 0xe5, 0x97, 0x11, 0xb7, + 0x91, 0xa5, 0x05, 0x44, 0x52, 0xcf, 0x4a, 0xbd, 0x50, 0xb0, 0xc4, 0xfc, 0x1f, 0x7c, 0x05, 0x10, + 0xbe, 0xa0, 0x54, 0xae, 0x35, 0xcc, 0xfd, 0x0c, 0xe0, 0xe3, 0x28, 0x16, 0x01, 0x89, 0x7d, 0xce, + 0x12, 0xc6, 0x73, 0xee, 0x47, 0x44, 0xfa, 0x69, 0xc6, 0x42, 0x2a, 0x5b, 0xa0, 0x77, 0x67, 0xd8, + 0x18, 0x77, 0xb0, 0x35, 0x50, 0x32, 0xab, 0xf6, 0x78, 0x4e, 0xc3, 0x99, 0x60, 0xc9, 0x74, 0xb2, + 0xff, 0xdd, 0x75, 0x7e, 0xfc, 0xe9, 0x3e, 0x8d, 0x98, 0x7a, 0x97, 0x07, 0x38, 0x14, 0xdc, 0xb3, + 0x1e, 0xcc, 0xeb, 0x99, 0xdc, 0xbe, 0xb7, 0xf7, 0xb1, 0x35, 0x72, 0xf3, 0xc8, 0xf4, 0x5c, 0x99, + 0x96, 0x0b, 0x22, 0xd7, 0xba, 0xe1, 0xe0, 0x1b, 0x80, 0xcd, 0x99, 0xae, 0x2a, 0xcf, 0x8c, 0xc5, + 0xe7, 0xd5, 0x84, 0xb4, 0x33, 0x9e, 0xc7, 0x8a, 0xa5, 0x31, 0xa3, 0x99, 0x9f, 0xe4, 0x9c, 0x66, + 0x44, 0x89, 0xac, 0x05, 0x7a, 0x60, 0x58, 0xdf, 0x74, 0xc2, 0xaa, 0x72, 0xf5, 0x5f, 0xf4, 0xaa, + 0xd2, 0xb8, 0x4b, 0xd8, 0xbf, 0x8c, 0xd9, 0xd2, 0x44, 0x70, 0x96, 0x68, 0x50, 0x4d, 0x83, 0xd0, + 0x05, 0xd0, 0xfc, 0xa4, 0x1a, 0xfc, 0x04, 0xf0, 0xde, 0x82, 0x26, 0x54, 0x32, 0xf9, 0x5a, 0x11, + 0x45, 0xdd, 0x97, 0xb0, 0xb1, 0xa3, 0x54, 0xfa, 0x26, 0x21, 0x6d, 0xa7, 0x31, 0xee, 0xe3, 0x8b, + 0xb9, 0xe1, 0xd3, 0xf4, 0xa7, 0xf5, 0x72, 0x76, 0x1b, 0xb8, 0x3b, 0xe5, 0xf1, 0x06, 0xde, 0xbf, + 0xe2, 0xd2, 0xf2, 0x6a, 0x9a, 0xf7, 0xe4, 0x06, 0xde, 0xd9, 0xbc, 0x2c, 0xb4, 0x19, 0x9e, 0x1d, + 0x2f, 0xbf, 0x17, 0x08, 0xec, 0x0b, 0x04, 0x0e, 0x05, 0x02, 0x7f, 0x0b, 0x04, 0xbe, 0x1c, 0x91, + 0x73, 0x38, 0x22, 0xe7, 0xd7, 0x11, 0x39, 0x6f, 0x6f, 0x4f, 0xef, 0xe3, 0xb5, 0xe5, 0x0c, 0xee, + 0xea, 0x55, 0x9a, 0xfc, 0x0b, 0x00, 0x00, 0xff, 0xff, 0x26, 0x95, 0xab, 0x38, 0xba, 0x02, 0x00, + 0x00, } func (this *FeesParams) Equal(that interface{}) bool { @@ -173,6 +241,33 @@ func (this *FeesParams) Equal(that interface{}) bool { } return true } +func (this *CosmosGasParams) Equal(that interface{}) bool { + if that == nil { + return this == nil + } + + that1, ok := that.(*CosmosGasParams) + if !ok { + that2, ok := that.(CosmosGasParams) + if ok { + that1 = &that2 + } else { + return false + } + } + if that1 == nil { + return this == nil + } else if this == nil { + return false + } + if this.CosmosGasMultiplierNumerator != that1.CosmosGasMultiplierNumerator { + return false + } + if this.CosmosGasMultiplierDenominator != that1.CosmosGasMultiplierDenominator { + return false + } + return true +} func (this *GenesisState) Equal(that interface{}) bool { if that == nil { return this == nil @@ -195,6 +290,9 @@ func (this *GenesisState) Equal(that interface{}) bool { if !this.FeesParams.Equal(&that1.FeesParams) { return false } + if !this.CosmosGasParams.Equal(&that1.CosmosGasParams) { + return false + } return true } func (m *FeesParams) Marshal() (dAtA []byte, err error) { @@ -234,6 +332,39 @@ func (m *FeesParams) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } +func (m *CosmosGasParams) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *CosmosGasParams) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *CosmosGasParams) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.CosmosGasMultiplierDenominator != 0 { + i = encodeVarintTypes(dAtA, i, uint64(m.CosmosGasMultiplierDenominator)) + i-- + dAtA[i] = 0x10 + } + if m.CosmosGasMultiplierNumerator != 0 { + i = encodeVarintTypes(dAtA, i, uint64(m.CosmosGasMultiplierNumerator)) + i-- + dAtA[i] = 0x8 + } + return len(dAtA) - i, nil +} + func (m *GenesisState) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) @@ -254,6 +385,16 @@ func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + { + size, err := m.CosmosGasParams.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintTypes(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 { size, err := m.FeesParams.MarshalToSizedBuffer(dAtA[:i]) if err != nil { @@ -293,6 +434,21 @@ func (m *FeesParams) Size() (n int) { return n } +func (m *CosmosGasParams) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.CosmosGasMultiplierNumerator != 0 { + n += 1 + sovTypes(uint64(m.CosmosGasMultiplierNumerator)) + } + if m.CosmosGasMultiplierDenominator != 0 { + n += 1 + sovTypes(uint64(m.CosmosGasMultiplierDenominator)) + } + return n +} + func (m *GenesisState) Size() (n int) { if m == nil { return 0 @@ -301,6 +457,8 @@ func (m *GenesisState) Size() (n int) { _ = l l = m.FeesParams.Size() n += 1 + l + sovTypes(uint64(l)) + l = m.CosmosGasParams.Size() + n += 1 + l + sovTypes(uint64(l)) return n } @@ -394,6 +552,94 @@ func (m *FeesParams) Unmarshal(dAtA []byte) error { } return nil } +func (m *CosmosGasParams) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: CosmosGasParams: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: CosmosGasParams: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field CosmosGasMultiplierNumerator", wireType) + } + m.CosmosGasMultiplierNumerator = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.CosmosGasMultiplierNumerator |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 2: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field CosmosGasMultiplierDenominator", wireType) + } + m.CosmosGasMultiplierDenominator = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.CosmosGasMultiplierDenominator |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + default: + iNdEx = preIndex + skippy, err := skipTypes(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthTypes + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} func (m *GenesisState) Unmarshal(dAtA []byte) error { l := len(dAtA) iNdEx := 0 @@ -456,6 +702,39 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field CosmosGasParams", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthTypes + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthTypes + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.CosmosGasParams.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipTypes(dAtA[iNdEx:]) From 60b826ac6477ab96bfb3dee8becb4465888b0677 Mon Sep 17 00:00:00 2001 From: kbhat1 Date: Wed, 17 Apr 2024 08:04:43 -0400 Subject: [PATCH 03/13] Update expected keeper --- x/auth/ante/expected_keepers.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x/auth/ante/expected_keepers.go b/x/auth/ante/expected_keepers.go index b7375a2fb..79af95e0a 100644 --- a/x/auth/ante/expected_keepers.go +++ b/x/auth/ante/expected_keepers.go @@ -24,6 +24,6 @@ type FeegrantKeeper interface { type ParamsKeeper interface { SetFeesParams(ctx sdk.Context, feesParams paramtypes.FeesParams) GetFeesParams(ctx sdk.Context) paramtypes.FeesParams - SetCosmosGasParams(ctx sdk.Context, cosmosGasParams types.CosmosGasParams) - GetCosmosGasParams(ctx sdk.Context) types.CosmosGasParams + SetCosmosGasParams(ctx sdk.Context, cosmosGasParams paramtypes.CosmosGasParams) + GetCosmosGasParams(ctx sdk.Context) paramtypes.CosmosGasParams } From a387a5f0dcb79083d3210a06567301c4935614b4 Mon Sep 17 00:00:00 2001 From: kbhat1 Date: Wed, 17 Apr 2024 08:06:53 -0400 Subject: [PATCH 04/13] Update default params --- x/params/keeper/keeper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/params/keeper/keeper.go b/x/params/keeper/keeper.go index 1fc98c6f0..fb9e024a2 100644 --- a/x/params/keeper/keeper.go +++ b/x/params/keeper/keeper.go @@ -72,7 +72,7 @@ func (k Keeper) GetCosmosGasParams(ctx sdk.Context) types.CosmosGasParams { subspace, _ := k.GetSubspace(types.ModuleName) if !subspace.Has(ctx, types.ParamStoreKeyFeesParams) { - defaultParams := *types.DefaultFeesParams() + defaultParams := *types.DefaultCosmosGasParams() k.SetCosmosGasParams(ctx, defaultParams) return defaultParams } From c76d2e31505f94653aa4885013d0b7febf0dd56f Mon Sep 17 00:00:00 2001 From: kbhat1 Date: Wed, 17 Apr 2024 08:25:20 -0400 Subject: [PATCH 05/13] Update context type --- types/context.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/types/context.go b/types/context.go index 2111cb048..b2c467257 100644 --- a/types/context.go +++ b/types/context.go @@ -61,11 +61,6 @@ type Context struct { traceSpanContext context.Context } -type CosmosConsensusParams struct { - CosmosGasParams CosmosGasParams - TendermintConsensusParams *tmproto.ConsensusParams -} - // Proposed rename, not done to avoid API breakage type Request = Context From f6bd65810ffec3792f52a676380b0cdfb8567929 Mon Sep 17 00:00:00 2001 From: kbhat1 Date: Wed, 17 Apr 2024 08:35:05 -0400 Subject: [PATCH 06/13] Validate --- x/params/types/params.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/x/params/types/params.go b/x/params/types/params.go index 8723d6c5e..d2e178ef7 100644 --- a/x/params/types/params.go +++ b/x/params/types/params.go @@ -20,6 +20,7 @@ func NewFeesParams(minGasPrices sdk.DecCoins) FeesParams { func ParamKeyTable() KeyTable { return NewKeyTable( NewParamSetPair(ParamStoreKeyFeesParams, &FeesParams{}, validateFeesParams), + NewParamSetPair(ParamStoreKeyCosmosGasParams, &CosmosGasParams{}, validateCosmosGasParams), ) } @@ -58,3 +59,15 @@ func (cg *CosmosGasParams) Validate() error { return nil } + +func validateCosmosGasParams(i interface{}) error { + v, ok := i.(CosmosGasParams) + if !ok { + return fmt.Errorf("invalid parameter type: %T", i) + } + + if err := v.Validate(); err != nil { + return err + } + return nil +} From 88fd96d3e0b4c37a1fc3ac1350c9568a6e997f88 Mon Sep 17 00:00:00 2001 From: kbhat1 Date: Wed, 17 Apr 2024 10:11:23 -0400 Subject: [PATCH 07/13] Update fields --- x/params/keeper/keeper.go | 2 +- x/params/module.go | 1 + x/params/types/types.pb.go | 4 ++-- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/x/params/keeper/keeper.go b/x/params/keeper/keeper.go index fb9e024a2..e71b90029 100644 --- a/x/params/keeper/keeper.go +++ b/x/params/keeper/keeper.go @@ -71,7 +71,7 @@ func (k Keeper) SetCosmosGasParams(ctx sdk.Context, cosmosGasParams types.Cosmos func (k Keeper) GetCosmosGasParams(ctx sdk.Context) types.CosmosGasParams { subspace, _ := k.GetSubspace(types.ModuleName) - if !subspace.Has(ctx, types.ParamStoreKeyFeesParams) { + if !subspace.Has(ctx, types.ParamStoreKeyCosmosGasParams) { defaultParams := *types.DefaultCosmosGasParams() k.SetCosmosGasParams(ctx, defaultParams) return defaultParams diff --git a/x/params/module.go b/x/params/module.go index 9b88452f3..1b865b1c0 100644 --- a/x/params/module.go +++ b/x/params/module.go @@ -100,6 +100,7 @@ func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} func (am AppModule) InitGenesis(ctx sdk.Context, _ codec.JSONCodec, _ json.RawMessage) []abci.ValidatorUpdate { defaultGenesis := types.DefaultGenesis() am.keeper.SetFeesParams(ctx, defaultGenesis.FeesParams) + am.keeper.SetCosmosGasParams(ctx, defaultGenesis.CosmosGasParams) return []abci.ValidatorUpdate{} } diff --git a/x/params/types/types.pb.go b/x/params/types/types.pb.go index daec2ec43..40b067edd 100644 --- a/x/params/types/types.pb.go +++ b/x/params/types/types.pb.go @@ -71,8 +71,8 @@ func (m *FeesParams) GetGlobalMinimumGasPrices() github_com_cosmos_cosmos_sdk_ty } type CosmosGasParams struct { - CosmosGasMultiplierNumerator uint64 `protobuf:"varint,1,opt,name=cosmos_gas_multiplier_numerator,json=cosmosGasMultiplierNumerator,proto3" json:"cosmos_gas_multiplier_numerator,omitempty"` - CosmosGasMultiplierDenominator uint64 `protobuf:"varint,2,opt,name=cosmos_gas_multiplier_denominator,json=cosmosGasMultiplierDenominator,proto3" json:"cosmos_gas_multiplier_denominator,omitempty"` + CosmosGasMultiplierNumerator uint64 `protobuf:"varint,1,opt,name=cosmos_gas_multiplier_numerator,json=cosmosGasMultiplierNumerator,proto3" json:"cosmos_gas_multiplier_numerator,string,omitempty"` + CosmosGasMultiplierDenominator uint64 `protobuf:"varint,2,opt,name=cosmos_gas_multiplier_denominator,json=cosmosGasMultiplierDenominator,proto3" json:"cosmos_gas_multiplier_denominator,string,omitempty"` } func (m *CosmosGasParams) Reset() { *m = CosmosGasParams{} } From 1afcb76539999493fff56e971dabbd89ebcf2304 Mon Sep 17 00:00:00 2001 From: kbhat1 Date: Wed, 17 Apr 2024 11:06:48 -0400 Subject: [PATCH 08/13] Add migration --- x/params/keeper/migrations.go | 23 +++++++++++++++++++++++ x/params/keeper/migrations_test.go | 22 ++++++++++++++++++++++ x/params/module.go | 5 +++-- 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 x/params/keeper/migrations.go create mode 100644 x/params/keeper/migrations_test.go diff --git a/x/params/keeper/migrations.go b/x/params/keeper/migrations.go new file mode 100644 index 000000000..171468a0e --- /dev/null +++ b/x/params/keeper/migrations.go @@ -0,0 +1,23 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/params/types" +) + +// Migrator is a struct for handling in-place store migrations. +type Migrator struct { + keeper Keeper +} + +// NewMigrator returns a new Migrator. +func NewMigrator(keeper Keeper) Migrator { + return Migrator{keeper: keeper} +} + +// Migrate1to2 migrates from version 1 to 2. +func (m Migrator) Migrate1to2(ctx sdk.Context) error { + defaultGenesis := types.DefaultGenesis() + m.keeper.SetCosmosGasParams(ctx, defaultGenesis.CosmosGasParams) + return nil +} diff --git a/x/params/keeper/migrations_test.go b/x/params/keeper/migrations_test.go new file mode 100644 index 000000000..e816b4bdc --- /dev/null +++ b/x/params/keeper/migrations_test.go @@ -0,0 +1,22 @@ +package keeper_test + +import ( + "testing" + + pk "github.com/cosmos/cosmos-sdk/x/params/keeper" + "github.com/cosmos/cosmos-sdk/x/params/types" + + "github.com/stretchr/testify/require" +) + +func TestMigrate1to2(t *testing.T) { + _, ctx, _, _, keeper := testComponents() + m := pk.NewMigrator(keeper) + err := m.Migrate1to2(ctx) + require.Nil(t, err) + cosmosGasParams := keeper.GetCosmosGasParams(ctx) + // ensure set to defaults + defaultParams := types.DefaultGenesis() + require.Equal(t, defaultParams.CosmosGasParams.CosmosGasMultiplierNumerator, cosmosGasParams.CosmosGasMultiplierNumerator) + require.Equal(t, defaultParams.CosmosGasParams.CosmosGasMultiplierDenominator, cosmosGasParams.CosmosGasMultiplierDenominator) +} diff --git a/x/params/module.go b/x/params/module.go index 1b865b1c0..85e448633 100644 --- a/x/params/module.go +++ b/x/params/module.go @@ -100,7 +100,6 @@ func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} func (am AppModule) InitGenesis(ctx sdk.Context, _ codec.JSONCodec, _ json.RawMessage) []abci.ValidatorUpdate { defaultGenesis := types.DefaultGenesis() am.keeper.SetFeesParams(ctx, defaultGenesis.FeesParams) - am.keeper.SetCosmosGasParams(ctx, defaultGenesis.CosmosGasParams) return []abci.ValidatorUpdate{} } @@ -121,6 +120,8 @@ func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sd // module-specific gRPC queries. func (am AppModule) RegisterServices(cfg module.Configurator) { proposal.RegisterQueryServer(cfg.QueryServer(), am.keeper) + m := keeper.NewMigrator(am.keeper) + _ = cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2) } // ProposalContents returns all the params content functions used to @@ -148,4 +149,4 @@ func (am AppModule) ExportGenesis(_ sdk.Context, _ codec.JSONCodec) json.RawMess } // ConsensusVersion implements AppModule/ConsensusVersion. -func (AppModule) ConsensusVersion() uint64 { return 1 } +func (AppModule) ConsensusVersion() uint64 { return 2 } From 58178e8b1dc1d57efe0a1488c095c1490cb709cf Mon Sep 17 00:00:00 2001 From: kbhat1 Date: Wed, 17 Apr 2024 12:28:57 -0400 Subject: [PATCH 09/13] Add more validation --- x/params/keeper/keeper_test.go | 16 ++++++++++++++++ x/params/types/params.go | 6 +++++- x/params/types/params_test.go | 18 ++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 x/params/types/params_test.go diff --git a/x/params/keeper/keeper_test.go b/x/params/keeper/keeper_test.go index 44c8223f2..7c3d32aa4 100644 --- a/x/params/keeper/keeper_test.go +++ b/x/params/keeper/keeper_test.go @@ -261,3 +261,19 @@ func TestJSONUpdate(t *testing.T) { space.Get(ctx, key, ¶m) require.Equal(t, paramJSON{40964096, "goodbyeworld"}, param) } + +func TestUpdateCosmosGasParams(t *testing.T) { + _, ctx, _, _, keeper := testComponents() + cosmosGasParams := keeper.GetCosmosGasParams(ctx) + // If not set, gas params set to default + defaultParams := types.DefaultCosmosGasParams() + require.Equal(t, defaultParams.CosmosGasMultiplierNumerator, cosmosGasParams.CosmosGasMultiplierNumerator) + require.Equal(t, defaultParams.CosmosGasMultiplierDenominator, cosmosGasParams.CosmosGasMultiplierDenominator) + + // Update to 1/4 + keeper.SetCosmosGasParams(ctx, types.NewCosmosGasParams(1, 4)) + + cosmosGasParams = keeper.GetCosmosGasParams(ctx) + require.Equal(t, uint64(1), cosmosGasParams.CosmosGasMultiplierNumerator) + require.Equal(t, uint64(4), cosmosGasParams.CosmosGasMultiplierDenominator) +} diff --git a/x/params/types/params.go b/x/params/types/params.go index d2e178ef7..083e28d91 100644 --- a/x/params/types/params.go +++ b/x/params/types/params.go @@ -53,8 +53,12 @@ func NewCosmosGasParams(multiplierNumerator uint64, multiplierDenominator uint64 } func (cg *CosmosGasParams) Validate() error { + if cg.CosmosGasMultiplierNumerator == 0 { + return errors.New("cosmos gas multiplier numerator can not be 0") + } + if cg.CosmosGasMultiplierDenominator == 0 { - return errors.New("Cosmos Gas Multiplier Denominator can not be 0") + return errors.New("cosmos gas multiplier denominator can not be 0") } return nil diff --git a/x/params/types/params_test.go b/x/params/types/params_test.go new file mode 100644 index 000000000..aa72f6120 --- /dev/null +++ b/x/params/types/params_test.go @@ -0,0 +1,18 @@ +package types + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestCosmosGasParams(t *testing.T) { + // Verify validation: numerator and denominator != 0 + invalidCosmosGasParam := NewCosmosGasParams(0, 1) + err := invalidCosmosGasParam.Validate() + require.NotNil(t, err) + + invalidCosmosGasParam = NewCosmosGasParams(1, 0) + err = invalidCosmosGasParam.Validate() + require.NotNil(t, err) +} From 060b94d4fb6693ad7067caee9071a70f35f5f45b Mon Sep 17 00:00:00 2001 From: kbhat1 Date: Wed, 17 Apr 2024 13:39:39 -0400 Subject: [PATCH 10/13] add cli query for cosmos gas params --- x/params/client/cli/query.go | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/x/params/client/cli/query.go b/x/params/client/cli/query.go index 6be4455dc..cd057de86 100644 --- a/x/params/client/cli/query.go +++ b/x/params/client/cli/query.go @@ -23,6 +23,7 @@ func NewQueryCmd() *cobra.Command { cmd.AddCommand(NewQuerySubspaceParamsCmd()) cmd.AddCommand(NewQueryFeeParamsCmd()) + cmd.AddCommand(NewQueryCosmosGasParamsCmd()) cmd.AddCommand(NewQueryBlockParamsCmd()) return cmd @@ -57,7 +58,6 @@ func NewQuerySubspaceParamsCmd() *cobra.Command { return cmd } - func NewQueryFeeParamsCmd() *cobra.Command { cmd := &cobra.Command{ Use: "feesparams", @@ -87,6 +87,35 @@ func NewQueryFeeParamsCmd() *cobra.Command { return cmd } +func NewQueryCosmosGasParamsCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "cosmosgasparams", + Short: "Query for cosmos gas params", + RunE: func(cmd *cobra.Command, args []string) error { + clientCtx, err := client.GetClientQueryContext(cmd) + if err != nil { + return err + } + queryClient := proposal.NewQueryClient(clientCtx) + + params := proposal.QueryParamsRequest{Subspace: "params", Key: string(types.ParamStoreKeyCosmosGasParams)} + res, err := queryClient.Params(cmd.Context(), ¶ms) + if err != nil { + return err + } + + cosmosGasParams := types.CosmosGasParams{} + clientCtx.Codec.UnmarshalJSON([]byte(res.Param.Value), &cosmosGasParams) + + return clientCtx.PrintProto(&cosmosGasParams) + }, + } + + flags.AddQueryFlagsToCmd(cmd) + + return cmd +} + func NewQueryBlockParamsCmd() *cobra.Command { cmd := &cobra.Command{ Use: "blockparams", From 0a05ed43e519b87d311ca537ed05d21b116c184e Mon Sep 17 00:00:00 2001 From: kbhat1 Date: Wed, 17 Apr 2024 14:25:38 -0400 Subject: [PATCH 11/13] Remove write inside the GetCosmosGasParams --- x/params/keeper/keeper.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/x/params/keeper/keeper.go b/x/params/keeper/keeper.go index e71b90029..2a88d5a8f 100644 --- a/x/params/keeper/keeper.go +++ b/x/params/keeper/keeper.go @@ -71,14 +71,12 @@ func (k Keeper) SetCosmosGasParams(ctx sdk.Context, cosmosGasParams types.Cosmos func (k Keeper) GetCosmosGasParams(ctx sdk.Context) types.CosmosGasParams { subspace, _ := k.GetSubspace(types.ModuleName) + var cosmosGasParams types.CosmosGasParams if !subspace.Has(ctx, types.ParamStoreKeyCosmosGasParams) { - defaultParams := *types.DefaultCosmosGasParams() - k.SetCosmosGasParams(ctx, defaultParams) - return defaultParams + return cosmosGasParams } bz := subspace.GetRaw(ctx, types.ParamStoreKeyCosmosGasParams) - var cosmosGasParams types.CosmosGasParams json.Unmarshal(bz, &cosmosGasParams) return cosmosGasParams } From cb689d04720e7debd7eeb6c21b0a222bf86f84e6 Mon Sep 17 00:00:00 2001 From: kbhat1 Date: Wed, 17 Apr 2024 14:38:38 -0400 Subject: [PATCH 12/13] infinite multiplier gas meter --- store/types/gas.go | 29 +++++++++++++++++++++++++++++ store/types/gas_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/store/types/gas.go b/store/types/gas.go index ef2283964..0a24db379 100644 --- a/store/types/gas.go +++ b/store/types/gas.go @@ -272,6 +272,35 @@ func (g *infiniteGasMeter) String() string { return fmt.Sprintf("InfiniteGasMeter:\n consumed: %d", g.consumed) } +type infiniteMultiplierGasMeter struct { + infiniteGasMeter + multiplierNumerator uint64 + multiplierDenominator uint64 +} + +func NewInfiniteMultiplierGasMeter(multiplierNumerator uint64, multiplierDenominator uint64) GasMeter { + return &infiniteMultiplierGasMeter{ + infiniteGasMeter: infiniteGasMeter{ + consumed: 0, + lock: &sync.Mutex{}, + }, + multiplierNumerator: multiplierNumerator, + multiplierDenominator: multiplierDenominator, + } +} + +func (g *infiniteMultiplierGasMeter) adjustGas(original Gas) Gas { + return original * g.multiplierNumerator / g.multiplierDenominator +} + +func (g *infiniteMultiplierGasMeter) ConsumeGas(amount Gas, descriptor string) { + g.infiniteGasMeter.ConsumeGas(g.adjustGas(amount), descriptor) +} + +func (g *infiniteMultiplierGasMeter) RefundGas(amount Gas, descriptor string) { + g.infiniteGasMeter.RefundGas(g.adjustGas(amount), descriptor) +} + type noConsumptionInfiniteGasMeter struct { infiniteGasMeter } diff --git a/store/types/gas_test.go b/store/types/gas_test.go index 683b99037..c02ecf969 100644 --- a/store/types/gas_test.go +++ b/store/types/gas_test.go @@ -107,6 +107,37 @@ func TestMultiplierGasMeter(t *testing.T) { } } +func TestInfiniteMultiplierGasMeter(t *testing.T) { + t.Parallel() + cases := []struct { + usage []Gas + multiplierNumerator uint64 + multiplierDenominator uint64 + }{ + {[]Gas{1, 2, 3, 4}, 1, 1}, + {[]Gas{40, 30, 20, 10}, 10, 1}, + {[]Gas{99998, 2, 100000}, 1, 2}, + {[]Gas{50000000, 40000000, 10000000}, 1, 1}, + {[]Gas{32768, 32767}, 1, 1}, + {[]Gas{32768, 32767, 1}, 1, 1}, + } + + for tcnum, tc := range cases { + meter := NewInfiniteMultiplierGasMeter(tc.multiplierNumerator, tc.multiplierDenominator) + used := uint64(0) + + for unum, usage := range tc.usage { + usage := usage + used += usage * tc.multiplierNumerator / tc.multiplierDenominator + require.NotPanics(t, func() { meter.ConsumeGas(usage, "") }, "Not exceeded limit but panicked. tc #%d, usage #%d", tcnum, unum) + require.Equal(t, used, meter.GasConsumed(), "Gas consumption not match. tc #%d, usage #%d", tcnum, unum) + require.Equal(t, used, meter.GasConsumedToLimit(), "Gas consumption (to limit) not match. tc #%d, usage #%d", tcnum, unum) + require.False(t, meter.IsPastLimit(), "Not exceeded limit but got IsPastLimit() true") + require.False(t, meter.IsOutOfGas(), "Not yet at limit but got IsOutOfGas() true") + } + } +} + func TestAddUint64Overflow(t *testing.T) { t.Parallel() testCases := []struct { From d2fa1374ec6241de426521232ebcae11720ef976 Mon Sep 17 00:00:00 2001 From: kbhat1 Date: Thu, 18 Apr 2024 09:47:44 -0400 Subject: [PATCH 13/13] Add back cachekv event manager --- store/cachekv/mergeiterator.go | 23 +++++++++++++++-------- store/cachekv/mergeiterator_test.go | 28 ++++++++++++++++++++++------ store/cachekv/store.go | 7 ++++++- 3 files changed, 43 insertions(+), 15 deletions(-) diff --git a/store/cachekv/mergeiterator.go b/store/cachekv/mergeiterator.go index a32dfb346..f13c4025c 100644 --- a/store/cachekv/mergeiterator.go +++ b/store/cachekv/mergeiterator.go @@ -16,10 +16,11 @@ import ( // // TODO: Optimize by memoizing. type cacheMergeIterator struct { - parent types.Iterator - cache types.Iterator - ascending bool - storeKey sdktypes.StoreKey + parent types.Iterator + cache types.Iterator + ascending bool + storeKey sdktypes.StoreKey + eventManager *sdktypes.EventManager } var _ types.Iterator = (*cacheMergeIterator)(nil) @@ -28,12 +29,14 @@ func NewCacheMergeIterator( parent, cache types.Iterator, ascending bool, storeKey sdktypes.StoreKey, + eventManager *sdktypes.EventManager, ) *cacheMergeIterator { iter := &cacheMergeIterator{ - parent: parent, - cache: cache, - ascending: ascending, - storeKey: storeKey, + parent: parent, + cache: cache, + ascending: ascending, + storeKey: storeKey, + eventManager: eventManager, } return iter @@ -135,12 +138,14 @@ func (iter *cacheMergeIterator) Value() []byte { // If parent is invalid, get the cache value. if !iter.parent.Valid() { value := iter.cache.Value() + iter.eventManager.EmitResourceAccessReadEvent("iterator", iter.storeKey, iter.cache.Key(), value) return value } // If cache is invalid, get the parent value. if !iter.cache.Valid() { value := iter.parent.Value() + iter.eventManager.EmitResourceAccessReadEvent("iterator", iter.storeKey, iter.parent.Key(), value) return value } @@ -151,9 +156,11 @@ func (iter *cacheMergeIterator) Value() []byte { switch cmp { case -1: // parent < cache value := iter.parent.Value() + iter.eventManager.EmitResourceAccessReadEvent("iterator", iter.storeKey, keyP, value) return value case 0, 1: // parent >= cache value := iter.cache.Value() + iter.eventManager.EmitResourceAccessReadEvent("iterator", iter.storeKey, keyC, value) return value default: panic("invalid comparison result") diff --git a/store/cachekv/mergeiterator_test.go b/store/cachekv/mergeiterator_test.go index b2648a865..00f065151 100644 --- a/store/cachekv/mergeiterator_test.go +++ b/store/cachekv/mergeiterator_test.go @@ -6,6 +6,7 @@ import ( "github.com/cosmos/cosmos-sdk/store/cachekv" "github.com/cosmos/cosmos-sdk/store/dbadapter" "github.com/cosmos/cosmos-sdk/store/types" + sdktypes "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" dbm "github.com/tendermint/tm-db" ) @@ -13,6 +14,7 @@ import ( func TestMangerIterator(t *testing.T) { // initiate mock kvstore mem := dbadapter.Store{DB: dbm.NewMemDB()} + eventManager := sdktypes.NewEventManager() kvstore := cachekv.NewStore(mem, types.NewKVStoreKey("CacheKvTest"), types.DefaultCacheSizeLimit) value := randSlice(defaultValueSizeBz) startKey := randSlice(32) @@ -27,13 +29,27 @@ func TestMangerIterator(t *testing.T) { cache := kvstore.Iterator(nil, nil) for ; cache.Valid(); cache.Next() { } - iter := cachekv.NewCacheMergeIterator(parent, cache, true, types.NewKVStoreKey("CacheKvTest")) + iter := cachekv.NewCacheMergeIterator(parent, cache, true, types.NewKVStoreKey("CacheKvTest"), eventManager) - // get the next value and it should not be nil - nextValue := iter.Value() - require.NotNil(t, nextValue) + // get the next value + iter.Value() + + // assert the resource access is still emitted correctly when the cache store is unavailable + require.Equal(t, "access_type", string(eventManager.Events()[0].Attributes[0].Key)) + require.Equal(t, "read", string(eventManager.Events()[0].Attributes[0].Value)) + require.Equal(t, "store_key", string(eventManager.Events()[0].Attributes[1].Key)) + require.Equal(t, "CacheKvTest", string(eventManager.Events()[0].Attributes[1].Value)) + + // assert event emission when cache is available + cache = kvstore.Iterator(keys[1], keys[2]) + iter = cachekv.NewCacheMergeIterator(parent, cache, true, types.NewKVStoreKey("CacheKvTest"), eventManager) // get the next value - nextValue = iter.Value() - require.NotNil(t, nextValue) + iter.Value() + + // assert the resource access is still emitted correctly when the cache store is available + require.Equal(t, "access_type", string(eventManager.Events()[0].Attributes[0].Key)) + require.Equal(t, "read", string(eventManager.Events()[0].Attributes[0].Value)) + require.Equal(t, "store_key", string(eventManager.Events()[0].Attributes[1].Key)) + require.Equal(t, "CacheKvTest", string(eventManager.Events()[0].Attributes[1].Value)) } diff --git a/store/cachekv/store.go b/store/cachekv/store.go index a82be0a01..16a4a7e26 100644 --- a/store/cachekv/store.go +++ b/store/cachekv/store.go @@ -56,6 +56,8 @@ func (store *Store) GetEvents() []abci.Event { // Implements Store func (store *Store) ResetEvents() { + store.mtx.Lock() + defer store.mtx.Unlock() store.eventManager = sdktypes.NewEventManager() } @@ -75,6 +77,7 @@ func (store *Store) getFromCache(key []byte) []byte { // Get implements types.KVStore. func (store *Store) Get(key []byte) (value []byte) { types.AssertValidKey(key) + store.eventManager.EmitResourceAccessReadEvent("get", store.storeKey, key, value) return store.getFromCache(key) } @@ -83,11 +86,13 @@ func (store *Store) Set(key []byte, value []byte) { types.AssertValidKey(key) types.AssertValidValue(value) store.setCacheValue(key, value, false, true) + store.eventManager.EmitResourceAccessWriteEvent("set", store.storeKey, key, value) } // Has implements types.KVStore. func (store *Store) Has(key []byte) bool { value := store.Get(key) + store.eventManager.EmitResourceAccessReadEvent("has", store.storeKey, key, value) return value != nil } @@ -189,7 +194,7 @@ func (store *Store) iterator(start, end []byte, ascending bool) types.Iterator { }() store.dirtyItems(start, end) cache = newMemIterator(start, end, store.sortedCache, store.deleted, ascending, store.eventManager, store.storeKey) - return NewCacheMergeIterator(parent, cache, ascending, store.storeKey) + return NewCacheMergeIterator(parent, cache, ascending, store.storeKey, store.eventManager) } func (store *Store) VersionExists(version int64) bool {