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: precompiles required-gas need adjustments #1178

Merged

Conversation

thomas-nguy
Copy link
Collaborator

@thomas-nguy thomas-nguy commented Sep 21, 2023

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

Using some base line value for estimation, may need adjustment in the future

Open for suggestions. Keeping this open as it is important to have this fixed before an official release.

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! :)

@thomas-nguy thomas-nguy requested a review from a team as a code owner September 21, 2023 01:17
@thomas-nguy thomas-nguy requested review from yihuang and mmsqe and removed request for a team September 21, 2023 01:17
@thomas-nguy thomas-nguy changed the title Problem: precompiles required gas need adjustments Problem: precompiles required-gas need adjustments Sep 21, 2023
@thomas-nguy thomas-nguy force-pushed the thomas/precompile-gas-cost branch 3 times, most recently from 6818d9a to 3f4ac9c Compare September 21, 2023 01:28
@codecov
Copy link

codecov bot commented Sep 21, 2023

Codecov Report

Merging #1178 (2e220af) into main (071bc21) will increase coverage by 20.71%.
The diff coverage is 51.76%.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##             main    #1178       +/-   ##
===========================================
+ Coverage   16.25%   36.97%   +20.71%     
===========================================
  Files          79      115       +36     
  Lines        5698    10235     +4537     
===========================================
+ Hits          926     3784     +2858     
- Misses       4694     6078     +1384     
- Partials       78      373      +295     
Files Coverage Δ
x/cronos/keeper/precompiles/relayer.go 25.80% <62.50%> (+16.57%) ⬆️
x/cronos/keeper/precompiles/ica.go 13.47% <46.15%> (+6.80%) ⬆️
x/cronos/keeper/precompiles/bank.go 9.35% <44.44%> (+6.85%) ⬆️

... and 54 files with indirect coverage changes

@thomas-nguy thomas-nguy force-pushed the thomas/precompile-gas-cost branch 7 times, most recently from 5c6c9a4 to c0459fb Compare September 25, 2023 05:47
@thomas-nguy thomas-nguy marked this pull request as draft September 25, 2023 05:55
@thomas-nguy thomas-nguy force-pushed the thomas/precompile-gas-cost branch 3 times, most recently from 083ab67 to c04a708 Compare September 26, 2023 05:15
@thomas-nguy thomas-nguy force-pushed the thomas/precompile-gas-cost branch from d3eacc5 to 408def7 Compare September 26, 2023 05:57
@calvinaco calvinaco mentioned this pull request Sep 26, 2023
13 tasks
@thomas-nguy thomas-nguy force-pushed the thomas/precompile-gas-cost branch from d5b8a37 to 4e44e7c Compare September 26, 2023 09:37
@JayT106
Copy link
Collaborator

JayT106 commented Sep 26, 2023

Not sure how the caller handles the return 0 from RequiredGas. Assume the caller called the contract function with no RequiredGas implementation. Should we return the max of uint64 instead of 0 for the error and the default cases?

Comment on lines 38 to 60
for methodName := range icaABI.Methods {
var methodID [4]byte
copy(methodID[:], icaABI.Methods[methodName].ID[:4])
switch methodName {
case RegisterAccountMethodName:
icaGasRequiredByMethod[methodID] = 10000
case QueryAccountMethodName:
icaGasRequiredByMethod[methodID] = 10000
case SubmitMsgsMethodName:
icaGasRequiredByMethod[methodID] = 10000
default:
icaGasRequiredByMethod[methodID] = 0
}
}

Check failure

Code scanning / gosec

the value in the range statement should be _ unless copying a map: want: for key := range m Error

expected exactly 1 statement (either append, delete, or copying to another map) in a range with a map, got 4
Comment on lines +38 to +53
for methodName := range bankABI.Methods {
var methodID [4]byte
copy(methodID[:], bankABI.Methods[methodName].ID[:4])
switch methodName {
case MintMethodName, BurnMethodName:
bankGasRequiredByMethod[methodID] = 200000
case BalanceOfMethodName:
bankGasRequiredByMethod[methodID] = 10000
case TransferMethodName:
bankGasRequiredByMethod[methodID] = 150000
default:
bankGasRequiredByMethod[methodID] = 0
}
}

Check failure

Code scanning / gosec

the value in the range statement should be _ unless copying a map: want: for key := range m Error

expected exactly 1 statement (either append, delete, or copying to another map) in a range with a map, got 3
Comment on lines +38 to +53
for methodName := range bankABI.Methods {
var methodID [4]byte
copy(methodID[:], bankABI.Methods[methodName].ID[:4])
switch methodName {
case MintMethodName, BurnMethodName:
bankGasRequiredByMethod[methodID] = 200000
case BalanceOfMethodName:
bankGasRequiredByMethod[methodID] = 10000
case TransferMethodName:
bankGasRequiredByMethod[methodID] = 150000
default:
bankGasRequiredByMethod[methodID] = 0
}
}

Check warning

Code scanning / CodeQL

Iteration over map Warning

Iteration over map may be a possible source of non-determinism
Comment on lines 38 to 60
for methodName := range icaABI.Methods {
var methodID [4]byte
copy(methodID[:], icaABI.Methods[methodName].ID[:4])
switch methodName {
case RegisterAccountMethodName:
icaGasRequiredByMethod[methodID] = 10000
case QueryAccountMethodName:
icaGasRequiredByMethod[methodID] = 10000
case SubmitMsgsMethodName:
icaGasRequiredByMethod[methodID] = 10000
default:
icaGasRequiredByMethod[methodID] = 0
}
}

Check warning

Code scanning / CodeQL

Iteration over map Warning

Iteration over map may be a possible source of non-determinism
@thomas-nguy thomas-nguy force-pushed the thomas/precompile-gas-cost branch 2 times, most recently from 079c936 to 4248963 Compare September 27, 2023 07:15
@thomas-nguy thomas-nguy marked this pull request as ready for review September 27, 2023 09:17
@thomas-nguy thomas-nguy force-pushed the thomas/precompile-gas-cost branch from 0982937 to f2ac99a Compare September 27, 2023 13:14
@JayT106
Copy link
Collaborator

JayT106 commented Sep 27, 2023

Not sure how the caller handles the return 0 from RequiredGas. Assume the caller called the contract function with no RequiredGas implementation. Should we return the max of uint64 instead of 0 for the error and the default cases?

During the execution, the handler would revert because of wrong method name. But it may also consume the gas. Lets say we set to max uint64. This may be a bit too punitive?

On the other hand, this could also be detectable it during the gas estimation phase if not skipped

So the handler would reverts and consumes some gas (like transaction base cost) but no addition cost for calling this pre-compiled contract, right?

Yes, the gas estimation can detect the error but cannot prevent attack. So my question is should we charge some gas for the contract (if we don't charge extra gas for entering the precompile contract, or we do?) if their is any executing errors or revert, perhaps can charge gas like half of the minimum gas cost of method in the contract?

@thomas-nguy thomas-nguy force-pushed the thomas/precompile-gas-cost branch from f2ac99a to e4e1083 Compare September 27, 2023 14:41
@thomas-nguy
Copy link
Collaborator Author

 perhaps can charge gas like half of the minimum gas cost of method in the contract?

Half the minimum of which method? In case method does not exists, there will be no execution on cosmos side. I think charging the base cost should be enough

@thomas-nguy thomas-nguy force-pushed the thomas/precompile-gas-cost branch from 68d8466 to 6ee2418 Compare September 28, 2023 06:03
@thomas-nguy thomas-nguy force-pushed the thomas/precompile-gas-cost branch from 6ee2418 to 51309ee Compare September 28, 2023 07:44
update integration tests config

add switch

fix lint

rename const

revert gas cost

estimate cost

convert to map

fix alert

fix golangci

update gas value based on estimation

add basecost to the precompile

increase gas in integration test
@thomas-nguy thomas-nguy force-pushed the thomas/precompile-gas-cost branch 3 times, most recently from 9d986ee to 9580897 Compare September 29, 2023 04:19
@thomas-nguy thomas-nguy force-pushed the thomas/precompile-gas-cost branch 4 times, most recently from 768d1fb to 47ba27e Compare September 29, 2023 09:12
@thomas-nguy thomas-nguy force-pushed the thomas/precompile-gas-cost branch from 47ba27e to caa5109 Compare September 29, 2023 10:11
@thomas-nguy thomas-nguy force-pushed the thomas/precompile-gas-cost branch from caa5109 to 2e220af Compare September 29, 2023 10:17
@thomas-nguy thomas-nguy added this pull request to the merge queue Sep 30, 2023
Merged via the queue into crypto-org-chain:main with commit 79c911c Sep 30, 2023
23 of 24 checks passed
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.

5 participants