Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Estimate Gas Usage #46

Open
5 tasks
behaviary opened this issue Mar 3, 2020 · 8 comments
Open
5 tasks

Estimate Gas Usage #46

behaviary opened this issue Mar 3, 2020 · 8 comments
Assignees
Labels

Comments

@behaviary
Copy link

behaviary commented Mar 3, 2020

Timeline: before MainNet.

Requirements:

  • Gas costs communicated clearly in each appropriate channels
  • A builder can estimate how much it's going to cost them to run on NEAR
  • Clear instructions for if you want to prototype with custom params
  • Optimization for costs are suggested as users navigate their tools
  • Provide a graph to see changing costs to the user that covers the historic boundaries of costs
@behaviary behaviary added the Epic label Mar 3, 2020
@behaviary
Copy link
Author

Context: near/nearcore#1829 (comment)

@behaviary behaviary self-assigned this Mar 3, 2020
@mikedotexe
Copy link

Shedding a little light into Ethereum and gas prices, here's how a person can estimate:
https://web3js.readthedocs.io/en/v1.2.0/web3-eth-contract.html#methods-mymethod-estimategas

@kcole16
Copy link
Collaborator

kcole16 commented Mar 5, 2020

Additional context:

The web3js function uses the eth_estimateGas RPC call, which essentially hits a node, executes the transaction, measures the gas used, then returns the result.

@willemneal Can add more detail about how https://github.com/nearprotocol/wasm-mock-vm might be able to replace the "node - execute- return result" portion of this.

@willemneal
Copy link

This is also relevant: near/nearcore#1937, basically we can also use symbolic execution to exercise all possible paths and get an upper and lower bound on gas usage.

@kcole16 As for the wasm-mock-vm, at first I simulated being inside a running contract and being able to call runtime apis. However, now I'm working on being able to invoke a method on a wasm binary, which can then be prepared with nearcore's runner. This includes the instrumenting needed for gas usage.

For the most part this will be a pretty easy metric (outside of cross contract calls), for example, a runtime call for stroage_write is

base + storage_write_base + storage_write_key_byte * num_key_bytes + storage_write_value_byte * num_value_bytes + get_vec_from_memory_or_register_cost x 2`.

However, a storage_read is:

`base + storage_read_base + storage_read_key_byte * num_key_bytes + storage_read_value_byte + num_value_bytes

Which means that we need to know the size of the value being read. This also applies for an added cost when a storage_write evicts another value.

Is there a way to read the storage outside of a contract call?

The other option is that we could update nearcore so that it can do a trial run that will perform reads only.

@mikedotexe
Copy link

@willemneal do you feel siloed on this effort? I know your wheelhouse is pretty deep in the weeds and it's not easy for other developers to jump in. I am, however, getting the sense that some of these important issues are kind of lobbed in your court and want to check in to see if folks can help.

@willemneal
Copy link

@mikedotexe I feel siloed do a little, but that is partly my fault at this point. After the conversion to gitpod, I'm looking to promote the wasm-mock-vm from prototype to product and could use help with that for sure. It would also be an opportunity for others to jump in. And those interested in rust can help with this aspect.

The same goes for near-sdk-as. I've made a lot of changes to the organization, which I hope make it clearer and a better dev environment, but I need to help others understand and their feedback.

Perhaps after the migration we should just have a meeting for those interested and split up tasks.

@mikedotexe
Copy link

Capturing basic notes as I start learning about this.
After speaking with Willem, I hear that the gas estimation is somewhat in-flight, but can be tested on AssemblyScript contracts right now.
Here's what I did:

  1. Went into our Guest Book example (written in AssemblyScript) in my terminal, copied the full path the out/main.wasm file
  2. Navigated to a local copy of near-api-js
  3. npm install near-mock-vm@next
  4. ./node_modules/.bin/near-vm /Users/mike/near/guest-book/out/main.wasm addMessage '{"text": "please homie"}' (using the path copied from Step 1)
  5. returned:
calls to injected gas: 47785
Gas used after startup: 0.250721756933
Outcome:
{
  balance1: 0,
  balance2: 4,
  storage_usage: 156,
  return_data: 'None',
  burnt_gas: 250721756933,
  used_gas: 250721756933,
  logs: []
}
length 0
Receipts:
[]
  1. drank a la croix and cheers the eastward air toward Willem.

@behaviary
Copy link
Author

@chadoh add the issues related to V1 of this associated with this epic.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants