improve storage build type checking (#1914)

* force build closure in decl_storage! to return explicit type
* fix when type was different

Note: it breaks API but it is easy to upgrade: just do the conversion from encode type to the final type yourself in the build closure:

```rust
Encode::using_encoded(&value, |mut value| Decode::decode(&mut v)).unwrap();
```
This commit is contained in:
thiolliere
2019-03-04 10:49:56 +01:00
committed by GitHub
parent a81f7f48a0
commit 66bc864f29
6 changed files with 13 additions and 10 deletions
+1 -1
View File
@@ -61,7 +61,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
impl_name: create_runtime_str!("substrate-node"),
authoring_version: 10,
spec_version: 31,
impl_version: 31,
impl_version: 32,
apis: RUNTIME_API_VERSIONS,
};
+1 -1
View File
@@ -117,7 +117,7 @@ decl_storage! {
pub VotingPeriod get(voting_period) config(): T::BlockNumber = T::BlockNumber::sa(3);
/// Number of blocks by which to delay enactment of successful, non-unanimous-council-instigated referendum proposals.
pub EnactDelayPeriod get(enact_delay_period) config(): T::BlockNumber = T::BlockNumber::sa(0);
pub Proposals get(proposals) build(|_| vec![0u8; 0]): Vec<(T::BlockNumber, T::Hash)>; // ordered by expiry.
pub Proposals get(proposals) build(|_| vec![]): Vec<(T::BlockNumber, T::Hash)>; // ordered by expiry.
pub ProposalOf get(proposal_of): map T::Hash => Option<T::Proposal>;
pub ProposalVoters get(proposal_voters): map T::Hash => Vec<T::AccountId>;
pub CouncilVoteOf get(vote_of): map (T::Hash, T::AccountId) => Option<bool>;
@@ -239,8 +239,6 @@ fn decl_store_extra_genesis(
let storage = (RefCell::new(&mut r), PhantomData::<Self>::default());
let v = (#builder)(&self);
let v = Encode::using_encoded(&v, |mut v| Decode::decode(&mut v))
.expect(#error_message);
<#name<#traitinstance> as #scrate::storage::generator::StorageValue<#typ>>::put(&v, &storage);
}}
},
@@ -252,8 +250,6 @@ fn decl_store_extra_genesis(
let storage = (RefCell::new(&mut r), PhantomData::<Self>::default());
let data = (#builder)(&self);
for (k, v) in data.into_iter() {
let v = Encode::using_encoded(&v, |mut v| Decode::decode(&mut v))
.expect(#error_message);
<#name<#traitinstance> as #scrate::storage::generator::StorageMap<#key_type, #typ>>::insert(&k, &v, &storage);
}
}}
+11 -4
View File
@@ -193,6 +193,13 @@ impl From<RawLog<substrate_primitives::H256>> for primitives::testing::DigestIte
}
}
// Create a Hash with 69 for each byte
fn hash69<T: AsMut<[u8]> + Default>() -> T {
let mut h = T::default();
h.as_mut().iter_mut().for_each(|byte| *byte = 69);
h
}
decl_storage! {
trait Store for Module<T: Trait> as System {
@@ -200,12 +207,12 @@ decl_storage! {
ExtrinsicCount: Option<u32>;
AllExtrinsicsLen: Option<u32>;
pub BlockHash get(block_hash) build(|_| vec![(T::BlockNumber::zero(), [69u8; 32])]): map T::BlockNumber => T::Hash;
pub BlockHash get(block_hash) build(|_| vec![(T::BlockNumber::zero(), hash69())]): map T::BlockNumber => T::Hash;
ExtrinsicData get(extrinsic_data): map u32 => Vec<u8>;
RandomSeed get(random_seed) build(|_| [0u8; 32]): T::Hash;
RandomSeed get(random_seed) build(|_| T::Hash::default()): T::Hash;
/// The current block number being processed. Set by `execute_block`.
Number get(block_number) build(|_| 1u64): T::BlockNumber;
ParentHash get(parent_hash) build(|_| [69u8; 32]): T::Hash;
Number get(block_number) build(|_| T::BlockNumber::sa(1u64)): T::BlockNumber;
ParentHash get(parent_hash) build(|_| hash69()): T::Hash;
ExtrinsicsRoot get(extrinsics_root): T::Hash;
Digest get(digest): T::Digest;