Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Introduce BlockExecutionWeight and ExtrinsicBaseWeight #5722

Merged
33 commits merged into from
Apr 25, 2020

Conversation

shawntabrizi
Copy link
Member

@shawntabrizi shawntabrizi commented Apr 21, 2020

This PR introduces the following concepts:

  • Every extrinsic has some base weight for existing in a block. For example, Reading/Writing from the sender account in storage, signature verification, etc...

  • Every block has some base weight independent of the included extrinsics.

To account for this, we make the following changes:

New System Traits

We introduce 2 new system configuration traits to account for all weights in the block execution process.

  1. BlockExecutionWeight: A fixed value that takes into account any overhead introduced by simply executing a block, not including the execution of the transactions themselves.
	/// Actually execute all transitions for `block`.
	pub fn execute_block(block: Block) {
		Self::initialize_block(block.header());

		// any initial checks
		Self::initial_checks(&block);

		let batching_safeguard = sp_runtime::SignatureBatching::start();
		// execute extrinsics
		let (header, extrinsics) = block.deconstruct();
		Self::execute_extrinsics_with_book_keeping(extrinsics, *header.number());
		if !sp_runtime::SignatureBatching::verify(batching_safeguard) {
			panic!("Signature verification failed.");
		}

		// any final checks
		Self::final_checks(&header);
	}

So the stuff above minus execute_extrinsics_with_book_keeping

  1. ExtrinsicBaseWeight: A fixed value that takes into account any overhead introduced by simply including an extrinsic in a block. Measured by executing many no-op extrinsics and deriving the individual cost of one such extrinsic.

These two weights will automatically be included through the block execution process when defined in the runtime.

We also replace the notion of TransactionBaseFee with ExtrinsicBaseWeight * WeightToFee.

New Weight Logic

Before this PR (and the larger goal of updating all extrinsic weights), all weights were constant. As a result, we assumed that no weight would be too big for a single block.

Now that extrinsic weights could be represented by formulas, and the input to an extrinsic could be beyond the capabilities of the blockchain. So we introduce the following new behavior:

  • Any extrinsic whose weight would push the weight of a block past its limit is discarded with the error ExhaustedResources.
  • There is an exception to this rule with DispatchClass::Mandatory, which will be allowed in a block no matter what. This may cause the weight of a block to be larger than MaximumBlockWeight.

This weight pipeline needs to take into account the new traits we defined, so to make it a bit more ergonomic to define weights, we also introduce a new System function max_extrinsic_weight, which takes a DispatchClass and returns the max weight allowed for an extrinsic in a block. Only one such extrinsic like this could exist in a block.

This PR also removed MINIMUM_WEIGHT, since that concept is basically replaced with ExtrinsicBaseWeight now.

@shawntabrizi shawntabrizi added the A3-in_progress Pull request is in progress. No review needed at this stage. label Apr 21, 2020
frame/system/src/lib.rs Outdated Show resolved Hide resolved
frame/system/src/lib.rs Outdated Show resolved Hide resolved
@shawntabrizi shawntabrizi added A0-please_review Pull request needs code review. and removed A3-in_progress Pull request is in progress. No review needed at this stage. labels Apr 22, 2020
@gavofyork gavofyork added A8-mergeoncegreen and removed A0-please_review Pull request needs code review. labels Apr 24, 2020
frame/contracts/src/lib.rs Outdated Show resolved Hide resolved
frame/contracts/src/lib.rs Outdated Show resolved Hide resolved
frame/executive/src/lib.rs Outdated Show resolved Hide resolved
frame/support/src/error.rs Outdated Show resolved Hide resolved
frame/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
@gnunicorn
Copy link
Contributor

bot merge

1 similar comment
@shawntabrizi
Copy link
Member Author

bot merge

@ghost
Copy link

ghost commented Apr 25, 2020

Cannot merge; please ensure the pull request is mergeable and has approval from the project owner or at least 2 core devs.

@ghost
Copy link

ghost commented Apr 25, 2020

bot merge cancel

@sjeohp-zz
Copy link

bot merge

@ghost ghost merged commit 32ae0fd into master Apr 25, 2020
@ghost
Copy link

ghost commented Apr 25, 2020

bot merge complete

@ghost ghost deleted the shawntabrizi-additional-block-weights branch April 25, 2020 05:59
sorpaas pushed a commit that referenced this pull request Nov 20, 2020
* Introduce `BlockExectionWeight` and `ExtrinsicBaseWeight`

* Add new traits everywhere

* Missed one update

* fix tests

* Update `check_weight` logic

* introduce `max_extrinsic_weight` function

* fix + add tests

* format nits

* remove println

* make test a bit more clear

* Remove minimum weight

* newlines left over from find/replace

* Fix test, improve clarity

* Fix executor tests

* Extrinsic base weight same as old `MINIMUM_WEIGHT`

* fix example test

* Expose constants

* Add test for full block with operational and normal

* Initiate test environment with `BlockExecutionWeight` weight

* format nit

* Update frame/system/src/lib.rs

Co-Authored-By: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Replace `TransactionBaseFee` with `ExtrinsicBaseWeight` (#5761)

* Replace `TransactionBaseFee` with `ExtrinsicBaseFee`

* Fix stuff

* Fix and make tests better

* Forgot to update this test

* Fix priority number in test

* Remove minimum weight from merge

* Fix weight in contracts

* remove `TransactionBaseFee` from contract tests

* Let `register_extra_weight_unchecked` go past `MaximumBlockWeight`

* address feedback

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants