Make constants exposable in metadata (#2975)

* Some cleanup

* Add module constant metadata declaration

* Begin to integrate the constants in `decl_module`

* Fixes tests

* Fix compilation and add tests

* Remove duplicate code

* Expose constants in democracy and staking + further fixes

* Update srml/metadata/src/lib.rs

Co-Authored-By: YJ <yjkimjunior@gmail.com>

* Hide `RawEvent` metadata function

* Prevent whitespaces in types

* Fix `offchain_worker` and `constants` with instances

* Up the `impl_version`

* Fix macro

* Incrase impl_version
This commit is contained in:
Bastian Köcher
2019-07-01 10:05:28 +02:00
committed by GitHub
parent 5f1538b834
commit 7202403bfc
12 changed files with 858 additions and 580 deletions
+28 -31
View File
@@ -102,10 +102,7 @@ impl<B, O> serde::Serialize for DecodeDifferent<B, O>
B: serde::Serialize + 'static,
O: serde::Serialize + 'static,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
match self {
DecodeDifferent::Encode(b) => b.serialize(serializer),
DecodeDifferent::Decoded(o) => o.serialize(serializer),
@@ -162,10 +159,7 @@ impl<E: Encode + ::std::fmt::Debug> std::fmt::Debug for FnEncode<E> {
#[cfg(feature = "std")]
impl<E: Encode + serde::Serialize> serde::Serialize for FnEncode<E> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
self.0().serialize(serializer)
}
}
@@ -190,21 +184,24 @@ pub struct EventMetadata {
pub documentation: DecodeDifferentArray<&'static str, StringBuf>,
}
/// All the metadata about a storage.
/// All the metadata about one storage entry.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Debug, Serialize))]
pub struct StorageMetadata {
pub functions: DecodeDifferentArray<StorageFunctionMetadata>,
pub struct StorageEntryMetadata {
pub name: DecodeDifferentStr,
pub modifier: StorageEntryModifier,
pub ty: StorageEntryType,
pub default: ByteGetter,
pub documentation: DecodeDifferentArray<&'static str, StringBuf>,
}
/// All the metadata about a storage function.
/// All the metadata about one module constant.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Debug, Serialize))]
pub struct StorageFunctionMetadata {
pub struct ModuleConstantMetadata {
pub name: DecodeDifferentStr,
pub modifier: StorageFunctionModifier,
pub ty: StorageFunctionType,
pub default: ByteGetter,
pub ty: DecodeDifferentStr,
pub value: ByteGetter,
pub documentation: DecodeDifferentArray<&'static str, StringBuf>,
}
@@ -238,10 +235,7 @@ impl Eq for DefaultByteGetter { }
#[cfg(feature = "std")]
impl serde::Serialize for DefaultByteGetter {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
self.0.default_byte().serialize(serializer)
}
}
@@ -264,10 +258,10 @@ pub enum StorageHasher {
Twox64Concat,
}
/// A storage function type.
/// A storage entry type.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Debug, Serialize))]
pub enum StorageFunctionType {
pub enum StorageEntryType {
Plain(DecodeDifferentStr),
Map {
hasher: StorageHasher,
@@ -284,10 +278,10 @@ pub enum StorageFunctionType {
},
}
/// A storage function modifier.
/// A storage entry modifier.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Debug, Serialize))]
pub enum StorageFunctionModifier {
pub enum StorageEntryModifier {
Optional,
Default,
}
@@ -313,8 +307,10 @@ pub enum RuntimeMetadata {
V3(RuntimeMetadataDeprecated),
/// Version 4 for runtime metadata. No longer used.
V4(RuntimeMetadataDeprecated),
/// Version 5 for runtime metadata.
V5(RuntimeMetadataV5),
/// Version 5 for runtime metadata. No longer used.
V5(RuntimeMetadataDeprecated),
/// Version 6 for runtime metadata.
V6(RuntimeMetadataV6),
}
/// Enum that should fail.
@@ -323,8 +319,7 @@ pub enum RuntimeMetadata {
pub enum RuntimeMetadataDeprecated { }
impl Encode for RuntimeMetadataDeprecated {
fn encode_to<W: Output>(&self, _dest: &mut W) {
}
fn encode_to<W: Output>(&self, _dest: &mut W) {}
}
#[cfg(feature = "std")]
@@ -337,7 +332,7 @@ impl Decode for RuntimeMetadataDeprecated {
/// The metadata of a runtime.
#[derive(Eq, Encode, PartialEq)]
#[cfg_attr(feature = "std", derive(Decode, Debug, Serialize))]
pub struct RuntimeMetadataV5 {
pub struct RuntimeMetadataV6 {
pub modules: DecodeDifferentArray<ModuleMetadata>,
}
@@ -347,12 +342,14 @@ pub struct RuntimeMetadataV5 {
pub struct ModuleMetadata {
pub name: DecodeDifferentStr,
pub prefix: DecodeDifferent<FnEncode<&'static str>, StringBuf>,
pub storage: ODFnA<StorageFunctionMetadata>,
pub storage: ODFnA<StorageEntryMetadata>,
pub calls: ODFnA<FunctionMetadata>,
pub event: ODFnA<EventMetadata>,
pub constants: DFnA<ModuleConstantMetadata>,
}
type ODFnA<T> = Option<DecodeDifferent<FnEncode<&'static [T]>, Vec<T>>>;
type ODFnA<T> = Option<DFnA<T>>;
type DFnA<T> = DecodeDifferent<FnEncode<&'static [T]>, Vec<T>>;
impl Into<primitives::OpaqueMetadata> for RuntimeMetadataPrefixed {
fn into(self) -> primitives::OpaqueMetadata {