-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathledger.go
873 lines (737 loc) · 29.2 KB
/
ledger.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
package ledger
import (
"encoding/hex"
"fmt"
"strconv"
"sync"
"time"
"github.com/spf13/viper"
"github.com/dnerochain/dnero/store"
"github.com/dnerochain/dnero/store/kvstore"
log "github.com/sirupsen/logrus"
"github.com/dnerochain/dnero/blockchain"
"github.com/dnerochain/dnero/common"
"github.com/dnerochain/dnero/common/result"
"github.com/dnerochain/dnero/core"
"github.com/dnerochain/dnero/crypto"
exec "github.com/dnerochain/dnero/ledger/execution"
"github.com/dnerochain/dnero/ledger/state"
st "github.com/dnerochain/dnero/ledger/state"
"github.com/dnerochain/dnero/ledger/types"
mp "github.com/dnerochain/dnero/mempool"
"github.com/dnerochain/dnero/store/database"
)
var logger *log.Entry = log.WithFields(log.Fields{"prefix": "ledger"})
var _ core.Ledger = (*Ledger)(nil)
//
// Ledger implements the core.Ledger interface
//
type Ledger struct {
db database.Database
chain *blockchain.Chain
consensus core.ConsensusEngine
valMgr core.ValidatorManager
mempool *mp.Mempool
currentBlock *core.Block
mu *sync.RWMutex // Lock for accessing ledger state.
state *st.LedgerState
executor *exec.Executor
}
// NewLedger creates an instance of Ledger
func NewLedger(chainID string, db database.Database, tagger st.Tagger, chain *blockchain.Chain, consensus core.ConsensusEngine, valMgr core.ValidatorManager, mempool *mp.Mempool) *Ledger {
state := st.NewLedgerState(chainID, db, tagger)
ledger := &Ledger{
db: db,
chain: chain,
consensus: consensus,
valMgr: valMgr,
mempool: mempool,
mu: &sync.RWMutex{},
state: state,
}
executor := exec.NewExecutor(db, chain, state, consensus, valMgr, ledger)
ledger.SetExecutor(executor)
return ledger
}
// SetExecutor sets the executor for the ledger
func (ledger *Ledger) SetExecutor(executor *exec.Executor) {
ledger.executor = executor
}
// State returns the state of the ledger
func (ledger *Ledger) State() *st.LedgerState {
return ledger.state
}
// GetCurrentBlock returns the block currently being processed
func (ledger *Ledger) GetCurrentBlock() *core.Block {
return ledger.currentBlock
}
// GetScreenedSnapshot returns a snapshot of screened ledger state to query about accounts, etc.
func (ledger *Ledger) GetScreenedSnapshot() (*st.StoreView, error) {
ledger.mu.Lock()
defer ledger.mu.Unlock()
return ledger.state.Screened().Copy()
}
// GetDeliveredSnapshot returns a snapshot of delivered ledger state to query about accounts, etc.
func (ledger *Ledger) GetDeliveredSnapshot() (*st.StoreView, error) {
ledger.mu.Lock()
defer ledger.mu.Unlock()
return ledger.state.Delivered().Copy()
}
// GetFinalizedSnapshot returns a snapshot of finalized ledger state to query about accounts, etc.
func (ledger *Ledger) GetFinalizedSnapshot() (*st.StoreView, error) {
ledger.mu.Lock()
defer ledger.mu.Unlock()
return ledger.state.Finalized().Copy()
}
// GetFinalizedValidatorCandidatePool returns the validator candidate pool of the latest DIRECTLY finalized block
func (ledger *Ledger) GetFinalizedValidatorCandidatePool(blockHash common.Hash, isNext bool) (*core.ValidatorCandidatePool, error) {
db := ledger.state.DB()
store := kvstore.NewKVStore(db)
var i int
if isNext {
i = 1
} else {
i = 2
}
for ; ; i-- {
block, err := findBlock(store, blockHash)
if err != nil {
logger.Errorf("Failed to find block for VCP: %v, err: %v", blockHash.Hex(), err)
return nil, err
}
if block == nil {
return nil, fmt.Errorf("Block is nil for hash %v", blockHash.Hex())
}
// Grandparent or root block.
if i == 0 || block.HCC.BlockHash.IsEmpty() || block.Status.IsTrusted() {
stateRoot := block.BlockHeader.StateHash
storeView := st.NewStoreView(block.Height, stateRoot, db)
if storeView == nil {
logger.WithFields(log.Fields{
"block.Hash": blockHash.Hex(),
"block.Height": block.Height,
"block.HCC.BlockHash": block.HCC.BlockHash.Hex(),
"block.BlockHeader.StateHash": block.BlockHeader.StateHash.Hex(),
"block.Status.IsTrusted()": block.Status.IsTrusted(),
}).Panic("Failed to load state for validator pool")
}
vcp := storeView.GetValidatorCandidatePool()
return vcp, nil
}
blockHash = block.HCC.BlockHash
}
return nil, fmt.Errorf("Failed to find a directly finalized ancestor block for %v", blockHash)
}
// GetSentryCandidatePool returns the sentry candidate pool of the given block.
func (ledger *Ledger) GetSentryCandidatePool(blockHash common.Hash) (*core.SentryCandidatePool, error) {
db := ledger.state.DB()
store := kvstore.NewKVStore(db)
// Find last checkpoint and retrieve SCP.
block, err := findBlock(store, blockHash)
if err != nil {
return nil, err
}
blockHash = block.Hash()
for {
logger.Debugf("Ledger.GetSentryCandidatePool, block.height = %v", block.Height)
block, err := findBlock(store, blockHash)
if err != nil {
return nil, err
}
if common.IsCheckPointHeight(block.Height) {
stateRoot := block.BlockHeader.StateHash
storeView := st.NewStoreView(block.Height, stateRoot, db)
scp := storeView.GetSentryCandidatePool()
return scp, nil
}
blockHash = block.Parent
}
}
// GetEliteEdgeNodePoolOfLastCheckpoint returns the elite edge node pool of the given block.
func (ledger *Ledger) GetEliteEdgeNodePoolOfLastCheckpoint(blockHash common.Hash) (core.EliteEdgeNodePool, error) {
db := ledger.state.DB()
store := kvstore.NewKVStore(db)
// Find last checkpoint and retrieve EENP.
block, err := findBlock(store, blockHash)
if err != nil {
return nil, err
}
blockHash = block.Hash()
for {
logger.Debugf("Ledger.GetEliteEdgeNodePoolOfLastCheckpoint, block.height = %v", block.Height)
block, err := findBlock(store, blockHash)
if err != nil {
return nil, err
}
if common.IsCheckPointHeight(block.Height) {
stateRoot := block.BlockHeader.StateHash
storeView := st.NewStoreView(block.Height, stateRoot, db)
eenp := state.NewEliteEdgeNodePool(storeView, true)
return eenp, nil
}
blockHash = block.Parent
}
}
func findBlock(store store.Store, blockHash common.Hash) (*core.ExtendedBlock, error) {
var block core.ExtendedBlock
err := store.Get(blockHash[:], &block)
if err != nil {
return nil, err
}
return &block, nil
}
// ScreenTxUnsafe screens the given transaction without locking.
func (ledger *Ledger) ScreenTxUnsafe(rawTx common.Bytes) (res result.Result) {
var tx types.Tx
tx, err := types.TxFromBytes(rawTx)
if err != nil {
return result.Error("Error decoding tx: %v", err)
}
_, res = ledger.executor.ScreenTx(tx)
return res
}
// ScreenTx screens the given transaction
func (ledger *Ledger) ScreenTx(rawTx common.Bytes) (txInfo *core.TxInfo, res result.Result) {
var tx types.Tx
tx, err := types.TxFromBytes(rawTx)
if err != nil {
return nil, result.Error("Error decoding tx: %v", err)
}
if ledger.shouldSkipCheckTx(tx) {
return nil, result.Error("Unauthorized transaction, should skip").
WithErrorCode(result.CodeUnauthorizedTx)
}
ledger.mu.RLock()
defer ledger.mu.RUnlock()
_, res = ledger.executor.ScreenTx(tx)
if res.IsError() {
return nil, res
}
txInfo, res = ledger.executor.GetTxInfo(tx)
if res.IsError() {
return nil, res
}
return txInfo, res
}
// ProposeBlockTxs collects and executes a list of transactions, which will be used to assemble the next blockl
// It also clears these transactions from the mempool.
func (ledger *Ledger) ProposeBlockTxs(block *core.Block, shouldIncludeValidatorUpdateTxs bool) (stateRootHash common.Hash, blockRawTxs []common.Bytes, res result.Result) {
// Must always acquire locks in following order to avoid deadlock: mempool, ledger.
// Otherwise, could cause deadlock since mempool.InsertTransaction() also first acquires the mempool, and then the ledger lock
logger.Debugf("ProposeBlockTxs: Propose block transactions, block.height = %v", block.Height)
start := time.Now()
ledger.mempool.Lock()
defer ledger.mempool.Unlock()
ledger.mu.Lock()
defer ledger.mu.Unlock()
ledger.currentBlock = block
defer func() { ledger.currentBlock = nil }()
view := ledger.state.Checked()
logger.Debugf("ProposeBlockTxs: Start adding block transactions, block.height = %v", block.Height)
preparationTime := time.Since(start)
start = time.Now()
// Add special transactions
rawTxCandidates := []common.Bytes{}
ledger.addSpecialTransactions(block, view, &rawTxCandidates)
// Add regular transactions submitted by the clients
regularRawTxs := ledger.mempool.ReapUnsafe(core.MaxNumRegularTxsPerBlock)
for _, regularRawTx := range regularRawTxs {
rawTxCandidates = append(rawTxCandidates, regularRawTx)
}
logger.Debugf("ProposeBlockTxs: block transactions added, block.height = %v", block.Height)
addTxsTime := time.Since(start)
start = time.Now()
blockRawTxs = []common.Bytes{}
for _, rawTxCandidate := range rawTxCandidates {
tx, err := types.TxFromBytes(rawTxCandidate)
if err != nil {
continue
}
if !shouldIncludeValidatorUpdateTxs {
// Skip validator updating txs
if _, ok := tx.(*types.DepositStakeTx); ok {
continue
}
if _, ok := tx.(*types.WithdrawStakeTx); ok {
continue
}
}
_, res := ledger.executor.CheckTx(tx)
if res.IsError() {
logger.Errorf("Transaction check failed: errMsg = %v, tx = %v", res.Message, tx)
continue
}
blockRawTxs = append(blockRawTxs, rawTxCandidate)
}
logger.Debugf("ProposeBlockTxs: block transactions executed, block.height = %v", block.Height)
execTxsTime := time.Since(start)
start = time.Now()
ledger.handleDelayedStateUpdates(view)
stateRootHash = view.Hash()
logger.Debugf("ProposeBlockTxs: delay update handled, block.height = %v", block.Height)
handleDelayedUpdateTime := time.Since(start)
logger.Debugf("ProposeBlockTxs: Done, block.height = %v, preparationTime = %v, addTxsTime = %v, execTxsTime = %v, handleDelayedUpdateTime = %v",
block.Height, preparationTime, addTxsTime, execTxsTime, handleDelayedUpdateTime)
return stateRootHash, blockRawTxs, result.OK
}
// ApplyBlockTxs applies the given block transactions. If any of the transactions failed, it returns
// an error immediately. If all the transactions execute successfully, it then validates the state
// root hash. If the states root hash matches the expected value, it clears the transactions from the mempool
func (ledger *Ledger) ApplyBlockTxs(block *core.Block) result.Result {
// Must always acquire locks in following order to avoid deadlock: mempool, ledger.
// Otherwise, could cause deadlock since mempool.InsertTransaction() also first acquires the mempool, and then the ledger lock
logger.Debugf("ApplyBlockTxs: Apply block transactions, block.height = %v", block.Height)
ledger.mu.Lock()
defer ledger.mu.Unlock()
ledger.currentBlock = block
defer func() { ledger.currentBlock = nil }()
blockRawTxs := ledger.currentBlock.Txs
expectedStateRoot := ledger.currentBlock.StateHash
view := ledger.state.Delivered()
// currHeight := view.Height()
// currStateRoot := view.Hash()
extParentBlock, err := ledger.chain.FindBlock(block.Parent)
if extParentBlock == nil || err != nil {
panic(fmt.Sprintf("Failed to find the parent block: %v, err: %v", block.Parent.Hex(), err))
}
parentBlock := extParentBlock.Block
logger.Debugf("ApplyBlockTxs: Start applying block transactions, block.height = %v", block.Height)
hasValidatorUpdate := false
txProcessTime := []time.Duration{}
for _, rawTx := range blockRawTxs {
start := time.Now()
tx, err := types.TxFromBytes(rawTx)
if err != nil {
//ledger.resetState(currHeight, currStateRoot)
ledger.resetState(parentBlock)
return result.Error("Failed to parse transaction: %v", hex.EncodeToString(rawTx))
}
if dtx, ok := tx.(*types.DepositStakeTx); ok && dtx.Purpose == core.StakeForValidator {
hasValidatorUpdate = true
} else if wtx, ok := tx.(*types.WithdrawStakeTx); ok && wtx.Purpose == core.StakeForValidator {
hasValidatorUpdate = true
}
_, res := ledger.executor.ExecuteTx(tx)
if res.IsError() {
//ledger.resetState(currHeight, currStateRoot)
ledger.resetState(parentBlock)
return res
}
txProcessTime = append(txProcessTime, time.Since(start))
}
logger.Debugf("ApplyBlockTxs: Finish applying block transactions, block.height=%v, txProcessTime=%v", block.Height, txProcessTime)
start := time.Now()
ledger.handleDelayedStateUpdates(view)
handleDelayedUpdateTime := time.Since(start)
newStateRoot := view.Hash()
if newStateRoot != expectedStateRoot {
//ledger.resetState(currHeight, currStateRoot)
ledger.resetState(parentBlock)
return result.Error("State root mismatch! root: %v, exptected: %v",
hex.EncodeToString(newStateRoot[:]),
hex.EncodeToString(expectedStateRoot[:]))
}
start = time.Now()
ledger.state.Commit() // commit to persistent storage
commitTime := time.Since(start)
logger.Debugf("ApplyBlockTxs: Committed state change, block.height = %v", block.Height)
go func() {
ledger.mempool.Lock()
defer ledger.mempool.Unlock()
ledger.mempool.UpdateUnsafe(blockRawTxs) // clear txs from the mempool
}()
logger.Debugf("ApplyBlockTxs: Cleared mempool transactions, block.height = %v", block.Height)
logger.Debugf("ApplyBlockTxs: Done, block.height = %v, txProcessTime = %v, handleDelayedUpdateTime = %v, commitTime = %v",
block.Height, txProcessTime, handleDelayedUpdateTime, commitTime)
return result.OKWith(result.Info{"hasValidatorUpdate": hasValidatorUpdate})
}
// ApplyBlockTxsForChainCorrection applies all block's txs and re-calculate root hash
func (ledger *Ledger) ApplyBlockTxsForChainCorrection(block *core.Block) (common.Hash, result.Result) {
ledger.mempool.Lock()
defer ledger.mempool.Unlock()
ledger.mu.Lock()
defer ledger.mu.Unlock()
ledger.currentBlock = block
defer func() { ledger.currentBlock = nil }()
blockRawTxs := ledger.currentBlock.Txs
view := ledger.state.Delivered()
//currHeight := view.Height()
//currStateRoot := view.Hash()
extParentBlock, err := ledger.chain.FindBlock(block.Parent)
if extParentBlock == nil || err != nil {
panic(fmt.Sprintf("Failed to find the parent block: %v, err: %v", block.Parent.Hex(), err))
}
parentBlock := extParentBlock.Block
hasValidatorUpdate := false
for _, rawTx := range blockRawTxs {
tx, err := types.TxFromBytes(rawTx)
if err != nil {
//ledger.resetState(currHeight, currStateRoot)
ledger.resetState(parentBlock)
return common.Hash{}, result.Error("Failed to parse transaction: %v", hex.EncodeToString(rawTx))
}
if dtx, ok := tx.(*types.DepositStakeTx); ok && dtx.Purpose == core.StakeForValidator {
hasValidatorUpdate = true
} else if wtx, ok := tx.(*types.WithdrawStakeTx); ok && wtx.Purpose == core.StakeForValidator {
hasValidatorUpdate = true
}
_, res := ledger.executor.ExecuteTx(tx)
if res.IsError() {
//ledger.resetState(currHeight, currStateRoot)
ledger.resetState(parentBlock)
return common.Hash{}, res
}
}
ledger.handleDelayedStateUpdates(view)
ledger.state.Commit() // commit to persistent storage
return view.Hash(), result.OKWith(result.Info{"hasValidatorUpdate": hasValidatorUpdate})
}
// PruneState attempts to prune the state up to the targetEndHeight
func (ledger *Ledger) PruneState(targetEndHeight uint64) error {
// Permanently disabled
return nil
// var processedHeight uint64
// db := ledger.State().DB()
// kvStore := kvstore.NewKVStore(db)
// err := kvStore.Get(state.StatePruningProgressKey(), &processedHeight)
// if err != nil {
// processedHeight = ledger.chain.Root().Height
// }
// pruneInterval := uint64(viper.GetInt(common.CfgStorageStatePruningInterval))
// maxHeightsToPrune := 3 * pruneInterval // prune too many heights at once could cause hang, should catchup gradually
// endHeight := processedHeight + maxHeightsToPrune
// if endHeight > targetEndHeight {
// endHeight = targetEndHeight
// }
// startHeight := processedHeight + 1
// if endHeight < startHeight {
// errMsg := fmt.Sprintf("endHeight (%v) < startHeight (%v)", endHeight, startHeight)
// logger.Warnf(errMsg)
// return fmt.Errorf(errMsg)
// }
// lastFinalizedBlock := ledger.consensus.GetLastFinalizedBlock()
// if endHeight >= lastFinalizedBlock.Height {
// errMsg := fmt.Sprintf("Can't prune at height >= %v yet", lastFinalizedBlock.Height)
// logger.Warnf(errMsg)
// return fmt.Errorf(errMsg)
// }
// // Need to save the progress before pruning -- in case the program exits during pruning (e.g. Ctrl+C),
// // the states that are already pruned do not get pruned again
// kvStore.Put(state.StatePruningProgressKey(), endHeight)
// err = ledger.pruneStateForRange(startHeight, endHeight)
// if err != nil {
// logger.Warnf("Unable to pruning state: %v", err)
// return err
// }
// return nil
}
// pruneStateForRange prunes states from startHeight to endHeight (inclusive for both end)
func (ledger *Ledger) pruneStateForRange(startHeight, endHeight uint64) error {
logger.Infof("Prune state from height %v to %v", startHeight, endHeight)
db := ledger.State().DB()
consensus := ledger.consensus
chain := ledger.chain
lastFinalizedBlock := consensus.GetLastFinalizedBlock()
sv := state.NewStoreView(lastFinalizedBlock.Height, lastFinalizedBlock.BlockHeader.StateHash, db)
stateHashMap := make(map[string]bool)
kvStore := kvstore.NewKVStore(db)
hl := sv.GetStakeTransactionHeightList().Heights
for _, height := range hl {
// check kvstore first
blockTrio := &core.SnapshotBlockTrio{}
blockTrioKey := []byte(core.BlockTrioStoreKeyPrefix + strconv.FormatUint(height, 10))
err := kvStore.Get(blockTrioKey, blockTrio)
if err == nil {
stateHashMap[blockTrio.First.Header.StateHash.String()] = true
continue
}
if height == core.GenesisBlockHeight {
blocks := chain.FindBlocksByHeight(core.GenesisBlockHeight)
genesisBlock := blocks[0]
stateHashMap[genesisBlock.StateHash.String()] = true
} else {
blocks := chain.FindBlocksByHeight(height)
for _, block := range blocks {
if block.Status.IsDirectlyFinalized() {
stateHashMap[block.StateHash.String()] = true
break
}
}
}
}
for height := endHeight; height >= startHeight && height > 0; height-- {
if common.IsCheckPointHeight(height+1) && viper.GetBool(common.CfgStorageStatePruningSkipCheckpoints) {
logger.Infof("Skip pruning checkpoint blocks")
continue // preserve checkpoint states
}
blocks := chain.FindBlocksByHeight(height)
for idx, block := range blocks {
if _, ok := stateHashMap[block.StateHash.String()]; !ok {
if block.HasValidatorUpdate {
continue
}
if block.Status.IsPending() || block.Status.IsInvalid() || block.Status.IsTrusted() {
continue // This could happen if the block is stored in the chain but its
// txs were not processed (e.g. an invalid block). In such cases the block
// is stored in the chain, but its state trie is not saved
}
logger.Infof("Prune state, idx: %v, height: %v, StateHash: %v", idx, height, block.StateHash.Hex())
_, err := db.Get(block.StateHash[:])
if err != nil {
logger.Errorf("StateRoot %v not found, skip pruning", block.StateHash.Hex())
continue
}
sv := state.NewStoreView(height, block.StateHash, db)
err = sv.Prune()
if err != nil {
return fmt.Errorf("Failed to prune storeview at height %v, %v", height, err)
}
}
}
}
logger.Infof("Prune state from height %v to %v completed", startHeight, endHeight)
return nil
}
// ResetState sets the ledger state with the designated root
//func (ledger *Ledger) ResetState(height uint64, rootHash common.Hash) result.Result {
func (ledger *Ledger) ResetState(block *core.Block) result.Result {
ledger.mu.Lock()
defer ledger.mu.Unlock()
//return ledger.resetState(height, rootHash)
return ledger.resetState(block)
}
// FinalizeState sets the ledger state with the finalized root
func (ledger *Ledger) FinalizeState(height uint64, rootHash common.Hash) result.Result {
ledger.mu.Lock()
defer ledger.mu.Unlock()
res := ledger.state.Finalize(height, rootHash)
if res.IsError() {
return result.Error("Failed to finalize state root: %v", hex.EncodeToString(rootHash[:]))
}
return result.OK
}
// resetState sets the ledger state with the designated root
//func (ledger *Ledger) resetState(height uint64, rootHash common.Hash) result.Result
func (ledger *Ledger) resetState(block *core.Block) result.Result {
height := block.Height
rootHash := block.StateHash
logger.Debugf("Reseting state to height %v, hash %v\n", height, rootHash.Hex())
//res := ledger.state.ResetState(height, rootHash)
res := ledger.state.ResetState(block)
if res.IsError() {
return result.Error("Failed to set state root: %v", hex.EncodeToString(rootHash[:]))
}
return result.OK
}
// CheckTx() should skip all the transactions that can only be initiated by the validators
// i.e., if a regular user submits a coinbaseTx or slashTx, it should be skipped so it will not
// get into the mempool
func (ledger *Ledger) shouldSkipCheckTx(tx types.Tx) bool {
switch tx.(type) {
case *types.CoinbaseTx:
return true
case *types.SlashTx:
return true
default:
return false
}
}
// handleDelayedStateUpdates handles delayed state updates, e.g. stake return, where the stake
// is returned only after X blocks of its corresponding StakeWithdraw transaction
func (ledger *Ledger) handleDelayedStateUpdates(view *st.StoreView) {
ledger.handleValidatorStakeReturn(view)
ledger.handleSentryStakeReturn(view)
blockHeight := view.Height() + 1
if blockHeight >= common.HeightEnableDneroV2 {
ledger.handleEliteEdgeNodeStakeReturns(view)
}
}
func (ledger *Ledger) handleValidatorStakeReturn(view *st.StoreView) {
vcp := view.GetValidatorCandidatePool()
if vcp == nil {
return
}
currentHeight := view.Height()
returnedStakes := vcp.ReturnStakes(currentHeight)
for _, returnedStake := range returnedStakes {
if !returnedStake.Withdrawn || currentHeight < returnedStake.ReturnHeight {
log.Panicf("Cannot return stake: withdrawn = %v, returnHeight = %v, currentHeight = %v",
returnedStake.Withdrawn, returnedStake.ReturnHeight, currentHeight)
}
sourceAddress := returnedStake.Source
sourceAccount := view.GetAccount(sourceAddress)
if sourceAccount == nil {
log.Panicf("Failed to retrieve source account for stake return: %v", sourceAddress)
}
returnedCoins := types.Coins{
DneroWei: returnedStake.Amount,
DTokenWei: types.Zero,
}
sourceAccount.Balance = sourceAccount.Balance.Plus(returnedCoins)
view.SetAccount(sourceAddress, sourceAccount)
}
view.UpdateValidatorCandidatePool(vcp)
}
func (ledger *Ledger) handleSentryStakeReturn(view *st.StoreView) {
scp := view.GetSentryCandidatePool()
if scp == nil || scp.Len() == 0 {
return
}
currentHeight := view.Height()
returnedStakes := scp.ReturnStakes(currentHeight)
for _, returnedStake := range returnedStakes {
if !returnedStake.Withdrawn || currentHeight < returnedStake.ReturnHeight {
log.Panicf("Cannot return stake: withdrawn = %v, returnHeight = %v, currentHeight = %v",
returnedStake.Withdrawn, returnedStake.ReturnHeight, currentHeight)
}
sourceAddress := returnedStake.Source
sourceAccount := view.GetAccount(sourceAddress)
if sourceAccount == nil {
log.Panicf("Failed to retrieve source account for stake return: %v", sourceAddress)
}
returnedCoins := types.Coins{
DneroWei: returnedStake.Amount,
DTokenWei: types.Zero,
}
sourceAccount.Balance = sourceAccount.Balance.Plus(returnedCoins)
view.SetAccount(sourceAddress, sourceAccount)
}
view.UpdateSentryCandidatePool(scp)
}
func (ledger *Ledger) handleEliteEdgeNodeStakeReturns(view *st.StoreView) {
currentHeight := view.Height()
returnedStakesWithHolders := view.GetEliteEdgeNodeStakeReturns(currentHeight)
if len(returnedStakesWithHolders) == 0 {
return // no need to call view.RemoveEliteEdgeNodeStakeReturns()
}
eenp := state.NewEliteEdgeNodePool(view, false)
for _, returnedStakeWithHolder := range returnedStakesWithHolders {
returnedStake := returnedStakeWithHolder.Stake
eenAddress := returnedStakeWithHolder.Holder
if !returnedStake.Withdrawn || currentHeight < returnedStake.ReturnHeight {
log.Panicf("Cannot return stake: withdrawn = %v, returnHeight = %v, currentHeight = %v",
returnedStake.Withdrawn, returnedStake.ReturnHeight, currentHeight)
}
sourceAddress := returnedStake.Source
sourceAccount := view.GetAccount(sourceAddress)
if sourceAccount == nil {
log.Panicf("Failed to retrieve source account for stake return: %v", sourceAddress)
}
returnedCoins := types.Coins{ // Important: Elite edge nodes deposit/withdraw DToken stake, NOT Dnero
DneroWei: types.Zero,
DTokenWei: returnedStake.Amount,
}
sourceAccount.Balance = sourceAccount.Balance.Plus(returnedCoins)
view.SetAccount(sourceAddress, sourceAccount)
// TODO: potentially O(m*n) runtime complexity, but the number of stakes on an EEN is bounded
err := eenp.ReturnStake(currentHeight, eenAddress, returnedStake)
if err != nil {
log.Panicf("Failed to return stake: currentHeight = %v, eenAddress = %v, returnedStake = %v, err = %v",
currentHeight, eenAddress, returnedStake, err)
}
logger.Infof("Stake returned: eenAddress = %v, source = %v, amount = %v",
eenAddress, returnedStake.Source, returnedStake.Amount)
}
view.RemoveEliteEdgeNodeStakeReturns(currentHeight)
}
// addSpecialTransactions adds special transactions (e.g. coinbase transaction, slash transaction) to the block
func (ledger *Ledger) addSpecialTransactions(block *core.Block, view *st.StoreView, rawTxs *[]common.Bytes) {
if block == nil {
logger.Warnf("addSpecialTransactions: block is nil")
return
}
// Note 1: Should call GetNextProposer() with the hash of the parent block, since the current block is not fully assembled yet
// Note 2: GetNextProposer() should use the current epoch instead of the parent's epoch, since different epochs might have different proposers
// Note 3: Similarly, should call GetNextValidatorSet() on the hash of the parent block
parentBlkHash := block.Parent
proposer := ledger.valMgr.GetNextProposer(parentBlkHash, block.Epoch)
validatorSet := ledger.valMgr.GetNextValidatorSet(parentBlkHash)
ledger.addCoinbaseTx(view, &proposer, validatorSet, rawTxs)
//ledger.addSlashTxs(view, &proposer, &validators, rawTxs)
}
// addCoinbaseTx adds a Coinbase transaction
func (ledger *Ledger) addCoinbaseTx(view *st.StoreView, proposer *core.Validator,
validatorSet *core.ValidatorSet, rawTxs *[]common.Bytes) {
proposerAddress := proposer.Address
proposerTxIn := types.TxInput{
Address: proposerAddress,
}
var accountRewardMap map[string]types.Coins
ch := ledger.GetCurrentBlock().Height
currentBlock := ledger.GetCurrentBlock()
sentryVotes := currentBlock.SentryVotes
eliteEdgeNodeVotes := currentBlock.EliteEdgeNodeVotes
if sentryVotes != nil && ch >= common.HeightEnableDneroV1 && common.IsCheckPointHeight(ch) {
sentryPool, eliteEdgeNodePool := exec.RetrievePools(ledger, ledger.chain, ledger.db, ch, sentryVotes, eliteEdgeNodeVotes)
accountRewardMap = exec.CalculateReward(ledger, view, validatorSet, sentryVotes, sentryPool, eliteEdgeNodeVotes, eliteEdgeNodePool)
} else { // for compatibility with lower versions (e.g. blockHeight < common.HeightEnableValidatorReward)
accountRewardMap = exec.CalculateReward(ledger, view, validatorSet, nil, nil, nil, nil)
}
coinbaseTxOutputs := []types.TxOutput{}
for accountAddressStr, accountReward := range accountRewardMap {
var accountAddress common.Address
copy(accountAddress[:], accountAddressStr)
coinbaseTxOutputs = append(coinbaseTxOutputs, types.TxOutput{
Address: accountAddress,
Coins: accountReward,
})
}
coinbaseTx := &types.CoinbaseTx{
Proposer: proposerTxIn,
Outputs: coinbaseTxOutputs,
BlockHeight: ledger.state.Height(),
}
signature, err := ledger.signTransaction(coinbaseTx)
if err != nil {
logger.Errorf("Failed to add coinbase transaction: %v", err)
return
}
coinbaseTx.SetSignature(proposerAddress, signature)
coinbaseTxBytes, err := types.TxToBytes(coinbaseTx)
if err != nil {
logger.Errorf("Failed to add coinbase transaction: %v", err)
return
}
*rawTxs = append(*rawTxs, coinbaseTxBytes)
logger.Debugf("Adding coinbase transction: tx: %v, bytes: %v", coinbaseTx, hex.EncodeToString(coinbaseTxBytes))
}
// addsSlashTx adds Slash transactions
func (ledger *Ledger) addSlashTxs(view *st.StoreView, proposer *core.Validator, validatorSet *core.ValidatorSet, rawTxs *[]common.Bytes) {
proposerAddress := proposer.Address
proposerTxIn := types.TxInput{
Address: proposerAddress,
}
slashIntents := view.GetSlashIntents()
for _, slashIntent := range slashIntents {
slashTx := &types.SlashTx{
Proposer: proposerTxIn,
SlashedAddress: slashIntent.Address,
ReserveSequence: slashIntent.ReserveSequence,
SlashProof: slashIntent.Proof,
}
signature, err := ledger.signTransaction(slashTx)
if err != nil {
logger.Errorf("Failed to add slash transaction: %v", err)
continue
}
slashTx.SetSignature(proposerAddress, signature)
slashTxBytes, err := types.TxToBytes(slashTx)
if err != nil {
logger.Errorf("Failed to add slash transaction: %v", err)
continue
}
*rawTxs = append(*rawTxs, slashTxBytes)
logger.Debugf("Adding slash transction: tx: %v, bytes: %v", slashTx, hex.EncodeToString(slashTxBytes))
}
view.ClearSlashIntents()
}
// signTransaction signs the given transaction
func (ledger *Ledger) signTransaction(tx types.Tx) (*crypto.Signature, error) {
chainID := ledger.state.GetChainID()
signBytes := tx.SignBytes(chainID)
signature, err := ledger.consensus.PrivateKey().Sign(signBytes)
if err != nil {
return nil, err
}
return signature, nil
}