-
Notifications
You must be signed in to change notification settings - Fork 46
/
parentchain_handler.rs
236 lines (201 loc) · 7.47 KB
/
parentchain_handler.rs
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
Copyright 2021 Integritee AG and Supercomputing Systems AG
Copyright (C) 2017-2019 Baidu, Inc. All Rights Reserved.
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.
*/
use crate::error::{Error, ServiceResult};
use codec::{Decode, Encode};
use itc_parentchain::{
light_client::light_client_init_params::{GrandpaParams, SimpleParams},
primitives::{ParentchainId, ParentchainInitParams},
};
use itp_api_client_types::ParentchainApi;
use itp_enclave_api::{enclave_base::EnclaveBase, sidechain::Sidechain};
use itp_node_api::api_client::ChainApi;
use itp_storage::StorageProof;
use log::*;
use my_node_runtime::Header;
use sp_consensus_grandpa::VersionedAuthorityList;
use sp_runtime::traits::Header as HeaderTrait;
use std::{cmp::min, sync::Arc};
use substrate_api_client::ac_primitives::{Block, Header as HeaderT};
const BLOCK_SYNC_BATCH_SIZE: u32 = 1000;
pub trait HandleParentchain {
/// Initializes all parentchain specific components on the enclave side.
/// Returns the latest synced block header.
fn init_parentchain_components(&self) -> ServiceResult<Header>;
/// Fetches the parentchain blocks to sync from the parentchain and feeds them to the enclave.
/// Returns the latest synced block header.
fn sync_parentchain(&self, last_synced_header: Header) -> ServiceResult<Header>;
/// Triggers the import of the synced parentchain blocks inside the enclave.
fn trigger_parentchain_block_import(&self) -> ServiceResult<()>;
/// Syncs and directly imports parentchain blocks from the latest synced header
/// until the specified until_header.
fn sync_and_import_parentchain_until(
&self,
last_synced_header: &Header,
until_header: &Header,
) -> ServiceResult<Header>;
}
/// Handles the interaction between parentchain and enclave.
pub(crate) struct ParentchainHandler<ParentchainApi, EnclaveApi> {
parentchain_api: ParentchainApi,
enclave_api: Arc<EnclaveApi>,
parentchain_init_params: ParentchainInitParams,
}
// #TODO: #1451: Reintroduce `ParentchainApi: ChainApi` once there is no trait bound conflict
// any more with the api-clients own trait definitions.
impl<EnclaveApi> ParentchainHandler<ParentchainApi, EnclaveApi>
where
EnclaveApi: EnclaveBase,
{
pub fn new(
parentchain_api: ParentchainApi,
enclave_api: Arc<EnclaveApi>,
parentchain_init_params: ParentchainInitParams,
) -> Self {
Self { parentchain_api, enclave_api, parentchain_init_params }
}
// FIXME: Necessary in the future? Fix with #1080
pub fn new_with_automatic_light_client_allocation(
parentchain_api: ParentchainApi,
enclave_api: Arc<EnclaveApi>,
id: ParentchainId,
) -> ServiceResult<Self> {
let genesis_hash = parentchain_api.get_genesis_hash()?;
let genesis_header =
parentchain_api.header(Some(genesis_hash))?.ok_or(Error::MissingGenesisHeader)?;
let parentchain_init_params: ParentchainInitParams = if parentchain_api
.is_grandpa_available()?
{
let grandpas = parentchain_api.grandpa_authorities(Some(genesis_hash))?;
let grandpa_proof = parentchain_api.grandpa_authorities_proof(Some(genesis_hash))?;
debug!("[{:?}] Grandpa Authority List: \n {:?} \n ", id, grandpas);
let authority_list = VersionedAuthorityList::from(grandpas);
(
id,
GrandpaParams::new(
// #TODO: #1451: clean up type hacks
Header::decode(&mut genesis_header.encode().as_slice())?,
authority_list.into(),
grandpa_proof,
),
)
.into()
} else {
(
id,
SimpleParams::new(
// #TODO: #1451: clean up type hacks
Header::decode(&mut genesis_header.encode().as_slice())?,
),
)
.into()
};
Ok(Self::new(parentchain_api, enclave_api, parentchain_init_params))
}
pub fn parentchain_api(&self) -> &ParentchainApi {
&self.parentchain_api
}
pub fn parentchain_id(&self) -> &ParentchainId {
self.parentchain_init_params.id()
}
}
impl<EnclaveApi> HandleParentchain for ParentchainHandler<ParentchainApi, EnclaveApi>
where
EnclaveApi: Sidechain + EnclaveBase,
{
fn init_parentchain_components(&self) -> ServiceResult<Header> {
Ok(self
.enclave_api
.init_parentchain_components(self.parentchain_init_params.clone())?)
}
fn sync_parentchain(&self, last_synced_header: Header) -> ServiceResult<Header> {
let id = self.parentchain_id();
trace!("[{:?}] Getting current head", id);
let curr_block = self
.parentchain_api
.last_finalized_block()?
.ok_or(Error::MissingLastFinalizedBlock)?;
let curr_block_number = curr_block.block.header().number();
println!(
"[{:?}] Syncing blocks from {} to {}",
id, last_synced_header.number, curr_block_number
);
let mut until_synced_header = last_synced_header;
loop {
let block_chunk_to_sync = self.parentchain_api.get_blocks(
until_synced_header.number + 1,
min(until_synced_header.number + BLOCK_SYNC_BATCH_SIZE, curr_block_number),
)?;
println!("[+] [{:?}] Found {} block(s) to sync", id, block_chunk_to_sync.len());
if block_chunk_to_sync.is_empty() {
return Ok(until_synced_header)
}
let events_chunk_to_sync: Vec<Vec<u8>> = block_chunk_to_sync
.iter()
.map(|block| {
self.parentchain_api.get_events_for_block(Some(block.block.header.hash()))
})
.collect::<Result<Vec<_>, _>>()?;
println!("[+] [{:?}] Found {} event vector(s) to sync", id, events_chunk_to_sync.len());
let events_proofs_chunk_to_sync: Vec<StorageProof> = block_chunk_to_sync
.iter()
.map(|block| {
self.parentchain_api.get_events_value_proof(Some(block.block.header.hash()))
})
.collect::<Result<Vec<_>, _>>()?;
self.enclave_api.sync_parentchain(
block_chunk_to_sync.as_slice(),
events_chunk_to_sync.as_slice(),
events_proofs_chunk_to_sync.as_slice(),
self.parentchain_id(),
)?;
let api_client_until_synced_header = block_chunk_to_sync
.last()
.map(|b| b.block.header.clone())
.ok_or(Error::EmptyChunk)?;
println!(
"[{:?}] Synced {} out of {} finalized parentchain blocks",
id, until_synced_header.number, curr_block_number,
);
// #TODO: #1451: fix api/client types
until_synced_header =
Header::decode(&mut api_client_until_synced_header.encode().as_slice())
.expect("Can decode previously encoded header; qed");
}
}
fn trigger_parentchain_block_import(&self) -> ServiceResult<()> {
trace!("[{:?}] trigger parentchain block import", self.parentchain_id());
Ok(self.enclave_api.trigger_parentchain_block_import(self.parentchain_id())?)
}
fn sync_and_import_parentchain_until(
&self,
last_synced_header: &Header,
until_header: &Header,
) -> ServiceResult<Header> {
let id = self.parentchain_id();
trace!(
"[{:?}] last synced block number: {}. synching until {}",
id,
last_synced_header.number,
until_header.number
);
let mut last_synced_header = last_synced_header.clone();
while last_synced_header.number() < until_header.number() {
last_synced_header = self.sync_parentchain(last_synced_header)?;
trace!("[{:?}] synced block number: {}", id, last_synced_header.number);
std::thread::sleep(std::time::Duration::from_secs(1));
}
self.trigger_parentchain_block_import()?;
Ok(last_synced_header)
}
}