-
Notifications
You must be signed in to change notification settings - Fork 0
/
attack.py
394 lines (302 loc) · 13.9 KB
/
attack.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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#!/usr/bin/env python3
from block import Block
from transactions import TXN
import numpy as np
import random
import sys
# Class for Attack Node
class AttackNode:
'''
NodeID: Unique Identifier for this Node
isSlow: Boolean value stating if the node is slow or fast
hashPower: Node's fraction of the total hashing power.
PoWI: The interarrival time between blocks on average
T_Tx: The mean interarrival time between transactions
status: Status of miner, possible status: {"free", "mining"}
cntSuccessfulBlocks: Count of blocks created by this node in the block tree (both public and private)
blocksTree: Dictionary of public blocks present in the Node's Blockchain Tree, Structure = { BlockID (BlockHash): { "arrival_time": ~ , "Block": Block Object }, ... }
leafBlocks: Dictionary of public blocks which are leaf nodes in the Node's Blockchain Tree, Structure = { BlockID (BlockHash): Block Object, ...}
peers: Contains information about the peers of the nodes, Structure = { Peer's NodeID : [ Peer's Node Object, propagation delay (rho_ij), link speed (c_ij) ], ... }
heardTXNs: Dictionary of all the Transactions which are heard by this node, Structure = { TXNID : TXN Object, ... }
privateChainExists: Boolean Value stating if private chain is active or not
privateChain: List of private blocks, Structure = [["timestamp" , Block Object ], []...]
lastBlock: Last block in the attacker's chain
futureEvents: Future events that may needed to be cancelled
atStateZero_: Boolean value stating whether the node is at State Zero Dash {0'}
'''
def __init__(self, nodeID, isSlow, hashPower, PoWI, T_Tx):
self.nodeID = nodeID
self.isSlow = isSlow
self.hashPower = hashPower
self.PoWI = PoWI
self.T_Tx = T_Tx
self.status = "free"
self.cntSuccessfulBlocks = 0
self.blocksTree = dict()
self.leafBlocks = dict()
self.peers = dict()
self.heardTXNs = dict()
self.privateChainExists = False
self.privateChain = []
self.lastBlock = None
self.futureEvents = []
self.atStateZero_ = False
# Calculating latency for transmitting a message to a connected peer
def calculate_latency(self, numberOfKBs, peerNodeID):
# Message Size in bits
messageSize = numberOfKBs * 8000
# Calculating various delays
propagation_delay = self.peers[peerNodeID][1]
link_speed = self.peers[peerNodeID][2]
# 96kbits = 96000
queueing_delay = np.random.exponential(scale=(96000 / link_speed))
# Total Latency
total_latency = propagation_delay + (messageSize / link_speed) + queueing_delay
return total_latency
# Calculating next transaction time gap
def next_create_transaction_delay(self):
return np.random.exponential(scale=self.T_Tx)
# Calculating POW time (T_k)
def calculate_POW_time(self):
if self.hashPower == 0:
return np.random.exponential(scale=(sys.maxsize))
return np.random.exponential(scale=(self.PoWI / self.hashPower))
# Create a random transaction with random amount
def create_transaction(self, nodeArray, timestamp):
# Choosing a random peer (can choose itself also, which is ok)
toNode = random.choice(nodeArray)
# Random amount from 1 to 10 coins
amount = random.randint(1, 10)
# Creating the TXN object
txn = TXN(timestamp, self.nodeID, toNode.nodeID, amount, False)
# Adding in seen transactions
self.heardTXNs[txn.TXNID] = txn
return txn
# Received a transaction from a peer
def receive_transaction(self, txn):
# Adding in seen transactions
self.heardTXNs[txn.TXNID] = txn
# Verifying the transaction
def verify_txn(self, nodeBalance, txn):
# This is a genesis block transaction (initial amount)
if txn.fromNode == -1:
nodeBalance[txn.toNode] += txn.amount
return True
# Adding and updating balances serially ensures no double spend happens within a block
if nodeBalance[txn.fromNode] >= txn.amount:
nodeBalance[txn.fromNode] -= txn.amount
nodeBalance[txn.toNode] += txn.amount
return True
else:
return False
# Create Block
def create_block(self, timestamp, nodeArray):
# If no private chain exists, create a new secret chain starting from highest depth publicly visible block
if not self.privateChainExists:
# Finding the Longest chain the Block Tree, Maximum depth leaf node, Arrival Time (To get first seen block)
longestChainLeaf = None
maxDepth = -1
arrivalTime = None
# To allow randomness in choosing 2 equal depth blocks (resolution of forks)
leafBlocksKeys = list(self.leafBlocks.keys())
random.shuffle(leafBlocksKeys)
for leafBlock in leafBlocksKeys:
if self.leafBlocks[leafBlock].depth > maxDepth or (self.leafBlocks[leafBlock].depth == maxDepth and self.blocksTree[leafBlock]["arrival_time"] < arrivalTime):
maxDepth = self.leafBlocks[leafBlock].depth
longestChainLeaf = self.leafBlocks[leafBlock]
arrivalTime = self.blocksTree[leafBlock]["arrival_time"]
# Getting transaction details about the longest chain
nodeBalance, transactionsInChain = self.get_details_chain(longestChainLeaf, nodeArray)
# Adding Randomness in TXN subset selection
heardTXNsKeys = list(self.heardTXNs.keys())
random.shuffle(heardTXNsKeys)
# Randomly selecting number of TXNs to added into the block, minimum = 0 transactions, maximum = min( number of transactions not included in any blocks in the longest chain, 999 ). 999 txns as 1MB max block size.
noOfTXNs = random.randint(0, min(len(heardTXNsKeys) - len(transactionsInChain), 999))
transactions = []
# Adding the coinbase transaction, at 0th index
transactions.append(TXN(timestamp, None, self.nodeID, 50, True))
cnt = 0
while(noOfTXNs and cnt < len(heardTXNsKeys)):
# If transaction is not included in any blocks in the longest chain and If transaction is valid then add to the block
if (heardTXNsKeys[cnt] not in transactionsInChain) and self.verify_txn(nodeBalance, self.heardTXNs[heardTXNsKeys[cnt]]):
transactions.append(self.heardTXNs[heardTXNsKeys[cnt]])
noOfTXNs -= 1
cnt += 1
# Create block based on transactions and longest chain leaf node
block = Block(timestamp, longestChainLeaf, False, transactions)
# Mining starts
self.status = "mining"
self.lastBlock = longestChainLeaf
return block
else:
# If private chain exists then building on the last block of the private chain as the lead is >= 1 (private chain is the longest one)
longestChainLeaf = self.lastBlock
# Getting transaction details about the longest chain
nodeBalance, transactionsInChain = self.get_details_chain(longestChainLeaf, nodeArray)
# Adding Randomness in TXN subset selection
heardTXNsKeys = list(self.heardTXNs.keys())
random.shuffle(heardTXNsKeys)
# Randomly selecting number of TXNs to added into the block, minimum = 0 transactions, maximum = min( number of transactions not included in any blocks in the longest chain, 999 ). 999 txns as 1MB max block size.
noOfTXNs = random.randint(0, min(len(heardTXNsKeys) - len(transactionsInChain), 999))
transactions = []
# Adding the coinbase transaction, at 0th index
transactions.append(TXN(timestamp, None, self.nodeID, 50, True))
cnt = 0
while(noOfTXNs and cnt < len(heardTXNsKeys)):
# If transaction is not included in any blocks in the longest chain and If transaction is valid then add to the block
if (heardTXNsKeys[cnt] not in transactionsInChain) and self.verify_txn(nodeBalance, self.heardTXNs[heardTXNsKeys[cnt]]):
transactions.append(self.heardTXNs[heardTXNsKeys[cnt]])
noOfTXNs -= 1
cnt += 1
# Create block based on transactions and longest chain leaf node
block = Block(timestamp, longestChainLeaf, False, transactions)
# Mining starts
self.status = "mining"
return block
# Create Block at state zero dash {0'}
def create_block_at_state_zero_dash(self, timestamp, nodeArray):
# At state 0', racing condition b/w honest's and attacker's chain arrives. The selfish miner continues to mine on top of his own block.
attackerBlock = self.lastBlock
# Getting transaction details about the chain
nodeBalance, transactionsInChain = self.get_details_chain(attackerBlock, nodeArray)
# Adding Randomness in TXN subset selection
heardTXNsKeys = list(self.heardTXNs.keys())
random.shuffle(heardTXNsKeys)
# Randomly selecting number of TXNs to added into the block, minimum = 0 transactions, maximum = min( number of transactions not included in any blocks in the chain, 999 ). 999 txns as 1MB max block size.
noOfTXNs = random.randint(0, min(len(heardTXNsKeys) - len(transactionsInChain), 999))
transactions = []
# Adding the coinbase transaction, at 0th index
transactions.append(TXN(timestamp, None, self.nodeID, 50, True))
cnt = 0
while(noOfTXNs and cnt < len(heardTXNsKeys)):
# If transaction is not included in any blocks in the chain and If transaction is valid then add to the block
if (heardTXNsKeys[cnt] not in transactionsInChain) and self.verify_txn(nodeBalance, self.heardTXNs[heardTXNsKeys[cnt]]):
transactions.append(self.heardTXNs[heardTXNsKeys[cnt]])
noOfTXNs -= 1
cnt += 1
# Create block based on transactions and attacker's block
block = Block(timestamp, attackerBlock, False, transactions)
# Mining starts
self.status = "mining"
return block
# Broadcast Block at state zero dash {0'}
def broadcast_block_at_state_zero_dash(self, block, timestamp):
# Finding the Longest chain depth the Block Tree, to verify if it is still the longest
maxDepth = -1
parentBlockHash = block.prevBlockHash
for leafBlock in self.leafBlocks.keys():
if self.leafBlocks[leafBlock].depth > maxDepth:
maxDepth = self.leafBlocks[leafBlock].depth
# Checking the block still extends the longest, if honest miners win, then this will fail
if(maxDepth > block.previousBlock.depth):
# Free from Mining
self.status = "free"
self.privateChainExists = False
self.privateChain = []
self.lastBlock = None
self.atStateZero_ = False
return False
# Adding block to public block Tree
self.blocksTree[block.blockHash] = { "arrival_time": timestamp, "Block": block }
# Removing parent block as leaf node and adding current block as leaf node
self.leafBlocks.pop(parentBlockHash)
self.leafBlocks[block.blockHash] = block
# Free from Mining
self.status = "free"
self.privateChainExists = False
self.privateChain = []
self.lastBlock = None
self.atStateZero_ = False
# Incrementing the count as the block is successfully created
self.cntSuccessfulBlocks += 1
return True
# Add Block to private chain
def finished_block(self, block, timestamp):
# Finding the Longest chain depth the Block Tree, to verify if it is still the longest
maxDepth = -1
parentBlockHash = block.prevBlockHash
for leafBlock in self.leafBlocks.keys():
if self.leafBlocks[leafBlock].depth > maxDepth:
maxDepth = self.leafBlocks[leafBlock].depth
# Checking the block still extends the longest
if(maxDepth > block.previousBlock.depth):
# The LVC exceeds the length of the selfish miner’s private chain
# Free from Mining
self.status = "free"
self.privateChainExists = False
self.privateChain = []
self.lastBlock = None
self.atStateZero_ = False
return False
# Free from Mining
self.status = "free"
# Incrementing the count as the block is successfully created
self.cntSuccessfulBlocks += 1
# Adding the block into the private chain
self.privateChainExists = True
self.privateChain.append([timestamp, block])
self.lastBlock = block
self.atStateZero_ = False
return True
# Validate Block
def validate_block(self, timestamp, block, nodeArray):
# Getting parent block of the node
parentBlock = block.previousBlock
# If parent not in block tree, return false (cannot validate)
if parentBlock.blockHash not in self.blocksTree:
return False
# Getting transaction details about the parent block
nodeBalance, transactionsInChain = self.get_details_chain(parentBlock, nodeArray)
# Validating the Transactions
for txn in block.transactions:
# If coinbase transaction amount is not 50, return False
if txn.isCoinbase:
if txn.amount != 50:
return False
continue
# If transaction invalid, return False
if not self.verify_txn(nodeBalance, txn):
return False
# All transactions Validated
# Adding block into the block tree
self.blocksTree[block.blockHash] = { "arrival_time": timestamp, "Block": block }
# Add block as a leaf node (no block pointing to the current block), and remove parent as leaf node as current block is pointing towards it
if parentBlock.blockHash in self.leafBlocks:
self.leafBlocks.pop(parentBlock.blockHash)
self.leafBlocks[block.blockHash] = block
else:
self.leafBlocks[block.blockHash] = block
# Adding transactions in block into heardTXNs
for txn in block.transactions:
if not txn.isCoinbase:
self.heardTXNs[txn.TXNID] = txn
return True
# Get all the transaction details about the chain from a block
def get_details_chain(self, block, nodeArray):
# Initialize an empty list to store transactions seen in the chain.
transactionsInChain = []
# Initialize an dictionary to store node balances.
nodeBalance = dict()
for node in nodeArray:
nodeBalance[node.nodeID] = 0
nodeBalance[-1] = 0
# Continue looping indefinitely until condition is met.
currBlock = block
while currBlock:
# Get the transactions from the current block.
transactions = currBlock.transactions
# Iterate through each transaction in the block.
for txn in transactions:
sender = txn.fromNode
recipient = txn.toNode
amount = txn.amount
# If the transaction is not a coinbase transaction, update the node balances accordingly.
if not txn.isCoinbase:
# Add the transaction to the list of transactions in the chain.
transactionsInChain.append(txn.TXNID)
nodeBalance[sender] -= amount
nodeBalance[recipient] += amount
# Update the current block to be the parent block for the next iteration. If genesis block then parent will be None, so loop exit
parentBlock = currBlock.previousBlock
currBlock = parentBlock
return nodeBalance, transactionsInChain