-
Notifications
You must be signed in to change notification settings - Fork 42
/
transactions.go
327 lines (244 loc) · 7.17 KB
/
transactions.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
package wavelet
import (
"sync"
"github.com/perlin-network/wavelet/conf"
"github.com/perlin-network/wavelet/internal/btree"
"github.com/pkg/errors"
)
type Transactions struct {
sync.RWMutex
buffer map[TransactionID]*Transaction
missing map[TransactionID]uint64
finalized map[TransactionID]struct{}
index btree.BTree
latest Block // The latest block height the node is aware of.
}
func NewTransactions(latest Block) *Transactions {
return &Transactions{
buffer: make(map[TransactionID]*Transaction),
missing: make(map[TransactionID]uint64),
finalized: make(map[TransactionID]struct{}),
latest: latest,
}
}
// Add adds a transaction into the node, and indexes it into the nodes mempool
// based on the value BLAKE2b(tx.ID || block.ID).
func (t *Transactions) Add(tx Transaction) {
t.Lock()
defer t.Unlock()
t.add(tx)
}
func (t *Transactions) BatchAdd(transactions []Transaction) {
t.Lock()
defer t.Unlock()
for _, tx := range transactions {
t.add(tx)
}
}
// BatchUnsafeAdd adds transactions to buffer without adding them to index
// used on node's start
func (t *Transactions) BatchUnsafeAdd(txs []*Transaction) {
t.Lock()
defer t.Unlock()
for _, tx := range txs {
t.buffer[tx.ID] = tx
}
}
func (t *Transactions) add(tx Transaction) {
if t.latest.Index >= tx.Block+uint64(conf.GetPruningLimit()) {
delete(t.missing, tx.ID)
return
}
if _, exists := t.buffer[tx.ID]; exists {
return
}
if _, finalized := t.finalized[tx.ID]; !finalized {
t.index.Set(tx.ComputeIndex(t.latest.ID), tx.ID)
}
t.buffer[tx.ID] = &tx
delete(t.missing, tx.ID) // In case the transaction was previously missing, mark it as no longer missing.
}
// MarkMissing marks that the node was expected to have archived a transaction with a specified id, but
// does not have it archived and so needs to have said transaction pulled from the nodes peers.
func (t *Transactions) MarkMissing(id TransactionID) bool {
t.Lock()
defer t.Unlock()
return t.markMissing(id)
}
// BatchMarkFinalized saves batch of transaction ids to finalized index
func (t *Transactions) BatchMarkFinalized(ids ...TransactionID) {
t.Lock()
defer t.Unlock()
for _, id := range ids {
t.finalized[id] = struct{}{}
}
}
// BatchMarkMissing is the same as MarkMissing, but it accepts a list of transaction IDs.
// It returns false if at least 1 transaction ID is found missing.
func (t *Transactions) BatchMarkMissing(ids ...TransactionID) bool {
t.Lock()
defer t.Unlock()
var missing bool
for _, id := range ids {
if t.markMissing(id) {
missing = true
}
}
return missing
}
func (t *Transactions) markMissing(id TransactionID) bool {
_, exists := t.buffer[id]
if exists {
return false
}
t.missing[id] = t.latest.Index
return true
}
// ReshufflePending reshuffles all transactions that may be proposed into a new block by recomputing
// the indices of all blocks given an updated block.
//
// It also prunes away transactions that are too stale, based on the index specified of the next
// block. It returns the total number of transactions pruned.
func (t *Transactions) ReshufflePending(next Block) []TransactionID {
t.Lock()
defer t.Unlock()
// Delete mempool entries for transactions in the finalized block.
for _, id := range next.Transactions {
t.finalized[id] = struct{}{}
}
// Recompute indices of all items in the mempool.
var updated btree.BTree
t.index.Scan(func(key []byte, value interface{}) bool {
id := value.(TransactionID)
if _, finalized := t.finalized[id]; finalized {
return true
}
tx := t.buffer[id]
if next.Index < tx.Block+uint64(conf.GetPruningLimit()) {
updated.Set(tx.ComputeIndex(next.ID), id)
}
return true
})
// Re-insert all mempool items with the recomputed indices.
t.index = updated
// Go through the entire transactions index and prune away
// any transactions that are too old.
var pruned []TransactionID
for _, tx := range t.buffer {
if next.Index >= tx.Block+uint64(conf.GetPruningLimit()) {
delete(t.buffer, tx.ID)
delete(t.finalized, tx.ID)
pruned = append(pruned, tx.ID)
}
}
// Prune any IDs of transactions that we are trying to pull from our peers
// that have not managed to be pulled for `PruningLimit` blocks.
for id, height := range t.missing {
if next.Index >= height+uint64(conf.GetPruningLimit()) {
delete(t.missing, id)
}
}
// Have all IDs of transactions missing from now on be marked to be missing from
// a new block height.
t.latest = next
return pruned
}
// Has returns whether or not the node is archiving some transaction specified
// by an id.
func (t *Transactions) Has(id TransactionID) bool {
t.RLock()
defer t.RUnlock()
_, exists := t.buffer[id]
return exists
}
func (t *Transactions) HasPending(block BlockID, id TransactionID) bool {
t.RLock()
defer t.RUnlock()
tx, exists := t.buffer[id]
if !exists {
return false
}
_, found := t.index.Get(tx.ComputeIndex(block))
return found
}
// Find searches and returns a transaction by its id if the node has it
// archived.
func (t *Transactions) Find(id TransactionID) *Transaction {
t.RLock()
defer t.RUnlock()
return t.buffer[id]
}
// BatchFind returns an error if one of the id does not exist.
func (t *Transactions) BatchFind(ids []TransactionID) ([]*Transaction, error) {
t.RLock()
defer t.RUnlock()
txs := make([]*Transaction, 0, len(ids))
for i := range ids {
tx, exist := t.buffer[ids[i]]
if !exist {
return nil, errors.Wrapf(ErrMissingTx, "%x", ids[i])
}
txs = append(txs, tx)
}
return txs, nil
}
// Len returns the total number of transactions that the node has archived.
func (t *Transactions) Len() int {
t.RLock()
defer t.RUnlock()
return len(t.buffer)
}
// PendingLen returns the number of transactions the node has archived that may be proposed
// into a block.
func (t *Transactions) PendingLen() int {
t.RLock()
defer t.RUnlock()
return t.index.Len()
}
// MissingLen returns the number of transactions that the node is looking to pull from
// its peers.
func (t *Transactions) MissingLen() int {
t.RLock()
defer t.RUnlock()
return len(t.missing)
}
// Iterate iterates through all transactions that the node has archived.
func (t *Transactions) Iterate(fn func(*Transaction) bool) {
t.RLock()
defer t.RUnlock()
for _, tx := range t.buffer {
if !fn(tx) {
return
}
}
}
// ProposableIDs returns a slice of IDs of transactions that may be wrapped
// into a block that may be proposed to be finalized within the network.
func (t *Transactions) ProposableIDs() []TransactionID {
t.RLock()
defer t.RUnlock()
limit := int(conf.GetBlockTXLimit())
if t.index.Len() < limit {
limit = t.index.Len()
}
proposable := make([]TransactionID, 0, limit)
t.index.Scan(func(key []byte, value interface{}) bool {
id := value.(TransactionID)
if t.buffer[id].Block <= t.latest.Index+1 {
proposable = append(proposable, id)
}
return len(proposable) < limit
})
return proposable
}
// MissingIDs returns a slice of IDs of transactions which the node would
// like to pull from its peers.
func (t *Transactions) MissingIDs() []TransactionID {
t.RLock()
defer t.RUnlock()
missing := make([]TransactionID, 0, len(t.missing))
for id := range t.missing {
missing = append(missing, id)
}
return missing
}