Skip to content

Commit

Permalink
mv log gas to base
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Oct 25, 2023
1 parent 707a600 commit a9d070e
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 104 deletions.
42 changes: 14 additions & 28 deletions x/cronos/keeper/precompiles/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,46 +61,32 @@ func EVMDenom(token common.Address) string {
}

type BankContract struct {
bankKeeper types.BankKeeper
cdc codec.Codec
kvGasConfig storetypes.GasConfig
logger log.Logger
BaseContract

bankKeeper types.BankKeeper
cdc codec.Codec
}

// NewBankContract creates the precompiled contract to manage native tokens
func NewBankContract(bankKeeper types.BankKeeper, cdc codec.Codec, kvGasConfig storetypes.GasConfig, logger log.Logger) vm.PrecompiledContract {
return &BankContract{
bankKeeper: bankKeeper,
cdc: cdc,
kvGasConfig: kvGasConfig,
logger: logger.With("precompiles", "bank"),
BaseContract: NewBaseContract(
bankContractAddress,
kvGasConfig,
bankMethodMap,
bankGasRequiredByMethod,
false,
logger.With("precompiles", "bank"),
),
cdc: cdc,
bankKeeper: bankKeeper,
}
}

func (bc *BankContract) Address() common.Address {
return bankContractAddress
}

// RequiredGas calculates the contract gas use
func (bc *BankContract) RequiredGas(input []byte) (gas uint64) {
method := ""
inputLen := 0
defer func() {
bc.logger.Info("required", "gas", gas, "method", method, "len", inputLen)
}()
// base cost to prevent large input size
inputLen = len(input)
baseCost := uint64(inputLen) * bc.kvGasConfig.WriteCostPerByte
var methodID [4]byte
copy(methodID[:], input[:4])
method = bankMethodMap[methodID]
requiredGas, ok := bankGasRequiredByMethod[methodID]
if ok {
return requiredGas + baseCost
}
return baseCost
}

func (bc *BankContract) IsStateful() bool {
return true
}
Expand Down
46 changes: 43 additions & 3 deletions x/cronos/keeper/precompiles/base_contract.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package precompiles

import (
"github.com/cometbft/cometbft/libs/log"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/ethereum/go-ethereum/common"
)

Expand All @@ -10,18 +12,56 @@ type Registrable interface {

type BaseContract interface {
Registrable
RequiredGas(input []byte) uint64
}

type baseContract struct {
address common.Address
address common.Address
kvGasConfig storetypes.GasConfig
nameByMethod map[[4]byte]string
gasByMethod map[[4]byte]uint64
emptyGasIfInputLessThanPrefix bool
logger log.Logger
}

func NewBaseContract(address common.Address) BaseContract {
func NewBaseContract(
address common.Address,
kvGasConfig storetypes.GasConfig,
nameByMethod map[[4]byte]string,
gasByMethod map[[4]byte]uint64,
emptyGasIfInputLessThanPrefix bool,
logger log.Logger,
) BaseContract {
return &baseContract{
address: address,
address,
kvGasConfig,
nameByMethod,
gasByMethod,
emptyGasIfInputLessThanPrefix,
logger,
}
}

func (c *baseContract) RegistryKey() common.Address {
return c.address
}

// RequiredGas calculates the contract gas use
func (c *baseContract) RequiredGas(input []byte) (gas uint64) {
var methodID [4]byte
copy(methodID[:], input[:4])
inputLen := len(input)
defer func() {
method := c.nameByMethod[methodID]
c.logger.Info("required", "gas", gas, "method", method, "len", inputLen)
}()
if c.emptyGasIfInputLessThanPrefix && inputLen < 4 {
return
}
// base cost to prevent large input size
gas = uint64(inputLen) * c.kvGasConfig.WriteCostPerByte

Check failure

Code scanning / gosec

Potential integer overflow by integer type conversion Error

Potential integer overflow by integer type conversion
if requiredGas, ok := c.gasByMethod[methodID]; ok {
gas += requiredGas
}
return
}
33 changes: 8 additions & 25 deletions x/cronos/keeper/precompiles/ica.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ type IcaContract struct {
cdc codec.Codec
icaauthKeeper types.Icaauthkeeper
cronosKeeper types.CronosKeeper
kvGasConfig storetypes.GasConfig
logger log.Logger
}

func NewIcaContract(
Expand All @@ -85,39 +83,24 @@ func NewIcaContract(
logger log.Logger,
) vm.PrecompiledContract {
return &IcaContract{
BaseContract: NewBaseContract(icaContractAddress),
BaseContract: NewBaseContract(
icaContractAddress,
kvGasConfig,
icaMethodMap,
icaGasRequiredByMethod,
false,
logger.With("precompiles", "ica"),
),
cdc: cdc,
icaauthKeeper: icaauthKeeper,
cronosKeeper: cronosKeeper,
kvGasConfig: kvGasConfig,
logger: logger.With("precompiles", "ica"),
}
}

func (ic *IcaContract) Address() common.Address {
return icaContractAddress
}

// RequiredGas calculates the contract gas use
func (ic *IcaContract) RequiredGas(input []byte) (gas uint64) {
method := ""
inputLen := 0
defer func() {
ic.logger.Info("required", "gas", gas, "method", method, "len", inputLen)
}()
// base cost to prevent large input size
inputLen = len(input)
baseCost := uint64(inputLen) * ic.kvGasConfig.WriteCostPerByte
var methodID [4]byte
copy(methodID[:], input[:4])
method = icaMethodMap[methodID]
requiredGas, ok := icaGasRequiredByMethod[methodID]
if ok {
return requiredGas + baseCost
}
return baseCost
}

func (ic *IcaContract) IsStateful() bool {
return true
}
Expand Down
94 changes: 46 additions & 48 deletions x/cronos/keeper/precompiles/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package precompiles
import (
"encoding/binary"
"errors"
"fmt"

storetypes "github.com/cosmos/cosmos-sdk/store/types"

Expand All @@ -17,72 +18,69 @@ import (

var (
relayerContractAddress = common.BytesToAddress([]byte{101})
relayerGasRequiredByMethod = map[int]uint64{}
relayerGasRequiredByMethod = map[[4]byte]uint64{}
relayerMethodMap = map[[4]byte]string{}
)

func assignMethodGas(prefix int, gas uint64) {
data := make([]byte, 4)
binary.LittleEndian.PutUint32(data, uint32(prefix))

Check failure

Code scanning / gosec

Potential integer overflow by integer type conversion Error

Potential integer overflow by integer type conversion
var id [4]byte
copy(id[:], data[:4])
relayerMethodMap[id] = fmt.Sprintf("%d", prefix)
relayerGasRequiredByMethod[id] = gas
}

func init() {
relayerGasRequiredByMethod[prefixCreateClient] = 200000
relayerGasRequiredByMethod[prefixUpdateClient] = 400000
relayerGasRequiredByMethod[prefixUpgradeClient] = 400000
relayerGasRequiredByMethod[prefixSubmitMisbehaviour] = 100000
relayerGasRequiredByMethod[prefixConnectionOpenInit] = 100000
relayerGasRequiredByMethod[prefixConnectionOpenTry] = 100000
relayerGasRequiredByMethod[prefixConnectionOpenAck] = 100000
relayerGasRequiredByMethod[prefixConnectionOpenConfirm] = 100000
relayerGasRequiredByMethod[prefixChannelOpenInit] = 100000
relayerGasRequiredByMethod[prefixChannelOpenTry] = 100000
relayerGasRequiredByMethod[prefixChannelOpenAck] = 100000
relayerGasRequiredByMethod[prefixChannelOpenConfirm] = 100000
relayerGasRequiredByMethod[prefixRecvPacket] = 250000
relayerGasRequiredByMethod[prefixAcknowledgement] = 250000
relayerGasRequiredByMethod[prefixTimeout] = 100000
relayerGasRequiredByMethod[prefixTimeoutOnClose] = 100000
assignMethodGas(prefixCreateClient, 200000)
assignMethodGas(prefixUpdateClient, 400000)
assignMethodGas(prefixUpgradeClient, 400000)
assignMethodGas(prefixSubmitMisbehaviour, 100000)
assignMethodGas(prefixConnectionOpenInit, 100000)
assignMethodGas(prefixConnectionOpenTry, 100000)
assignMethodGas(prefixConnectionOpenAck, 100000)
assignMethodGas(prefixConnectionOpenConfirm, 100000)
assignMethodGas(prefixChannelOpenInit, 100000)
assignMethodGas(prefixChannelOpenTry, 100000)
assignMethodGas(prefixChannelOpenAck, 100000)
assignMethodGas(prefixChannelOpenConfirm, 100000)
assignMethodGas(prefixRecvPacket, 250000)
assignMethodGas(prefixAcknowledgement, 250000)
assignMethodGas(prefixTimeout, 100000)
assignMethodGas(prefixTimeoutOnClose, 100000)
}

type RelayerContract struct {
BaseContract

cdc codec.Codec
ibcKeeper *ibckeeper.Keeper
kvGasConfig storetypes.GasConfig
logger log.Logger
cdc codec.Codec
ibcKeeper *ibckeeper.Keeper
}

func NewRelayerContract(ibcKeeper *ibckeeper.Keeper, cdc codec.Codec, kvGasConfig storetypes.GasConfig, logger log.Logger) vm.PrecompiledContract {
func NewRelayerContract(
ibcKeeper *ibckeeper.Keeper,
cdc codec.Codec,
kvGasConfig storetypes.GasConfig,
logger log.Logger,
) vm.PrecompiledContract {
return &RelayerContract{
BaseContract: NewBaseContract(relayerContractAddress),
ibcKeeper: ibcKeeper,
cdc: cdc,
kvGasConfig: kvGasConfig,
logger: logger.With("precompiles", "relayer"),
BaseContract: NewBaseContract(
relayerContractAddress,
kvGasConfig,
relayerMethodMap,
relayerGasRequiredByMethod,
true,
logger.With("precompiles", "relayer"),
),
ibcKeeper: ibcKeeper,
cdc: cdc,
}
}

func (bc *RelayerContract) Address() common.Address {
return relayerContractAddress
}

// RequiredGas calculates the contract gas use
func (bc *RelayerContract) RequiredGas(input []byte) (gas uint64) {
prefix := 0
inputLen := 0
defer func() {
bc.logger.Info("required", "gas", gas, "prefix", prefix, "len", inputLen)
}()
// base cost to prevent large input size
inputLen = len(input)
baseCost := uint64(inputLen) * bc.kvGasConfig.WriteCostPerByte
if len(input) < prefixSize4Bytes {
return
}
prefix = int(binary.LittleEndian.Uint32(input[:prefixSize4Bytes]))
requiredGas, ok := relayerGasRequiredByMethod[prefix]
if ok {
return requiredGas + baseCost
}
return baseCost
}

func (bc *RelayerContract) IsStateful() bool {
return true
}
Expand Down

0 comments on commit a9d070e

Please sign in to comment.