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

Add an endpoint on the node that will enable estimation of the execution cost #2880

Closed
piotr-dziubecki opened this issue Apr 20, 2022 · 0 comments · Fixed by #3093
Closed
Assignees
Labels
association release blocker PR to be merged before releasing

Comments

@piotr-dziubecki
Copy link

We've been asked many times in the past months (or maybe even more than a year) to give people a way to easily estimate gas cost. We have some plans to do that (like leveraging some of the fast-sync logic and extending it a bit) but they're all rather involving and won't be ready earlier than in a couple of months. I've been thinking whether there's some short-term, quick solution to this and I think I found it:

Let's add another endpoint on the node that will skip the gas accounting. It would be off by default, node operators should be able to easily limit the access to it (via iptables or other administratory tools) but anyone willing to sync A state (easy with fast sync) will be able to estimate the execution gas cost against MainNet state.

@piotr-dziubecki piotr-dziubecki changed the title Add an endpoint on the node that will skip the gas accounting Add an endpoint on the node that will enable estimation of the execution cost Apr 20, 2022
@piotr-dziubecki piotr-dziubecki added association release blocker PR to be merged before releasing labels May 18, 2022
@goral09 goral09 closed this as completed May 30, 2022
@goral09 goral09 reopened this Jun 9, 2022
casperlabs-bors-ng bot added a commit that referenced this issue Jun 10, 2022
3093: Adds support for the new `speculative_exec` endpoint. r=goral09 a=goral09

### Adds an JSON RPC endpoint executing a deploy and returning effects to the sender.

The new endpoint runs on a separate port from the previous JSON RPC. It can be configured in the `rpc_server` section of the `config.toml`. Setting `speculative_execution_qps_limit` to 0 will disable the endpoint. When sending a request to a node with disabled endpoint we get the following result:
```
* Could not resolve host: POST
* Closing connection 0
curl: (6) Could not resolve host: POST
*   Trying 127.0.0.1:25102...
* TCP_NODELAY set
* connect to 127.0.0.1 port 25102 failed: Connection refused
* Failed to connect to localhost port 25102: Connection refused
* Closing connection 1
curl: (7) Failed to connect to localhost port 25102: Connection refused
```

Endpoint accepts two parameters:
* block_identifier- this can be `BlockHash`, `StateRootHash` or null if deploy is to be executed on top of the latest block
* deploy - a deploy to execute.

(One can think of this as _"deploy dry run"_)

It will execute the deploy on top of the specified block and return the results of the execution to the caller. The effects of the execution won't be committed to the trie (blockchain database/GlobalState). Endpoint can be used for debugging, discovery - for example price estimation. If block_hash can't be found, proper error
will be returned. Any error returned during execution will be forwarded to the caller of the API.


### Example usage
1. Run nctl network - `nctl-compile && nctl-assets-setup && nctl-start`
2. Let it run for a while and then note hash of the recent block: `nctl-view-chain-lfb node=1`.
3. Stop node=1 (`nctl-stop node=1` and edit its config file to enable the new endpoint, its disabled by default. To do that, go to `utils/nctl/assets/net-1/nodes/node-1/config/1_0_0` and edit `speculative_qps_limit` in the `config.toml` to a positive number.
4. Start the node `nctl-start node=1 hash=<hash taken from nctl-view-chain-lfb node=2>`.
5. Create a deploy (in `casper-client-rs` directory), execute the command:
```
./target/release/casper-client make-transfer --chain-name "casper-net-1" --amount 1000 --secret-key ../casper-node/utils/nctl/assets/net-1/nodes/node-1/keys/secret_key.pem --target-account=01bf65587c501685ca5d0cb5293509fa4cecdb4503c7364084e18e14339e46a46f --transfer-id 1 --payment-amount 25000000 | jq -c
```
6. Send deploy to the node
```
curl -v POST -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","method":"speculative_exec","params":{"deploy": <previously store deploy formatted as JSON>, "block_identifier": <block_identifier> },"id":1}' 'http://localhost:25101/rpc'
```

where `block_identifier` can be a `{"Hash" : <block_hash> }`, `{"Height" : <block_height>}` or absent entirely (i.e. no `block_identifier` value), in which case it will execute deploy on top of the latest block.

7. Collect the results. If one wants to extract the cost, and the execution was a success, pipe the results into `jq` and extract the cost:
```
<previous command> | jq -c .result.execution_result.Success.cost
```


Closes #2880

Co-authored-by: Mateusz <mateusz@casperlabs.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
association release blocker PR to be merged before releasing
Projects
None yet
2 participants