-
Notifications
You must be signed in to change notification settings - Fork 0
/
epool.py
93 lines (66 loc) · 2.15 KB
/
epool.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 20 13:11:12 2018
@author: aviasayag
"""
import transaction
import math
import random
"""
Each node from the nodesList is adding its own transactions (by its portion)
to the global-Epool. Returns the global epool
"""
def createGlobalEpool(nodesList, epoolSize):
epool = []
totalTxCreated = 0
nodesNum = len(nodesList)
for node in nodesList:
txNum = math.floor(node.portion * epoolSize)
for i in range(txNum):
epool.append(transaction.Transaction(node.index))
totalTxCreated += txNum
# check if need to fill more transaction to reach EPOOL_SIZE
txLeft = epoolSize - totalTxCreated
if txLeft > 0:
for i in range(txLeft):
nodeIndex = i % nodesNum
epool.append(transaction.Transaction(nodeIndex))
return epool
"""
Each node chooses extra txs by its epoolSample
"""
def chooseTXs(nodesList, globalEpool):
for node in nodesList:
for epoolTx in globalEpool:
# enter node's own txs
if epoolTx.source == node.index:
node.epool.append(epoolTx)
else:
# enter other nodes txs in probability
if random.random() < node.sample:
node.epool.append(epoolTx)
node.epoolSize = len(node.epool)
"""
Get node and threshold and return all the txs from the node's epool that
their hash value is lower than the threshold
"""
def sliceEpoolByThreshold(node, threshold):
epool = node.epool
slicedEpool = []
for tx in epool:
if tx.hashInt < threshold:
slicedEpool.append(tx)
return slicedEpool
"""
returns a list where each cell index represents a node and
in the cell is the real portion of the node's distribution
in the input epool
"""
def getNodesDistribution(epool, nodesNum):
totalTxsList = [0] * nodesNum
epoolSize = 0
for tx in epool:
totalTxsList[tx.source] += 1
epoolSize += 1
return [tx/epoolSize for tx in totalTxsList]