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