-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtransactionPool.py
More file actions
54 lines (47 loc) · 1.98 KB
/
transactionPool.py
File metadata and controls
54 lines (47 loc) · 1.98 KB
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
import numpy as np
from broadcast import broadcast
from transaction import Transaction
from utils import getTransmissionDelay, PriorityQueue
class TransactionPool:
"""Transaction pool for a miner"""
def __init__(self, env, identifier, neighbourList, nodes, params):
self.env = env
self.identifier = identifier
self.neighbourList = neighbourList
self.params = params
self.nodes = nodes
self.transactionQueue = PriorityQueue()
self.prevTransactions = []
def getTransaction(self, transactionCount):
"""Returns transactionCount number of Transactions. Returns
top transactions based on miner reward"""
return self.transactionQueue.get(transactionCount)
def popTransaction(self, transactionCount):
"""Remove transactions from transaction pool. Called when transactions
are added by a received block or a block is mined."""
poppedTransactions = self.transactionQueue.pop(transactionCount)
self.prevTransactions.append(poppedTransactions)
def putTransaction(self, transaction, sourceLocation):
"""Add received transaction to the transaction pool and broadcast further"""
destLocation = self.nodes[self.identifier].location
delay = getTransmissionDelay(sourceLocation, destLocation)
yield self.env.timeout(delay)
if (
not self.transactionQueue.isPresent(transaction)
and transaction not in self.prevTransactions
):
self.transactionQueue.insert(transaction)
broadcast(
self.env,
transaction,
"Transaction",
self.identifier,
self.neighbourList,
self.params,
nodes=self.nodes,
)
if self.params["verbose"] == "vv":
print(
"%7.4f : %s accepted by %s"
% (self.env.now, transaction.identifier, self.identifier)
)