1.6.1/2 : block proposer sortition verified

parent 5f02235c
lambda_proposer = 3 # wait time for priority messages to be received
tou_proposer = 20 # proposer rolecount
ROLE_BLOCK_PROPOSER = 'BLOCK_PROPOSER'
\ No newline at end of file
ROLE_BLOCK_PROPOSER = 'BLOCK_PROPOSER'
tou_step = 3
ROLE_COMMITEE_FOR_VOTE = 'committee'
lambda_block = 30 #seconds
BLOCK_PROPOSAL_MSG_type = 'BLOCK_PROPOSAL_MSGs'
PRIORITY_MSG_type = 'PRIORITY_MSG'
\ No newline at end of file
......@@ -21,7 +21,7 @@ class Node(SystemEntity):
}
self.messagesTillNow = [] # list of messages forwarded
self.uniqueReceivedMessages = [] #this is a list of messages in buffer which were not processed
self.uniqueReceivedMessages = defaultdict(list) #this is a list of messages in buffer which were not processed, expecting two list of 'BLOCK_PROPOSAL_MSG','PRIORITY_MSG'
def enqueMessage(self,message):
......@@ -51,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)))#(time,(sender,receiver,payload))
###### self.logger.info("Sending : " + str((time,params)))#(time,(sender,receiver,payload))
pass
def broadcast(self,time,gossipPayload):
......@@ -68,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))))#(time,(sender,receiver,payload))
###### 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 '''
......@@ -78,7 +78,7 @@ class Node(SystemEntity):
# TODO check messages for duplicates and brodacast them : Done
if not (hashlib.sha256((str(payload[0])).encode('utf-8')).hexdigest() in self.messagesTillNow) :
self.broadcast(time,payload[0])
self.uniqueReceivedMessages.append(payload[0])
self.uniqueReceivedMessages[payload[0][0]].append(payload[0])
def nextOn(self,time,generator):
return next(generator[0])
......
......@@ -42,20 +42,21 @@ class OverlayNode(Node):
#increment round number
blockForBA = None
# Checking if I am a Block Propser
previousHash = hashlib.sha256(self.blockchain[-1].encode('utf-8')).hexdigest()
seed = ( previousHash,self.currentRound,0)
hash, proof, j = Utility.sortition(self.sk,seed,Config.tou_proposer, Config.ROLE_BLOCK_PROPOSER, self.weight, self.network.badaW, self.pk)
print("I am the mighty J =",j)
if j > 0 : # only the block proposer commity mebers can enter here
self.logger.info("selection for BLOCK PROPOSAL with J ="+str( j))
min_sha_priority = None
# min_sub_user_index = None
# msg_to_broadcast = ( currentRound, hash , min_sub_user_index , min_sha_priority )
for sub_user_index in range(self.weight):
input_to_SHA256 = (hash , sub_user_index,) # TODO : can concatenation means this
input_to_SHA256 = (hash , sub_user_index,)
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
......@@ -64,7 +65,7 @@ class OverlayNode(Node):
min_sub_user_index = sub_user_index
priority_msg_to_broadcast = ( "PRIORITY_MSG",self.currentRound, hash , min_sub_user_index , min_sha_priority )
priority_msg_to_broadcast = ( Config.PRIORITY_MSG_type,self.currentRound, hash , min_sub_user_index , min_sha_priority,proof )
self.logger.info("broadcsting priority message : "+str(priority_msg_to_broadcast))
self.broadcast(self.network.time, priority_msg_to_broadcast)
......@@ -73,7 +74,9 @@ class OverlayNode(Node):
yield "resume",Config.lambda_proposer
greater_flag = True
for priority_msg_other in self.uniqueReceivedMessages:
msgs = self.uniqueReceivedMessages.pop(Config.PRIORITY_MSG_type)
for priority_msg_other in msgs :
if min_sha_priority > priority_msg_other[4]:
greater_flag = False
......@@ -82,14 +85,69 @@ class OverlayNode(Node):
'''
BLOCK_PROPOSAL_MSG_to_broadcast : ("BLOCK_PROPOSAL_MSG", previousblockhash,A random string as txns,Nod`s priority payload)
'''
BLOCK_PROPOSAL_MSG_to_broadcast = ("BLOCK_PROPOSAL_MSG", previousHash,Utility.pseudoRandomGenerator(random.randint(0,1000)),priority_msg_to_broadcast[1:])
BLOCK_PROPOSAL_MSG_to_broadcast = (Config.BLOCK_PROPOSAL_MSG_type, previousHash,Utility.pseudoRandomGenerator(random.randint(0,1000)),priority_msg_to_broadcast[1:])
self.logger.info("broadcsting BLOCK Proposal Message : " + str(BLOCK_PROPOSAL_MSG_to_broadcast))
self.broadcast(self.network.time, BLOCK_PROPOSAL_MSG_to_broadcast)
blockForBA = (previousHash,BLOCK_PROPOSAL_MSG_to_broadcast[2])
else: # others than block proposers
# everybody except highest priority block proposer is waiting for lambda_proposer+ lambda_block 33 seconds
yield "resume", Config.lambda_proposer + Config.lambda_block
else: # others than block proposers
# everybody is waiting for lambda_proposer+ lambda_block 33 seconds
yield "resume", Config.lambda_proposer+Config.lambda_block
# TODO:done : drop any priority messages in queue
try :
self.uniqueReceivedMessages.pop(Config.PRIORITY_MSG_type)
except Exception:
pass
# print("NOde id :"+self.id)
# print(self.uniqueReceivedMessages)
######### Performing selection of block BA* ############################
if not (self.uniqueReceivedMessages[Config.BLOCK_PROPOSAL_MSG_type] or blockForBA) :
blockForBA = (previousHash,"Empty")
elif not blockForBA :
# Todo select block from highest priority node
blockForBA = (self.uniqueReceivedMessages[Config.BLOCK_PROPOSAL_MSG_type][0][1],
self.uniqueReceivedMessages[Config.BLOCK_PROPOSAL_MSG_type][0][2])
pk = self.uniqueReceivedMessages[Config.BLOCK_PROPOSAL_MSG_type][0][3][4]
hash = self.uniqueReceivedMessages[Config.BLOCK_PROPOSAL_MSG_type][0][3][1]
proof = self.uniqueReceivedMessages[Config.BLOCK_PROPOSAL_MSG_type][0][3][4]
w = self.network.pk_weight_map.get(pk)
seed = (previousHash, self.currentRound, 0)
# TODO verify block proposal
j = Utility.verifySort(pk, hash, proof, seed, Config.tou_proposer, Config.ROLE_BLOCK_PROPOSER, w, self.network.badaW)
if j :
self.logger.log("it is a valid block proposed message with j : ",j)
#todo : DONE : drop block proposal msges
try :
self.uniqueReceivedMessages.pop(Config.BLOCK_PROPOSAL_MSG_type)
except Exception as e:
self.logger.error(e)
print(blockForBA)
######### END : Performing selection of block BA* ############################
''' START : REDUCTION ALGORITHM '''
# reduct_step_1 = 1
# reduct_seed = (previousHash, self.currentRound, reduct_step_1)
# reduct_hash, reduct_proof, reduct_j = Utility.sortition(self.sk, reduct_seed, Config.tou_step, Config.ROLE_COMMITEE_FOR_VOTE,
# self.weight, self.network.badaW, self.pk)
#
# if reduct_j :
# self.logger.info("selection for VOTIN COMMITEE with J =" + str(reduct_j))
yield "resume", 9
self.logger.info(self.uniqueReceivedMessages)
yield "resume",40
yield "resume",400
''' END : REDUCTION ALGORITHM '''
'''
TODo check if node is selected as BLOCK_PROPOSER
if yes
......@@ -210,7 +268,6 @@ class B(A):
if __name__ == '__main__':
l = (1,2,tuple(str('3|4|5').split('|')))
x = B(2)
print(x.commands)
x.process(list(l))
x = [1]
if x:
print('yup')
\ No newline at end of file
......@@ -12,7 +12,7 @@ class TimeSimulator(object):
return "Time : "+ str(self._systemTime)
def startTicking(self,callback):
for i in range(25):
for i in range(100):
self._systemTime = self._systemTime + 1
logger.info(self)
callback(str(self._systemTime))
......
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