-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract a new extrinsics factory and nonce cache crate (#498)
Extract the extrinsics composition to a new extrinsics factory crate and also introduce a nonce cache in the process
- Loading branch information
Felix Müller
authored
Nov 10, 2021
1 parent
11e25fc
commit e959c9c
Showing
17 changed files
with
669 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
pub trait CreateExtrinsics { | ||
fn create_extrinsics(&self, calls: &[OpaqueCall]) -> Result<Vec<OpaqueExtrinsic>>; | ||
} | ||
|
||
/// 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); | ||
|
||
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); | ||
// } | ||
|
||
fn test_account() -> ed25519::Pair { | ||
ed25519::Pair::from_seed(b"42315678901234567890123456789012") | ||
} | ||
|
||
fn test_genesis_hash() -> H256 { | ||
H256::from_slice(&[56u8; 32]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
] | ||
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"] } |
Oops, something went wrong.