Commit 3169d637 authored by UNHALE NILESH ARUN's avatar UNHALE NILESH ARUN

added weights, pk, sk, start nodelife cycle

parent 2bd28bca
......@@ -4,8 +4,10 @@ import csv
from Node import Node
from OverlayNode import OverlayNode
import logging
import random
from numpy.random import randint
logger = logging.getLogger(__name__)
import numpy as np
......@@ -14,6 +16,7 @@ class Network():
self.nodes = {}#{1 : Node(1),2:Node(2)}
self.links = {} #{(1,2):Link(1-2),(2,1):Link(2,1),(3,2):Link(3*2)} linkID = tuple(1,2)
self.networkClockTime = 0
self.pk_weight_map = {}
def setupNetwork(self):
'''
......@@ -27,12 +30,16 @@ class Network():
:return:
'''
w = randint(1, 51, 4) # 51 is excluded - range (1,50)
self.badaW = np.sum(w)
i = 0
with open('Nodes.dat', 'r') as f:
reader = csv.reader(f)
for row in reader:
node = row[0] # tuple of a link
self.nodes.update({node:OverlayNode(node,self)})
node = row[0] # NOde id
xnode = {node:OverlayNode(node,self,w[i])}
self.pk_weight_map.update({xnode[node].pk:w[i]})
self.nodes.update(xnode)#added node to network
# print(self.nodes.get('1')) #keys are strings
......
from SystemEntity import SystemEntity
import logging
import Utility
class Node(SystemEntity):
def __init__(self,id,network):
def __init__(self,id,network,weight):
SystemEntity.__init__(self,network)
self.logger = logging.getLogger(__name__ + "-" + str(id))
self.id = id
self.weight = weight
self.adjacentNodes = []
self.messageQueue = []
self.sk,self.pk = Utility.genratePublicPrivateKey()
'''make sure methods included here are not overriddenn'''
self.commands={"sendMessage":self.sendMessage,
"nextOn":self.nextOn
}
......@@ -54,6 +59,9 @@ class Node(SystemEntity):
payload.pop(0) # removed source header
payload.pop(0) #removed destination header
def nextOn(self,time,generator):
next(generator[0])
def startNodeLifeCycle(self):
pass
......@@ -82,10 +90,15 @@ class Node(SystemEntity):
tasks = self.todoList.pop(time)
for task in tasks:
command = self.commands.get(task[0])
command(time,task[1:])
out = command(time,task[1:])
if out and out[0] == "resume":
self.logger.info("resume has been called")
self.todoList[time+1].append(("nextOn", self.startNodeLifeCycleGenerator,))
except KeyError as ke:
# not task pending at this time
pass
except StopIteration as si:
pass
......
......@@ -6,8 +6,9 @@ import hashlib
genesisBlock = "I am the first block"
class OverlayNode(Node):
def __init__(self,id,network):
Node.__init__(self,id,network)
def __init__(self,id,network,weight):
Node.__init__(self,id,network,weight)
self.startNodeLifeCycleGenerator = self.startNodeLifeCycle()
self.commands.update({})
def processMessage(self,time,payload):
......@@ -16,33 +17,51 @@ class OverlayNode(Node):
def startNodeLifeCycle(self):
'''THis is monolithic algorand algorithm for a NOde'''
self.logger.info("Life cycle started")
while True:
#increment round number
# Checking if I am a Block Propser
previousHash = hashlib.sha256(genesisBlock.encode('utf-8')).hexdigest()
currentRound = 0
step = 0
blockHeight = 0
seed = ( previousHash,currentRound,blockHeight )
roleCount = 20
role = "BLOCK_PROPOSER"
w = self.w
w = self.weight
badaW = 100
hash, proof, j = Utility.sortition(self.sk,seed,roleCount, role, w, badaW, self.pk)
hash, proof, j = Utility.sortition(self.sk,seed,roleCount, role, w, self.network.badaW, self.pk)
print("I am the mighty J =",j)
if j > 0 :
min = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
min_sha_priority = None
# min_sub_user_index = None
# msg_to_broadcast = ( currentRound, hash , min_sub_user_index , min_sha_priority )
print(w)
print(range(w))
for sub_user_index in range(w):
input_to_SHA256 = hash + sub_user_index # TODO : can concatenation means this
sha256_hash = hashlib.sha256(input_to_SHA256.encode('utf-8')).hexdigest()
if sha256_hash < min:
input_to_SHA256 = (hash , sub_user_index,) # TODO : can concatenation means this
sha256_hash = hashlib.sha256((str(input_to_SHA256)).encode('utf-8')).hexdigest()
print(sha256_hash)
if not min_sha_priority :
min_sha_priority = sha256_hash
min_sub_user_index = sub_user_index
if sha256_hash < min_sha_priority:
min_sha_priority = sha256_hash
min_sub_user_index = sub_user_index
msg_to_broadcast = ( roundNumber, hash , min_sub_user_index , min_sha_priority )
msg_to_broadcast = ( currentRound, hash , min_sub_user_index , min_sha_priority )
print(msg_to_broadcast)
self.logger.info(msg_to_broadcast)
print("i am before yield 1",self.id)
yield "resume"
print("i am before yield 2", self.id)
yield
'''
TODo check if node is selected as BLOCK_PROPOSER
......
......@@ -18,6 +18,8 @@ def populateTodolist(net):
instruction = row[0].split() # tuple of a link
node = network.nodes.get(instruction[0])
node.todoList[instruction[1]].append(tuple(instruction[2:]))
for id, node in network.nodes.items():
node.todoList['0'].append(("nextOn",node.startNodeLifeCycleGenerator,))
for id,node in network.nodes.items():
logger.info(id +" : "+str(node.todoList))
......@@ -38,7 +40,7 @@ def simulateNetwork(net):
if __name__ == '__main__':
logging.basicConfig(filename='TestCases.log', level=logging.INFO)
# populateTodolist()
# populateTodolist(None)
simulateNetwork(None)
pass
......@@ -222,6 +222,10 @@ def testVerifySort():
assert (y==0),"Test Verify sort failed : change of seed not detected"
def tester():
'''
tests if sortition distributes the stake
:return:
'''
sk,pk = genratePublicPrivateKey()
seed = ("a",1,2)
roleCount = 26
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment