-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathtxpool.go
171 lines (153 loc) · 4.24 KB
/
txpool.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
// the define and some operation of txpool
package core
import (
"blockEmulator/utils"
"sync"
"time"
"unsafe"
)
type TxPool struct {
TxQueue []*Transaction // transaction Queue
RelayPool map[uint64][]*Transaction //designed for sharded blockchain, from Monoxide
lock sync.Mutex
// The pending list is ignored
}
func NewTxPool() *TxPool {
return &TxPool{
TxQueue: make([]*Transaction, 0),
RelayPool: make(map[uint64][]*Transaction),
}
}
// Add a transaction to the pool (consider the queue only)
func (txpool *TxPool) AddTx2Pool(tx *Transaction) {
txpool.lock.Lock()
defer txpool.lock.Unlock()
if tx.Time.IsZero() {
tx.Time = time.Now()
}
txpool.TxQueue = append(txpool.TxQueue, tx)
}
// Add a list of transactions to the pool
func (txpool *TxPool) AddTxs2Pool(txs []*Transaction) {
txpool.lock.Lock()
defer txpool.lock.Unlock()
for _, tx := range txs {
if tx.Time.IsZero() {
tx.Time = time.Now()
}
txpool.TxQueue = append(txpool.TxQueue, tx)
}
}
// add transactions into the pool head
func (txpool *TxPool) AddTxs2Pool_Head(tx []*Transaction) {
txpool.lock.Lock()
defer txpool.lock.Unlock()
txpool.TxQueue = append(tx, txpool.TxQueue...)
}
// Pack transactions for a proposal
func (txpool *TxPool) PackTxs(max_txs uint64) []*Transaction {
txpool.lock.Lock()
defer txpool.lock.Unlock()
txNum := max_txs
if uint64(len(txpool.TxQueue)) < txNum {
txNum = uint64(len(txpool.TxQueue))
}
txs_Packed := txpool.TxQueue[:txNum]
txpool.TxQueue = txpool.TxQueue[txNum:]
return txs_Packed
}
// Pack transaction for a proposal (use 'BlocksizeInBytes' to control)
func (txpool *TxPool) PackTxsWithBytes(max_bytes int) []*Transaction {
txpool.lock.Lock()
defer txpool.lock.Unlock()
txNum := len(txpool.TxQueue)
currentSize := 0
for tx_idx, tx := range txpool.TxQueue {
currentSize += int(unsafe.Sizeof(*tx))
if currentSize > max_bytes {
txNum = tx_idx
break
}
}
txs_Packed := txpool.TxQueue[:txNum]
txpool.TxQueue = txpool.TxQueue[txNum:]
return txs_Packed
}
// Relay transactions
func (txpool *TxPool) AddRelayTx(tx *Transaction, shardID uint64) {
txpool.lock.Lock()
defer txpool.lock.Unlock()
_, ok := txpool.RelayPool[shardID]
if !ok {
txpool.RelayPool[shardID] = make([]*Transaction, 0)
}
txpool.RelayPool[shardID] = append(txpool.RelayPool[shardID], tx)
}
// txpool get locked
func (txpool *TxPool) GetLocked() {
txpool.lock.Lock()
}
// txpool get unlocked
func (txpool *TxPool) GetUnlocked() {
txpool.lock.Unlock()
}
// get the length of tx queue
func (txpool *TxPool) GetTxQueueLen() int {
txpool.lock.Lock()
defer txpool.lock.Unlock()
return len(txpool.TxQueue)
}
// get the length of ClearRelayPool
func (txpool *TxPool) ClearRelayPool() {
txpool.lock.Lock()
defer txpool.lock.Unlock()
txpool.RelayPool = nil
}
// abort ! Pack relay transactions from relay pool
func (txpool *TxPool) PackRelayTxs(shardID, minRelaySize, maxRelaySize uint64) ([]*Transaction, bool) {
txpool.lock.Lock()
defer txpool.lock.Unlock()
if _, ok := txpool.RelayPool[shardID]; !ok {
return nil, false
}
if len(txpool.RelayPool[shardID]) < int(minRelaySize) {
return nil, false
}
txNum := maxRelaySize
if uint64(len(txpool.RelayPool[shardID])) < txNum {
txNum = uint64(len(txpool.RelayPool[shardID]))
}
relayTxPacked := txpool.RelayPool[shardID][:txNum]
txpool.RelayPool[shardID] = txpool.RelayPool[shardID][txNum:]
return relayTxPacked, true
}
// abort ! Transfer transactions when re-sharding
func (txpool *TxPool) TransferTxs(addr utils.Address) []*Transaction {
txpool.lock.Lock()
defer txpool.lock.Unlock()
txTransfered := make([]*Transaction, 0)
newTxQueue := make([]*Transaction, 0)
for _, tx := range txpool.TxQueue {
if tx.Sender == addr {
txTransfered = append(txTransfered, tx)
} else {
newTxQueue = append(newTxQueue, tx)
}
}
newRelayPool := make(map[uint64][]*Transaction)
for shardID, shardPool := range txpool.RelayPool {
for _, tx := range shardPool {
if tx.Sender == addr {
txTransfered = append(txTransfered, tx)
} else {
if _, ok := newRelayPool[shardID]; !ok {
newRelayPool[shardID] = make([]*Transaction, 0)
}
newRelayPool[shardID] = append(newRelayPool[shardID], tx)
}
}
}
txpool.TxQueue = newTxQueue
txpool.RelayPool = newRelayPool
return txTransfered
}