-
Notifications
You must be signed in to change notification settings - Fork 50
/
ledger_store.go
996 lines (902 loc) · 32.2 KB
/
ledger_store.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
/*
* Copyright (C) 2021 The poly network Authors
* This file is part of The poly network library.
*
* The poly network is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The poly network is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the poly network. If not, see <http://www.gnu.org/licenses/>.
*/
//Storage of ledger
package ledgerstore
import (
"fmt"
"os"
"sort"
"strings"
"sync"
"time"
"github.com/polynetwork/poly/core/payload"
"github.com/polynetwork/poly/core/states"
"github.com/polynetwork/poly/native"
"github.com/ontio/ontology-crypto/keypair"
"github.com/polynetwork/poly/common"
"github.com/polynetwork/poly/common/config"
"github.com/polynetwork/poly/common/log"
vconfig "github.com/polynetwork/poly/consensus/vbft/config"
"github.com/polynetwork/poly/core/signature"
"github.com/polynetwork/poly/core/store"
scom "github.com/polynetwork/poly/core/store/common"
"github.com/polynetwork/poly/core/store/overlaydb"
"github.com/polynetwork/poly/core/types"
"github.com/polynetwork/poly/events"
"github.com/polynetwork/poly/events/message"
"github.com/polynetwork/poly/merkle"
"github.com/polynetwork/poly/native/event"
cstates "github.com/polynetwork/poly/native/states"
sstate "github.com/polynetwork/poly/native/states"
"github.com/polynetwork/poly/native/storage"
)
const (
SYSTEM_VERSION = byte(1) //Version of ledger store
HEADER_INDEX_BATCH_SIZE = uint32(2000) //Bath size of saving header index
)
var (
//Storage save path.
DBDirEvent = "ledgerevent"
DBDirBlock = "block"
DBDirState = "states"
MerkleTreeStorePath = "merkle_tree.db"
)
//LedgerStoreImp is main store struct fo ledger
type LedgerStoreImp struct {
blockStore *BlockStore //BlockStore for saving block & transaction data
stateStore *StateStore //StateStore for saving state data, like balance, smart contract execution result, and so on.
eventStore *EventStore //EventStore for saving log those gen after smart contract executed.
storedIndexCount uint32 //record the count of have saved block index
currBlockHeight uint32 //Current block height
currBlockHash common.Uint256 //Current block hash
headerCache map[common.Uint256]*types.Header //BlockHash => Header
headerIndex map[uint32]common.Uint256 //Header index, Mapping header height => block hash
savingBlockSemaphore chan bool
vbftPeerInfoheader map[string]uint32 //pubInfo save pubkey,peerindex
vbftPeerInfoblock map[string]uint32 //pubInfo save pubkey,peerindex
lock sync.RWMutex
}
//NewLedgerStore return LedgerStoreImp instance
func NewLedgerStore(dataDir string) (*LedgerStoreImp, error) {
ledgerStore := &LedgerStoreImp{
headerIndex: make(map[uint32]common.Uint256),
headerCache: make(map[common.Uint256]*types.Header, 0),
vbftPeerInfoheader: make(map[string]uint32),
vbftPeerInfoblock: make(map[string]uint32),
savingBlockSemaphore: make(chan bool, 1),
}
blockStore, err := NewBlockStore(fmt.Sprintf("%s%s%s", dataDir, string(os.PathSeparator), DBDirBlock), true)
if err != nil {
return nil, fmt.Errorf("NewBlockStore error %s", err)
}
ledgerStore.blockStore = blockStore
dbPath := fmt.Sprintf("%s%s%s", dataDir, string(os.PathSeparator), DBDirState)
merklePath := fmt.Sprintf("%s%s%s", dataDir, string(os.PathSeparator), MerkleTreeStorePath)
stateStore, err := NewStateStore(dbPath, merklePath)
if err != nil {
return nil, fmt.Errorf("NewStateStore error %s", err)
}
ledgerStore.stateStore = stateStore
eventState, err := NewEventStore(fmt.Sprintf("%s%s%s", dataDir, string(os.PathSeparator), DBDirEvent))
if err != nil {
return nil, fmt.Errorf("NewEventStore error %s", err)
}
ledgerStore.eventStore = eventState
return ledgerStore, nil
}
//InitLedgerStoreWithGenesisBlock init the ledger store with genesis block. It's the first operation after NewLedgerStore.
func (this *LedgerStoreImp) InitLedgerStoreWithGenesisBlock(genesisBlock *types.Block, defaultBookkeeper []keypair.PublicKey) error {
hasInit, err := this.hasAlreadyInitGenesisBlock()
if err != nil {
return fmt.Errorf("hasAlreadyInit error %s", err)
}
if !hasInit {
err = this.blockStore.ClearAll()
if err != nil {
return fmt.Errorf("blockStore.ClearAll error %s", err)
}
err = this.stateStore.ClearAll()
if err != nil {
return fmt.Errorf("stateStore.ClearAll error %s", err)
}
err = this.eventStore.ClearAll()
if err != nil {
return fmt.Errorf("eventStore.ClearAll error %s", err)
}
defaultBookkeeper = keypair.SortPublicKeys(defaultBookkeeper)
bookkeeperState := &states.BookkeeperState{
CurrBookkeeper: defaultBookkeeper,
NextBookkeeper: defaultBookkeeper,
}
err = this.stateStore.SaveBookkeeperState(bookkeeperState)
if err != nil {
return fmt.Errorf("SaveBookkeeperState error %s", err)
}
result, err := this.executeBlock(genesisBlock)
if err != nil {
return err
}
err = this.submitBlock(genesisBlock, result)
if err != nil {
return fmt.Errorf("save genesis block error %s", err)
}
err = this.initGenesisBlock()
if err != nil {
return fmt.Errorf("init error %s", err)
}
genHash := genesisBlock.Hash()
log.Infof("GenesisBlock init success. GenesisBlock hash:%s\n", genHash.ToHexString())
} else {
genesisHash := genesisBlock.Hash()
exist, err := this.blockStore.ContainBlock(genesisHash)
if err != nil {
return fmt.Errorf("HashBlockExist error %s", err)
}
if !exist {
return fmt.Errorf("GenesisBlock arenot init correctly")
}
err = this.init()
if err != nil {
return fmt.Errorf("init error %s", err)
}
}
//load vbft peerInfo
consensusType := strings.ToLower(config.DefConfig.Genesis.ConsensusType)
if consensusType == "vbft" {
header, err := this.GetHeaderByHash(this.currBlockHash)
if err != nil {
return err
}
blkInfo, err := vconfig.VbftBlock(header)
if err != nil {
return err
}
var cfg *vconfig.ChainConfig
if blkInfo.NewChainConfig != nil {
cfg = blkInfo.NewChainConfig
} else {
cfgHeader, err := this.GetHeaderByHeight(blkInfo.LastConfigBlockNum)
if err != nil {
return err
}
Info, err := vconfig.VbftBlock(cfgHeader)
if err != nil {
return err
}
if Info.NewChainConfig == nil {
return fmt.Errorf("getNewChainConfig error block num:%d", blkInfo.LastConfigBlockNum)
}
cfg = Info.NewChainConfig
}
this.lock.Lock()
this.vbftPeerInfoheader = make(map[string]uint32)
this.vbftPeerInfoblock = make(map[string]uint32)
for _, p := range cfg.Peers {
this.vbftPeerInfoheader[p.ID] = p.Index
this.vbftPeerInfoblock[p.ID] = p.Index
}
this.lock.Unlock()
}
return err
}
func (this *LedgerStoreImp) hasAlreadyInitGenesisBlock() (bool, error) {
version, err := this.blockStore.GetVersion()
if err != nil && err != scom.ErrNotFound {
return false, fmt.Errorf("GetVersion error %s", err)
}
return version == SYSTEM_VERSION, nil
}
func (this *LedgerStoreImp) initGenesisBlock() error {
return this.blockStore.SaveVersion(SYSTEM_VERSION)
}
func (this *LedgerStoreImp) init() error {
err := this.loadCurrentBlock()
if err != nil {
return fmt.Errorf("loadCurrentBlock error %s", err)
}
err = this.loadHeaderIndexList()
if err != nil {
return fmt.Errorf("loadHeaderIndexList error %s", err)
}
err = this.recoverStore()
if err != nil {
return fmt.Errorf("recoverStore error %s", err)
}
return nil
}
func (this *LedgerStoreImp) loadCurrentBlock() error {
currentBlockHash, currentBlockHeight, err := this.blockStore.GetCurrentBlock()
if err != nil {
return fmt.Errorf("LoadCurrentBlock error %s", err)
}
log.Infof("InitCurrentBlock currentBlockHash %s currentBlockHeight %d", currentBlockHash.ToHexString(), currentBlockHeight)
this.currBlockHash = currentBlockHash
this.currBlockHeight = currentBlockHeight
return nil
}
func (this *LedgerStoreImp) loadHeaderIndexList() error {
currBlockHeight := this.GetCurrentBlockHeight()
headerIndex, err := this.blockStore.GetHeaderIndexList()
if err != nil {
return fmt.Errorf("LoadHeaderIndexList error %s", err)
}
storeIndexCount := uint32(len(headerIndex))
this.headerIndex = headerIndex
this.storedIndexCount = storeIndexCount
for i := storeIndexCount; i <= currBlockHeight; i++ {
height := i
blockHash, err := this.blockStore.GetBlockHash(height)
if err != nil {
return fmt.Errorf("LoadBlockHash height %d error %s", height, err)
}
if blockHash == common.UINT256_EMPTY {
return fmt.Errorf("LoadBlockHash height %d hash nil", height)
}
this.headerIndex[height] = blockHash
}
return nil
}
func (this *LedgerStoreImp) recoverStore() error {
blockHeight := this.GetCurrentBlockHeight()
_, stateHeight, err := this.stateStore.GetCurrentBlock()
if err != nil {
return fmt.Errorf("stateStore.GetCurrentBlock error %s", err)
}
for i := stateHeight; i < blockHeight; i++ {
blockHash, err := this.blockStore.GetBlockHash(i)
if err != nil {
return fmt.Errorf("blockStore.GetBlockHash height:%d error:%s", i, err)
}
block, err := this.blockStore.GetBlock(blockHash)
if err != nil {
return fmt.Errorf("blockStore.GetBlock height:%d error:%s", i, err)
}
this.eventStore.NewBatch()
this.stateStore.NewBatch()
result, err := this.executeBlock(block)
if err != nil {
return err
}
err = this.saveBlockToStateStore(block, result)
if err != nil {
return fmt.Errorf("save to state store height:%d error:%s", i, err)
}
err = this.saveBlockToEventStore(block)
if err != nil {
return fmt.Errorf("save to event store height:%d error:%s", i, err)
}
err = this.eventStore.CommitTo()
if err != nil {
return fmt.Errorf("eventStore.CommitTo height:%d error %s", i, err)
}
err = this.stateStore.CommitTo()
if err != nil {
return fmt.Errorf("stateStore.CommitTo height:%d error %s", i, err)
}
}
return nil
}
func (this *LedgerStoreImp) setHeaderIndex(height uint32, blockHash common.Uint256) {
this.lock.Lock()
defer this.lock.Unlock()
this.headerIndex[height] = blockHash
}
func (this *LedgerStoreImp) getHeaderIndex(height uint32) common.Uint256 {
this.lock.RLock()
defer this.lock.RUnlock()
blockHash, ok := this.headerIndex[height]
if !ok {
return common.Uint256{}
}
return blockHash
}
//GetCurrentHeaderHeight return the current header height.
//In block sync states, Header height is usually higher than block height that is has already committed to storage
func (this *LedgerStoreImp) GetCurrentHeaderHeight() uint32 {
this.lock.RLock()
defer this.lock.RUnlock()
size := len(this.headerIndex)
if size == 0 {
return 0
}
return uint32(size) - 1
}
//GetCurrentHeaderHash return the current header hash. The current header means the latest header.
func (this *LedgerStoreImp) GetCurrentHeaderHash() common.Uint256 {
this.lock.RLock()
defer this.lock.RUnlock()
size := len(this.headerIndex)
if size == 0 {
return common.Uint256{}
}
return this.headerIndex[uint32(size)-1]
}
func (this *LedgerStoreImp) setCurrentBlock(height uint32, blockHash common.Uint256) {
this.lock.Lock()
defer this.lock.Unlock()
this.currBlockHash = blockHash
this.currBlockHeight = height
return
}
//GetCurrentBlock return the current block height, and block hash.
//Current block means the latest block in store.
func (this *LedgerStoreImp) GetCurrentBlock() (uint32, common.Uint256) {
this.lock.RLock()
defer this.lock.RUnlock()
return this.currBlockHeight, this.currBlockHash
}
//GetCurrentBlockHash return the current block hash
func (this *LedgerStoreImp) GetCurrentBlockHash() common.Uint256 {
this.lock.RLock()
defer this.lock.RUnlock()
return this.currBlockHash
}
//GetCurrentBlockHeight return the current block height
func (this *LedgerStoreImp) GetCurrentBlockHeight() uint32 {
this.lock.RLock()
defer this.lock.RUnlock()
return this.currBlockHeight
}
func (this *LedgerStoreImp) addHeaderCache(header *types.Header) {
this.lock.Lock()
defer this.lock.Unlock()
this.headerCache[header.Hash()] = header
}
func (this *LedgerStoreImp) delHeaderCache(blockHash common.Uint256) {
this.lock.Lock()
defer this.lock.Unlock()
delete(this.headerCache, blockHash)
}
func (this *LedgerStoreImp) getHeaderCache(blockHash common.Uint256) *types.Header {
this.lock.RLock()
defer this.lock.RUnlock()
header, ok := this.headerCache[blockHash]
if !ok {
return nil
}
return header
}
func (this *LedgerStoreImp) verifyHeader(header *types.Header, vbftPeerInfo map[string]uint32) (map[string]uint32, error) {
if header.Height == 0 {
return vbftPeerInfo, nil
}
var prevHeader *types.Header
prevHeaderHash := header.PrevBlockHash
prevHeader, err := this.GetHeaderByHash(prevHeaderHash)
if err != nil && err != scom.ErrNotFound {
return vbftPeerInfo, fmt.Errorf("get prev header error %s", err)
}
if prevHeader == nil {
return vbftPeerInfo, fmt.Errorf("cannot find pre header by blockHash %s", prevHeaderHash.ToHexString())
}
if prevHeader.Height+1 != header.Height {
return vbftPeerInfo, fmt.Errorf("block height is incorrect")
}
if prevHeader.Timestamp >= header.Timestamp {
return vbftPeerInfo, fmt.Errorf("block timestamp is incorrect")
}
consensusType := strings.ToLower(config.DefConfig.Genesis.ConsensusType)
if consensusType == "vbft" {
//check bookkeeppers
needFix := config.NETWORK_ID_MAIN_NET != config.DefConfig.P2PNode.NetworkId || this.GetCurrentHeaderHeight() <= 20000000
m := len(vbftPeerInfo) - (len(vbftPeerInfo)-1)/3
if needFix {
m = len(vbftPeerInfo) - (len(vbftPeerInfo)*6)/7
}
if len(header.Bookkeepers) < m {
return vbftPeerInfo, fmt.Errorf("header Bookkeepers %d more than 2/3 len vbftPeerInfo%d", len(header.Bookkeepers), len(vbftPeerInfo))
}
usedPubKey := make(map[string]bool)
for _, bookkeeper := range header.Bookkeepers {
pubkey := vconfig.PubkeyID(bookkeeper)
_, present := vbftPeerInfo[pubkey]
if !present || usedPubKey[pubkey] {
log.Errorf("invalid pubkey :%v,height:%d", pubkey, header.Height)
return vbftPeerInfo, fmt.Errorf("invalid pubkey :%v", pubkey)
}
usedPubKey[pubkey] = true
}
hash := header.Hash()
err = signature.VerifyMultiSignature(hash[:], header.Bookkeepers, m, header.SigData)
if err != nil {
log.Errorf("VerifyMultiSignature:%s,Bookkeepers:%d,pubkey:%d,heigh:%d", err, len(header.Bookkeepers), len(vbftPeerInfo), header.Height)
return vbftPeerInfo, err
}
blkInfo, err := vconfig.VbftBlock(header)
if err != nil {
return vbftPeerInfo, err
}
if blkInfo.NewChainConfig != nil {
peerInfo := make(map[string]uint32)
for _, p := range blkInfo.NewChainConfig.Peers {
peerInfo[p.ID] = p.Index
}
return peerInfo, nil
}
return vbftPeerInfo, nil
} else {
address, err := types.AddressFromBookkeepers(header.Bookkeepers)
if err != nil {
return vbftPeerInfo, err
}
if prevHeader.NextBookkeeper != address {
return vbftPeerInfo, fmt.Errorf("bookkeeper address error")
}
m := len(header.Bookkeepers) - (len(header.Bookkeepers)-1)/3
hash := header.Hash()
err = signature.VerifyMultiSignature(hash[:], header.Bookkeepers, m, header.SigData)
if err != nil {
return vbftPeerInfo, err
}
}
return vbftPeerInfo, nil
}
//AddHeader add header to cache, and add the mapping of block height to block hash. Using in block sync
func (this *LedgerStoreImp) AddHeader(header *types.Header) error {
nextHeaderHeight := this.GetCurrentHeaderHeight() + 1
if header.Height != nextHeaderHeight {
return fmt.Errorf("header height %d not equal next header height %d", header.Height, nextHeaderHeight)
}
var err error
this.vbftPeerInfoheader, err = this.verifyHeader(header, this.vbftPeerInfoheader)
if err != nil {
return fmt.Errorf("verifyHeader error %s", err)
}
this.addHeaderCache(header)
this.setHeaderIndex(header.Height, header.Hash())
return nil
}
//AddHeaders bath add header.
func (this *LedgerStoreImp) AddHeaders(headers []*types.Header) error {
sort.Slice(headers, func(i, j int) bool {
return headers[i].Height < headers[j].Height
})
var err error
for _, header := range headers {
err = this.AddHeader(header)
if err != nil {
return err
}
}
return nil
}
func (this *LedgerStoreImp) GetStateMerkleRoot(height uint32) (common.Uint256, error) {
return this.stateStore.GetStateMerkleRoot(height)
}
func (this *LedgerStoreImp) GetCrossStateRoot(height uint32) (common.Uint256, error) {
return this.stateStore.GetCrossStateRoot(height)
}
func (this *LedgerStoreImp) ExecuteBlock(block *types.Block) (result store.ExecuteResult, err error) {
this.getSavingBlockLock()
defer this.releaseSavingBlockLock()
currBlockHeight := this.GetCurrentBlockHeight()
blockHeight := block.Header.Height
if blockHeight <= currBlockHeight {
result.MerkleRoot, err = this.GetStateMerkleRoot(blockHeight)
return
}
nextBlockHeight := currBlockHeight + 1
if blockHeight != nextBlockHeight {
err = fmt.Errorf("block height %d not equal next block height %d", blockHeight, nextBlockHeight)
return
}
result, err = this.executeBlock(block)
return
}
func (this *LedgerStoreImp) SubmitBlock(block *types.Block, result store.ExecuteResult) error {
this.getSavingBlockLock()
defer this.releaseSavingBlockLock()
currBlockHeight := this.GetCurrentBlockHeight()
blockHeight := block.Header.Height
if blockHeight <= currBlockHeight {
return nil
}
nextBlockHeight := currBlockHeight + 1
if blockHeight != nextBlockHeight {
return fmt.Errorf("block height %d not equal next block height %d", blockHeight, nextBlockHeight)
}
var err error
this.vbftPeerInfoblock, err = this.verifyHeader(block.Header, this.vbftPeerInfoblock)
if err != nil {
return fmt.Errorf("verifyHeader error %s", err)
}
err = this.submitBlock(block, result)
if err != nil {
return fmt.Errorf("saveBlock error %s", err)
}
this.delHeaderCache(block.Hash())
return nil
}
//AddBlock add the block to store.
//When the block is not the next block, it will be cache. until the missing block arrived
func (this *LedgerStoreImp) AddBlock(block *types.Block, stateMerkleRoot common.Uint256) error {
currBlockHeight := this.GetCurrentBlockHeight()
blockHeight := block.Header.Height
if blockHeight <= currBlockHeight {
return nil
}
nextBlockHeight := currBlockHeight + 1
if blockHeight != nextBlockHeight {
return fmt.Errorf("block height %d not equal next block height %d", blockHeight, nextBlockHeight)
}
var err error
this.vbftPeerInfoblock, err = this.verifyHeader(block.Header, this.vbftPeerInfoblock)
if err != nil {
return fmt.Errorf("verifyHeader error %s", err)
}
err = this.saveBlock(block, stateMerkleRoot)
if err != nil {
return fmt.Errorf("saveBlock error %s", err)
}
this.delHeaderCache(block.Hash())
return nil
}
func (this *LedgerStoreImp) GetCrossStatesProof(height uint32, key []byte) ([]byte, error) {
hashes, err := this.stateStore.GetCrossStates(height)
if err != nil {
return nil, fmt.Errorf("GetCrossStates:%s", err)
}
state, err := this.stateStore.GetStorageValue(key)
if err != nil {
return nil, fmt.Errorf("GetStorageState key:%x", key)
}
path, err := merkle.MerkleLeafPath(state, hashes)
if err != nil {
return nil, err
}
return path, nil
}
func (this *LedgerStoreImp) saveBlockToBlockStore(block *types.Block) error {
blockHash := block.Hash()
blockHeight := block.Header.Height
this.setHeaderIndex(blockHeight, blockHash)
err := this.saveHeaderIndexList()
if err != nil {
return fmt.Errorf("saveHeaderIndexList error %s", err)
}
err = this.blockStore.SaveCurrentBlock(blockHeight, blockHash)
if err != nil {
return fmt.Errorf("SaveCurrentBlock error %s", err)
}
this.blockStore.SaveBlockHash(blockHeight, blockHash)
err = this.blockStore.SaveBlock(block)
if err != nil {
return fmt.Errorf("SaveBlock height %d hash %s error %s", blockHeight, blockHash.ToHexString(), err)
}
return nil
}
func (this *LedgerStoreImp) executeBlock(block *types.Block) (result store.ExecuteResult, err error) {
overlay := this.stateStore.NewOverlayDB()
cache := storage.NewCacheDB(overlay)
for _, tx := range block.Transactions {
cache.Reset()
notify, crossHashes, e := this.handleTransaction(overlay, cache, block, tx)
if e != nil {
err = e
return
}
result.Notify = append(result.Notify, notify)
result.CrossHashes = append(result.CrossHashes, crossHashes...)
}
if len(result.CrossHashes) != 0 {
result.CrossStatesRoot = merkle.TreeHasher{}.HashFullTreeWithLeafHash(result.CrossHashes)
} else {
result.CrossStatesRoot = common.UINT256_EMPTY
}
result.Hash = overlay.ChangeHash()
result.WriteSet = overlay.GetWriteSet()
result.MerkleRoot = this.stateStore.GetStateMerkleRootWithNewHash(result.Hash)
return
}
func (this *LedgerStoreImp) saveBlockToStateStore(block *types.Block, result store.ExecuteResult) error {
blockHash := block.Hash()
blockHeight := block.Header.Height
for _, notify := range result.Notify {
err := SaveNotify(this.eventStore, notify.TxHash, notify)
if err != nil {
return fmt.Errorf("SaveNotify error %s", err)
}
}
err := this.stateStore.AddStateMerkleTreeRoot(blockHeight, result.Hash)
if err != nil {
return fmt.Errorf("AddBlockMerkleTreeRoot error %s", err)
}
err = this.stateStore.AddBlockMerkleTreeRoot(block.Header.PrevBlockHash)
if err != nil {
return fmt.Errorf("AddBlockMerkleTreeRoot error %s", err)
}
err = this.stateStore.SaveCurrentBlock(blockHeight, blockHash)
if err != nil {
return fmt.Errorf("SaveCurrentBlock error %s", err)
}
err = this.stateStore.AddCrossStates(blockHeight, result.CrossHashes, result.CrossStatesRoot)
if err != nil {
return err
}
log.Debugf("the state transition hash of block %d is:%s", blockHeight, result.Hash.ToHexString())
result.WriteSet.ForEach(func(key, val []byte) {
if len(val) == 0 {
this.stateStore.BatchDeleteRawKey(key)
} else {
this.stateStore.BatchPutRawKeyVal(key, val)
}
})
return nil
}
func (this *LedgerStoreImp) saveBlockToEventStore(block *types.Block) error {
blockHash := block.Hash()
blockHeight := block.Header.Height
txs := make([]common.Uint256, 0)
for _, tx := range block.Transactions {
txHash := tx.Hash()
txs = append(txs, txHash)
}
if len(txs) > 0 {
err := this.eventStore.SaveEventNotifyByBlock(block.Header.Height, txs)
if err != nil {
return fmt.Errorf("SaveEventNotifyByBlock error %s", err)
}
}
err := this.eventStore.SaveCurrentBlock(blockHeight, blockHash)
if err != nil {
return fmt.Errorf("SaveCurrentBlock error %s", err)
}
return nil
}
func (this *LedgerStoreImp) tryGetSavingBlockLock() (hasLocked bool) {
select {
case this.savingBlockSemaphore <- true:
return false
default:
return true
}
}
func (this *LedgerStoreImp) getSavingBlockLock() {
this.savingBlockSemaphore <- true
}
func (this *LedgerStoreImp) releaseSavingBlockLock() {
select {
case <-this.savingBlockSemaphore:
return
default:
panic("can not release in unlocked state")
}
}
//saveBlock do the job of execution samrt contract and commit block to store.
func (this *LedgerStoreImp) submitBlock(block *types.Block, result store.ExecuteResult) error {
blockHash := block.Hash()
blockHeight := block.Header.Height
blockRoot := this.GetBlockRootWithPreBlockHashes(block.Header.Height, []common.Uint256{block.Header.PrevBlockHash})
if block.Header.Height != 0 && blockRoot != block.Header.BlockRoot {
return fmt.Errorf("wrong block root at height:%d, expected:%s, got:%s",
block.Header.Height, blockRoot.ToHexString(), block.Header.BlockRoot.ToHexString())
}
this.blockStore.NewBatch()
this.stateStore.NewBatch()
this.eventStore.NewBatch()
err := this.saveBlockToBlockStore(block)
if err != nil {
return fmt.Errorf("save to block store height:%d error:%s", blockHeight, err)
}
err = this.saveBlockToStateStore(block, result)
if err != nil {
return fmt.Errorf("save to state store height:%d error:%s", blockHeight, err)
}
err = this.saveBlockToEventStore(block)
if err != nil {
return fmt.Errorf("save to event store height:%d error:%s", blockHeight, err)
}
err = this.blockStore.CommitTo()
if err != nil {
return fmt.Errorf("blockStore.CommitTo height:%d error %s", blockHeight, err)
}
// event store is idempotent to re-save when in recovering process, so save first before stateStore
err = this.eventStore.CommitTo()
if err != nil {
return fmt.Errorf("eventStore.CommitTo height:%d error %s", blockHeight, err)
}
err = this.stateStore.CommitTo()
if err != nil {
return fmt.Errorf("stateStore.CommitTo height:%d error %s", blockHeight, err)
}
this.setCurrentBlock(blockHeight, blockHash)
if events.DefActorPublisher != nil {
events.DefActorPublisher.Publish(
message.TOPIC_SAVE_BLOCK_COMPLETE,
&message.SaveBlockCompleteMsg{
Block: block,
})
}
return nil
}
//saveBlock do the job of execution samrt contract and commit block to store.
func (this *LedgerStoreImp) saveBlock(block *types.Block, stateMerkleRoot common.Uint256) error {
blockHeight := block.Header.Height
if this.tryGetSavingBlockLock() {
//hash already saved or is saving
return nil
}
defer this.releaseSavingBlockLock()
if blockHeight > 0 && blockHeight != (this.GetCurrentBlockHeight()+1) {
return nil
}
result, err := this.executeBlock(block)
if err != nil {
return err
}
if result.MerkleRoot != stateMerkleRoot {
return fmt.Errorf("state merkle root mismatch!")
}
return this.submitBlock(block, result)
}
func (this *LedgerStoreImp) handleTransaction(overlay *overlaydb.OverlayDB, cache *storage.CacheDB, block *types.Block, tx *types.Transaction) (*event.ExecuteNotify, []common.Uint256, error) {
txHash := tx.Hash()
notify := &event.ExecuteNotify{TxHash: txHash, State: event.CONTRACT_STATE_FAIL}
if tx.TxType == types.Invoke {
crossHashes, err := this.stateStore.HandleInvokeTransaction(this, overlay, cache, tx, block, notify)
if overlay.Error() != nil {
return nil, nil, fmt.Errorf("HandleInvokeTransaction tx %s error %s", txHash.ToHexString(), overlay.Error())
}
if err != nil {
log.Debugf("HandleInvokeTransaction tx %s error %s", txHash.ToHexString(), err)
}
return notify, crossHashes, nil
} else {
return nil, nil, fmt.Errorf("Unsupported transaction type!")
}
}
func (this *LedgerStoreImp) saveHeaderIndexList() error {
this.lock.RLock()
storeCount := this.storedIndexCount
currHeight := this.currBlockHeight
if currHeight-storeCount < HEADER_INDEX_BATCH_SIZE {
this.lock.RUnlock()
return nil
}
headerList := make([]common.Uint256, HEADER_INDEX_BATCH_SIZE)
for i := uint32(0); i < HEADER_INDEX_BATCH_SIZE; i++ {
height := storeCount + i
headerList[i] = this.headerIndex[height]
}
this.lock.RUnlock()
err := this.blockStore.SaveHeaderIndexList(storeCount, headerList)
if err != nil {
return fmt.Errorf("SaveHeaderIndexList start %d error %s", storeCount, err)
}
this.lock.Lock()
this.storedIndexCount += HEADER_INDEX_BATCH_SIZE
this.lock.Unlock()
return nil
}
func (this *LedgerStoreImp) PreExecuteContract(tx *types.Transaction) (*cstates.PreExecResult, error) {
result := &sstate.PreExecResult{State: event.CONTRACT_STATE_FAIL, Result: nil}
if _, ok := tx.Payload.(*payload.InvokeCode); !ok {
return result, fmt.Errorf("transaction payload type error")
}
hash := this.GetCurrentBlockHash()
block, err := this.GetBlockByHash(hash)
if err != nil {
return result, fmt.Errorf("get current block error")
}
overlay := this.stateStore.NewOverlayDB()
cache := storage.NewCacheDB(overlay)
service, err := native.NewNativeService(cache, tx, uint32(time.Now().Unix()), block.Header.Height,
hash, block.Header.ChainID, tx.Payload.(*payload.InvokeCode).Code, true)
if err != nil {
return result, fmt.Errorf("PreExecuteContract Error: %+v\n", err)
}
res, err := service.Invoke()
if err != nil {
return result, err
}
return &sstate.PreExecResult{State: event.CONTRACT_STATE_SUCCESS, Result: common.ToHexString(res.([]byte)), Notify: service.GetNotify()}, nil
}
//IsContainBlock return whether the block is in store
func (this *LedgerStoreImp) IsContainBlock(blockHash common.Uint256) (bool, error) {
return this.blockStore.ContainBlock(blockHash)
}
//IsContainTransaction return whether the transaction is in store. Wrap function of BlockStore.ContainTransaction
func (this *LedgerStoreImp) IsContainTransaction(txHash common.Uint256) (bool, error) {
return this.blockStore.ContainTransaction(txHash)
}
func (this *LedgerStoreImp) GetBlockRootWithPreBlockHashes(startHeight uint32, preBlockHashes []common.Uint256) common.Uint256 {
this.lock.RLock()
defer this.lock.RUnlock()
// the block height in consensus is far behind ledger, this case should be rare
if this.currBlockHeight > startHeight+uint32(len(preBlockHashes))-1 {
// or return error?
log.Errorf("this.currBlockHeight= %d, startHeight= %d, len(preBlockHashes)= %d\n", this.currBlockHeight, startHeight, len(preBlockHashes))
return common.UINT256_EMPTY
}
needs := preBlockHashes[this.currBlockHeight+1-startHeight:]
return this.stateStore.GetBlockRootWithPreBlockHashes(needs)
}
//GetBlockHash return the block hash by block height
func (this *LedgerStoreImp) GetBlockHash(height uint32) common.Uint256 {
return this.getHeaderIndex(height)
}
//GetHeaderByHash return the block header by block hash
func (this *LedgerStoreImp) GetHeaderByHash(blockHash common.Uint256) (*types.Header, error) {
header := this.getHeaderCache(blockHash)
if header != nil {
return header, nil
}
return this.blockStore.GetHeader(blockHash)
}
//GetHeaderByHash return the block header by block height
func (this *LedgerStoreImp) GetHeaderByHeight(height uint32) (*types.Header, error) {
blockHash := this.GetBlockHash(height)
var empty common.Uint256
if blockHash == empty {
return nil, nil
}
return this.GetHeaderByHash(blockHash)
}
//GetTransaction return transaction by transaction hash. Wrap function of BlockStore.GetTransaction
func (this *LedgerStoreImp) GetTransaction(txHash common.Uint256) (*types.Transaction, uint32, error) {
return this.blockStore.GetTransaction(txHash)
}
//GetBlockByHash return block by block hash. Wrap function of BlockStore.GetBlockByHash
func (this *LedgerStoreImp) GetBlockByHash(blockHash common.Uint256) (*types.Block, error) {
return this.blockStore.GetBlock(blockHash)
}
//GetBlockByHeight return block by height.
func (this *LedgerStoreImp) GetBlockByHeight(height uint32) (*types.Block, error) {
blockHash := this.GetBlockHash(height)
var empty common.Uint256
if blockHash == empty {
return nil, nil
}
return this.GetBlockByHash(blockHash)
}
//GetBookkeeperState return the bookkeeper state. Wrap function of StateStore.GetBookkeeperState
func (this *LedgerStoreImp) GetBookkeeperState() (*states.BookkeeperState, error) {
return this.stateStore.GetBookkeeperState()
}
//GetMerkleProof return the block merkle proof. Wrap function of StateStore.GetMerkleProof
func (this *LedgerStoreImp) GetMerkleProof(raw []byte, proofHeight, rootHeight uint32) ([]byte, error) {
return this.stateStore.GetMerkleProof(raw, proofHeight, rootHeight)
}
//GetStorageItem return the storage value of the key in smart contract. Wrap function of StateStore.GetStorageState
func (this *LedgerStoreImp) GetStorageItem(key *states.StorageKey) (*states.StorageItem, error) {
return this.stateStore.GetStorageState(key)
}
//GetEventNotifyByTx return the events notify gen by executing of smart contract. Wrap function of EventStore.GetEventNotifyByTx
func (this *LedgerStoreImp) GetEventNotifyByTx(tx common.Uint256) (*event.ExecuteNotify, error) {
return this.eventStore.GetEventNotifyByTx(tx)
}
//GetEventNotifyByBlock return the transaction hash which have event notice after execution of smart contract. Wrap function of EventStore.GetEventNotifyByBlock
func (this *LedgerStoreImp) GetEventNotifyByBlock(height uint32) ([]*event.ExecuteNotify, error) {
return this.eventStore.GetEventNotifyByBlock(height)
}
//Close ledger store.
func (this *LedgerStoreImp) Close() error {
err := this.blockStore.Close()
if err != nil {
return fmt.Errorf("blockStore close error %s", err)
}
err = this.stateStore.Close()
if err != nil {
return fmt.Errorf("stateStore close error %s", err)
}
err = this.eventStore.Close()
if err != nil {
return fmt.Errorf("eventStore close error %s", err)
}
return nil
}