forked from CovenantSQL/CovenantSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chain.go
1058 lines (950 loc) · 26.4 KB
/
chain.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
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2018 The CovenantSQL Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package blockproducer
import (
"context"
"expvar"
"fmt"
"math"
"os"
"sync"
"time"
lru "github.com/hashicorp/golang-lru"
"github.com/pkg/errors"
mw "github.com/zserge/metric"
pi "github.com/CovenantSQL/CovenantSQL/blockproducer/interfaces"
"github.com/CovenantSQL/CovenantSQL/conf"
"github.com/CovenantSQL/CovenantSQL/crypto"
"github.com/CovenantSQL/CovenantSQL/crypto/asymmetric"
"github.com/CovenantSQL/CovenantSQL/crypto/hash"
"github.com/CovenantSQL/CovenantSQL/crypto/kms"
"github.com/CovenantSQL/CovenantSQL/proto"
"github.com/CovenantSQL/CovenantSQL/route"
rpc "github.com/CovenantSQL/CovenantSQL/rpc/mux"
"github.com/CovenantSQL/CovenantSQL/types"
"github.com/CovenantSQL/CovenantSQL/utils/log"
xi "github.com/CovenantSQL/CovenantSQL/xenomint/interfaces"
)
// Metric keys
const (
mwKeyHeight = "service:bp:height"
mwKeyTxPooled = "service:bp:pooled"
mwKeyTxConfirmed = "service:bp:confirmed"
)
func init() {
expvar.Publish(mwKeyTxPooled, mw.NewCounter("5m1m"))
expvar.Publish(mwKeyTxConfirmed, mw.NewCounter("5m1m"))
}
// Chain defines the main chain.
type Chain struct {
// Routine controlling components
ctx context.Context
cancel context.CancelFunc
wg *sync.WaitGroup
// RPC components
server *rpc.Server
caller *rpc.Caller
// Other components
storage xi.Storage
// NOTE(leventeliu): this LRU object is only used for block cache control,
// do NOT read it in any case.
blockCache *lru.Cache
// Channels for incoming blocks and transactions
pendingBlocks chan *types.BPBlock
pendingAddTxReqs chan *types.AddTxReq
// The following fields are read-only in runtime
address proto.AccountAddress
mode RunMode
genesisTime time.Time
period time.Duration
tick time.Duration
sync.RWMutex // protects following fields
bpInfos []*blockProducerInfo
localBPInfo *blockProducerInfo
localNodeID proto.NodeID
confirms uint32
nextHeight uint32
offset time.Duration
lastIrre *blockNode
immutable *metaState
headIndex int
headBranch *branch
branches []*branch
txPool map[hash.Hash]pi.Transaction
}
// NewChain creates a new blockchain.
func NewChain(cfg *Config) (c *Chain, err error) {
return NewChainWithContext(context.Background(), cfg)
}
// NewChainWithContext creates a new blockchain with context.
func NewChainWithContext(ctx context.Context, cfg *Config) (c *Chain, err error) {
var (
ierr error
st xi.Storage
cache *lru.Cache
lastIrre *blockNode
heads []*blockNode
immutable *metaState
txPool map[hash.Hash]pi.Transaction
branches []*branch
headBranch *branch
headIndex int
addr proto.AccountAddress
bpInfos []*blockProducerInfo
localBPInfo *blockProducerInfo
)
// Verify genesis block in config
if cfg.Genesis == nil {
err = ErrNilGenesis
return
}
if ierr = cfg.Genesis.VerifyHash(); ierr != nil {
err = errors.Wrap(ierr, "failed to verify genesis block hash")
return
}
// Open storage
var existed bool
if fi, err := os.Stat(cfg.DataFile); err == nil && fi.Mode().IsRegular() {
existed = true
}
if st, ierr = openStorage(fmt.Sprintf("file:%s", cfg.DataFile)); ierr != nil {
err = errors.Wrap(ierr, "failed to open storage")
return
}
defer func() {
if err != nil {
st.Close()
}
}()
// Create block cache
if cfg.BlockCacheSize > conf.MaxCachedBlock {
cfg.BlockCacheSize = conf.MaxCachedBlock
}
if cfg.BlockCacheSize <= 0 {
cfg.BlockCacheSize = 1 // Must provide a positive size
}
if cache, err = lru.NewWithEvict(cfg.BlockCacheSize, func(key interface{}, value interface{}) {
if node, ok := value.(*blockNode); ok && node != nil {
node.clear()
}
}); err != nil {
return
}
// Create initial state from genesis block and store
if !existed {
var init = newMetaState()
for _, v := range cfg.Genesis.Transactions {
if ierr = init.apply(v, 0); ierr != nil {
err = errors.Wrap(ierr, "failed to initialize immutable state")
return
}
}
var sps = init.compileChanges(nil)
sps = append(sps, addBlock(0, cfg.Genesis))
sps = append(sps, updateIrreversible(cfg.Genesis.SignedHeader.DataHash))
if ierr = store(st, sps, nil); ierr != nil {
err = errors.Wrap(ierr, "failed to initialize storage")
return
}
}
// Load from database
if lastIrre, heads, immutable, txPool, ierr = loadDatabase(st); ierr != nil {
err = errors.Wrap(ierr, "failed to load data from storage")
return
}
// Check genesis block
if persistedGenesis := lastIrre.ancestorByCount(0); persistedGenesis == nil ||
!persistedGenesis.hash.IsEqual(cfg.Genesis.BlockHash()) {
err = ErrGenesisHashNotMatch
return
}
// Rebuild branches
for _, v := range heads {
log.WithFields(log.Fields{
"irre_hash": lastIrre.hash.Short(4),
"irre_count": lastIrre.count,
"head_hash": v.hash.Short(4),
"head_count": v.count,
}).Debug("checking head")
if v.hasAncestor(lastIrre) {
// Load any reversible blocks from storage for branch rebuilding
reversibles := v.fetchNodeList(lastIrre.count + 1)
for _, r := range reversibles {
if r.load() != nil {
continue
}
var block *types.BPBlock
if block, ierr = loadBlock(st, r.hash); ierr != nil {
err = errors.Wrapf(
ierr, "failed to load block %s from database", r.hash.Short(4))
return
}
// Store block object
r.block.Store(block)
}
var br *branch
if br, ierr = newBranch(lastIrre, v, immutable, txPool); ierr != nil {
err = errors.Wrapf(ierr, "failed to rebuild branch with head %s", v.hash.Short(4))
return
}
branches = append(branches, br)
}
}
if len(branches) == 0 {
err = ErrNoAvailableBranch
return
}
// Select head branch
for i, v := range branches {
if headBranch == nil || v.head.count > headBranch.head.count {
headIndex = i
headBranch = v
}
}
// Get accountAddress
var pubKey *asymmetric.PublicKey
if pubKey, err = kms.GetLocalPublicKey(); err != nil {
return
}
if addr, err = crypto.PubKeyHash(pubKey); err != nil {
return
}
// Setup peer list
var (
l = uint32(len(cfg.Peers.Servers))
threshold float64
needConfirms uint32
)
if localBPInfo, bpInfos, err = buildBlockProducerInfos(
cfg.NodeID, cfg.Peers, cfg.Mode == APINodeMode,
); err != nil {
return
}
if threshold = cfg.ConfirmThreshold; threshold <= 0.0 {
threshold = conf.DefaultConfirmThreshold
}
if needConfirms = uint32(math.Ceil(float64(l)*threshold + 1)); needConfirms > l {
needConfirms = l
}
// create chain
var cld, ccl = context.WithCancel(ctx)
c = &Chain{
ctx: cld,
cancel: ccl,
wg: &sync.WaitGroup{},
server: cfg.Server,
caller: rpc.NewCaller(),
storage: st,
blockCache: cache,
pendingBlocks: make(chan *types.BPBlock),
pendingAddTxReqs: make(chan *types.AddTxReq),
address: addr,
mode: cfg.Mode,
genesisTime: cfg.Genesis.SignedHeader.Timestamp,
period: cfg.Period,
tick: cfg.Tick,
bpInfos: bpInfos,
localBPInfo: localBPInfo,
localNodeID: cfg.NodeID,
confirms: needConfirms,
nextHeight: headBranch.head.height + 1,
offset: time.Duration(0), // TODO(leventeliu): initialize offset
lastIrre: lastIrre,
immutable: immutable,
headIndex: headIndex,
headBranch: headBranch,
branches: branches,
txPool: txPool,
}
// NOTE(leventeliu): this implies that BP chain is a singleton, otherwise we will need
// independent metric key for each chain instance.
if expvar.Get(mwKeyHeight) == nil {
expvar.Publish(mwKeyHeight, mw.NewGauge(fmt.Sprintf("5m%.0fs", cfg.Period.Seconds())))
}
expvar.Get(mwKeyHeight).(mw.Metric).Add(float64(c.head().height))
log.WithFields(log.Fields{
"local": c.getLocalBPInfo(),
"period": c.period,
"tick": c.tick,
"height": c.head().height,
}).Debug("current chain state")
return
}
// Start starts the chain by step:
// 1. sync the chain
// 2. goroutine for getting blocks
// 3. goroutine for getting txes.
func (c *Chain) Start() {
// Start blocks/txs processing goroutines
c.goFunc(c.processBlocks)
c.goFunc(c.processTxs)
// Synchronize heads to current block period
c.syncHeads()
// TODO(leventeliu): subscribe ChainBus.
// ...
// Start main cycle and service
c.goFunc(c.mainCycle)
c.startService(c)
}
// Stop stops the main process of the sql-chain.
func (c *Chain) Stop() (err error) {
// Stop main process
var le = log.WithFields(log.Fields{
"local": c.getLocalBPInfo(),
})
le.Debug("stopping chain")
c.stop()
le.Debug("chain service stopped")
c.storage.Close()
le.Debug("chain database closed")
// FIXME(leventeliu): RPC server should provide an `unregister` method to detach chain service
// instance. Add it to Chain.stop(), then working channels can be closed safely.
// Otherwise a DATARACE (while closing a channel with a blocking write from RPC service) or
// `write on closed channel` panic may occur.
// Comment this out for now, IT IS A RESOURCE LEAK.
//
//close(c.pendingBlocks)
//close(c.pendingTxs)
return
}
func (c *Chain) pushBlock(b *types.BPBlock) (err error) {
var ierr error
if ierr = b.Verify(); ierr != nil {
err = errors.Wrap(ierr, "failed to check block")
return
}
if ierr = c.applyBlock(b); ierr != nil {
err = errors.Wrap(ierr, "failed to apply block")
return
}
return
}
func (c *Chain) produceBlock(now time.Time) (err error) {
var (
priv *asymmetric.PrivateKey
b *types.BPBlock
)
if priv, err = kms.GetLocalPrivateKey(); err != nil {
return
}
if b, err = c.produceAndStoreBlock(now, priv); err != nil {
return
}
log.WithFields(log.Fields{
"block_time": b.Timestamp(),
"block_hash": b.BlockHash().Short(4),
"parent_hash": b.ParentHash().Short(4),
}).Debug("produced new block")
// Broadcast to other block producers
c.nonblockingBroadcastBlock(b)
return
}
// advanceNextHeight does the check and runs block producing if its my turn.
func (c *Chain) advanceNextHeight(now time.Time, d time.Duration) {
var elapsed = -d
log.WithFields(log.Fields{
"local": c.getLocalBPInfo(),
"enclosing_height": c.getNextHeight() - 1,
"using_timestamp": now.Format(time.RFC3339Nano),
"elapsed_seconds": elapsed.Seconds(),
}).Info("enclosing current height and advancing to next height")
defer func() {
c.increaseNextHeight()
expvar.Get(mwKeyHeight).(mw.Metric).Add(float64(c.head().height))
}()
// Skip if it's not my turn
if c.mode == APINodeMode || !c.isMyTurn() {
return
}
// Normally, a block producing should start right after the new period, but more time may also
// elapse since the last block synchronizing.
if elapsed+c.tick > c.period { // TODO(leventeliu): add threshold config for `elapsed`.
log.WithFields(log.Fields{
"advanced_height": c.getNextHeight(),
"using_timestamp": now.Format(time.RFC3339Nano),
"elapsed_seconds": elapsed.Seconds(),
}).Warn("too much time elapsed in the new period, skip this block")
return
}
log.WithField("height", c.getNextHeight()).Info("producing a new block")
if err := c.produceBlock(now); err != nil {
log.WithField("now", now.Format(time.RFC3339Nano)).WithError(err).Errorln(
"failed to produce block")
}
}
func (c *Chain) syncHeads() {
for {
var (
now = c.now()
nowHeight uint32
)
if now.Before(c.genesisTime) {
log.WithFields(log.Fields{
"local": c.getLocalBPInfo(),
}).Info("now time is before genesis time, waiting for genesis")
break
}
if nowHeight = c.heightOfTime(c.now()); c.getNextHeight() > nowHeight {
break
}
for c.getNextHeight() <= nowHeight {
// TODO(leventeliu): use the test mode flag to bypass the long-running synchronizing
// on startup by now, need better solution here.
if conf.GConf.StartupSyncHoles {
log.WithFields(log.Fields{
"local": c.getLocalBPInfo(),
"next_height": c.getNextHeight(),
"now_height": nowHeight,
}).Debug("synchronizing head blocks")
c.blockingSyncCurrentHead(c.ctx, conf.BPStartupRequiredReachableCount)
}
c.increaseNextHeight()
}
}
}
func (c *Chain) processBlocks(ctx context.Context) {
for {
select {
case block := <-c.pendingBlocks:
err := c.pushBlock(block)
if err != nil {
log.WithFields(log.Fields{
"block_hash": block.BlockHash(),
"block_parent_hash": block.ParentHash(),
"block_timestamp": block.Timestamp(),
}).Debug(err)
}
case <-ctx.Done():
log.WithError(c.ctx.Err()).Info("abort block processing")
return
}
}
}
func (c *Chain) addTx(req *types.AddTxReq) {
select {
case c.pendingAddTxReqs <- req:
case <-c.ctx.Done():
log.WithError(c.ctx.Err()).Warn("add transaction aborted")
}
}
func (c *Chain) processAddTxReq(addTxReq *types.AddTxReq) {
// Nil check
if addTxReq == nil || addTxReq.Tx == nil {
log.Warn("empty add tx request")
return
}
var (
ttl = addTxReq.TTL
tx = addTxReq.Tx
txhash = tx.Hash()
addr = tx.GetAccountAddress()
nonce = tx.GetAccountNonce()
le = log.WithFields(log.Fields{
"hash": txhash.Short(4),
"address": addr,
"nonce": nonce,
"type": tx.GetTransactionType(),
})
base pi.AccountNonce
err error
)
// Existense check
if ok := func() (ok bool) {
c.RLock()
defer c.RUnlock()
_, ok = c.txPool[txhash]
return
}(); ok {
le.Debug("tx already exists, abort processing")
return
}
// Verify transaction
if err = tx.Verify(); err != nil {
le.WithError(err).Warn("failed to verify transaction")
return
}
if base, err = c.immutableNextNonce(addr); err != nil {
le.WithError(err).Warn("failed to load base nonce of transaction account")
return
}
if nonce < base || nonce >= base+conf.MaxPendingTxsPerAccount {
// TODO(leventeliu): should persist to somewhere for tx query?
le.WithFields(log.Fields{
"base_nonce": base,
"pending_limit": conf.MaxPendingTxsPerAccount,
}).Warn("invalid transaction nonce")
return
}
// Broadcast to other block producers
if ttl > conf.MaxTxBroadcastTTL {
ttl = conf.MaxTxBroadcastTTL
}
if ttl > 0 {
c.nonblockingBroadcastTx(ttl-1, tx)
}
// Add to tx pool
if err = c.storeTx(tx); err != nil {
le.WithError(err).Error("failed to add transaction")
return
}
expvar.Get(mwKeyTxPooled).(mw.Metric).Add(1)
}
func (c *Chain) processTxs(ctx context.Context) {
for {
select {
case addTxReq := <-c.pendingAddTxReqs:
c.processAddTxReq(addTxReq)
case <-ctx.Done():
log.WithError(c.ctx.Err()).Info("abort transaction processing")
return
}
}
}
func (c *Chain) mainCycle(ctx context.Context) {
var timer = time.NewTimer(0)
defer func() {
if !timer.Stop() {
<-timer.C
}
}()
for {
select {
case <-timer.C:
// Try to fetch block at height `nextHeight-1` until enough peers are reachable
if err := c.blockingSyncCurrentHead(ctx, c.getRequiredConfirms()); err != nil {
log.WithError(err).Info("abort main cycle")
timer.Reset(0)
return
}
var t, d = c.nextTick()
if d <= 0 {
// Try to produce block at `nextHeight` if it's my turn, and increase height by 1
c.advanceNextHeight(t, d)
} else {
log.WithFields(log.Fields{
"peer": c.getLocalBPInfo(),
"next_height": c.getNextHeight(),
"head_height": c.head().height,
"head_block": c.head().hash.Short(4),
"now_time": t.Format(time.RFC3339Nano),
"duration": d,
}).Debug("main cycle")
}
timer.Reset(d)
case <-ctx.Done():
log.WithError(ctx.Err()).Info("abort main cycle")
return
}
}
}
func (c *Chain) blockingSyncCurrentHead(ctx context.Context, requiredReachable uint32) (err error) {
var (
ticker *time.Ticker
interval = 1 * time.Second
)
if c.tick < interval {
interval = c.tick
}
ticker = time.NewTicker(interval)
defer ticker.Stop()
for {
if c.syncCurrentHead(ctx, requiredReachable) {
return
}
select {
case <-ticker.C:
case <-ctx.Done():
err = ctx.Err()
return
}
}
}
// syncCurrentHead synchronizes a block at the current height of the local peer from the known
// remote peers. The return value `ok` indicates that there're at least `requiredReachable-1`
// replies from these gossip calls.
func (c *Chain) syncCurrentHead(ctx context.Context, requiredReachable uint32) (ok bool) {
var currentHeight = c.getNextHeight() - 1
if c.head().height >= currentHeight {
ok = true
return
}
// Initiate blocking gossip calls to fetch block of the current height,
// with timeout of one tick.
var (
unreachable = c.blockingFetchBlock(ctx, currentHeight)
serversNum = c.getLocalBPInfo().total
)
switch c.mode {
case BPMode:
ok = unreachable+requiredReachable <= serversNum
case APINodeMode:
ok = unreachable < serversNum
default:
ok = false
log.Fatalf("unknown run mode: %v", c.mode)
}
if !ok {
log.WithFields(log.Fields{
"peer": c.getLocalBPInfo(),
"sync_head_height": currentHeight,
"unreachable_count": unreachable,
}).Warn("one or more block producers are currently unreachable")
}
return
}
func (c *Chain) storeTx(tx pi.Transaction) (err error) {
var k = tx.Hash()
c.Lock()
defer c.Unlock()
if _, ok := c.txPool[k]; ok {
err = ErrExistedTx
return
}
return store(c.storage, []storageProcedure{addTx(tx)}, func() {
c.txPool[k] = tx
for _, v := range c.branches {
v.addTx(tx)
}
})
}
func (c *Chain) replaceAndSwitchToBranch(
newBlock *types.BPBlock, originBrIdx int, newBranch *branch) (err error,
) {
var (
lastIrre *blockNode
newIrres []*blockNode
sps []storageProcedure
up storageCallback
txCount int
height = c.heightOfTime(newBlock.Timestamp())
resultTxPool = make(map[hash.Hash]pi.Transaction)
expiredTxs []pi.Transaction
)
// Find new irreversible blocks
//
// NOTE(leventeliu):
// May have multiple new irreversible blocks here if peer list shrinks. May also have
// no new irreversible block at all if peer list expands.
lastIrre = newBranch.head.lastIrreversible(c.confirms)
newIrres = lastIrre.fetchNodeList(c.lastIrre.count + 1)
// Apply irreversible blocks to create dirty map on immutable cache
for k, v := range c.txPool {
resultTxPool[k] = v
}
for _, b := range newIrres {
txCount += b.txCount
for _, tx := range b.load().Transactions {
if err := c.immutable.apply(tx, b.height); err != nil {
log.WithError(err).Fatal("failed to apply block to immutable database")
}
delete(resultTxPool, tx.Hash()) // Remove confirmed transaction
}
}
// Check tx expiration
for k, v := range resultTxPool {
if base, err := c.immutable.nextNonce(
v.GetAccountAddress(),
); err != nil || v.GetAccountNonce() < base {
log.WithFields(log.Fields{
"hash": k.Short(4),
"type": v.GetTransactionType(),
"account": v.GetAccountAddress(),
"nonce": v.GetAccountNonce(),
"immutable_base_nonce": base,
}).Debug("transaction expired")
expiredTxs = append(expiredTxs, v)
delete(resultTxPool, k) // Remove expired transaction
}
}
// Prepare storage procedures to update immutable database
sps = c.immutable.compileChanges(sps)
sps = append(sps, addBlock(height, newBlock))
sps = append(sps, buildBlockIndex(height, newBlock))
for _, n := range newIrres {
sps = append(sps, deleteTxs(n.load().Transactions))
}
if len(expiredTxs) > 0 {
sps = append(sps, deleteTxs(expiredTxs))
}
sps = append(sps, updateIrreversible(lastIrre.hash))
// Prepare callback to update cache
up = func() {
// Update last irreversible block
c.lastIrre = lastIrre
// Apply irreversible blocks to immutable database
c.immutable.commit()
// Prune branches
var (
idx int
brs = make([]*branch, 0, len(c.branches))
)
for i, b := range c.branches {
if i == originBrIdx {
// Replace origin branch with new branch
newBranch.preview.commit()
brs = append(brs, newBranch)
idx = len(brs) - 1
} else if b.head.hasAncestor(lastIrre) {
// Move to new branches slice
brs = append(brs, b)
} else {
// Prune this branch
log.WithFields(log.Fields{
"branch": func() string {
if i == c.headIndex {
return "[head]"
}
return fmt.Sprintf("[%04d]", i)
}(),
}).Debug("pruning branch")
}
}
// Replace current branches
c.headBranch = newBranch
c.headIndex = idx
c.branches = brs
// Clear transactions in each branch
for _, b := range newIrres {
for _, br := range c.branches {
br.clearPackedTxs(b.load().Transactions)
}
}
for _, br := range c.branches {
br.clearUnpackedTxs(expiredTxs)
}
// Update txPool to result txPool (packed and expired transactions cleared!)
c.txPool = resultTxPool
// Register new irreversible blocks to LRU cache list
for _, b := range newIrres {
c.blockCache.Add(b.count, b)
}
}
// Write to immutable database and update cache
if err = store(c.storage, sps, up); err != nil {
c.immutable.clean()
return
}
expvar.Get(mwKeyTxConfirmed).(mw.Metric).Add(float64(txCount))
// TODO(leventeliu): trigger ChainBus.Publish.
// ...
return
}
func (c *Chain) stat() {
c.RLock()
defer c.RUnlock()
for i, v := range c.branches {
var buff string
if i == c.headIndex {
buff += "[head] "
} else {
buff += fmt.Sprintf("[%04d] ", i)
}
buff += v.sprint(c.lastIrre.count + 1)
log.WithFields(log.Fields{
"branch": buff,
}).Info("runtime state")
}
}
func (c *Chain) applyBlock(bl *types.BPBlock) (err error) {
var (
ok bool
ierr error
br *branch
parent *blockNode
head *blockNode
height = c.heightOfTime(bl.Timestamp())
)
defer c.stat()
c.Lock()
defer c.Unlock()
for i, v := range c.branches {
// Grow a branch
if v.head.hash.IsEqual(bl.ParentHash()) {
head = newBlockNode(height, bl, v.head)
if br, ierr = v.applyBlock(head); ierr != nil {
err = errors.Wrapf(ierr, "failed to apply block %s", head.hash.Short(4))
return
}
// Grow a branch while the current branch is not changed
if br.head.count <= c.headBranch.head.count {
return store(c.storage,
[]storageProcedure{addBlock(height, bl)},
func() {
br.preview.commit()
c.branches[i] = br
},
)
}
// Switch branch or grow current branch
return c.replaceAndSwitchToBranch(bl, i, br)
}
}
for _, v := range c.branches {
if n := v.head.ancestor(height); n != nil && n.hash.IsEqual(bl.BlockHash()) {
// Return silently if block exists in the current branch
return
}
}
for _, v := range c.branches {
// Fork and create new branch
if parent, ok = v.head.hasAncestorWithMinCount(
bl.SignedHeader.ParentHash, c.lastIrre.count,
); ok {
head = newBlockNode(height, bl, parent)
if br, ierr = newBranch(c.lastIrre, head, c.immutable, c.txPool); ierr != nil {
err = errors.Wrapf(ierr, "failed to fork from %s", parent.hash.Short(4))
return
}
return store(c.storage,
[]storageProcedure{addBlock(height, bl)},
func() { c.branches = append(c.branches, br) },
)
}
}
err = ErrParentNotFound
return
}
func (c *Chain) produceAndStoreBlock(
now time.Time, priv *asymmetric.PrivateKey) (out *types.BPBlock, err error,
) {
var (
bl *types.BPBlock
br *branch
ierr error
)
defer c.stat()
c.Lock()
defer c.Unlock()
// Try to produce new block
if br, bl, ierr = c.headBranch.produceBlock(
c.heightOfTime(now), now, c.address, priv,
); ierr != nil {
err = errors.Wrapf(ierr, "failed to produce block at head %s",
c.headBranch.head.hash.Short(4))
return
}
if ierr = c.replaceAndSwitchToBranch(bl, c.headIndex, br); ierr != nil {
err = errors.Wrapf(ierr, "failed to switch branch #%d:%s",
c.headIndex, c.headBranch.head.hash.Short(4))
return
}
out = bl
return
}
// now returns the current coordinated chain time.
func (c *Chain) now() time.Time {
c.RLock()
defer c.RUnlock()
return time.Now().Add(c.offset).UTC()
}
func (c *Chain) startService(chain *Chain) {
c.server.RegisterService(route.BlockProducerRPCName, &ChainRPCService{chain: chain})
}
// nextTick returns the current clock reading and the duration till the next turn. If duration
// is less or equal to 0, use the clock reading to run the next cycle - this avoids some problem
// caused by concurrent time synchronization.
func (c *Chain) nextTick() (t time.Time, d time.Duration) {
var h uint32
h, t = func() (nt uint32, t time.Time) {
c.RLock()
defer c.RUnlock()
nt = c.nextHeight
t = time.Now().Add(c.offset).UTC()
return
}()
d = c.genesisTime.Add(time.Duration(h) * c.period).Sub(t)
if d > c.tick {
d = c.tick
}
return
}
func (c *Chain) isMyTurn() bool {
c.RLock()
defer c.RUnlock()
return c.nextHeight%c.localBPInfo.total == c.localBPInfo.rank
}
// increaseNextHeight prepares the chain state for the next turn.
func (c *Chain) increaseNextHeight() {
c.Lock()
defer c.Unlock()
c.nextHeight++
}
// heightOfTime calculates the heightOfTime with this sql-chain config of a given time reading.
func (c *Chain) heightOfTime(t time.Time) uint32 {
return uint32(t.Sub(c.genesisTime) / c.period)
}
func (c *Chain) getRequiredConfirms() uint32 {
c.RLock()
defer c.RUnlock()
return c.confirms
}
func (c *Chain) getNextHeight() uint32 {
c.RLock()
defer c.RUnlock()
return c.nextHeight
}
func (c *Chain) getLocalBPInfo() *blockProducerInfo {
c.RLock()