Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2421,6 +2421,7 @@ dependencies = [
"finality-grandpa",
"frame-system",
"hash-db",
"itp-ocall-api",
"itp-settings",
"itp-sgx-io",
"itp-storage",
Expand Down Expand Up @@ -2571,6 +2572,35 @@ dependencies = [
"sgx_types",
]

[[package]]
name = "itp-extrinsics-factory"
version = "0.8.0"
dependencies = [
"itp-nonce-cache",
"itp-settings",
"itp-types",
"log 0.4.14 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-scale-codec",
"sgx_tstd",
"sgx_types",
"sp-core",
"sp-runtime",
"substrate-api-client",
"thiserror 1.0.29",
"thiserror 1.0.9",
]

[[package]]
name = "itp-nonce-cache"
version = "0.8.0"
dependencies = [
"lazy_static",
"log 0.4.14 (registry+https://github.com/rust-lang/crates.io-index)",
"sgx_tstd",
"thiserror 1.0.29",
"thiserror 1.0.9",
]

[[package]]
name = "itp-ocall-api"
version = "0.8.0"
Expand Down
26 changes: 13 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
[workspace]
resolver = "2"
members = [
"app-libs/stf",
"cli",
"core/light-client",
"core/direct-rpc-server",
"core/tls-websocket-server",
"core/light-client",
"core/rest-client",
"core/rpc-client",
"core/rpc-server",
"core/tls-websocket-server",
"core-primitives/api-client-extensions",
"core-primitives/enclave-api",
"core-primitives/enclave-api/ffi",
"core-primitives/types",
"core-primitives/extrinsics-factory",
"core-primitives/nonce-cache",
"core-primitives/ocall-api",
"core-primitives/storage-verified",
"core-primitives/teerex-storage",
"core-primitives/settings",
"core-primitives/sgx/crypto",
"core-primitives/sgx/io",
"core-primitives/stf-executor",
"core-primitives/stf-state-handler",
"core-primitives/storage",
"core-primitives/storage-verified",
"core-primitives/teerex-storage",
"core-primitives/test",
"core-primitives/types",
"sidechain/sidechain-crate",
"sidechain/validateer-fetch",
"sidechain/primitives",
"service",
"sidechain/consensus/aura",
"sidechain/consensus/common",
"sidechain/consensus/slots",
"sidechain/primitives",
"sidechain/rpc-handler",
"sidechain/sidechain-crate",
"sidechain/state",
"sidechain/top-pool",
"sidechain/top-pool-rpc-author",
"app-libs/stf",
"service",
"core/rpc-client",
"core/rpc-server",
# "enclave",
"sidechain/validateer-fetch",
Comment on lines +4 to +38
Copy link
Contributor Author

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

]

#[patch."https://github.com/integritee-network/pallet-teerex.git"]
Expand Down
46 changes: 46 additions & 0 deletions core-primitives/extrinsics-factory/Cargo.toml
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"}
47 changes: 47 additions & 0 deletions core-primitives/extrinsics-factory/src/error.rs
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())
}
}
161 changes: 161 additions & 0 deletions core-primitives/extrinsics-factory/src/lib.rs
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.
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the new trait to create extrinsics, transforming OpaqueCall into 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);
Comment on lines +81 to +107
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.
The reason we have a strict nonce lock here, is that we want to prevent any other concurrent process to get or write the nonce while we're incrementing it here.


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
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 😞

Copy link
Contributor

@clangenb clangenb Nov 10, 2021

Choose a reason for hiding this comment

The 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])
}
}
35 changes: 35 additions & 0 deletions core-primitives/nonce-cache/Cargo.toml
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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"thiserror",
"thiserror",
"log/std"

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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"] }
Loading