Skip to content

Commit

Permalink
Derive serde traits in no_std too (#131)
Browse files Browse the repository at this point in the history
* derive Serialize and Deserialize in no_std also

* cargo fmt

* adding a cumulus primitives crate will magically fix the build

* The actual magical crate is polkadot-parachain-primitives, a dependency of the previously suspected magic crate.

* Pull in important features from magic crate

* toml sort

* relax some changes
  • Loading branch information
JoshOrndorff authored Oct 18, 2023
1 parent bad03b6 commit 3a1f2a7
Show file tree
Hide file tree
Showing 20 changed files with 221 additions and 102 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ log = "0.4"
parity-scale-codec = { version = "3.4.0", default-features = false }
parity-util-mem = "0.12.0"
scale-info = { version = "2.1.1", default-features = false }
serde = "1.0"
serde = { version = "1.0.188", default-features = false }

# Procedural macro dependencies
proc-macro2 = "1.0.67"
Expand Down
4 changes: 2 additions & 2 deletions tuxedo-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ log = { workspace = true }
parity-scale-codec = { features = [ "derive" ], workspace = true }
parity-util-mem = { optional = true, workspace = true }
scale-info = { features = [ "derive" ], workspace = true }
serde = { features = [ "derive" ], optional = true, workspace = true }
serde = { features = [ "derive" ], workspace = true }

aggregator = { path = "aggregator" }
derive-no-bound = { path = "no_bound" }
Expand All @@ -37,7 +37,7 @@ std = [
"parity-scale-codec/std",
"sp-core/std",
"sp-std/std",
"serde",
"serde/std",
"sp-api/std",
"sp-inherents/std",
"sp-io/std",
Expand Down
4 changes: 1 addition & 3 deletions tuxedo-core/src/dynamic_typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,12 @@
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use sp_std::vec::Vec;

/// A piece of encoded data with a type id associated
/// Strongly typed data can be extracted
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)]
#[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)]
pub struct DynamicallyTypedData {
pub data: Vec<u8>,
pub type_id: [u8; 4],
Expand Down
13 changes: 4 additions & 9 deletions tuxedo-core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
use crate::{dynamic_typing::DynamicallyTypedData, ConstraintChecker, Verifier};
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use sp_core::H256;
use sp_runtime::traits::Extrinsic;
use sp_std::vec::Vec;

/// A reference to a output that is expected to exist in the state.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)]
#[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)]
pub struct OutputRef {
/// A hash of the transaction that created this output
pub tx_hash: H256,
Expand All @@ -34,8 +32,7 @@ pub struct OutputRef {
/// In the future, there may be additional notions of peeks (inputs that are not consumed)
/// and evictions (inputs that are forcefully consumed.)
/// Existing state to be read and consumed from storage
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Default, Debug, PartialEq, Eq, Clone, TypeInfo)]
#[derive(Serialize, Deserialize, Default, Debug, PartialEq, Eq, Clone, TypeInfo)]
pub struct Transaction<V, C> {
/// Existing pieces of state to be read and consumed from storage
pub inputs: Vec<Input>,
Expand Down Expand Up @@ -142,8 +139,7 @@ where
}

/// A reference the a utxo that will be consumed along with proof that it may be consumed
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)]
#[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)]
pub struct Input {
/// a reference to the output being consumed
pub output_ref: OutputRef,
Expand Down Expand Up @@ -174,8 +170,7 @@ pub type DispatchResult<VerifierError> = Result<(), UtxoError<VerifierError>>;
/// the verifier is checked, strongly typed data will be extracted and passed to the constraint checker.
/// In a cryptocurrency, the data represents a single coin. In Tuxedo, the type of
/// the contained data is generic.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)]
#[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)]
pub struct Output<V> {
pub payload: DynamicallyTypedData,
pub verifier: V,
Expand Down
15 changes: 6 additions & 9 deletions tuxedo-core/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use sp_core::sr25519::{Public, Signature};
use sp_core::H256;
Expand All @@ -25,8 +24,7 @@ pub trait Verifier: Debug + Encode + Decode + Clone {
}

/// A typical verifier that checks an sr25519 signature
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)]
#[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)]
pub struct SigCheck {
pub owner_pubkey: H256,
}
Expand All @@ -43,8 +41,9 @@ impl Verifier for SigCheck {
}

/// A simple verifier that allows anyone to consume an output at any time
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo, Default)]
#[derive(
Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo, Default,
)]
pub struct UpForGrabs;

impl Verifier for UpForGrabs {
Expand All @@ -57,8 +56,7 @@ impl Verifier for UpForGrabs {
/// guarded by this verifier. A valid redeemer must supply valid signatures by at least
/// `threshold` of the signatories. If the threshold is greater than the number of signatories
/// the input can never be consumed.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)]
#[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)]
pub struct ThresholdMultiSignature {
/// The minimum number of valid signatures needed to consume this input
pub threshold: u8,
Expand All @@ -75,8 +73,7 @@ impl ThresholdMultiSignature {
}
}

#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone)]
#[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone)]
/// Combination of a signature plus and index so that the signer can specify which
/// index this signature pertains too of the available signatories for a `ThresholdMultiSignature`
pub struct SignatureAndIndex {
Expand Down
10 changes: 5 additions & 5 deletions tuxedo-template-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ version = "1.0.0-dev"
log = { workspace = true }
parity-scale-codec = { features = [ "derive" ], workspace = true }
parity-util-mem = { optional = true, workspace = true }
scale-info = { features = [ "derive" ], workspace = true }
serde = { features = [ "derive" ], optional = true, workspace = true }
scale-info = { features = [ "derive", "serde" ], workspace = true }
serde = { features = [ "derive" ], workspace = true }

sp-api = { default_features = false, workspace = true }
sp-block-builder = { default_features = false, workspace = true }
sp-core = { default_features = false, workspace = true }
sp-core = { features = [ "serde" ], default_features = false, workspace = true }
sp-debug-derive = { features = [ "force-debug" ], default_features = false, workspace = true }
sp-inherents = { default_features = false, workspace = true }
sp-io = { features = [ "with-tracing" ], default_features = false, workspace = true }
sp-runtime = { default_features = false, workspace = true }
sp-runtime = { features = [ "serde" ], default_features = false, workspace = true }
sp-session = { default_features = false, workspace = true }
sp-std = { default_features = false, workspace = true }
sp-storage = { default_features = false, workspace = true }
Expand Down Expand Up @@ -57,7 +57,7 @@ std = [
"parity-scale-codec/std",
"sp-core/std",
"sp-std/std",
"serde",
"serde/std",
"sp-api/std",
"sp-session/std",
"sp-io/std",
Expand Down
10 changes: 7 additions & 3 deletions tuxedo-template-runtime/build.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use substrate_wasm_builder::WasmBuilder;

#[cfg(feature = "std")]
fn main() {
WasmBuilder::new()
substrate_wasm_builder::WasmBuilder::new()
.with_current_project()
.export_heap_base()
.import_memory()
.build()
}

/// The wasm builder is deactivated when compiling
/// this crate for wasm to speed up the compilation.
#[cfg(not(feature = "std"))]
fn main() {}
9 changes: 3 additions & 6 deletions tuxedo-template-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ use sp_runtime::{BuildStorage, Storage};
use sp_version::NativeVersion;
use sp_version::RuntimeVersion;

#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};

use tuxedo_core::{
Expand Down Expand Up @@ -105,7 +104,7 @@ pub fn native_version() -> NativeVersion {
}
}

#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Serialize, Deserialize)]
pub struct GenesisConfig {
pub genesis_utxos: Vec<Output>,
}
Expand Down Expand Up @@ -192,8 +191,7 @@ const BLOCK_TIME: u64 = 3000;

/// A verifier checks that an individual input can be consumed. For example that it is signed properly
/// To begin playing, we will have two kinds. A simple signature check, and an anyone-can-consume check.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)]
#[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)]
#[tuxedo_verifier]
pub enum OuterVerifier {
SigCheck(SigCheck),
Expand All @@ -220,8 +218,7 @@ impl timestamp::TimestampConfig for Runtime {
/// A constraint checker is a piece of logic that can be used to check a transaction.
/// For any given Tuxedo runtime there is a finite set of such constraint checkers.
/// For example, this may check that input token values exceed output token values.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)]
#[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)]
#[tuxedo_constraint_checker(OuterVerifier)]
pub enum OuterConstraintChecker {
/// Checks monetary transactions in a basic fungible cryptocurrency
Expand Down
4 changes: 2 additions & 2 deletions wardrobe/amoeba/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ version = "0.1.0"
[dependencies]
parity-scale-codec = { features = [ "derive" ], workspace = true }
scale-info = { features = [ "derive" ], workspace = true }
serde = { features = [ "derive" ], optional = true, workspace = true }
serde = { features = [ "derive" ], workspace = true }
sp-runtime = { default_features = false, workspace = true }
tuxedo-core = { default-features = false, path = "../../tuxedo-core" }

Expand All @@ -19,5 +19,5 @@ std = [
"tuxedo-core/std",
"parity-scale-codec/std",
"sp-runtime/std",
"serde",
"serde/std",
]
13 changes: 4 additions & 9 deletions wardrobe/amoeba/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use sp_runtime::transaction_validity::TransactionPriority;
use tuxedo_core::{
Expand All @@ -24,8 +23,7 @@ use tuxedo_core::{
mod tests;

/// An amoeba tracked by our simple Amoeba APP
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone)]
#[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone)]
pub struct AmoebaDetails {
/// How many generations after the original Eve Amoeba this one is.
/// When going through mitosis, this number must increase by 1 each time.
Expand Down Expand Up @@ -80,8 +78,7 @@ pub enum ConstraintCheckerError {
/// 1. There is exactly one mother amoeba.
/// 2. There are exactly two daughter amoebas
/// 3. Each Daughter amoeba has a generation one higher than its mother.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)]
#[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)]
pub struct AmoebaMitosis;

impl SimpleConstraintChecker for AmoebaMitosis {
Expand Down Expand Up @@ -139,8 +136,7 @@ impl SimpleConstraintChecker for AmoebaMitosis {
///
/// Any amoeba can be killed by providing it as the sole input to this constraint checker. No
/// new outputs are ever created.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)]
#[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)]
pub struct AmoebaDeath;

impl SimpleConstraintChecker for AmoebaDeath {
Expand Down Expand Up @@ -179,8 +175,7 @@ impl SimpleConstraintChecker for AmoebaDeath {
///
/// A new amoeba can be created by providing it as the sole output to this constraint checker. No
/// inputs are ever consumed.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)]
#[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)]
pub struct AmoebaCreation;

impl SimpleConstraintChecker for AmoebaCreation {
Expand Down
4 changes: 2 additions & 2 deletions wardrobe/kitties/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ version = "0.1.0"
[dependencies]
parity-scale-codec = { features = [ "derive" ], workspace = true }
scale-info = { features = [ "derive" ], workspace = true }
serde = { features = [ "derive" ], optional = true, workspace = true }
serde = { features = [ "derive" ], workspace = true }
sp-core = { default_features = false, workspace = true }
sp-runtime = { default_features = false, workspace = true }
sp-std = { default_features = false, workspace = true }
Expand All @@ -23,5 +23,5 @@ std = [
"sp-runtime/std",
"sp-std/std",
"sp-core/std",
"serde",
"serde/std",
]
Loading

0 comments on commit 3a1f2a7

Please sign in to comment.