-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
159 lines (112 loc) · 4.18 KB
/
main.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 18 15:10:11 2018
@author: aviasayag
"""
import utils
import simulator
import time
import numpy as np
# Parameters
NODES_NUM = 10
BYZANTINE_NUM = 2
EPOOL_SIZE = 10000
EPOOL_SAMPLE = 0.95 # num between 0 to 1
NUM_TX_IN_BLOCK = 100
"""
An example for an Eblock construction simulation
"""
def simulateEblockConstructionMethods(nodesNum, epoolSample, epoolSize, txsInBlock):
sim = simulator.EblockSimulator(nodesNum, epoolSample, epoolSize, 1)
sim.sim1(txsInBlock)
threshold = (txsInBlock/9000) * (2**256)
sim.sim2(txsInBlock, threshold)
sim.sim2B(threshold)
"""
Simulate the manipulation byzantine node can do when constructing an
"""
def simulateByzantineFairnessManipulation(nodesNum, byzantineNum, epoolSample, epoolSize,
txsInBlock, beta, manipulationBeta):
sim = simulator.FairnessSimulator(nodesNum, byzantineNum, epoolSample, epoolSize, 1)
sim.allConstructValidEblocks(txsInBlock)
sim.byzantineManipulateItsBlock(manipulationBeta)
return sim.validateEblocks(beta)
"""
Analysis for the question: "How much of the Eblock can Byzantine
node manipulate so it would still be valid in the other nodes eyes?"
"""
def manipulationAnalysis():
beta = EPOOL_SAMPLE ** 2 - ((10/NUM_TX_IN_BLOCK) ** (0.5))
avgTries = 20
betaTries = 10
factor = 1.044
betaManipulation = beta * factor
for j in range (betaTries):
avgList = []
for i in range(avgTries):
fails = simulateByzantineFairnessManipulation(NODES_NUM, BYZANTINE_NUM, \
EPOOL_SAMPLE, EPOOL_SIZE, NUM_TX_IN_BLOCK, beta, betaManipulation)
avgList.append(fails / (NODES_NUM-1))
avg = sum(avgList)/avgTries
minAvg = min(avgList)
maxAvg = max(avgList)
stdDeviation = (sum(map(lambda x: (x-avg)**2, avgList)) / (avgTries-1)) ** 0.5
print(
"""beta: %s, beta manipulation: %s, betaFactor: %s, avg fails: %s, minAvg %s, \
maxAvg %s, std deviation %s""" %
(beta, betaManipulation, factor, avg, minAvg, maxAvg, stdDeviation))
factor += 0.002
betaManipulation = beta * factor
"""
Analysis for an Eblock nodes' distribution after a
consecutive Byzantine primaries.
"""
def manipulationRepresenationAnalysis(nodesNum, byzantineNum, epoolSample, epoolSize,
txsInBlock, numConsecutiveByz):
beta = epoolSample ** 2 - ((10/txsInBlock) ** (0.5))
betaManipulation = beta * 1
print("beta: %s, beta manipulation: %s" % (beta, betaManipulation))
gamma = epoolSize / txsInBlock
tau1 = (1-beta)/beta
tau2 = 1 - (1 - (beta/gamma))**numConsecutiveByz
tau = tau1 * tau2
print("gamma: %s, 2tau: %s" % (gamma, 2*tau))
sim = simulator.FairnessSimulator(nodesNum, byzantineNum, epoolSample, epoolSize, 0)
initialDist = sim.getGlobalEpoolDist()
print(initialDist)
dist = []
for i in range(numConsecutiveByz):
sim.allConstructValidEblocks(txsInBlock, i)
manipulatedBlock = sim.byzantineManipulateItsBlock(betaManipulation, i)
numFail = sim.validateEblocks(beta)
if numFail < 13:
sim.inputNewTxs(manipulatedBlock)
else:
print("Failed to pass the Eblock!")
dist = sim.getGlobalEpoolDist()
print(dist)
distDiff = [abs(dist2-dist1) for dist1, dist2 in zip(initialDist, dist)]
L1Norm = sum(distDiff)
print("L^1 norm: %s" % (L1Norm))
if L1Norm < (2*tau):
print("Success - bound is true")
return True
else:
print("Fail - bound is false")
return False
if __name__ == '__main__':
#simulateEblockConstructionMethods(NODES_NUM, EPOOL_SAMPLE, EPOOL_SIZE, NUM_TX_IN_BLOCK)
#manipulationAnalysis()
countFail = 0
countSuccess = 0
for i in range (10):
if manipulationRepresenationAnalysis(NODES_NUM, BYZANTINE_NUM, EPOOL_SAMPLE, EPOOL_SIZE,
NUM_TX_IN_BLOCK, 3):
countSuccess += 1
else:
countFail += 1
print("")
print("----------------------------------------")
print("")
print("Total fails: %s, Total success: %s" % (countFail, countSuccess))