-
Notifications
You must be signed in to change notification settings - Fork 319
/
blockchain.go
1307 lines (1183 loc) · 38.8 KB
/
blockchain.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 (c) 2018 IoTeX
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package blockchain
import (
"context"
"math/big"
"os"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/facebookgo/clock"
"github.com/golang/protobuf/proto"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/action/protocol/account"
accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util"
"github.com/iotexproject/iotex-core/action/protocol/execution/evm"
"github.com/iotexproject/iotex-core/action/protocol/poll"
"github.com/iotexproject/iotex-core/action/protocol/poll/pollpb"
"github.com/iotexproject/iotex-core/action/protocol/rewarding"
"github.com/iotexproject/iotex-core/action/protocol/rolldpos"
"github.com/iotexproject/iotex-core/action/protocol/vote"
"github.com/iotexproject/iotex-core/actpool/actioniterator"
"github.com/iotexproject/iotex-core/address"
"github.com/iotexproject/iotex-core/blockchain/block"
"github.com/iotexproject/iotex-core/config"
"github.com/iotexproject/iotex-core/crypto"
"github.com/iotexproject/iotex-core/db"
"github.com/iotexproject/iotex-core/pkg/hash"
"github.com/iotexproject/iotex-core/pkg/lifecycle"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/pkg/prometheustimer"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
"github.com/iotexproject/iotex-core/pkg/util/fileutil"
"github.com/iotexproject/iotex-core/state"
"github.com/iotexproject/iotex-core/state/factory"
)
var (
blockMtc = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "iotex_block_metrics",
Help: "Block metrics.",
},
[]string{"type"},
)
errDelegatesNotExist = errors.New("delegates cannot be found")
)
func init() {
prometheus.MustRegister(blockMtc)
}
// Blockchain represents the blockchain data structure and hosts the APIs to access it
type Blockchain interface {
lifecycle.StartStopper
// Balance returns balance of an account
Balance(addr string) (*big.Int, error)
// Nonce returns the nonce if the account exists
Nonce(addr string) (uint64, error)
// CreateState adds a new account with initial balance to the factory
CreateState(addr string, init *big.Int) (*state.Account, error)
// CandidatesByHeight returns the candidate list by a given height
CandidatesByHeight(height uint64) ([]*state.Candidate, error)
// ProductivityByEpoch returns the number of produced blocks per delegate in an epoch
ProductivityByEpoch(epochNum uint64) (uint64, map[string]uint64, error)
// For exposing blockchain states
// GetHeightByHash returns Block's height by hash
GetHeightByHash(h hash.Hash256) (uint64, error)
// GetHashByHeight returns Block's hash by height
GetHashByHeight(height uint64) (hash.Hash256, error)
// GetBlockByHeight returns Block by height
GetBlockByHeight(height uint64) (*block.Block, error)
// GetBlockByHash returns Block by hash
GetBlockByHash(h hash.Hash256) (*block.Block, error)
// GetTotalActions returns the total number of actions
GetTotalActions() (uint64, error)
// GetReceiptByActionHash returns the receipt by action hash
GetReceiptByActionHash(h hash.Hash256) (*action.Receipt, error)
// GetActionsFromAddress returns actions from address
GetActionsFromAddress(address string) ([]hash.Hash256, error)
// GetActionsToAddress returns actions to address
GetActionsToAddress(address string) ([]hash.Hash256, error)
// GetActionByActionHash returns action by action hash
GetActionByActionHash(h hash.Hash256) (action.SealedEnvelope, error)
// GetBlockHashByActionHash returns Block hash by action hash
GetBlockHashByActionHash(h hash.Hash256) (hash.Hash256, error)
// GetFactory returns the state factory
GetFactory() factory.Factory
// GetChainID returns the chain ID
ChainID() uint32
// ChainAddress returns chain address on parent chain, the root chain return empty.
ChainAddress() string
// TipHash returns tip block's hash
TipHash() hash.Hash256
// TipHeight returns tip block's height
TipHeight() uint64
// StateByAddr returns account of a given address
StateByAddr(address string) (*state.Account, error)
// RecoverChainAndState recovers the chain to target height and refresh state db if necessary
RecoverChainAndState(targetHeight uint64) error
// GenesisTimestamp returns the timestamp of genesis
GenesisTimestamp() int64
// For block operations
// MintNewBlock creates a new block with given actions
// Note: the coinbase transfer will be added to the given transfers when minting a new block
MintNewBlock(
actionMap map[string][]action.SealedEnvelope,
timestamp time.Time,
) (*block.Block, error)
// CommitBlock validates and appends a block to the chain
CommitBlock(blk *block.Block) error
// ValidateBlock validates a new block before adding it to the blockchain
ValidateBlock(blk *block.Block) error
// For action operations
// Validator returns the current validator object
Validator() Validator
// SetValidator sets the current validator object
SetValidator(val Validator)
// For smart contract operations
// ExecuteContractRead runs a read-only smart contract operation, this is done off the network since it does not
// cause any state change
ExecuteContractRead(caller address.Address, ex *action.Execution) (*action.Receipt, error)
// AddSubscriber make you listen to every single produced block
AddSubscriber(BlockCreationSubscriber) error
// RemoveSubscriber make you listen to every single produced block
RemoveSubscriber(BlockCreationSubscriber) error
}
// blockchain implements the Blockchain interface
type blockchain struct {
mu sync.RWMutex // mutex to protect utk, tipHeight and tipHash
dao *blockDAO
config config.Config
tipHeight uint64
tipHash hash.Hash256
validator Validator
lifecycle lifecycle.Lifecycle
clk clock.Clock
blocklistener []BlockCreationSubscriber
timerFactory *prometheustimer.TimerFactory
// used by account-based model
sf factory.Factory
registry *protocol.Registry
}
// Option sets blockchain construction parameter
type Option func(*blockchain, config.Config) error
// DefaultStateFactoryOption sets blockchain's sf from config
func DefaultStateFactoryOption() Option {
return func(bc *blockchain, cfg config.Config) (err error) {
if cfg.Chain.EnableTrielessStateDB {
bc.sf, err = factory.NewStateDB(cfg, factory.DefaultStateDBOption())
} else {
bc.sf, err = factory.NewFactory(cfg, factory.DefaultTrieOption())
}
if err != nil {
return errors.Wrapf(err, "Failed to create state factory")
}
return nil
}
}
// PrecreatedStateFactoryOption sets blockchain's state.Factory to sf
func PrecreatedStateFactoryOption(sf factory.Factory) Option {
return func(bc *blockchain, conf config.Config) error {
bc.sf = sf
return nil
}
}
// InMemStateFactoryOption sets blockchain's factory.Factory as in memory sf
func InMemStateFactoryOption() Option {
return func(bc *blockchain, cfg config.Config) error {
sf, err := factory.NewFactory(cfg, factory.InMemTrieOption())
if err != nil {
return errors.Wrapf(err, "Failed to create state factory")
}
bc.sf = sf
return nil
}
}
// PrecreatedDaoOption sets blockchain's dao
func PrecreatedDaoOption(dao *blockDAO) Option {
return func(bc *blockchain, conf config.Config) error {
bc.dao = dao
return nil
}
}
// BoltDBDaoOption sets blockchain's dao with BoltDB from config.Chain.ChainDBPath
func BoltDBDaoOption() Option {
return func(bc *blockchain, cfg config.Config) error {
cfg.DB.DbPath = cfg.Chain.ChainDBPath // TODO: remove this after moving TrieDBPath from cfg.Chain to cfg.DB
_, gateway := cfg.Plugins[config.GatewayPlugin]
bc.dao = newBlockDAO(
db.NewOnDiskDB(cfg.DB),
gateway && !cfg.Chain.EnableAsyncIndexWrite,
cfg.Chain.CompressBlock,
cfg.Chain.MaxCacheSize,
)
return nil
}
}
// InMemDaoOption sets blockchain's dao with MemKVStore
func InMemDaoOption() Option {
return func(bc *blockchain, cfg config.Config) error {
_, gateway := cfg.Plugins[config.GatewayPlugin]
bc.dao = newBlockDAO(
db.NewMemKVStore(),
gateway && !cfg.Chain.EnableAsyncIndexWrite,
cfg.Chain.CompressBlock,
cfg.Chain.MaxCacheSize,
)
return nil
}
}
// ClockOption overrides the default clock
func ClockOption(clk clock.Clock) Option {
return func(bc *blockchain, conf config.Config) error {
bc.clk = clk
return nil
}
}
// RegistryOption sets the blockchain with the protocol registry
func RegistryOption(registry *protocol.Registry) Option {
return func(bc *blockchain, conf config.Config) error {
bc.registry = registry
return nil
}
}
// NewBlockchain creates a new blockchain and DB instance
func NewBlockchain(cfg config.Config, opts ...Option) Blockchain {
// create the Blockchain
chain := &blockchain{
config: cfg,
clk: clock.New(),
}
for _, opt := range opts {
if err := opt(chain, cfg); err != nil {
log.S().Panicf("Failed to execute blockchain creation option %p: %v", opt, err)
}
}
timerFactory, err := prometheustimer.New(
"iotex_blockchain_perf",
"Performance of blockchain module",
[]string{"topic", "chainID"},
[]string{"default", strconv.FormatUint(uint64(cfg.Chain.ID), 10)},
)
if err != nil {
log.L().Panic("Failed to generate prometheus timer factory.", zap.Error(err))
}
chain.timerFactory = timerFactory
// Set block validator
if err != nil {
log.L().Panic("Failed to get block producer address.", zap.Error(err))
}
chain.validator = &validator{sf: chain.sf, validatorAddr: cfg.ProducerAddress().String()}
if chain.dao != nil {
chain.lifecycle.Add(chain.dao)
}
if chain.sf != nil {
chain.lifecycle.Add(chain.sf)
}
return chain
}
func (bc *blockchain) ChainID() uint32 {
return atomic.LoadUint32(&bc.config.Chain.ID)
}
func (bc *blockchain) ChainAddress() string {
return bc.config.Chain.Address
}
// Start starts the blockchain
func (bc *blockchain) Start(ctx context.Context) (err error) {
bc.mu.Lock()
defer bc.mu.Unlock()
if err = bc.lifecycle.OnStart(ctx); err != nil {
return err
}
// get blockchain tip height
if bc.tipHeight, err = bc.dao.getBlockchainHeight(); err != nil {
return err
}
if bc.tipHeight == 0 {
return bc.startEmptyBlockchain()
}
// get blockchain tip hash
if bc.tipHash, err = bc.dao.getBlockHash(bc.tipHeight); err != nil {
return err
}
return bc.startExistingBlockchain()
}
// Stop stops the blockchain.
func (bc *blockchain) Stop(ctx context.Context) error {
bc.mu.Lock()
defer bc.mu.Unlock()
return bc.lifecycle.OnStop(ctx)
}
// Balance returns balance of address
func (bc *blockchain) Balance(addr string) (*big.Int, error) {
return bc.sf.Balance(addr)
}
// Nonce returns the nonce if the account exists
func (bc *blockchain) Nonce(addr string) (uint64, error) {
return bc.sf.Nonce(addr)
}
// CandidatesByHeight returns the candidate list by a given height
func (bc *blockchain) CandidatesByHeight(height uint64) ([]*state.Candidate, error) {
return bc.candidatesByHeight(height)
}
// ProductivityByEpoch returns the map of the number of blocks produced per delegate in an epoch
func (bc *blockchain) ProductivityByEpoch(epochNum uint64) (uint64, map[string]uint64, error) {
p, ok := bc.registry.Find(rolldpos.ProtocolID)
if !ok {
return 0, nil, errors.New("rolldpos protocol is not registered")
}
rp, ok := p.(*rolldpos.Protocol)
if !ok {
return 0, nil, errors.New("fail to cast rolldpos protocol")
}
var isCurrentEpoch bool
currentEpochNum := rp.GetEpochNum(bc.tipHeight)
if epochNum > currentEpochNum {
return 0, nil, errors.New("epoch number is larger than current epoch number")
}
if epochNum == currentEpochNum {
isCurrentEpoch = true
}
epochStartHeight := rp.GetEpochHeight(epochNum)
var epochEndHeight uint64
if isCurrentEpoch {
epochEndHeight = bc.tipHeight
} else {
epochEndHeight = rp.GetEpochLastBlockHeight(epochNum)
}
numBlks := epochEndHeight - epochStartHeight + 1
p, ok = bc.registry.Find(poll.ProtocolID)
if !ok {
return 0, nil, errors.New("poll protocol is not registered")
}
ctx := protocol.WithRunActionsCtx(context.Background(), protocol.RunActionsCtx{
BlockHeight: bc.tipHeight,
Registry: bc.registry,
})
ws, err := bc.sf.NewWorkingSet()
if err != nil {
return 0, nil, err
}
state, err := p.ReadState(ctx, ws, []byte("CommitteeBlockProducersByHeight"), byteutil.Uint64ToBytes(epochStartHeight))
if err != nil {
return 0, nil, status.Error(codes.NotFound, err.Error())
}
var committeeBlockProducers pollpb.BlockProducerList
if err := proto.Unmarshal(state, &committeeBlockProducers); err != nil {
return 0, nil, status.Error(codes.Internal, err.Error())
}
produce := make(map[string]uint64)
for _, bp := range committeeBlockProducers.BlockProducers {
produce[bp] = 0
}
for i := uint64(0); i < numBlks; i++ {
blk, err := bc.getBlockByHeight(epochStartHeight + i)
if err != nil {
return 0, nil, err
}
produce[blk.ProducerAddress()]++
}
return numBlks, produce, nil
}
// GetHeightByHash returns block's height by hash
func (bc *blockchain) GetHeightByHash(h hash.Hash256) (uint64, error) {
return bc.dao.getBlockHeight(h)
}
// GetHashByHeight returns block's hash by height
func (bc *blockchain) GetHashByHeight(height uint64) (hash.Hash256, error) {
return bc.dao.getBlockHash(height)
}
// GetBlockByHeight returns block from the blockchain hash by height
func (bc *blockchain) GetBlockByHeight(height uint64) (*block.Block, error) {
blk, err := bc.getBlockByHeight(height)
if blk == nil || err != nil {
return blk, err
}
blk.HeaderLogger(log.L()).Debug("Get block.")
return blk, err
}
// GetBlockByHash returns block from the blockchain hash by hash
func (bc *blockchain) GetBlockByHash(h hash.Hash256) (*block.Block, error) {
return bc.dao.getBlock(h)
}
// GetTotalActions returns the total number of actions
func (bc *blockchain) GetTotalActions() (uint64, error) {
return bc.dao.getTotalActions()
}
// GetReceiptByActionHash returns the receipt by action hash
func (bc *blockchain) GetReceiptByActionHash(h hash.Hash256) (*action.Receipt, error) {
return bc.dao.getReceiptByActionHash(h)
}
// GetActionsFromAddress returns actions from address
func (bc *blockchain) GetActionsFromAddress(addrStr string) ([]hash.Hash256, error) {
addr, err := address.FromString(addrStr)
if err != nil {
return nil, err
}
return getActionsBySenderAddress(bc.dao.kvstore, hash.BytesToHash160(addr.Bytes()))
}
// GetActionToAddress returns action to address
func (bc *blockchain) GetActionsToAddress(addrStr string) ([]hash.Hash256, error) {
addr, err := address.FromString(addrStr)
if err != nil {
return nil, err
}
return getActionsByRecipientAddress(bc.dao.kvstore, hash.BytesToHash160(addr.Bytes()))
}
func (bc *blockchain) getActionByActionHashHelper(h hash.Hash256) (hash.Hash256, error) {
return getBlockHashByActionHash(bc.dao.kvstore, h)
}
// GetActionByActionHash returns action by action hash
func (bc *blockchain) GetActionByActionHash(h hash.Hash256) (action.SealedEnvelope, error) {
blkHash, err := bc.getActionByActionHashHelper(h)
if err != nil {
return action.SealedEnvelope{}, err
}
blk, err := bc.dao.getBlock(blkHash)
if err != nil {
return action.SealedEnvelope{}, err
}
for _, act := range blk.Actions {
if act.Hash() == h {
return act, nil
}
}
return action.SealedEnvelope{}, errors.Errorf("block %x does not have transfer %x", blkHash, h)
}
// GetBlockHashByActionHash returns Block hash by action hash
func (bc *blockchain) GetBlockHashByActionHash(h hash.Hash256) (hash.Hash256, error) {
return getBlockHashByActionHash(bc.dao.kvstore, h)
}
// GetFactory returns the state factory
func (bc *blockchain) GetFactory() factory.Factory {
return bc.sf
}
// TipHash returns tip block's hash
func (bc *blockchain) TipHash() hash.Hash256 {
bc.mu.RLock()
defer bc.mu.RUnlock()
return bc.tipHash
}
// TipHeight returns tip block's height
func (bc *blockchain) TipHeight() uint64 {
return atomic.LoadUint64(&bc.tipHeight)
}
// ValidateBlock validates a new block before adding it to the blockchain
func (bc *blockchain) ValidateBlock(blk *block.Block) error {
bc.mu.RLock()
defer bc.mu.RUnlock()
timer := bc.timerFactory.NewTimer("ValidateBlock")
defer timer.End()
return bc.validateBlock(blk)
}
func (bc *blockchain) MintNewBlock(
actionMap map[string][]action.SealedEnvelope,
timestamp time.Time,
) (*block.Block, error) {
bc.mu.RLock()
defer bc.mu.RUnlock()
mintNewBlockTimer := bc.timerFactory.NewTimer("MintNewBlock")
defer mintNewBlockTimer.End()
newblockHeight := bc.tipHeight + 1
// run execution and update state trie root hash
ws, err := bc.sf.NewWorkingSet()
if err != nil {
return nil, errors.Wrap(err, "Failed to obtain working set from state factory")
}
gasLimitForContext := bc.config.Genesis.BlockGasLimit
ctx := protocol.WithRunActionsCtx(context.Background(),
protocol.RunActionsCtx{
BlockHeight: newblockHeight,
BlockTimeStamp: timestamp,
Producer: bc.config.ProducerAddress(),
GasLimit: gasLimitForContext,
ActionGasLimit: bc.config.Genesis.ActionGasLimit,
Registry: bc.registry,
})
_, rc, actions, err := bc.pickAndRunActions(ctx, actionMap, ws)
if err != nil {
return nil, errors.Wrapf(err, "Failed to update state changes in new block %d", newblockHeight)
}
blockMtc.WithLabelValues("numActions").Set(float64(len(actions)))
blockMtc.WithLabelValues("gasConsumed").Set(float64(bc.config.Genesis.BlockGasLimit - gasLimitForContext))
sk := bc.config.ProducerPrivateKey()
ra := block.NewRunnableActionsBuilder().
SetHeight(newblockHeight).
SetTimeStamp(timestamp).
AddActions(actions...).
Build(sk.PublicKey())
prevBlkHash := bc.tipHash
// The first block's previous block hash is pointing to the digest of genesis config. This is to guarantee all nodes
// could verify that they start from the same genesis
if newblockHeight == 1 {
prevBlkHash = bc.config.Genesis.Hash()
}
blk, err := block.NewBuilder(ra).
SetPrevBlockHash(prevBlkHash).
SetDeltaStateDigest(ws.Digest()).
SetReceipts(rc).
SetReceiptRoot(calculateReceiptRoot(rc)).
SignAndBuild(sk)
if err != nil {
return nil, errors.Wrapf(err, "failed to create block")
}
blk.WorkingSet = ws
return &blk, nil
}
// CommitBlock validates and appends a block to the chain
func (bc *blockchain) CommitBlock(blk *block.Block) error {
bc.mu.Lock()
defer bc.mu.Unlock()
timer := bc.timerFactory.NewTimer("CommitBlock")
defer timer.End()
return bc.commitBlock(blk)
}
// StateByAddr returns the account of an address
func (bc *blockchain) StateByAddr(address string) (*state.Account, error) {
if bc.sf != nil {
s, err := bc.sf.AccountState(address)
if err != nil {
log.L().Warn("Failed to get account.", zap.String("address", address), zap.Error(err))
return nil, errors.New("account does not exist")
}
return s, nil
}
return nil, errors.New("state factory is nil")
}
// SetValidator sets the current validator object
func (bc *blockchain) SetValidator(val Validator) {
bc.mu.Lock()
defer bc.mu.Unlock()
bc.validator = val
}
// Validator gets the current validator object
func (bc *blockchain) Validator() Validator {
bc.mu.RLock()
defer bc.mu.RUnlock()
return bc.validator
}
func (bc *blockchain) AddSubscriber(s BlockCreationSubscriber) error {
bc.mu.Lock()
defer bc.mu.Unlock()
log.L().Info("Add a subscriber.")
if s == nil {
return errors.New("subscriber could not be nil")
}
bc.blocklistener = append(bc.blocklistener, s)
return nil
}
func (bc *blockchain) RemoveSubscriber(s BlockCreationSubscriber) error {
bc.mu.Lock()
defer bc.mu.Unlock()
for i, sub := range bc.blocklistener {
if sub == s {
bc.blocklistener = append(bc.blocklistener[:i], bc.blocklistener[i+1:]...)
log.L().Info("Successfully unsubscribe block creation.")
return nil
}
}
return errors.New("cannot find subscription")
}
//======================================
// internal functions
//=====================================
// ExecuteContractRead runs a read-only smart contract operation, this is done off the network since it does not
// cause any state change
func (bc *blockchain) ExecuteContractRead(caller address.Address, ex *action.Execution) (*action.Receipt, error) {
// use latest block as carrier to run the offline execution
// the block itself is not used
h := bc.TipHeight()
blk, err := bc.GetBlockByHeight(h)
if err != nil {
return nil, errors.Wrap(err, "failed to get block in ExecuteContractRead")
}
ws, err := bc.sf.NewWorkingSet()
if err != nil {
return nil, errors.Wrap(err, "failed to obtain working set from state factory")
}
producer, err := address.FromString(blk.ProducerAddress())
if err != nil {
return nil, err
}
gasLimit := bc.config.Genesis.BlockGasLimit
ctx := protocol.WithRunActionsCtx(context.Background(), protocol.RunActionsCtx{
BlockHeight: blk.Height(),
BlockTimeStamp: blk.Timestamp(),
Producer: producer,
Caller: caller,
GasLimit: gasLimit,
ActionGasLimit: bc.config.Genesis.ActionGasLimit,
GasPrice: big.NewInt(0),
IntrinsicGas: 0,
})
return evm.ExecuteContract(
ctx,
ws,
ex,
bc,
)
}
// CreateState adds a new account with initial balance to the factory
func (bc *blockchain) CreateState(addr string, init *big.Int) (*state.Account, error) {
if bc.sf == nil {
return nil, errors.New("empty state factory")
}
ws, err := bc.sf.NewWorkingSet()
if err != nil {
return nil, errors.Wrapf(err, "failed to create clean working set")
}
account, err := accountutil.LoadOrCreateAccount(ws, addr, init)
if err != nil {
return nil, errors.Wrapf(err, "failed to create new account %s", addr)
}
gasLimit := bc.config.Genesis.BlockGasLimit
callerAddr, err := address.FromString(addr)
if err != nil {
return nil, err
}
ctx := protocol.WithRunActionsCtx(context.Background(),
protocol.RunActionsCtx{
GasLimit: gasLimit,
ActionGasLimit: bc.config.Genesis.ActionGasLimit,
Caller: callerAddr,
ActionHash: hash.ZeroHash256,
Nonce: 0,
Registry: bc.registry,
})
if _, err = ws.RunActions(ctx, 0, nil); err != nil {
return nil, errors.Wrap(err, "failed to run the account creation")
}
if err = bc.sf.Commit(ws); err != nil {
return nil, errors.Wrap(err, "failed to commit the account creation")
}
return account, nil
}
// RecoverChainAndState recovers the chain to target height and refresh state db if necessary
func (bc *blockchain) RecoverChainAndState(targetHeight uint64) error {
var buildStateFromScratch bool
stateHeight, err := bc.sf.Height()
if err != nil {
buildStateFromScratch = true
}
if targetHeight > 0 {
if err := bc.recoverToHeight(targetHeight); err != nil {
return errors.Wrapf(err, "failed to recover blockchain to target height %d", targetHeight)
}
if stateHeight > bc.tipHeight {
buildStateFromScratch = true
}
}
if buildStateFromScratch {
return bc.refreshStateDB()
}
return nil
}
func (bc *blockchain) GenesisTimestamp() int64 {
return bc.config.Genesis.Timestamp
}
//======================================
// private functions
//=====================================
func (bc *blockchain) protocol(id string) (protocol.Protocol, bool) {
if bc.registry == nil {
return nil, false
}
return bc.registry.Find(id)
}
func (bc *blockchain) mustGetRollDPoSProtocol() *rolldpos.Protocol {
p, ok := bc.protocol(rolldpos.ProtocolID)
if !ok {
log.L().Panic("protocol rolldpos has not been registered")
}
rp, ok := p.(*rolldpos.Protocol)
if !ok {
log.L().Panic("failed to cast to rolldpos protocol")
}
return rp
}
func (bc *blockchain) candidatesByHeight(height uint64) (state.CandidateList, error) {
if bc.config.Genesis.EnableGravityChainVoting {
rp := bc.mustGetRollDPoSProtocol()
return bc.sf.CandidatesByHeight(rp.GetEpochHeight(rp.GetEpochNum(height)))
}
for {
candidates, err := bc.sf.CandidatesByHeight(height)
if err == nil {
return candidates, nil
}
if height == 0 {
return nil, err
}
height--
}
}
func (bc *blockchain) getBlockByHeight(height uint64) (*block.Block, error) {
hash, err := bc.dao.getBlockHash(height)
if err != nil {
return nil, err
}
return bc.dao.getBlock(hash)
}
func (bc *blockchain) startEmptyBlockchain() error {
var ws factory.WorkingSet
var err error
if ws, err = bc.sf.NewWorkingSet(); err != nil {
return errors.Wrap(err, "failed to obtain working set from state factory")
}
if !bc.config.Chain.EmptyGenesis {
// Initialize the states before any actions happen on the blockchain
if err := bc.createGenesisStates(ws); err != nil {
return err
}
_ = ws.UpdateBlockLevelInfo(0)
}
// add Genesis states
if err := bc.sf.Commit(ws); err != nil {
return errors.Wrap(err, "failed to commit Genesis states")
}
return nil
}
func (bc *blockchain) startExistingBlockchain() error {
if bc.sf == nil {
return errors.New("statefactory cannot be nil")
}
stateHeight, err := bc.sf.Height()
if err != nil {
return err
}
if stateHeight > bc.tipHeight {
return errors.New("factory is higher than blockchain")
}
for i := stateHeight + 1; i <= bc.tipHeight; i++ {
blk, err := bc.getBlockByHeight(i)
if err != nil {
return err
}
ws, err := bc.sf.NewWorkingSet()
if err != nil {
return errors.Wrap(err, "failed to obtain working set from state factory")
}
if _, err := bc.runActions(blk.RunnableActions(), ws); err != nil {
return err
}
if err := bc.sf.Commit(ws); err != nil {
return err
}
}
stateHeight, err = bc.sf.Height()
if err != nil {
return errors.Wrap(err, "failed to get factory's height")
}
log.L().Info("Restarting blockchain.",
zap.Uint64("chainHeight",
bc.tipHeight),
zap.Uint64("factoryHeight", stateHeight))
return nil
}
func (bc *blockchain) validateBlock(blk *block.Block) error {
validateTimer := bc.timerFactory.NewTimer("validate")
prevBlkHash := bc.tipHash
if blk.Height() == 1 {
prevBlkHash = bc.config.Genesis.Hash()
}
err := bc.validator.Validate(blk, bc.tipHeight, prevBlkHash)
validateTimer.End()
if err != nil {
return errors.Wrapf(err, "error when validating block %d", blk.Height())
}
// run actions and update state factory
ws, err := bc.sf.NewWorkingSet()
if err != nil {
return errors.Wrap(err, "Failed to obtain working set from state factory")
}
runTimer := bc.timerFactory.NewTimer("runActions")
receipts, err := bc.runActions(blk.RunnableActions(), ws)
runTimer.End()
if err != nil {
log.L().Panic("Failed to update state.", zap.Uint64("tipHeight", bc.tipHeight), zap.Error(err))
}
if err = blk.VerifyDeltaStateDigest(ws.Digest()); err != nil {
return err
}
if err = blk.VerifyReceiptRoot(calculateReceiptRoot(receipts)); err != nil {
return errors.Wrap(err, "Failed to verify receipt root")
}
blk.Receipts = receipts
// attach working set to be committed to state factory
blk.WorkingSet = ws
return nil
}
// commitBlock commits a block to the chain
func (bc *blockchain) commitBlock(blk *block.Block) error {
// Check if it is already exists, and return earlier
blkHash, err := bc.dao.getBlockHash(blk.Height())
if blkHash != hash.ZeroHash256 {
log.L().Debug("Block already exists.", zap.Uint64("height", blk.Height()))
return nil
}
// If it's a ready db io error, return earlier with the error
if errors.Cause(err) != db.ErrNotExist {
return err
}
// write block into DB
putTimer := bc.timerFactory.NewTimer("putBlock")
err = bc.dao.putBlock(blk)
putTimer.End()
if err != nil {
return err
}
// update tip hash and height
atomic.StoreUint64(&bc.tipHeight, blk.Height())
bc.tipHash = blk.HashBlock()
if bc.sf != nil {
sfTimer := bc.timerFactory.NewTimer("sf.Commit")
err := bc.sf.Commit(blk.WorkingSet)
sfTimer.End()
// detach working set so it can be freed by GC
blk.WorkingSet = nil
if err != nil {
log.L().Panic("Error when committing states.", zap.Error(err))
}
// write smart contract receipt into DB
receiptTimer := bc.timerFactory.NewTimer("putReceipt")
err = bc.dao.putReceipts(blk.Height(), blk.Receipts)
receiptTimer.End()
if err != nil {
return errors.Wrapf(err, "failed to put smart contract receipts into DB on height %d", blk.Height())
}
}
blk.HeaderLogger(log.L()).Info("Committed a block.", log.Hex("tipHash", bc.tipHash[:]))
// emit block to all block subscribers
bc.emitToSubscribers(blk)
return nil
}
func (bc *blockchain) runActions(
acts block.RunnableActions,
ws factory.WorkingSet,
) ([]*action.Receipt, error) {
if bc.sf == nil {
return nil, errors.New("statefactory cannot be nil")
}
gasLimit := bc.config.Genesis.BlockGasLimit
// update state factory
producer, err := address.FromBytes(acts.BlockProducerPubKey().Hash())
if err != nil {
return nil, err
}
ctx := protocol.WithRunActionsCtx(context.Background(),
protocol.RunActionsCtx{
BlockHeight: acts.BlockHeight(),
BlockTimeStamp: acts.BlockTimeStamp(),
Producer: producer,
GasLimit: gasLimit,
ActionGasLimit: bc.config.Genesis.ActionGasLimit,
Registry: bc.registry,
})
return ws.RunActions(ctx, acts.BlockHeight(), acts.Actions())
}
func (bc *blockchain) pickAndRunActions(ctx context.Context, actionMap map[string][]action.SealedEnvelope,
ws factory.WorkingSet) (hash.Hash256, []*action.Receipt, []action.SealedEnvelope, error) {
if bc.sf == nil {
return hash.ZeroHash256, nil, nil, errors.New("statefactory cannot be nil")
}
receipts := make([]*action.Receipt, 0)
executedActions := make([]action.SealedEnvelope, 0)
raCtx := protocol.MustGetRunActionsCtx(ctx)
// initial action iterator
actionIterator := actioniterator.NewActionIterator(actionMap)
for {
nextAction, ok := actionIterator.Next()
if !ok {
break
}
receipt, err := ws.RunAction(raCtx, nextAction)
if err != nil {
if errors.Cause(err) == action.ErrHitGasLimit {
// hit block gas limit, we should not process actions belong to this user anymore since we
// need monotonically increasing nounce. But we can continue processing other actions
// that belong other users
actionIterator.PopAccount()
continue
}
return hash.ZeroHash256, nil, nil, errors.Wrapf(err, "Failed to update state changes for selp %x", nextAction.Hash())