forked from febe19/bazo-miner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblockpreparation.go
365 lines (317 loc) · 11.3 KB
/
blockpreparation.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package miner
import (
"encoding/binary"
"github.com/julwil/bazo-miner/p2p"
"github.com/julwil/bazo-miner/protocol"
"github.com/julwil/bazo-miner/storage"
"sort"
"time"
)
//The code here is needed if a new block is built. All open (not yet validated) transactions are first fetched
//from the mempool and then sorted. The sorting is important because if transactions are fetched from the mempool
//they're received in random order (because it's implemented as a map). However, if a user wants to issue more fundsTxs
//they need to be sorted according to increasing txCnt, this greatly increases throughput.
type openTxs []protocol.Transaction
var (
receivedBlockInTheMeantime bool
nonAggregatableTxCounter int
blockSize int
transactionHashSize int
)
func prepareBlock(block *protocol.Block) {
//Fetch all txs from mempool (opentxs).
opentxs := storage.ReadAllOpenTxs()
opentxs = append(opentxs, storage.ReadAllINVALIDOpenTx()...)
var opentxToAdd []protocol.Transaction
//This copy is strange, but seems to be necessary to leverage the sort interface.
//Shouldn't be too bad because no deep copy.
var tmpCopy openTxs
tmpCopy = opentxs
sort.Sort(tmpCopy)
//Counter for all transactions which will not be aggregated. (Stake-, config-, acct, deletetx)
nonAggregatableTxCounter = 0
blockSize = int(activeParameters.BlockSize) - (activeParameters.FixedSpace + activeParameters.BloomFilterSize)
transactionHashSize = protocol.TRANSACTION_HASH_SIZE // in bytes
//map where all senders from FundsTx and AggTx are added to. --> this ensures that tx with same sender are only counted once.
storage.DifferentSenders = map[[protocol.TRANSACTION_SENDER_SIZE]byte]uint32{}
storage.DifferentReceivers = map[[protocol.TRANSACTION_RECEIVER_SIZE]byte]uint32{}
storage.FundsTxBeforeAggregation = nil
type senderTxCounterForMissingTransactions struct {
senderAddress [protocol.TRANSACTION_SENDER_SIZE]byte
txcnt uint32
missingTransactions []uint32
}
var missingTxCntSender = map[[protocol.TRANSACTION_SENDER_SIZE]byte]*senderTxCounterForMissingTransactions{}
//Get Best combination of transactions
opentxToAdd = checkBestCombination(opentxs)
//Search missing transactions for the transactions which will be added...
for _, tx := range opentxToAdd {
switch tx.(type) {
case *protocol.FundsTx:
trx := tx.(*protocol.FundsTx)
//Create Mininmal txCnt for the different senders with stateTxCnt.. This is used to fetch missing transactions later on.
if missingTxCntSender[trx.From] == nil {
if storage.State[trx.From] != nil {
if storage.State[trx.From].TxCnt == 0 {
missingTxCntSender[trx.From] = &senderTxCounterForMissingTransactions{trx.From, 0, nil}
} else {
missingTxCntSender[trx.From] = &senderTxCounterForMissingTransactions{trx.From, storage.State[trx.From].TxCnt - 1, nil}
}
}
}
if missingTxCntSender[trx.From] != nil {
for i := missingTxCntSender[trx.From].txcnt + 1; i < trx.TxCnt; i++ {
if i == 1 {
missingTxCntSender[trx.From].missingTransactions = append(missingTxCntSender[trx.From].missingTransactions, 0)
}
missingTxCntSender[trx.From].missingTransactions = append(missingTxCntSender[trx.From].missingTransactions, i)
}
if trx.TxCnt > missingTxCntSender[trx.From].txcnt {
missingTxCntSender[trx.From].txcnt = trx.TxCnt
}
}
}
}
//Special Request for transactions missing between the Tx with the lowest TxCnt and the state.
// With this transactions may are validated quicker.
for _, sender := range missingTxCntSender {
//This limits the searching process to teh block interval * TX_FETCH_TIMEOUT
if len(missingTxCntSender[sender.senderAddress].missingTransactions) > int(activeParameters.BlockInterval) {
missingTxCntSender[sender.senderAddress].missingTransactions = missingTxCntSender[sender.senderAddress].missingTransactions[0:int(activeParameters.BlockInterval)]
}
if len(missingTxCntSender[sender.senderAddress].missingTransactions) > 0 {
logger.Printf("Missing Transaction: All these Transactions are missing for sender %x: %v ", sender.senderAddress[0:8], missingTxCntSender[sender.senderAddress].missingTransactions)
}
for _, missingTxcnt := range missingTxCntSender[sender.senderAddress].missingTransactions {
var missingTransaction protocol.Transaction
//Abort requesting if a block is received in the meantime
if receivedBlockInTheMeantime {
logger.Printf("Received Block in the Meantime --> Abort requesting missing Tx (1)")
break
}
//Search Tx in the local storage, if it may is received in the meantime.
for _, txhash := range storage.ReadTxcntToTx(missingTxcnt) {
tx := storage.ReadOpenTx(txhash)
if tx != nil {
if tx.Sender() == sender.senderAddress {
missingTransaction = tx
break
}
} else {
tx = storage.ReadINVALIDOpenTx(txhash)
if tx != nil {
if tx.Sender() == sender.senderAddress {
missingTransaction = tx
break
}
} else {
tx = storage.ReadClosedTx(txhash)
if tx != nil {
if tx.Sender() == sender.senderAddress {
missingTransaction = tx
break
}
}
}
}
}
//Try to fetch the transaction form the network, if it is not received until now.
if missingTransaction == nil {
var requestTx = specialTxRequest{sender.senderAddress, p2p.SPECIALTX_REQ, missingTxcnt}
payload := requestTx.Encoding()
//Special Request can be received through the fundsTxChan.
err := p2p.TxWithTxCntReq(payload, p2p.SPECIALTX_REQ)
if err != nil {
continue
}
select {
case trx := <-p2p.FundsTxChan:
//If correct transaction is received, write to openStorage and good, if wrong one is received, break.
if trx.TxCnt != missingTxcnt && trx.From != sender.senderAddress {
logger.Printf("Missing Transaction: Received Wrong Transaction")
break
} else {
storage.WriteOpenTx(trx)
missingTransaction = trx
break
}
case <-time.After(TXFETCH_TIMEOUT * time.Second):
stash := p2p.ReceivedFundsTXStash
//Try to find missing transaction in the stash...
for _, trx := range stash {
if trx.From == sender.senderAddress && trx.TxCnt == missingTxcnt {
storage.WriteOpenTx(trx)
missingTransaction = trx
break
}
}
if missingTransaction == nil {
logger.Printf("Missing Transaction: Tx Request Timed out...")
}
break
}
}
if missingTransaction == nil {
logger.Printf("Missing txcnt %v not found", missingTxcnt)
} else {
opentxToAdd = append(opentxToAdd, missingTransaction)
}
}
//If Block is receifed before, break now.
if receivedBlockInTheMeantime {
logger.Printf("Received Block in the Meantime --> Abort requesting missing Tx (2)")
receivedBlockInTheMeantime = false
break
}
}
missingTxCntSender = nil
//Sort Tx Again to get lowest TxCnt at the beginning.
tmpCopy = opentxToAdd
sort.Sort(tmpCopy)
//Add previous selected transactions.
for _, tx := range opentxToAdd {
err := addTx(block, tx)
if err != nil {
//If the tx is invalid, we remove it completely, prevents starvation in the mempool.
storage.DeleteOpenTx(tx)
}
}
// In miner\block.go --> AddFundsTx the transactions get added into storage.TxBeforeAggregation.
if len(storage.ReadFundsTxBeforeAggregation()) > 0 {
splitSortedAggregatableTransactions(block)
}
//Set measurement values back to zero / nil.
storage.DifferentSenders = nil
storage.DifferentReceivers = nil
nonAggregatableTxCounter = 0
return
}
func checkBestCombination(openTxs []protocol.Transaction) (TxToAppend []protocol.Transaction) {
nrWhenCombinedBest := 0
moreOpenTx := true
for moreOpenTx {
var intermediateTxToAppend []protocol.Transaction
for i, tx := range openTxs {
switch tx.(type) {
case *protocol.FundsTx:
storage.DifferentSenders[tx.(*protocol.FundsTx).From] = storage.DifferentSenders[tx.(*protocol.FundsTx).From] + 1
storage.DifferentReceivers[tx.(*protocol.FundsTx).To] = storage.DifferentReceivers[tx.(*protocol.FundsTx).To] + 1
case *protocol.AggTx:
continue
default:
//If another non-FundsTx can fit into the block, add it, else block is already full, so return the tx
//This does help that non-FundsTx get validated as fast as possible.
if (nonAggregatableTxCounter+1)*transactionHashSize < blockSize {
nonAggregatableTxCounter += 1
TxToAppend = append(TxToAppend, tx)
if i != len(openTxs) {
openTxs = append(openTxs[:i], openTxs[i+1:]...)
}
} else {
return TxToAppend
}
}
}
maxSender, addressSender := getMaxKeyAndValueFormMap(storage.DifferentSenders)
maxReceiver, addressReceiver := getMaxKeyAndValueFormMap(storage.DifferentReceivers)
i := 0
if maxSender >= maxReceiver {
for _, tx := range openTxs {
switch tx.(type) {
case *protocol.FundsTx:
//Append Tx To the ones which get added, else remove added tx such that no space exists.
if tx.(*protocol.FundsTx).From == addressSender {
intermediateTxToAppend = append(intermediateTxToAppend, tx)
} else {
openTxs[i] = tx
i++
}
}
}
} else {
for _, tx := range openTxs {
switch tx.(type) {
case *protocol.FundsTx:
if tx.(*protocol.FundsTx).To == addressReceiver {
intermediateTxToAppend = append(intermediateTxToAppend, tx)
} else {
openTxs[i] = tx
i++
}
}
}
}
openTxs = openTxs[:i]
storage.DifferentSenders = make(map[[32]byte]uint32)
storage.DifferentReceivers = make(map[[32]byte]uint32)
nrWhenCombinedBest = nrWhenCombinedBest + 1
//Stop when block is full
if (nrWhenCombinedBest+nonAggregatableTxCounter)*transactionHashSize >= blockSize {
moreOpenTx = false
break
} else {
TxToAppend = append(TxToAppend, intermediateTxToAppend...)
}
//Stop when list is empty
if len(openTxs) > 0 {
//If adding a new transaction combination, gets bigger than the blocksize, abort
moreOpenTx = true
} else {
moreOpenTx = false
}
}
return TxToAppend
}
type specialTxRequest struct {
senderHash [32]byte
reqType uint8
txcnt uint32
}
func (R *specialTxRequest) Encoding() (encodedTx []byte) {
// Encode
if R == nil {
return nil
}
var txcnt [8]byte
binary.BigEndian.PutUint32(txcnt[:], R.txcnt)
encodedTx = make([]byte, 42)
encodedTx[0] = R.reqType
copy(encodedTx[1:9], txcnt[:])
copy(encodedTx[10:42], R.senderHash[:])
return encodedTx
}
//Implement the sort interface
func (f openTxs) Len() int {
return len(f)
}
func (f openTxs) Swap(i, j int) {
f[i], f[j] = f[j], f[i]
}
func (f openTxs) Less(i, j int) bool {
//Comparison only makes sense if both tx are fundsTxs.
//Why can we only do that with switch, and not e.g., if tx.(type) == ..?
switch f[i].(type) {
case *protocol.AccTx:
//We only want to sort a subset of all transactions, namely all fundsTxs.
//However, to successfully do that we have to place all other txs at the beginning.
//The order between accTxs and configTxs doesn't matter.
return true
case *protocol.ConfigTx:
return true
case *protocol.StakeTx:
return true
case *protocol.AggTx:
return true
}
switch f[j].(type) {
case *protocol.AccTx:
return false
case *protocol.ConfigTx:
return false
case *protocol.StakeTx:
return false
case *protocol.AggTx:
return false
}
return f[i].(*protocol.FundsTx).TxCnt < f[j].(*protocol.FundsTx).TxCnt
}