-
Notifications
You must be signed in to change notification settings - Fork 0
/
chain_store.go
209 lines (192 loc) · 7.1 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
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
/*
* 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/>.
*/
package vbft
import (
"fmt"
"github.com/ontio/ontology-eventbus/actor"
"github.com/tsfdsong/ontology/common"
"github.com/tsfdsong/ontology/common/log"
"github.com/tsfdsong/ontology/core/ledger"
"github.com/tsfdsong/ontology/core/store"
"github.com/tsfdsong/ontology/core/store/overlaydb"
"github.com/tsfdsong/ontology/core/types"
"github.com/tsfdsong/ontology/events/message"
)
type PendingBlock struct {
block *Block
execResult *store.ExecuteResult
hasSubmitted bool
}
type ChainStore struct {
db *ledger.Ledger
chainedBlockNum uint32
pendingBlocks map[uint32]*PendingBlock
pid *actor.PID
}
func OpenBlockStore(db *ledger.Ledger, serverPid *actor.PID) (*ChainStore, error) {
chainstore := &ChainStore{
db: db,
chainedBlockNum: db.GetCurrentBlockHeight(),
pendingBlocks: make(map[uint32]*PendingBlock),
pid: serverPid,
}
merkleRoot, err := db.GetStateMerkleRoot(chainstore.chainedBlockNum)
if err != nil {
log.Errorf("GetStateMerkleRoot blockNum:%d, error :%s", chainstore.chainedBlockNum, err)
return nil, fmt.Errorf("GetStateMerkleRoot blockNum:%d, error :%s", chainstore.chainedBlockNum, err)
}
crossStatesRoot, err := db.GetCrossStatesRoot(chainstore.chainedBlockNum)
if err != nil {
log.Errorf("GetCrossStatesRoot blockNum:%d, error :%s", chainstore.chainedBlockNum, err)
return nil, fmt.Errorf("GetCrossStatesRoot blockNum:%d, error :%s", chainstore.chainedBlockNum, err)
}
writeSet := overlaydb.NewMemDB(1, 1)
block, err := chainstore.getBlock(chainstore.chainedBlockNum)
if err != nil {
return nil, err
}
log.Debugf("chainstore openblockstore pendingBlocks height:%d,", chainstore.chainedBlockNum)
chainstore.pendingBlocks[chainstore.chainedBlockNum] = &PendingBlock{block: block, execResult: &store.ExecuteResult{WriteSet: writeSet, MerkleRoot: merkleRoot, CrossStatesRoot: crossStatesRoot}, hasSubmitted: true}
return chainstore, nil
}
func (self *ChainStore) close() {
// TODO: any action on ledger actor??
}
func (self *ChainStore) GetChainedBlockNum() uint32 {
return self.chainedBlockNum
}
func (self *ChainStore) getExecMerkleRoot(blkNum uint32) (common.Uint256, error) {
if blk, present := self.pendingBlocks[blkNum]; blk != nil && present {
return blk.execResult.MerkleRoot, nil
}
merkleRoot, err := self.db.GetStateMerkleRoot(blkNum)
if err != nil {
log.Infof("GetStateMerkleRoot blockNum:%d, error :%s", blkNum, err)
return common.Uint256{}, fmt.Errorf("GetStateMerkleRoot blockNum:%d, error :%s", blkNum, err)
} else {
return merkleRoot, nil
}
}
func (self *ChainStore) getCrossStatesRoot(blkNum uint32) (common.Uint256, error) {
if blk, present := self.pendingBlocks[blkNum]; blk != nil && present {
return blk.execResult.CrossStatesRoot, nil
}
statesRoot, err := self.db.GetCrossStatesRoot(blkNum)
if err != nil {
log.Infof("getCrossStatesRoot blockNum:%d, error :%s", blkNum, err)
return common.UINT256_EMPTY, fmt.Errorf("getCrossStatesRoot blockNum:%d, error :%s", blkNum, err)
} else {
return statesRoot, nil
}
}
func (self *ChainStore) getExecWriteSet(blkNum uint32) *overlaydb.MemDB {
if blk, present := self.pendingBlocks[blkNum]; blk != nil && present {
return blk.execResult.WriteSet
}
return nil
}
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]*PendingBlock)
for blkNum, blk := range self.pendingBlocks {
if blkNum > height {
newPending[blkNum] = blk
}
}
log.Debug("chainstore ReloadFromLedger pendingBlocks")
// 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")
}
blkNum := self.GetChainedBlockNum() + 1
err := self.submitBlock(blkNum - 1)
if err != nil {
log.Errorf("chainstore blkNum:%d, SubmitBlock: %s", blkNum-1, err)
}
execResult, err := self.db.ExecuteBlock(block.Block)
if err != nil {
log.Errorf("chainstore AddBlock GetBlockExecResult: %s", err)
return fmt.Errorf("chainstore AddBlock GetBlockExecResult: %s", err)
}
log.Debugf("execResult:%+v, AddBlock execResult height:%d \n", execResult, block.Block.Header.Height)
log.Debugf("chainstore addblock pendingBlocks height:%d,block height:%d", blkNum, block.getBlockNum())
self.pendingBlocks[blkNum] = &PendingBlock{block: block, execResult: &execResult, hasSubmitted: false}
if self.pid != nil {
self.pid.Tell(
&message.BlockConsensusComplete{
Block: block.Block,
})
}
self.chainedBlockNum = blkNum
return nil
}
func (self *ChainStore) submitBlock(blkNum uint32) error {
if blkNum == 0 {
return nil
}
if submitBlk, present := self.pendingBlocks[blkNum]; submitBlk != nil && submitBlk.hasSubmitted == false && present {
err := self.db.SubmitBlock(submitBlk.block.Block, submitBlk.block.CrossChainMsg, *submitBlk.execResult)
if err != nil {
return fmt.Errorf("ledger add submitBlk (%d, %d, %d) failed: %s", blkNum, self.GetChainedBlockNum(), self.db.GetCurrentBlockHeight(), err)
}
if _, present := self.pendingBlocks[blkNum-1]; present {
delete(self.pendingBlocks, blkNum-1)
}
submitBlk.hasSubmitted = true
}
return nil
}
func (self *ChainStore) getBlock(blockNum uint32) (*Block, error) {
if blk, present := self.pendingBlocks[blockNum]; present {
return blk.block, nil
}
block, err := self.db.GetBlockByHeight(blockNum)
if err != nil {
return nil, err
}
prevMerkleRoot := common.Uint256{}
var crossChainMsg *types.CrossChainMsg
if blockNum > 1 {
prevMerkleRoot, err = self.db.GetStateMerkleRoot(blockNum - 1)
if err != nil {
log.Errorf("GetStateMerkleRoot blockNum:%d, error :%s", blockNum, err)
return nil, fmt.Errorf("GetStateMerkleRoot blockNum:%d, error :%s", blockNum, err)
}
crossChainMsg, err = self.db.GetCrossChainMsg(blockNum - 1)
if err != nil {
log.Errorf("GetCrossChainMsg blockNum:%d, error :%s", blockNum, err)
return nil, fmt.Errorf("v blockNum:%d, error :%s", blockNum, err)
}
}
return initVbftBlock(block, crossChainMsg, prevMerkleRoot)
}