near / core-contracts

Core contracts: reference staking pool, lockup, voting, whitelist, multisig.
322 stars 130 forks source link

Potential updates of factory contracts needed due to increased deployment cost #206

Open jakmeier opened 2 years ago

jakmeier commented 2 years ago

Deployment costs per byte of code are going to be increased by a factor of ~5 with this change: https://github.com/near/nearcore/pull/6397

Probably this will happen with protocol version 53, which would be released on testnet by March 23 and on mainnet by April 20.

I listed all the indirect deployments from the past that would fail with the new cost. lockup-factory showed up 74 times and staking-pool-factory showed up once. Meaning today's usage patterns of these contracts will potentially break with this change.

Details on lockup-factory

It looks like today always 110Tgas is attached (example). 2x 25Tgas of that are reserved for function calls after the deployment, leaving 60Tgas for the create function call. Today's deployment cost for lockup_contract.wasm with 337 KB is ~4.96Tgas and it will change to ~24.43Tgas. And it looks like the function call execution itself costs ~40Tgas. So 40Tgas + 24.43Tgas < 60Tgas means calling create with the new costs and only 110Tgas will fail to execute.

Details on staking-pool-factory

The transaction in question attached 200Tgas. 2x 50Tgas are reserved and ~38.86 Tgas is burnt in the function call execution today. staking_pool.wasm is 250kB today, which translates to ~3.78Tgas for deployment with the current price and 18.22Tgas with new costs. So I reckon this should not break as it is.

cc @austinabell

ConfidenceYobo commented 2 years ago

I also noticed the deployment of the multisig-factory contract require at least 2 near of storage balance for the new contract. I also tried creating a fresh factory contract that creates a ft contract and it costs 6 near to deploy the contract. why is it that expensive?

jakmeier commented 2 years ago

@ConfidenceYobo storage costs are somewhat orthogonal to gas costs. But it could be that the contract you are deploying is larger than it needs to be. 100kB ~ 1 NEAR of storage staking, so if it requires 6 NEAR in your case it seems your contract WASM is 600kB. That's not unreasonable but intuitively I would have expected a simple FT contract to be smaller.

Here is some good documentation on how to reduce WASM size: https://rustwasm.github.io/docs/book/reference/code-size.html#optimizing-builds-for-code-size