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

Remove Default bound for AccountId #10403

Merged
merged 52 commits into from
Dec 13, 2021
Merged

Remove Default bound for AccountId #10403

merged 52 commits into from
Dec 13, 2021

Conversation

gavofyork
Copy link
Member

@gavofyork gavofyork commented Dec 2, 2021

Polkadot companion: paritytech/polkadot#4500
Cumulus companion: paritytech/cumulus#842

Having a default AccountId effectively privileges the zero account as a special case, to be used when "no other account makes sense". This should obviously not be the case. AccountId conceptually does not have a default value and therefore the implementation should not exist and pallets should not assume it does.

There are several API ramifications of this change, though all of them have simple solutions.

Code migration

SignedExtension

Pre no longer has a Default bound. This means that the default implementation for pre_dispatch (which executes the validate function and returns a default Pre value) cannot exist and must be defined at the implementation site. There is a "standard" implementation which is functionally equivalent to the previous default implementation as long as Pre is ():

fn pre_dispatch(
	self,
	who: &Self::AccountId,
	call: &Self::Call,
	info: &sp_runtime::traits::DispatchInfoOf<Self::Call>,
	len: usize,
) -> Result<Self::Pre, sp_runtime::transaction_validity::TransactionValidityError> {
	Ok(self.validate(who, call, info, len).map(|_| ())?)
}

This can generally just be dropped in to a SignedExtension impl which was not otherwise defining it.

Authoring pallet fn author

pallet_authoring::author() now returns an Option, which is None if the block author is unknown. This should not typically be the case on production chains, but often is the case for tests. A future question is whether we really want to support this or rather to force tests to introduce a dummy author. The easiest way to transition is wrap any usage in an if let Some(author) = author() { ... }.

Session keys in tests

Session pallet now forces the existence of session keys for validators whereas previously it used default keys if they had not been set. This means that tests must explicitly set any session keys of validator accounts if they expect to see the account in the list of elected validators.

Public trait split into ByteArray

The functions within the sp_core::crypto::Public trait which worked on the assumption that the underlying type was a fixed-size u8 array are now in a new trait ByteArray. Public implies this. Generally you should use sp_core::crypto::ByteArray; instead of Public.

from_slice is fallible

The function ByteArray::from_slice (which used to be Public::from_slice) has been changed to return a Result in the case that the slice provided was the incorrect size. For an infallible conversion, use the UncheckedFrom trait with a fixed size array.

Sudo pallet GenesisConfig

Sudo pallet's GenesisConfig::key parameter is now an Option<AccountId> since a safe default value for the field must be provided. Wrap any previous value in Some, so that what used to be:

sudo: my_chain_runtime::SudoConfig { key: root_key },

Now becomes:

sudo: my_chain_runtime::SudoConfig { key: Some(root_key) },

@gavofyork gavofyork added the A3-in_progress Pull request is in progress. No review needed at this stage. label Dec 2, 2021
@github-actions github-actions bot 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 Dec 2, 2021
@gavofyork gavofyork marked this pull request as draft December 2, 2021 00:51
@gavofyork gavofyork added A3-in_progress Pull request is in progress. No review needed at this stage. and removed A0-please_review Pull request needs code review. labels Dec 2, 2021
frame/authorship/src/lib.rs Outdated Show resolved Hide resolved
frame/authorship/src/lib.rs Outdated Show resolved Hide resolved
frame/authorship/src/lib.rs Outdated Show resolved Hide resolved
frame/authorship/src/lib.rs Outdated Show resolved Hide resolved
frame/authorship/src/lib.rs Show resolved Hide resolved
gavofyork and others added 14 commits December 2, 2021 17:12
Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
@@ -417,8 +416,8 @@ fn reduce_all<A: IdentifierT>(assignments: &mut Vec<StakedAssignment<A>>) -> u32
// find minimum of cycle.
let mut min_value: ExtendedBalance = Bounded::max_value();
// The voter and the target pair that create the min edge.
let mut min_target: A = Default::default();
let mut min_voter: A = Default::default();
let mut min_target: Option<A> = None;
Copy link
Contributor

Choose a reason for hiding this comment

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

About this: after looking at the code, this bit seems like the only controversial bit. My conclusion is that this diff is not changing the logic of this snippet at all. The Default::default(), and min_index = 0 were clearly meant to be overwritten in the for loop that follows. In fact, the outcome is (pretty bad) UB if it doesn't. The changes of this PR are not altering any of that. The assumption still must hold that these values get written to. Making them an Option<_> doesn't harm that.

I wish I had the time now, or in the near future, to re-write the majority of this reduce algorithm. Admittedly, it is the last historical Spaghetti code left in sp-npos-election that I myself even have a very hard time reading. It was mostly written during a crunch and never been re-visited ever since. While I can't take the time now to improve this code fundamentally, I did propose some reasonable changes in this branch to make it more compatible with Option<_>, most notably making the assumption explicit that these Option will all become Some and stop the algorithm otherwise.

Lastly, luckily this code has a fuzzer that has proven quite helpful in the past. I'll re-run it again on my aforementioned branch and ring some bells if anything is broken.

Feel free to either apply the commits of https://github.com/paritytech/substrate/compare/gav-no-default-accountid...gav-no-default-accountid-staking?expand=1 either directly here, or I can open a PR on top of this. All changes in it are pretty clear in my opinion.

seunlanlege pushed a commit to seunlanlege/substrate that referenced this pull request Dec 17, 2021
* Remove Default for AccountId

* More removals of default

* Update frame/authorship/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update frame/authorship/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update frame/authorship/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update frame/authorship/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* More work

* More work

* Remove old code

* More work

* pallet-asset-tx-payment

* tips

* sc-consensus-babe

* sc-finality-grandpa

* sc-consensus-babe-rpc

* sc-cli

* make npos crates accept non-default account (paritytech#10420)

* minimal changes to make npos pallets all work

* make this pesky reduce.rs a bit cleaner

* more work

* more work

* Tests build

* Fix imonline tests

* Formatting

* Fixes

* Fixes

* Fix bench

* Fixes

* Fixes

* Fixes

* Fixes

* Fixes

* Formatting

* Fixes

* Formatting

* Fixes

* Formatting

* Fixes

* Formatting

* Fixes

* Formatting

* Update client/keystore/src/local.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/finality-grandpa/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/keystore/src/local.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/keystore/src/local.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update frame/staking/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update frame/staking/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update primitives/runtime/src/traits.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Formatting

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
Co-authored-by: kianenigma <kian@parity.io>
alistair-singh added a commit to Snowfork/snowbridge that referenced this pull request Mar 15, 2022
alistair-singh added a commit to Snowfork/snowbridge that referenced this pull request Mar 15, 2022
alistair-singh added a commit to Snowfork/snowbridge that referenced this pull request Mar 17, 2022
alistair-singh added a commit to Snowfork/snowbridge that referenced this pull request Mar 19, 2022
grishasobol pushed a commit to gear-tech/substrate that referenced this pull request Mar 28, 2022
* Remove Default for AccountId

* More removals of default

* Update frame/authorship/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update frame/authorship/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update frame/authorship/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update frame/authorship/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* More work

* More work

* Remove old code

* More work

* pallet-asset-tx-payment

* tips

* sc-consensus-babe

* sc-finality-grandpa

* sc-consensus-babe-rpc

* sc-cli

* make npos crates accept non-default account (paritytech#10420)

* minimal changes to make npos pallets all work

* make this pesky reduce.rs a bit cleaner

* more work

* more work

* Tests build

* Fix imonline tests

* Formatting

* Fixes

* Fixes

* Fix bench

* Fixes

* Fixes

* Fixes

* Fixes

* Fixes

* Formatting

* Fixes

* Formatting

* Fixes

* Formatting

* Fixes

* Formatting

* Fixes

* Formatting

* Update client/keystore/src/local.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/finality-grandpa/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/keystore/src/local.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/keystore/src/local.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update frame/staking/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update frame/staking/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update primitives/runtime/src/traits.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Formatting

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
Co-authored-by: kianenigma <kian@parity.io>
@jakoblell jakoblell added D1-audited 👍 PR contains changes to fund-managing 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 Jun 9, 2022
AurevoirXavier added a commit to darwinia-network/darwinia-common that referenced this pull request Sep 14, 2022
AurevoirXavier added a commit to darwinia-network/darwinia-common that referenced this pull request Sep 14, 2022
AurevoirXavier added a commit to darwinia-network/darwinia-common that referenced this pull request Sep 15, 2022
ark0f pushed a commit to gear-tech/substrate that referenced this pull request Feb 27, 2023
* Remove Default for AccountId

* More removals of default

* Update frame/authorship/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update frame/authorship/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update frame/authorship/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update frame/authorship/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* More work

* More work

* Remove old code

* More work

* pallet-asset-tx-payment

* tips

* sc-consensus-babe

* sc-finality-grandpa

* sc-consensus-babe-rpc

* sc-cli

* make npos crates accept non-default account (paritytech#10420)

* minimal changes to make npos pallets all work

* make this pesky reduce.rs a bit cleaner

* more work

* more work

* Tests build

* Fix imonline tests

* Formatting

* Fixes

* Fixes

* Fix bench

* Fixes

* Fixes

* Fixes

* Fixes

* Fixes

* Formatting

* Fixes

* Formatting

* Fixes

* Formatting

* Fixes

* Formatting

* Fixes

* Formatting

* Update client/keystore/src/local.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/finality-grandpa/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/keystore/src/local.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/keystore/src/local.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update frame/staking/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update frame/staking/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update primitives/runtime/src/traits.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Formatting

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
Co-authored-by: kianenigma <kian@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 fund-managing logic that has been properly reviewed and externally audited
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants