Open behaviary opened 4 years ago
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
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.
This is also relevant: https://github.com/nearprotocol/nearcore/issues/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.
@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.
@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.
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:
out/main.wasm
filenpm install near-mock-vm@next
./node_modules/.bin/near-vm /Users/mike/near/guest-book/out/main.wasm addMessage '{"text": "please homie"}'
(using the path copied from Step 1)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:
[]
@chadoh add the issues related to V1 of this associated with this epic.
Timeline: before MainNet.
Requirements: