-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.py
77 lines (60 loc) · 2.05 KB
/
node.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 18 12:06:33 2018
@author: aviasayag
"""
import random
class Node:
def __init__(self, index, sample):
self.index = index
self.portion = 0 # eblock portion
self.sample = sample # global epool sample
self.epool = []
self.epoolSize = 0
self.block = []
"""
Receive node lists and initalize their portions so it sums to 1.
If byzantineFactor is greater than 0, the portion of the first
byzantine node is factored at this facotr.
"""
def generateRandomPortions(nodesList, byzantineFactor=0):
nodesNum = len(nodesList)
randNums = []
sum = 0
for i in range(0, nodesNum):
rand = random.random()
if i == 0 and byzantineFactor > 0:
rand *= byzantineFactor
sum += rand
randNums.append(rand)
for node in nodesList:
node.portion = randNums[node.index]/sum
"""
Return nodes by the nodesNum and each node samples from the global epool by
the epoolSample (between 0 to 1). Furthermore each node contributes to the
global epool by a uniform distribution.
"""
def createNodes(nodesNum, epoolSample):
nodesList = []
for n in range(0, nodesNum):
newNode = Node(n, epoolSample)
nodesList.append(newNode)
generateRandomPortions(nodesList)
return nodesList
"""
Return nodes by the nodesNum, where the first is byzantine, and each node
samples from the global epool by the epoolSample (between 0 to 1). byzantine
node have sample of 1. Furthermore each node contributes to the global epool
by a uniform distribution except the byzantine node that contributes
a factor of byzantineFactor more.
"""
def createNodesWithSingleByzantine(nodesNum, byzantineFactor, epoolSample):
nodesList = []
for n in range(0, nodesNum):
newNode = Node(n, epoolSample)
if n == 0:
newNode.sample = 1
nodesList.append(newNode)
generateRandomPortions(nodesList, byzantineFactor)
return nodesList