successfully gossiped priority message and other nodes have received it...

 successfully gossiped priority message and other nodes have received it priority messages of each other successfully
parent e6b3b384
......@@ -17,6 +17,7 @@ class Network():
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 = {}
self.time = 0
def setupNetwork(self):
'''
......@@ -71,6 +72,7 @@ class Network():
return self.links.get((tup[1],tup[0]))
def simulate(self,time):
self.time = time
# logger.info("simulating .. ")
self.networkClockTime = time
# Note links should be simulated before nodeself.ResumeTasks.append(("nextOn", self.startNodeLifeCycleGenerator,))s
......
......@@ -2,6 +2,7 @@ from SystemEntity import SystemEntity
import logging
import Utility
import hashlib
from collections import defaultdict
class Node(SystemEntity):
def __init__(self,id,network,weight):
......@@ -13,13 +14,13 @@ class Node(SystemEntity):
self.adjacentNodes = []
self.messageQueue = []
self.sk,self.pk = Utility.genratePublicPrivateKey()
self.ResumeTasks = []
self.ResumeTasks = defaultdict(list)
'''make sure methods included here are not overriddenn'''
self.commands={"sendMessage":self.sendMessage,
"nextOn":self.nextOn
}
self.messagesTillNow = []
self.messagesTillNow = [] # list of messages forwarded
self.uniqueReceivedMessages = [] #this is a list of messages in buffer which were not processed
......@@ -50,7 +51,7 @@ class Node(SystemEntity):
link.enqueMessage((time,params))
# any message going out is recorded as sent and will be checked for duplicates
self.messagesTillNow.append(hashlib.sha256((str(params[2])).encode('utf-8')).hexdigest())
self.logger.info("Sending : " + str((time,params)))
self.logger.info("Sending : " + str((time,params)))#(time,(sender,receiver,payload))
pass
def broadcast(self,time,gossipPayload):
......@@ -67,7 +68,7 @@ class Node(SystemEntity):
node = self.network.nodes.get(nodeid)
link = self.network.getLink((self.id, node.id))
link.enqueMessage((time, (self.id, node.id,gossipPayload)))
self.logger.info("Sending : " + str((time, (self.id, node.id,gossipPayload))))
self.logger.info("Sending : " + str((time, (self.id, node.id,gossipPayload))))#(time,(sender,receiver,payload))
def processMessage(self,time,payload):
'''remove any extra headers like source and destination here '''
......@@ -80,7 +81,7 @@ class Node(SystemEntity):
self.uniqueReceivedMessages.append(payload[0])
def nextOn(self,time,generator):
next(generator[0])
return next(generator[0])
def startNodeLifeCycle(self):
pass
......@@ -103,15 +104,15 @@ class Node(SystemEntity):
self.logger.info("received : "+str(message))
self.dequeMessage()
# check if main ba* has to be resumed or not
try:
ResumeTask = self.ResumeTasks.pop(0)
command = self.commands.get(ResumeTask[0])
ResumeOut = command(time,ResumeTask[1:])
if ResumeOut and ResumeOut[0] == "resume":
self.logger.info("resume has been called")
self.ResumeTasks.append(("nextOn", self.startNodeLifeCycleGenerator,))
ResumeTasks = self.ResumeTasks.pop(time)
for ResumeTask in ResumeTasks:
command = self.commands.get(ResumeTask[0])
ResumeOut = command(time,ResumeTask[1:])
if ResumeOut and ResumeOut[0] == "resume":
self.logger.info("resume has been called")
self.ResumeTasks[str(int(time)+int(ResumeOut[1]))].append(("nextOn", self.startNodeLifeCycleGenerator,))
except KeyError as ke:
# not task pending at this time
pass
......
......@@ -3,16 +3,31 @@ import logging
import Utility
import hashlib
genesisBlock = "I am the first block"
genesisBlock = ("I am the first block")
#
# class Role:
# def __init__(self):
# self.role =
class OverlayNode(Node):
def __init__(self,id,network,weight):
Node.__init__(self,id,network,weight)
self.blockchain = [genesisBlock]
self.currentRound = 0 # round is equivalent ot block height
self.step = 0
self.currentRoleCount = 20
self.currentRole = "BLOCK_PROPOSER"
self.startNodeLifeCycleGenerator = self.startNodeLifeCycle()
# adding initial task of bootstraping node.and it only contains ba* resume tasks
self.ResumeTasks.append(("nextOn", self.startNodeLifeCycleGenerator,))
# self.ResumeTasks.append(("nextOn", self.startNodeLifeCycleGenerator,))
self.ResumeTasks['1'].append(("nextOn", self.startNodeLifeCycleGenerator,))
self.commands.update({})
# def processMessage(self,time,payload):
# super(OverlayNode, self).processMessage(time,payload)
# print("from overlay node : "+str(payload))
......@@ -26,25 +41,15 @@ class OverlayNode(Node):
# 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.weight
badaW = 100
hash, proof, j = Utility.sortition(self.sk,seed,roleCount, role, w, self.network.badaW, self.pk)
previousHash = hashlib.sha256(self.blockchain[-1].encode('utf-8')).hexdigest()
seed = ( previousHash,self.currentRound,0)
hash, proof, j = Utility.sortition(self.sk,seed,20, 'BLOCK_PROPOSER', self.weight, self.network.badaW, self.pk)
print("I am the mighty J =",j)
if j > 0 :
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):
for sub_user_index in range(self.weight):
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)
......@@ -56,14 +61,14 @@ class OverlayNode(Node):
min_sub_user_index = sub_user_index
msg_to_broadcast = ( currentRound, hash , min_sub_user_index , min_sha_priority )
print(msg_to_broadcast)
self.logger.info(msg_to_broadcast)
priority_msg_to_broadcast = ( "PRIORITY_MSG",self.currentRound, hash , min_sub_user_index , min_sha_priority )
self.logger.info("broadcsting priority message : "+str(priority_msg_to_broadcast))
self.broadcast(self.network.time, priority_msg_to_broadcast)
print("i am before yield 1",self.id)
yield "resume"
print("i am before yield 2", self.id)
yield
yield "resume",7
self.logger.info(self.uniqueReceivedMessages)
yield "resume",40
'''
TODo check if node is selected as BLOCK_PROPOSER
......
......@@ -155,6 +155,13 @@ class TestYieldSequence:
self.B()
self.C()
def gen():
for i in range(5):
yield 'ram', 'ganesh'
def testit():
for k1 in gen():
print(k1[1])
if __name__ == '__main__':
# testSortition()
......@@ -162,5 +169,5 @@ if __name__ == '__main__':
# testVerifySort(
# testclassLessGenerator()
# testGeneratorClass()
testSelfReferGen()
# testSelfReferGen()
testit()
......@@ -12,7 +12,7 @@ class TimeSimulator(object):
return "Time : "+ str(self._systemTime)
def startTicking(self,callback):
for i in range(10):
for i in range(15):
self._systemTime = self._systemTime + 1
logger.info(self)
callback(str(self._systemTime))
......
1 5 sendMessage 1 2 hello2
2 3 sendMessage 2 3 hello3
4 5 sendMessage 4 1 hello1
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