Skip to content

Commit

Permalink
Move to unsigned extrinsics, use logical hashes to de-duplicate chron…
Browse files Browse the repository at this point in the history
…icle extrinsics in the txpool

Signed-off-by: Ryan <[email protected]>
  • Loading branch information
ryan-s-roberts committed Jul 7, 2024
1 parent 716b1c1 commit eef50c6
Show file tree
Hide file tree
Showing 40 changed files with 1,313 additions and 1,192 deletions.
1,922 changes: 1,079 additions & 843 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions crates/chronicle-domain-test/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ pub async fn main() {
#[cfg(test)]
mod test {
use chronicle::{
api::{
chronicle_graphql::{construct_schema, OpaCheck, Subscription},
Store,
},
api::chronicle_graphql::{construct_schema, Subscription},
async_graphql::{Request, Response, Schema},
bootstrap::opa::CliPolicyLoader,
chrono::{NaiveDate, TimeZone, Utc},
Expand Down
6 changes: 1 addition & 5 deletions crates/chronicle-telemetry/src/telemetry.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use opentelemetry::{global, logs::LogError, metrics::MetricsError, trace::TraceError};
use opentelemetry_appender_tracing::layer::OpenTelemetryTracingBridge;
use opentelemetry_sdk::{
logs::Config,
runtime,
trace::{self as sdktrace, RandomIdGenerator},
};
use opentelemetry_sdk::{runtime, trace as sdktrace};
use tracing_opentelemetry::{MetricsLayer, OpenTelemetryLayer};
use tracing_subscriber::{fmt::format::FmtSpan, prelude::*, EnvFilter};

Expand Down
5 changes: 3 additions & 2 deletions crates/chronicle-test-infrastructure/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ futures = { workspace = true }
insta = { workspace = true, features = ["json", "yaml"] }
lazy_static = { workspace = true }
pallet-chronicle = { path = "../pallet-chronicle" }
parity-scale-codec = { version = "3.6.1", default-features = false, features = [
parity-scale-codec = { version = "3.6", default-features = false, features = [
"derive",
] }
portpicker = { workspace = true }
Expand All @@ -40,7 +40,8 @@ scale-info = { version = "2.10.0", default-features = false, features = [
"derive",
] }
serde_json = { workspace = true }
subxt = { version = "0.34", features = ["substrate-compat"] }
subxt = { git = "https://github.com/paritytech/subxt", features = ["substrate-compat"] }

tempfile = { workspace = true }
testcontainers = { workspace = true }
tokio = { workspace = true }
Expand Down
3 changes: 1 addition & 2 deletions crates/chronicle-test-infrastructure/src/api_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ use common::{
prov::{
json_ld::ToJson,
operations::{ChronicleOperation, DerivationType},
ActivityId, AgentId, DomaintypeId, EntityId, NamespaceId,
ActivityId, AgentId, DomaintypeId, EntityId,
},
};
use uuid::Uuid;

use crate::substitutes::test_api;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use super::mockchain::{new_test_ext, ChronicleModule, RuntimeEvent, RuntimeOrigi
use protocol_abstract::{
BlockId, FromBlock, LedgerEvent, LedgerEventContext, LedgerReader, LedgerWriter, Position, Span,
};
use protocol_substrate::{PolkadotConfig, SubstrateStateReader, SubxtClientError};
use protocol_substrate::{SubstrateStateReader, SubxtClientError};
use protocol_substrate_chronicle::{
protocol::WriteConsistency, ChronicleEvent, ChronicleEventCodec, ChronicleTransaction,
};
use std::sync::{Arc, Mutex};
use subxt::metadata::{DecodeWithMetadata, EncodeWithMetadata};
use subxt::metadata::DecodeWithMetadata;

#[derive(Clone)]
pub struct Stubstrate {
Expand Down Expand Up @@ -90,7 +90,7 @@ impl LedgerWriter for Stubstrate {
) -> Result<(Self::Submittable, ChronicleTransactionId), Self::Error> {
Ok((
OperationSubmission {
correlation_id: tx.correlation_id.into_bytes(),
correlation_id: tx.correlation_id,
identity: tx.identity,
items: tx.operations,
},
Expand Down Expand Up @@ -142,7 +142,7 @@ impl LedgerWriter for Stubstrate {
impl SubstrateStateReader for Stubstrate {
type Error = SubxtClientError;

async fn get_state_entry<K: EncodeWithMetadata + Send + Sync, V: DecodeWithMetadata>(
async fn get_state_entry<K: std::convert::AsRef<[u8]> + Send + Sync, V: DecodeWithMetadata>(
&self,
_pallet_name: &str,
_entry_name: &str,
Expand Down
10 changes: 4 additions & 6 deletions crates/chronicle/src/bootstrap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ where
#[allow(dead_code)]
fn namespace_bindings(options: &ArgMatches) -> Vec<NamespaceId> {
options
.values_of("namespace-binding")
.get_many::<String>("namespace-binding")
.map(|values| {
values
.map(|value| {
Expand Down Expand Up @@ -297,7 +297,7 @@ pub async fn api(
embedded_tp.connect_chronicle::<ChronicleConfig>().await?,
UniqueUuid,
chronicle_signing(options).await?,
vec![],
namespace_bindings(options),
remote_opa,
liveness_check_interval,
)
Expand Down Expand Up @@ -413,10 +413,8 @@ async fn configure_opa(options: &ArgMatches) -> Result<ConfiguredOpa, CliError>
} else {
let (opa, settings) = self::opa::opa_executor_from_substrate_state(
&ChronicleSubstrateClient::connect(validator_address(options)?).await?,
&protocol_substrate_opa::OpaSubstrateClient::connect(
validator_address(options)?,
)
.await?,
&protocol_substrate_opa::OpaSubstrateClient::connect(validator_address(options)?)
.await?,
)
.await?;

Expand Down
9 changes: 5 additions & 4 deletions crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ macro-attr-2018 = { workspace = true }
mime = { version = "0.3", optional = true }
newtype-derive-2018 = { workspace = true }
opa = { git = "https://github.com/chronicleworks/opa-rs", rev = "9fa2fbce", optional = true }
parity-scale-codec = { version = "3.6.1", default-features = false, features = [
parity-scale-codec = { version = "3.6", default-features = false, features = [
"derive",
"max-encoded-len",
], optional = true }
Expand All @@ -62,15 +62,15 @@ rust-embed = { version = "6", features = [
"debug-embed",
"include-exclude",
], optional = true }
scale-decode = { version = "^0.10.0", default-features = false, features = [
scale-decode = { version = "0.13.1", default-features = false, features = [
"derive",
], optional = true }
scale-encode = { version = "^0.5.0", default-features = false, features = [
scale-encode = { version = "0.7.1", default-features = false, features = [
"derive",
"primitive-types",
"bits",
], optional = true }
scale-info = { version = "^2.10.0", default-features = false, features = [
scale-info = { version = "2.11.3", default-features = false, features = [
"derive",
], optional = true }
serde = { version = "1.0", default-features = false, features = [
Expand Down Expand Up @@ -134,6 +134,7 @@ std = [
]
# Enable parity support, annoyingly lazy_static has a non standard way of enabling non_std
parity-encoding = [
"dep:sp-std",
"lazy_static/spin_no_std",
"parity-scale-codec",
"scale-info",
Expand Down
10 changes: 5 additions & 5 deletions crates/common/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,19 @@ impl From<Value> for SerdeWrapper {

#[cfg(feature = "parity-encoding")]
impl scale_encode::EncodeAsType for SerdeWrapper {
fn encode_as_type_to(
fn encode_as_type_to<R: scale_decode::TypeResolver>(
&self,
type_id: u32,
_types: &scale_info::PortableRegistry,
out: &mut scale_encode::Vec<u8>,
type_id: R::TypeId,
types: &R,
out: &mut Vec<u8>,
) -> Result<(), scale_encode::Error> {
let json_string = match serde_json::to_string(&self.0) {
Ok(json_string) => json_string,
Err(e) => {
tracing::error!("Failed to serialize Value to JSON string: {}", e);
return Err(scale_encode::Error::new(scale_encode::error::ErrorKind::WrongShape {
actual: Kind::Str,
expected: type_id,
expected_id: "type_id".to_string(),
}));
},
};
Expand Down
44 changes: 38 additions & 6 deletions crates/common/src/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ use parity_scale_codec::{
};
#[cfg(not(feature = "std"))]
use scale_info::prelude::*;


#[cfg(feature = "std")]
use std::{
boxed::Box, collections::btree_map::Entry, collections::BTreeMap, collections::BTreeSet,
sync::Arc,
sync::Arc
};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -93,25 +95,55 @@ impl core::fmt::Display for SubmissionError {
)]
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct OperationSubmission {
pub correlation_id: [u8; 16],
pub correlation_id: [u8; 32],
pub items: Arc<Vec<ChronicleOperation>>,
pub identity: Arc<SignedIdentity>,
}

impl OperationSubmission {
pub fn new(uuid: Uuid, identity: SignedIdentity, operations: Vec<ChronicleOperation>) -> Self {

pub fn correlation_id(&self) -> &[u8; 32] {
&self.correlation_id
}

#[cfg(feature = "std")]
pub fn logical_hash(operations: &[ChronicleOperation]) -> [u8; 32] {
use k256::sha2::{Sha256, Digest};
use tracing::debug;

let mut hasher = Sha256::new();
use core::hash::{Hash, Hasher};

for operation in operations.iter() {
let mut operation_hasher = std::hash::SipHasher::new_with_keys(1, 1);
operation.hash(&mut operation_hasher);
let operation_hash = operation_hasher.finish().to_ne_bytes();
debug!("Operation hash: {:?} {:?}", operation_hash, operation);
hasher.update(operation_hash);
}
let result = hasher.finalize();
let mut hash: [u8; 32] = result.into();
hash.copy_from_slice(&result);
debug!("Final hash: {:?}", hash);
hash
}

#[cfg(feature = "std")]
pub fn new(identity: SignedIdentity, operations: Vec<ChronicleOperation>) -> Self {
OperationSubmission {
correlation_id: uuid.into_bytes(),
correlation_id: Self::logical_hash(&operations),
identity: identity.into(),
items: operations.into(),
}
}

pub fn new_anonymous(uuid: Uuid, operations: Vec<ChronicleOperation>) -> Self {
Self::new(uuid, SignedIdentity::new_no_identity(), operations)
#[cfg(feature = "std")]
pub fn new_anonymous(operations: Vec<ChronicleOperation>) -> Self {
Self::new(SignedIdentity::new_no_identity(), operations)
}
}


pub type SubmitResult = Result<ChronicleTransactionId, SubmissionError>;

#[derive(Debug, Clone)]
Expand Down
10 changes: 8 additions & 2 deletions crates/common/src/opa/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ impl From<H128> for KeyAddress {
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct KeyAddress(H128);

impl AsRef<[u8]> for KeyAddress {
fn as_ref(&self) -> &[u8] {
self.0 .0.as_ref()
}
}

#[derive(Debug, Clone, Eq, PartialEq, Hash)]
// This message is used to bootstrap the root key for a newly created authz tp,
// it can only be executed once
Expand Down Expand Up @@ -229,7 +235,7 @@ pub enum Operation {
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct OpaSubmission {
pub version: String,
pub correlation_id: [u8; 16],
pub correlation_id: [u8; 32],
pub span_id: u64,
pub payload: Payload,
}
Expand Down Expand Up @@ -509,7 +515,7 @@ pub mod codec {
#[derive(Encode, EncodeAsType, DecodeAsType, Decode, Debug, TypeInfo, Clone, PartialEq, Eq)]
pub struct OpaSubmissionV1 {
pub version: String,
pub correlation_id: [u8; 16],
pub correlation_id: [u8; 32],
pub span_id: u64,
pub payload: PayloadV1,
}
Expand Down
18 changes: 6 additions & 12 deletions crates/common/src/prov/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,10 @@ pub enum ChronicleTransactionIdError {
derive(scale_info::TypeInfo, parity_scale_codec::Encode, parity_scale_codec::Decode)
)]
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone, Copy, Default)]
pub struct ChronicleTransactionId([u8; 16]);
pub struct ChronicleTransactionId([u8; 32]);

impl core::ops::Deref for ChronicleTransactionId {
type Target = [u8; 16];
type Target = [u8; 32];

fn deref(&self) -> &Self::Target {
&self.0
Expand All @@ -249,14 +249,8 @@ impl core::fmt::Display for ChronicleTransactionId {
}
}

impl From<Uuid> for ChronicleTransactionId {
fn from(u: Uuid) -> Self {
Self(u.into_bytes())
}
}

impl From<[u8; 16]> for ChronicleTransactionId {
fn from(u: [u8; 16]) -> Self {
impl From<[u8; 32]> for ChronicleTransactionId {
fn from(u: [u8; 32]) -> Self {
Self(u)
}
}
Expand All @@ -274,8 +268,8 @@ impl core::convert::TryFrom<&str> for ChronicleTransactionId {

fn try_from(s: &str) -> Result<Self, Self::Error> {
let bytes = hex::decode(s)?;
let mut array = [0; 16];
array.copy_from_slice(&bytes[0..16]);
let mut array = [0; 32];
array.copy_from_slice(&bytes[0..32]);
Ok(Self(array))
}
}
Expand Down
11 changes: 7 additions & 4 deletions crates/common/src/prov/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,11 @@ impl From<DateTime<Utc>> for TimeWrapper {

#[cfg(feature = "parity-encoding")]
impl scale_encode::EncodeAsType for TimeWrapper {
fn encode_as_type_to(
fn encode_as_type_to<R: scale_decode::TypeResolver>(
&self,
type_id: u32,
types: &scale_info::PortableRegistry,
out: &mut scale_encode::Vec<u8>,
type_id: R::TypeId,
types: &R,
out: &mut Vec<u8>,
) -> Result<(), scale_encode::Error> {
let timestamp = self.0.timestamp();
let subsec_nanos = self.0.timestamp_subsec_nanos();
Expand Down Expand Up @@ -617,7 +617,10 @@ pub enum ChronicleOperation {
WasInformedBy(WasInformedBy),
}



impl ChronicleOperation {

pub fn create_namespace(id: NamespaceId) -> Self {
ChronicleOperation::CreateNamespace(CreateNamespace::new(id))
}
Expand Down
3 changes: 2 additions & 1 deletion crates/embedded-substrate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ sc-cli = { git = 'https://github.com/paritytech/polkadot-sdk.git', tag = 'polkad
sp-io = { git = 'https://github.com/paritytech/polkadot-sdk.git', tag = 'polkadot-v1.9.0' }
sp-runtime = { git = 'https://github.com/paritytech/polkadot-sdk.git', tag = 'polkadot-v1.9.0' }

subxt = { version = "0.34", features = ["substrate-compat"] }
subxt = { git = "https://github.com/paritytech/subxt", features = ["substrate-compat"] }

tempfile = { version = "3" }
thiserror = { workspace = true }
tokio = { workspace = true }
Expand Down
10 changes: 3 additions & 7 deletions crates/embedded-substrate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use sc_cli::{print_node_infos, CliConfiguration, Signals, SubstrateCli};
use subxt::{
config::ExtrinsicParams,
ext::futures::{pin_mut, FutureExt},
utils::{AccountId32, MultiAddress, MultiSignature},
};
use tempfile::TempDir;
use thiserror::Error;
Expand Down Expand Up @@ -229,12 +228,9 @@ pub mod test_runtime {
},
ChronicleEvent, ChronicleTransaction,
};
use subxt::{
ext::{
futures::StreamExt,
sp_core::{Pair, Public},
},
PolkadotConfig,
use subxt::ext::{
futures::StreamExt,
sp_core::{Pair, Public},
};
use uuid::Uuid;

Expand Down
Loading

0 comments on commit eef50c6

Please sign in to comment.