coded down reductin not working yet though

parent 6b047651
......@@ -2,13 +2,16 @@ lambda_proposer = 3 # wait time for priority messages to be received
tou_proposer = 20 # proposer rolecount
ROLE_BLOCK_PROPOSER = 'BLOCK_PROPOSER'
tou_step = 3
tou_step = 3 # tou is expected number of users that sortition selects for committee
ROLE_COMMITEE_FOR_VOTE = 'committee'
lambda_block = 30 #seconds
lambda_step = 3
BLOCK_PROPOSAL_MSG_type = 'BLOCK_PROPOSAL_MSGs'
PRIORITY_MSG_type = 'PRIORITY_MSG'
COMMITTEE_VOTE_MSG_type = "COMMITTEE_VOTE"
TIMEOUT = "TIMEOUT"
T = 2/3 # T isa a fraction of expected committee size that defines BA*`s voting thershold
......@@ -4,6 +4,8 @@ import Utility
import hashlib
import Config
import random
from ecdsa import SigningKey,SECP256k1
from ecdsa.keys import BadSignatureError
genesisBlock = ("I am the first block")
#
......@@ -17,10 +19,11 @@ class OverlayNode(Node):
self.blockchain = [genesisBlock]
self.currentRound = 0 # round is equivalent ot block height
self.currentRound = 1 # round is equivalent ot block height
self.step = 0
self.currentRoleCount = 20
self.currentRole = "BLOCK_PROPOSER"
self.currentSeed = None
self.startNodeLifeCycleGenerator = self.startNodeLifeCycle()
# adding initial task of bootstraping node.and it only contains ba* resume tasks
......@@ -33,7 +36,58 @@ class OverlayNode(Node):
# def processMessage(self,time,payload):
# super(OverlayNode, self).processMessage(time,payload)
# print("from overlay node : "+str(payload))
def algo_processMsg(self,msg,tou):
pk = msg[1][0]
sign = msg[1][2]
payload = msg[1][1]
try:
pk.verify(sign,bytes(str(payload).encode('UTF8')) )
except BadSignatureError as badSign:
print("bad signature")
return 0,None,None
round, step, sorthash, proof, previousblockHash, value = payload
if previousblockHash != hashlib.sha256(self.blockchain[-1].encode('utf-8')).hexdigest() :
print("not in the right chain")
return 0, None, None
seed = self.currentSeed
role= (Config.ROLE_COMMITEE_FOR_VOTE, round, step)
# Todo something is wrong here not getting correct votes back
votes = Utility.verifySort(pk, sorthash, proof, seed, tou, role, self.network.pk_weight_map.get(pk), self.network.badaW)
print("why votes be zero")
return votes,value,sorthash
pass
def partOfCountVotes(self,round,step,start,counts,voters,T,tou):
msgs = self.uniqueReceivedMessages.pop(Config.COMMITTEE_VOTE_MSG_type)
for msg in msgs:
msg_round = msg[1][1][0]
msg_step = msg[1][1][1]
payload = msg[1][1]
msg_pk = msg[1][0]
# print(round, " # ",step )
if msg_round== round and msg_step==step : #check if message belong to current round and current step or not
votes,value,sorthash = self.algo_processMsg(msg,tou)
print("votes : ",votes)
if msg_pk in voters or votes == 0:
print(self.id + " continuing")
continue
voters.append(msg_pk)
counts[value] = counts[value]+ votes
if counts[value] > T*tou :
print("returning value")
return value
pass
else:
self.uniqueReceivedMessages[Config.COMMITTEE_VOTE_MSG_type].append(msg)
pass
pass
def startNodeLifeCycle(self):
'''THis is monolithic algorand algorithm for a NOde'''
self.logger.info("Life cycle started")
......@@ -46,6 +100,7 @@ class OverlayNode(Node):
# Checking if I am a Block Propser
previousHash = hashlib.sha256(self.blockchain[-1].encode('utf-8')).hexdigest()
seed = ( previousHash,self.currentRound,0)
self.currentSeed= (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)
......@@ -142,7 +197,7 @@ class OverlayNode(Node):
reduct_role_1 = (Config.ROLE_COMMITEE_FOR_VOTE,self.currentRound,reduct_step_1)
reduct_sorthash, reduct_proof, reduct_j = Utility.sortition(self.sk, reduct_seed, Config.tou_step, reduct_role_1,
self.weight, self.network.badaW, self.pk)
print(self.id," : ",reduct_j)
# print("Reduction algo " + self.id," : ",reduct_j)
if reduct_j > 0:
self.logger.info("selection for VOTING COMMITEE with J =" + str(reduct_j))
......@@ -151,12 +206,137 @@ class OverlayNode(Node):
self.broadcast(self.network.time,committeeVoteMsg)
self.logger.info("Broadcasting commitee messgae"+str(committeeVoteMsg))
print(self.uniqueReceivedMessages[Config.COMMITTEE_VOTE_MSG_type])
# print(self.uniqueReceivedMessages[Config.COMMITTEE_VOTE_MSG_type])
yield "resume",Config.lambda_block+Config.lambda_step
######## count votes for hblock1 ###########
countVote_round = self.currentRound
countVote_reduct_step_1 = 1
# Config.T
# Config.tou_step
countVote_lmbda = Config.lambda_block+Config.lambda_step
start = int(self.network.time)
counts = {}
voters = []
hblock1 = None
while True:
if not self.uniqueReceivedMessages[Config.COMMITTEE_VOTE_MSG_type] :
if int(self.network.time) > start + countVote_lmbda:
print(self.id + "Timing out")
hblock1 = Config.TIMEOUT
break
pass
else:
temp_return = self.partOfCountVotes(countVote_round,countVote_reduct_step_1,start,counts,voters,Config.T,Config.tou_step)
print("temp_retiurn " ,temp_return)
if temp_return :
hblock1 = temp_return
break
yield "resume",1 # this is to allow other nodes to be simulated and not get this node into infinite waiting on single node
self.logger.info("returning from first count values with return value : "+str(hblock1))
print("returning from first count values with return value",hblock1)
######## END : count votes for hblock1 ###########
''' END : COmmitte vote reduction step 1'''
''' Start : COmmitte vote reduction step 2 '''
empty_hash = Utility.pseudoRandomGenerator((previousHash, "Empty"))
# which block to vote empty or hblock1
reduct_step2_block_to_vote = None
if hblock1 == Config.TIMEOUT :
reduct_step2_block_to_vote = empty_hash
pass
else:
reduct_step2_block_to_vote = hblock1
pass
''' COmmitte vote reduction step2 : reduct_step2_block_to_vote'''
variables_reduct_step_2 = {}
variables_reduct_step_2['step'] = 2
variables_reduct_step_2['seed'] = (previousHash, self.currentRound, variables_reduct_step_2['step'])
variables_reduct_step_2['role'] = (
Config.ROLE_COMMITEE_FOR_VOTE, self.currentRound, variables_reduct_step_2['step'])
variables_reduct_step_2['sorthash'], variables_reduct_step_2['proof'], variables_reduct_step_2[
'j'] = Utility.sortition(self.sk, variables_reduct_step_2['seed'],
Config.tou_step,
variables_reduct_step_2['role'],
self.weight, self.network.badaW,
self.pk)
# print("Reduction algo " + self.id," : ",reduct_j)
if variables_reduct_step_2['j'] > 0:
self.logger.info("selection for VOTING COMMITEE with J =" + str(variables_reduct_step_2['j']))
variables_reduct_step_2['payload'] = (
self.currentRound, variables_reduct_step_2['step'], variables_reduct_step_2['sorthash'],
variables_reduct_step_2['proof'], previousHash, reduct_step2_block_to_vote)
variables_reduct_step_2['committeeVoteMsg'] = (Config.COMMITTEE_VOTE_MSG_type,
(self.pk, variables_reduct_step_2['payload'],
self.sk.sign(bytes(
str(variables_reduct_step_2['payload']).encode(
'UTF8')))))
self.broadcast(self.network.time, variables_reduct_step_2['committeeVoteMsg'])
self.logger.info("Broadcasting commitee messgae" + str(variables_reduct_step_2['committeeVoteMsg']))
# yield "resume", Config.lambda_block + Config.lambda_step
''' COmmitte vote reduction step2 : reduct_step2_block_to_vote'''
######## count votes for hblock1 ###########
CountVotes_step2 = {}
CountVotes_step2['countVote_round'] = self.currentRound
CountVotes_step2['countVote_step'] = 1
# Config.T
# Config.tou_step
CountVotes_step2['countVote_lmbda'] = Config.lambda_block + Config.lambda_step
CountVotes_step2['start'] = int(self.network.time)
CountVotes_step2['counts'] = {}
CountVotes_step2['voters'] = []
CountVotes_step2['hblock2'] = None
while True:
if not self.uniqueReceivedMessages[Config.COMMITTEE_VOTE_MSG_type]:
if int(self.network.time) > CountVotes_step2['start'] + CountVotes_step2[
'countVote_lmbda']:
print(self.id + "Timing out")
CountVotes_step2['hblock2'] = Config.TIMEOUT
break
pass
else:
CountVotes_step2['temp_return'] = self.partOfCountVotes(
CountVotes_step2['countVote_round'], CountVotes_step2['countVote_step'],
CountVotes_step2['start'], CountVotes_step2['counts'],
CountVotes_step2['voters'], Config.T, Config.tou_step)
print("CountVotes_step2",CountVotes_step2['temp_return'])
if CountVotes_step2['temp_return']:
CountVotes_step2['hblock2'] = CountVotes_step2['temp_return']
break
yield "resume", 1 # this is to allow other nodes to be simulated and not get this node into infinite waiting on single node
self.logger.info(
"returning from first count values with return value : " + str(CountVotes_step2['hblock2']))
print("CountVotes_step2",CountVotes_step2['hblock2'])
######## END : count votes for hblock1 ###########
var_BBA_star = {}
if CountVotes_step2['hblock2'] == Config.TIMEOUT:
var_BBA_star['block_hash']= empty_hash
else:
var_BBA_star['block_hash'] = CountVotes_step2['hblock2']
print("I am culprit " ,var_BBA_star['hblock2'])
''' END : COmmitte vote reduction step 2 '''
yield "resume", 9
# self.logger.info(self.uniqueReceivedMessages)
yield "resume",400
yield "resume",40000
''' END : REDUCTION ALGORITHM '''
'''
TODo check if node is selected as BLOCK_PROPOSER
......@@ -209,8 +389,16 @@ class OverlayNode(Node):
def m(v):
v.append('ganesh')
if __name__ == '__main__':
x = 2
if True:
x = 3
print(x)
print(x)
......@@ -239,45 +427,3 @@ class OverlayNode(Node):
class A(object):
def __init__(self,id):
self.id = id
self.commands = {"sendMessage": self.sendMessage,
}
def sendMessage(self):
pass
def process(self,param):
print("from A " +str(param) )
param.pop(0)
param.pop(0)
class B(A):
def __init__(self,id):
A.__init__(self, id)
self.commands.update({"hello":self.working})
def working(self):
pass
def process(self,param):
super(B, self).process(param)
print("from B" + str(param))
if __name__ == '__main__':
x = [1]
if x:
print('yup')
\ No newline at end of file
......@@ -22,3 +22,4 @@ class SystemEntity(object):
'''
pass
......@@ -12,7 +12,7 @@ class TimeSimulator(object):
return "Time : "+ str(self._systemTime)
def startTicking(self,callback):
for i in range(100):
for i in range(10000):
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