16
16
package core
17
17
18
18
import (
19
- "bytes"
20
19
"context"
21
- "fmt"
22
20
"math/big"
23
21
"os"
24
22
"sync"
@@ -59,10 +57,6 @@ type Service struct {
59
57
net Network
60
58
digestHandler DigestHandler
61
59
62
- // Current runtime and hash of the current runtime code
63
- rt runtime.Instance
64
- codeHash common.Hash
65
-
66
60
// map of code substitutions keyed by block hash
67
61
codeSubstitute map [common.Hash ]string
68
62
codeSubstitutedState CodeSubstitutedState
@@ -103,10 +97,6 @@ func NewService(cfg *Config) (*Service, error) {
103
97
return nil , ErrNilStorageState
104
98
}
105
99
106
- if cfg .Runtime == nil {
107
- return nil , ErrNilRuntime
108
- }
109
-
110
100
if cfg .Network == nil {
111
101
return nil , ErrNilNetwork
112
102
}
@@ -123,24 +113,12 @@ func NewService(cfg *Config) (*Service, error) {
123
113
h = log .CallerFileHandler (h )
124
114
logger .SetHandler (log .LvlFilterHandler (cfg .LogLvl , h ))
125
115
126
- sr , err := cfg .BlockState .BestBlockStateRoot ()
127
- if err != nil {
128
- return nil , err
129
- }
130
-
131
- codeHash , err := cfg .StorageState .LoadCodeHash (& sr )
132
- if err != nil {
133
- return nil , err
134
- }
135
-
136
116
blockAddCh := make (chan * types.Block , 256 )
137
117
138
118
ctx , cancel := context .WithCancel (context .Background ())
139
119
srv := & Service {
140
120
ctx : ctx ,
141
121
cancel : cancel ,
142
- rt : cfg .Runtime ,
143
- codeHash : codeHash ,
144
122
keys : cfg .Keystore ,
145
123
blockState : cfg .BlockState ,
146
124
epochState : cfg .EpochState ,
@@ -221,7 +199,7 @@ func (s *Service) handleBlock(block *types.Block, state *rtstorage.TrieState) er
221
199
}
222
200
223
201
// store block in database
224
- if err : = s .blockState .AddBlock (block ); err != nil {
202
+ if err = s .blockState .AddBlock (block ); err != nil {
225
203
if err == blocktree .ErrParentNotFound && block .Header .Number .Cmp (big .NewInt (0 )) != 0 {
226
204
return err
227
205
} else if err == blocktree .ErrBlockExists || block .Header .Number .Cmp (big .NewInt (0 )) == 0 {
@@ -236,8 +214,13 @@ func (s *Service) handleBlock(block *types.Block, state *rtstorage.TrieState) er
236
214
// handle consensus digests
237
215
s .digestHandler .HandleDigests (block .Header )
238
216
217
+ rt , err := s .blockState .GetRuntime (& block .Header .ParentHash )
218
+ if err != nil {
219
+ return err
220
+ }
221
+
239
222
// check for runtime changes
240
- if err := s .handleRuntimeChanges (state ); err != nil {
223
+ if err := s .blockState . HandleRuntimeChanges (state , rt , block . Header . Hash () ); err != nil {
241
224
logger .Crit ("failed to update runtime code" , "error" , err )
242
225
return err
243
226
}
@@ -267,57 +250,6 @@ func (s *Service) handleBlock(block *types.Block, state *rtstorage.TrieState) er
267
250
return nil
268
251
}
269
252
270
- func (s * Service ) handleRuntimeChanges (newState * rtstorage.TrieState ) error {
271
- currCodeHash , err := newState .LoadCodeHash ()
272
- if err != nil {
273
- return err
274
- }
275
-
276
- if bytes .Equal (s .codeHash [:], currCodeHash [:]) {
277
- return nil
278
- }
279
-
280
- logger .Info ("🔄 detected runtime code change, upgrading..." , "block" , s .blockState .BestBlockHash (), "previous code hash" , s .codeHash , "new code hash" , currCodeHash )
281
- code := newState .LoadCode ()
282
- if len (code ) == 0 {
283
- return ErrEmptyRuntimeCode
284
- }
285
-
286
- codeSubBlockHash := s .codeSubstitutedState .LoadCodeSubstitutedBlockHash ()
287
-
288
- if ! codeSubBlockHash .Equal (common.Hash {}) {
289
- // don't do runtime change if using code substitution and runtime change spec version are equal
290
- // (do a runtime change if code substituted and runtime spec versions are different, or code not substituted)
291
- newVersion , err := s .rt .CheckRuntimeVersion (code ) //nolint
292
- if err != nil {
293
- return err
294
- }
295
-
296
- previousVersion , _ := s .rt .Version ()
297
- if previousVersion .SpecVersion () == newVersion .SpecVersion () {
298
- return nil
299
- }
300
-
301
- logger .Info ("🔄 detected runtime code change, upgrading..." , "block" , s .blockState .BestBlockHash (),
302
- "previous code hash" , s .codeHash , "new code hash" , currCodeHash ,
303
- "previous spec version" , previousVersion .SpecVersion (), "new spec version" , newVersion .SpecVersion ())
304
- }
305
-
306
- err = s .rt .UpdateRuntimeCode (code )
307
- if err != nil {
308
- return err
309
- }
310
-
311
- s .codeHash = currCodeHash
312
-
313
- err = s .codeSubstitutedState .StoreCodeSubstitutedBlockHash (common.Hash {})
314
- if err != nil {
315
- return fmt .Errorf ("failed to update code substituted block hash: %w" , err )
316
- }
317
-
318
- return nil
319
- }
320
-
321
253
func (s * Service ) handleCodeSubstitution (hash common.Hash ) error {
322
254
value := s .codeSubstitute [hash ]
323
255
if value == "" {
@@ -330,7 +262,12 @@ func (s *Service) handleCodeSubstitution(hash common.Hash) error {
330
262
return ErrEmptyRuntimeCode
331
263
}
332
264
333
- err := s .rt .UpdateRuntimeCode (code )
265
+ rt , err := s .blockState .GetRuntime (& hash )
266
+ if err != nil {
267
+ return err
268
+ }
269
+
270
+ err = rt .UpdateRuntimeCode (code )
334
271
if err != nil {
335
272
return err
336
273
}
@@ -415,6 +352,12 @@ func (s *Service) handleChainReorg(prev, curr common.Hash) error {
415
352
subchain = subchain [1 :]
416
353
}
417
354
355
+ // Check transaction validation on the best block.
356
+ rt , err := s .blockState .GetRuntime (nil )
357
+ if err != nil {
358
+ return err
359
+ }
360
+
418
361
// for each block in the previous chain, re-add its extrinsics back into the pool
419
362
for _ , hash := range subchain {
420
363
body , err := s .blockState .GetBlockBody (hash )
@@ -448,7 +391,7 @@ func (s *Service) handleChainReorg(prev, curr common.Hash) error {
448
391
}
449
392
450
393
externalExt := types .Extrinsic (append ([]byte {byte (types .TxnExternal )}, encExt ... ))
451
- txv , err := s . rt .ValidateTransaction (externalExt )
394
+ txv , err := rt .ValidateTransaction (externalExt )
452
395
if err != nil {
453
396
logger .Debug ("failed to validate transaction" , "error" , err , "extrinsic" , ext )
454
397
continue
@@ -519,6 +462,7 @@ func (s *Service) HasKey(pubKeyStr, keyType string) (bool, error) {
519
462
// GetRuntimeVersion gets the current RuntimeVersion
520
463
func (s * Service ) GetRuntimeVersion (bhash * common.Hash ) (runtime.Version , error ) {
521
464
var stateRootHash * common.Hash
465
+
522
466
// If block hash is not nil then fetch the state root corresponding to the block.
523
467
if bhash != nil {
524
468
var err error
@@ -533,16 +477,36 @@ func (s *Service) GetRuntimeVersion(bhash *common.Hash) (runtime.Version, error)
533
477
return nil , err
534
478
}
535
479
536
- s .rt .SetContextStorage (ts )
537
- return s .rt .Version ()
480
+ rt , err := s .blockState .GetRuntime (bhash )
481
+ if err != nil {
482
+ return nil , err
483
+ }
484
+
485
+ rt .SetContextStorage (ts )
486
+ return rt .Version ()
538
487
}
539
488
540
489
// HandleSubmittedExtrinsic is used to send a Transaction message containing a Extrinsic @ext
541
490
func (s * Service ) HandleSubmittedExtrinsic (ext types.Extrinsic ) error {
491
+ if s .net == nil {
492
+ return nil
493
+ }
494
+
495
+ ts , err := s .storageState .TrieState (nil )
496
+ if err != nil {
497
+ return err
498
+ }
499
+
500
+ rt , err := s .blockState .GetRuntime (nil )
501
+ if err != nil {
502
+ logger .Crit ("failed to get runtime" )
503
+ return err
504
+ }
505
+
506
+ rt .SetContextStorage (ts )
542
507
// the transaction source is External
543
508
externalExt := types .Extrinsic (append ([]byte {byte (types .TxnExternal )}, ext ... ))
544
-
545
- txv , err := s .rt .ValidateTransaction (externalExt )
509
+ txv , err := rt .ValidateTransaction (externalExt )
546
510
if err != nil {
547
511
return err
548
512
}
@@ -576,6 +540,11 @@ func (s *Service) GetMetadata(bhash *common.Hash) ([]byte, error) {
576
540
return nil , err
577
541
}
578
542
579
- s .rt .SetContextStorage (ts )
580
- return s .rt .Metadata ()
543
+ rt , err := s .blockState .GetRuntime (bhash )
544
+ if err != nil {
545
+ return nil , err
546
+ }
547
+
548
+ rt .SetContextStorage (ts )
549
+ return rt .Metadata ()
581
550
}
0 commit comments