Skip to content
Snippets Groups Projects
Commit ee6633e0 authored by Demi Obenour's avatar Demi Obenour Committed by GitHub
Browse files

Add notes about safe uses of twox (#6082)


* Add notes about safe uses of twox

* Update frame/grandpa/src/lib.rs

Co-authored-by: default avatarNikolay Volf <nikvolf@gmail.com>

* Update frame/elections/src/lib.rs

* Apply suggestions from code review

Co-authored-by: default avatarGavin Wood <gavin@parity.io>
Co-authored-by: default avatarNikolay Volf <nikvolf@gmail.com>
parent a90c4232
No related merge requests found
...@@ -257,6 +257,8 @@ decl_storage! { ...@@ -257,6 +257,8 @@ decl_storage! {
/// The next asset identifier up for grabs. /// The next asset identifier up for grabs.
NextAssetId get(fn next_asset_id): T::AssetId; NextAssetId get(fn next_asset_id): T::AssetId;
/// The total unit supply of an asset. /// The total unit supply of an asset.
///
/// TWOX-NOTE: `AssetId` is trusted, so this is safe.
TotalSupply: map hasher(twox_64_concat) T::AssetId => T::Balance; TotalSupply: map hasher(twox_64_concat) T::AssetId => T::Balance;
} }
} }
......
...@@ -152,6 +152,8 @@ decl_storage! { ...@@ -152,6 +152,8 @@ decl_storage! {
/// We reset all segments and return to `0` at the beginning of every /// We reset all segments and return to `0` at the beginning of every
/// epoch. /// epoch.
SegmentIndex build(|_| 0): u32; SegmentIndex build(|_| 0): u32;
/// TWOX-NOTE: `SegmentIndex` is an increasing integer, so this is okay.
UnderConstruction: map hasher(twox_64_concat) u32 => Vec<schnorrkel::Randomness>; UnderConstruction: map hasher(twox_64_concat) u32 => Vec<schnorrkel::Randomness>;
/// Temporary value (cleared at block finalization) which is `Some` /// Temporary value (cleared at block finalization) which is `Some`
......
...@@ -836,6 +836,8 @@ decl_storage! { ...@@ -836,6 +836,8 @@ decl_storage! {
/// The subtrie counter. /// The subtrie counter.
pub AccountCounter: u64 = 0; pub AccountCounter: u64 = 0;
/// The code associated with a given account. /// The code associated with a given account.
///
/// TWOX-NOTE: SAFE since `AccountId` is a secure hash.
pub ContractInfoOf: map hasher(twox_64_concat) T::AccountId => Option<ContractInfo<T>>; pub ContractInfoOf: map hasher(twox_64_concat) T::AccountId => Option<ContractInfo<T>>;
} }
} }
......
...@@ -344,6 +344,8 @@ decl_storage! { ...@@ -344,6 +344,8 @@ decl_storage! {
/// The public proposals. Unsorted. The second item is the proposal's hash. /// The public proposals. Unsorted. The second item is the proposal's hash.
pub PublicProps get(fn public_props): Vec<(PropIndex, T::Hash, T::AccountId)>; pub PublicProps get(fn public_props): Vec<(PropIndex, T::Hash, T::AccountId)>;
/// Those who have locked a deposit. /// Those who have locked a deposit.
///
/// TWOX-NOTE: Safe, as increasing integer keys are safe.
pub DepositOf get(fn deposit_of): pub DepositOf get(fn deposit_of):
map hasher(twox_64_concat) PropIndex => Option<(Vec<T::AccountId>, BalanceOf<T>)>; map hasher(twox_64_concat) PropIndex => Option<(Vec<T::AccountId>, BalanceOf<T>)>;
...@@ -362,22 +364,30 @@ decl_storage! { ...@@ -362,22 +364,30 @@ decl_storage! {
pub LowestUnbaked get(fn lowest_unbaked) build(|_| 0 as ReferendumIndex): ReferendumIndex; pub LowestUnbaked get(fn lowest_unbaked) build(|_| 0 as ReferendumIndex): ReferendumIndex;
/// Information concerning any given referendum. /// Information concerning any given referendum.
///
/// TWOX-NOTE: SAFE as indexes are not under an attacker’s control.
pub ReferendumInfoOf get(fn referendum_info): pub ReferendumInfoOf get(fn referendum_info):
map hasher(twox_64_concat) ReferendumIndex map hasher(twox_64_concat) ReferendumIndex
=> Option<ReferendumInfo<T::BlockNumber, T::Hash, BalanceOf<T>>>; => Option<ReferendumInfo<T::BlockNumber, T::Hash, BalanceOf<T>>>;
/// All votes for a particular voter. We store the balance for the number of votes that we /// All votes for a particular voter. We store the balance for the number of votes that we
/// have recorded. The second item is the total amount of delegations, that will be added. /// have recorded. The second item is the total amount of delegations, that will be added.
///
/// TWOX-NOTE: SAFE as `AccountId`s are crypto hashes anyway.
pub VotingOf: map hasher(twox_64_concat) T::AccountId => Voting<BalanceOf<T>, T::AccountId, T::BlockNumber>; pub VotingOf: map hasher(twox_64_concat) T::AccountId => Voting<BalanceOf<T>, T::AccountId, T::BlockNumber>;
/// Who is able to vote for whom. Value is the fund-holding account, key is the /// Who is able to vote for whom. Value is the fund-holding account, key is the
/// vote-transaction-sending account. /// vote-transaction-sending account.
///
/// TWOX-NOTE: OK ― `AccountId` is a secure hash.
// TODO: Refactor proxy into its own pallet. // TODO: Refactor proxy into its own pallet.
// https://github.com/paritytech/substrate/issues/5322 // https://github.com/paritytech/substrate/issues/5322
pub Proxy get(fn proxy): map hasher(twox_64_concat) T::AccountId => Option<ProxyState<T::AccountId>>; pub Proxy get(fn proxy): map hasher(twox_64_concat) T::AccountId => Option<ProxyState<T::AccountId>>;
/// Accounts for which there are locks in action which may be removed at some point in the /// Accounts for which there are locks in action which may be removed at some point in the
/// future. The value is the block number at which the lock expires and may be removed. /// future. The value is the block number at which the lock expires and may be removed.
///
/// TWOX-NOTE: OK ― `AccountId` is a secure hash.
pub Locks get(fn locks): map hasher(twox_64_concat) T::AccountId => Option<T::BlockNumber>; pub Locks get(fn locks): map hasher(twox_64_concat) T::AccountId => Option<T::BlockNumber>;
/// True if the last referendum tabled was submitted externally. False if it was a public /// True if the last referendum tabled was submitted externally. False if it was a public
......
...@@ -197,6 +197,8 @@ decl_storage! { ...@@ -197,6 +197,8 @@ decl_storage! {
pub ElectionRounds get(fn election_rounds): u32 = Zero::zero(); pub ElectionRounds get(fn election_rounds): u32 = Zero::zero();
/// Votes and locked stake of a particular voter. /// Votes and locked stake of a particular voter.
///
/// TWOX-NOTE: SAFE as `AccountId` is a crypto hash
pub Voting get(fn voting): map hasher(twox_64_concat) T::AccountId => (BalanceOf<T>, Vec<T::AccountId>); pub Voting get(fn voting): map hasher(twox_64_concat) T::AccountId => (BalanceOf<T>, Vec<T::AccountId>);
/// The present candidate list. Sorted based on account-id. A current member or runner-up /// The present candidate list. Sorted based on account-id. A current member or runner-up
......
...@@ -237,16 +237,25 @@ decl_storage! { ...@@ -237,16 +237,25 @@ decl_storage! {
// bit-wise manner. In order to get a human-readable representation (`Vec<bool>`), use // bit-wise manner. In order to get a human-readable representation (`Vec<bool>`), use
// [`all_approvals_of`]. Furthermore, each vector of scalars is chunked with the cap of // [`all_approvals_of`]. Furthermore, each vector of scalars is chunked with the cap of
// `APPROVAL_SET_SIZE`. // `APPROVAL_SET_SIZE`.
///
/// TWOX-NOTE: SAFE as `AccountId` is a crypto hash and `SetIndex` is not
/// attacker-controlled.
pub ApprovalsOf get(fn approvals_of): pub ApprovalsOf get(fn approvals_of):
map hasher(twox_64_concat) (T::AccountId, SetIndex) => Vec<ApprovalFlag>; map hasher(twox_64_concat) (T::AccountId, SetIndex) => Vec<ApprovalFlag>;
/// The vote index and list slot that the candidate `who` was registered or `None` if they /// The vote index and list slot that the candidate `who` was registered or `None` if they
/// are not currently registered. /// are not currently registered.
///
/// TWOX-NOTE: SAFE as `AccountId` is a crypto hash.
pub RegisterInfoOf get(fn candidate_reg_info): pub RegisterInfoOf get(fn candidate_reg_info):
map hasher(twox_64_concat) T::AccountId => Option<(VoteIndex, u32)>; map hasher(twox_64_concat) T::AccountId => Option<(VoteIndex, u32)>;
/// Basic information about a voter. /// Basic information about a voter.
///
/// TWOX-NOTE: SAFE as `AccountId` is a crypto hash.
pub VoterInfoOf get(fn voter_info): pub VoterInfoOf get(fn voter_info):
map hasher(twox_64_concat) T::AccountId => Option<VoterInfo<BalanceOf<T>>>; map hasher(twox_64_concat) T::AccountId => Option<VoterInfo<BalanceOf<T>>>;
/// The present voter list (chunked and capped at [`VOTER_SET_SIZE`]). /// The present voter list (chunked and capped at [`VOTER_SET_SIZE`]).
///
/// TWOX-NOTE: OKAY ― `SetIndex` is not user-controlled data.
pub Voters get(fn voters): map hasher(twox_64_concat) SetIndex => Vec<Option<T::AccountId>>; pub Voters get(fn voters): map hasher(twox_64_concat) SetIndex => Vec<Option<T::AccountId>>;
/// the next free set to store a voter in. This will keep growing. /// the next free set to store a voter in. This will keep growing.
pub NextVoterSet get(fn next_nonfull_voter_set): SetIndex = 0; pub NextVoterSet get(fn next_nonfull_voter_set): SetIndex = 0;
......
...@@ -442,16 +442,22 @@ pub struct BalanceLock<Balance> { ...@@ -442,16 +442,22 @@ pub struct BalanceLock<Balance> {
decl_storage! { decl_storage! {
trait Store for Module<T: Trait> as GenericAsset { trait Store for Module<T: Trait> as GenericAsset {
/// Total issuance of a given asset. /// Total issuance of a given asset.
///
/// TWOX-NOTE: `AssetId` is trusted.
pub TotalIssuance get(fn total_issuance) build(|config: &GenesisConfig<T>| { pub TotalIssuance get(fn total_issuance) build(|config: &GenesisConfig<T>| {
let issuance = config.initial_balance * (config.endowed_accounts.len() as u32).into(); let issuance = config.initial_balance * (config.endowed_accounts.len() as u32).into();
config.assets.iter().map(|id| (id.clone(), issuance)).collect::<Vec<_>>() config.assets.iter().map(|id| (id.clone(), issuance)).collect::<Vec<_>>()
}): map hasher(twox_64_concat) T::AssetId => T::Balance; }): map hasher(twox_64_concat) T::AssetId => T::Balance;
/// The free balance of a given asset under an account. /// The free balance of a given asset under an account.
///
/// TWOX-NOTE: `AssetId` is trusted.
pub FreeBalance: pub FreeBalance:
double_map hasher(twox_64_concat) T::AssetId, hasher(blake2_128_concat) T::AccountId => T::Balance; double_map hasher(twox_64_concat) T::AssetId, hasher(blake2_128_concat) T::AccountId => T::Balance;
/// The reserved balance of a given asset under an account. /// The reserved balance of a given asset under an account.
///
/// TWOX-NOTE: `AssetId` is trusted.
pub ReservedBalance: pub ReservedBalance:
double_map hasher(twox_64_concat) T::AssetId, hasher(blake2_128_concat) T::AccountId => T::Balance; double_map hasher(twox_64_concat) T::AssetId, hasher(blake2_128_concat) T::AccountId => T::Balance;
...@@ -459,6 +465,8 @@ decl_storage! { ...@@ -459,6 +465,8 @@ decl_storage! {
pub NextAssetId get(fn next_asset_id) config(): T::AssetId; pub NextAssetId get(fn next_asset_id) config(): T::AssetId;
/// Permission options for a given asset. /// Permission options for a given asset.
///
/// TWOX-NOTE: `AssetId` is trusted.
pub Permissions get(fn get_permission): pub Permissions get(fn get_permission):
map hasher(twox_64_concat) T::AssetId => PermissionVersions<T::AccountId>; map hasher(twox_64_concat) T::AssetId => PermissionVersions<T::AccountId>;
......
...@@ -212,6 +212,8 @@ decl_storage! { ...@@ -212,6 +212,8 @@ decl_storage! {
/// A mapping from grandpa set ID to the index of the *most recent* session for which its /// A mapping from grandpa set ID to the index of the *most recent* session for which its
/// members were responsible. /// members were responsible.
///
/// TWOX-NOTE: `SetId` is not under user control.
SetIdSession get(fn session_for_set): map hasher(twox_64_concat) SetId => Option<SessionIndex>; SetIdSession get(fn session_for_set): map hasher(twox_64_concat) SetId => Option<SessionIndex>;
} }
add_extra_genesis { add_extra_genesis {
......
...@@ -389,6 +389,8 @@ pub struct RegistrarInfo< ...@@ -389,6 +389,8 @@ pub struct RegistrarInfo<
decl_storage! { decl_storage! {
trait Store for Module<T: Trait> as Identity { trait Store for Module<T: Trait> as Identity {
/// Information that is pertinent to identify the entity behind an account. /// Information that is pertinent to identify the entity behind an account.
///
/// TWOX-NOTE: OK ― `AccountId` is a secure hash.
pub IdentityOf get(fn identity): pub IdentityOf get(fn identity):
map hasher(twox_64_concat) T::AccountId => Option<Registration<BalanceOf<T>>>; map hasher(twox_64_concat) T::AccountId => Option<Registration<BalanceOf<T>>>;
...@@ -400,6 +402,8 @@ decl_storage! { ...@@ -400,6 +402,8 @@ decl_storage! {
/// Alternative "sub" identities of this account. /// Alternative "sub" identities of this account.
/// ///
/// The first item is the deposit, the second is a vector of the accounts. /// The first item is the deposit, the second is a vector of the accounts.
///
/// TWOX-NOTE: OK ― `AccountId` is a secure hash.
pub SubsOf get(fn subs_of): pub SubsOf get(fn subs_of):
map hasher(twox_64_concat) T::AccountId => (BalanceOf<T>, Vec<T::AccountId>); map hasher(twox_64_concat) T::AccountId => (BalanceOf<T>, Vec<T::AccountId>);
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment