-
Notifications
You must be signed in to change notification settings - Fork 4
/
blocks_trie.go
320 lines (265 loc) · 7.21 KB
/
blocks_trie.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
package txpool
import (
"errors"
"fmt"
"github.com/LemoFoundationLtd/lemochain-core/chain/params"
"github.com/LemoFoundationLtd/lemochain-core/chain/types"
"github.com/LemoFoundationLtd/lemochain-core/common"
"github.com/LemoFoundationLtd/lemochain-core/common/log"
)
var ErrTxPoolBlockExpired = errors.New("the block has expired")
type BlockTimeBucket struct {
/* 块时间 */
Time uint32
/* 块列表,以高度索引块列表 */
BlocksByHeight map[uint32]HashSet
}
func newBlockTimeBucket(block *types.Block) *BlockTimeBucket {
blockBucket := &BlockTimeBucket{
Time: block.Time(),
BlocksByHeight: make(map[uint32]HashSet),
}
blockBucket.add(block)
return blockBucket
}
func (blockBucket *BlockTimeBucket) add(block *types.Block) {
if blockBucket.Time != block.Time() {
log.Errorf("add block to time queue.err: time(%d) != block.time(%d)", blockBucket.Time, block.Time())
return
}
height := block.Height()
_, ok := blockBucket.BlocksByHeight[height]
if !ok {
blockBucket.BlocksByHeight[height] = make(HashSet)
}
blockBucket.BlocksByHeight[height].Add(block.Hash())
}
func (blockBucket *BlockTimeBucket) del(block *types.Block) {
if blockBucket.Time != block.Time() {
log.Errorf("add block to time queue.err: time(%d) != block.time(%d)", blockBucket.Time, block.Time())
return
}
if len(blockBucket.BlocksByHeight) <= 0 {
return
}
blockSet, ok := blockBucket.BlocksByHeight[block.Height()]
if ok {
blockSet.Del(block.Hash())
if len(blockSet) <= 0 {
delete(blockBucket.BlocksByHeight, block.Height())
}
}
}
func (blockBucket *BlockTimeBucket) bucketIsTimeOut(block *types.Block) bool {
if blockBucket.Time < block.Time() {
return true
} else {
return false
}
}
func (blockBucket *BlockTimeBucket) bucketIsNotTimeOut(block *types.Block) bool {
if blockBucket.Time == block.Time() {
return true
} else {
return false
}
}
func (blockBucket *BlockTimeBucket) halfHourAgo(block *types.Block) bool {
if block.Time() < blockBucket.Time {
return true
} else {
return false
}
}
type NodeByHash map[common.Hash]*TrieNode
func newNodeByHash(block *types.Block) NodeByHash {
nodeByHash := make(map[common.Hash]*TrieNode)
nodeByHash[block.Hash()] = buildTrieNode(block)
return nodeByHash
}
func buildTxSet(txs []*types.Transaction) HashSet {
txSet := make(HashSet)
if len(txs) <= 0 {
return txSet
}
for index := 0; index < len(txs); index++ {
txSet.Add(txs[index].Hash())
}
return txSet
}
func buildTrieNode(block *types.Block) *TrieNode {
if block == nil {
return nil
}
return &TrieNode{
Header: block.Header,
TxHashSet: buildTxSet(block.Txs),
}
}
func (nodeByHash NodeByHash) add(block *types.Block) {
hash := block.Hash()
_, ok := nodeByHash[hash]
if !ok {
nodeByHash[hash] = buildTrieNode(block)
}
}
func (nodeByHash NodeByHash) del(hash common.Hash) {
delete(nodeByHash, hash)
}
func (nodeByHash NodeByHash) delBatch(delBlocks HashSet) {
if len(nodeByHash) <= 0 {
return
}
if len(delBlocks) <= 0 {
return
}
for k, _ := range delBlocks {
delete(nodeByHash, k)
}
}
func (nodeByHash NodeByHash) get(hash common.Hash) *TrieNode {
node, ok := nodeByHash[hash]
if !ok {
return nil
} else {
return node
}
}
type TrieNode struct {
Header *types.Header
/* 该块打包的交易列表的索引 */
TxHashSet HashSet
}
/* 最近一个小时的所有块 */
type BlocksTrie struct {
/* 根据高度对块进行索引 */
HeightBuckets map[uint32]NodeByHash
/* 根据时间刻度对块Hash进行索引,用来回收块 */
TimeBuckets []*BlockTimeBucket
}
func NewBlocksTrie() *BlocksTrie {
return &BlocksTrie{
HeightBuckets: make(map[uint32]NodeByHash),
TimeBuckets: make([]*BlockTimeBucket, params.TransactionExpiration),
}
}
func (trie *BlocksTrie) delFromHeightBucket(block *types.Block) {
height := block.Height()
blocks := trie.HeightBuckets[height]
if blocks == nil {
return
}
blocks.del(block.Hash())
if len(blocks) <= 0 {
delete(trie.HeightBuckets, height)
}
}
func (trie *BlocksTrie) delFromHeightBucketBatch(delBlocks map[uint32]HashSet) {
if len(delBlocks) <= 0 || len(trie.HeightBuckets) <= 0 {
return
}
for height, hashSet := range delBlocks {
blocks := trie.HeightBuckets[height]
if len(blocks) <= 0 {
continue
}
blocks.delBatch(hashSet)
if len(blocks) <= 0 {
delete(trie.HeightBuckets, height)
}
}
}
func (trie *BlocksTrie) DelBlock(block *types.Block) {
if block == nil {
return
}
trie.delFromHeightBucket(block)
trie.delFromTimeBucket(block)
}
func (trie *BlocksTrie) addToHeightBucket(block *types.Block) {
_, ok := trie.HeightBuckets[block.Height()]
if !ok {
trie.HeightBuckets[block.Height()] = newNodeByHash(block)
} else {
trie.HeightBuckets[block.Height()].add(block)
}
}
func (trie *BlocksTrie) addToTimeBucket(block *types.Block) {
slot := block.Time() % uint32(params.TransactionExpiration)
if trie.TimeBuckets[slot] == nil {
trie.TimeBuckets[slot] = newBlockTimeBucket(block)
} else {
trie.TimeBuckets[slot].add(block)
}
}
func (trie *BlocksTrie) delFromTimeBucket(block *types.Block) {
slot := block.Time() % uint32(params.TransactionExpiration)
bucket := trie.TimeBuckets[slot]
if bucket == nil {
return
}
bucket.del(block)
}
func (trie *BlocksTrie) resetTimeBucket(block *types.Block) {
slot := block.Time() % uint32(params.TransactionExpiration)
trie.TimeBuckets[slot] = newBlockTimeBucket(block)
}
/* 从指定块开始,收集该块所在链指定高度区间的块[minHeight, maxHeight] */
func (trie *BlocksTrie) Path(hash common.Hash, height uint32, minHeight uint32, maxHeight uint32) []*TrieNode {
if hash == (common.Hash{}) || (minHeight > maxHeight) {
return make([]*TrieNode, 0)
}
result := make([]*TrieNode, 0, maxHeight-minHeight+1)
pHash := hash
pHeight := height
for pHeight >= minHeight {
nodes := trie.HeightBuckets[pHeight]
node := nodes.get(pHash)
/**
* 1、[minHeight, maxHeight]范围内的块存在超时被淘汰,则存在node为空,此为正常现象
* 2、调用PushBlock,添加了错误的块,也会存在,此为异常情况,只能调用的时候小心了
*/
if node == nil {
return result
}
if pHeight <= maxHeight {
result = append(result, node)
}
if pHeight <= 0 { // 防止uint32(-1)溢出
break
} else {
pHeight = node.Header.Height - 1
pHash = node.Header.ParentHash
}
}
return result
}
/* 收到一个新块,并返回过期的块的交易列表,块过期了,块中的交易肯定也过期了 */
func (trie *BlocksTrie) PushBlock(block *types.Block) error {
if block == nil {
return nil
}
slot := block.Time() % uint32(params.TransactionExpiration)
timeBucket := trie.TimeBuckets[slot]
if timeBucket == nil {
trie.resetTimeBucket(block)
trie.addToHeightBucket(block)
return nil
}
if timeBucket.bucketIsTimeOut(block) {
trie.delFromHeightBucketBatch(timeBucket.BlocksByHeight)
trie.resetTimeBucket(block)
trie.addToHeightBucket(block)
return nil
}
if timeBucket.bucketIsNotTimeOut(block) {
trie.addToHeightBucket(block)
trie.addToTimeBucket(block)
return nil
}
if timeBucket.halfHourAgo(block) {
log.Errorf(fmt.Sprintf("item.Time(%d) - block.Time(%d) = %d", timeBucket.Time, block.Time(), timeBucket.Time-block.Time()))
return ErrTxPoolBlockExpired
}
return nil
}