def propose_block(self,sortition_result, current_round): proof,hash,j,indexes = sortition_result #getting seed hashstring_for_seed = self.get_seed(current_round) + str(current_round) proof_for_seed = vrftool.get_proof(self.sk, hashstring_for_seed) new_seed = vrftool.get_hash(proof_for_seed) new_block = Block(current_round, self.chain.chain[current_round - 1].block_hash, self.pk, proof, new_seed) new_block.gen_bhash() # caculate the max priority among all subusers priority, max_index = self.max_priority(new_block.block_hash, indexes) new_block.add_priority(priority) # self is proposer then add hihest proority block into self block block_candidates self.block_candidates[current_round] = list() # store hash and proof # hash for seed and proof for random seed for next round self.block_candidates[current_round].append(new_block) # construc max pritority message block_message = Message(self.pk, current_round, self.tokens, new_block.block_hash, entire_block=new_block, vrf_proof=proof, vrf_hash=hash, priority=priority) gossip_message = (self.env.now, (self.id,current_round), block_message) # test message delay self.gossip_pipes.put(gossip_message) print(f'node {self.id} sent out proposal msg with block hash {new_block.block_hash}for its peer at time{self.env.now}')
def Sortition(self, secret_key, round, role, thresh_num): vrfstring = self.get_seed(round) + role proof = vrftool.get_proof(secret_key, vrfstring) hash = vrftool.get_hash(proof) # probs shoud be equal to the threshhold set by the system # proposers/totla_tokens, varing from 26 to 70 probs = thresh_num / self.chain.total_tokens #get the subuser number and indexes js, indexes = self.sub_users(self.tokens, probs, hash) dirpath = '/scratch/yliu7/' filename = f'Round{str(round)}sort.txt' file_path = 'SortRes/' + filename path = os.path.join(dirpath, file_path) if js == 0: return False else: result = (proof, hash, js, indexes) with open(path, "a+") as f: f.write(f'{self.id},{round},{role},{js} \n') return result
def Sortition(self,secret_key, round, role, thresh_num): vrfstring = self.get_seed(round) + role proof = vrftool.get_proof(secret_key, vrfstring) hash = vrftool.get_hash(proof) # probs shoud be equal to the threshhold set by the system # proposers/totla_tokens, varing from 26 to 70 probs = thresh_num / self.chain.total_tokens #get the subuser number and indexes js, indexes = self.sub_users(self.tokens, probs, hash) if js==0: return False else: result =(proof,hash,js,indexes) return result
def generate_empty_block(self,current_round): null_priority = hex(0) hashstring_for_seed = self.get_seed(current_round) + str(current_round) proof_for_seed = vrftool.get_proof(system_sk, hashstring_for_seed) new_seed = vrftool.get_hash(proof_for_seed) empty_block = Block(current_round, self.chain.chain[current_round-1].block_hash, author=system_pk,author_proof=proof_for_seed, seed = new_seed, isEmpty=True) empty_block.add_priority(null_priority) return empty_block
def propose_block(self, sortition_result, current_round): proof, hash, j, indexes = sortition_result #getting seed hashstring_for_seed = self.get_seed(current_round) + str(current_round) proof_for_seed = vrftool.get_proof(self.sk, hashstring_for_seed) new_seed = vrftool.get_hash(proof_for_seed) new_block = Block(current_round, self.chain.chain[current_round - 1].block_hash, self.pk, proof, new_seed) new_block.gen_bhash() # caculate the max priority among all subusers priority, max_index = self.max_priority(new_block.block_hash, indexes) new_block.add_priority(priority) # self is proposer then add hihest proority block into self block block_candidates self.block_candidates[current_round] = [new_block] filename = f'{str(current_round)}blockproposals.txt' file_path = 'RoundStatus/' + filename dirpath = '/scratch/yliu7/' folder_path = os.path.join(dirpath, file_path) with open(folder_path, 'a+') as f: f.write(f'{new_block.block_hash}\n') proposal_msg = ('b', current_round, new_block) priority_msg = ('p', current_round, new_block.block_hash, priority) self.Max_Priority_Proposal[current_round] = priority msg_id = 'b' + str(current_round) + new_block.block_hash + priority pmsg_id = 'p' + str(current_round) + new_block.block_hash + priority self.Gossiped_Msg.append(msg_id) self.Gossiped_Msg.append(pmsg_id) self.Gossip_Msg(priority_msg) self.Gossip_Msg(proposal_msg) print( f'node {self.id} sent out proposal msg with block hash {new_block.block_hash} with priority {priority}for its peer at time{self.env.now}' )
return j, indexes_of_j def get_bond(j, user_total, prob): sum = 0 while j >= 0: sum += ss.binom.pmf(j, user_total, prob) j -= 1 return sum ms = '0x9d' sk = vrftool.new_sk() p = vrftool.get_proof(sk, ms) h = vrftool.get_hash(p) j, indexes_of_j = subuser(5, 26 / 100000000, h) algolist = [vrftool.new_sk() for i in range(50000)] hash = [vrftool.get_hash(vrftool.get_proof(sk, ms)) for sk in algolist] start = time.time() count = 0 times = 0 c = 10 while c > 0: for h in hash: times += 1 weight = random.randint(1, 500) print(f'counting for {h} with weight{weight}') j, indexes_of_j = subuser(weight, 26 / 1000000, h) if j > 0:
import vrftool import math import binascii newsk = vrftool.new_sk() print(newsk) newpk=vrftool.get_pk(newsk) print(newpk) ms = '0x9d' newproof = vrftool.get_proof(newsk,ms) print(newproof) newhash = vrftool.get_hash(newproof) print(newhash) flag = vrftool.verify_proof(newproof,newpk,ms,newhash) print(flag) hash = newhash.decode('utf-8') print(hash) val = bin(int(hash,16)) total = pow(2,len(val)) num = int(val,2) print(num) print(total) hashres = num/total user_total_tokens=5 total = 100 prob = 0.2 #factorial function def factorial(num):
import vrftool import math from utils import bino_bond import random subusers = 200 total_tokens = 10000 thresh = 2 / 3 prob = subusers / total_tokens nodes = [vrftool.new_sk() for x in range(100)] ms = '0x9d' proofs = [vrftool.get_proof(node, ms) for node in nodes] hash = [vrftool.get_hash(proof) for proof in proofs] p = vrftool.get_proof(nodes[0], ms) h = vrftool.get_hash(p) print('vrfproof length', len(p)) print('vrfhash lenthg', len(h), h) user_tokens = [100 for x in range(100)] def subusers(user_total_tokens, probs, hash): print(hash) bonds = bino_bond(user_total_tokens, probs) js = 0 hash = hash.decode('utf-8') val = bin(int(hash, 16)) total = pow(2, 512) num = int(val, 2) hashprob = num / total print(hashprob) flag = True