-
Notifications
You must be signed in to change notification settings - Fork 291
/
ledger_store.go
959 lines (871 loc) · 30.8 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
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
*
* The ontology 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 ontology 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 ontology. If not, see <http://www.gnu.org/licenses/>.
*/
//Storage of ledger
package ledgerstore
import (
"bytes"
"fmt"
"math"
"os"
"sort"
"strings"
"sync"
"strconv"
"github.com/ontio/ontology-crypto/keypair"
"github.com/ontio/ontology/common"
"github.com/ontio/ontology/common/config"
"github.com/ontio/ontology/common/log"
"github.com/ontio/ontology/common/serialization"
vconfig "github.com/ontio/ontology/consensus/vbft/config"
"github.com/ontio/ontology/core/payload"
"github.com/ontio/ontology/core/signature"
"github.com/ontio/ontology/core/states"
scom "github.com/ontio/ontology/core/store/common"
"github.com/ontio/ontology/core/store/statestore"
"github.com/ontio/ontology/core/types"
"github.com/ontio/ontology/errors"
"github.com/ontio/ontology/events"
"github.com/ontio/ontology/events/message"
"github.com/ontio/ontology/smartcontract"
scommon "github.com/ontio/ontology/smartcontract/common"
"github.com/ontio/ontology/smartcontract/event"
"github.com/ontio/ontology/smartcontract/service/native/global_params"
"github.com/ontio/ontology/smartcontract/service/native/utils"
"github.com/ontio/ontology/smartcontract/service/neovm"
sstate "github.com/ontio/ontology/smartcontract/states"
"github.com/ontio/ontology/smartcontract/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
savingBlock bool //is saving block now
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),
}
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
stateStore, err := NewStateStore(fmt.Sprintf("%s%s%s", dataDir, string(os.PathSeparator), DBDirState),
fmt.Sprintf("%s%s%s", dataDir, string(os.PathSeparator), MerkleTreeStorePath))
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)
}
err = this.saveBlock(genesisBlock)
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" {
blk, err := this.GetBlockByHeight(this.currBlockHeight)
if err != nil {
return err
}
blkInfo, err := vconfig.VbftBlock(blk.Header)
if err != nil {
return err
}
var cfg *vconfig.ChainConfig
if blkInfo.NewChainConfig != nil {
cfg = blkInfo.NewChainConfig
} else {
cfgBlock, err := this.GetBlockByHeight(blkInfo.LastConfigBlockNum)
if err != nil {
return err
}
Info, err := vconfig.VbftBlock(cfgBlock.Header)
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 nil
}
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.initCurrentBlock()
if err != nil {
return fmt.Errorf("initCurrentBlock error %s", err)
}
err = this.initHeaderIndexList()
if err != nil {
return fmt.Errorf("initHeaderIndexList error %s", err)
}
err = this.initStore()
if err != nil {
return fmt.Errorf("initStore error %s", err)
}
return nil
}
func (this *LedgerStoreImp) initCurrentBlock() 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) initHeaderIndexList() 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) initStore() 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.stateStore.NewBatch()
err = this.saveBlockToStateStore(block)
if err != nil {
return fmt.Errorf("save to state store height:%d error:%s", i, err)
}
err = this.stateStore.CommitTo()
if err != nil {
return fmt.Errorf("stateStore.CommitTo height:%d error %s", i, err)
}
}
_, eventHeight, err := this.eventStore.GetCurrentBlock()
if err != nil {
return fmt.Errorf("eventStore.GetCurrentBlock error:%s", err)
}
for i := eventHeight; 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()
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)
}
}
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
m := len(header.Bookkeepers) - (len(header.Bookkeepers)-1)/3
if len(header.Bookkeepers) < m {
return vbftPeerInfo, fmt.Errorf("header Bookkeepers %d less than 2/3 len vbftPeerInfo%d", len(header.Bookkeepers), len(vbftPeerInfo))
}
for _, bookkeeper := range header.Bookkeepers {
pubkey := vconfig.PubkeyID(bookkeeper)
_, present := vbftPeerInfo[pubkey]
if !present {
log.Errorf("invalid pubkey :%v,height:%d", pubkey, header.Height)
return vbftPeerInfo, fmt.Errorf("invalid pubkey :%v", pubkey)
}
}
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
}
//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) 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)
if err != nil {
return fmt.Errorf("saveBlock error %s", err)
}
this.delHeaderCache(block.Hash())
return 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) saveBlockToStateStore(block *types.Block) error {
blockHash := block.Hash()
blockHeight := block.Header.Height
stateBatch := this.stateStore.NewStateBatch()
if block.Header.Height != 0 {
config := &smartcontract.Config{
Time: block.Header.Timestamp,
Height: block.Header.Height,
Tx: &types.Transaction{},
}
if err := refreshGlobalParam(config, storage.NewCloneCache(this.stateStore.NewStateBatch()), this); err != nil {
return err
}
}
for _, tx := range block.Transactions {
err := this.handleTransaction(stateBatch, block, tx)
if err != nil {
return fmt.Errorf("handleTransaction error %s", err)
}
}
err := this.stateStore.AddMerkleTreeRoot(block.Header.TransactionsRoot)
if err != nil {
return fmt.Errorf("AddMerkleTreeRoot error %s", err)
}
err = this.stateStore.SaveCurrentBlock(blockHeight, blockHash)
if err != nil {
return fmt.Errorf("SaveCurrentBlock error %s", err)
}
err = stateBatch.CommitTo()
if err != nil {
return fmt.Errorf("stateBatch.CommitTo error %s", err)
}
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) isSavingBlock() bool {
this.lock.Lock()
defer this.lock.Unlock()
if !this.savingBlock {
this.savingBlock = true
return false
}
return true
}
func (this *LedgerStoreImp) resetSavingBlock() {
this.lock.Lock()
defer this.lock.Unlock()
this.savingBlock = false
}
//saveBlock do the job of execution samrt contract and commit block to store.
func (this *LedgerStoreImp) saveBlock(block *types.Block) error {
blockHash := block.Hash()
blockHeight := block.Header.Height
if this.isSavingBlock() {
//hash already saved or is saving
return nil
}
defer this.resetSavingBlock()
if blockHeight > 0 && blockHeight != (this.GetCurrentBlockHeight()+1) {
return nil
}
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)
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)
}
err = this.stateStore.CommitTo()
if err != nil {
return fmt.Errorf("stateStore.CommitTo height:%d error %s", blockHeight, err)
}
err = this.eventStore.CommitTo()
if err != nil {
return fmt.Errorf("eventStore.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
}
func (this *LedgerStoreImp) handleTransaction(stateBatch *statestore.StateBatch, block *types.Block, tx *types.Transaction) error {
txHash := tx.Hash()
notify := &event.ExecuteNotify{TxHash: txHash, State: event.CONTRACT_STATE_FAIL}
switch tx.TxType {
case types.Deploy:
err := this.stateStore.HandleDeployTransaction(this, stateBatch, tx, block, notify)
if stateBatch.Error() != nil {
return fmt.Errorf("HandleDeployTransaction tx %s error %s", txHash.ToHexString(), stateBatch.Error())
}
if err != nil {
log.Debugf("HandleDeployTransaction tx %s error %s", txHash.ToHexString(), err)
}
SaveNotify(this.eventStore, txHash, notify)
case types.Invoke:
err := this.stateStore.HandleInvokeTransaction(this, stateBatch, tx, block, notify)
if stateBatch.Error() != nil {
return fmt.Errorf("HandleInvokeTransaction tx %s error %s", txHash.ToHexString(), stateBatch.Error())
}
if err != nil {
log.Debugf("HandleInvokeTransaction tx %s error %s", txHash.ToHexString(), err)
}
SaveNotify(this.eventStore, txHash, notify)
}
return nil
}
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
}
//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)
}
//GetBlockRootWithNewTxRoot return the block root(merkle root of blocks) after add a new tx root of block
func (this *LedgerStoreImp) GetBlockRootWithNewTxRoot(txRoot common.Uint256) common.Uint256 {
return this.stateStore.GetBlockRootWithNewTxRoot(txRoot)
}
//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)
}
//GetSysFeeAmount return the sys fee for block by block hash. Wrap function of BlockStore.GetSysFeeAmount
func (this *LedgerStoreImp) GetSysFeeAmount(blockHash common.Uint256) (common.Fixed64, error) {
return this.blockStore.GetSysFeeAmount(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(proofHeight, rootHeight uint32) ([]common.Uint256, error) {
return this.stateStore.GetMerkleProof(proofHeight, rootHeight)
}
//GetContractState return contract by contract address. Wrap function of StateStore.GetContractState
func (this *LedgerStoreImp) GetContractState(contractHash common.Address) (*payload.DeployCode, error) {
return this.stateStore.GetContractState(contractHash)
}
//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)
}
//PreExecuteContract return the result of smart contract execution without commit to store
func (this *LedgerStoreImp) PreExecuteContract(tx *types.Transaction) (*sstate.PreExecResult, error) {
height := this.GetCurrentBlockHeight()
stf := &sstate.PreExecResult{State: event.CONTRACT_STATE_FAIL, Gas: neovm.MIN_TRANSACTION_GAS, Result: nil}
header, err := this.GetHeaderByHeight(height)
if err != nil {
return stf, err
}
config := &smartcontract.Config{
Time: header.Timestamp,
Height: header.Height,
Tx: tx,
RandomHash: this.GetBlockHash(height),
}
cache := storage.NewCloneCache(this.stateStore.NewStateBatch())
preGas, err := this.getPreGas(config, cache)
if err != nil {
return stf, err
}
if tx.TxType == types.Invoke {
invoke := tx.Payload.(*payload.InvokeCode)
sc := smartcontract.SmartContract{
Config: config,
Store: this,
CloneCache: cache,
Gas: math.MaxUint64 - calcGasByCodeLen(len(invoke.Code), preGas[neovm.UINT_INVOKE_CODE_LEN_NAME]),
}
//start the smart contract executive function
engine, _ := sc.NewExecuteEngine(invoke.Code)
result, err := engine.Invoke()
if err != nil {
return stf, err
}
gasCost := math.MaxUint64 - sc.Gas
mixGas := neovm.MIN_TRANSACTION_GAS
if gasCost < mixGas {
gasCost = mixGas
}
cv, err := scommon.ConvertNeoVmTypeHexString(result)
if err != nil {
return stf, err
}
return &sstate.PreExecResult{State: event.CONTRACT_STATE_SUCCESS, Gas: gasCost, Result: cv}, nil
} else if tx.TxType == types.Deploy {
deploy := tx.Payload.(*payload.DeployCode)
return &sstate.PreExecResult{State: event.CONTRACT_STATE_SUCCESS, Gas: preGas[neovm.CONTRACT_CREATE_NAME] + calcGasByCodeLen(len(deploy.Code), preGas[neovm.UINT_DEPLOY_CODE_LEN_NAME]), Result: nil}, nil
} else {
return stf, errors.NewErr("transaction type error")
}
}
func (this *LedgerStoreImp) getPreGas(config *smartcontract.Config, cache *storage.CloneCache) (map[string]uint64, error) {
bf := new(bytes.Buffer)
names := []string{neovm.CONTRACT_CREATE_NAME, neovm.UINT_INVOKE_CODE_LEN_NAME, neovm.UINT_DEPLOY_CODE_LEN_NAME}
if err := utils.WriteVarUint(bf, uint64(len(names))); err != nil {
return nil, fmt.Errorf("write gas_table_keys length error:%s", err)
}
for _, v := range names {
if err := serialization.WriteString(bf, v); err != nil {
return nil, fmt.Errorf("serialize param name error:%s", err)
}
}
sc := smartcontract.SmartContract{
Config: config,
CloneCache: cache,
Store: this,
Gas: math.MaxUint64,
}
service, _ := sc.NewNativeService()
result, err := service.NativeCall(utils.ParamContractAddress, "getGlobalParam", bf.Bytes())
if err != nil {
return nil, err
}
params := new(global_params.Params)
if err := params.Deserialize(bytes.NewBuffer(result.([]byte))); err != nil {
return nil, fmt.Errorf("deserialize global params error:%s", err)
}
m := make(map[string]uint64, 0)
for _, v := range names {
n, ps := params.GetParam(v)
if n != -1 && ps.Value != "" {
pu, err := strconv.ParseUint(ps.Value, 10, 64)
if err != nil {
return nil, fmt.Errorf("failed to parse uint %v", err)
}
m[v] = pu
}
}
return m, nil
}
//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
}