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

feat(Gnovm/std): GasUsed function for std #2149

Closed

Conversation

thinhnx-var
Copy link
Contributor

@thinhnx-var thinhnx-var commented May 20, 2024

Contributors' checklist...
  • Added new tests, or not needed, or not feasible
  • Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory
  • Updated the official documentation or not needed
  • No breaking changes were made, or a BREAKING CHANGE: xxx message was included in the description
  • Added references to related issues and PRs
  • Provided any useful hints for running manual tests
  • Added new benchmarks to generated graphs, if any. More info here.

As discussion in #1998, We should have this gasUsed() std function with two potential use cases:

  1. This can be helpful for benchmarking gno code -- users trying to figure out which parts of their code is consuming large amounts of gas
  2. This can be used to measure performance of running a bit of code if someone would like to deploy a leetcode style realm that accepts interfaces with methods for solving a posed coding problem.

This PR defines a GasUsed() func and a defaultInvokeCost in gas (?) within std package, and whenever the GasUsed() is invoked, the GasMeter will consume this amount, returns the GasConsumedToLimit.

(?) What is a reasonable number for this amount? I just set it to 1000 (store.DefaultGasConfig().ReadCostFlat).
TODO: Should we move the default cost config into store.DefaultGasConfig ?

@thinhnx-var thinhnx-var requested a review from thehowl as a code owner May 20, 2024 08:30
@github-actions github-actions bot added the 📦 🤖 gnovm Issues or PRs gnovm related label May 20, 2024
@thinhnx-var
Copy link
Contributor Author

@thehowl can you take a look on this. I really need your opinion.

Copy link

codecov bot commented May 29, 2024

Codecov Report

Attention: Patch coverage is 23.07692% with 10 lines in your changes are missing coverage. Please review.

Project coverage is 49.93%. Comparing base (088eeca) to head (2af4c25).

Files Patch % Lines
gnovm/stdlibs/native.go 0.00% 10 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2149      +/-   ##
==========================================
- Coverage   49.93%   49.93%   -0.01%     
==========================================
  Files         576      577       +1     
  Lines       77828    77841      +13     
==========================================
+ Hits        38862    38868       +6     
- Misses      35840    35849       +9     
+ Partials     3126     3124       -2     
Flag Coverage Δ
gnovm 44.96% <23.07%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@thehowl thehowl left a comment

Choose a reason for hiding this comment

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

I'm unsure whether this makes sense in the first place. See my comment on #1998 .

@@ -13,6 +13,8 @@ func IsOriginCall() bool // injected
func GetChainID() string // injected
func GetHeight() int64 // injected

func GasUsed() int64 // injected
Copy link
Member

Choose a reason for hiding this comment

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

Please place this next to the other injected functions (and align // injected), please!

Copy link
Member

Choose a reason for hiding this comment

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

Name this gas.go

Comment on lines +12 to +26
var (
gasUsedInvoked = "GasUsedCalled"
/*
Consider where to save this config
defaultGasConfig = store.DefaultGasConfig()
*/
// defaultGasConfig = int64(1000)
defaultInvokeCost = store.DefaultGasConfig().ReadCostFlat
)

// DefaultCost will be consumed whenever GasUsed is called, now set it ReadCostPerByte
func GasUsed(m *gno.Machine) int64 {
m.GasMeter.ConsumeGas(defaultInvokeCost, gasUsedInvoked)
return m.GasMeter.GasConsumedToLimit()
}
Copy link
Member

Choose a reason for hiding this comment

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

Don't consume gas here; we'll need to benchmark all native functions and add a cost on another occasion.

@thehowl
Copy link
Member

thehowl commented Jun 18, 2024

Apologies for the late review

@Kouteki
Copy link
Contributor

Kouteki commented Jul 5, 2024

Fixes #2467

@thinhnx-var
Copy link
Contributor Author

Closed due to #2571

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 🤖 gnovm Issues or PRs gnovm related
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants