-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
72 lines (54 loc) · 1.8 KB
/
utils.py
File metadata and controls
72 lines (54 loc) · 1.8 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import numpy as np
import json
def getBlockDelay(mu, sigma):
delay = mu + sigma * np.random.randn()
if delay < 0:
return getBlockDelay(mu, sigma)
return delay
def getTransactionDelay(mu, sigma):
delay = mu + sigma * np.random.randn()
if delay < 0:
return getTransactionDelay(mu, sigma)
return delay
def getTransmissionDelay(source, destination):
with open("params/params.json", "r") as f:
params = f.read()
params = json.loads(params)
mu = params["delay"][source][destination]["mu"]
sigma = params["delay"][source][destination]["sigma"]
delay = mu + sigma * np.random.randn()
if delay < 0:
return getTransmissionDelay(source, destination)
return delay
"""Custom Priority Queue implementation to maintain the order of
transactions to be mined by a miner, with key as the miner reward"""
class PriorityQueue:
def __init__(self):
self.queue = []
def isEmpty(self):
return len(self.queue) == []
def isPresent(self, transaction):
return transaction in self.queue
def length(self):
return len(self.queue)
def insert(self, newTransaction):
indx = 0
while (
indx < len(self.queue) and self.queue[indx].reward >= newTransaction.reward
):
indx += 1
self.queue.insert(indx, newTransaction)
def pop(self, count):
count = min(self.length(), count)
elements = self.queue[:count]
self.queue = self.queue[count:]
return elements
def remove(self, transaction):
self.queue.remove(transaction)
def get(self, count):
count = min(self.length(), count)
return self.queue[:count]
def display(self):
for tx in self.queue:
print(tx.identifier, end=" ")
print()