-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path[Simulation] Selfish mining simulation.py
More file actions
226 lines (171 loc) · 5.99 KB
/
Copy path[Simulation] Selfish mining simulation.py
File metadata and controls
226 lines (171 loc) · 5.99 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
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
#-*- coding: utf-8 -*-
from fractions import Fraction
import sys
import random
import copy
import numpy as np
import matplotlib.pyplot as plt
def randomIdx(dist):
l = len(dist)
r = random.random()
for i in range(0,l):
if r < dist[i]:
ret = i
break
else:
r = r - dist[i]
ret = i
return ret
"""
Detective mining simulation function Based on a state machine
alpha : the selfish mining pool's mining power
delta : the detevtive mining pool's mining power
Therefore, (1-alpha-delta) : honest mining pools' mining power
blocks : The number of blocks will be generated conceptually
"""
def detectiveMining(alpha, delta, gamma, blocks):
# gamma is an always fair value
# for example, 2 -> 1/2 , 3-> 1/3 ... etc
#leakage_ratio = delta/(1-alpha)
dist = [alpha, delta, (1-alpha-delta)]
# State initialization
# State follows the state machine figure in my research paper
state = 0
# Block initialization
# The number of block each miner gets
block = [0,0,0]
# Rate initialization
# The proportion of block each miner gets
rate = [0,0,0]
# Relative Revenue initialization
# The proportion of block each miner gets
relativeRevenue = [0,0,0]
for i in range(0, blocks):
miner = randomIdx(dist)
# debug
# print(miner)
# State -1 means State 0' for simplicity
if state == -1 :
if miner == 0:
block[0] += 2
state = 0
# miners should select a block among fork chains
if miner == 1:
select = randomIdx([gamma, (1-gamma)])
if select == 0:
block[0] += 1
block[1] += 1
state = 0
else:
block[1] += 1
block[2] += 1
state = 0
if miner == 2:
select = randomIdx([gamma, (1-gamma)])
if select == 0:
block[0] += 1
block[2] += 1
state = 0
else:
block[2] += 2
state = 0
elif state == 0 :
if miner == 0:
block[0] += 0
state = 1
if miner == 1:
block[1] += 1
state = 0
if miner == 2:
block[2] += 1
state = 0
elif state == 1 :
if miner == 0:
block[0] += 0
state = 2
if miner == 1:
block[0] += 1
block[1] += 1
state = 0
if miner == 2:
block[2] += 0
state = -1
elif state == 2 :
if miner == 0:
block[0] += 0
state = 3
if miner == 1:
block[0] += 2
block[1] += 1
state = 0
if miner == 2:
block[0] += 2
state = 0
# State > 2
else:
if miner == 0:
block[0] += 0
state += 1
if miner == 1:
block[0] += state
block[1] += 1
state = 0
if miner == 2:
# State is decreased but the block will be a profit of the selfish miner
block[0] += 1
state -= 1
# If the selfish miner has a private chain, then it should be taken in the result
if state > 0:
block[0] += state
# Counting block generation late
sum = 0
for num in block:
sum += num
for i in range(0,3):
rate[i] = block[i]/sum
# Evalutate Relative Revenue
for i in range(0,3):
if dist[i] == 0:
relativeRevenue[i] == 0
else:
relativeRevenue[i] = rate[i]/dist[i]
# debug
# print('[+] A blocks finished')
return (relativeRevenue)
"""
Selfish mining simulation function Based on a state machine
alpha : the selfish mining pool's mining power
Therefore, (1-alpha) : honest mining pools' mining power
blocks : The number of blocks will be generated conceptually
It is the case when there is no detective miners.
"""
def selfishMining(alpha, gamma, blocks):
rate = detectiveMining(alpha, 0, gamma, blocks)
return [rate[0], rate[2]]
# main function for module test
if __name__=='__main__':
# alpha = 0.48
# delta = 0.48
# gamma = 0.5
# blocks = 10000
# rate = selfishMining(0.3, 0.5, 1000)
# rate = detectiveMining(alpha, delta, gamma, blocks)
# print(rate)
density = 10
A, B = np.meshgrid(np.linspace(0, 0.5, density), np.linspace(0, 1, density))
selfish_profit = np.zeros((density, density))
honest_profit = np.zeros((density, density))
blocks = 10000
for i in range(0, density) :
print('[+] blocks')
for j in range(0, density) :
rate = selfishMining(A[i,j], B[i,j], blocks)
# rate = bwhReduction(A[i,j], B[i,j], blocks, 1)
selfish_profit[i,j] = rate[0]
honest_profit[i,j] = rate[1]
plt.contourf(A, B, selfish_profit, cmap=plt.cm.gray)
plt.colorbar()
plt.xlabel('Selfish mining hashrate')
plt.ylabel('Network coefficient hashrate')
plt.savefig('Selfish mining.png', dpi=1000)
plt.show()