-
Notifications
You must be signed in to change notification settings - Fork 4
/
chain_store.go
135 lines (112 loc) · 3.52 KB
/
chain_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
/*
* Copyright (C) 2018 The ZeepinChain Authors
* This file is part of The ZeepinChain library.
*
* The ZeepinChain 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 ZeepinChain 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 ZeepinChain. If not, see <http://www.gnu.org/licenses/>.
*/
package vbft
import (
"fmt"
"github.com/zeepin/ZeepinChain/common"
"github.com/zeepin/ZeepinChain/common/log"
"github.com/zeepin/ZeepinChain/core/ledger"
)
type ChainStore struct {
db *ledger.Ledger
chainedBlockNum uint32
pendingBlocks map[uint32]*Block
}
func OpenBlockStore(db *ledger.Ledger) (*ChainStore, error) {
return &ChainStore{
db: db,
chainedBlockNum: db.GetCurrentBlockHeight(),
pendingBlocks: make(map[uint32]*Block),
}, nil
}
func (self *ChainStore) close() {
// TODO: any action on ledger actor??
}
func (self *ChainStore) GetChainedBlockNum() uint32 {
return self.chainedBlockNum
}
func (self *ChainStore) ReloadFromLedger() {
height := self.db.GetCurrentBlockHeight()
if height > self.chainedBlockNum {
// update chainstore height
self.chainedBlockNum = height
// remove persisted pending blocks
newPending := make(map[uint32]*Block)
for blkNum, blk := range self.pendingBlocks {
if blkNum > height {
newPending[blkNum] = blk
}
}
// update pending blocks
self.pendingBlocks = newPending
}
}
func (self *ChainStore) AddBlock(block *Block) error {
if block == nil {
return fmt.Errorf("try add nil block")
}
if block.getBlockNum() <= self.GetChainedBlockNum() {
log.Warnf("chain store adding chained block(%d, %d)", block.getBlockNum(), self.GetChainedBlockNum())
return nil
}
if block.Block.Header == nil {
panic("nil block header")
}
self.pendingBlocks[block.getBlockNum()] = block
blkNum := self.GetChainedBlockNum() + 1
for {
if blk, present := self.pendingBlocks[blkNum]; blk != nil && present {
log.Infof("ledger adding chained block (%d, %d)", blkNum, self.GetChainedBlockNum())
err := self.db.AddBlock(blk.Block)
if err != nil && blkNum > self.GetChainedBlockNum() {
return fmt.Errorf("ledger add blk (%d, %d) failed: %s", blkNum, self.GetChainedBlockNum(), err)
}
self.chainedBlockNum = blkNum
if blkNum != self.db.GetCurrentBlockHeight() {
log.Errorf("!!! chain store added chained block (%d, %d): %s",
blkNum, self.db.GetCurrentBlockHeight(), err)
}
delete(self.pendingBlocks, blkNum)
blkNum++
} else {
break
}
}
return nil
}
//
// SetBlock is used when recovering from fork-chain
//
func (self *ChainStore) SetBlock(block *Block, blockHash common.Uint256) error {
err := self.db.AddBlock(block.Block)
self.chainedBlockNum = self.db.GetCurrentBlockHeight()
if err != nil {
return fmt.Errorf("ledger failed to add block: %s", err)
}
return nil
}
func (self *ChainStore) GetBlock(blockNum uint32) (*Block, error) {
if blk, present := self.pendingBlocks[blockNum]; present {
return blk, nil
}
block, err := self.db.GetBlockByHeight(uint32(blockNum))
if err != nil {
return nil, err
}
return initVbftBlock(block)
}