-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathmod.rs
More file actions
115 lines (105 loc) · 3.82 KB
/
Copy pathmod.rs
File metadata and controls
115 lines (105 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
Copyright 2021 Integritee 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.
*/
mod event_filter;
mod event_handler;
mod extrinsic_parser;
use crate::{
decode_and_log_error,
indirect_calls::{
transfer_to_alice_shields_funds::TransferToAliceShieldsFundsArgs, ALICE_ACCOUNT_ID,
},
};
use codec::{Decode, Encode};
use core::marker::PhantomData;
pub use event_filter::FilterableEvents;
pub use event_handler::ParentchainEventHandler;
use extrinsic_parser::ParseExtrinsic;
pub use extrinsic_parser::{
ParentchainAdditionalParams, ParentchainAdditionalSigned, ParentchainExtrinsicParams,
ParentchainExtrinsicParser, ParentchainSignedExtra,
};
use ita_stf::TrustedCallSigned;
use itc_parentchain_indirect_calls_executor::{
error::{Error, Result},
filter_metadata::FilterIntoDataFrom,
IndirectDispatch,
};
use itp_node_api::metadata::pallet_balances::BalancesCallIndexes;
use itp_stf_primitives::traits::IndirectExecutor;
use log::trace;
/// The default indirect call (extrinsic-triggered) of the Target-A-Parachain.
#[derive(Debug, Clone, Encode, Decode, Eq, PartialEq)]
pub enum IndirectCall {
TransferToAliceShieldsFunds(TransferToAliceShieldsFundsArgs),
}
impl<Executor: IndirectExecutor<TrustedCallSigned, Error>>
IndirectDispatch<Executor, TrustedCallSigned> for IndirectCall
{
fn dispatch(&self, executor: &Executor) -> Result<()> {
trace!("dispatching indirect call {:?}", self);
match self {
IndirectCall::TransferToAliceShieldsFunds(args) => args.dispatch(executor),
}
}
}
/// Simple demo filter for testing.
///
/// A transfer to Alice will issue the corresponding balance to Alice in the enclave.
/// It does not do anything else.
pub struct TransferToAliceShieldsFundsFilter<ExtrinsicParser> {
_phantom: PhantomData<ExtrinsicParser>,
}
impl<ExtrinsicParser, NodeMetadata: BalancesCallIndexes> FilterIntoDataFrom<NodeMetadata>
for TransferToAliceShieldsFundsFilter<ExtrinsicParser>
where
ExtrinsicParser: ParseExtrinsic,
{
type Output = IndirectCall;
type ParseParentchainMetadata = ExtrinsicParser;
fn filter_into_from_metadata(
encoded_data: &[u8],
metadata: &NodeMetadata,
) -> Option<Self::Output> {
let call_mut = &mut &encoded_data[..];
// Todo: the filter should not need to parse, only filter. This should directly be configured
// in the indirect executor.
let xt = match Self::ParseParentchainMetadata::parse(call_mut) {
Ok(xt) => xt,
Err(e) => {
log::error!("[TransferToAliceShieldsFundsFilter] Could not parse parentchain extrinsic: {:?}", e);
return None
},
};
let index = xt.call_index;
let call_args = &mut &xt.call_args[..];
log::trace!("[TransferToAliceShieldsFundsFilter] attempting to execute indirect call with index {:?}", index);
if index == metadata.transfer_call_indexes().ok()?
|| index == metadata.transfer_keep_alive_call_indexes().ok()?
|| index == metadata.transfer_allow_death_call_indexes().ok()?
{
log::debug!(
"found `transfer` or `transfer_allow_death` or `transfer_keep_alive` call."
);
let args = decode_and_log_error::<TransferToAliceShieldsFundsArgs>(call_args)?;
if args.destination == ALICE_ACCOUNT_ID.into() {
Some(IndirectCall::TransferToAliceShieldsFunds(args))
} else {
log::debug!("Parentchain transfer was not for Alice; ignoring...");
// No need to put it into the top pool if it isn't executed in the first place.
None
}
} else {
None
}
}
}