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: unnecessary GetBalance in ante handlers #514

Closed
wants to merge 2 commits into from

Conversation

mmsqe
Copy link
Collaborator

@mmsqe mmsqe commented Aug 13, 2024

reuse balance after verify account balance to avoid get again when check can transfer

benchmark need remove the skip part

pkg: github.com/crypto-org-chain/cronos/v2/app
                                │  before.txt  │             after.txt             │
                                │    sec/op    │   sec/op     vs base              │
ERC20Transfer/memiavl-stm-1-12    870.5m ±  7%   866.1m ± 1%       ~ (p=0.699 n=6)
ERC20Transfer/memiavl-stm-8-12    180.7m ±  4%   174.9m ± 4%       ~ (p=0.065 n=6)
ERC20Transfer/memiavl-stm-16-12   205.6m ± 19%   198.5m ± 4%       ~ (p=0.180 n=6)
ERC20Transfer/memiavl-stm-32-12   243.7m ± 10%   247.1m ± 8%       ~ (p=0.818 n=6)
geomean                           297.9m         293.6m       -1.46%

                                │  before.txt  │             after.txt              │
                                │     B/op     │     B/op      vs base              │
ERC20Transfer/memiavl-stm-1-12    475.9Mi ± 0%   482.4Mi ± 0%  +1.36% (p=0.002 n=6)
ERC20Transfer/memiavl-stm-8-12    484.9Mi ± 0%   491.7Mi ± 0%  +1.39% (p=0.002 n=6)
ERC20Transfer/memiavl-stm-16-12   498.6Mi ± 1%   505.4Mi ± 0%  +1.36% (p=0.002 n=6)
ERC20Transfer/memiavl-stm-32-12   523.4Mi ± 1%   532.6Mi ± 1%  +1.77% (p=0.002 n=6)
geomean                           495.4Mi        502.7Mi       +1.47%

                                │ before.txt  │             after.txt             │
                                │  allocs/op  │  allocs/op   vs base              │
ERC20Transfer/memiavl-stm-1-12    5.998M ± 0%   6.023M ± 0%  +0.42% (p=0.002 n=6)
ERC20Transfer/memiavl-stm-8-12    6.127M ± 0%   6.153M ± 0%  +0.42% (p=0.002 n=6)
ERC20Transfer/memiavl-stm-16-12   6.347M ± 1%   6.365M ± 0%  +0.28% (p=0.041 n=6)
ERC20Transfer/memiavl-stm-32-12   6.758M ± 2%   6.801M ± 2%       ~ (p=0.240 n=6)
geomean                           6.301M        6.329M       +0.44%

Description


For contributor use:

  • Targeted PR against correct branch (see CONTRIBUTING.md)
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
  • Code follows the module structure standards.
  • Wrote unit and integration tests
  • Updated relevant documentation (docs/) or specification (x/<module>/spec/)
  • Added relevant godoc comments.
  • Added a relevant changelog entry to the Unreleased section in CHANGELOG.md
  • Re-reviewed Files changed in the Github PR explorer

For admin use:

  • Added appropriate labels to PR (ex. WIP, R4R, docs, etc)
  • Reviewers assigned
  • Squashed all commits, uses message "Merge pull request #XYZ: [title]" (coding standards)

Copy link

codecov bot commented Aug 13, 2024

Codecov Report

Attention: Patch coverage is 90.90909% with 2 lines in your changes missing coverage. Please review.

Project coverage is 61.60%. Comparing base (fe3f4fd) to head (8f89bb1).

Files Patch % Lines
app/ante/handler_options.go 33.33% 0 Missing and 2 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #514      +/-   ##
===========================================
+ Coverage    61.57%   61.60%   +0.03%     
===========================================
  Files          128      128              
  Lines         9512     9520       +8     
===========================================
+ Hits          5857     5865       +8     
  Misses        3115     3115              
  Partials       540      540              
Files Coverage Δ
app/ante/eth.go 80.43% <100.00%> (+1.04%) ⬆️
app/ante/handler_options.go 61.95% <33.33%> (+0.41%) ⬆️

reuse balance after verify account balance to avoid get again when check can transfer
@mmsqe mmsqe changed the title Problem: CheckEthGasConsume is hard to maintain Problem: unnecessary GetBalance in ante handlers Aug 16, 2024
@mmsqe mmsqe requested a review from yihuang August 16, 2024 01:51
// canTransfer adapted the core.CanTransfer from go-ethereum
func canTransfer(ctx sdk.Context, evmKeeper EVMKeeper, denom string, from common.Address, amount *big.Int) bool {
balance := evmKeeper.GetBalance(ctx, sdk.AccAddress(from.Bytes()), denom)
return balance.Cmp(amount) >= 0
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

balance could be nil, but there was no nil check before

Copy link
Collaborator

Choose a reason for hiding this comment

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

try to reproduce in integration test?

@yihuang
Copy link
Collaborator

yihuang commented Aug 16, 2024

again, how does this affect the benchmark result, I guess the effect of this is even smaller than the account PR, which is almost unnoticeable in the benchmark result.

@mmsqe
Copy link
Collaborator Author

mmsqe commented Aug 16, 2024

again, how does this affect the benchmark result, I guess the effect of this is even smaller than the account PR, which is almost unnoticeable in the benchmark result.

Do you mean GetBalance is faster than GetAccount, no need to cache?

@yihuang
Copy link
Collaborator

yihuang commented Aug 16, 2024

again, how does this affect the benchmark result, I guess the effect of this is even smaller than the account PR, which is almost unnoticeable in the benchmark result.

Do you mean GetBalance is faster than GetAccount, no need to cache?

what do you think we are caching here? the raw bytes fetched from db is already cached by cachekv store, so what we are caching here is just to avoid the decoding step, if the decoding speed is similar to or even faster than the added map operations, then there's no point to add this cache, right?

@mmsqe mmsqe closed this Aug 16, 2024
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.

2 participants