From 96b3a8f92f3709920f70d253604458109922b938 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Wed, 8 Aug 2018 17:47:22 +0200 Subject: [PATCH] Derivable Encode & Decode (#509) * Derive macro for simple structs. * Derive Encode/Decode wherever we can. * Derive for enums. * Support discriminant. * Get rid of some repetition. * Support custom indices. * Derive codec for enums wherever possible. * Fix no_std * WASM rebuilt * Avoid excessive import. * Fix compilation. * Address review grumbles. --- substrate/Cargo.lock | 23 + substrate/Cargo.toml | 1 + substrate/demo/runtime/wasm/Cargo.lock | 19 +- .../release/demo_runtime.compact.wasm | Bin 244061 -> 193206 bytes .../release/demo_runtime.wasm | Bin 244147 -> 193262 bytes substrate/polkadot/network/Cargo.toml | 1 + .../polkadot/network/src/collator_pool.rs | 19 +- substrate/polkadot/network/src/lib.rs | 86 +--- substrate/polkadot/parachain/Cargo.toml | 1 + substrate/polkadot/parachain/src/lib.rs | 39 +- substrate/polkadot/parachain/tests/adder.rs | 57 +-- .../polkadot/parachain/tests/res/adder.wasm | Bin 29853 -> 10174 bytes substrate/polkadot/primitives/Cargo.toml | 1 + substrate/polkadot/primitives/src/lib.rs | 22 +- .../polkadot/primitives/src/parachain.rs | 137 +----- substrate/polkadot/runtime/wasm/Cargo.lock | 20 +- .../release/polkadot_runtime.compact.wasm | Bin 322956 -> 221887 bytes .../release/polkadot_runtime.wasm | Bin 323041 -> 221942 bytes substrate/polkadot/statement-table/Cargo.toml | 1 + .../polkadot/statement-table/src/generic.rs | 85 +--- substrate/polkadot/statement-table/src/lib.rs | 3 + .../polkadot/test-parachains/adder/Cargo.toml | 1 + .../polkadot/test-parachains/adder/src/lib.rs | 43 +- substrate/substrate/client/db/Cargo.toml | 1 + substrate/substrate/client/db/src/cache.rs | 19 +- substrate/substrate/client/db/src/lib.rs | 3 + substrate/substrate/codec/derive/Cargo.toml | 20 + .../substrate/codec/derive/src/decode.rs | 111 +++++ .../substrate/codec/derive/src/encode.rs | 153 +++++++ substrate/substrate/codec/derive/src/lib.rs | 127 ++++++ substrate/substrate/codec/derive/tests/mod.rs | 151 +++++++ substrate/substrate/codec/src/codec.rs | 36 ++ substrate/substrate/codec/src/keyedvec.rs | 2 +- substrate/substrate/executor/wasm/Cargo.lock | 51 ++- .../release/runtime_test.compact.wasm | Bin 54272 -> 14419 bytes .../release/runtime_test.wasm | Bin 54482 -> 14548 bytes substrate/substrate/network/Cargo.toml | 1 + substrate/substrate/network/src/lib.rs | 1 + substrate/substrate/network/src/message.rs | 417 ++---------------- substrate/substrate/network/src/service.rs | 12 + substrate/substrate/primitives/Cargo.toml | 1 + .../substrate/primitives/src/authority_id.rs | 16 +- substrate/substrate/primitives/src/lib.rs | 7 +- substrate/substrate/primitives/src/sandbox.rs | 188 +------- .../substrate/runtime-support/src/dispatch.rs | 1 - .../substrate/runtime/democracy/Cargo.toml | 1 + .../substrate/runtime/democracy/src/lib.rs | 11 +- .../runtime/democracy/src/vote_threshold.rs | 24 +- .../substrate/runtime/primitives/Cargo.toml | 1 + .../substrate/runtime/primitives/src/bft.rs | 167 ++----- .../runtime/primitives/src/generic.rs | 85 +--- .../substrate/runtime/primitives/src/lib.rs | 57 +-- .../runtime/primitives/src/testing.rs | 71 +-- .../substrate/runtime/version/Cargo.toml | 1 + .../substrate/runtime/version/src/lib.rs | 41 +- substrate/substrate/state-db/Cargo.toml | 1 + substrate/substrate/state-db/src/lib.rs | 1 + substrate/substrate/state-db/src/pruning.rs | 21 +- .../substrate/state-db/src/unfinalized.rs | 23 +- substrate/substrate/test-runtime/Cargo.toml | 1 + substrate/substrate/test-runtime/src/lib.rs | 44 +- .../substrate/test-runtime/wasm/Cargo.lock | 19 +- .../substrate/test-runtime/wasm/Cargo.toml | 1 + .../substrate_test_runtime.compact.wasm | Bin 56218 -> 22785 bytes .../release/substrate_test_runtime.wasm | Bin 56350 -> 22868 bytes 65 files changed, 936 insertions(+), 1511 deletions(-) create mode 100644 substrate/substrate/codec/derive/Cargo.toml create mode 100644 substrate/substrate/codec/derive/src/decode.rs create mode 100644 substrate/substrate/codec/derive/src/encode.rs create mode 100644 substrate/substrate/codec/derive/src/lib.rs create mode 100644 substrate/substrate/codec/derive/tests/mod.rs diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index fc053e1e0e..772009b7b6 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -3,6 +3,7 @@ name = "adder" version = "0.1.0" dependencies = [ "polkadot-parachain 0.1.0", + "substrate-codec-derive 0.1.0", "tiny-keccak 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1953,6 +1954,7 @@ dependencies = [ "rhododendron 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "substrate-bft 0.1.0", "substrate-codec 0.1.0", + "substrate-codec-derive 0.1.0", "substrate-network 0.1.0", "substrate-primitives 0.1.0", "tokio 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1964,6 +1966,7 @@ version = "0.1.0" dependencies = [ "error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", "substrate-codec 0.1.0", + "substrate-codec-derive 0.1.0", "tiny-keccak 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "wasmi 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1976,6 +1979,7 @@ dependencies = [ "serde 1.0.70 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.70 (registry+https://github.com/rust-lang/crates.io-index)", "substrate-codec 0.1.0", + "substrate-codec-derive 0.1.0", "substrate-primitives 0.1.0", "substrate-runtime-primitives 0.1.0", "substrate-runtime-std 0.1.0", @@ -2047,6 +2051,7 @@ version = "0.1.0" dependencies = [ "polkadot-primitives 0.1.0", "substrate-codec 0.1.0", + "substrate-codec-derive 0.1.0", "substrate-primitives 0.1.0", ] @@ -2722,6 +2727,7 @@ dependencies = [ "patricia-trie 0.1.0 (git+https://github.com/paritytech/parity.git)", "substrate-client 0.1.0", "substrate-codec 0.1.0", + "substrate-codec-derive 0.1.0", "substrate-executor 0.1.0", "substrate-primitives 0.1.0", "substrate-runtime-primitives 0.1.0", @@ -2737,6 +2743,16 @@ dependencies = [ "arrayvec 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "substrate-codec-derive" +version = "0.1.0" +dependencies = [ + "proc-macro2 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", + "substrate-codec 0.1.0", + "syn 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "substrate-executor" version = "0.1.0" @@ -2829,6 +2845,7 @@ dependencies = [ "parking_lot 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "substrate-client 0.1.0", "substrate-codec 0.1.0", + "substrate-codec-derive 0.1.0", "substrate-keyring 0.1.0", "substrate-network-libp2p 0.1.0", "substrate-primitives 0.1.0", @@ -2875,6 +2892,7 @@ dependencies = [ "serde 1.0.70 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.70 (registry+https://github.com/rust-lang/crates.io-index)", "substrate-codec 0.1.0", + "substrate-codec-derive 0.1.0", "substrate-runtime-std 0.1.0", "substrate-serializer 0.1.0", "twox-hash 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2990,6 +3008,7 @@ dependencies = [ "serde 1.0.70 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.70 (registry+https://github.com/rust-lang/crates.io-index)", "substrate-codec 0.1.0", + "substrate-codec-derive 0.1.0", "substrate-primitives 0.1.0", "substrate-runtime-consensus 0.1.0", "substrate-runtime-io 0.1.0", @@ -3047,6 +3066,7 @@ dependencies = [ "serde_derive 1.0.70 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.22 (registry+https://github.com/rust-lang/crates.io-index)", "substrate-codec 0.1.0", + "substrate-codec-derive 0.1.0", "substrate-primitives 0.1.0", "substrate-runtime-io 0.1.0", "substrate-runtime-std 0.1.0", @@ -3172,6 +3192,7 @@ dependencies = [ "serde 1.0.70 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.70 (registry+https://github.com/rust-lang/crates.io-index)", "substrate-codec 0.1.0", + "substrate-codec-derive 0.1.0", "substrate-runtime-std 0.1.0", "substrate-runtime-support 0.1.0", ] @@ -3223,6 +3244,7 @@ dependencies = [ "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", "substrate-codec 0.1.0", + "substrate-codec-derive 0.1.0", "substrate-primitives 0.1.0", ] @@ -3282,6 +3304,7 @@ dependencies = [ "serde 1.0.70 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.70 (registry+https://github.com/rust-lang/crates.io-index)", "substrate-codec 0.1.0", + "substrate-codec-derive 0.1.0", "substrate-keyring 0.1.0", "substrate-primitives 0.1.0", "substrate-runtime-io 0.1.0", diff --git a/substrate/Cargo.toml b/substrate/Cargo.toml index 08a994197c..97df56d615 100644 --- a/substrate/Cargo.toml +++ b/substrate/Cargo.toml @@ -41,6 +41,7 @@ members = [ "substrate/client", "substrate/client/db", "substrate/codec", + "substrate/codec/derive", "substrate/environmental", "substrate/executor", "substrate/extrinsic-pool", diff --git a/substrate/demo/runtime/wasm/Cargo.lock b/substrate/demo/runtime/wasm/Cargo.lock index e3fda5b1f0..1a147b2a41 100644 --- a/substrate/demo/runtime/wasm/Cargo.lock +++ b/substrate/demo/runtime/wasm/Cargo.lock @@ -639,7 +639,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "rustc-hex" version = "2.0.0" -source = "git+https://github.com/rphmeier/rustc-hex.git#ee2ec40b9062ac7769ccb9dc891d6dc2cc9009d7" +source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "rustc_version" @@ -706,6 +706,15 @@ dependencies = [ "arrayvec 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "substrate-codec-derive" +version = "0.1.0" +dependencies = [ + "proc-macro2 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "substrate-keyring" version = "0.1.0" @@ -723,10 +732,11 @@ dependencies = [ "byteorder 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "crunchy 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", "fixed-hash 0.1.3 (git+https://github.com/rphmeier/primitives.git?branch=compile-for-wasm)", - "rustc-hex 2.0.0 (git+https://github.com/rphmeier/rustc-hex.git)", + "rustc-hex 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.64 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.64 (registry+https://github.com/rust-lang/crates.io-index)", "substrate-codec 0.1.0", + "substrate-codec-derive 0.1.0", "substrate-runtime-std 0.1.0", "twox-hash 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "uint 0.1.2 (git+https://github.com/rphmeier/primitives.git?branch=compile-for-wasm)", @@ -781,6 +791,7 @@ dependencies = [ "serde 1.0.64 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.64 (registry+https://github.com/rust-lang/crates.io-index)", "substrate-codec 0.1.0", + "substrate-codec-derive 0.1.0", "substrate-primitives 0.1.0", "substrate-runtime-consensus 0.1.0", "substrate-runtime-io 0.1.0", @@ -831,6 +842,7 @@ dependencies = [ "serde 1.0.64 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.64 (registry+https://github.com/rust-lang/crates.io-index)", "substrate-codec 0.1.0", + "substrate-codec-derive 0.1.0", "substrate-primitives 0.1.0", "substrate-runtime-io 0.1.0", "substrate-runtime-std 0.1.0", @@ -954,6 +966,7 @@ dependencies = [ "serde 1.0.64 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.64 (registry+https://github.com/rust-lang/crates.io-index)", "substrate-codec 0.1.0", + "substrate-codec-derive 0.1.0", "substrate-runtime-std 0.1.0", "substrate-runtime-support 0.1.0", ] @@ -1206,7 +1219,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum rlp 0.2.1 (git+https://github.com/paritytech/parity.git)" = "" "checksum rlp 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "89db7f8dfdd5eb7ab3ac3ece7a07fd273a680b4b224cb231181280e8996f9f0b" "checksum rustc-hex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0ceb8ce7a5e520de349e1fa172baeba4a9e8d5ef06c47471863530bc4972ee1e" -"checksum rustc-hex 2.0.0 (git+https://github.com/rphmeier/rustc-hex.git)" = "" +"checksum rustc-hex 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d2b03280c2813907a030785570c577fb27d3deec8da4c18566751ade94de0ace" "checksum rustc_version 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a54aa04a10c68c1c4eacb4337fd883b435997ede17a9385784b990777686b09a" "checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27" "checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" diff --git a/substrate/demo/runtime/wasm/target/wasm32-unknown-unknown/release/demo_runtime.compact.wasm b/substrate/demo/runtime/wasm/target/wasm32-unknown-unknown/release/demo_runtime.compact.wasm index fd2432257f5e2cfcfba1816abe47d880cff81357..93828266e1003a24953b86287e9b94b6b90231bc 100644 GIT binary patch literal 193206 zcmeFa3%p&|UEjGM=XKBH>PVI?S(5F2j+OYzv9R)zWIGUT6+1GDiGX~9KYaMqa$~gD1aUGR8U{De0!`AI#=-7PJftlYQvzHF3@K9>Iw{oCrqpeiVp>W(rBJsM z#`FFC|7-2N&$(Am%Z6zi=j!ae_S)<5fB*mMzt&D}f8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