From 462f95d8d239b39078102e7202413a2b8da80538 Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Tue, 8 Jun 2021 11:53:01 +0200 Subject: [PATCH 01/14] [primitives/node] fix std flags --- primitives/node/Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/primitives/node/Cargo.toml b/primitives/node/Cargo.toml index 4d0d8b8f13..3a8519de2c 100644 --- a/primitives/node/Cargo.toml +++ b/primitives/node/Cargo.toml @@ -20,7 +20,6 @@ optional = true git = "https://github.com/scs/substraTEE-node" package = "substratee-node-runtime" optional = true -default-features = false [dependencies.substrate-api-client] git = "https://github.com/scs/substrate-api-client" @@ -34,13 +33,14 @@ default-features = false [dependencies.sp-runtime] git = "https://github.com/paritytech/substrate.git" version = "3.0.0" -default-features = false optional = true [features] default = ['std'] -std = [ 'substrate-api-client', +std = [ 'codec/std', + 'substrate-api-client', 'my-node-runtime', + 'primitive-types/std', 'sp-core/std', 'sp-runtime', 'base58', From b911ad38db5ff2580ae7b0bb5bb9fa3e1ca9a035 Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Tue, 8 Jun 2021 11:54:24 +0200 Subject: [PATCH 02/14] [primitives/node] add api-client extensions --- Cargo.lock | 256 +++++++++++++++++++++++++++++++++---- client/Cargo.toml | 3 + client/src/main.rs | 23 +--- primitives/node/src/api.rs | 71 ++++++++++ primitives/node/src/lib.rs | 75 +---------- worker/src/main.rs | 29 +---- 6 files changed, 320 insertions(+), 137 deletions(-) create mode 100644 primitives/node/src/api.rs diff --git a/Cargo.lock b/Cargo.lock index 8275a35e89..1476b99ed9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -340,6 +340,15 @@ version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" +[[package]] +name = "beef" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6736e2428df2ca2848d846c43e88745121a6654696e349ce0054a420815a7409" +dependencies = [ + "serde", +] + [[package]] name = "bitflags" version = "1.2.1" @@ -449,6 +458,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "771fe0050b883fcc3ea2359b1a96bcfbc090b7116eae7c3c512c7a083fdf23d3" +[[package]] +name = "bstr" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a40b47ad93e1a5404e6c18dec46b628214fee441c70f4ab5d6942142cc268a3d" +dependencies = [ + "memchr 2.3.4", +] + [[package]] name = "build-helper" version = "0.1.1" @@ -681,6 +699,15 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ea221b5284a47e40033bf9b66f35f984ec0ea2931eb03505246cd27a963f981b" +[[package]] +name = "cpufeatures" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed00c67cb5d0a7d64a44f6ad2668db7e7530311dd53ea79bcd4fb022c64911c8" +dependencies = [ + "libc", +] + [[package]] name = "cpuid-bool" version = "0.1.2" @@ -1236,9 +1263,9 @@ dependencies = [ [[package]] name = "futures-channel" -version = "0.3.13" +version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c2dd2df839b57db9ab69c2c9d8f3e8c81984781937fe2807dc6dcf3b2ad2939" +checksum = "e682a68b29a882df0545c143dc3646daefe80ba479bcdede94d5a703de2871e2" dependencies = [ "futures-core", "futures-sink", @@ -1246,9 +1273,9 @@ dependencies = [ [[package]] name = "futures-core" -version = "0.3.13" +version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "15496a72fabf0e62bdc3df11a59a3787429221dd0710ba8ef163d6f7a9112c94" +checksum = "0402f765d8a89a26043b889b26ce3c4679d268fa6bb22cd7c6aad98340e179d1" [[package]] name = "futures-executor" @@ -1264,9 +1291,9 @@ dependencies = [ [[package]] name = "futures-io" -version = "0.3.13" +version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d71c2c65c57704c32f5241c1223167c2c3294fd34ac020c807ddbe6db287ba59" +checksum = "acc499defb3b348f8d8f3f66415835a9131856ff7714bf10dadfc4ec4bdb29a1" [[package]] name = "futures-lite" @@ -1285,10 +1312,11 @@ dependencies = [ [[package]] name = "futures-macro" -version = "0.3.13" +version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea405816a5139fb39af82c2beb921d52143f556038378d6db21183a5c37fbfb7" +checksum = "a4c40298486cdf52cc00cd6d6987892ba502c7656a16a4192a9992b1ccedd121" dependencies = [ + "autocfg 1.0.1", "proc-macro-hack", "proc-macro2", "quote 1.0.9", @@ -1297,15 +1325,15 @@ dependencies = [ [[package]] name = "futures-sink" -version = "0.3.13" +version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85754d98985841b7d4f5e8e6fbfa4a4ac847916893ec511a2917ccd8525b8bb3" +checksum = "a57bead0ceff0d6dde8f465ecd96c9338121bb7717d3e7b108059531870c4282" [[package]] name = "futures-task" -version = "0.3.13" +version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa189ef211c15ee602667a6fcfe1c1fd9e07d42250d2156382820fba33c9df80" +checksum = "8a16bef9fc1a4dddb5bee51c989e3fbba26569cbb0e31f5b303c184e3dd33dae" [[package]] name = "futures-timer" @@ -1315,10 +1343,11 @@ checksum = "e64b03909df88034c26dc1547e8970b91f98bdb65165d6a4e9110d94263dbb2c" [[package]] name = "futures-util" -version = "0.3.13" +version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1812c7ab8aedf8d6f2701a43e1243acdbcc2b36ab26e2ad421eb99ac963d96d1" +checksum = "feb5c238d27e2bf94ffdfd27b2c29e3df4a68c4193bb6427384259e2bf191967" dependencies = [ + "autocfg 1.0.1", "futures 0.1.31", "futures-channel", "futures-core", @@ -1416,6 +1445,19 @@ version = "0.23.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f6503fe142514ca4799d4c26297c4248239fe8838d827db6bd6065c6ed29a6ce" +[[package]] +name = "globset" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c152169ef1e421390738366d2f796655fec62621dabbd0fd476f905934061e4a" +dependencies = [ + "aho-corasick 0.7.15", + "bstr", + "fnv", + "log 0.4.14 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 1.4.5", +] + [[package]] name = "gloo-timers" version = "0.2.1" @@ -1463,7 +1505,7 @@ dependencies = [ "http", "indexmap", "slab", - "tokio 1.4.0", + "tokio 1.6.1", "tokio-util 0.6.5", "tracing", ] @@ -1671,7 +1713,7 @@ dependencies = [ "itoa", "pin-project 1.0.6", "socket2", - "tokio 1.4.0", + "tokio 1.6.1", "tower-service", "tracing", "want", @@ -1699,7 +1741,7 @@ dependencies = [ "bytes 1.0.1", "hyper 0.14.5", "native-tls", - "tokio 1.4.0", + "tokio 1.6.1", "tokio-native-tls", ] @@ -1844,7 +1886,7 @@ dependencies = [ "serde", "serde_json", "serde_urlencoded", - "tokio 1.4.0", + "tokio 1.6.1", "tokio-util 0.6.5", "tracing", "typed-builder", @@ -1944,6 +1986,97 @@ dependencies = [ "serde", ] +[[package]] +name = "jsonrpsee" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "316a89048d2ea5530ab5502aa31e1128f6429b524a37e4c0bc54903bcdf3d342" +dependencies = [ + "jsonrpsee-http-server", + "jsonrpsee-utils", + "jsonrpsee-ws-server", +] + +[[package]] +name = "jsonrpsee-http-server" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d22372378f63f7d16de453e786afc740fca5ee80bd260be024a616b6ac2cefe5" +dependencies = [ + "futures-channel", + "futures-util", + "globset", + "hyper 0.14.5", + "jsonrpsee-types", + "jsonrpsee-utils", + "lazy_static", + "log 0.4.14 (registry+https://github.com/rust-lang/crates.io-index)", + "serde", + "serde_json", + "socket2", + "thiserror", + "tokio 1.6.1", + "unicase", +] + +[[package]] +name = "jsonrpsee-types" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0cf7bd4e93b3b56e59131de7f24afbea871faf914e97bcdd942c86927ab0172" +dependencies = [ + "async-trait", + "beef", + "futures-channel", + "futures-util", + "hyper 0.14.5", + "log 0.4.14 (registry+https://github.com/rust-lang/crates.io-index)", + "serde", + "serde_json", + "soketto", + "thiserror", +] + +[[package]] +name = "jsonrpsee-utils" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47554ecaacb479285da68799d9b6afc258c32b332cc8b85829c6a9304ee98776" +dependencies = [ + "futures-channel", + "futures-util", + "hyper 0.14.5", + "jsonrpsee-types", + "log 0.4.14 (registry+https://github.com/rust-lang/crates.io-index)", + "parking_lot 0.11.1", + "rand 0.8.3", + "rustc-hash", + "serde", + "serde_json", + "thiserror", +] + +[[package]] +name = "jsonrpsee-ws-server" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b512c3c679a89d20f97802f69188a2d01f6234491b7513076e21e8424efccafe" +dependencies = [ + "futures-channel", + "futures-util", + "jsonrpsee-types", + "jsonrpsee-utils", + "log 0.4.14 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-hash", + "serde", + "serde_json", + "soketto", + "thiserror", + "tokio 1.6.1", + "tokio-stream", + "tokio-util 0.6.5", +] + [[package]] name = "keccak" version = "0.1.0" @@ -4207,6 +4340,19 @@ dependencies = [ "opaque-debug 0.2.3", ] +[[package]] +name = "sha-1" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c4cfa741c5832d0ef7fab46cabed29c2aae926db0b11bb2069edd8db5e64e16" +dependencies = [ + "block-buffer 0.9.0", + "cfg-if 1.0.0", + "cpufeatures", + "digest 0.9.0", + "opaque-debug 0.3.0", +] + [[package]] name = "sha1" version = "0.5.0" @@ -4333,6 +4479,21 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "soketto" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4919971d141dbadaa0e82b5d369e2d7666c98e4625046140615ca363e50d4daa" +dependencies = [ + "base64 0.13.0", + "bytes 1.0.1", + "futures 0.3.13", + "httparse", + "log 0.4.14 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.8.3", + "sha-1 0.9.6", +] + [[package]] name = "sp-api" version = "3.0.0" @@ -5067,6 +5228,7 @@ dependencies = [ "substrate-api-client", "substrate-bip39 0.3.1", "substrate-client-keystore", + "substratee-node-primitives", "substratee-node-runtime", "substratee-stf", "substratee-worker-api", @@ -5230,6 +5392,21 @@ dependencies = [ "sp-runtime", ] +[[package]] +name = "substratee-worker-rpc" +version = "0.8.0" +dependencies = [ + "anyhow", + "env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-util", + "jsonrpsee", + "log 0.4.14 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json", + "soketto", + "tokio 1.6.1", + "tokio-util 0.6.5", +] + [[package]] name = "subtle" version = "1.0.0" @@ -5456,21 +5633,27 @@ dependencies = [ "futures-core", "memchr 2.3.4", "pin-project-lite 0.1.12", - "tokio-macros", + "tokio-macros 0.2.6", ] [[package]] name = "tokio" -version = "1.4.0" +version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "134af885d758d645f0f0505c9a8b3f9bf8a348fd822e112ab5248138348f1722" +checksum = "0a38d31d7831c6ed7aad00aa4c12d9375fd225a6dd77da1d25b707346319a975" dependencies = [ "autocfg 1.0.1", "bytes 1.0.1", "libc", "memchr 2.3.4", "mio 0.7.11", + "num_cpus", + "once_cell 1.7.2", + "parking_lot 0.11.1", "pin-project-lite 0.2.6", + "signal-hook-registry", + "tokio-macros 1.2.0", + "winapi 0.3.9", ] [[package]] @@ -5484,6 +5667,17 @@ dependencies = [ "syn 1.0.68", ] +[[package]] +name = "tokio-macros" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c49e3df43841dafb86046472506755d8501c5615673955f6aa17181125d13c37" +dependencies = [ + "proc-macro2", + "quote 1.0.9", + "syn 1.0.68", +] + [[package]] name = "tokio-native-tls" version = "0.3.0" @@ -5491,7 +5685,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7d995660bd2b7f8c1568414c1126076c13fbb725c40112dc0120b78eb9b717b" dependencies = [ "native-tls", - "tokio 1.4.0", + "tokio 1.6.1", +] + +[[package]] +name = "tokio-stream" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8864d706fdb3cc0843a49647ac892720dac98a6eeb818b77190592cf4994066" +dependencies = [ + "futures-core", + "pin-project-lite 0.2.6", + "tokio 1.6.1", ] [[package]] @@ -5516,10 +5721,11 @@ checksum = "5143d049e85af7fbc36f5454d990e62c2df705b3589f123b71f441b6b59f443f" dependencies = [ "bytes 1.0.1", "futures-core", + "futures-io", "futures-sink", "log 0.4.14 (registry+https://github.com/rust-lang/crates.io-index)", "pin-project-lite 0.2.6", - "tokio 1.4.0", + "tokio 1.6.1", ] [[package]] @@ -5658,7 +5864,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04f8ab788026715fa63b31960869617cba39117e520eb415b0139543e325ab59" dependencies = [ "cfg-if 0.1.10", - "rand 0.6.5", + "rand 0.7.3", "static_assertions", ] @@ -6119,7 +6325,7 @@ dependencies = [ "mio-extras", "openssl", "rand 0.7.3", - "sha-1", + "sha-1 0.8.2", "slab", "url 2.2.1", ] diff --git a/client/Cargo.toml b/client/Cargo.toml index 3e709da226..f029755a09 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -63,6 +63,9 @@ package = "substratee-node-runtime" [dependencies.substratee-worker-primitives] path = "../primitives/worker" +[dependencies.substratee-node-primitives] +path = "../primitives/node" + [dependencies.substratee-stf] path = "../stf" diff --git a/client/src/main.rs b/client/src/main.rs index 21d96873ec..adffcd40a3 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -40,7 +40,7 @@ use clap_nested::{Command, Commander}; use codec::{Decode, Encode}; use log::*; use my_node_runtime::{ - substratee_registry::{Enclave, Request}, + substratee_registry::Request, AccountId, BalancesCall, Call, Event, Hash, Signature, }; use sp_core::{crypto::Ss58Codec, sr25519 as sr25519_core, Pair, H256}; @@ -64,6 +64,7 @@ use substrate_api_client::{ use substrate_client_keystore::LocalKeystore; use substratee_stf::{ShardIdentifier, TrustedCallSigned, TrustedOperation}; use substratee_worker_api::direct_client::DirectApi as DirectWorkerApi; +use substratee_node_primitives::SubstrateeRegistryApi; use substratee_worker_primitives::{DirectRequestStatus, RpcRequest, RpcResponse, RpcReturnValue}; type AccountPublic = ::Signer; @@ -293,10 +294,10 @@ fn main() { .description("query enclave registry and list all workers") .runner(|_args: &str, matches: &ArgMatches<'_>| { let api = get_chain_api(matches); - let wcount = get_enclave_count(&api); + let wcount = api.enclave_count(); println!("number of workers registered: {}", wcount); for w in 1..=wcount { - let enclave = get_enclave(&api, w); + let enclave = api.enclave(w); if enclave.is_none() { println!("error reading enclave data"); continue; @@ -865,19 +866,3 @@ fn get_pair_from_str(account: &str) -> sr25519::AppPair { } } } - -fn get_enclave_count(api: &Api) -> u64 { - if let Some(count) = api - .get_storage_value("SubstrateeRegistry", "EnclaveCount", None) - .unwrap() - { - count - } else { - 0 - } -} - -fn get_enclave(api: &Api, eindex: u64) -> Option>> { - api.get_storage_map("SubstrateeRegistry", "EnclaveRegistry", eindex, None) - .unwrap() -} diff --git a/primitives/node/src/api.rs b/primitives/node/src/api.rs new file mode 100644 index 0000000000..f72cf0f8db --- /dev/null +++ b/primitives/node/src/api.rs @@ -0,0 +1,71 @@ +use sp_core::crypto::Pair; + +pub use my_node_runtime::{ + substratee_registry::{Enclave as EnclaveGen, ShardIdentifier}, + AccountId, +}; +use substrate_api_client::Api; +use sp_runtime::MultiSignature; + + +pub type Enclave = EnclaveGen>; +pub type IpfsHash = [u8; 46]; + +/// ApiClient extension that enables communication with the `substratee-registry` pallet. +pub trait SubstrateeRegistryApi { + fn enclave(&self, index: u64) -> Option; + fn worker_for_shard(&self, shard: &ShardIdentifier) -> Option; + fn enclave_count(&self) -> u64; + fn latest_ipfs_hash(&self, shard: &ShardIdentifier) -> Option; +} + +/// Simple ApiClient extension that contains some convenience methods. +pub trait ApiClientExt { + fn get_nonce_of(&self, who: &AccountId) -> u32; + fn get_free_balance(&self, who: &AccountId) -> u128; +} + +impl ApiClientExt for Api

+ where + MultiSignature: From +{ + fn get_nonce_of(&self, who: &AccountId) -> u32 { + self.get_account_info(who) + .unwrap() + .map_or_else(|| 0, |info| info.nonce) + } + + fn get_free_balance(&self, who: &AccountId) -> u128 { + self.get_account_data(who) + .unwrap() + .map_or_else(|| 0, |data| data.free) + } +} + + +impl SubstrateeRegistryApi for Api

+ where + MultiSignature: From +{ + fn enclave(&self, index: u64) -> Option { + self.get_storage_map("SubstrateeRegistry", "EnclaveRegistry", index, None) + .unwrap() + } + + fn worker_for_shard(&self, shard: &ShardIdentifier) -> Option { + self.get_storage_map("SubstrateeRegistry", "WorkerForShard", shard, None) + .unwrap() + .and_then(|w| self.enclave(w)) + } + + fn enclave_count(&self) -> u64 { + self.get_storage_value("SubstrateeRegistry", "EnclaveCount", None) + .unwrap() + .unwrap_or(0u64) + } + + fn latest_ipfs_hash(&self, shard: &ShardIdentifier) -> Option<[u8; 46]> { + self.get_storage_map("SubstrateeRegistry", "LatestIPFSHash", shard, None) + .unwrap() + } +} \ No newline at end of file diff --git a/primitives/node/src/lib.rs b/primitives/node/src/lib.rs index 0d010b095c..dd0a01ea15 100644 --- a/primitives/node/src/lib.rs +++ b/primitives/node/src/lib.rs @@ -12,7 +12,7 @@ use sp_core::H256; pub type ShardIdentifier = H256; pub type BlockNumber = u32; -// Note in the substratee-pallet-registry this is a struct. But for the coded this does not matter. +// Note in the substratee-pallet-registry this is a struct. But for the codec this does not matter. #[derive(Encode, Decode, Default, Clone, PartialEq, Eq, Debug)] pub struct Request { pub shard: ShardIdentifier, @@ -24,74 +24,7 @@ pub type ShieldFundsFn = ([u8; 2], Vec, u128, ShardIdentifier); pub type CallWorkerFn = ([u8; 2], Request); #[cfg(feature = "std")] -pub mod calls { - pub use my_node_runtime::{ - substratee_registry::{Enclave, ShardIdentifier}, - AccountId, - }; - use sp_core::crypto::Pair; - use sp_runtime::MultiSignature; +pub mod api; - pub fn get_worker_info( - api: &substrate_api_client::Api

, - index: u64, - ) -> Option>> - where - MultiSignature: From, - { - api.get_storage_map("SubstrateeRegistry", "EnclaveRegistry", index, None) - .unwrap() - } - - pub fn get_worker_for_shard( - api: &substrate_api_client::Api

, - shard: &ShardIdentifier, - ) -> Option>> - where - MultiSignature: From, - { - api.get_storage_map("SubstrateeRegistry", "WorkerForShard", shard, None) - .unwrap() - .and_then(|w| get_worker_info(&api, w)) - } - - pub fn get_worker_amount(api: &substrate_api_client::Api

) -> Option - where - MultiSignature: From, - { - api.get_storage_value("SubstrateeRegistry", "EnclaveCount", None) - .unwrap() - } - - pub fn get_first_worker_that_is_not_equal_to_self( - api: &substrate_api_client::Api

, - self_account: &AccountId, - ) -> Option>> - where - MultiSignature: From, - { - // the registry starts indexing its map at one - for n in 1..=api - .get_storage_value("SubstrateeRegistry", "EnclaveCount", None) - .ok()? - .unwrap() - { - let worker = get_worker_info(api, n).unwrap(); - if &worker.pubkey != self_account { - return Some(worker); - } - } - None - } - - pub fn get_latest_state( - api: &substrate_api_client::Api

, - shard: &ShardIdentifier, - ) -> Option<[u8; 46]> - where - MultiSignature: From, - { - api.get_storage_map("SubstrateeRegistry", "LatestIPFSHash", shard, None) - .unwrap() - } -} +#[cfg(feature = "std")] +pub use api::*; \ No newline at end of file diff --git a/worker/src/main.rs b/worker/src/main.rs index b69703901b..1180be5058 100644 --- a/worker/src/main.rs +++ b/worker/src/main.rs @@ -55,6 +55,7 @@ use enclave::worker_api_direct_server::start_worker_api_direct_server; use sp_finality_grandpa::{AuthorityList, VersionedAuthorityList, GRANDPA_AUTHORITIES_KEY}; use std::time::{Duration, SystemTime}; +use substratee_node_primitives::ApiClientExt; use substratee_worker_primitives::block::SignedBlock as SignedSidechainBlock; use config::Config; use utils::extract_shard; @@ -257,7 +258,7 @@ fn worker( println!("[!] skipping remote attestation. will not register this enclave on chain"); } else { // get enclaves's account nonce - let nonce = get_nonce(&api, &tee_accountid); + let nonce = api.get_nonce_of(&tee_accountid); info!("Enclave nonce = {:?}", nonce); let uxt = @@ -545,7 +546,7 @@ pub fn produce_blocks( blocks_to_sync[0].block.header.number as usize }; for chunk in blocks_to_sync.chunks(BLOCK_SYNC_BATCH_SIZE as usize) { - let tee_nonce = get_nonce(&api, &tee_accountid); + let tee_nonce = api.get_nonce_of(&tee_accountid); // Produce blocks if let Err(e) = enclave_produce_blocks(eid, chunk.to_vec(), tee_nonce) { error!("{}", e); @@ -605,13 +606,13 @@ fn ensure_account_has_funds(api: &mut Api, accountid: &AccountId3 let alice_acc = AccountId32::from(*alice.public().as_array_ref()); info!("encoding Alice's AccountId = {:?}", alice_acc.encode()); - let free = get_balance(&api, &alice_acc); + let free = api.get_free_balance(&alice_acc); info!(" Alice's free balance = {:?}", free); - let nonce = get_nonce(&api, &alice_acc); + let nonce = api.get_nonce_of(&alice_acc); info!(" Alice's Account Nonce is {}", nonce); // check account balance - let free = get_balance(&api, &accountid); + let free = api.get_free_balance(&accountid); info!("TEE's free balance = {:?}", free); if free < 1_000_000_000_000 { @@ -626,29 +627,13 @@ fn ensure_account_has_funds(api: &mut Api, accountid: &AccountId3 info!("[<] Extrinsic got finalized. Hash: {:?}\n", xt_hash); //verify funds have arrived - let free = get_balance(&api, &accountid); + let free = api.get_free_balance(&accountid); info!("TEE's NEW free balance = {:?}", free); api.signer = signer_orig; } } -fn get_nonce(api: &Api, who: &AccountId32) -> u32 { - if let Some(info) = api.get_account_info(who).unwrap() { - info.nonce - } else { - 0 - } -} - -fn get_balance(api: &Api, who: &AccountId32) -> u128 { - if let Some(data) = api.get_account_data(who).unwrap() { - data.free - } else { - 0 - } -} - pub fn check_files() { debug!("*** Check files"); let files = vec![ From 6b258e6840a68cbeeb368e4129baaa654b8f54f2 Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Tue, 8 Jun 2021 11:56:00 +0200 Subject: [PATCH 03/14] Cargo.lock after rebase --- Cargo.lock | 207 +---------------------------------------------------- 1 file changed, 2 insertions(+), 205 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1476b99ed9..3bbedc0c5a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -340,15 +340,6 @@ version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" -[[package]] -name = "beef" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6736e2428df2ca2848d846c43e88745121a6654696e349ce0054a420815a7409" -dependencies = [ - "serde", -] - [[package]] name = "bitflags" version = "1.2.1" @@ -458,15 +449,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "771fe0050b883fcc3ea2359b1a96bcfbc090b7116eae7c3c512c7a083fdf23d3" -[[package]] -name = "bstr" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a40b47ad93e1a5404e6c18dec46b628214fee441c70f4ab5d6942142cc268a3d" -dependencies = [ - "memchr 2.3.4", -] - [[package]] name = "build-helper" version = "0.1.1" @@ -699,15 +681,6 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ea221b5284a47e40033bf9b66f35f984ec0ea2931eb03505246cd27a963f981b" -[[package]] -name = "cpufeatures" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed00c67cb5d0a7d64a44f6ad2668db7e7530311dd53ea79bcd4fb022c64911c8" -dependencies = [ - "libc", -] - [[package]] name = "cpuid-bool" version = "0.1.2" @@ -1445,19 +1418,6 @@ version = "0.23.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f6503fe142514ca4799d4c26297c4248239fe8838d827db6bd6065c6ed29a6ce" -[[package]] -name = "globset" -version = "0.4.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c152169ef1e421390738366d2f796655fec62621dabbd0fd476f905934061e4a" -dependencies = [ - "aho-corasick 0.7.15", - "bstr", - "fnv", - "log 0.4.14 (registry+https://github.com/rust-lang/crates.io-index)", - "regex 1.4.5", -] - [[package]] name = "gloo-timers" version = "0.2.1" @@ -1986,97 +1946,6 @@ dependencies = [ "serde", ] -[[package]] -name = "jsonrpsee" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "316a89048d2ea5530ab5502aa31e1128f6429b524a37e4c0bc54903bcdf3d342" -dependencies = [ - "jsonrpsee-http-server", - "jsonrpsee-utils", - "jsonrpsee-ws-server", -] - -[[package]] -name = "jsonrpsee-http-server" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d22372378f63f7d16de453e786afc740fca5ee80bd260be024a616b6ac2cefe5" -dependencies = [ - "futures-channel", - "futures-util", - "globset", - "hyper 0.14.5", - "jsonrpsee-types", - "jsonrpsee-utils", - "lazy_static", - "log 0.4.14 (registry+https://github.com/rust-lang/crates.io-index)", - "serde", - "serde_json", - "socket2", - "thiserror", - "tokio 1.6.1", - "unicase", -] - -[[package]] -name = "jsonrpsee-types" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0cf7bd4e93b3b56e59131de7f24afbea871faf914e97bcdd942c86927ab0172" -dependencies = [ - "async-trait", - "beef", - "futures-channel", - "futures-util", - "hyper 0.14.5", - "log 0.4.14 (registry+https://github.com/rust-lang/crates.io-index)", - "serde", - "serde_json", - "soketto", - "thiserror", -] - -[[package]] -name = "jsonrpsee-utils" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "47554ecaacb479285da68799d9b6afc258c32b332cc8b85829c6a9304ee98776" -dependencies = [ - "futures-channel", - "futures-util", - "hyper 0.14.5", - "jsonrpsee-types", - "log 0.4.14 (registry+https://github.com/rust-lang/crates.io-index)", - "parking_lot 0.11.1", - "rand 0.8.3", - "rustc-hash", - "serde", - "serde_json", - "thiserror", -] - -[[package]] -name = "jsonrpsee-ws-server" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b512c3c679a89d20f97802f69188a2d01f6234491b7513076e21e8424efccafe" -dependencies = [ - "futures-channel", - "futures-util", - "jsonrpsee-types", - "jsonrpsee-utils", - "log 0.4.14 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-hash", - "serde", - "serde_json", - "soketto", - "thiserror", - "tokio 1.6.1", - "tokio-stream", - "tokio-util 0.6.5", -] - [[package]] name = "keccak" version = "0.1.0" @@ -4340,19 +4209,6 @@ dependencies = [ "opaque-debug 0.2.3", ] -[[package]] -name = "sha-1" -version = "0.9.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c4cfa741c5832d0ef7fab46cabed29c2aae926db0b11bb2069edd8db5e64e16" -dependencies = [ - "block-buffer 0.9.0", - "cfg-if 1.0.0", - "cpufeatures", - "digest 0.9.0", - "opaque-debug 0.3.0", -] - [[package]] name = "sha1" version = "0.5.0" @@ -4479,21 +4335,6 @@ dependencies = [ "winapi 0.3.9", ] -[[package]] -name = "soketto" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4919971d141dbadaa0e82b5d369e2d7666c98e4625046140615ca363e50d4daa" -dependencies = [ - "base64 0.13.0", - "bytes 1.0.1", - "futures 0.3.13", - "httparse", - "log 0.4.14 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.8.3", - "sha-1 0.9.6", -] - [[package]] name = "sp-api" version = "3.0.0" @@ -5392,21 +5233,6 @@ dependencies = [ "sp-runtime", ] -[[package]] -name = "substratee-worker-rpc" -version = "0.8.0" -dependencies = [ - "anyhow", - "env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", - "futures-util", - "jsonrpsee", - "log 0.4.14 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json", - "soketto", - "tokio 1.6.1", - "tokio-util 0.6.5", -] - [[package]] name = "subtle" version = "1.0.0" @@ -5633,7 +5459,7 @@ dependencies = [ "futures-core", "memchr 2.3.4", "pin-project-lite 0.1.12", - "tokio-macros 0.2.6", + "tokio-macros", ] [[package]] @@ -5647,13 +5473,7 @@ dependencies = [ "libc", "memchr 2.3.4", "mio 0.7.11", - "num_cpus", - "once_cell 1.7.2", - "parking_lot 0.11.1", "pin-project-lite 0.2.6", - "signal-hook-registry", - "tokio-macros 1.2.0", - "winapi 0.3.9", ] [[package]] @@ -5667,17 +5487,6 @@ dependencies = [ "syn 1.0.68", ] -[[package]] -name = "tokio-macros" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c49e3df43841dafb86046472506755d8501c5615673955f6aa17181125d13c37" -dependencies = [ - "proc-macro2", - "quote 1.0.9", - "syn 1.0.68", -] - [[package]] name = "tokio-native-tls" version = "0.3.0" @@ -5688,17 +5497,6 @@ dependencies = [ "tokio 1.6.1", ] -[[package]] -name = "tokio-stream" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8864d706fdb3cc0843a49647ac892720dac98a6eeb818b77190592cf4994066" -dependencies = [ - "futures-core", - "pin-project-lite 0.2.6", - "tokio 1.6.1", -] - [[package]] name = "tokio-util" version = "0.3.1" @@ -5721,7 +5519,6 @@ checksum = "5143d049e85af7fbc36f5454d990e62c2df705b3589f123b71f441b6b59f443f" dependencies = [ "bytes 1.0.1", "futures-core", - "futures-io", "futures-sink", "log 0.4.14 (registry+https://github.com/rust-lang/crates.io-index)", "pin-project-lite 0.2.6", @@ -6325,7 +6122,7 @@ dependencies = [ "mio-extras", "openssl", "rand 0.7.3", - "sha-1 0.8.2", + "sha-1", "slab", "url 2.2.1", ] From 3a86bda5983c893225c4c01ae81c193ddd2f6db6 Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Tue, 8 Jun 2021 12:39:39 +0200 Subject: [PATCH 04/14] [primitives/node] api introduce errors --- client/src/main.rs | 5 ++-- primitives/node/src/api.rs | 49 +++++++++++++++++--------------------- worker/src/main.rs | 8 +++---- 3 files changed, 29 insertions(+), 33 deletions(-) diff --git a/client/src/main.rs b/client/src/main.rs index adffcd40a3..33cc59f97f 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -43,6 +43,7 @@ use my_node_runtime::{ substratee_registry::Request, AccountId, BalancesCall, Call, Event, Hash, Signature, }; + use sp_core::{crypto::Ss58Codec, sr25519 as sr25519_core, Pair, H256}; use sp_runtime::{ traits::{IdentifyAccount, Verify}, @@ -294,10 +295,10 @@ fn main() { .description("query enclave registry and list all workers") .runner(|_args: &str, matches: &ArgMatches<'_>| { let api = get_chain_api(matches); - let wcount = api.enclave_count(); + let wcount = api.enclave_count().unwrap(); println!("number of workers registered: {}", wcount); for w in 1..=wcount { - let enclave = api.enclave(w); + let enclave = api.enclave(w).unwrap(); if enclave.is_none() { println!("error reading enclave data"); continue; diff --git a/primitives/node/src/api.rs b/primitives/node/src/api.rs index f72cf0f8db..0931303e67 100644 --- a/primitives/node/src/api.rs +++ b/primitives/node/src/api.rs @@ -4,41 +4,40 @@ pub use my_node_runtime::{ substratee_registry::{Enclave as EnclaveGen, ShardIdentifier}, AccountId, }; -use substrate_api_client::Api; +use substrate_api_client::{Api, ApiClientError}; use sp_runtime::MultiSignature; +pub type ApiResult = Result; pub type Enclave = EnclaveGen>; pub type IpfsHash = [u8; 46]; /// ApiClient extension that enables communication with the `substratee-registry` pallet. pub trait SubstrateeRegistryApi { - fn enclave(&self, index: u64) -> Option; - fn worker_for_shard(&self, shard: &ShardIdentifier) -> Option; - fn enclave_count(&self) -> u64; - fn latest_ipfs_hash(&self, shard: &ShardIdentifier) -> Option; + fn enclave(&self, index: u64) -> ApiResult>; + fn worker_for_shard(&self, shard: &ShardIdentifier) -> ApiResult>; + fn enclave_count(&self) -> ApiResult; + fn latest_ipfs_hash(&self, shard: &ShardIdentifier) -> ApiResult>; } /// Simple ApiClient extension that contains some convenience methods. pub trait ApiClientExt { - fn get_nonce_of(&self, who: &AccountId) -> u32; - fn get_free_balance(&self, who: &AccountId) -> u128; + fn get_nonce_of(&self, who: &AccountId) -> ApiResult; + fn get_free_balance(&self, who: &AccountId) -> ApiResult; } impl ApiClientExt for Api

where MultiSignature: From { - fn get_nonce_of(&self, who: &AccountId) -> u32 { - self.get_account_info(who) - .unwrap() - .map_or_else(|| 0, |info| info.nonce) + fn get_nonce_of(&self, who: &AccountId) -> ApiResult { + Ok(self.get_account_info(who)? + .map_or_else(|| 0, |info| info.nonce)) } - fn get_free_balance(&self, who: &AccountId) -> u128 { - self.get_account_data(who) - .unwrap() - .map_or_else(|| 0, |data| data.free) + fn get_free_balance(&self, who: &AccountId) -> ApiResult { + Ok(self.get_account_data(who)? + .map_or_else(|| 0, |data| data.free)) } } @@ -47,25 +46,21 @@ impl SubstrateeRegistryApi for Api

where MultiSignature: From { - fn enclave(&self, index: u64) -> Option { + fn enclave(&self, index: u64) -> ApiResult> { self.get_storage_map("SubstrateeRegistry", "EnclaveRegistry", index, None) - .unwrap() } - fn worker_for_shard(&self, shard: &ShardIdentifier) -> Option { - self.get_storage_map("SubstrateeRegistry", "WorkerForShard", shard, None) - .unwrap() - .and_then(|w| self.enclave(w)) + fn worker_for_shard(&self, shard: &ShardIdentifier) -> ApiResult> { + self.get_storage_map("SubstrateeRegistry", "WorkerForShard", shard, None)? + .map_or_else(|| Ok(None), |w_index| self.enclave(w_index)) } - fn enclave_count(&self) -> u64 { - self.get_storage_value("SubstrateeRegistry", "EnclaveCount", None) - .unwrap() - .unwrap_or(0u64) + fn enclave_count(&self) -> ApiResult { + Ok(self.get_storage_value("SubstrateeRegistry", "EnclaveCount", None)? + .unwrap_or(0u64)) } - fn latest_ipfs_hash(&self, shard: &ShardIdentifier) -> Option<[u8; 46]> { + fn latest_ipfs_hash(&self, shard: &ShardIdentifier) -> ApiResult> { self.get_storage_map("SubstrateeRegistry", "LatestIPFSHash", shard, None) - .unwrap() } } \ No newline at end of file diff --git a/worker/src/main.rs b/worker/src/main.rs index 1180be5058..d4b09eb853 100644 --- a/worker/src/main.rs +++ b/worker/src/main.rs @@ -258,7 +258,7 @@ fn worker( println!("[!] skipping remote attestation. will not register this enclave on chain"); } else { // get enclaves's account nonce - let nonce = api.get_nonce_of(&tee_accountid); + let nonce = api.get_nonce_of(&tee_accountid).unwrap(); info!("Enclave nonce = {:?}", nonce); let uxt = @@ -546,7 +546,7 @@ pub fn produce_blocks( blocks_to_sync[0].block.header.number as usize }; for chunk in blocks_to_sync.chunks(BLOCK_SYNC_BATCH_SIZE as usize) { - let tee_nonce = api.get_nonce_of(&tee_accountid); + let tee_nonce = api.get_nonce_of(&tee_accountid).unwrap(); // Produce blocks if let Err(e) = enclave_produce_blocks(eid, chunk.to_vec(), tee_nonce) { error!("{}", e); @@ -608,11 +608,11 @@ fn ensure_account_has_funds(api: &mut Api, accountid: &AccountId3 let free = api.get_free_balance(&alice_acc); info!(" Alice's free balance = {:?}", free); - let nonce = api.get_nonce_of(&alice_acc); + let nonce = api.get_nonce_of(&alice_acc).unwrap(); info!(" Alice's Account Nonce is {}", nonce); // check account balance - let free = api.get_free_balance(&accountid); + let free = api.get_free_balance(&accountid).unwrap(); info!("TEE's free balance = {:?}", free); if free < 1_000_000_000_000 { From a525be4e6749fe4b8ff0fba8d0366eab88a41091 Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Tue, 8 Jun 2021 12:41:12 +0200 Subject: [PATCH 05/14] [primitives/node] rename api to api_ext --- primitives/node/src/{api.rs => api_ext.rs} | 0 primitives/node/src/lib.rs | 4 ++-- 2 files changed, 2 insertions(+), 2 deletions(-) rename primitives/node/src/{api.rs => api_ext.rs} (100%) diff --git a/primitives/node/src/api.rs b/primitives/node/src/api_ext.rs similarity index 100% rename from primitives/node/src/api.rs rename to primitives/node/src/api_ext.rs diff --git a/primitives/node/src/lib.rs b/primitives/node/src/lib.rs index dd0a01ea15..470fa7568a 100644 --- a/primitives/node/src/lib.rs +++ b/primitives/node/src/lib.rs @@ -24,7 +24,7 @@ pub type ShieldFundsFn = ([u8; 2], Vec, u128, ShardIdentifier); pub type CallWorkerFn = ([u8; 2], Request); #[cfg(feature = "std")] -pub mod api; +pub mod api_ext; #[cfg(feature = "std")] -pub use api::*; \ No newline at end of file +pub use api_ext::*; \ No newline at end of file From f832126e37dbf95efa5a791648a5f8896bad2c23 Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Tue, 8 Jun 2021 12:52:20 +0200 Subject: [PATCH 06/14] [primitives/node] add `all_enclaves` to substratee_registry api. --- primitives/node/src/api_ext.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/primitives/node/src/api_ext.rs b/primitives/node/src/api_ext.rs index 0931303e67..8bf8357942 100644 --- a/primitives/node/src/api_ext.rs +++ b/primitives/node/src/api_ext.rs @@ -15,6 +15,7 @@ pub type IpfsHash = [u8; 46]; /// ApiClient extension that enables communication with the `substratee-registry` pallet. pub trait SubstrateeRegistryApi { fn enclave(&self, index: u64) -> ApiResult>; + fn all_enclaves(&self) -> ApiResult>; fn worker_for_shard(&self, shard: &ShardIdentifier) -> ApiResult>; fn enclave_count(&self) -> ApiResult; fn latest_ipfs_hash(&self, shard: &ShardIdentifier) -> ApiResult>; @@ -50,6 +51,15 @@ impl SubstrateeRegistryApi for Api

self.get_storage_map("SubstrateeRegistry", "EnclaveRegistry", index, None) } + fn all_enclaves(&self) -> ApiResult> { + let count = self.enclave_count()?; + let mut enclaves = Vec::with_capacity(count as usize); + for n in 1..=count { + enclaves.push(self.enclave(n)?) + } + Ok(enclaves) + } + fn worker_for_shard(&self, shard: &ShardIdentifier) -> ApiResult> { self.get_storage_map("SubstrateeRegistry", "WorkerForShard", shard, None)? .map_or_else(|| Ok(None), |w_index| self.enclave(w_index)) From d14c887934767963501b4b47383149cad2395e39 Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Tue, 8 Jun 2021 12:54:09 +0200 Subject: [PATCH 07/14] [primitives/node] change order of methods in api traits --- primitives/node/src/api_ext.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/primitives/node/src/api_ext.rs b/primitives/node/src/api_ext.rs index 8bf8357942..42200f1e95 100644 --- a/primitives/node/src/api_ext.rs +++ b/primitives/node/src/api_ext.rs @@ -15,9 +15,9 @@ pub type IpfsHash = [u8; 46]; /// ApiClient extension that enables communication with the `substratee-registry` pallet. pub trait SubstrateeRegistryApi { fn enclave(&self, index: u64) -> ApiResult>; + fn enclave_count(&self) -> ApiResult; fn all_enclaves(&self) -> ApiResult>; fn worker_for_shard(&self, shard: &ShardIdentifier) -> ApiResult>; - fn enclave_count(&self) -> ApiResult; fn latest_ipfs_hash(&self, shard: &ShardIdentifier) -> ApiResult>; } @@ -51,6 +51,11 @@ impl SubstrateeRegistryApi for Api

self.get_storage_map("SubstrateeRegistry", "EnclaveRegistry", index, None) } + fn enclave_count(&self) -> ApiResult { + Ok(self.get_storage_value("SubstrateeRegistry", "EnclaveCount", None)? + .unwrap_or(0u64)) + } + fn all_enclaves(&self) -> ApiResult> { let count = self.enclave_count()?; let mut enclaves = Vec::with_capacity(count as usize); @@ -65,11 +70,6 @@ impl SubstrateeRegistryApi for Api

.map_or_else(|| Ok(None), |w_index| self.enclave(w_index)) } - fn enclave_count(&self) -> ApiResult { - Ok(self.get_storage_value("SubstrateeRegistry", "EnclaveCount", None)? - .unwrap_or(0u64)) - } - fn latest_ipfs_hash(&self, shard: &ShardIdentifier) -> ApiResult> { self.get_storage_map("SubstrateeRegistry", "LatestIPFSHash", shard, None) } From 01c98c76d0eebab71909a45c91fb618ac8470541 Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Tue, 8 Jun 2021 13:20:15 +0200 Subject: [PATCH 08/14] [primitives/node] api:fix `all_enclaves` --- primitives/node/src/api_ext.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/primitives/node/src/api_ext.rs b/primitives/node/src/api_ext.rs index 42200f1e95..2c768cc395 100644 --- a/primitives/node/src/api_ext.rs +++ b/primitives/node/src/api_ext.rs @@ -56,11 +56,11 @@ impl SubstrateeRegistryApi for Api

.unwrap_or(0u64)) } - fn all_enclaves(&self) -> ApiResult> { + fn all_enclaves(&self) -> ApiResult> { let count = self.enclave_count()?; let mut enclaves = Vec::with_capacity(count as usize); for n in 1..=count { - enclaves.push(self.enclave(n)?) + enclaves.push(self.enclave(n)?.unwrap()) } Ok(enclaves) } From d4e507296725162becce3da3d8fc950aae025b0b Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Tue, 8 Jun 2021 14:35:44 +0200 Subject: [PATCH 09/14] [primitives/node/api] add `ChainApi` --- primitives/node/src/api_ext.rs | 25 +++++++++++++++++++++++++ worker/src/main.rs | 11 +++-------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/primitives/node/src/api_ext.rs b/primitives/node/src/api_ext.rs index 2c768cc395..000c2b9819 100644 --- a/primitives/node/src/api_ext.rs +++ b/primitives/node/src/api_ext.rs @@ -6,6 +6,8 @@ pub use my_node_runtime::{ }; use substrate_api_client::{Api, ApiClientError}; use sp_runtime::MultiSignature; +use my_node_runtime::SignedBlock; +use sp_core::H256; pub type ApiResult = Result; @@ -27,6 +29,12 @@ pub trait ApiClientExt { fn get_free_balance(&self, who: &AccountId) -> ApiResult; } +/// ApiClient extension that simplifies chain data access. +pub trait ChainApi { + fn last_finalized_block(&self) -> ApiResult>; + fn signed_block(&self, hash: Option) -> ApiResult>; +} + impl ApiClientExt for Api

where MultiSignature: From @@ -42,6 +50,23 @@ impl ApiClientExt for Api

} } +impl ChainApi for Api

+ where + MultiSignature: From +{ + fn last_finalized_block(&self) -> ApiResult> { + self.get_finalized_head()? + .map_or_else(|| Ok(None), |hash| self.signed_block(Some(hash))) + } + + fn signed_block(&self, hash: Option) -> ApiResult> { + // Even though this is only a wrapper here, we want to have this in the trait + // to be able to be generic over the trait and mock the `signed_block` method + // in tests. + self.get_signed_block(hash) + } +} + impl SubstrateeRegistryApi for Api

where diff --git a/worker/src/main.rs b/worker/src/main.rs index d4b09eb853..6fa9a8beac 100644 --- a/worker/src/main.rs +++ b/worker/src/main.rs @@ -55,7 +55,7 @@ use enclave::worker_api_direct_server::start_worker_api_direct_server; use sp_finality_grandpa::{AuthorityList, VersionedAuthorityList, GRANDPA_AUTHORITIES_KEY}; use std::time::{Duration, SystemTime}; -use substratee_node_primitives::ApiClientExt; +use substratee_node_primitives::{ApiClientExt, ChainApi}; use substratee_worker_primitives::block::SignedBlock as SignedSidechainBlock; use config::Config; use utils::extract_shard; @@ -493,12 +493,7 @@ pub fn produce_blocks( ) -> Header { // obtain latest finalized block from layer one debug!("Getting current head"); - let curr_head: SignedBlock = api - .get_finalized_head() - .unwrap() - .map(|hash| api.get_signed_block(Some(hash)).unwrap()) - .unwrap() - .unwrap(); + let curr_head: SignedBlock = api.last_finalized_block().unwrap().unwrap(); let mut blocks_to_sync = Vec::::new(); @@ -522,7 +517,7 @@ pub fn produce_blocks( while head.block.header.parent_hash != last_synced_head.hash() { debug!("Getting head of hash: {:?}", head.block.header.parent_hash); head = api - .get_signed_block(Some(head.block.header.parent_hash)) + .signed_block(Some(head.block.header.parent_hash)) .unwrap() .unwrap(); blocks_to_sync.push(head.clone()); From 1bb4f2ee9fbf342f3000c2219e4744062682bc3a Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Tue, 8 Jun 2021 14:51:27 +0200 Subject: [PATCH 10/14] [primitives/node] divided api extensions into submodules --- primitives/node/src/api_ext.rs | 101 ------------------ primitives/node/src/api_ext/account.rs | 29 +++++ primitives/node/src/api_ext/chain.rs | 30 ++++++ primitives/node/src/api_ext/mod.rs | 13 +++ .../node/src/api_ext/substratee_registry.rs | 51 +++++++++ primitives/node/src/lib.rs | 15 ++- worker/src/main.rs | 2 +- 7 files changed, 131 insertions(+), 110 deletions(-) delete mode 100644 primitives/node/src/api_ext.rs create mode 100644 primitives/node/src/api_ext/account.rs create mode 100644 primitives/node/src/api_ext/chain.rs create mode 100644 primitives/node/src/api_ext/mod.rs create mode 100644 primitives/node/src/api_ext/substratee_registry.rs diff --git a/primitives/node/src/api_ext.rs b/primitives/node/src/api_ext.rs deleted file mode 100644 index 000c2b9819..0000000000 --- a/primitives/node/src/api_ext.rs +++ /dev/null @@ -1,101 +0,0 @@ -use sp_core::crypto::Pair; - -pub use my_node_runtime::{ - substratee_registry::{Enclave as EnclaveGen, ShardIdentifier}, - AccountId, -}; -use substrate_api_client::{Api, ApiClientError}; -use sp_runtime::MultiSignature; -use my_node_runtime::SignedBlock; -use sp_core::H256; - - -pub type ApiResult = Result; -pub type Enclave = EnclaveGen>; -pub type IpfsHash = [u8; 46]; - -/// ApiClient extension that enables communication with the `substratee-registry` pallet. -pub trait SubstrateeRegistryApi { - fn enclave(&self, index: u64) -> ApiResult>; - fn enclave_count(&self) -> ApiResult; - fn all_enclaves(&self) -> ApiResult>; - fn worker_for_shard(&self, shard: &ShardIdentifier) -> ApiResult>; - fn latest_ipfs_hash(&self, shard: &ShardIdentifier) -> ApiResult>; -} - -/// Simple ApiClient extension that contains some convenience methods. -pub trait ApiClientExt { - fn get_nonce_of(&self, who: &AccountId) -> ApiResult; - fn get_free_balance(&self, who: &AccountId) -> ApiResult; -} - -/// ApiClient extension that simplifies chain data access. -pub trait ChainApi { - fn last_finalized_block(&self) -> ApiResult>; - fn signed_block(&self, hash: Option) -> ApiResult>; -} - -impl ApiClientExt for Api

- where - MultiSignature: From -{ - fn get_nonce_of(&self, who: &AccountId) -> ApiResult { - Ok(self.get_account_info(who)? - .map_or_else(|| 0, |info| info.nonce)) - } - - fn get_free_balance(&self, who: &AccountId) -> ApiResult { - Ok(self.get_account_data(who)? - .map_or_else(|| 0, |data| data.free)) - } -} - -impl ChainApi for Api

- where - MultiSignature: From -{ - fn last_finalized_block(&self) -> ApiResult> { - self.get_finalized_head()? - .map_or_else(|| Ok(None), |hash| self.signed_block(Some(hash))) - } - - fn signed_block(&self, hash: Option) -> ApiResult> { - // Even though this is only a wrapper here, we want to have this in the trait - // to be able to be generic over the trait and mock the `signed_block` method - // in tests. - self.get_signed_block(hash) - } -} - - -impl SubstrateeRegistryApi for Api

- where - MultiSignature: From -{ - fn enclave(&self, index: u64) -> ApiResult> { - self.get_storage_map("SubstrateeRegistry", "EnclaveRegistry", index, None) - } - - fn enclave_count(&self) -> ApiResult { - Ok(self.get_storage_value("SubstrateeRegistry", "EnclaveCount", None)? - .unwrap_or(0u64)) - } - - fn all_enclaves(&self) -> ApiResult> { - let count = self.enclave_count()?; - let mut enclaves = Vec::with_capacity(count as usize); - for n in 1..=count { - enclaves.push(self.enclave(n)?.unwrap()) - } - Ok(enclaves) - } - - fn worker_for_shard(&self, shard: &ShardIdentifier) -> ApiResult> { - self.get_storage_map("SubstrateeRegistry", "WorkerForShard", shard, None)? - .map_or_else(|| Ok(None), |w_index| self.enclave(w_index)) - } - - fn latest_ipfs_hash(&self, shard: &ShardIdentifier) -> ApiResult> { - self.get_storage_map("SubstrateeRegistry", "LatestIPFSHash", shard, None) - } -} \ No newline at end of file diff --git a/primitives/node/src/api_ext/account.rs b/primitives/node/src/api_ext/account.rs new file mode 100644 index 0000000000..6c07f5c5fe --- /dev/null +++ b/primitives/node/src/api_ext/account.rs @@ -0,0 +1,29 @@ +use sp_core::crypto::Pair; +use my_node_runtime::AccountId; +use substrate_api_client::Api; +use sp_runtime::MultiSignature; + +use crate::ApiResult; + + +/// ApiClient extension that contains some convenience methods around accounts. +pub trait AccountApi { + fn get_nonce_of(&self, who: &AccountId) -> ApiResult; + fn get_free_balance(&self, who: &AccountId) -> ApiResult; +} + +impl AccountApi for Api

+ where + MultiSignature: From +{ + fn get_nonce_of(&self, who: &AccountId) -> ApiResult { + Ok(self.get_account_info(who)? + .map_or_else(|| 0, |info| info.nonce)) + } + + fn get_free_balance(&self, who: &AccountId) -> ApiResult { + Ok(self.get_account_data(who)? + .map_or_else(|| 0, |data| data.free)) + } +} + diff --git a/primitives/node/src/api_ext/chain.rs b/primitives/node/src/api_ext/chain.rs new file mode 100644 index 0000000000..ad4ff1bc62 --- /dev/null +++ b/primitives/node/src/api_ext/chain.rs @@ -0,0 +1,30 @@ +use sp_core::Pair; +use sp_runtime::MultiSignature; +use my_node_runtime::SignedBlock; +use primitive_types::H256; +use substrate_api_client::Api; + +use crate::ApiResult; + +/// ApiClient extension that simplifies chain data access. +pub trait ChainApi { + fn last_finalized_block(&self) -> ApiResult>; + fn signed_block(&self, hash: Option) -> ApiResult>; +} + +impl ChainApi for Api

+ where + MultiSignature: From +{ + fn last_finalized_block(&self) -> ApiResult> { + self.get_finalized_head()? + .map_or_else(|| Ok(None), |hash| self.signed_block(Some(hash))) + } + + fn signed_block(&self, hash: Option) -> ApiResult> { + // Even though this is only a wrapper here, we want to have this in the trait + // to be able to be generic over the trait and mock the `signed_block` method + // in tests. + self.get_signed_block(hash) + } +} \ No newline at end of file diff --git a/primitives/node/src/api_ext/mod.rs b/primitives/node/src/api_ext/mod.rs new file mode 100644 index 0000000000..e09a276db0 --- /dev/null +++ b/primitives/node/src/api_ext/mod.rs @@ -0,0 +1,13 @@ +//! Some substrate-api-client extension traits. + +use substrate_api_client::ApiClientError; + +pub mod account; +pub mod substratee_registry; +pub mod chain; + +pub use account::*; +pub use substratee_registry::*; +pub use chain::*; + +pub type ApiResult = Result; diff --git a/primitives/node/src/api_ext/substratee_registry.rs b/primitives/node/src/api_ext/substratee_registry.rs new file mode 100644 index 0000000000..d8f9a6b43a --- /dev/null +++ b/primitives/node/src/api_ext/substratee_registry.rs @@ -0,0 +1,51 @@ +use my_node_runtime::{AccountId, substratee_registry::Enclave as EnclaveGen}; +use sp_runtime::MultiSignature; +use substrate_api_client::Api; +use sp_core::Pair; + +use crate::{ShardIdentifier, ApiResult}; + +pub type Enclave = EnclaveGen>; +pub type IpfsHash = [u8; 46]; + +/// ApiClient extension that enables communication with the `substratee-registry` pallet. +pub trait SubstrateeRegistryApi { + fn enclave(&self, index: u64) -> ApiResult>; + fn enclave_count(&self) -> ApiResult; + fn all_enclaves(&self) -> ApiResult>; + fn worker_for_shard(&self, shard: &ShardIdentifier) -> ApiResult>; + fn latest_ipfs_hash(&self, shard: &ShardIdentifier) -> ApiResult>; +} + + +impl SubstrateeRegistryApi for Api

+ where + MultiSignature: From +{ + fn enclave(&self, index: u64) -> ApiResult> { + self.get_storage_map("SubstrateeRegistry", "EnclaveRegistry", index, None) + } + + fn enclave_count(&self) -> ApiResult { + Ok(self.get_storage_value("SubstrateeRegistry", "EnclaveCount", None)? + .unwrap_or(0u64)) + } + + fn all_enclaves(&self) -> ApiResult> { + let count = self.enclave_count()?; + let mut enclaves = Vec::with_capacity(count as usize); + for n in 1..=count { + enclaves.push(self.enclave(n)?.unwrap()) + } + Ok(enclaves) + } + + fn worker_for_shard(&self, shard: &ShardIdentifier) -> ApiResult> { + self.get_storage_map("SubstrateeRegistry", "WorkerForShard", shard, None)? + .map_or_else(|| Ok(None), |w_index| self.enclave(w_index)) + } + + fn latest_ipfs_hash(&self, shard: &ShardIdentifier) -> ApiResult> { + self.get_storage_map("SubstrateeRegistry", "LatestIPFSHash", shard, None) + } +} \ No newline at end of file diff --git a/primitives/node/src/lib.rs b/primitives/node/src/lib.rs index 470fa7568a..f80c5b7a3c 100644 --- a/primitives/node/src/lib.rs +++ b/primitives/node/src/lib.rs @@ -1,13 +1,17 @@ #![cfg_attr(all(not(target_env = "sgx"), not(feature = "std")), no_std)] #![cfg_attr(target_env = "sgx", feature(rustc_private))] +use codec::{Decode, Encode}; #[cfg(feature = "sgx")] use sgx_tstd as std; - +use sp_core::H256; use std::vec::Vec; -use codec::{Decode, Encode}; -use sp_core::H256; +#[cfg(feature = "std")] +mod api_ext; + +#[cfg(feature = "std")] +pub use api_ext::*; pub type ShardIdentifier = H256; pub type BlockNumber = u32; @@ -23,8 +27,3 @@ pub type SubstrateeConfirmCallFn = ([u8; 2], ShardIdentifier, H256, Vec); pub type ShieldFundsFn = ([u8; 2], Vec, u128, ShardIdentifier); pub type CallWorkerFn = ([u8; 2], Request); -#[cfg(feature = "std")] -pub mod api_ext; - -#[cfg(feature = "std")] -pub use api_ext::*; \ No newline at end of file diff --git a/worker/src/main.rs b/worker/src/main.rs index 6fa9a8beac..08ce1e069c 100644 --- a/worker/src/main.rs +++ b/worker/src/main.rs @@ -55,7 +55,7 @@ use enclave::worker_api_direct_server::start_worker_api_direct_server; use sp_finality_grandpa::{AuthorityList, VersionedAuthorityList, GRANDPA_AUTHORITIES_KEY}; use std::time::{Duration, SystemTime}; -use substratee_node_primitives::{ApiClientExt, ChainApi}; +use substratee_node_primitives::{AccountApi, ChainApi}; use substratee_worker_primitives::block::SignedBlock as SignedSidechainBlock; use config::Config; use utils::extract_shard; From 6c3564445ae2ab73dfe915b2cc25926a679a76bf Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Tue, 8 Jun 2021 15:19:43 +0200 Subject: [PATCH 11/14] move api-client extensions to their own crate --- Cargo.lock | 13 +++++++++++++ Cargo.toml | 1 + client/Cargo.toml | 3 +++ client/src/main.rs | 2 +- primitives/api-client-extensions/Cargo.toml | 18 ++++++++++++++++++ .../src}/account.rs | 4 ++-- .../src}/chain.rs | 6 +++--- .../src/lib.rs} | 2 +- .../src}/substratee_registry.rs | 8 +++----- primitives/node/src/lib.rs | 9 +++++---- worker/Cargo.toml | 3 +++ worker/src/main.rs | 2 +- 12 files changed, 54 insertions(+), 17 deletions(-) create mode 100644 primitives/api-client-extensions/Cargo.toml rename primitives/{node/src/api_ext => api-client-extensions/src}/account.rs (94%) rename primitives/{node/src/api_ext => api-client-extensions/src}/chain.rs (91%) rename primitives/{node/src/api_ext/mod.rs => api-client-extensions/src/lib.rs} (81%) rename primitives/{node/src/api_ext => api-client-extensions/src}/substratee_registry.rs (87%) diff --git a/Cargo.lock b/Cargo.lock index 3bbedc0c5a..ac3a4a01c8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5040,6 +5040,17 @@ dependencies = [ "wasm-gc-api", ] +[[package]] +name = "substratee-api-client-extensions" +version = "0.8.0" +dependencies = [ + "parity-scale-codec 2.0.1", + "sp-core", + "sp-runtime", + "substrate-api-client", + "substratee-node-primitives", +] + [[package]] name = "substratee-client" version = "0.8.0" @@ -5069,6 +5080,7 @@ dependencies = [ "substrate-api-client", "substrate-bip39 0.3.1", "substrate-client-keystore", + "substratee-api-client-extensions", "substratee-node-primitives", "substratee-node-runtime", "substratee-stf", @@ -5194,6 +5206,7 @@ dependencies = [ "sp-keyring", "sp-runtime", "substrate-api-client", + "substratee-api-client-extensions", "substratee-node-primitives", "substratee-node-runtime", "substratee-settings", diff --git a/Cargo.toml b/Cargo.toml index 0155b92785..1f1aa586cf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [workspace] members = [ "client", + "primitives/api-client-extensions", "primitives/settings", "primitives/node", "primitives/worker", diff --git a/client/Cargo.toml b/client/Cargo.toml index f029755a09..4b8c818f44 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -66,6 +66,9 @@ path = "../primitives/worker" [dependencies.substratee-node-primitives] path = "../primitives/node" +[dependencies.substratee-api-client-extensions] +path = "../primitives/api-client-extensions" + [dependencies.substratee-stf] path = "../stf" diff --git a/client/src/main.rs b/client/src/main.rs index 33cc59f97f..43289a9cba 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -65,7 +65,7 @@ use substrate_api_client::{ use substrate_client_keystore::LocalKeystore; use substratee_stf::{ShardIdentifier, TrustedCallSigned, TrustedOperation}; use substratee_worker_api::direct_client::DirectApi as DirectWorkerApi; -use substratee_node_primitives::SubstrateeRegistryApi; +use substratee_api_client_extensions::SubstrateeRegistryApi; use substratee_worker_primitives::{DirectRequestStatus, RpcRequest, RpcResponse, RpcReturnValue}; type AccountPublic = ::Signer; diff --git a/primitives/api-client-extensions/Cargo.toml b/primitives/api-client-extensions/Cargo.toml new file mode 100644 index 0000000000..b19e4a0220 --- /dev/null +++ b/primitives/api-client-extensions/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "substratee-api-client-extensions" +version = "0.8.0" +authors = ["clangenbacher "] +edition = "2018" + +[dependencies] +codec = { package = "parity-scale-codec", version = "2.0.0", features = ["derive"] } + +# substrate +sp-core = { version = "3.0.0", git = "https://github.com/paritytech/substrate.git" } +sp-runtime = { version = "3.0.0", git = "https://github.com/paritytech/substrate.git" } + +# scs +substrate-api-client = { git = "https://github.com/scs/substrate-api-client" } + +# substratee +substratee-node-primitives = { path = "../node"} \ No newline at end of file diff --git a/primitives/node/src/api_ext/account.rs b/primitives/api-client-extensions/src/account.rs similarity index 94% rename from primitives/node/src/api_ext/account.rs rename to primitives/api-client-extensions/src/account.rs index 6c07f5c5fe..5b6608f6a6 100644 --- a/primitives/node/src/api_ext/account.rs +++ b/primitives/api-client-extensions/src/account.rs @@ -1,10 +1,10 @@ use sp_core::crypto::Pair; -use my_node_runtime::AccountId; use substrate_api_client::Api; use sp_runtime::MultiSignature; -use crate::ApiResult; +use substratee_node_primitives::AccountId; +use crate::ApiResult; /// ApiClient extension that contains some convenience methods around accounts. pub trait AccountApi { diff --git a/primitives/node/src/api_ext/chain.rs b/primitives/api-client-extensions/src/chain.rs similarity index 91% rename from primitives/node/src/api_ext/chain.rs rename to primitives/api-client-extensions/src/chain.rs index ad4ff1bc62..6623d461cb 100644 --- a/primitives/node/src/api_ext/chain.rs +++ b/primitives/api-client-extensions/src/chain.rs @@ -1,9 +1,9 @@ -use sp_core::Pair; +use sp_core::{Pair, H256}; use sp_runtime::MultiSignature; -use my_node_runtime::SignedBlock; -use primitive_types::H256; use substrate_api_client::Api; +use substratee_node_primitives::SignedBlock; + use crate::ApiResult; /// ApiClient extension that simplifies chain data access. diff --git a/primitives/node/src/api_ext/mod.rs b/primitives/api-client-extensions/src/lib.rs similarity index 81% rename from primitives/node/src/api_ext/mod.rs rename to primitives/api-client-extensions/src/lib.rs index e09a276db0..4a61380d1a 100644 --- a/primitives/node/src/api_ext/mod.rs +++ b/primitives/api-client-extensions/src/lib.rs @@ -10,4 +10,4 @@ pub use account::*; pub use substratee_registry::*; pub use chain::*; -pub type ApiResult = Result; +pub type ApiResult = Result; \ No newline at end of file diff --git a/primitives/node/src/api_ext/substratee_registry.rs b/primitives/api-client-extensions/src/substratee_registry.rs similarity index 87% rename from primitives/node/src/api_ext/substratee_registry.rs rename to primitives/api-client-extensions/src/substratee_registry.rs index d8f9a6b43a..6476c9349b 100644 --- a/primitives/node/src/api_ext/substratee_registry.rs +++ b/primitives/api-client-extensions/src/substratee_registry.rs @@ -1,12 +1,10 @@ -use my_node_runtime::{AccountId, substratee_registry::Enclave as EnclaveGen}; use sp_runtime::MultiSignature; use substrate_api_client::Api; use sp_core::Pair; -use crate::{ShardIdentifier, ApiResult}; +use substratee_node_primitives::{Enclave, ShardIdentifier, IpfsHash}; -pub type Enclave = EnclaveGen>; -pub type IpfsHash = [u8; 46]; +use crate::ApiResult; /// ApiClient extension that enables communication with the `substratee-registry` pallet. pub trait SubstrateeRegistryApi { @@ -45,7 +43,7 @@ impl SubstrateeRegistryApi for Api

.map_or_else(|| Ok(None), |w_index| self.enclave(w_index)) } - fn latest_ipfs_hash(&self, shard: &ShardIdentifier) -> ApiResult> { + fn latest_ipfs_hash(&self, shard: &ShardIdentifier) -> ApiResult> { self.get_storage_map("SubstrateeRegistry", "LatestIPFSHash", shard, None) } } \ No newline at end of file diff --git a/primitives/node/src/lib.rs b/primitives/node/src/lib.rs index f80c5b7a3c..10aa97fc17 100644 --- a/primitives/node/src/lib.rs +++ b/primitives/node/src/lib.rs @@ -8,10 +8,7 @@ use sp_core::H256; use std::vec::Vec; #[cfg(feature = "std")] -mod api_ext; - -#[cfg(feature = "std")] -pub use api_ext::*; +pub use my_node_runtime::{AccountId, substratee_registry::Enclave as EnclaveGen, SignedBlock}; pub type ShardIdentifier = H256; pub type BlockNumber = u32; @@ -23,6 +20,10 @@ pub struct Request { pub cyphertext: Vec, } +#[cfg(feature = "std")] +pub type Enclave = EnclaveGen>; +pub type IpfsHash = [u8; 46]; + pub type SubstrateeConfirmCallFn = ([u8; 2], ShardIdentifier, H256, Vec); pub type ShieldFundsFn = ([u8; 2], Vec, u128, ShardIdentifier); pub type CallWorkerFn = ([u8; 2], Request); diff --git a/worker/Cargo.toml b/worker/Cargo.toml index 8d8d429b48..4479db3950 100644 --- a/worker/Cargo.toml +++ b/worker/Cargo.toml @@ -46,6 +46,9 @@ path = "../primitives/node" [dependencies.substratee-worker-primitives] path = "../primitives/worker" +[dependencies.substratee-api-client-extensions] +path = "../primitives/api-client-extensions" + [dependencies.substratee-worker-api] path = "worker-api" diff --git a/worker/src/main.rs b/worker/src/main.rs index 08ce1e069c..eaa70d3877 100644 --- a/worker/src/main.rs +++ b/worker/src/main.rs @@ -55,7 +55,7 @@ use enclave::worker_api_direct_server::start_worker_api_direct_server; use sp_finality_grandpa::{AuthorityList, VersionedAuthorityList, GRANDPA_AUTHORITIES_KEY}; use std::time::{Duration, SystemTime}; -use substratee_node_primitives::{AccountApi, ChainApi}; +use substratee_api_client_extensions::{AccountApi, ChainApi}; use substratee_worker_primitives::block::SignedBlock as SignedSidechainBlock; use config::Config; use utils::extract_shard; From ad71ab09fbbed99d219fca4537692158147614cd Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Tue, 8 Jun 2021 15:31:16 +0200 Subject: [PATCH 12/14] some fmt and remove obsolete deps from primitives/node --- Cargo.lock | 5 ----- enclave/Cargo.lock | 1 - primitives/api-client-extensions/Cargo.toml | 2 +- .../api-client-extensions/src/account.rs | 1 - primitives/api-client-extensions/src/chain.rs | 2 +- primitives/api-client-extensions/src/lib.rs | 2 +- .../src/substratee_registry.rs | 2 +- primitives/node/Cargo.toml | 20 ++----------------- primitives/node/src/lib.rs | 1 - 9 files changed, 6 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ac3a4a01c8..79f4cd318b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5094,14 +5094,9 @@ dependencies = [ name = "substratee-node-primitives" version = "0.8.0" dependencies = [ - "base58", - "log 0.4.14 (registry+https://github.com/rust-lang/crates.io-index)", "parity-scale-codec 2.0.1", - "primitive-types 0.9.0", "sgx_tstd", "sp-core", - "sp-runtime", - "substrate-api-client", "substratee-node-runtime", ] diff --git a/enclave/Cargo.lock b/enclave/Cargo.lock index 0708f706d0..471fd79df8 100644 --- a/enclave/Cargo.lock +++ b/enclave/Cargo.lock @@ -2495,7 +2495,6 @@ name = "substratee-node-primitives" version = "0.8.0" dependencies = [ "parity-scale-codec", - "primitive-types", "sgx_tstd", "sp-core", ] diff --git a/primitives/api-client-extensions/Cargo.toml b/primitives/api-client-extensions/Cargo.toml index b19e4a0220..f6556e9bce 100644 --- a/primitives/api-client-extensions/Cargo.toml +++ b/primitives/api-client-extensions/Cargo.toml @@ -15,4 +15,4 @@ sp-runtime = { version = "3.0.0", git = "https://github.com/paritytech/substrate substrate-api-client = { git = "https://github.com/scs/substrate-api-client" } # substratee -substratee-node-primitives = { path = "../node"} \ No newline at end of file +substratee-node-primitives = { path = "../node"} diff --git a/primitives/api-client-extensions/src/account.rs b/primitives/api-client-extensions/src/account.rs index 5b6608f6a6..5d4ae93595 100644 --- a/primitives/api-client-extensions/src/account.rs +++ b/primitives/api-client-extensions/src/account.rs @@ -26,4 +26,3 @@ impl AccountApi for Api

.map_or_else(|| 0, |data| data.free)) } } - diff --git a/primitives/api-client-extensions/src/chain.rs b/primitives/api-client-extensions/src/chain.rs index 6623d461cb..3dfd328270 100644 --- a/primitives/api-client-extensions/src/chain.rs +++ b/primitives/api-client-extensions/src/chain.rs @@ -27,4 +27,4 @@ impl ChainApi for Api

// in tests. self.get_signed_block(hash) } -} \ No newline at end of file +} diff --git a/primitives/api-client-extensions/src/lib.rs b/primitives/api-client-extensions/src/lib.rs index 4a61380d1a..e09a276db0 100644 --- a/primitives/api-client-extensions/src/lib.rs +++ b/primitives/api-client-extensions/src/lib.rs @@ -10,4 +10,4 @@ pub use account::*; pub use substratee_registry::*; pub use chain::*; -pub type ApiResult = Result; \ No newline at end of file +pub type ApiResult = Result; diff --git a/primitives/api-client-extensions/src/substratee_registry.rs b/primitives/api-client-extensions/src/substratee_registry.rs index 6476c9349b..0a3c25e579 100644 --- a/primitives/api-client-extensions/src/substratee_registry.rs +++ b/primitives/api-client-extensions/src/substratee_registry.rs @@ -46,4 +46,4 @@ impl SubstrateeRegistryApi for Api

fn latest_ipfs_hash(&self, shard: &ShardIdentifier) -> ApiResult> { self.get_storage_map("SubstrateeRegistry", "LatestIPFSHash", shard, None) } -} \ No newline at end of file +} diff --git a/primitives/node/Cargo.toml b/primitives/node/Cargo.toml index 3a8519de2c..a8cd1792ea 100644 --- a/primitives/node/Cargo.toml +++ b/primitives/node/Cargo.toml @@ -5,10 +5,7 @@ authors = ["clangenbacher "] edition = "2018" [dependencies] -log = { version = "0.4", optional = true } codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } -primitive-types = { version = "0.9", default-features = false, features = ["codec"] } -base58 = { version = "0.1", optional = true } [dependencies.sgx_tstd] git = "https://github.com/apache/teaclave-sgx-sdk.git" @@ -21,28 +18,15 @@ git = "https://github.com/scs/substraTEE-node" package = "substratee-node-runtime" optional = true -[dependencies.substrate-api-client] -git = "https://github.com/scs/substrate-api-client" -optional = true - [dependencies.sp-core] git = "https://github.com/paritytech/substrate.git" version = "3.0.0" default-features = false -[dependencies.sp-runtime] -git = "https://github.com/paritytech/substrate.git" -version = "3.0.0" -optional = true - [features] default = ['std'] std = [ 'codec/std', - 'substrate-api-client', 'my-node-runtime', - 'primitive-types/std', - 'sp-core/std', - 'sp-runtime', - 'base58', - 'log'] + 'sp-core/std' +] sgx = [ 'sgx_tstd' ] diff --git a/primitives/node/src/lib.rs b/primitives/node/src/lib.rs index 10aa97fc17..cf98585c98 100644 --- a/primitives/node/src/lib.rs +++ b/primitives/node/src/lib.rs @@ -27,4 +27,3 @@ pub type IpfsHash = [u8; 46]; pub type SubstrateeConfirmCallFn = ([u8; 2], ShardIdentifier, H256, Vec); pub type ShieldFundsFn = ([u8; 2], Vec, u128, ShardIdentifier); pub type CallWorkerFn = ([u8; 2], Request); - From ca8c4fb7bf51350ec3bb1f991254d2d2d90e3d11 Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Wed, 9 Jun 2021 15:05:03 +0200 Subject: [PATCH 13/14] [primitves/api-extensions] add grandpa_authorities and grandpa_authorities_proof to ChainApi --- Cargo.lock | 1 + primitives/api-client-extensions/Cargo.toml | 1 + primitives/api-client-extensions/src/chain.rs | 25 ++++++++++++++++++- worker/src/main.rs | 21 +++------------- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 79f4cd318b..bad1a2b54f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5046,6 +5046,7 @@ version = "0.8.0" dependencies = [ "parity-scale-codec 2.0.1", "sp-core", + "sp-finality-grandpa", "sp-runtime", "substrate-api-client", "substratee-node-primitives", diff --git a/primitives/api-client-extensions/Cargo.toml b/primitives/api-client-extensions/Cargo.toml index f6556e9bce..d4f1141721 100644 --- a/primitives/api-client-extensions/Cargo.toml +++ b/primitives/api-client-extensions/Cargo.toml @@ -9,6 +9,7 @@ codec = { package = "parity-scale-codec", version = "2.0.0", features # substrate sp-core = { version = "3.0.0", git = "https://github.com/paritytech/substrate.git" } +sp-finality-grandpa = { version = "3.0.0", git = "https://github.com/paritytech/substrate.git" } sp-runtime = { version = "3.0.0", git = "https://github.com/paritytech/substrate.git" } # scs diff --git a/primitives/api-client-extensions/src/chain.rs b/primitives/api-client-extensions/src/chain.rs index 3dfd328270..4dd5aa601d 100644 --- a/primitives/api-client-extensions/src/chain.rs +++ b/primitives/api-client-extensions/src/chain.rs @@ -1,4 +1,5 @@ -use sp_core::{Pair, H256}; +use sp_core::{Pair, H256, storage::StorageKey}; +use sp_finality_grandpa::{AuthorityList, VersionedAuthorityList, GRANDPA_AUTHORITIES_KEY}; use sp_runtime::MultiSignature; use substrate_api_client::Api; @@ -6,10 +7,14 @@ use substratee_node_primitives::SignedBlock; use crate::ApiResult; +pub type StorageProof = Vec>; + /// ApiClient extension that simplifies chain data access. pub trait ChainApi { fn last_finalized_block(&self) -> ApiResult>; fn signed_block(&self, hash: Option) -> ApiResult>; + fn grandpa_authorities(&self, hash: Option) -> ApiResult; + fn grandpa_authorities_proof(&self, hash: Option) -> ApiResult; } impl ChainApi for Api

@@ -27,4 +32,22 @@ impl ChainApi for Api

// in tests. self.get_signed_block(hash) } + + fn grandpa_authorities(&self, at_block: Option) -> ApiResult { + Ok(self.get_storage_by_key_hash( + StorageKey(GRANDPA_AUTHORITIES_KEY.to_vec()), + at_block, + )? + .map(|g: VersionedAuthorityList| g.into()) + .expect("No grandpa authorities found. Can that be??")) + } + + fn grandpa_authorities_proof(&self, at_block: Option) -> ApiResult { + Ok(self.get_storage_proof_by_keys( + vec![StorageKey(GRANDPA_AUTHORITIES_KEY.to_vec())], + at_block, + )? + .map(|read_proof| read_proof.proof.into_iter().map(|bytes| bytes.0).collect()) + .expect("No grandpa authorities found. Can that be??")) + } } diff --git a/worker/src/main.rs b/worker/src/main.rs index eaa70d3877..93197565c5 100644 --- a/worker/src/main.rs +++ b/worker/src/main.rs @@ -52,7 +52,7 @@ use enclave::api::{ }; use enclave::tls_ra::{enclave_request_key_provisioning, enclave_run_key_provisioning_server}; use enclave::worker_api_direct_server::start_worker_api_direct_server; -use sp_finality_grandpa::{AuthorityList, VersionedAuthorityList, GRANDPA_AUTHORITIES_KEY}; +use sp_finality_grandpa::VersionedAuthorityList; use std::time::{Duration, SystemTime}; use substratee_api_client_extensions::{AccountApi, ChainApi}; @@ -450,23 +450,8 @@ pub fn init_chain_relay(eid: sgx_enclave_id_t, api: &Api) -> Head let genesis_hash = api.get_genesis_hash().unwrap(); let genesis_header: Header = api.get_header(Some(genesis_hash)).unwrap().unwrap(); info!("Got genesis Header: \n {:?} \n", genesis_header); - let grandpas: AuthorityList = api - .get_storage_by_key_hash( - StorageKey(GRANDPA_AUTHORITIES_KEY.to_vec()), - Some(genesis_header.hash()), - ) - .unwrap() - .map(|g: VersionedAuthorityList| g.into()) - .unwrap(); - - let grandpa_proof = api - .get_storage_proof_by_keys( - vec![StorageKey(GRANDPA_AUTHORITIES_KEY.to_vec())], - Some(genesis_header.hash()), - ) - .unwrap() - .map(|read_proof| read_proof.proof.into_iter().map(|bytes| bytes.0).collect()) - .unwrap(); + let grandpas = api.grandpa_authorities(Some(genesis_hash)).unwrap(); + let grandpa_proof = api.grandpa_authorities_proof(Some(genesis_hash)).unwrap(); debug!("Grandpa Authority List: \n {:?} \n ", grandpas); From 2eeca696f7bc2c93b182cf48b146698d1f426312 Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Wed, 23 Jun 2021 15:33:31 +0200 Subject: [PATCH 14/14] [primitives/api-client-extensions] remove expects and add todo referencing custom api-client error. --- primitives/api-client-extensions/src/chain.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/primitives/api-client-extensions/src/chain.rs b/primitives/api-client-extensions/src/chain.rs index 4dd5aa601d..f34fec552b 100644 --- a/primitives/api-client-extensions/src/chain.rs +++ b/primitives/api-client-extensions/src/chain.rs @@ -39,7 +39,7 @@ impl ChainApi for Api

at_block, )? .map(|g: VersionedAuthorityList| g.into()) - .expect("No grandpa authorities found. Can that be??")) + .unwrap()) // todo: Introduce an error instead of unwrap: See: https://github.com/scs/substrate-api-client/issues/123 } fn grandpa_authorities_proof(&self, at_block: Option) -> ApiResult { @@ -48,6 +48,6 @@ impl ChainApi for Api

at_block, )? .map(|read_proof| read_proof.proof.into_iter().map(|bytes| bytes.0).collect()) - .expect("No grandpa authorities found. Can that be??")) + .unwrap()) // todo: Introduce an error instead of unwrap: See: https://github.com/scs/substrate-api-client/issues/123 } }