-
Notifications
You must be signed in to change notification settings - Fork 246
/
processor.rs
414 lines (361 loc) · 11.4 KB
/
processor.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
use rand::{seq::SliceRandom, SeedableRng};
use rand_chacha::ChaCha8Rng;
use sc_client_api::{BlockBackend, ProofProvider};
use sc_consensus::{
BlockImport, BlockImportParams, ForkChoiceStrategy, StateAction, StorageChanges,
};
use sp_api::ProvideRuntimeApi;
use sp_consensus::BlockOrigin;
use sp_inherents::{CreateInherentDataProviders, InherentData, InherentDataProvider};
use sp_runtime::{
generic::BlockId,
traits::{BlakeTwo256, Block as BlockT, Hash as HashT, Header as HeaderT},
};
use std::{
collections::{BTreeMap, VecDeque},
fmt::Debug,
};
use cirrus_block_builder::{BlockBuilder, BuiltBlock, RecordProof};
use cirrus_node_primitives::ProcessorResult;
use cirrus_primitives::{AccountId, SecondaryApi};
use sp_executor::{ExecutionReceipt, OpaqueBundle};
use subspace_core_primitives::Randomness;
use subspace_runtime_primitives::Hash as PHash;
use super::{Executor, LOG_TARGET};
use codec::{Decode, Encode};
/// Shuffles the extrinsics in a deterministic way.
///
/// The extrinsics are grouped by the signer. The extrinsics without a signer, i.e., unsigned
/// extrinsics, are considered as a special group. The items in different groups are cross shuffled,
/// while the order of items inside the same group is still maintained.
fn shuffle_extrinsics<Extrinsic: Debug>(
extrinsics: Vec<(Option<AccountId>, Extrinsic)>,
shuffling_seed: Randomness,
) -> Vec<Extrinsic> {
let mut rng = ChaCha8Rng::from_seed(shuffling_seed);
let mut positions = extrinsics
.iter()
.map(|(maybe_signer, _)| maybe_signer)
.cloned()
.collect::<Vec<_>>();
// Shuffles the positions using Fisher–Yates algorithm.
positions.shuffle(&mut rng);
let mut grouped_extrinsics: BTreeMap<Option<AccountId>, VecDeque<_>> =
extrinsics.into_iter().fold(BTreeMap::new(), |mut groups, (maybe_signer, tx)| {
groups.entry(maybe_signer).or_insert_with(VecDeque::new).push_back(tx);
groups
});
// The relative ordering for the items in the same group does not change.
let shuffled_extrinsics = positions
.into_iter()
.map(|maybe_signer| {
grouped_extrinsics
.get_mut(&maybe_signer)
.expect("Extrinsics are grouped correctly; qed")
.pop_front()
.expect("Extrinsic definitely exists as it's correctly grouped above; qed")
})
.collect::<Vec<_>>();
tracing::trace!(target: LOG_TARGET, ?shuffled_extrinsics, "Shuffled extrinsics");
shuffled_extrinsics
}
impl<Block, BS, RA, Client, TransactionPool, Backend, CIDP>
Executor<Block, BS, RA, Client, TransactionPool, Backend, CIDP>
where
Block: BlockT,
Client: sp_blockchain::HeaderBackend<Block> + ProofProvider<Block>,
for<'b> &'b Client: sc_consensus::BlockImport<
Block,
Transaction = sp_api::TransactionFor<RA, Block>,
Error = sp_consensus::Error,
>,
BS: BlockBackend<Block>,
Backend: sc_client_api::Backend<Block>,
RA: ProvideRuntimeApi<Block>,
RA::Api: SecondaryApi<Block, AccountId>
+ sp_block_builder::BlockBuilder<Block>
+ sp_api::ApiExt<
Block,
StateBackend = sc_client_api::backend::StateBackendFor<Backend, Block>,
>,
TransactionPool: sc_transaction_pool_api::TransactionPool<Block = Block>,
CIDP: CreateInherentDataProviders<Block, cirrus_primitives::Hash>,
{
/// Actually implements `process_bundles`.
pub(super) async fn process_bundles_impl(
&self,
primary_hash: PHash,
bundles: Vec<OpaqueBundle>,
shuffling_seed: Randomness,
) -> Result<Option<ProcessorResult>, sp_blockchain::Error> {
let mut extrinsics = bundles
.into_iter()
.flat_map(|bundle| {
bundle.opaque_extrinsics.into_iter().filter_map(|opaque_extrinsic| {
match <<Block as BlockT>::Extrinsic>::decode(
&mut opaque_extrinsic.encode().as_slice(),
) {
Ok(uxt) => Some(uxt),
Err(e) => {
tracing::error!(
target: LOG_TARGET,
error = ?e,
"Failed to decode the opaque extrisic in bundle, this should not happen"
);
None
},
}
})
})
.collect::<Vec<_>>();
// TODO: or just Vec::new()?
// Ideally there should be only a few duplicated transactions.
let mut seen = Vec::with_capacity(extrinsics.len());
extrinsics.retain(|uxt| match seen.contains(uxt) {
true => {
tracing::trace!(target: LOG_TARGET, extrinsic = ?uxt, "Duplicated extrinsic");
false
},
false => {
seen.push(uxt.clone());
true
},
});
drop(seen);
tracing::trace!(target: LOG_TARGET, ?extrinsics, "Origin deduplicated extrinsics");
let parent_hash = self.client.info().best_hash;
let parent_number = self.client.info().best_number;
let extrinsics: Vec<_> = match self
.runtime_api
.runtime_api()
.extract_signer(&BlockId::Hash(parent_hash), extrinsics)
{
Ok(res) => res,
Err(e) => {
tracing::error!(
target: LOG_TARGET,
error = ?e,
"Error at calling runtime api: extract_signer"
);
return Err(e.into())
},
};
let extrinsics =
shuffle_extrinsics::<<Block as BlockT>::Extrinsic>(extrinsics, shuffling_seed);
let mut block_builder = BlockBuilder::new(
&*self.runtime_api,
parent_hash,
parent_number,
RecordProof::No,
Default::default(),
&*self.backend,
&*self.client,
)?;
let inherent_data = self.inherent_data(parent_hash, primary_hash).await?;
let mut final_extrinsics = block_builder.create_inherents(inherent_data)?;
final_extrinsics.extend(extrinsics);
block_builder.set_extrinsics(final_extrinsics);
let BuiltBlock { block, storage_changes, proof: _ } = block_builder.build()?;
let (header, body) = block.deconstruct();
let state_root = *header.state_root();
let header_hash = header.hash();
let block_import_params = {
let mut import_block = BlockImportParams::new(BlockOrigin::Own, header);
import_block.body = Some(body);
import_block.state_action =
StateAction::ApplyChanges(StorageChanges::Changes(storage_changes));
// TODO: double check the fork choice is correct, see also ParachainBlockImport.
import_block.fork_choice = Some(ForkChoiceStrategy::LongestChain);
import_block
};
(&*self.client).import_block(block_import_params, Default::default()).await?;
let mut roots =
self.runtime_api.runtime_api().intermediate_roots(&BlockId::Hash(parent_hash))?;
roots.push(state_root.encode());
tracing::debug!(
target: LOG_TARGET,
intermediate_roots = ?roots
.iter()
.map(|r| {
Block::Hash::decode(&mut r.clone().as_slice())
.expect("Intermediate root uses the same Block hash type; qed")
})
.collect::<Vec<_>>(),
final_state_root = ?state_root,
"Calculating the state transition root for #{}", header_hash
);
let state_transition_root =
BlakeTwo256::ordered_trie_root(roots, sp_core::storage::StateVersion::V1);
let execution_receipt = ExecutionReceipt {
primary_hash,
secondary_hash: header_hash,
state_root,
state_transition_root,
};
// The applied txs can be fully removed from the transaction pool
// TODO: win the executor election to broadcast ER.
let is_elected = true;
if is_elected {
if let Err(e) = self.execution_receipt_sender.unbounded_send(execution_receipt.clone())
{
tracing::error!(target: LOG_TARGET, error = ?e, "Failed to send execution receipt");
}
// Return `Some(_)` to broadcast ER to all farmers via unsigned extrinsic.
Ok(Some(ProcessorResult { opaque_execution_receipt: execution_receipt.into() }))
} else {
Ok(None)
}
}
/// Get the inherent data.
async fn inherent_data(
&self,
parent: Block::Hash,
relay_parent: PHash,
) -> Result<InherentData, sp_blockchain::Error> {
let inherent_data_providers = self
.create_inherent_data_providers
.create_inherent_data_providers(parent, relay_parent)
.await?;
inherent_data_providers.create_inherent_data().map_err(|e| {
tracing::error!(
target: LOG_TARGET,
error = ?e,
"Failed to create inherent data.",
);
sp_blockchain::Error::Consensus(sp_consensus::Error::InherentData(e.into()))
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use sp_keyring::sr25519::Keyring;
use sp_runtime::traits::{BlakeTwo256, Hash as HashT};
#[test]
fn shuffle_extrinsics_should_work() {
let alice = Keyring::Alice.to_account_id();
let bob = Keyring::Bob.to_account_id();
let charlie = Keyring::Charlie.to_account_id();
let extrinsics = vec![
(Some(alice.clone()), 10),
(None, 100),
(Some(bob.clone()), 1),
(Some(bob), 2),
(Some(charlie.clone()), 30),
(Some(alice.clone()), 11),
(Some(charlie), 31),
(None, 101),
(None, 102),
(Some(alice), 12),
];
let dummy_seed = BlakeTwo256::hash_of(&[1u8; 64]).into();
let shuffled_extrinsics = shuffle_extrinsics(extrinsics, dummy_seed);
assert_eq!(shuffled_extrinsics, vec![100, 30, 10, 1, 11, 101, 31, 12, 102, 2]);
}
#[test]
fn test_generate_proof() {
use sc_client_api::{HeaderBackend, StateBackend};
use sp_core::Blake2Hasher;
use sp_keyring::AccountKeyring;
use substrate_test_runtime::Transfer;
use substrate_test_runtime_client::{DefaultTestClientBuilderExt, TestClientBuilderExt};
let builder = substrate_test_runtime_client::TestClientBuilder::new();
let backend = builder.backend();
let client = builder.build();
let transfer_tx = Transfer {
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Bob.into(),
amount: 1,
nonce: 0,
}
.into_signed_tx();
let mut block_builder = BlockBuilder::new(
&client,
client.info().best_hash,
client.info().best_number,
RecordProof::Yes,
Default::default(),
&*backend,
&client,
)
.unwrap();
block_builder.set_extrinsics(vec![transfer_tx.clone()]);
let first_proof = block_builder.first_extrinsic_execution_proof().unwrap().unwrap();
let mut block_builder = BlockBuilder::new(
&client,
client.info().best_hash,
client.info().best_number,
RecordProof::Yes,
Default::default(),
&*backend,
&client,
)
.unwrap();
block_builder.set_extrinsics(vec![transfer_tx]);
let second_proof = block_builder.execution_proof(0).unwrap();
println!("====== first proof: {:?}", first_proof);
println!("====== second proof: {:?}", second_proof);
assert_eq!(first_proof, second_proof);
/*
let extrinsics = vec![
Transfer {
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Bob.into(),
amount: 1,
nonce: 0,
},
Transfer {
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Charlie.into(),
amount: 2,
nonce: 1,
},
Transfer {
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Dave.into(),
amount: 3,
nonce: 2,
},
]
.into_iter()
.map(Transfer::into_signed_tx)
.collect::<Vec<_>>();
let bad_extrinsics = vec![
Transfer {
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Bob.into(),
amount: 1,
nonce: 0,
},
Transfer {
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Bob.into(),
amount: 2,
nonce: 1,
},
Transfer {
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Bob.into(),
amount: 3,
nonce: 2,
},
]
.into_iter()
.map(Transfer::into_signed_tx)
.collect::<Vec<_>>();
block_builder.set_extrinsics(bad_extrinsics);
let block = block_builder.build().unwrap();
println!("block.proof: {:?}", block.proof);
let proof = block.proof.expect("Proof is build on request");
let backend = sp_state_machine::create_proof_check_backend::<Blake2Hasher>(
block.storage_changes.transaction_storage_root,
proof,
)
.unwrap();
assert!(backend
.storage(&sp_core::storage::well_known_keys::CODE)
.unwrap_err()
.contains("Database missing expected key"),);
*/
}
}