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

Problem: no required gas log in precompiles #1223

Closed
wants to merge 6 commits into from

Conversation

mmsqe
Copy link
Collaborator

@mmsqe mmsqe commented Oct 25, 2023

👮🏻👮🏻👮🏻 !!!! REFERENCE THE PROBLEM YOUR ARE SOLVING IN THE PR TITLE AND DESCRIBE YOUR SOLUTION HERE !!!! DO NOT FORGET !!!! 👮🏻👮🏻👮🏻

PR Checklist:

  • Have you read the CONTRIBUTING.md?
  • Does your PR follow the C4 patch requirements?
  • Have you rebased your work on top of the latest master?
  • Have you checked your code compiles? (make)
  • Have you included tests for any non-trivial functionality?
  • Have you checked your code passes the unit tests? (make test)
  • Have you checked your code formatting is correct? (go fmt)
  • Have you checked your basic code style is fine? (golangci-lint run)
  • If you added any dependencies, have you checked they do not contain any known vulnerabilities? (go list -json -m all | nancy sleuth)
  • If your changes affect the client infrastructure, have you run the integration test?
  • If your changes affect public APIs, does your PR follow the C4 evolution of public contracts?
  • If your code changes public APIs, have you incremented the crate version numbers and documented your changes in the CHANGELOG.md?
  • If you are contributing for the first time, please read the agreement in CONTRIBUTING.md now and add a comment to this pull request stating that your PR is in accordance with the Developer's Certificate of Origin.

Thank you for your code, it's appreciated! :)

Summary by CodeRabbit

Refactor

  • Updated the baseContract structure and its associated methods for better configurability and functionality, including a new method to calculate contract gas usage.
  • Removed the RequiredGas function from the BankContract, IcaContract, and RelayerContract, simplifying the gas calculation process.

New Feature

  • Enhanced logging functionality for the Cronos and ICA contracts, providing more transparency and traceability.
  • Introduced method mapping for the bank and ICA contracts, improving the readability and management of contract methods.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 25, 2023

Walkthrough

The changes primarily focus on enhancing the logging functionality and gas calculation in the precompiled contracts of the Cronos application. The baseContract struct and its associated methods have been significantly updated to accommodate these changes.

Changes

File(s) Summary
CHANGELOG.md Added a required gas log in precompiles.
app/app.go Introduced logging functionality to the cronosprecompiles.NewRelayerContract and cronosprecompiles.NewIcaContract function calls.
x/cronos/keeper/precompiles/bank.go, x/cronos/keeper/precompiles/ica.go, x/cronos/keeper/precompiles/relayer.go Added logging functionality, updated method maps, and removed the RequiredGas function.
x/cronos/keeper/precompiles/base_contract.go Enhanced the baseContract struct and its associated methods to include additional fields and functionality for gas calculation.

"In the land of code, where logic is king, 🤴
A rabbit hopped by, making changes with a zing. 🐇💥
With a logger in hand, and gas calculations anew, 📝⛽
He enhanced the contracts, making them better than a few. 📜✨
So, let's celebrate these changes, for they are quite grand, 🎉
In the world of software, it's another step forward in this vast land!" 🌍🚀


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai help to get help.
  • @coderabbitai resolve to resolve all the CodeRabbit review comments.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CHANGELOG.md Outdated Show resolved Hide resolved
Signed-off-by: mmsqe <[email protected]>
)

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
x/cronos/keeper/precompiles/base_contract.go Fixed Show fixed Hide fixed
@mmsqe mmsqe marked this pull request as ready for review October 25, 2023 01:59
@mmsqe mmsqe requested a review from a team as a code owner October 25, 2023 01:59
@mmsqe mmsqe requested review from yihuang and devashishdxt and removed request for a team October 25, 2023 01:59
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 29ee38e and 41f3ebc.
Files selected for processing (6)
  • CHANGELOG.md (1 hunks)
  • app/app.go (1 hunks)
  • x/cronos/keeper/precompiles/bank.go (4 hunks)
  • x/cronos/keeper/precompiles/base_contract.go (2 hunks)
  • x/cronos/keeper/precompiles/ica.go (4 hunks)
  • x/cronos/keeper/precompiles/relayer.go (1 hunks)
Additional comments: 19
CHANGELOG.md (1)
  • 10-13: The new hunk in the CHANGELOG.md file provides an update on the improvements made in the project. It mentions the addition of a required gas log in precompiles, which is linked to PR #1223. This change is consistent with the summary provided for the pull request. No issues or concerns are identified in this hunk.
app/app.go (1)
  • 556-557: The NewRelayerContract and NewIcaContract functions now include an additional app.Logger() parameter. Ensure that the logger is properly initialized and that it doesn't introduce any potential data races when used concurrently. Also, verify that the logger is used appropriately within these functions to log relevant information.
x/cronos/keeper/precompiles/base_contract.go (5)
  • 4-5: The new imports github.com/cometbft/cometbft/libs/log and github.com/cosmos/cosmos-sdk/store/types are introduced to support the logging functionality and to use the GasConfig type respectively. Ensure that these packages are included in your dependencies.

  • 15-15: The RequiredGas method is added to the BaseContract interface. This method is expected to calculate the gas required for a contract based on the input. Make sure all implementations of BaseContract implement this method.

  • 18-24: The baseContract struct is enhanced with additional fields for kvGasConfig, nameByMethod, gasByMethod, emptyGasIfInputLessThanPrefix, and logger. These fields are used to configure the gas usage and logging for the contract. Ensure that these fields are properly initialized and used in the contract's methods.

  • 27-43: The NewBaseContract function now takes additional parameters to initialize the new fields in the baseContract struct. Ensure that all calls to NewBaseContract are updated to match the new function signature.

  • 49-67: The RequiredGas method is implemented for the baseContract struct. This method calculates the gas required for a contract based on the input. It also logs the method name, input length, and required gas. Ensure that this method is correctly calculating the gas and logging the information.

x/cronos/keeper/precompiles/relayer.go (6)
  • 6-6: The fmt package is imported but not used in the new hunks. Please verify if it's used in the rest of the file. If not, consider removing it to avoid unnecessary imports.

  • 25-32: The assignMethodGas function is a new addition that helps to assign gas values to methods in a more structured way. It also maps method prefixes to their string representations. This is a good practice as it improves code readability and maintainability.

  • 34-51: The init function has been updated to use the new assignMethodGas function. This is a good practice as it improves code readability and maintainability.

  • 60-78: The NewRelayerContract function has been updated to include a logger parameter and to initialize the BaseContract with additional parameters. This is a good practice as it improves the functionality and configurability of the contract.

  • 80-82: The Address function is unchanged and continues to return the relayerContractAddress. This is consistent with the previous implementation.

  • 83-86: The RequiredGas function has been removed from the RelayerContract struct. This is consistent with the changes in base_contract.go where the RequiredGas method has been added to the BaseContract struct. This is a good practice as it reduces code duplication and improves maintainability.

x/cronos/keeper/precompiles/bank.go (3)
  • 7-7: The new hunk imports the log package from github.com/cometbft/cometbft/libs/log. This is likely to be used for logging purposes in the code.

  • 35-35: A new global variable bankMethodMap of type map[[4]byte]string{} is introduced. This map will likely be used to map method IDs to their corresponding method names.

  • 55-55: In the init() function, the bankMethodMap is populated with method IDs as keys and method names as values. This will allow for easy retrieval of method names using their IDs.

x/cronos/keeper/precompiles/ica.go (3)
  • 9-9: The new hunk imports the log package from github.com/cometbft/cometbft/libs/log. This package is used to provide logging functionality to the IcaContract.

  • 38-38: The new hunk introduces a global variable icaMethodMap of type map[[4]byte]string{}. This map is used to store the mapping between method IDs and method names.

  • 62-62: The new hunk adds the method name to the icaMethodMap in the init function. This ensures that each method ID is associated with its corresponding method name.

Comment on lines 64 to 83
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) vm.PrecompiledContract {
return &BankContract{bankKeeper, cdc, kvGasConfig}
func NewBankContract(bankKeeper types.BankKeeper, cdc codec.Codec, kvGasConfig storetypes.GasConfig, logger log.Logger) vm.PrecompiledContract {
return &BankContract{
BaseContract: NewBaseContract(
bankContractAddress,
kvGasConfig,
bankMethodMap,
bankGasRequiredByMethod,
false,
logger.With("precompiles", "bank"),
),
cdc: cdc,
bankKeeper: bankKeeper,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The BankContract struct is updated to embed the BaseContract struct. The kvGasConfig field is removed from BankContract as it is now part of BaseContract. The NewBankContract function is updated to initialize the BaseContract with additional parameters including the contract address, gas configuration, method map, gas required by method, a boolean indicating if the contract is stateless, and a logger with a specific context. This change enhances the modularity and maintainability of the code by moving common functionality to the BaseContract.

Comment on lines 86 to 89
func (bc *BankContract) Address() common.Address {
return bankContractAddress
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The RequiredGas method is removed from the BankContract struct. This is likely because the method is now part of the BaseContract and can be accessed through it. This change reduces code duplication and enhances maintainability.

Comment on lines 78 to 98
func NewIcaContract(
icaauthKeeper types.Icaauthkeeper,
cronosKeeper types.CronosKeeper,
cdc codec.Codec,
kvGasConfig storetypes.GasConfig,
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,
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The NewIcaContract function signature has been updated to include two new parameters: kvGasConfig of type storetypes.GasConfig and logger of type log.Logger. The kvGasConfig parameter is used to configure the gas usage for key-value store operations, while the logger parameter is used for logging. The kvGasConfig field has been removed from the IcaContract struct, and the logger field has been added to the BaseContract struct. The RequiredGas function has been removed from the IcaContract and moved to the BaseContract. These changes are part of the effort to enhance the functionality and configurability of the contracts.

- func NewIcaContract(icaauthKeeper types.Icaauthkeeper, cronosKeeper types.CronosKeeper, cdc codec.Codec, kvGasConfig storetypes.GasConfig) vm.PrecompiledContract {
+ func NewIcaContract(icaauthKeeper types.Icaauthkeeper, cronosKeeper types.CronosKeeper, cdc codec.Codec, kvGasConfig storetypes.GasConfig, 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,
  	}
  }
Committable suggestion (Beta)
Suggested change
func NewIcaContract(
icaauthKeeper types.Icaauthkeeper,
cronosKeeper types.CronosKeeper,
cdc codec.Codec,
kvGasConfig storetypes.GasConfig,
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,
}
}
func NewIcaContract(
icaauthKeeper types.Icaauthkeeper,
cronosKeeper types.CronosKeeper,
cdc codec.Codec,
kvGasConfig storetypes.GasConfig,
logger log.Logger,
) vm.PrecompiledContract {
return &IcaContract{
BaseContract: NewBaseContract(
icaContractAddress,
kvGasConfig,
icaMethodMap,
icaGasRequiredByMethod,
false,
logger.With("precompiles", "ica"),
),
cdc: cdc,
icaauthKeeper: icaauthKeeper,
cronosKeeper: cronosKeeper,
}
}

@thomas-nguy thomas-nguy changed the title Problem: no require gas log in precompiles Problem: no required gas log in precompiles Oct 25, 2023
@codecov
Copy link

codecov bot commented Oct 25, 2023

Codecov Report

Merging #1223 (74860a9) into main (376da21) will increase coverage by 11.23%.
The diff coverage is n/a.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##             main    #1223       +/-   ##
===========================================
+ Coverage   36.89%   48.13%   +11.23%     
===========================================
  Files         115       74       -41     
  Lines       10255     6197     -4058     
===========================================
- Hits         3784     2983      -801     
+ Misses       6097     2896     -3201     
+ Partials      374      318       -56     

see 52 files with indirect coverage changes

inputLen := len(input)
defer func() {
method := c.nameByMethod[methodID]
c.logger.Info("required", "gas", gas, "method", method, "len", inputLen)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's this for, temporary debugging?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we want to record required gas for each method

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for debugging or what reason?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes debugging, we could collect the data on some nodes to tune the requiredgas map later on

but the data has to be the gas consumed at cosmos level during execution, not the value returned from the map

Copy link
Collaborator

@yihuang yihuang Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, so this could be a draft PR for building a local binary, no need to merge, right?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could use those data on mainnet
https://cronos.yummy.capital/txs/E2B835B672BC046028E3FEBEB1770CD8A506DB505380982AD16C39F13E5DD861

Somehow hermes in mainnet is spending more gas than that. But may be also because of hermez config?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should have bigger client header like more validators, and I'm not sure why we use TransientGasConfig

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the TransientGasConfig, it inspired from evmos implementation of the gasRequired
https://github.com/evmos/evmos/blob/main/precompiles/common/precompile.go#L34

They also use the TransientGasConfig, I think that make sense since the goal for us is just to add an "extra" fee based on the length of the input, to avoid DDOS attack for very large input

But in practice, the additional fee does not make much sense since input length is not related to the cost of operation in memory

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think evmos use KvGasConfig instead and input length does related to TxSizeCost which is the major cost in cosmos tx

Copy link
Collaborator

@thomas-nguy thomas-nguy Nov 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmm in the original context, the base cost is just an extra cost to avoid DDOS attack, since the input it never written in store, it would make sense to use TransientGasConfig for the computation.

But I think what you are trying to do is to replicate the cosmos intrinsic cost? In that case KvGasConfig is used

@mmsqe mmsqe marked this pull request as draft October 25, 2023 07:10
@mmsqe mmsqe closed this Oct 31, 2023
@mmsqe mmsqe reopened this Nov 7, 2023
return
}
// base cost to prevent large input size
gas = uint64(inputLen) * c.writeCostPerByte

Check failure

Code scanning / gosec

Potential integer overflow by integer type conversion Error

Potential integer overflow by integer type conversion
@mmsqe mmsqe marked this pull request as ready for review November 7, 2023 01:44
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 41f3ebc and 74860a9.
Files selected for processing (6)
  • CHANGELOG.md (1 hunks)
  • app/app.go (1 hunks)
  • x/cronos/keeper/precompiles/bank.go (4 hunks)
  • x/cronos/keeper/precompiles/base_contract.go (2 hunks)
  • x/cronos/keeper/precompiles/ica.go (4 hunks)
  • x/cronos/keeper/precompiles/relayer.go (4 hunks)
Additional comments: 18
CHANGELOG.md (1)
  • 18-21: The change log update is clear and concise, providing a link to the relevant pull request for more context. Ensure that the version number and date are updated appropriately when the changes are released.
app/app.go (1)
  • 539-540: Ensure that all calls to NewRelayerContract and NewIcaContract throughout the codebase have been updated to match the new signature.
- cronosprecompiles.NewRelayerContract(app.IBCKeeper, appCodec, app.Logger()),
- cronosprecompiles.NewIcaContract(&app.ICAAuthKeeper, &app.CronosKeeper, appCodec, gasConfig, app.Logger()),
+ cronosprecompiles.NewRelayerContract(app.IBCKeeper, appCodec, app.Logger()),
+ cronosprecompiles.NewIcaContract(&app.ICAAuthKeeper, &app.CronosKeeper, appCodec, gasConfig, app.Logger()),
x/cronos/keeper/precompiles/base_contract.go (4)
  • 11-15: The BaseContract interface has been updated to include the RequiredGas method. Ensure that all implementations of this interface have been updated accordingly.

  • 17-24: The baseContract struct has been updated with new fields. Ensure that these fields are being used appropriately throughout the codebase.

  • 26-42: The NewBaseContract function has been updated to accept new parameters. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 48-66: The RequiredGas method has been added to calculate the contract gas use. Ensure that this method is being called and used correctly throughout the codebase.

x/cronos/keeper/precompiles/relayer.go (4)
  • 13-19: The import section is well organized and follows the best practices of separating standard library imports from third-party imports.

  • 36-55: The init function is correctly used to initialize the relayerMethodMap and relayerGasRequiredByMethod maps. It calls the assignMethodGas function with the correct parameters.

  • 65-84: The NewRelayerContract function has been updated to accept an additional ibcKeeper *ibckeeper.Keeper parameter. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 90-105: The RequiredGas method now calls the BaseContract.RequiredGas method and logs additional information. This is a good practice as it enhances the debuggability of the code.

x/cronos/keeper/precompiles/bank.go (4)
  • 4-10: The new import github.com/cometbft/cometbft/libs/log is added to support logging functionality. Ensure that the package is available and accessible.

  • 32-36: The bankMethodMap is introduced and initialized with method IDs and names. This map will be used to map method IDs to their corresponding names.

  • 52-56: The bankMethodMap is populated with method IDs and their corresponding names in the init function. This is a good practice as it ensures that the map is populated before it is used anywhere in the code.

  • 61-92: The NewBankContract function signature is modified to include a logger parameter. The function is also updated to initialize BaseContract and set cdc and bankKeeper fields. The RequiredGas function is removed, and the IsStateful function is modified to return true. Ensure that all calls to NewBankContract throughout the codebase have been updated to match the new signature.

- func NewBankContract(bankKeeper types.BankKeeper, cdc codec.Codec) vm.PrecompiledContract {
+ func NewBankContract(bankKeeper types.BankKeeper, cdc codec.Codec, kvGasConfig storetypes.GasConfig, logger log.Logger) vm.PrecompiledContract {
x/cronos/keeper/precompiles/ica.go (4)
  • 6-12: The new import statement for the "github.com/cometbft/cometbft/libs/log" package is added correctly.

  • 35-39: The new global variable "icaMethodMap" is added correctly. Ensure that it is used appropriately throughout the codebase.

  • 78-98: The "NewIcaContract" function signature is updated to include a "logger" parameter. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

- func NewIcaContract(icaauthKeeper types.Icaauthkeeper, cronosKeeper types.CronosKeeper, cdc codec.Codec, kvGasConfig storetypes.GasConfig) vm.PrecompiledContract {
+ func NewIcaContract(icaauthKeeper types.Icaauthkeeper, cronosKeeper types.CronosKeeper, cdc codec.Codec, kvGasConfig storetypes.GasConfig, logger log.Logger) vm.PrecompiledContract {
  • 86-93: The "BaseContract" initialization within the "NewIcaContract" function is modified correctly to include the new "logger" parameter.
- BaseContract: NewBaseContract(icaContractAddress, kvGasConfig.WriteCostPerByte, icaMethodMap, icaGasRequiredByMethod, false),
+ BaseContract: NewBaseContract(icaContractAddress, kvGasConfig.WriteCostPerByte, icaMethodMap, icaGasRequiredByMethod, false, logger.With("precompiles", "ica")),

return
}
// base cost to prevent large input size
gas = uint64(inputLen) * c.writeCostPerByte
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential integer overflow by integer type conversion. Consider adding a check to prevent overflow.

+ if inputLen > math.MaxUint64/c.writeCostPerByte {
+     // handle the error, possibly return an error
+ }
  gas = uint64(inputLen) * c.writeCostPerByte

Commitable suggestion

[!IMPORTANT]
Carefully review the code before committing. Make sure it correctly replaces the highlighted code, has no missing lines and indentaion issues.

Suggested change
gas = uint64(inputLen) * c.writeCostPerByte
if inputLen > math.MaxUint64/c.writeCostPerByte:
# handle the error, possibly return an error
gas = uint64(inputLen) * c.writeCostPerByte

Comment on lines +27 to +34
func assignMethodGas(prefix int, gas uint64) {
data := make([]byte, 4)
binary.LittleEndian.PutUint32(data, uint32(prefix))
var id [4]byte
copy(id[:], data[:4])
relayerMethodMap[id] = fmt.Sprintf("%d", prefix)
relayerGasRequiredByMethod[id] = gas
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assignMethodGas function is well implemented. It populates the relayerMethodMap and relayerGasRequiredByMethod maps with the correct values. However, there is a potential integer overflow issue on line 29 as pointed out by the bot in the previous comments. You should handle this potential overflow.

binary.LittleEndian.PutUint32(data, uint32(prefix))

@mmsqe mmsqe closed this Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants