Skip to content

Commit

Permalink
generic mortality. stay immortal for starters
Browse files Browse the repository at this point in the history
  • Loading branch information
brenzi committed Nov 5, 2024
1 parent 10fffb6 commit ae60b49
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 73 deletions.
38 changes: 25 additions & 13 deletions app-libs/stf/src/trusted_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use itp_stf_primitives::{
types::{AccountId, KeyPair, ShardIdentifier, Signature, TrustedOperation},
};
use itp_types::{
parentchain::{ParentchainCall, ParentchainId, ProxyType},
parentchain::{GenericMortality, ParentchainCall, ParentchainId, ProxyType},
Address, Moment, OpaqueCall,
};
use itp_utils::stringify::account_id_to_string;
Expand Down Expand Up @@ -340,9 +340,18 @@ where
vault_transfer_call,
));
let parentchain_call = match parentchain_id {
ParentchainId::Integritee => ParentchainCall::Integritee(proxy_call),
ParentchainId::TargetA => ParentchainCall::TargetA(proxy_call),
ParentchainId::TargetB => ParentchainCall::TargetB(proxy_call),
ParentchainId::Integritee => ParentchainCall::Integritee {
call: proxy_call,
mortality: GenericMortality::immortal(),
},
ParentchainId::TargetA => ParentchainCall::TargetA {
call: proxy_call,
mortality: GenericMortality::immortal(),
},
ParentchainId::TargetB => ParentchainCall::TargetB {
call: proxy_call,
mortality: GenericMortality::immortal(),
},
};
calls.push(parentchain_call);
Ok(())
Expand All @@ -365,15 +374,18 @@ where
shield_funds(who, value)?;

// Send proof of execution on chain.
calls.push(ParentchainCall::Integritee(OpaqueCall::from_tuple(&(
node_metadata_repo
.get_from_metadata(|m| m.publish_hash_call_indexes())
.map_err(|_| StfError::InvalidMetadata)?
.map_err(|_| StfError::InvalidMetadata)?,
call_hash,
Vec::<itp_types::H256>::new(),
b"shielded some funds!".to_vec(),
))));
calls.push(ParentchainCall::Integritee {
call: OpaqueCall::from_tuple(&(
node_metadata_repo
.get_from_metadata(|m| m.publish_hash_call_indexes())
.map_err(|_| StfError::InvalidMetadata)?
.map_err(|_| StfError::InvalidMetadata)?,
call_hash,
Vec::<itp_types::H256>::new(),
b"shielded some funds!".to_vec(),
)),
mortality: GenericMortality::immortal(),
});
Ok(())
},
TrustedCall::timestamp_set(enclave_account, now, parentchain_id) => {
Expand Down
25 changes: 16 additions & 9 deletions core-primitives/extrinsics-factory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ use itp_node_api::{
metadata::{provider::AccessNodeMetadata, NodeMetadata},
};
use itp_nonce_cache::{MutateNonce, Nonce};
use itp_types::{parentchain::AccountId, OpaqueCall};
use itp_types::{
parentchain::{AccountId, GenericMortality},
OpaqueCall,
};
use sp_core::H256;
use sp_runtime::{generic::Era, OpaqueExtrinsic};
use sp_runtime::OpaqueExtrinsic;
use std::{sync::Arc, vec::Vec};
use substrate_api_client::ac_compose_macros::compose_extrinsic_offline;

Expand All @@ -55,7 +58,7 @@ pub mod mock;
pub trait CreateExtrinsics {
fn create_extrinsics(
&self,
calls: &[OpaqueCall],
calls: &[(OpaqueCall, GenericMortality)],
extrinsics_params: Option<ParentchainAdditionalParams>,
) -> Result<Vec<OpaqueExtrinsic>>;
}
Expand Down Expand Up @@ -108,24 +111,28 @@ where
{
fn create_extrinsics(
&self,
calls: &[OpaqueCall],
calls: &[(OpaqueCall, GenericMortality)],
extrinsics_params: Option<ParentchainAdditionalParams>,
) -> Result<Vec<OpaqueExtrinsic>> {
let mut nonce_lock = self.nonce_cache.load_for_mutation()?;
let mut nonce_value = nonce_lock.0;

let additional_extrinsic_params = extrinsics_params.unwrap_or_else(|| {
ParentchainAdditionalParams::new().era(Era::Immortal, self.genesis_hash).tip(0)
});

let (runtime_spec_version, runtime_transaction_version) =
self.node_metadata_repository.get_from_metadata(|m| {
(m.get_runtime_version(), m.get_runtime_transaction_version())
})?;

let extrinsics_buffer: Vec<OpaqueExtrinsic> = calls
.iter()
.map(|call| {
.map(|(call, mortality)| {
let additional_extrinsic_params = extrinsics_params.unwrap_or_else(|| {
ParentchainAdditionalParams::new()
.era(
mortality.era,
mortality.mortality_checkpoint.unwrap_or(self.genesis_hash),
)
.tip(0)
});
let extrinsic_params = ParentchainExtrinsicParams::new(
runtime_spec_version,
runtime_transaction_version,
Expand Down
4 changes: 2 additions & 2 deletions core-primitives/extrinsics-factory/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

use crate::{error::Result, CreateExtrinsics};
use itp_node_api::api_client::ParentchainAdditionalParams;
use itp_types::OpaqueCall;
use itp_types::{parentchain::GenericMortality, OpaqueCall};
use sp_runtime::OpaqueExtrinsic;
use std::vec::Vec;

Expand All @@ -30,7 +30,7 @@ pub struct ExtrinsicsFactoryMock;
impl CreateExtrinsics for ExtrinsicsFactoryMock {
fn create_extrinsics(
&self,
_calls: &[OpaqueCall],
_calls: &[(OpaqueCall, GenericMortality)],
_additional_params: Option<ParentchainAdditionalParams>,
) -> Result<Vec<OpaqueExtrinsic>> {
// Intention was to map an OpaqueCall to some dummy OpaqueExtrinsic,
Expand Down
18 changes: 9 additions & 9 deletions core-primitives/stf-executor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,17 @@ where

for call in extrinsic_call_backs.clone() {
match call {
ParentchainCall::Integritee(opaque_call) => trace!(
"trusted_call wants to send encoded call to [Integritee] parentchain: 0x{}",
hex::encode(opaque_call.encode())
ParentchainCall::Integritee { call, mortality } => trace!(
"trusted_call wants to send encoded call to [Integritee] parentchain: 0x{} with mortality {:?}",
hex::encode(call.encode()), mortality
),
ParentchainCall::TargetA(opaque_call) => trace!(
"trusted_call wants to send encoded call to [TargetA] parentchain: 0x{}",
hex::encode(opaque_call.encode())
ParentchainCall::TargetA { call, mortality } => trace!(
"trusted_call wants to send encoded call to [TargetA] parentchain: 0x{} with mortality {:?}",
hex::encode(call.encode()), mortality
),
ParentchainCall::TargetB(opaque_call) => trace!(
"trusted_call wants to send encoded call to [TargetB] parentchain: 0x{}",
hex::encode(opaque_call.encode())
ParentchainCall::TargetB { call, mortality } => trace!(
"trusted_call wants to send encoded call to [TargetB] parentchain: 0x{} with mortality {:?}",
hex::encode(call.encode()), mortality
),
}
}
Expand Down
47 changes: 31 additions & 16 deletions core-primitives/types/src/parentchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ use serde::{Deserialize, Serialize};
pub use sidechain_primitives::SidechainBlockConfirmation;
use sp_core::bounded::alloc;
use sp_runtime::{
generic::Header as HeaderG, traits::BlakeTwo256, DispatchError, MultiAddress, MultiSignature,
generic::{Era, Header as HeaderG},
traits::BlakeTwo256,
DispatchError, MultiAddress, MultiSignature,
};
use substrate_api_client::{
ac_node_api::StaticEvent,
Expand Down Expand Up @@ -283,52 +285,65 @@ impl From<ParentchainError> for () {
fn from(_: ParentchainError) -> Self {}
}

// All info for additionalParam except tip whi
#[derive(Encode, Debug, Clone, PartialEq, Eq)]
pub struct GenericMortality {
pub era: Era,
pub mortality_checkpoint: Option<Hash>,
}

impl GenericMortality {
pub fn immortal() -> Self {
Self { era: Era::Immortal, mortality_checkpoint: None }
}
}

/// a wrapper to target calls to specific parentchains
#[derive(Encode, Debug, Clone, PartialEq, Eq)]
pub enum ParentchainCall {
Integritee(OpaqueCall),
TargetA(OpaqueCall),
TargetB(OpaqueCall),
Integritee { call: OpaqueCall, mortality: GenericMortality },
TargetA { call: OpaqueCall, mortality: GenericMortality },
TargetB { call: OpaqueCall, mortality: GenericMortality },
}

impl ParentchainCall {
pub fn as_integritee(&self) -> Option<OpaqueCall> {
if let Self::Integritee(call) = self {
Some(call.clone())
pub fn as_integritee(&self) -> Option<(OpaqueCall, GenericMortality)> {
if let Self::Integritee { call, mortality } = self {
Some((call.clone(), mortality.clone()))
} else {
None
}
}
pub fn as_target_a(&self) -> Option<OpaqueCall> {
if let Self::TargetA(call) = self {
Some(call.clone())
pub fn as_target_a(&self) -> Option<(OpaqueCall, GenericMortality)> {
if let Self::TargetA { call, mortality } = self {
Some((call.clone(), mortality.clone()))
} else {
None
}
}
pub fn as_target_b(&self) -> Option<OpaqueCall> {
if let Self::TargetB(call) = self {
Some(call.clone())
pub fn as_target_b(&self) -> Option<(OpaqueCall, GenericMortality)> {
if let Self::TargetB { call, mortality } = self {
Some((call.clone(), mortality.clone()))
} else {
None
}
}
pub fn as_opaque_call_for(&self, parentchain_id: ParentchainId) -> Option<OpaqueCall> {
match parentchain_id {
ParentchainId::Integritee =>
if let Self::Integritee(call) = self {
if let Self::Integritee { call, mortality: _ } = self {
Some(call.clone())
} else {
None
},
ParentchainId::TargetA =>
if let Self::TargetA(call) = self {
if let Self::TargetA { call, mortality: _ } = self {
Some(call.clone())
} else {
None
},
ParentchainId::TargetB =>
if let Self::TargetB(call) = self {
if let Self::TargetB { call, mortality: _ } = self {
Some(call.clone())
} else {
None
Expand Down
15 changes: 9 additions & 6 deletions core/offchain-worker-executor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ use itp_stf_interface::system_pallet::SystemPalletEventInterface;
use itp_stf_primitives::{traits::TrustedCallVerification, types::TrustedOperationOrHash};
use itp_stf_state_handler::{handle_state::HandleState, query_shard_state::QueryShardState};
use itp_top_pool_author::traits::AuthorApi;
use itp_types::{parentchain::ParentchainCall, OpaqueCall, ShardIdentifier, H256};
use itp_types::{
parentchain::{GenericMortality, ParentchainCall},
OpaqueCall, ShardIdentifier, H256,
};
use log::*;
use sp_runtime::traits::Block;
use std::{marker::PhantomData, sync::Arc, time::Duration, vec::Vec};
Expand Down Expand Up @@ -185,15 +188,15 @@ impl<
}

fn send_parentchain_effects(&self, parentchain_effects: Vec<ParentchainCall>) -> Result<()> {
let integritee_calls: Vec<OpaqueCall> = parentchain_effects
let integritee_calls: Vec<(OpaqueCall, GenericMortality)> = parentchain_effects
.iter()
.filter_map(|parentchain_call| parentchain_call.as_integritee())
.collect();
let target_a_calls: Vec<OpaqueCall> = parentchain_effects
let target_a_calls: Vec<(OpaqueCall, GenericMortality)> = parentchain_effects
.iter()
.filter_map(|parentchain_call| parentchain_call.as_target_a())
.collect();
let target_b_calls: Vec<OpaqueCall> = parentchain_effects
let target_b_calls: Vec<(OpaqueCall, GenericMortality)> = parentchain_effects
.iter()
.filter_map(|parentchain_call| parentchain_call.as_target_b())
.collect();
Expand All @@ -204,10 +207,10 @@ impl<
target_b_calls.len()
);
if !target_a_calls.is_empty() {
warn!("sending extrinsics to target A unimplemented")
warn!("sending extrinsics to target A unimplemented for OCW")
};
if !target_b_calls.is_empty() {
warn!("sending extrinsics to target B unimplemented")
warn!("sending extrinsics to target B unimplemented for OCW")
};

let extrinsics =
Expand Down
18 changes: 13 additions & 5 deletions core/parentchain/block-importer/src/block_importer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ use itp_extrinsics_factory::CreateExtrinsics;
use itp_stf_executor::traits::StfUpdateState;
use itp_stf_interface::ShardCreationInfo;
use itp_types::{
parentchain::{IdentifyParentchain, ParentchainId},
parentchain::{GenericMortality, IdentifyParentchain, ParentchainId},
OpaqueCall, H256,
};
use log::*;
use sp_runtime::{
generic::SignedBlock as SignedBlockG,
generic::{Era, SignedBlock as SignedBlockG},
traits::{Block as ParentchainBlockTrait, Header as HeaderT, NumberFor},
};
use std::{marker::PhantomData, sync::Arc, vec, vec::Vec};
Expand Down Expand Up @@ -118,7 +118,7 @@ impl<
blocks_to_import: Vec<Self::SignedBlockType>,
events_to_import: Vec<Vec<u8>>,
) -> Result<()> {
let mut calls = Vec::<OpaqueCall>::new();
let mut calls = Vec::<(OpaqueCall, GenericMortality)>::new();
let id = self.validator_accessor.parentchain_id();

debug!(
Expand Down Expand Up @@ -172,7 +172,15 @@ impl<
.execute_indirect_calls_in_extrinsics(&block, &raw_events)
{
Ok(Some(confirm_processed_parentchain_block_call)) => {
calls.push(confirm_processed_parentchain_block_call);
let opaque_call = confirm_processed_parentchain_block_call;
// if we have significant downtime, this mortality means we will not confirm all imported blocks
/*
let mortality = GenericMortality {
era: Era::Mortal(300, 0),
mortality_checkpoint: Some(block.hash()),
};*/
let mortality = GenericMortality::immortal();
calls.push((opaque_call, mortality));
},
Ok(None) => trace!("omitting confirmation call to non-integritee parentchain"),
Err(e) => error!("[{:?}] Error executing relevant extrinsics: {:?}", id, e),
Expand All @@ -186,7 +194,7 @@ impl<
);
}

// Create extrinsics for all `unshielding` and `block processed` calls we've gathered.
// Create extrinsics for all `block processed` calls we've gathered.
let parentchain_extrinsics =
self.extrinsics_factory.create_extrinsics(calls.as_slice(), None)?;

Expand Down
5 changes: 3 additions & 2 deletions enclave-runtime/src/attestation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use itp_node_api::metadata::{
};
use itp_node_api_metadata::NodeMetadata;
use itp_settings::worker::MR_ENCLAVE_SIZE;
use itp_types::OpaqueCall;
use itp_types::{parentchain::GenericMortality, OpaqueCall};
use itp_utils::write_slice_and_whitespace_pad;
use log::*;
use sgx_types::*;
Expand Down Expand Up @@ -390,7 +390,8 @@ pub fn generate_ias_skip_ra_extrinsic_from_der_cert_internal(

fn create_extrinsics(call: OpaqueCall) -> EnclaveResult<OpaqueExtrinsic> {
let extrinsics_factory = get_extrinsic_factory_from_integritee_solo_or_parachain()?;
let extrinsics = extrinsics_factory.create_extrinsics(&[call], None)?;
let extrinsics =
extrinsics_factory.create_extrinsics(&[(call, GenericMortality::immortal())], None)?;

Ok(extrinsics[0].clone())
}
Expand Down
3 changes: 2 additions & 1 deletion enclave-runtime/src/shard_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use itp_types::{
use itp_utils::hex::hex_encode;
use log::*;

use itp_types::parentchain::GenericMortality;
use teerex_primitives::EnclaveFingerprint;

pub(crate) fn init_shard_config(shard: ShardIdentifier) -> EnclaveResult<()> {
Expand All @@ -56,7 +57,7 @@ pub(crate) fn init_shard_config(shard: ShardIdentifier) -> EnclaveResult<()> {
let opaque_call = OpaqueCall::from_tuple(&(call, shard, shard_config, BlockNumber::from(0u8)));
debug!("encoded call: {}", hex_encode(opaque_call.encode().as_slice()));
let xts = extrinsics_factory
.create_extrinsics(&[opaque_call], None)
.create_extrinsics(&[(opaque_call, GenericMortality::immortal())], None)
.map_err(|e| Error::Other(e.into()))?;

info!("Initializing or touching shard config on integritee network. awaiting inclusion before continuing");
Expand Down
Loading

0 comments on commit ae60b49

Please sign in to comment.