@@ -130,7 +130,7 @@ use sp_runtime::{
130
130
transaction_validity:: { TransactionValidity , TransactionSource } ,
131
131
} ;
132
132
use codec:: { Codec , Encode } ;
133
- use frame_system:: { extrinsics_root , DigestOf } ;
133
+ use frame_system:: DigestOf ;
134
134
135
135
/// Trait that can be used to execute a block.
136
136
pub trait ExecuteBlock < Block : BlockT > {
@@ -213,7 +213,6 @@ where
213
213
Self :: initialize_block_impl (
214
214
header. number ( ) ,
215
215
header. parent_hash ( ) ,
216
- header. extrinsics_root ( ) ,
217
216
& digests
218
217
) ;
219
218
}
@@ -231,7 +230,6 @@ where
231
230
fn initialize_block_impl (
232
231
block_number : & System :: BlockNumber ,
233
232
parent_hash : & System :: Hash ,
234
- extrinsics_root : & System :: Hash ,
235
233
digest : & Digest < System :: Hash > ,
236
234
) {
237
235
let mut weight = 0 ;
@@ -244,7 +242,6 @@ where
244
242
<frame_system:: Module < System > >:: initialize (
245
243
block_number,
246
244
parent_hash,
247
- extrinsics_root,
248
245
digest,
249
246
frame_system:: InitKind :: Full ,
250
247
) ;
@@ -286,13 +283,8 @@ where
286
283
assert ! (
287
284
n > System :: BlockNumber :: zero( )
288
285
&& <frame_system:: Module <System >>:: block_hash( n - System :: BlockNumber :: one( ) ) == * header. parent_hash( ) ,
289
- "Parent hash should be valid."
286
+ "Parent hash should be valid." ,
290
287
) ;
291
-
292
- // Check that transaction trie root represents the transactions.
293
- let xts_root = extrinsics_root :: < System :: Hashing , _ > ( & block. extrinsics ( ) ) ;
294
- header. extrinsics_root ( ) . check_equal ( & xts_root) ;
295
- assert ! ( header. extrinsics_root( ) == & xts_root, "Transaction trie root must be valid." ) ;
296
288
}
297
289
298
290
/// Actually execute all transitions for `block`.
@@ -322,8 +314,14 @@ where
322
314
}
323
315
324
316
/// Execute given extrinsics and take care of post-extrinsics book-keeping.
325
- fn execute_extrinsics_with_book_keeping ( extrinsics : Vec < Block :: Extrinsic > , block_number : NumberFor < Block > ) {
326
- extrinsics. into_iter ( ) . for_each ( Self :: apply_extrinsic_no_note) ;
317
+ fn execute_extrinsics_with_book_keeping (
318
+ extrinsics : Vec < Block :: Extrinsic > ,
319
+ block_number : NumberFor < Block > ,
320
+ ) {
321
+ extrinsics. into_iter ( ) . for_each ( |e| if let Err ( e) = Self :: apply_extrinsic ( e) {
322
+ let err: & ' static str = e. into ( ) ;
323
+ panic ! ( err)
324
+ } ) ;
327
325
328
326
// post-extrinsics book-keeping
329
327
<frame_system:: Module < System > >:: note_finished_extrinsics ( ) ;
@@ -341,8 +339,6 @@ where
341
339
<frame_system:: Module < System > as OnFinalize < System :: BlockNumber > >:: on_finalize ( block_number) ;
342
340
<AllModules as OnFinalize < System :: BlockNumber > >:: on_finalize ( block_number) ;
343
341
344
- // set up extrinsics
345
- <frame_system:: Module < System > >:: derive_extrinsics ( ) ;
346
342
<frame_system:: Module < System > >:: finalize ( )
347
343
}
348
344
@@ -354,23 +350,14 @@ where
354
350
sp_io:: init_tracing ( ) ;
355
351
let encoded = uxt. encode ( ) ;
356
352
let encoded_len = encoded. len ( ) ;
357
- Self :: apply_extrinsic_with_len ( uxt, encoded_len, Some ( encoded) )
358
- }
359
-
360
- /// Apply an extrinsic inside the block execution function.
361
- fn apply_extrinsic_no_note ( uxt : Block :: Extrinsic ) {
362
- let l = uxt. encode ( ) . len ( ) ;
363
- match Self :: apply_extrinsic_with_len ( uxt, l, None ) {
364
- Ok ( _) => ( ) ,
365
- Err ( e) => { let err: & ' static str = e. into ( ) ; panic ! ( err) } ,
366
- }
353
+ Self :: apply_extrinsic_with_len ( uxt, encoded_len, encoded)
367
354
}
368
355
369
356
/// Actually apply an extrinsic given its `encoded_len`; this doesn't note its hash.
370
357
fn apply_extrinsic_with_len (
371
358
uxt : Block :: Extrinsic ,
372
359
encoded_len : usize ,
373
- to_note : Option < Vec < u8 > > ,
360
+ to_note : Vec < u8 > ,
374
361
) -> ApplyExtrinsicResult {
375
362
sp_tracing:: enter_span!(
376
363
sp_tracing:: info_span!( "apply_extrinsic" ,
@@ -382,9 +369,7 @@ where
382
369
// We don't need to make sure to `note_extrinsic` only after we know it's going to be
383
370
// executed to prevent it from leaking in storage since at this point, it will either
384
371
// execute or panic (and revert storage changes).
385
- if let Some ( encoded) = to_note {
386
- <frame_system:: Module < System > >:: note_extrinsic ( encoded) ;
387
- }
372
+ <frame_system:: Module < System > >:: note_extrinsic ( to_note) ;
388
373
389
374
// AUDIT: Under no circumstances may this function panic from here onwards.
390
375
@@ -418,6 +403,11 @@ where
418
403
let storage_root = new_header. state_root ( ) ;
419
404
header. state_root ( ) . check_equal ( & storage_root) ;
420
405
assert ! ( header. state_root( ) == storage_root, "Storage root must match that calculated." ) ;
406
+
407
+ assert ! (
408
+ header. extrinsics_root( ) == new_header. extrinsics_root( ) ,
409
+ "Transaction trie root must be valid." ,
410
+ ) ;
421
411
}
422
412
423
413
/// Check a given signed transaction for validity. This doesn't execute any
@@ -462,7 +452,6 @@ where
462
452
<frame_system:: Module < System > >:: initialize (
463
453
header. number ( ) ,
464
454
header. parent_hash ( ) ,
465
- header. extrinsics_root ( ) ,
466
455
& digests,
467
456
frame_system:: InitKind :: Inspection ,
468
457
) ;
@@ -558,6 +547,12 @@ mod tests {
558
547
fn offchain_worker( n: T :: BlockNumber ) {
559
548
assert_eq!( T :: BlockNumber :: from( 1u32 ) , n) ;
560
549
}
550
+
551
+ #[ weight = 0 ]
552
+ fn calculate_storage_root( origin) {
553
+ let root = sp_io:: storage:: root( ) ;
554
+ sp_io:: storage:: set( "storage_root" . as_bytes( ) , & root) ;
555
+ }
561
556
}
562
557
}
563
558
@@ -1153,4 +1148,29 @@ mod tests {
1153
1148
assert_eq ! ( header. hash( ) , System :: block_hash( 1 ) ) ;
1154
1149
} ) ;
1155
1150
}
1151
+
1152
+ #[ test]
1153
+ fn calculating_storage_root_twice_works ( ) {
1154
+ let call = Call :: Custom ( custom:: Call :: calculate_storage_root ( ) ) ;
1155
+ let xt = TestXt :: new ( call, sign_extra ( 1 , 0 , 0 ) ) ;
1156
+
1157
+ let header = new_test_ext ( 1 ) . execute_with ( || {
1158
+ // Let's build some fake block.
1159
+ Executive :: initialize_block ( & Header :: new (
1160
+ 1 ,
1161
+ H256 :: default ( ) ,
1162
+ H256 :: default ( ) ,
1163
+ [ 69u8 ; 32 ] . into ( ) ,
1164
+ Digest :: default ( ) ,
1165
+ ) ) ;
1166
+
1167
+ Executive :: apply_extrinsic ( xt. clone ( ) ) . unwrap ( ) . unwrap ( ) ;
1168
+
1169
+ Executive :: finalize_block ( )
1170
+ } ) ;
1171
+
1172
+ new_test_ext ( 1 ) . execute_with ( || {
1173
+ Executive :: execute_block ( Block :: new ( header, vec ! [ xt] ) ) ;
1174
+ } ) ;
1175
+ }
1156
1176
}
0 commit comments