def test_gossip(self): # test that universal gossip works with all nodes gossip.send_message = GossipTest.send_message gossip.gossip_message("whee", "test") expected_output = set([('http://127.0.0.1:5001/', 'whee', 'test'), ('http://127.0.0.1:5006/', 'whee', 'test'), ('http://127.0.0.1:5002/', 'whee', 'test'), ('http://127.0.0.1:5003/', 'whee', 'test'), ('http://127.0.0.1:5004/', 'whee', 'test'), ('http://127.0.0.1:5005/', 'whee', 'test')]) self.assertEqual(GossipTest.messages_sent, expected_output) GossipTest.messages_sent = set() # make sure nodes don't gossip to own ID gossip.config.node_id = 1 gossip.gossip_message("whee", "test") expected_output = set([('http://127.0.0.1:5006/', 'whee', 'test'), ('http://127.0.0.1:5002/', 'whee', 'test'), ('http://127.0.0.1:5003/', 'whee', 'test'), ('http://127.0.0.1:5004/', 'whee', 'test'), ('http://127.0.0.1:5005/', 'whee', 'test')]) self.assertEqual(GossipTest.messages_sent, expected_output) GossipTest.messages_sent = set() gossip.config.node_id = 6 gossip.gossip_message("whee", "test") expected_output = set([('http://127.0.0.1:5001/', 'whee', 'test'), ('http://127.0.0.1:5002/', 'whee', 'test'), ('http://127.0.0.1:5003/', 'whee', 'test'), ('http://127.0.0.1:5004/', 'whee', 'test'), ('http://127.0.0.1:5005/', 'whee', 'test')]) self.assertEqual(GossipTest.messages_sent, expected_output)
def broadcast_votes_for(self, round, votes): """ Broadcast votes on a proposal to all nodes; this happens once a proposal is added to s_i. """ for proposal in votes: gossip.gossip_message("ba-vote", json.dumps([proposal, self.signatures[proposal]]))
for user in USERS: user_utxos[user] = [] # insert genesis block; populate all users w huge balance outputs = [] for user in USERS: genesis_utxo = TransactionOutput("Genesis", user, 100000000) outputs.append(genesis_utxo) genesis_tx = Transaction([], outputs) for user_num in range(len(USERS)): user = USERS[user_num] user_utxos[user].append((genesis_tx.hash + ":" + str(user_num), 100000000)) genesis_block = PoWBlock(0, [genesis_tx], "genesis", is_genesis=True) chaindb.chain.add_block(genesis_block) gossip.gossip_message("addblock", genesis_block) curr_height = 1 parent = genesis_block while curr_height <= HEIGHT_TO_REACH: chain = chaindb.chain txs = [] if random.random() < FORK_PROBABILITY: if parent.parent_hash == "genesis": continue curr_height -= 1 new_parent_hash = random.choice( chain.chain[curr_height - 1]) # fork random previous block parent = chain.blocks[new_parent_hash]