near / nearcore

Reference client for NEAR Protocol
https://near.org
GNU General Public License v3.0
2.34k stars 632 forks source link

WASM execution without prior deployment #9379

Open jakmeier opened 1 year ago

jakmeier commented 1 year ago

A truly stateless validator would not have access to pre-deployed contract code. If we can't rely on that optimization, how much slower would WASM execution be and what can we do about it?

Luckily, we have already spent a lot of time optimizing compilation and are proud of our linear in time and space complexity WASM validation & compilation passes. So maybe it's actually okay to add this extra latency?

According to our gas costs today, compilation takes ~0.3ms + ~6.4ms per 100kB on a chunk producer.

Adding this extra delay for every function call on the stateless validator is unfortunate. But something we can potentially overcome.

Also note that due to everything being in-memory on the stateless validator, all storage related costs should be much faster. So for storage access heavy receipts, chunks would be produced much slower than stateless validators can validate them if they are running on comparable CPUs.

Other possible solutions to get around the compilation include:

robin-near commented 1 year ago

Validators can locally cache compiled contracts. Hopefully this will resolve most cases?

jakmeier commented 1 year ago

Yeah that's right, for the foreseeable future even a stateless validator should have enough disk space to cache the executables. There will be an ugly drop in performance anytime we have a protocol upgrade that invalidates old compilation results. But I would expect it to be fine anyway. Or at least I think we have bigger problems to worry about first. :P

jakmeier commented 1 year ago

https://github.com/near/NEPs/pull/481#issuecomment-1663591388 contains some info about interpreter speed if we ever want to go down that route anyway

nagisa commented 1 year ago

Just to add an alternative which is not written down: ZK validation does not care as much about execution time as there's only a single (or a couple) machines actually doing the actual execution and giving a significant time for the validators to compile and execute the contracts only serves to increase latency of individual transactions, rather than throughput of the network.