-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathservice.go
More file actions
637 lines (523 loc) · 16.5 KB
/
Copy pathservice.go
File metadata and controls
637 lines (523 loc) · 16.5 KB
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
// Copyright 2021 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only
package core
import (
"bytes"
"context"
"errors"
"fmt"
"sync"
"github.com/ChainSafe/gossamer/dot/network"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/internal/log"
"github.com/ChainSafe/gossamer/lib/blocktree"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/crypto"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
"github.com/ChainSafe/gossamer/lib/services"
"github.com/ChainSafe/gossamer/lib/transaction"
cscale "github.com/centrifuge/go-substrate-rpc-client/v3/scale"
ctypes "github.com/centrifuge/go-substrate-rpc-client/v3/types"
)
var (
_ services.Service = &Service{}
logger = log.NewFromGlobal(log.AddContext("pkg", "core"))
)
// QueryKeyValueChanges represents the key-value data inside a block storage
type QueryKeyValueChanges map[string]string
type wasmerInstanceFunc func(code []byte, cfg *wasmer.Config) (instance *wasmer.Instance, err error)
// Service is an overhead layer that allows communication between the runtime,
// BABE session, and network service. It deals with the validation of transactions
// and blocks by calling their respective validation functions in the runtime.
type Service struct {
ctx context.Context
cancel context.CancelFunc
blockAddCh chan *types.Block // for asynchronous block handling
sync.Mutex // lock for channel
// Service interfaces
blockState BlockState
epochState EpochState
storageState StorageState
transactionState TransactionState
net Network
// map of code substitutions keyed by block hash
codeSubstitute map[common.Hash]string
codeSubstitutedState CodeSubstitutedState
// Keystore
keys *keystore.GlobalKeystore
}
// Config holds the configuration for the core Service.
type Config struct {
LogLvl log.Level
BlockState BlockState
EpochState EpochState
StorageState StorageState
TransactionState TransactionState
Network Network
Keystore *keystore.GlobalKeystore
Runtime runtime.Instance
CodeSubstitutes map[common.Hash]string
CodeSubstitutedState CodeSubstitutedState
}
// NewService returns a new core service that connects the runtime, BABE
// session, and network service.
func NewService(cfg *Config) (*Service, error) {
if cfg.Keystore == nil {
return nil, ErrNilKeystore
}
if cfg.BlockState == nil {
return nil, ErrNilBlockState
}
if cfg.StorageState == nil {
return nil, ErrNilStorageState
}
if cfg.Network == nil {
return nil, ErrNilNetwork
}
if cfg.CodeSubstitutedState == nil {
return nil, errNilCodeSubstitutedState
}
logger.Patch(log.SetLevel(cfg.LogLvl))
blockAddCh := make(chan *types.Block, 256)
ctx, cancel := context.WithCancel(context.Background())
srv := &Service{
ctx: ctx,
cancel: cancel,
keys: cfg.Keystore,
blockState: cfg.BlockState,
epochState: cfg.EpochState,
storageState: cfg.StorageState,
transactionState: cfg.TransactionState,
net: cfg.Network,
blockAddCh: blockAddCh,
codeSubstitute: cfg.CodeSubstitutes,
codeSubstitutedState: cfg.CodeSubstitutedState,
}
return srv, nil
}
// Start starts the core service
func (s *Service) Start() error {
go s.handleBlocksAsync()
return nil
}
// Stop stops the core service
func (s *Service) Stop() error {
s.Lock()
defer s.Unlock()
s.cancel()
close(s.blockAddCh)
return nil
}
// StorageRoot returns the hash of the storage root
func (s *Service) StorageRoot() (common.Hash, error) {
if s.storageState == nil {
return common.Hash{}, ErrNilStorageState
}
ts, err := s.storageState.TrieState(nil)
if err != nil {
return common.Hash{}, err
}
return ts.Root()
}
// HandleBlockImport handles a block that was imported via the network
func (s *Service) HandleBlockImport(block *types.Block, state *rtstorage.TrieState) error {
return s.handleBlock(block, state)
}
// HandleBlockProduced handles a block that was produced by us
// It is handled the same as an imported block in terms of state updates; the only difference
// is we send a BlockAnnounceMessage to our peers.
func (s *Service) HandleBlockProduced(block *types.Block, state *rtstorage.TrieState) error {
if err := s.handleBlock(block, state); err != nil {
return err
}
digest := types.NewDigest()
for i := range block.Header.Digest.Types {
err := digest.Add(block.Header.Digest.Types[i].Value())
if err != nil {
return err
}
}
msg := &network.BlockAnnounceMessage{
ParentHash: block.Header.ParentHash,
Number: block.Header.Number,
StateRoot: block.Header.StateRoot,
ExtrinsicsRoot: block.Header.ExtrinsicsRoot,
Digest: digest,
BestBlock: true,
}
s.net.GossipMessage(msg)
return nil
}
func (s *Service) handleBlock(block *types.Block, state *rtstorage.TrieState) error {
if block == nil || state == nil {
return ErrNilBlockHandlerParameter
}
// store updates state trie nodes in database
err := s.storageState.StoreTrie(state, &block.Header)
if err != nil {
logger.Warnf("failed to store state trie for imported block %s: %s",
block.Header.Hash(), err)
return err
}
// store block in database
if err = s.blockState.AddBlock(block); err != nil {
if errors.Is(err, blocktree.ErrParentNotFound) && block.Header.Number != 0 {
return err
} else if errors.Is(err, blocktree.ErrBlockExists) || block.Header.Number == 0 {
// this is fine
} else {
return err
}
}
logger.Debugf("imported block %s and stored state trie with root %s",
block.Header.Hash(), state.MustRoot())
rt, err := s.blockState.GetRuntime(&block.Header.ParentHash)
if err != nil {
return err
}
// check for runtime changes
if err := s.blockState.HandleRuntimeChanges(state, rt, block.Header.Hash()); err != nil {
logger.Criticalf("failed to update runtime code: %s", err)
return err
}
// check if there was a runtime code substitution
if err := s.handleCodeSubstitution(block.Header.Hash(), state, wasmer.NewInstance); err != nil {
logger.Criticalf("failed to substitute runtime code: %s", err)
return err
}
go func() {
s.Lock()
defer s.Unlock()
if s.ctx.Err() != nil {
return
}
s.blockAddCh <- block
}()
return nil
}
func (s *Service) handleCodeSubstitution(
hash common.Hash,
state *rtstorage.TrieState,
instance wasmerInstanceFunc,
) error {
value := s.codeSubstitute[hash]
if value == "" {
return nil
}
logger.Infof("🔄 detected runtime code substitution, upgrading for block %s...", hash)
code := common.MustHexToBytes(value)
if len(code) == 0 {
return ErrEmptyRuntimeCode
}
rt, err := s.blockState.GetRuntime(&hash)
if err != nil {
return err
}
// this needs to create a new runtime instance, otherwise it will update
// the blocks that reference the current runtime version to use the code substition
cfg := &wasmer.Config{
Imports: wasmer.ImportsNodeRuntime,
}
cfg.Storage = state
cfg.Keystore = rt.Keystore()
cfg.NodeStorage = rt.NodeStorage()
cfg.Network = rt.NetworkService()
if rt.Validator() {
cfg.Role = 4
}
next, err := instance(code, cfg)
if err != nil {
return err
}
err = s.codeSubstitutedState.StoreCodeSubstitutedBlockHash(hash)
if err != nil {
return err
}
s.blockState.StoreRuntime(hash, next)
return nil
}
// handleBlocksAsync handles a block asynchronously; the handling performed by this function
// does not need to be completed before the next block can be imported.
func (s *Service) handleBlocksAsync() {
for {
prev := s.blockState.BestBlockHash()
select {
case block, ok := <-s.blockAddCh:
if !ok {
return
}
if block == nil {
continue
}
if err := s.handleChainReorg(prev, block.Header.Hash()); err != nil {
logger.Warnf("failed to re-add transactions to chain upon re-org: %s", err)
}
s.maintainTransactionPool(block)
case <-s.ctx.Done():
return
}
}
}
// handleChainReorg checks if there is a chain re-org (ie. new chain head is on a different chain than the
// previous chain head). If there is a re-org, it moves the transactions that were included on the previous
// chain back into the transaction pool.
func (s *Service) handleChainReorg(prev, curr common.Hash) error {
ancestor, err := s.blockState.HighestCommonAncestor(prev, curr)
if err != nil {
return err
}
// if the highest common ancestor of the previous chain head and current chain head is the previous chain head,
// then the current chain head is the descendant of the previous and thus are on the same chain
if ancestor == prev {
return nil
}
subchain, err := s.blockState.SubChain(ancestor, prev)
if err != nil {
return err
}
// subchain contains the ancestor as well so we need to remove it.
if len(subchain) > 0 {
subchain = subchain[1:]
} else {
return nil
}
// Check transaction validation on the best block.
rt, err := s.blockState.GetRuntime(nil)
if err != nil {
return err
}
if rt == nil {
return ErrNilRuntime
}
// for each block in the previous chain, re-add its extrinsics back into the pool
for _, hash := range subchain {
body, err := s.blockState.GetBlockBody(hash)
if err != nil || body == nil {
continue
}
for _, ext := range *body {
logger.Tracef("validating transaction on re-org chain for extrinsic %s", ext)
decExt := &ctypes.Extrinsic{}
decoder := cscale.NewDecoder(bytes.NewReader(ext))
if err = decoder.Decode(&decExt); err != nil {
continue
}
// Inherent are not signed.
if !decExt.IsSigned() {
continue
}
externalExt := make(types.Extrinsic, 0, 1+len(ext))
externalExt = append(externalExt, byte(types.TxnExternal))
externalExt = append(externalExt, ext...)
txv, err := rt.ValidateTransaction(externalExt)
if err != nil {
logger.Debugf("failed to validate transaction for extrinsic %s: %s", ext, err)
continue
}
vtx := transaction.NewValidTransaction(ext, txv)
s.transactionState.AddToPool(vtx)
}
}
return nil
}
// maintainTransactionPool removes any transactions that were included in
// the new block, revalidates the transactions in the pool, and moves
// them to the queue if valid.
// See https://github.com/paritytech/substrate/blob/74804b5649eccfb83c90aec87bdca58e5d5c8789/client/transaction-pool/src/lib.rs#L545
func (s *Service) maintainTransactionPool(block *types.Block) {
// remove extrinsics included in a block
for _, ext := range block.Body {
s.transactionState.RemoveExtrinsic(ext)
}
// re-validate transactions in the pool and move them to the queue
txs := s.transactionState.PendingInPool()
for _, tx := range txs {
// get the best block corresponding runtime
rt, err := s.blockState.GetRuntime(nil)
if err != nil {
logger.Warnf("failed to get runtime to re-validate transactions in pool: %s", err)
continue
}
txnValidity, err := rt.ValidateTransaction(tx.Extrinsic)
if err != nil {
s.transactionState.RemoveExtrinsic(tx.Extrinsic)
continue
}
tx = transaction.NewValidTransaction(tx.Extrinsic, txnValidity)
// Err is only thrown if tx is already in pool, in which case it still gets removed
h, _ := s.transactionState.Push(tx)
s.transactionState.RemoveExtrinsicFromPool(tx.Extrinsic)
logger.Tracef("moved transaction %s to queue", h)
}
}
// InsertKey inserts keypair into the account keystore
func (s *Service) InsertKey(kp crypto.Keypair, keystoreType string) error {
ks, err := s.keys.GetKeystore([]byte(keystoreType))
if err != nil {
return err
}
return ks.Insert(kp)
}
// HasKey returns true if given hex encoded public key string is found in keystore, false otherwise, error if there
// are issues decoding string
func (s *Service) HasKey(pubKeyStr, keystoreType string) (bool, error) {
ks, err := s.keys.GetKeystore([]byte(keystoreType))
if err != nil {
return false, err
}
return keystore.HasKey(pubKeyStr, keystoreType, ks)
}
// DecodeSessionKeys executes the runtime DecodeSessionKeys and return the scale encoded keys
func (s *Service) DecodeSessionKeys(enc []byte) ([]byte, error) {
rt, err := s.blockState.GetRuntime(nil)
if err != nil {
return nil, err
}
return rt.DecodeSessionKeys(enc)
}
// GetRuntimeVersion gets the current RuntimeVersion
func (s *Service) GetRuntimeVersion(bhash *common.Hash) (runtime.Version, error) {
var stateRootHash *common.Hash
// If block hash is not nil then fetch the state root corresponding to the block.
if bhash != nil {
var err error
stateRootHash, err = s.storageState.GetStateRootFromBlock(bhash)
if err != nil {
return nil, err
}
}
ts, err := s.storageState.TrieState(stateRootHash)
if err != nil {
return nil, err
}
rt, err := s.blockState.GetRuntime(bhash)
if err != nil {
return nil, err
}
rt.SetContextStorage(ts)
return rt.Version()
}
// HandleSubmittedExtrinsic is used to send a Transaction message containing a Extrinsic @ext
func (s *Service) HandleSubmittedExtrinsic(ext types.Extrinsic) error {
if s.net == nil {
return nil
}
if s.transactionState.Exists(ext) {
return nil
}
bestBlockHash := s.blockState.BestBlockHash()
stateRoot, err := s.storageState.GetStateRootFromBlock(&bestBlockHash)
if err != nil {
return fmt.Errorf("could not get state root from block %s: %w", bestBlockHash, err)
}
ts, err := s.storageState.TrieState(stateRoot)
if err != nil {
return err
}
rt, err := s.blockState.GetRuntime(&bestBlockHash)
if err != nil {
logger.Critical("failed to get runtime")
return err
}
rt.SetContextStorage(ts)
// the transaction source is External
externalExt := types.Extrinsic(append([]byte{byte(types.TxnExternal)}, ext...))
txv, err := rt.ValidateTransaction(externalExt)
if err != nil {
return err
}
// add transaction to pool
vtx := transaction.NewValidTransaction(ext, txv)
s.transactionState.AddToPool(vtx)
// broadcast transaction
msg := &network.TransactionMessage{Extrinsics: []types.Extrinsic{ext}}
s.net.GossipMessage(msg)
return nil
}
//GetMetadata calls runtime Metadata_metadata function
func (s *Service) GetMetadata(bhash *common.Hash) ([]byte, error) {
var (
stateRootHash *common.Hash
err error
)
// If block hash is not nil then fetch the state root corresponding to the block.
if bhash != nil {
stateRootHash, err = s.storageState.GetStateRootFromBlock(bhash)
if err != nil {
return nil, err
}
}
ts, err := s.storageState.TrieState(stateRootHash)
if err != nil {
return nil, err
}
rt, err := s.blockState.GetRuntime(bhash)
if err != nil {
return nil, err
}
rt.SetContextStorage(ts)
return rt.Metadata()
}
// QueryStorage returns the key-value data by block based on `keys` params
// on every block starting `from` until `to` block, if `to` is not nil
func (s *Service) QueryStorage(from, to common.Hash, keys ...string) (map[common.Hash]QueryKeyValueChanges, error) {
if to.IsEmpty() {
to = s.blockState.BestBlockHash()
}
blocksToQuery, err := s.blockState.SubChain(from, to)
if err != nil {
return nil, err
}
queries := make(map[common.Hash]QueryKeyValueChanges)
for _, hash := range blocksToQuery {
changes, err := s.tryQueryStorage(hash, keys...)
if err != nil {
return nil, err
}
queries[hash] = changes
}
return queries, nil
}
// tryQueryStorage will try to get all the `keys` inside the block's current state
func (s *Service) tryQueryStorage(block common.Hash, keys ...string) (QueryKeyValueChanges, error) {
stateRootHash, err := s.storageState.GetStateRootFromBlock(&block)
if err != nil {
return nil, err
}
changes := make(QueryKeyValueChanges)
for _, k := range keys {
keyBytes, err := common.HexToBytes(k)
if err != nil {
return nil, err
}
storedData, err := s.storageState.GetStorage(stateRootHash, keyBytes)
if err != nil {
return nil, err
}
if storedData == nil {
continue
}
changes[k] = common.BytesToHex(storedData)
}
return changes, nil
}
// GetReadProofAt will return an array with the proofs for the keys passed as params
// based on the block hash passed as param as well, if block hash is nil then the current state will take place
func (s *Service) GetReadProofAt(block common.Hash, keys [][]byte) (
hash common.Hash, proofForKeys [][]byte, err error) {
if block.IsEmpty() {
block = s.blockState.BestBlockHash()
}
stateRoot, err := s.blockState.GetBlockStateRoot(block)
if err != nil {
return hash, nil, err
}
proofForKeys, err = s.storageState.GenerateTrieProof(stateRoot, keys)
if err != nil {
return hash, nil, err
}
return block, proofForKeys, nil
}