-
Notifications
You must be signed in to change notification settings - Fork 46
Extract a new extrinsics factory and nonce cache crate #498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| [package] | ||
| name = "itp-extrinsics-factory" | ||
| version = "0.8.0" | ||
| authors = ["Integritee AG <[email protected]>"] | ||
| edition = "2018" | ||
| resolver = "2" | ||
|
|
||
| # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
|
||
| [features] | ||
| default = ["std"] | ||
| std = [ | ||
| "itp-nonce-cache/std", | ||
| "itp-types/std", | ||
| "log/std", | ||
| "substrate-api-client/std", | ||
| "thiserror", | ||
| ] | ||
| sgx = [ | ||
| "itp-nonce-cache/sgx", | ||
| "sgx_tstd", | ||
| "thiserror_sgx", | ||
| ] | ||
|
|
||
| [dependencies] | ||
| # sgx dependencies | ||
| sgx_types = { rev = "v1.1.3", git = "https://github.com/apache/teaclave-sgx-sdk.git" } | ||
| sgx_tstd = { rev = "v1.1.3", git = "https://github.com/apache/teaclave-sgx-sdk.git", optional = true } | ||
| substrate-api-client = { default-features = false, git = "https://github.com/scs/substrate-api-client", branch = "master" } | ||
|
|
||
| # local dependencies | ||
| itp-nonce-cache = { path = "../nonce-cache", default-features = false } | ||
| itp-settings = { path = "../settings", default-features = false } | ||
| itp-types = { path = "../types", default-features = false } | ||
|
|
||
| # sgx enabled external libraries | ||
| thiserror_sgx = { package = "thiserror", git = "https://github.com/mesalock-linux/thiserror-sgx", tag = "sgx_1.1.3", optional = true } | ||
|
|
||
| # std compatible external libraries (make sure these versions match with the sgx-enabled ones above) | ||
| thiserror = { version = "1.0", optional = true } | ||
|
|
||
| # no-std dependencies | ||
| log = { version = "0.4", default-features = false } | ||
| codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } | ||
| sp-core = { version = "4.0.0-dev", default-features = false, features = ["full_crypto"], git = "https://github.com/paritytech/substrate.git", branch = "master" } | ||
| sp-runtime = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master"} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| Copyright 2021 Integritee AG and Supercomputing Systems AG | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
|
|
||
| */ | ||
|
|
||
| #[cfg(all(not(feature = "std"), feature = "sgx"))] | ||
| use crate::sgx_reexport_prelude::*; | ||
|
|
||
| use sgx_types::sgx_status_t; | ||
| use std::{boxed::Box, format}; | ||
|
|
||
| pub type Result<T> = core::result::Result<T, Error>; | ||
|
|
||
| /// extrinsics factory error | ||
| #[derive(Debug, thiserror::Error)] | ||
| pub enum Error { | ||
| #[error("Nonce cache error: {0}")] | ||
| NonceCache(#[from] itp_nonce_cache::error::Error), | ||
| #[error("SGX error, status: {0}")] | ||
| Sgx(sgx_status_t), | ||
| #[error(transparent)] | ||
| Other(#[from] Box<dyn std::error::Error + Sync + Send + 'static>), | ||
| } | ||
|
|
||
| impl From<sgx_status_t> for Error { | ||
| fn from(sgx_status: sgx_status_t) -> Self { | ||
| Self::Sgx(sgx_status) | ||
| } | ||
| } | ||
|
|
||
| impl From<codec::Error> for Error { | ||
| fn from(e: codec::Error) -> Self { | ||
| Self::Other(format!("{:?}", e).into()) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| /* | ||
| Copyright 2021 Integritee AG and Supercomputing Systems AG | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
|
|
||
| */ | ||
|
|
||
| #![cfg_attr(not(feature = "std"), no_std)] | ||
|
|
||
| #[cfg(all(feature = "std", feature = "sgx"))] | ||
| compile_error!("feature \"std\" and feature \"sgx\" cannot be enabled at the same time"); | ||
|
|
||
| #[cfg(all(not(feature = "std"), feature = "sgx"))] | ||
| extern crate sgx_tstd as std; | ||
|
|
||
| // re-export module to properly feature gate sgx and regular std environment | ||
| #[cfg(all(not(feature = "std"), feature = "sgx"))] | ||
| pub mod sgx_reexport_prelude { | ||
| pub use thiserror_sgx as thiserror; | ||
| } | ||
|
|
||
| use codec::Encode; | ||
| use error::Result; | ||
| use itp_nonce_cache::{MutateNonce, Nonce}; | ||
| use itp_settings::node::{RUNTIME_SPEC_VERSION, RUNTIME_TRANSACTION_VERSION}; | ||
| use itp_types::OpaqueCall; | ||
| use sp_core::{Pair, H256}; | ||
| use sp_runtime::{MultiSignature, OpaqueExtrinsic}; | ||
| use std::{sync::Arc, vec::Vec}; | ||
| use substrate_api_client::compose_extrinsic_offline; | ||
|
|
||
| pub mod error; | ||
|
|
||
| /// Create extrinsics from opaque calls | ||
| /// | ||
| /// Also increases the nonce counter for each extrinsic that is created. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ohh, thanks alot for that one ❤️ |
||
| pub trait CreateExtrinsics { | ||
| fn create_extrinsics(&self, calls: &[OpaqueCall]) -> Result<Vec<OpaqueExtrinsic>>; | ||
| } | ||
|
Comment on lines
+44
to
+49
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the new trait to create extrinsics, transforming |
||
|
|
||
| /// Extrinsics factory | ||
| pub struct ExtrinsicsFactory<Signer, NonceCache> | ||
| where | ||
| Signer: Pair<Public = sp_core::ed25519::Public>, | ||
| Signer::Signature: Into<MultiSignature>, | ||
| NonceCache: MutateNonce, | ||
| { | ||
| genesis_hash: H256, | ||
| signer: Signer, | ||
| nonce_cache: Arc<NonceCache>, | ||
| } | ||
|
|
||
| impl<Signer, NonceCache> ExtrinsicsFactory<Signer, NonceCache> | ||
| where | ||
| Signer: Pair<Public = sp_core::ed25519::Public>, | ||
| Signer::Signature: Into<MultiSignature>, | ||
| NonceCache: MutateNonce, | ||
| { | ||
| pub fn new(genesis_hash: H256, signer: Signer, nonce_cache: Arc<NonceCache>) -> Self { | ||
| ExtrinsicsFactory { genesis_hash, signer, nonce_cache } | ||
| } | ||
| } | ||
|
|
||
| impl<Signer, NonceCache> CreateExtrinsics for ExtrinsicsFactory<Signer, NonceCache> | ||
| where | ||
| Signer: Pair<Public = sp_core::ed25519::Public>, | ||
| Signer::Signature: Into<MultiSignature>, | ||
| NonceCache: MutateNonce, | ||
| { | ||
| fn create_extrinsics(&self, calls: &[OpaqueCall]) -> Result<Vec<OpaqueExtrinsic>> { | ||
| let mut nonce_lock = self.nonce_cache.load_for_mutation()?; | ||
| let mut nonce_value = nonce_lock.0; | ||
|
|
||
| let extrinsics_buffer: Vec<OpaqueExtrinsic> = calls | ||
| .iter() | ||
| .map(|call| { | ||
| let xt = compose_extrinsic_offline!( | ||
| self.signer.clone(), | ||
| call, | ||
| nonce_value, | ||
| Era::Immortal, | ||
| self.genesis_hash, | ||
| self.genesis_hash, | ||
| RUNTIME_SPEC_VERSION, | ||
| RUNTIME_TRANSACTION_VERSION | ||
| ) | ||
| .encode(); | ||
| nonce_value += 1; | ||
| xt | ||
| }) | ||
| .map(|xt| { | ||
| OpaqueExtrinsic::from_bytes(&xt) | ||
| .expect("A previously encoded extrinsic has valid codec; qed.") | ||
| }) | ||
| .collect(); | ||
|
|
||
| *nonce_lock = Nonce(nonce_value); | ||
|
Comment on lines
+81
to
+107
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Retrieves the nonce write lock, creates all the extrinsics and increments the nonce value for each of them. Then at the end, write the nonce value back into the cache. |
||
|
|
||
| Ok(extrinsics_buffer) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| pub mod tests { | ||
|
|
||
| use super::*; | ||
| use itp_nonce_cache::{GetNonce, Nonce, NonceCache, NonceValue}; | ||
| use sp_core::ed25519; | ||
| //use substrate_api_client::extrinsic::xt_primitives::UncheckedExtrinsicV4; | ||
|
|
||
| #[test] | ||
| pub fn creating_xts_increases_nonce_for_each_xt() { | ||
| let nonce_cache = Arc::new(NonceCache::default()); | ||
| let extrinsics_factory = | ||
| ExtrinsicsFactory::new(test_genesis_hash(), test_account(), nonce_cache.clone()); | ||
|
|
||
| let opaque_calls = [OpaqueCall(vec![3u8; 42]), OpaqueCall(vec![12u8, 78])]; | ||
| let xts = extrinsics_factory.create_extrinsics(&opaque_calls).unwrap(); | ||
|
|
||
| assert_eq!(opaque_calls.len(), xts.len()); | ||
| assert_eq!(nonce_cache.get_nonce().unwrap(), Nonce(opaque_calls.len() as NonceValue)); | ||
| } | ||
|
|
||
| // #[test] | ||
| // pub fn xts_have_increasing_nonce() { | ||
| // let nonce_cache = Arc::new(NonceCache::default()); | ||
| // nonce_cache.set_nonce(Nonce(34)).unwrap(); | ||
| // let extrinsics_factory = | ||
| // ExtrinsicsFactory::new(test_genesis_hash(), test_account(), nonce_cache); | ||
| // | ||
| // let opaque_calls = | ||
| // [OpaqueCall(vec![3u8; 42]), OpaqueCall(vec![12u8, 78]), OpaqueCall(vec![15u8, 12])]; | ||
| // let xts: Vec<UncheckedExtrinsicV4<OpaqueCall>> = extrinsics_factory | ||
| // .create_extrinsics(&opaque_calls) | ||
| // .unwrap() | ||
| // .iter() | ||
| // .map(|mut x| UncheckedExtrinsicV4::<OpaqueCall>::decode(&mut x)) | ||
| // .collect(); | ||
| // | ||
| // assert_eq!(xts.len(), opaque_calls.len()); | ||
| // assert_eq!(xts[0].signature.unwrap().2 .2, 34u128); | ||
| // } | ||
|
Comment on lines
+134
to
+152
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted to write a test where I create extrinsics and then check if the nonce in the resulting extrinsics is correct. Unfortunately, the nonce in the extrinsic is in a private member that I cannot access here 😞
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you want to be hardcore, you could check the relevant bits in the encoded extrinsics. :D. Anyhow, we can make the nonce public in the api-client. I don't see anything against that. |
||
|
|
||
| fn test_account() -> ed25519::Pair { | ||
| ed25519::Pair::from_seed(b"42315678901234567890123456789012") | ||
| } | ||
|
|
||
| fn test_genesis_hash() -> H256 { | ||
| H256::from_slice(&[56u8; 32]) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,35 @@ | ||||||||
| [package] | ||||||||
| name = "itp-nonce-cache" | ||||||||
| version = "0.8.0" | ||||||||
| authors = ["Integritee AG <[email protected]>"] | ||||||||
| edition = "2018" | ||||||||
| resolver = "2" | ||||||||
|
|
||||||||
| # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||||||||
|
|
||||||||
| [features] | ||||||||
| default = ["std"] | ||||||||
| std = [ | ||||||||
| "log/std", | ||||||||
| "thiserror", | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done 👍 |
||||||||
| ] | ||||||||
| sgx = [ | ||||||||
| "sgx_tstd", | ||||||||
| "thiserror_sgx", | ||||||||
| ] | ||||||||
|
|
||||||||
| [dependencies] | ||||||||
| # sgx dependencies | ||||||||
| sgx_tstd = { rev = "v1.1.3", git = "https://github.com/apache/teaclave-sgx-sdk.git", optional = true } | ||||||||
|
|
||||||||
| # local dependencies | ||||||||
|
|
||||||||
| # sgx enabled external libraries | ||||||||
| thiserror_sgx = { package = "thiserror", git = "https://github.com/mesalock-linux/thiserror-sgx", tag = "sgx_1.1.3", optional = true } | ||||||||
|
|
||||||||
| # std compatible external libraries (make sure these versions match with the sgx-enabled ones above) | ||||||||
| thiserror = { version = "1.0", optional = true } | ||||||||
|
|
||||||||
| # no-std dependencies | ||||||||
| log = { version = "0.4", default-features = false } | ||||||||
| lazy_static = { version = "1.1.0", features = ["spin_no_std"] } | ||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re-ordered the members alphabetically