-
Notifications
You must be signed in to change notification settings - Fork 4
/
tx_pool.go
204 lines (164 loc) · 4.28 KB
/
tx_pool.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
package txpool
import (
"github.com/LemoFoundationLtd/lemochain-core/chain/types"
"github.com/LemoFoundationLtd/lemochain-core/common"
"github.com/LemoFoundationLtd/lemochain-core/common/log"
"sync"
)
type TxPool struct {
/* 还未被打包进块的交易 */
PendingTxs *TxQueue
/* 最近1个小时的所有交易 */
RecentTxs *RecentTx
/* 从当前高度向后的3600个块 */
BlockCache *BlocksTrie
RW sync.RWMutex
}
func NewTxPool() *TxPool {
return &TxPool{
PendingTxs: NewTxQueue(),
RecentTxs: NewTxRecently(),
BlockCache: NewBlocksTrie(),
}
}
/* 本节点出块时,从交易池中取出交易进行打包,但并不从交易池中删除 */
func (pool *TxPool) Get(time uint32, size int) []*types.Transaction {
pool.RW.Lock()
defer pool.RW.Unlock()
return pool.PendingTxs.Pop(time, size)
}
/* 本节点出块时,执行交易后,发现错误的交易通过该接口进行删除 */
func (pool *TxPool) DelInvalidTxs(txs []*types.Transaction) {
pool.RW.Lock()
defer pool.RW.Unlock()
if len(txs) <= 0 {
return
}
hashes := make([]common.Hash, 0, len(txs))
for _, tx := range txs {
hashes = append(hashes, tx.Hash())
}
pool.PendingTxs.DelBatch(hashes)
}
func (pool *TxPool) isInBlocks(hashes HashSet, blocks []*TrieNode) bool {
if len(hashes) <= 0 || len(blocks) <= 0 {
return false
}
for _, v := range blocks {
if !hashes.Has(v.Header.Hash()) {
continue
} else {
return true
}
}
return false
}
/* 新收一个块时,验证块中的交易是否被同一条分叉上的其他块打包了 */
func (pool *TxPool) VerifyTxInBlock(block *types.Block) bool {
pool.RW.Lock()
defer pool.RW.Unlock()
if block == nil {
return false
}
if len(block.Txs) <= 0 {
return true
}
traceByHash := pool.RecentTxs.GetTrace(block.Txs)
minHeight, maxHeight, blocks := pool.distance(traceByHash)
startHash := block.ParentHash()
startHeight := block.Height() - 1
nodes := pool.BlockCache.Path(startHash, startHeight, uint32(minHeight), uint32(maxHeight))
return !pool.isInBlocks(blocks, nodes)
}
func (pool *TxPool) distance(traceByHash map[common.Hash]TxTrace) (int64, int64, HashSet) {
minHeight := int64(^uint64(0) >> 1)
maxHeight := int64(-1)
blockSet := make(HashSet)
for _, trace := range traceByHash {
if len(trace) <= 0 {
continue
} else {
minHeightTmp, maxHeightTmp := trace.heightRange()
if minHeight > minHeightTmp {
minHeight = minHeightTmp
}
if maxHeight < maxHeightTmp {
maxHeight = maxHeightTmp
}
for blockHash, _ := range trace {
blockSet.Add(blockHash)
}
}
}
return minHeight, maxHeight, blockSet
}
/* 新收到一个通过验证的新块(包括本节点出的块),需要从交易池中删除该块中已打包的交易 */
func (pool *TxPool) RecvBlock(block *types.Block) {
pool.RW.Lock()
defer pool.RW.Unlock()
if block == nil {
return
}
txs := block.Txs
if len(txs) <= 0 {
return
}
hashes := make([]common.Hash, 0, len(txs))
for _, v := range txs {
hashes = append(hashes, v.Hash())
}
pool.PendingTxs.DelBatch(hashes)
pool.RecentTxs.RecvBlock(block.Hash(), int64(block.Height()), txs)
pool.BlockCache.PushBlock(block)
}
/* 收到一笔新的交易 */
func (pool *TxPool) RecvTx(tx *types.Transaction) bool {
pool.RW.Lock()
defer pool.RW.Unlock()
if tx == nil {
return false
}
isExist := pool.RecentTxs.IsExist(tx)
if isExist {
log.Debug("tx is already exist. hash: " + tx.Hash().Hex())
return false
} else {
pool.RecentTxs.RecvTx(tx)
pool.PendingTxs.Push(tx)
return true
}
}
func (pool *TxPool) RecvTxs(txs []*types.Transaction) bool {
pool.RW.Lock()
defer pool.RW.Unlock()
if len(txs) <= 0 {
return false
}
for _, v := range txs {
isExist := pool.RecentTxs.IsExist(v)
if !isExist {
continue
} else {
log.Debug("tx is already exist. hash: " + v.Hash().Hex())
return false
}
}
for _, v := range txs {
pool.RecentTxs.RecvTx(v)
pool.PendingTxs.Push(v)
}
return true
}
/* 对链进行剪枝,剪下的块中的交易需要回归交易池 */
func (pool *TxPool) PruneBlock(block *types.Block) {
pool.RW.Lock()
defer pool.RW.Unlock()
if block == nil {
return
}
if len(block.Txs) > 0 {
pool.PendingTxs.PushBatch(block.Txs)
pool.RecentTxs.PruneBlock(block.Hash(), int64(block.Height()), block.Txs)
}
pool.BlockCache.DelBlock(block)
}