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

Allow an Offset to Lease Periods #3980

Merged
merged 18 commits into from
Oct 5, 2021
Merged

Conversation

shawntabrizi
Copy link
Member

@shawntabrizi shawntabrizi commented Oct 1, 2021

Supersedes: #3960

This PR allows the entire crowdloan, slots, auction system to be delayed by a constant block number to do fine adjustments to when the lease period cycle should start. We do this by extending lease period 0 by LeaseOffset blocks, which offsets all lease periods by the same amount.

A few core level API changes have been introduced which ensures that no existing APIs break due to this new feature. By doing this, we ensure there is a single point of calculation of the current lease period, and every other API just reads from it. So other APIs should not be exposed to any concept of the lease period offset, and the underlying behavior of calculating the current lease period is completely opaque.

  • fn lease_period() was renamed to fn lease_period_length(), returns the offset in addition to the length, and is removed from the public api. We only use it in benchmarking, so we make sure to hide it behind a feature flag so that no external logic would use it unexpectedly.
  • fn lease_period_index() use to always return the CURRENT lease period for the current block number. We updated the API so that it always accepts an input of the block number we want to query, and it will return both the current lease period for that block, and if that was the first block of a new lease period.
  • In order to support the traits which span the different pallets, we had to expose BlockNumber as a generic argument, slightly changing the signature of the Leaser and Auctioneer traits. No big deal.
  • Docs updated for these changed traits.
  • A new test was added to verify fn lease_period_index() behaves correct with and without an offset.
  • An end-to-end integration test was updated to verify everything behaves as expected with a bunch of different offset values.

Comment on lines +434 to +436
#[cfg(any(feature = "runtime-benchmarks", test))]
fn lease_period_length() -> (T::BlockNumber, T::BlockNumber) {
(T::LeasePeriod::get(), T::LeaseOffset::get())
Copy link
Member Author

@shawntabrizi shawntabrizi Oct 1, 2021

Choose a reason for hiding this comment

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

This API is only needed for benchmarks, so we hide it from the public API, and therefore eliminate any chance that a user may try to calculate the current of future lease period index on their own.

Comment on lines 454 to 455
let now = frame_system::Pallet::<T>::block_number();
let current_lease_period = Self::lease_period_index(now).0;
Copy link
Member Author

Choose a reason for hiding this comment

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

In most places where we just called lease_period_index before, we now need to pass the current block number. This is the same logic and complexity as before, just explicit now.


Balances::make_free_balance_be(&10, 1_000_000_000);
Balances::make_free_balance_be(&20, 1_000_000_000);
for offset in [0u32, 50, 100, 200].iter() {
Copy link
Member Author

Choose a reason for hiding this comment

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

This test is basically unchanged except introducing offset into the timeline of the setup.

@shawntabrizi shawntabrizi added B7-runtimenoteworthy C1-low PR touches the given topic and has a low impact on builders. D9-needsaudit 👮 PR contains changes to fund-managing logic that should be properly reviewed and externally audited. A0-please_review Pull request needs code review. labels Oct 1, 2021
Comment on lines 439 to 452
fn lease_period_index(b: T::BlockNumber) -> (Self::LeasePeriod, bool) {
// Note that lease lease period 0 is artificially extended by the lease offset.
let offset_block_now = b.saturating_sub(T::LeaseOffset::get());
let lease_period = offset_block_now / T::LeasePeriod::get();

// Special logic to handle lease period 0 being extended by the offset.
let first_block = if lease_period.is_zero() {
// Lease period 0 has their first block on only block 0.
b.is_zero()
} else {
(offset_block_now % T::LeasePeriod::get()).is_zero()
};

(lease_period, first_block)
Copy link
Member Author

Choose a reason for hiding this comment

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

Here is the core change. This is the one source of truth for calculating the current lease period index given some offset. All other APIs must use this to do stuff.

@crystalin
Copy link

@shawntabrizi , isn't this feature going to bring even more complexity to the parachain lease computations? I still have today to explain people why Moonriver is not ending 48 weeks after it started :p

I'm curious, what is the use case for this change ?

@rphmeier
Copy link
Contributor

rphmeier commented Oct 2, 2021

@crystalin It's very useful for a relay chain's governance to decide the exact date at which parachains go live instead of waiting for the next lease period.

Copy link
Contributor

@emostov emostov left a comment

Choose a reason for hiding this comment

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

Code LGTM

w.r.t. Gav's suggestion I think it could be nice for a relay chain to have fine grained control over the start of lease period 0. But given that the target use case is already past that point I don't see a compelling reason to add the little bit of complexity in handling an Option from lease_period_index.

@shawntabrizi
Copy link
Member Author

@rphmeier take a look at the last changes made and if it is good for you, feel free to merge.

@rphmeier rphmeier merged commit 2a3fca3 into master Oct 5, 2021
@rphmeier rphmeier deleted the shawntabrizi-offset-generic branch October 5, 2021 14:07
ordian added a commit that referenced this pull request Oct 5, 2021
* master: (138 commits)
  Allow an Offset to Lease Periods (#3980)
  Bump quote from 1.0.9 to 1.0.10 (#4013)
  Companion for #9834 (Transaction Priority) (#3901)
  chore: update `builder` image (#3884)
  Free disputed cores before processing bitfields (#4008)
  Make candidate validation timeouts configurable (#4001)
  Add extrinsic ordering filtering (#3631)
  chore: ci list files that spellcheck finds (#3992)
  Use background tasks properly in candidate-validation (#4002)
  Fix unoccupied bitfields (#4004)
  Bump syn from 1.0.77 to 1.0.78 (#4006)
  Bump jsonrpsee-ws-client from 0.3.0 to 0.3.1 (#3931)
  fix clock drift for assignments issued before the block (#3851)
  Remove unoccupied bit check (#3999)
  bump substrate (#4000)
  change genesis authority set for wococo-local, revert rococo-local (#3998)
  ignore irrelevant approvals in logs (#3859)
  avoid expect, on free availability core (#3994)
  preserve finalized block in active leaves (#3997)
  some tweaks to rococo-local (#3996)
  ...
@chevdor chevdor added this to the v0.9.11 milestone Oct 7, 2021
ParityReleases pushed a commit that referenced this pull request Oct 7, 2021
* add slot offset for slots

* trying things out

* fix test

* improve api to return the first block of a new lease period

* add an integration test with offset

* de-duplicate test

* hide lease period_period_length from public api

* fix benchmarks

* Update runtime/common/src/slots.rs

* support the exact same range of crowdloans

* fix docs

* fix docs again

* introduce offset to runtimes

* fix and check edge case w/ offset and lease period first block

* remove newline

* turn into an option

* fix benchmarks

Co-authored-by: Robert Habermeier <rphmeier@gmail.com>
@s3krit s3krit mentioned this pull request Oct 7, 2021
@redzsina redzsina added D1-audited 👍 PR contains changes to critical logic that has been properly reviewed and externally audited. and removed D9-needsaudit 👮 PR contains changes to fund-managing logic that should be properly reviewed and externally audited. labels Oct 7, 2021
s3krit added a commit that referenced this pull request Oct 7, 2021
* v0.9.11 weights

* Allow an Offset to Lease Periods (#3980)

* add slot offset for slots

* trying things out

* fix test

* improve api to return the first block of a new lease period

* add an integration test with offset

* de-duplicate test

* hide lease period_period_length from public api

* fix benchmarks

* Update runtime/common/src/slots.rs

* support the exact same range of crowdloans

* fix docs

* fix docs again

* introduce offset to runtimes

* fix and check edge case w/ offset and lease period first block

* remove newline

* turn into an option

* fix benchmarks

Co-authored-by: Robert Habermeier <rphmeier@gmail.com>

* Polkadot: Remove configuration's runtime migration and just set StorageVersion  (#4035)

* wip

* Remove unused members

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
Co-authored-by: Robert Habermeier <rphmeier@gmail.com>
Co-authored-by: Sergei Shulepov <sergei@parity.io>
stiiifff pushed a commit that referenced this pull request Oct 11, 2021
bkchr pushed a commit that referenced this pull request Dec 2, 2021
* Permanent & Temp parachain slots on Rococo - WIP

* Revert test change

* Revert test change

* Fix formatting

* Extract logic to separate assigned_slots pallet

* Formatting

* Parachain downgrade logic

* Pallet doc comment

* Revert unnecessary changes

* Fix few issues, tweak temp slots allocation logic; add a bunch of tests

* Address review comments; track active temp slots

* Update runtime/common/src/assigned_slots.rs

* Update runtime/common/src/assigned_slots.rs

* Remove assigned_slots calls from paras_sudo_wrapper

* Unassign is a perfectly valid verb

* Remove unneeded collect

* Update code following #3980

* Cleanup

* Generate storage info for pallet

* Address review comments

* Add ForceOrigin to slots pallet

* Track permanent slot duration in storage

* Fix tests build

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review. C1-low PR touches the given topic and has a low impact on builders. D1-audited 👍 PR contains changes to critical logic that has been properly reviewed and externally audited.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants