forked from dominant-strategies/go-quai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheaderchain.go
975 lines (866 loc) · 30.9 KB
/
headerchain.go
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
package core
import (
"errors"
"fmt"
"io"
"math/big"
"sync"
"sync/atomic"
"time"
"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/consensus"
"github.com/dominant-strategies/go-quai/consensus/misc"
"github.com/dominant-strategies/go-quai/core/rawdb"
"github.com/dominant-strategies/go-quai/core/state"
"github.com/dominant-strategies/go-quai/core/types"
"github.com/dominant-strategies/go-quai/core/vm"
"github.com/dominant-strategies/go-quai/ethdb"
"github.com/dominant-strategies/go-quai/event"
"github.com/dominant-strategies/go-quai/log"
"github.com/dominant-strategies/go-quai/params"
"github.com/dominant-strategies/go-quai/rlp"
"github.com/dominant-strategies/go-quai/trie"
lru "github.com/hashicorp/golang-lru"
)
const (
headerCacheLimit = 512
numberCacheLimit = 2048
c_subRollupCacheSize = 50
primeHorizonThreshold = 20
)
// getPendingEtxsRollup gets the pendingEtxsRollup rollup from appropriate Region
type getPendingEtxsRollup func(blockHash common.Hash, hash common.Hash, location common.Location) (types.PendingEtxsRollup, error)
// getPendingEtxs gets the pendingEtxs from the appropriate Zone
type getPendingEtxs func(blockHash common.Hash, hash common.Hash, location common.Location) (types.PendingEtxs, error)
type HeaderChain struct {
config *params.ChainConfig
bc *BodyDb
engine consensus.Engine
pool *TxPool
chainHeadFeed event.Feed
chainSideFeed event.Feed
scope event.SubscriptionScope
headerDb ethdb.Database
genesisHeader *types.Header
currentHeader atomic.Value // Current head of the header chain (may be above the block chain!)
headerCache *lru.Cache // Cache for the most recent block headers
numberCache *lru.Cache // Cache for the most recent block numbers
fetchPEtxRollup getPendingEtxsRollup
fetchPEtx getPendingEtxs
pendingEtxsRollup *lru.Cache
pendingEtxs *lru.Cache
blooms *lru.Cache
subRollupCache *lru.Cache
wg sync.WaitGroup // chain processing wait group for shutting down
running int32 // 0 if chain is running, 1 when stopped
procInterrupt int32 // interrupt signaler for block processing
headermu sync.RWMutex
heads []*types.Header
slicesRunning []common.Location
}
// NewHeaderChain creates a new HeaderChain structure. ProcInterrupt points
// to the parent's interrupt semaphore.
func NewHeaderChain(db ethdb.Database, engine consensus.Engine, pEtxsRollupFetcher getPendingEtxsRollup, pEtxsFetcher getPendingEtxs, chainConfig *params.ChainConfig, cacheConfig *CacheConfig, txLookupLimit *uint64, vmConfig vm.Config, slicesRunning []common.Location) (*HeaderChain, error) {
headerCache, _ := lru.New(headerCacheLimit)
numberCache, _ := lru.New(numberCacheLimit)
nodeCtx := common.NodeLocation.Context()
hc := &HeaderChain{
config: chainConfig,
headerDb: db,
headerCache: headerCache,
numberCache: numberCache,
engine: engine,
slicesRunning: slicesRunning,
fetchPEtxRollup: pEtxsRollupFetcher,
fetchPEtx: pEtxsFetcher,
}
pendingEtxsRollup, _ := lru.New(c_maxPendingEtxsRollup)
hc.pendingEtxsRollup = pendingEtxsRollup
if nodeCtx == common.PRIME_CTX {
hc.pendingEtxs, _ = lru.New(c_maxPendingEtxBatchesPrime)
} else {
hc.pendingEtxs, _ = lru.New(c_maxPendingEtxBatchesRegion)
}
blooms, _ := lru.New(c_maxBloomFilters)
hc.blooms = blooms
subRollupCache, _ := lru.New(c_subRollupCacheSize)
hc.subRollupCache = subRollupCache
hc.genesisHeader = hc.GetHeaderByNumber(0)
if hc.genesisHeader.Hash() != chainConfig.GenesisHash {
return nil, fmt.Errorf("genesis block mismatch: have %x, want %x", hc.genesisHeader.Hash(), chainConfig.GenesisHash)
}
log.Info("Genesis", "Hash:", hc.genesisHeader.Hash())
if hc.genesisHeader == nil {
return nil, ErrNoGenesis
}
//Load any state that is in our db
if err := hc.loadLastState(); err != nil {
return nil, err
}
var err error
hc.bc, err = NewBodyDb(db, engine, hc, chainConfig, cacheConfig, txLookupLimit, vmConfig, slicesRunning)
if err != nil {
return nil, err
}
// Initialize the heads slice
heads := make([]*types.Header, 0)
hc.heads = heads
return hc, nil
}
// CollectSubRollup collects the rollup of ETXs emitted from the subordinate
// chain in the slice which emitted the given block.
func (hc *HeaderChain) CollectSubRollup(b *types.Block) (types.Transactions, error) {
nodeCtx := common.NodeLocation.Context()
subRollup := types.Transactions{}
if nodeCtx < common.ZONE_CTX {
// Since in prime the pending etxs are stored in 2 parts, pendingEtxsRollup
// consists of region header and its sub manifests
// Prime independently stores the pending etxs for each of the hashes in
// the sub manifests, so it needs the pendingEtxsRollup to do so.
for _, hash := range b.SubManifest() {
if nodeCtx == common.PRIME_CTX {
pEtxRollup, err := hc.GetPendingEtxsRollup(hash)
if err == nil {
for _, pEtxHash := range pEtxRollup.Manifest {
pendingEtxs, err := hc.GetPendingEtxs(pEtxHash)
if err != nil {
// Get the pendingEtx from the appropriate zone
hc.fetchPEtx(b.Hash(), pEtxHash, pEtxRollup.Header.Location())
return nil, ErrPendingEtxNotFound
}
subRollup = append(subRollup, pendingEtxs.Etxs...)
}
} else {
// Try to get the pending etx from the Regions
hc.fetchPEtxRollup(b.Hash(), hash, b.Location())
return nil, ErrPendingEtxNotFound
}
// Region works normally as before collecting pendingEtxs for each hash in the manifest
} else if nodeCtx == common.REGION_CTX {
pendingEtxs, err := hc.GetPendingEtxs(hash)
if err != nil {
// Get the pendingEtx from the appropriate zone
hc.fetchPEtx(b.Hash(), hash, b.Header().Location())
return nil, ErrPendingEtxNotFound
}
subRollup = append(subRollup, pendingEtxs.Etxs...)
}
}
// Rolluphash is specifically for zone rollup, which can only be validated by region
if nodeCtx == common.REGION_CTX {
if subRollupHash := types.DeriveSha(subRollup, trie.NewStackTrie(nil)); subRollupHash != b.EtxRollupHash() {
return nil, errors.New("sub rollup does not match sub rollup hash")
}
}
}
return subRollup, nil
}
// GetPendingEtxs gets the pendingEtxs form the
func (hc *HeaderChain) GetPendingEtxs(hash common.Hash) (*types.PendingEtxs, error) {
var pendingEtxs types.PendingEtxs
// Look for pending ETXs first in pending ETX cache, then in database
if res, ok := hc.pendingEtxs.Get(hash); ok && res != nil {
pendingEtxs = res.(types.PendingEtxs)
} else if res := rawdb.ReadPendingEtxs(hc.headerDb, hash); res != nil {
pendingEtxs = *res
} else {
log.Trace("unable to find pending etxs for hash in manifest", "hash:", hash.String())
return nil, ErrPendingEtxNotFound
}
return &pendingEtxs, nil
}
func (hc *HeaderChain) GetPendingEtxsRollup(hash common.Hash) (*types.PendingEtxsRollup, error) {
var rollups types.PendingEtxsRollup
// Look for pending ETXs first in pending ETX cache, then in database
if res, ok := hc.pendingEtxsRollup.Get(hash); ok && res != nil {
rollups = res.(types.PendingEtxsRollup)
} else if res := rawdb.ReadPendingEtxsRollup(hc.headerDb, hash); res != nil {
rollups = *res
} else {
log.Trace("unable to find pending etxs rollups for hash in manifest", "hash:", hash.String())
return nil, ErrPendingEtxRollupNotFound
}
return &rollups, nil
}
// GetBloom gets the bloom from the cache or database
func (hc *HeaderChain) GetBloom(hash common.Hash) (*types.Bloom, error) {
var bloom types.Bloom
// Look for bloom first in bloom cache, then in database
if res, ok := hc.blooms.Get(hash); ok && res != nil {
bloom = res.(types.Bloom)
} else if res := rawdb.ReadBloom(hc.headerDb, hash); res != nil {
bloom = *res
} else {
log.Debug("unable to find bloom for hash in database", "hash:", hash.String())
return nil, ErrBloomNotFound
}
return &bloom, nil
}
// Collect all emmitted ETXs since the last coincident block, but excluding
// those emitted in this block
func (hc *HeaderChain) CollectEtxRollup(b *types.Block) (types.Transactions, error) {
if b.NumberU64() == 0 && b.Hash() == hc.config.GenesisHash {
return b.ExtTransactions(), nil
}
parent := hc.GetBlock(b.ParentHash(), b.NumberU64()-1)
if parent == nil {
return nil, errors.New("parent not found")
}
return hc.collectInclusiveEtxRollup(parent)
}
func (hc *HeaderChain) collectInclusiveEtxRollup(b *types.Block) (types.Transactions, error) {
// Initialize the rollup with ETXs emitted by this block
newEtxs := b.ExtTransactions()
// Terminate the search if we reached genesis
if b.NumberU64() == 0 {
if b.Hash() != hc.config.GenesisHash {
return nil, fmt.Errorf("manifest builds on incorrect genesis, block0 hash: %s", b.Hash().String())
} else {
return newEtxs, nil
}
}
// Terminate the search on coincidence with dom chain
if hc.engine.IsDomCoincident(hc, b.Header()) {
return newEtxs, nil
}
// Recursively get the ancestor rollup, until a coincident ancestor is found
ancestor := hc.GetBlock(b.ParentHash(), b.NumberU64()-1)
if ancestor == nil {
return nil, errors.New("ancestor not found")
}
etxRollup, err := hc.collectInclusiveEtxRollup(ancestor)
if err != nil {
return nil, err
}
etxRollup = append(etxRollup, newEtxs...)
return etxRollup, nil
}
// Append
func (hc *HeaderChain) AppendHeader(header *types.Header) error {
nodeCtx := common.NodeLocation.Context()
log.Debug("HeaderChain Append:", "Header information: Hash:", header.Hash(), "header header hash:", header.Hash(), "Number:", header.NumberU64(), "Location:", header.Location, "Parent:", header.ParentHash())
err := hc.engine.VerifyHeader(hc, header)
if err != nil {
return err
}
// Verify the manifest matches expected
// Load the manifest of headers preceding this header
// note: prime manifest is non-existent, because a prime header cannot be
// coincident with a higher order chain. So, this check is skipped for prime
// nodes.
if nodeCtx > common.PRIME_CTX {
manifest := rawdb.ReadManifest(hc.headerDb, header.ParentHash())
if manifest == nil {
return errors.New("manifest not found for parent")
}
if header.ManifestHash(nodeCtx) != types.DeriveSha(manifest, trie.NewStackTrie(nil)) {
return errors.New("manifest does not match hash")
}
}
return nil
}
func (hc *HeaderChain) ProcessingState() bool {
return hc.bc.ProcessingState()
}
// Append
func (hc *HeaderChain) AppendBlock(block *types.Block, newInboundEtxs types.Transactions) error {
blockappend := time.Now()
// Append block else revert header append
logs, err := hc.bc.Append(block, newInboundEtxs)
if err != nil {
return err
}
log.Debug("Time taken to", "Append in bc", common.PrettyDuration(time.Since(blockappend)))
hc.bc.chainFeed.Send(ChainEvent{Block: block, Hash: block.Hash(), Logs: logs})
if len(logs) > 0 {
hc.bc.logsFeed.Send(logs)
}
return nil
}
// SetCurrentHeader sets the current header based on the POEM choice
func (hc *HeaderChain) SetCurrentHeader(head *types.Header) error {
hc.headermu.Lock()
defer hc.headermu.Unlock()
prevHeader := hc.CurrentHeader()
// if trying to set the same header, escape
if prevHeader.Hash() == head.Hash() {
return nil
}
// write the head block hash to the db
rawdb.WriteHeadBlockHash(hc.headerDb, head.Hash())
log.Info("Setting the current header", "Hash", head.Hash(), "Number", head.NumberArray())
hc.currentHeader.Store(head)
// If head is the normal extension of canonical head, we can return by just wiring the canonical hash.
if prevHeader.Hash() == head.ParentHash() {
rawdb.WriteCanonicalHash(hc.headerDb, head.Hash(), head.NumberU64())
return nil
}
//Find a common header
commonHeader := hc.findCommonAncestor(head)
newHeader := types.CopyHeader(head)
// Delete each header and rollback state processor until common header
// Accumulate the hash slice stack
var hashStack []*types.Header
for {
if newHeader.Hash() == commonHeader.Hash() {
break
}
hashStack = append(hashStack, newHeader)
newHeader = hc.GetHeader(newHeader.ParentHash(), newHeader.NumberU64()-1)
// genesis check to not delete the genesis block
if newHeader.Hash() == hc.config.GenesisHash {
break
}
}
for {
if prevHeader.Hash() == commonHeader.Hash() {
break
}
rawdb.DeleteCanonicalHash(hc.headerDb, prevHeader.NumberU64())
prevHeader = hc.GetHeader(prevHeader.ParentHash(), prevHeader.NumberU64()-1)
// genesis check to not delete the genesis block
if prevHeader.Hash() == hc.config.GenesisHash {
break
}
}
// Run through the hash stack to update canonicalHash and forward state processor
for i := len(hashStack) - 1; i >= 0; i-- {
rawdb.WriteCanonicalHash(hc.headerDb, hashStack[i].Hash(), hashStack[i].NumberU64())
}
return nil
}
// SetCurrentHeader sets the in-memory head header marker of the canonical chan
// as the given header.
func (hc *HeaderChain) SetCurrentState(head *types.Header) error {
hc.headermu.Lock()
defer hc.headermu.Unlock()
nodeCtx := common.NodeLocation.Context()
if nodeCtx != common.ZONE_CTX || !hc.ProcessingState() {
return nil
}
current := types.CopyHeader(head)
var headersWithoutState []*types.Header
for {
headersWithoutState = append(headersWithoutState, current)
header := hc.GetHeader(current.ParentHash(), current.NumberU64()-1)
if header == nil {
return ErrSubNotSyncedToDom
}
// Checking of the Etx set exists makes sure that we have processed the
// state of the parent block
etxSet := rawdb.ReadEtxSet(hc.headerDb, header.Hash(), header.NumberU64())
if etxSet != nil {
break
}
current = types.CopyHeader(header)
}
// Run through the hash stack to update canonicalHash and forward state processor
for i := len(headersWithoutState) - 1; i >= 0; i-- {
err := hc.ReadInboundEtxsAndAppendBlock(headersWithoutState[i])
if err != nil {
return err
}
}
return nil
}
// ReadInboundEtxsAndAppendBlock reads the inbound etxs from database and appends the block
func (hc *HeaderChain) ReadInboundEtxsAndAppendBlock(header *types.Header) error {
block := hc.GetBlockOrCandidate(header.Hash(), header.NumberU64())
if block == nil {
return errors.New("Could not find block during reorg")
}
_, order, err := hc.engine.CalcOrder(block.Header())
if err != nil {
return err
}
nodeCtx := common.NodeLocation.Context()
var inboundEtxs types.Transactions
if order < nodeCtx {
inboundEtxs = rawdb.ReadInboundEtxs(hc.headerDb, header.Hash())
}
return hc.AppendBlock(block, inboundEtxs)
}
// findCommonAncestor
func (hc *HeaderChain) findCommonAncestor(header *types.Header) *types.Header {
current := types.CopyHeader(header)
for {
if current == nil {
return nil
}
canonicalHash := rawdb.ReadCanonicalHash(hc.headerDb, current.NumberU64())
if canonicalHash == current.Hash() {
return hc.GetHeaderByHash(canonicalHash)
}
current = hc.GetHeader(current.ParentHash(), current.NumberU64()-1)
}
}
func (hc *HeaderChain) AddPendingEtxs(pEtxs types.PendingEtxs) error {
if !pEtxs.IsValid(trie.NewStackTrie(nil)) {
log.Info("PendingEtx is not valid")
return ErrPendingEtxNotValid
}
log.Debug("Received pending ETXs", "block: ", pEtxs.Header.Hash())
// Only write the pending ETXs if we have not seen them before
if !hc.pendingEtxs.Contains(pEtxs.Header.Hash()) {
// Write to pending ETX database
rawdb.WritePendingEtxs(hc.headerDb, pEtxs)
// Also write to cache for faster access
hc.pendingEtxs.Add(pEtxs.Header.Hash(), pEtxs)
} else {
return ErrPendingEtxAlreadyKnown
}
return nil
}
func (hc *HeaderChain) AddBloom(bloom types.Bloom, hash common.Hash) error {
// Only write the bloom if we have not seen it before
if !hc.blooms.Contains(hash) {
// Write to bloom database
rawdb.WriteBloom(hc.headerDb, hash, bloom)
// Also write to cache for faster access
hc.blooms.Add(hash, bloom)
} else {
return ErrBloomAlreadyKnown
}
return nil
}
// loadLastState loads the last known chain state from the database. This method
// assumes that the chain manager mutex is held.
func (hc *HeaderChain) loadLastState() error {
// TODO: create function to find highest block number and fill Head FIFO
headsHashes := rawdb.ReadHeadsHashes(hc.headerDb)
if head := rawdb.ReadHeadBlockHash(hc.headerDb); head != (common.Hash{}) {
if chead := hc.GetHeaderByHash(head); chead != nil {
hc.currentHeader.Store(chead)
} else {
// This is only done if during the stop, currenthead hash was not stored
// properly and it doesn't crash the nodes
hc.currentHeader.Store(hc.genesisHeader)
}
} else {
// Recover the current header
log.Warn("Recovering Current Header")
recoverdHeader := hc.RecoverCurrentHeader()
rawdb.WriteHeadBlockHash(hc.headerDb, recoverdHeader.Hash())
hc.currentHeader.Store(recoverdHeader)
}
heads := make([]*types.Header, 0)
for _, hash := range headsHashes {
heads = append(heads, hc.GetHeaderByHash(hash))
}
hc.heads = heads
return nil
}
// Stop stops the blockchain service. If any imports are currently in progress
// it will abort them using the procInterrupt.
func (hc *HeaderChain) Stop() {
if !atomic.CompareAndSwapInt32(&hc.running, 0, 1) {
return
}
hashes := make([]common.Hash, 0)
for i := 0; i < len(hc.heads); i++ {
hashes = append(hashes, hc.heads[i].Hash())
}
// Save the heads
rawdb.WriteHeadsHashes(hc.headerDb, hashes)
// Unsubscribe all subscriptions registered from blockchain
hc.scope.Close()
hc.bc.scope.Close()
hc.wg.Wait()
if common.NodeLocation.Context() == common.ZONE_CTX && hc.ProcessingState() {
hc.bc.processor.Stop()
}
log.Info("headerchain stopped")
}
// Empty checks if the headerchain is empty.
func (hc *HeaderChain) Empty() bool {
genesis := hc.config.GenesisHash
for _, hash := range []common.Hash{rawdb.ReadHeadBlockHash(hc.headerDb)} {
if hash != genesis {
return false
}
}
return true
}
// GetBlockNumber retrieves the block number belonging to the given hash
// from the cache or database
func (hc *HeaderChain) GetBlockNumber(hash common.Hash) *uint64 {
if cached, ok := hc.numberCache.Get(hash); ok {
number := cached.(uint64)
return &number
}
number := rawdb.ReadHeaderNumber(hc.headerDb, hash)
if number != nil {
hc.numberCache.Add(hash, *number)
}
return number
}
func (hc *HeaderChain) GetTerminiByHash(hash common.Hash) *types.Termini {
termini := rawdb.ReadTermini(hc.headerDb, hash)
return termini
}
// GetBlockHashesFromHash retrieves a number of block hashes starting at a given
// hash, fetching towards the genesis block.
func (hc *HeaderChain) GetBlockHashesFromHash(hash common.Hash, max uint64) []common.Hash {
// Get the origin header from which to fetch
header := hc.GetHeaderByHash(hash)
if header == nil {
return nil
}
// Iterate the headers until enough is collected or the genesis reached
chain := make([]common.Hash, 0, max)
for i := uint64(0); i < max; i++ {
next := header.ParentHash()
if header = hc.GetHeader(next, header.NumberU64()-1); header == nil {
break
}
chain = append(chain, next)
if header.Number().Sign() == 0 {
break
}
}
return chain
}
// GetAncestor retrieves the Nth ancestor of a given block. It assumes that either the given block or
// a close ancestor of it is canonical. maxNonCanonical points to a downwards counter limiting the
// number of blocks to be individually checked before we reach the canonical chain.
//
// Note: ancestor == 0 returns the same block, 1 returns its parent and so on.
func (hc *HeaderChain) GetAncestor(hash common.Hash, number, ancestor uint64, maxNonCanonical *uint64) (common.Hash, uint64) {
if ancestor > number {
return common.Hash{}, 0
}
if ancestor == 1 {
// in this case it is cheaper to just read the header
if header := hc.GetHeader(hash, number); header != nil {
return header.ParentHash(), number - 1
}
return common.Hash{}, 0
}
for ancestor != 0 {
if rawdb.ReadCanonicalHash(hc.headerDb, number) == hash {
ancestorHash := rawdb.ReadCanonicalHash(hc.headerDb, number-ancestor)
if rawdb.ReadCanonicalHash(hc.headerDb, number) == hash {
number -= ancestor
return ancestorHash, number
}
}
if *maxNonCanonical == 0 {
return common.Hash{}, 0
}
*maxNonCanonical--
ancestor--
header := hc.GetHeader(hash, number)
if header == nil {
return common.Hash{}, 0
}
hash = header.ParentHash()
number--
}
return hash, number
}
func (hc *HeaderChain) WriteBlock(block *types.Block) {
hc.bc.WriteBlock(block)
}
// GetHeader retrieves a block header from the database by hash and number,
// caching it if found.
func (hc *HeaderChain) GetHeader(hash common.Hash, number uint64) *types.Header {
termini := hc.GetTerminiByHash(hash)
if termini == nil {
return nil
}
// Short circuit if the header's already in the cache, retrieve otherwise
if header, ok := hc.headerCache.Get(hash); ok {
return header.(*types.Header)
}
header := rawdb.ReadHeader(hc.headerDb, hash, number)
if header == nil {
return nil
}
// Cache the found header for next time and return
hc.headerCache.Add(hash, header)
return header
}
// GetHeaderByHash retrieves a block header from the database by hash, caching it if
// found.
func (hc *HeaderChain) GetHeaderByHash(hash common.Hash) *types.Header {
termini := hc.GetTerminiByHash(hash)
if termini == nil {
return nil
}
number := hc.GetBlockNumber(hash)
if number == nil {
return nil
}
return hc.GetHeader(hash, *number)
}
// GetHeaderOrCandidate retrieves a block header from the database by hash and number,
// caching it if found.
func (hc *HeaderChain) GetHeaderOrCandidate(hash common.Hash, number uint64) *types.Header {
// Short circuit if the header's already in the cache, retrieve otherwise
if header, ok := hc.headerCache.Get(hash); ok {
return header.(*types.Header)
}
header := rawdb.ReadHeader(hc.headerDb, hash, number)
if header == nil {
return nil
}
// Cache the found header for next time and return
hc.headerCache.Add(hash, header)
return header
}
// RecoverCurrentHeader retrieves the current head header of the canonical chain. The
// header is retrieved from the HeaderChain's internal cache
func (hc *HeaderChain) RecoverCurrentHeader() *types.Header {
// Start logarithmic ascent to find the upper bound
high := uint64(1)
for hc.GetHeaderByNumber(high) != nil {
high *= 2
}
// Run binary search to find the max header
low := high / 2
for low <= high {
mid := (low + high) / 2
if hc.GetHeaderByNumber(mid) != nil {
low = mid + 1
} else {
high = mid - 1
}
}
header := hc.GetHeaderByNumber(high)
log.Info("Header Recovered: ", "hash", header.Hash().String())
return header
}
// GetHeaderOrCandidateByHash retrieves a block header from the database by hash, caching it if
// found.
func (hc *HeaderChain) GetHeaderOrCandidateByHash(hash common.Hash) *types.Header {
number := hc.GetBlockNumber(hash)
if number == nil {
return nil
}
return hc.GetHeaderOrCandidate(hash, *number)
}
// HasHeader checks if a block header is present in the database or not.
// In theory, if header is present in the database, all relative components
// like td and hash->number should be present too.
func (hc *HeaderChain) HasHeader(hash common.Hash, number uint64) bool {
if hc.numberCache.Contains(hash) || hc.headerCache.Contains(hash) {
return true
}
return rawdb.HasHeader(hc.headerDb, hash, number)
}
// GetHeaderByNumber retrieves a block header from the database by number,
// caching it (associated with its hash) if found.
func (hc *HeaderChain) GetHeaderByNumber(number uint64) *types.Header {
hash := rawdb.ReadCanonicalHash(hc.headerDb, number)
if hash == (common.Hash{}) {
return nil
}
return hc.GetHeader(hash, number)
}
func (hc *HeaderChain) GetCanonicalHash(number uint64) common.Hash {
hash := rawdb.ReadCanonicalHash(hc.headerDb, number)
return hash
}
// CurrentHeader retrieves the current head header of the canonical chain. The
// header is retrieved from the HeaderChain's internal cache.
func (hc *HeaderChain) CurrentHeader() *types.Header {
return hc.currentHeader.Load().(*types.Header)
}
// CurrentBlock returns the block for the current header.
func (hc *HeaderChain) CurrentBlock() *types.Block {
return hc.GetBlockOrCandidateByHash(hc.CurrentHeader().Hash())
}
// SetGenesis sets a new genesis block header for the chain
func (hc *HeaderChain) SetGenesis(head *types.Header) {
hc.genesisHeader = head
}
// Config retrieves the header chain's chain configuration.
func (hc *HeaderChain) Config() *params.ChainConfig { return hc.config }
// GetBlock implements consensus.ChainReader, and returns nil for every input as
// a header chain does not have blocks available for retrieval.
func (hc *HeaderChain) GetBlock(hash common.Hash, number uint64) *types.Block {
return hc.bc.GetBlock(hash, number)
}
// CheckContext checks to make sure the range of a context or order is valid
func (hc *HeaderChain) CheckContext(context int) error {
if context < 0 || context > common.HierarchyDepth {
return errors.New("the provided path is outside the allowable range")
}
return nil
}
// CheckLocationRange checks to make sure the range of r and z are valid
func (hc *HeaderChain) CheckLocationRange(location []byte) error {
if int(location[0]) < 1 || int(location[0]) > common.NumRegionsInPrime {
return errors.New("the provided location is outside the allowable region range")
}
if int(location[1]) < 1 || int(location[1]) > common.NumZonesInRegion {
return errors.New("the provided location is outside the allowable zone range")
}
return nil
}
// GasLimit returns the gas limit of the current HEAD block.
func (hc *HeaderChain) GasLimit() uint64 {
return hc.CurrentHeader().GasLimit()
}
// GetUnclesInChain retrieves all the uncles from a given block backwards until
// a specific distance is reached.
func (hc *HeaderChain) GetUnclesInChain(block *types.Block, length int) []*types.Header {
uncles := []*types.Header{}
for i := 0; block != nil && i < length; i++ {
uncles = append(uncles, block.Uncles()...)
block = hc.GetBlock(block.ParentHash(), block.NumberU64()-1)
}
return uncles
}
// GetGasUsedInChain retrieves all the gas used from a given block backwards until
// a specific distance is reached.
func (hc *HeaderChain) GetGasUsedInChain(block *types.Block, length int) int64 {
gasUsed := 0
for i := 0; block != nil && i < length; i++ {
gasUsed += int(block.GasUsed())
block = hc.GetBlock(block.ParentHash(), block.NumberU64()-1)
}
return int64(gasUsed)
}
// GetGasUsedInChain retrieves all the gas used from a given block backwards until
// a specific distance is reached.
func (hc *HeaderChain) CalculateBaseFee(header *types.Header) *big.Int {
return misc.CalcBaseFee(hc.Config(), header)
}
// Export writes the active chain to the given writer.
func (hc *HeaderChain) Export(w io.Writer) error {
return hc.ExportN(w, uint64(0), hc.CurrentHeader().NumberU64())
}
// ExportN writes a subset of the active chain to the given writer.
func (hc *HeaderChain) ExportN(w io.Writer, first uint64, last uint64) error {
hc.headermu.RLock()
defer hc.headermu.RUnlock()
if first > last {
return fmt.Errorf("export failed: first (%d) is greater than last (%d)", first, last)
}
log.Info("Exporting batch of blocks", "count", last-first+1)
start, reported := time.Now(), time.Now()
for nr := first; nr <= last; nr++ {
block := hc.GetBlockByNumber(nr)
if block == nil {
return fmt.Errorf("export failed on #%d: not found", nr)
}
if err := block.EncodeRLP(w); err != nil {
return err
}
if time.Since(reported) >= statsReportLimit {
log.Info("Exporting blocks", "exported", block.NumberU64()-first, "elapsed", common.PrettyDuration(time.Since(start)))
reported = time.Now()
}
}
return nil
}
// GetBlockFromCacheOrDb looks up the body cache first and then checks the db
func (hc *HeaderChain) GetBlockFromCacheOrDb(hash common.Hash, number uint64) *types.Block {
// Short circuit if the block's already in the cache, retrieve otherwise
if cached, ok := hc.bc.blockCache.Get(hash); ok {
block := cached.(*types.Block)
return block
}
return hc.GetBlock(hash, number)
}
// GetBlockByHash retrieves a block from the database by hash, caching it if found.
func (hc *HeaderChain) GetBlockByHash(hash common.Hash) *types.Block {
number := hc.GetBlockNumber(hash)
if number == nil {
return nil
}
return hc.GetBlock(hash, *number)
}
func (hc *HeaderChain) GetBlockOrCandidate(hash common.Hash, number uint64) *types.Block {
return hc.bc.GetBlockOrCandidate(hash, number)
}
// GetBlockOrCandidateByHash retrieves any block from the database by hash, caching it if found.
func (hc *HeaderChain) GetBlockOrCandidateByHash(hash common.Hash) *types.Block {
number := hc.GetBlockNumber(hash)
if number == nil {
return nil
}
return hc.bc.GetBlockOrCandidate(hash, *number)
}
// GetBlockByNumber retrieves a block from the database by number, caching it
// (associated with its hash) if found.
func (hc *HeaderChain) GetBlockByNumber(number uint64) *types.Block {
hash := rawdb.ReadCanonicalHash(hc.headerDb, number)
if hash == (common.Hash{}) {
return nil
}
return hc.GetBlock(hash, number)
}
// GetBody retrieves a block body (transactions and uncles) from the database by
// hash, caching it if found.
func (hc *HeaderChain) GetBody(hash common.Hash) *types.Body {
// Short circuit if the body's already in the cache, retrieve otherwise
if cached, ok := hc.bc.bodyCache.Get(hash); ok {
body := cached.(*types.Body)
return body
}
number := hc.GetBlockNumber(hash)
if number == nil {
return nil
}
body := rawdb.ReadBody(hc.headerDb, hash, *number)
if body == nil {
return nil
}
// Cache the found body for next time and return
hc.bc.bodyCache.Add(hash, body)
return body
}
// GetBodyRLP retrieves a block body in RLP encoding from the database by hash,
// caching it if found.
func (hc *HeaderChain) GetBodyRLP(hash common.Hash) rlp.RawValue {
// Short circuit if the body's already in the cache, retrieve otherwise
if cached, ok := hc.bc.bodyRLPCache.Get(hash); ok {
return cached.(rlp.RawValue)
}
number := hc.GetBlockNumber(hash)
if number == nil {
return nil
}
body := rawdb.ReadBodyRLP(hc.headerDb, hash, *number)
if len(body) == 0 {
return nil
}
// Cache the found body for next time and return
hc.bc.bodyRLPCache.Add(hash, body)
return body
}
// GetBlocksFromHash returns the block corresponding to hash and up to n-1 ancestors.
// [deprecated by eth/62]
func (hc *HeaderChain) GetBlocksFromHash(hash common.Hash, n int) (blocks []*types.Block) {
number := hc.GetBlockNumber(hash)
if number == nil {
return nil
}
for i := 0; i < n; i++ {
block := hc.GetBlock(hash, *number)
if block == nil {
break
}
blocks = append(blocks, block)
hash = block.ParentHash()
*number--
}
return
}
// Engine reterives the consensus engine.
func (hc *HeaderChain) Engine() consensus.Engine {
return hc.engine
}
// SubscribeChainHeadEvent registers a subscription of ChainHeadEvent.
func (hc *HeaderChain) SubscribeChainHeadEvent(ch chan<- ChainHeadEvent) event.Subscription {
return hc.scope.Track(hc.chainHeadFeed.Subscribe(ch))
}
// SubscribeChainSideEvent registers a subscription of ChainSideEvent.
func (hc *HeaderChain) SubscribeChainSideEvent(ch chan<- ChainSideEvent) event.Subscription {
return hc.scope.Track(hc.chainSideFeed.Subscribe(ch))
}
func (hc *HeaderChain) StateAt(root common.Hash) (*state.StateDB, error) {
return hc.bc.processor.StateAt(root)
}