-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathattack-selfish.py
197 lines (143 loc) · 5.32 KB
/
attack-selfish.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
"""
bitcoin network simulator - btcsim
Copyright (C) 2013 Rafael Brune <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 and
only version 2 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
"""
import os
import sys
import numpy
import pylab
from heapq import *
from btcsim import *
class BadMiner(Miner):
chain_head_others = '*'
privateBranchLen = 0
def add_block(self, t_block):
self.blocks[hash(t_block)] = t_block
if (self.chain_head == '*'):
self.chain_head = hash(t_block)
self.chain_head_others = hash(t_block)
self.mine_block()
return
if (t_block.miner_id == self.miner_id) and (t_block.height > self.blocks[self.chain_head].height):
delta_prev = self.blocks[self.chain_head].height - self.blocks[self.chain_head_others].height
self.chain_head = hash(t_block)
self.privateBranchLen += 1
if (delta_prev == 0) and (self.privateBranchLen == 2):
self.announce_block(self.chain_head)
self.privateBranchLen = 0
self.mine_block()
if (t_block.miner_id != self.miner_id) and (t_block.height > self.blocks[self.chain_head_others].height):
delta_prev = self.blocks[self.chain_head].height - self.blocks[self.chain_head_others].height
self.chain_head_others = hash(t_block)
if delta_prev <= 0:
self.chain_head = hash(t_block)
self.privateBranchLen = 0
elif delta_prev == 1:
self.announce_block(self.chain_head)
elif delta_prev == 2:
self.announce_block(self.chain_head)
self.privateBranchLen = 0
else:
iter_hash = self.chain_head
# the temp is in case we get too far ahead (in case we have >51%)
temp = 0
if delta_prev >= 6: temp = 1
while self.blocks[iter_hash].height != t_block.height + temp:
iter_hash = self.blocks[iter_hash].prev
self.announce_block(iter_hash)
self.mine_block()
t = 0.0
event_q = []
# root block
seed_block = Block(None, 0, t, -1, 0, 1)
# set up some miners with random hashrate
numminers = 6
hashrates = numpy.random.exponential(1.0, numminers)
# setup very strong miner
attacker_strength = 0.30
hashrates[numminers-1] = 0.0
hashrates[numminers-1] = hashrates.sum() * (attacker_strength/(1.0 - attacker_strength))
hashrates = hashrates/hashrates.sum()
miners = []
for i in range(numminers):
miners.append(Miner(i, hashrates[i] * 1.0/600.0, 200*1024, seed_block, event_q, t))
# make the strong miner a bad miner
miners[i] = BadMiner(i, hashrates[i] * 1.0/600.0, 200*1024, seed_block, event_q, t)
# add some random links to each miner
for i in range(numminers):
for k in range(4):
j = numpy.random.randint(0, numminers)
if i != j:
latency = 0.020 + 0.200*numpy.random.random()
bandwidth = 10*1024 + 200*1024*numpy.random.random()
miners[i].add_link(j, latency, bandwidth)
miners[j].add_link(i, latency, bandwidth)
# simulate some days of block generation
curday = 0
maxdays = 5*7*24*60*60
while t < maxdays:
t, t_event = heappop(event_q)
#print('%08.3f: %02d->%02d %s' % (t, t_event.orig, t_event.dest, t_event.action), t_event.payload)
miners[t_event.dest].receive_event(t, t_event)
if t/(24*60*60) > curday:
print('day %03d' % curday)
curday = int(t/(24*60*60))+1
# data analysis
pylab.figure()
cols = ['r-', 'g-', 'b-', 'y-']
mine = miners[0]
t_hash = mine.chain_head
rewardsum = 0.0
for i in range(numminers):
miners[i].reward = 0.0
main_chain = dict()
main_chain[hash(seed_block)] = 1
while t_hash != None:
t_block = mine.blocks[t_hash]
if t_hash not in main_chain:
main_chain[t_hash] = 1
miners[t_block.miner_id].reward += 1
rewardsum += 1
if t_block.prev != None:
pylab.plot([mine.blocks[t_block.prev].time, t_block.time], [mine.blocks[t_block.prev].height, t_block.height], cols[t_block.miner_id%4])
t_hash = t_block.prev
pylab.xlabel('time in s')
pylab.ylabel('block height')
pylab.draw()
pylab.figure()
pylab.plot([0, numpy.max(hashrates)*1.05], [0, numpy.max(hashrates)*1.05], '-', color='0.4')
for i in range(numminers):
#print('%2d: %0.3f -> %0.3f' % (i, hashrates[i], miners[i].reward/rewardsum))
pylab.plot(hashrates[i], miners[i].reward/rewardsum, 'k.')
pylab.plot(hashrates[i], miners[i].reward/rewardsum, 'rx')
pylab.xlabel('hashrate')
pylab.ylabel('reward')
pylab.figure()
orphans = 0
for i in range(numminers):
for t_hash in miners[i].blocks:
if t_hash not in main_chain:
orphans += 1
# draws the chains
if miners[i].blocks[t_hash].height > 1:
cur_b = miners[i].blocks[t_hash]
pre_b = miners[i].blocks[cur_b.prev]
pylab.plot([hashrates[pre_b.miner_id], hashrates[cur_b.miner_id]], [pre_b.height, cur_b.height], 'k-')
pylab.ylabel('block height')
pylab.xlabel('hashrate')
pylab.ylim([0, 100])
print('Orphaned blocks: %d (%0.3f)' % (orphans, orphans/mine.blocks[mine.chain_head].height))
print('Average block height time: %0.3f min' % (mine.blocks[mine.chain_head].time/(60*mine.blocks[mine.chain_head].height)))
pylab.draw()
pylab.show()