117
117
118
118
use sp_std:: { prelude:: * , marker:: PhantomData } ;
119
119
use frame_support:: {
120
- storage :: StorageValue , weights:: { GetDispatchInfo , DispatchInfo , DispatchClass } ,
120
+ StorageValue , StorageMap , weights:: { GetDispatchInfo , DispatchInfo , DispatchClass } ,
121
121
traits:: { OnInitialize , OnFinalize , OnRuntimeUpgrade , OffchainWorker } ,
122
122
dispatch:: PostDispatchInfo ,
123
123
} ;
@@ -453,7 +453,7 @@ where
453
453
// We need to keep events available for offchain workers,
454
454
// hence we initialize the block manually.
455
455
// OffchainWorker RuntimeApi should skip initialization.
456
- let digests = Self :: extract_pre_digest ( header) ;
456
+ let digests = header. digest ( ) . clone ( ) ;
457
457
458
458
<frame_system:: Module < System > >:: initialize (
459
459
header. number ( ) ,
@@ -463,15 +463,16 @@ where
463
463
frame_system:: InitKind :: Inspection ,
464
464
) ;
465
465
466
+ // Frame system only inserts the parent hash into the block hashes as normally we don't know
467
+ // the hash for the header before. However, here we are aware of the hash and we can add it
468
+ // as well.
469
+ frame_system:: BlockHash :: < System > :: insert ( header. number ( ) , header. hash ( ) ) ;
470
+
466
471
// Initialize logger, so the log messages are visible
467
472
// also when running WASM.
468
473
frame_support:: debug:: RuntimeLogger :: init ( ) ;
469
474
470
- <AllModules as OffchainWorker < System :: BlockNumber > >:: offchain_worker (
471
- // to maintain backward compatibility we call module offchain workers
472
- // with parent block number.
473
- header. number ( ) . saturating_sub ( 1u32 . into ( ) )
474
- )
475
+ <AllModules as OffchainWorker < System :: BlockNumber > >:: offchain_worker ( * header. number ( ) )
475
476
}
476
477
}
477
478
@@ -481,7 +482,7 @@ mod tests {
481
482
use super :: * ;
482
483
use sp_core:: H256 ;
483
484
use sp_runtime:: {
484
- generic:: Era , Perbill , DispatchError , testing:: { Digest , Header , Block } ,
485
+ generic:: { Era , DigestItem } , Perbill , DispatchError , testing:: { Digest , Header , Block } ,
485
486
traits:: { Header as HeaderT , BlakeTwo256 , IdentityLookup } ,
486
487
transaction_validity:: {
487
488
InvalidTransaction , ValidTransaction , TransactionValidityError , UnknownTransaction
@@ -547,6 +548,10 @@ mod tests {
547
548
sp_io:: storage:: set( super :: TEST_KEY , "module" . as_bytes( ) ) ;
548
549
200
549
550
}
551
+
552
+ fn offchain_worker( n: T :: BlockNumber ) {
553
+ assert_eq!( T :: BlockNumber :: from( 1u32 ) , n) ;
554
+ }
550
555
}
551
556
}
552
557
@@ -1115,4 +1120,27 @@ mod tests {
1115
1120
) ;
1116
1121
} ) ;
1117
1122
}
1123
+
1124
+ #[ test]
1125
+ fn offchain_worker_works_as_expected ( ) {
1126
+ new_test_ext ( 1 ) . execute_with ( || {
1127
+ let parent_hash = sp_core:: H256 :: from ( [ 69u8 ; 32 ] ) ;
1128
+ let mut digest = Digest :: default ( ) ;
1129
+ digest. push ( DigestItem :: Seal ( [ 1 , 2 , 3 , 4 ] , vec ! [ 5 , 6 , 7 , 8 ] ) ) ;
1130
+
1131
+ let header = Header :: new (
1132
+ 1 ,
1133
+ H256 :: default ( ) ,
1134
+ H256 :: default ( ) ,
1135
+ parent_hash,
1136
+ digest. clone ( ) ,
1137
+ ) ;
1138
+
1139
+ Executive :: offchain_worker ( & header) ;
1140
+
1141
+ assert_eq ! ( digest, System :: digest( ) ) ;
1142
+ assert_eq ! ( parent_hash, System :: block_hash( 0 ) ) ;
1143
+ assert_eq ! ( header. hash( ) , System :: block_hash( 1 ) ) ;
1144
+ } ) ;
1145
+ }
1118
1146
}
0 commit comments