-
Notifications
You must be signed in to change notification settings - Fork 4
/
recent_tx.go
332 lines (276 loc) · 7.04 KB
/
recent_tx.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
package txpool
import (
"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"
)
type TxTimeBucket struct {
/* 交易超时时间,UTC */
Expiration uint64
/* 该超时时间下的所有交易列表 */
TxIndexes HashSet
}
func newTxTimeBucket(tx *types.Transaction) *TxTimeBucket {
bucket := &TxTimeBucket{
Expiration: tx.Expiration(),
TxIndexes: make(HashSet),
}
bucket.TxIndexes.Add(tx.Hash())
return bucket
}
func (bucket *TxTimeBucket) timeOut(time uint64) bool {
if bucket.Expiration < time {
return true
} else {
return false
}
}
func (bucket *TxTimeBucket) notTimeOut(time uint64) bool {
if bucket.Expiration == time {
return true
} else {
return false
}
}
func (bucket *TxTimeBucket) halfHourAgo(time uint64) bool {
if bucket.Expiration > time {
return true
} else {
return false
}
}
func (bucket *TxTimeBucket) add(tx *types.Transaction) {
if bucket.Expiration != tx.Expiration() {
log.Errorf("add tx to time.expiration(%d) != tx.expiration(%d)", bucket.Expiration, tx.Expiration())
} else {
bucket.TxIndexes.Add(tx.Hash())
}
}
// 交易在区块中出现过的记录。key是区块hash,value是区块高度
type TxTrace map[common.Hash]int64
func NewEmptyTxTrace() TxTrace {
return make(TxTrace)
}
func NewTxTrace(hash common.Hash, height int64) TxTrace {
txTrace := make(TxTrace)
txTrace[hash] = height
return txTrace
}
func (txTrace TxTrace) add(hash common.Hash, height int64) {
txTrace[hash] = height
}
func (txTrace TxTrace) heightRange() (int64, int64) {
minHeight := int64(^uint64(0) >> 1)
maxHeight := int64(-1)
if len(txTrace) <= 0 {
return 0, 0
}
for _, v := range txTrace {
if minHeight > int64(v) {
minHeight = int64(v)
}
if maxHeight < int64(v) {
maxHeight = int64(v)
}
}
return minHeight, maxHeight
}
func (txTrace TxTrace) del(hash common.Hash) {
delete(txTrace, hash)
}
/* 近一个小时收到的所有交易的集合,用于防止交易重放 */
type RecentTx struct {
// key是交易hash,value是交易所在块的记录
TraceMap map[common.Hash]TxTrace
TxsByTime []*TxTimeBucket
}
func NewTxRecently() *RecentTx {
return &RecentTx{
TraceMap: make(map[common.Hash]TxTrace),
TxsByTime: make([]*TxTimeBucket, params.TransactionExpiration),
}
}
func (recent *RecentTx) delBatch4Txs(hashes []common.Hash) {
if len(hashes) <= 0 || len(recent.TraceMap) <= 0 {
return
}
for _, hash := range hashes {
delete(recent.TraceMap, hash)
}
}
func (recent *RecentTx) delBox4Block(bHash common.Hash, tx *types.Transaction) {
box, err := types.GetBox(tx.Data())
if err != nil {
log.Debug("get box from tx err: " + err.Error())
} else {
txs := box.SubTxList
for _, v := range txs {
trace, ok := recent.TraceMap[v.Hash()]
if ok {
trace.del(bHash)
}
}
}
}
func (recent *RecentTx) del4Block(bHash common.Hash, tx *types.Transaction) {
trace, ok := recent.TraceMap[tx.Hash()]
if ok {
trace.del(bHash)
}
if tx.Type() == params.BoxTx {
recent.delBox4Block(bHash, tx)
}
}
func (recent *RecentTx) delBatch4Block(bHash common.Hash, txs []*types.Transaction) {
if len(txs) <= 0 {
return
}
for _, tx := range txs {
recent.del4Block(bHash, tx)
}
}
// GetTrace 获取交易在区块中出现过的记录
func (recent *RecentTx) GetTrace(txs []*types.Transaction) map[common.Hash]TxTrace {
result := make(map[common.Hash]TxTrace)
if len(txs) <= 0 || len(recent.TraceMap) <= 0 {
return result
}
for _, tx := range txs {
hash := tx.Hash()
val, ok := recent.TraceMap[hash]
if !ok || (val == nil) {
continue
} else {
result[hash] = val
}
}
return result
}
func (recent *RecentTx) isExist(tx *types.Transaction) bool {
_, ok := recent.TraceMap[tx.Hash()]
if !ok {
return false
} else {
return true
}
}
func (recent *RecentTx) boxIsExist(tx *types.Transaction) bool {
box, err := types.GetBox(tx.Data())
if err != nil {
log.Error("unmarshal box err: " + err.Error())
return false
}
txs := box.SubTxList
for _, v := range txs {
isExist := recent.isExist(v)
if isExist {
return true
}
}
return false
}
func (recent *RecentTx) IsExist(tx *types.Transaction) bool {
if len(recent.TraceMap) <= 0 {
return false
}
isExist := recent.isExist(tx)
if isExist {
return true
}
if tx.Type() == params.BoxTx {
return recent.boxIsExist(tx)
} else {
return false
}
}
func (recent *RecentTx) add2Time(tx *types.Transaction) error {
expiration := tx.Expiration()
slot := expiration % uint64(params.TransactionExpiration)
bucket := recent.TxsByTime[slot]
if bucket == nil {
recent.TxsByTime[slot] = newTxTimeBucket(tx)
return nil
}
if bucket.timeOut(expiration) {
recent.delBatch4Txs(bucket.TxIndexes.Collect())
recent.TxsByTime[slot] = newTxTimeBucket(tx)
return nil
}
if bucket.notTimeOut(expiration) {
bucket.add(tx)
return nil
}
if bucket.halfHourAgo(expiration) {
log.Errorf("tx is already time out.expiration: %d", expiration)
return ErrTxPoolBlockExpired
}
return nil
}
func (recent *RecentTx) add2Hash(hash common.Hash, height int64, tx *types.Transaction) {
_, ok := recent.TraceMap[tx.Hash()]
if !ok {
if height == -1 { // 新收到的交易,还没有被打包过
recent.TraceMap[tx.Hash()] = NewEmptyTxTrace()
} else {
recent.TraceMap[tx.Hash()] = NewTxTrace(hash, height)
}
} else {
if height == -1 { // 其他节点已经把该交易打包了,本节点同步该交易后,才收到该交易(不会发生)
return
} else {
recent.TraceMap[tx.Hash()].add(hash, height)
}
}
}
func (recent *RecentTx) addBox(hash common.Hash, height int64, tx *types.Transaction) {
box, err := types.GetBox(tx.Data())
if err != nil {
log.Error("unmarshal box err: " + err.Error())
return
}
txs := box.SubTxList
for _, v := range txs {
err := recent.add2Time(v)
if err == nil {
recent.add2Hash(hash, height, v)
}
}
}
func (recent *RecentTx) add(hash common.Hash, height int64, tx *types.Transaction) {
if (height < -1) || (tx == nil) {
return
}
err := recent.add2Time(tx)
if err == nil {
recent.add2Hash(hash, height, tx)
}
if tx.Type() == params.BoxTx {
recent.addBox(hash, height, tx)
}
}
func (recent *RecentTx) addBatch(hash common.Hash, height int64, txs []*types.Transaction) {
for index := 0; index < len(txs); index++ {
recent.add(hash, height, txs[index])
}
}
/* 收到一条新的交易,放入最近交易列表 */
func (recent *RecentTx) RecvTx(tx *types.Transaction) {
if tx == nil {
return
}
recent.add(common.Hash{}, -1, tx) // 没在块上的交易, 设置其高度为-1
}
/* 收到一个新块,把该块种的交易放入最近交易列表 */
func (recent *RecentTx) RecvBlock(hash common.Hash, height int64, txs []*types.Transaction) {
if (height < -1) || (len(txs) <= 0) {
return
}
recent.addBatch(hash, height, txs)
}
func (recent *RecentTx) PruneBlock(hash common.Hash, height int64, txs []*types.Transaction) {
if len(txs) <= 0 {
return
}
recent.delBatch4Block(hash, txs)
}