-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathminer.py
More file actions
147 lines (131 loc) · 5.4 KB
/
miner.py
File metadata and controls
147 lines (131 loc) · 5.4 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
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
"""
Implementation of custom miner resource
"""
import simpy
import numpy as np
from block import Block
from fullNode import FullNode
from broadcast import broadcast
from utils import getBlockDelay, getTransmissionDelay
from transactionPool import TransactionPool
class Miner(FullNode):
"""docstring for Miner"""
def __init__(
self,
identifier,
env,
neighbourList,
pipes,
nodes,
location,
blockPropData,
params,
):
FullNode.__init__(
self,
identifier,
env,
neighbourList,
pipes,
nodes,
location,
blockPropData,
params,
)
self.blockGeneratorAction = self.env.process(self.blockGenerator(params))
def blockGenerator(self, params):
"""Block generator"""
while True:
try:
delay = getBlockDelay(self.params["blockMu"], self.params["blockSigma"])
yield self.env.timeout(delay)
transactionCount = int(params["blockCapacity"])
transactionList = self.transactionPool.getTransaction(transactionCount)
l = []
for transaction in transactionList:
transaction.miningTime = self.env.now
"""Simulating Tx validation time"""
self.env.timeout(0.1)
l.append(transaction.identifier)
b = Block("B" + str(self.currentBlockID), transactionList, params)
"""Collection of data"""
"""If block has been mined earlier, inrease count of fork"""
if b.identifier in [
id[: id.index("_")] for id in self.data["blockProp"].keys()
]:
self.data["numForks"] += 1
print(
"%7.4f" % self.env.now
+ " : %s proposing %s with transaction list count %d with transactions %s ..."
% (self.identifier, b.identifier, len(l), l[:3])
)
if bool(self.params["verbose"]):
print(
"%7.4f" % self.env.now
+ " : %s" % self.identifier
+ " generated Block %s" % b.identifier
)
self.blockchain.append(b)
if bool(self.params["verbose"]):
self.displayChain()
"""Mark the block creation time"""
if b.hash not in self.data["blockProp"].keys():
self.data["blockProp"][b.hash] = [self.env.now, self.env.now]
"""Broadcast block to all neighbours"""
broadcast(
self.env,
b,
"Block",
self.identifier,
self.neighbourList,
self.params,
pipes=self.pipes,
nodes=self.nodes,
)
"""Remove transactions from local pool"""
self.transactionPool.popTransaction(transactionCount)
self.currentBlockID += 1
except simpy.Interrupt:
if bool(self.params["verbose"]):
print(
"%7.4f" % self.env.now
+ " : "
+ "%s" % self.identifier
+ " interrupted. To mine block %s" % (self.currentBlockID + 1)
+ " now"
)
self.currentBlockID += 1
def receiveBlock(self):
"""Receive newly mined block from neighbour"""
while True:
b = yield self.pipes[self.identifier].get()
if len(self.blockchain) > 0:
currID = int(self.blockchain[-1].identifier[1:])
else:
currID = -1
currIDs = [x.identifier for x in self.blockchain]
if int(b.identifier[1:]) == currID + 1 and b.identifier not in currIDs:
"""Interrupt block generation"""
self.blockGeneratorAction.interrupt()
"""Remove already mined transactions from private pool"""
for transaction in b.transactionList:
if self.transactionPool.transactionQueue.isPresent(transaction):
self.transactionPool.transactionQueue.remove(transaction)
self.transactionPool.prevTransactions.append(transaction)
"""Append block to own chain"""
self.blockchain.append(b)
if bool(self.params["verbose"]):
print(
"%7.4f" % self.env.now
+ " : "
+ "%s" % self.identifier
+ " added Block %s" % b.identifier
+ " to the chain"
)
self.displayChain()
"""Mark the block receive time"""
self.data["blockProp"][b.hash][1] = self.env.now
else:
"""If an invalid block is received, check neighbours and update
the chain if a longer chain is found"""
self.updateBlockchain()