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

Whitelist gas accounting #1242

Closed
wants to merge 23 commits into from
Closed

Whitelist gas accounting #1242

wants to merge 23 commits into from

Conversation

grarco
Copy link
Contributor

@grarco grarco commented Mar 22, 2023

Addresses #1010.

This PR implements a first version of gas metering based on the runtime cost of the whitelisted txs and vps:

  • In storage table of gas costs (taken from the now modified checksums.json file)
  • Native vps charge gas only for the calls to the host exposed functions
  • Gas cost for a single signature verification (to account for multisig transactions)
  • Tx and block GasLimit checks in mempool_validate, prepare_proposal and process_proposal
  • Runtime tx gas check against the declared GasLimit in finalize_block
  • Removes MASP fees
  • Check that all the whitelisted txs/vps have an associated gas cost in checksums.py, and fn load_genesis_config
  • Unit tests

@grarco grarco force-pushed the grarco/whitelist-gas-accounting branch 2 times, most recently from 458c28b to 7ae6159 Compare March 22, 2023 13:35
@grarco grarco mentioned this pull request Mar 22, 2023
4 tasks
@grarco grarco force-pushed the grarco/whitelist-gas-accounting branch from 7ae6159 to 66ab4d6 Compare March 22, 2023 14:06
@@ -1892,23 +1882,26 @@ pub async fn query_conversions(ctx: Context, args: args::QueryConversions) {
}

/// Query a conversion.
pub async fn query_conversion(
client: HttpClient,
pub async fn query_conversion<CLIENT>(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed these changes for benchmarks since I wanted to reuse some client functions of masp (specifically to construct shielded txs) but without a real client nor a running ledger. I suspect the SDK guys are doing something similar to this but I haven't has the time to check

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, it's exactly like that

@grarco grarco force-pushed the grarco/whitelist-gas-accounting branch 6 times, most recently from 7103311 to c9ca5ad Compare March 24, 2023 15:33
@grarco
Copy link
Contributor Author

grarco commented Mar 24, 2023

pls update wasm

@grarco grarco force-pushed the grarco/whitelist-gas-accounting branch 6 times, most recently from 2466237 to a0b5d93 Compare March 28, 2023 16:50
@grarco
Copy link
Contributor Author

grarco commented Mar 28, 2023

pls update wasm

@grarco grarco force-pushed the grarco/whitelist-gas-accounting branch 2 times, most recently from dff51dd to d1843d2 Compare March 29, 2023 14:48
@grarco
Copy link
Contributor Author

grarco commented Mar 30, 2023

pls update wasm

@grarco grarco force-pushed the grarco/whitelist-gas-accounting branch 3 times, most recently from 37b8279 to 823e5a7 Compare March 31, 2023 14:05
@grarco
Copy link
Contributor Author

grarco commented Mar 31, 2023

pls update wasm

grarco added a commit that referenced this pull request Mar 31, 2023
@grarco grarco force-pushed the grarco/whitelist-gas-accounting branch from 09322e8 to f5ee56d Compare March 31, 2023 16:35
const FEE_AMOUNT: ArgDefault<token::Amount> =
arg_default("fee-amount", DefaultFn(|| token::Amount::from(0)));
const GAS_LIMIT: ArgDefault<GasLimit> =
arg_default("gas-limit", DefaultFn(|| GasLimit::from(10)));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This default for the GasLimit doesn't really have much sense given how different the costs for different txs will be and the lack of a refunding mechanism (at the moment). It's there to ease the tx submission process in testnets but it will probably be better to remove it in future PRs or even in this one

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Does the client do gas estimation? A reasonable approach for now can be to estimate gas and add a little bit of a buffer on top, then set that as the gas limit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment I've added the possibility to dry-run the entire tx (wrapper + inner) to estimate the gas cost manually. Were you thinking about something that doesn't require an action from the user? Like the client automatically doing the estimation right before submitting a tx?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the latter - at least this should be possible as a default, which can be turned off by a flag manually specifying gas.

grarco added a commit that referenced this pull request Mar 31, 2023
@grarco grarco force-pushed the grarco/whitelist-gas-accounting branch from 8cf8135 to 9768a85 Compare May 20, 2023 17:34
grarco added a commit that referenced this pull request May 23, 2023
@grarco grarco force-pushed the grarco/whitelist-gas-accounting branch from 9768a85 to fce81b4 Compare May 23, 2023 15:40
@grarco grarco force-pushed the grarco/whitelist-gas-accounting branch from fce81b4 to 6349f96 Compare June 5, 2023 01:31
Copy link
Contributor

@cwgoes cwgoes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly looks fine, a few comments.

const FEE_AMOUNT: ArgDefault<token::Amount> =
arg_default("fee-amount", DefaultFn(|| token::Amount::from(0)));
const GAS_LIMIT: ArgDefault<GasLimit> =
arg_default("gas-limit", DefaultFn(|| GasLimit::from(10)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the latter - at least this should be possible as a default, which can be turned off by a flag manually specifying gas.

@@ -0,0 +1,20 @@
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How are we generating this file, exactly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment manually. We first run the benchmarks, then we go through the html report produced and compute the gas cost based on the average execution time. The conversion should be something like 1ms~10 gas units. I didn't feel like automating this because we only need to run the benchmarks from time to time

let wrapper_fees = self.get_wrapper_tx_fees();
match balance.checked_sub(wrapper_fees) {
Some(amount) => {
self.wl_storage
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a more... abstract version of this function, perhaps?

I feel like we often do logic with raw keys that should be abstracted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I've improved this in #1327

/// Add gas cost. It will return error when the
/// consumed gas exceeds the provided transaction gas limit, but the state
/// will still be updated
fn add(&mut self, gas: u64) -> Result<()>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be clearer to call this "consume" ?

@grarco
Copy link
Contributor Author

grarco commented Aug 10, 2023

Closed in favor of #1327

@grarco grarco closed this Aug 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants