def test_both_types_of_conflicts(self): dag = Dag(0) watcher = ConflictWatcher(dag) actor1 = Private.publickey(Private.generate()) actor2 = Private.publickey(Private.generate()) actor3 = Private.publickey(Private.generate()) block1_hash = ChainGenerator.insert_dummy(dag, [dag.genesis_hash()], 1) watcher.on_new_block_by_validator(block1_hash, 1, actor1) block2_hash = ChainGenerator.insert_dummy(dag, [block1_hash], 2) watcher.on_new_block_by_validator(block2_hash, 1, actor2) block2c_hash = ChainGenerator.insert_dummy(dag, [block1_hash], 2) watcher.on_new_block_by_validator(block2c_hash, 1, actor2) block3_hash = ChainGenerator.insert_dummy(dag, [block2_hash], 3) watcher.on_new_block_by_validator(block3_hash, 1, actor3) #this is possible if we have two epoch seeds block4_hash = ChainGenerator.insert_dummy(dag, [block2c_hash], 4) watcher.on_new_block_by_validator(block4_hash, 1, actor1) tops = dag.get_top_hashes() explicits, candidate_groups = watcher.find_conflicts_in_between(tops) self.assertEqual(len(explicits), 1) self.assertIn(block4_hash, explicits) self.assertEqual(len(candidate_groups), 1) self.assertEqual(len(candidate_groups[0]), 2) self.assertIn(block2_hash, candidate_groups[0]) self.assertIn(block2c_hash, candidate_groups[0])
def test_find_conflicts(self): dag = Dag(0) watcher = ConflictWatcher(dag) actor1 = Private.publickey(Private.generate()) actor2 = Private.publickey(Private.generate()) block1_hash = ChainGenerator.insert_dummy(dag, [dag.genesis_hash()], 1) watcher.on_new_block_by_validator(block1_hash, 1, actor1) block2_hash = ChainGenerator.insert_dummy(dag, [block1_hash], 2) watcher.on_new_block_by_validator(block2_hash, 1, actor2) block2c_hash = ChainGenerator.insert_dummy(dag, [block1_hash], 2) watcher.on_new_block_by_validator(block2c_hash, 1, actor2) # block3_hash = ChainGenerator.insert_dummy(dag, [block2_hash, block2c_hash], 3) # watcher.on_new_block_by_validator(block3_hash, 1, actor3) tops = dag.get_top_hashes() explicits, candidate_groups = watcher.find_conflicts_in_between(tops) self.assertEqual(len(explicits), 0) self.assertEqual(len(candidate_groups), 1) self.assertEqual(len(candidate_groups[0]), 2) self.assertIn(block2_hash, candidate_groups[0]) self.assertIn(block2c_hash, candidate_groups[0])
def test_explicit_conflict(self): dag = Dag(0) watcher = ConflictWatcher(dag) actor1 = Private.publickey(Private.generate()) actor2 = Private.publickey(Private.generate()) block1_hash = ChainGenerator.insert_dummy(dag, [dag.genesis_hash()], 1) watcher.on_new_block_by_validator(block1_hash, 1, actor2) block2_hash = ChainGenerator.insert_dummy(dag, [block1_hash], 2) watcher.on_new_block_by_validator(block2_hash, 1, actor1) block3_hash = ChainGenerator.insert_dummy(dag, [block2_hash], 3) watcher.on_new_block_by_validator(block3_hash, 1, actor2) block3c_hash = ChainGenerator.insert_dummy(dag, [block2_hash], 3) watcher.on_new_block_by_validator(block3c_hash, 1, actor2) tops = dag.get_top_hashes() #here block was signed by node even before merge appeared #this is explicit merge and both following blocks are conflicting explicits, candidates = watcher.find_conflicts_in_between(tops) self.assertEqual(len(explicits), 2) self.assertEqual(len(candidates), 0) self.assertIn(block3_hash, explicits) self.assertIn(block3c_hash, explicits)
def test_different_timeslot_watch(self): dag = Dag(0) conflict_watcher = ConflictWatcher(dag) actor1 = Private.publickey(Private.generate()) actor2 = Private.publickey(Private.generate()) actor3 = Private.publickey(Private.generate()) block1_hash = ChainGenerator.insert_dummy(dag, [dag.genesis_hash()], 1) conflict_watcher.on_new_block_by_validator(block1_hash, 1, actor1) block2_hash = ChainGenerator.insert_dummy(dag, [block1_hash], 2) conflict_watcher.on_new_block_by_validator(block2_hash, 1, actor2) # second block is signed by third validator # its not possible by usual means, but quite possible when we have two different epoch seeds block2c_hash = ChainGenerator.insert_dummy(dag, [block1_hash], 2) conflict_watcher.on_new_block_by_validator(block2c_hash, 1, actor3) block3_hash = ChainGenerator.insert_dummy(dag, [block2_hash, block2c_hash], 3) conflict_watcher.on_new_block_by_validator(block3_hash, 1, actor3) conflicts = conflict_watcher.get_conflicts_by_block(block3_hash) self.assertEqual(len(conflicts), 2) self.assertIn(block2c_hash, conflicts) self.assertIn(block3_hash, conflicts)
def test_merged_chain_without_transaction_conflicts(self): dag = Dag(0) block_hash, block_reward = ChainGenerator.insert_dummy_with_payments( dag, [dag.genesis_hash()], [], 1) payment1 = TransactionFactory.create_payment(block_reward, 0, [os.urandom(32)], [15]) block2_hash, block2_reward = ChainGenerator.insert_dummy_with_payments( dag, [block_hash], [payment1], 2) payment2 = TransactionFactory.create_payment(block2_reward, 0, [os.urandom(32)], [15]) block3_hash, block3_reward = ChainGenerator.insert_dummy_with_payments( dag, [block_hash], [payment2], 3) block4_hash, block4_reward = ChainGenerator.insert_dummy_with_payments( dag, [block2_hash, block3_hash], [], 4) iterator = MergingIter(dag, block4_hash) payments = [ block.block.payment_txs for block in iterator if block != None ] payments = list(reversed(payments)) spent, unspent = Resolver.resolve(payments) self.assertEqual(len(spent), 0) self.assertEqual(len(unspent), 4) self.assertIn(Entry(block3_reward, 0), unspent) self.assertIn(Entry(block4_reward, 0), unspent) self.assertIn(Entry(payment1.get_hash(), 0), unspent) self.assertIn(Entry(payment2.get_hash(), 0), unspent)
def test_merge_with_conflict_simple(self): dag = Dag(0) genesis_hash = dag.genesis_block().get_hash() block_hash1 = ChainGenerator.insert_dummy(dag, [genesis_hash], 1) conflicting_block_hash1 = ChainGenerator.insert_dummy( dag, [block_hash1], 2) conflicting_block_hash2 = ChainGenerator.insert_dummy( dag, [block_hash1], 2) block_hash3 = ChainGenerator.insert_dummy(dag, [conflicting_block_hash2], 3) # DagVisualizer.visualize(dag, True) # uncomment for discover in visualization folder conflicts = [conflicting_block_hash1] merger = Merger(dag) res = merger.merge(dag.get_top_hashes(), conflicts) res = res.filter_out_skipped_blocks() # self.assertEqual(res[0].get_hash(), genesis_hash) #no genesis hash since common ancestor is first block self.assertEqual(res[0].get_hash(), block_hash1) self.assertEqual(res[1].get_hash(), conflicting_block_hash2) self.assertEqual(res[2].get_hash(), block_hash3)
def test_same_timeslot_watch(self): dag = Dag(0) conflict_watcher = ConflictWatcher(dag) actor1 = Private.publickey(Private.generate()) actor2 = Private.publickey(Private.generate()) actor3 = Private.publickey(Private.generate()) block1_hash = ChainGenerator.insert_dummy(dag, [dag.genesis_hash()], 1) conflict_watcher.on_new_block_by_validator(block1_hash, 1, actor1) block2_hash = ChainGenerator.insert_dummy(dag, [block1_hash], 2) conflict_watcher.on_new_block_by_validator(block2_hash, 1, actor2) block2c_hash = ChainGenerator.insert_dummy(dag, [block1_hash], 2) conflict_watcher.on_new_block_by_validator(block2c_hash, 1, actor2) block3_hash = ChainGenerator.insert_dummy(dag, [block2_hash, block2c_hash], 3) conflict_watcher.on_new_block_by_validator(block3_hash, 1, actor3) conflicts = conflict_watcher.get_conflicts_by_block(block2_hash) self.assertEqual(len(conflicts), 2) self.assertIn(block2_hash, conflicts) self.assertIn(block2c_hash, conflicts) conflicts = conflict_watcher.get_conflicts_by_block(block1_hash) self.assertEqual(conflicts, None)
def test_conflicts_with_skips(self): dag = Dag(0) # generate test case # time_slot [0, 1, 2, 3, 4, 5] # ------------------------------- # 1 ------- [0, 1, 2, 3, , 5, 6, 7, 8] # 2 ------- [ , , , 3, 4, , 6, 7, 8] # 3 ------- [ , , , , 4, 5, 6, , 8] # block number 3 MUST BE signed by same key private1 = Private.generate() private2 = Private.generate() private3 = Private.generate() top_hash_1 = \ ChainGenerator.fill_with_dummies_and_skips(dag=dag, prev_hash=dag.genesis_block().get_hash(), range=range(1, 9), indices_to_skip=[4], dummy_private=private1) top_hash_2 = \ ChainGenerator.fill_with_dummies_and_skips(dag=dag, prev_hash=dag.blocks_by_number[2][0].get_hash(), range=range(3, 9), indices_to_skip=[5], dummy_private=private2) top_hash_3 = \ ChainGenerator.fill_with_dummies_and_skips(dag=dag, prev_hash=dag.blocks_by_number[3][1].get_hash(), range=range(4, 9), indices_to_skip=[7], dummy_private=private3) # DagVisualizer.visualize(dag) conflict_finder = ConflictFinder(dag) top_blocks = list(dag.get_top_blocks().keys()) top, conflicts = conflict_finder.find_conflicts(top_blocks) # assert determined top (it can be one of top_hash1,2,3) tops = [top_hash_1, top_hash_2, top_hash_3] self.assertIn(top, tops) if top == top_hash_1: # test conflicts # conflicts include all [3],[4,4],[5],[6,6],[7],[8,8] # EXCLUDE flatten top chain from list of conflict block hashes self.assertEqual(len(conflicts), 9) if top == top_hash_2: # test conflicts # conflicts include all [3],[4],[5,5],[6,6],[7],[8,8] # EXCLUDE flatten top chain from list of conflict block hashes self.assertEqual(len(conflicts), 9) if top == top_hash_3: # test conflicts # conflicts include all [3],[4],[5],[6,6],[7,7],[8,8] # EXCLUDE flatten top chain from list of conflict block hashes self.assertEqual(len(conflicts), 9)
def test_complicated_dag_with_skips(self): dag = Dag(0) # generate test case # time_slot [0, 1, 2, 3, 4, 5] # ------------------------------- # 1 ------- [-, -, 2, 3, 4, 5, , , 8] # 2 ------- [0, 1, 2, , , 5, 6, 7, 8] # 3 ------- [-, -, -, 3, 4, , 6, 7, 8] # 4 ------- [-, -, -, -, 4, 5, 6, , 8] # block number 3 MUST BE signed by same key private1 = Private.generate() private2 = Private.generate() private3 = Private.generate() private4 = Private.generate() top_hash_2 = \ ChainGenerator.fill_with_dummies_and_skips(dag=dag, prev_hash=dag.genesis_block().get_hash(), range=range(1, 9), indices_to_skip=[3, 4], dummy_private=private2) top_hash_1 = \ ChainGenerator.fill_with_dummies_and_skips(dag=dag, prev_hash=dag.blocks_by_number[1][0].get_hash(), range=range(2, 9), indices_to_skip=[6, 7], dummy_private=private1) top_hash_3 = \ ChainGenerator.fill_with_dummies_and_skips(dag=dag, prev_hash=dag.blocks_by_number[2][0].get_hash(), range=range(3, 9), indices_to_skip=[5], dummy_private=private3) top_hash_4 = \ ChainGenerator.fill_with_dummies_and_skips(dag=dag, prev_hash=dag.blocks_by_number[3][1].get_hash(), range=range(4, 9), indices_to_skip=[7], dummy_private=private4) # DagVisualizer.visualize(dag) conflict_finder = ConflictFinder(dag) top_blocks = list(dag.get_top_blocks().keys()) top, conflicts = conflict_finder.find_conflicts(top_blocks) # assert determined top (it can be one of longest top_hash_3,4) tops = [top_hash_3, top_hash_4] self.assertIn(top, tops) # test conflicts self.assertEqual(len(conflicts), 13)
def test_sane_prev_hashes_found(self): dag = Dag(0) epoch = Epoch(dag) genesis_hash = dag.genesis_block().get_hash() block_hash1 = ChainGenerator.insert_dummy(dag, [genesis_hash], 1) block_hash2 = ChainGenerator.insert_dummy(dag, [block_hash1], 2) _block_hash3 = ChainGenerator.insert_dummy(dag, [genesis_hash, block_hash2], 3) verifier = BlockAcceptor(epoch, None) with self.assertRaises(AcceptionException): verifier.validate_non_ancestor_prev_hashes( [genesis_hash, block_hash2])
def test_merge(self): dag = Dag(0) prev_hash = dag.genesis_block().get_hash() ChainGenerator.fill_with_dummies_and_skips(dag, prev_hash, range(1, 4), [1, 2]) ChainGenerator.fill_with_dummies_and_skips(dag, prev_hash, range(1, 4), [3]) merger = Merger(dag) res = merger.merge(dag.get_top_hashes()) res = res.filter_out_skipped_blocks() self.assertEqual(res[0], dag.blocks_by_number[0][0]) self.assertEqual(res[1], dag.blocks_by_number[1][0]) self.assertEqual(res[2], dag.blocks_by_number[2][0]) self.assertEqual(res[3], dag.blocks_by_number[3][0])
def test_skip_confirmation_requirement(self): dag = Dag(0) conf_req = ConfirmationRequirement(dag) genesis_hash = dag.genesis_block().get_hash() block_hash = ChainGenerator.insert_dummy(dag, [genesis_hash], 1) # confirmation requirement decreases because we have large skip confirmation_requirement = conf_req.get_confirmation_requirement( block_hash) self.assertEqual(confirmation_requirement, 5) block_hash = ChainGenerator.insert_dummy(dag, [block_hash], 3) skip = SkippedBlock(block_hash, 1) confirmation_requirement = conf_req.get_confirmation_requirement(skip) self.assertEqual(confirmation_requirement, 5) # do a larger gap block_hash = ChainGenerator.insert_dummy(dag, [block_hash], 11) confirmation_requirement = conf_req.get_confirmation_requirement( block_hash) self.assertEqual(confirmation_requirement, 3) confirmation_requirement = conf_req.get_confirmation_requirement( SkippedBlock(block_hash, 1)) self.assertEqual(confirmation_requirement, 3) confirmation_requirement = conf_req.get_confirmation_requirement( SkippedBlock(block_hash, 2)) self.assertEqual(confirmation_requirement, 4) confirmation_requirement = conf_req.get_confirmation_requirement( SkippedBlock(block_hash, 4)) self.assertEqual(confirmation_requirement, 4) confirmation_requirement = conf_req.get_confirmation_requirement( SkippedBlock(block_hash, 5)) self.assertEqual(confirmation_requirement, 5) confirmation_requirement = conf_req.get_confirmation_requirement( SkippedBlock(block_hash, 6)) self.assertEqual(confirmation_requirement, 5) confirmation_requirement = conf_req.get_confirmation_requirement( SkippedBlock(block_hash, 10)) self.assertEqual(confirmation_requirement, ZETA_MAX)
def test_common_ancestor(self): dag = ChainGenerator.generate_two_chains(5) expected_intersection = dag.blocks_by_number[1][0].get_hash() tops = dag.get_top_blocks_hashes() found_intersection = dag.get_common_ancestor([tops[0], tops[1]]) self.assertEqual(expected_intersection, found_intersection)
def test_merge_with_conflict_simplest(self): dag = Dag(0) genesis_hash = dag.genesis_block().get_hash() block_hash1 = ChainGenerator.insert_dummy(dag, [genesis_hash], 1) block_hash2 = ChainGenerator.insert_dummy(dag, [genesis_hash], 1) # DagVisualizer.visualize(dag, True) # uncomment for discover in visualization folder conflicts = [block_hash1] merger = Merger(dag) res = merger.merge(dag.get_top_hashes(), conflicts) res = res.filter_out_skipped_blocks() self.assertEqual(res[0].get_hash(), genesis_hash) self.assertEqual(res[1].get_hash(), block_hash2)
def test_sane_prev_hashes_not_found(self): dag = Dag(0) epoch = Epoch(dag) genesis_hash = dag.genesis_block().get_hash() block_hash1 = ChainGenerator.insert_dummy(dag, [genesis_hash], 1) block_hash2 = ChainGenerator.insert_dummy(dag, [block_hash1], 2) block_hash3 = ChainGenerator.insert_dummy(dag, [genesis_hash], 3) _block_hash4 = ChainGenerator.insert_dummy(dag, [block_hash2, block_hash3], 4) verifier = BlockAcceptor(epoch, None) try: verifier.validate_non_ancestor_prev_hashes( [block_hash2, block_hash3]) except: self.fail("Prev hashes should not be self referential")
def test_find_conflicts_longest_chain(self): dag = Dag(0) # generate test case # time_slot [0, 1, 2, 3, 4, 5, 6] # ------------------------------- # 1 ------- [0, 1, 2, 3, 4, 5, 6] # 2 ------- [ , , , 3, 4, 5, ] # 3 ------- [ , , , , 4, 5, ] # block number 3 MUST BE signed by same key private1 = Private.generate() private2 = Private.generate() private3 = Private.generate() determinated_top_hash = \ ChainGenerator.fill_with_dummies_and_skips(dag=dag, prev_hash=dag.genesis_block().get_hash(), range=range(1, 7), indices_to_skip=[], dummy_private=private1) ChainGenerator.fill_with_dummies_and_skips(dag=dag, prev_hash=dag.blocks_by_number[2][0].get_hash(), range=range(3, 6), indices_to_skip=[], dummy_private=private2) ChainGenerator.fill_with_dummies_and_skips(dag=dag, prev_hash=dag.blocks_by_number[3][1].get_hash(), range=range(4, 6), indices_to_skip=[], dummy_private=private3) # DagVisualizer.visualize(dag) conflict_finder = ConflictFinder(dag) top_blocks = list(dag.get_top_blocks().keys()) top, conflicts = conflict_finder.find_conflicts(top_blocks) # assert determined top self.assertEqual(determinated_top_hash, top) # test conflicts [3],[4,4],[5,5] EXCLUDE flatten top chain from list of conflict block hashes self.assertEqual(len(conflicts), 5)
def test_merged_chain_with_transaction_conflicts(self): dag = Dag(0) block_hash, block_reward = ChainGenerator.insert_dummy_with_payments( dag, [dag.genesis_hash()], [], 1) #second block spends first block reward payment1 = TransactionFactory.create_payment(block_reward, 0, [os.urandom(32)], [15]) block2_hash, block2_reward = ChainGenerator.insert_dummy_with_payments( dag, [block_hash], [payment1], 2) #third block also spends first block reward payment2 = TransactionFactory.create_payment(block_reward, 0, [os.urandom(32)], [15]) block3_hash, block3_reward = ChainGenerator.insert_dummy_with_payments( dag, [block_hash], [payment2], 3) #making block2_hash go first, so its transactions will have a priority block4_hash, block4_reward = ChainGenerator.insert_dummy_with_payments( dag, [block2_hash, block3_hash], [], 4) iterator = MergingIter(dag, block4_hash) payments = [ block.block.payment_txs for block in iterator if block != None ] payments = list(reversed(payments)) spent, unspent = Resolver.resolve(payments) self.assertEqual(len(spent), 0) self.assertEqual(len(unspent), 4) self.assertIn(Entry(block2_reward, 0), unspent) self.assertIn(Entry(block3_reward, 0), unspent) self.assertIn(Entry(block4_reward, 0), unspent) self.assertIn(Entry(payment1.get_hash(), 0), unspent) self.assertNotIn( Entry(payment2.get_hash(), 0), unspent ) #payment 2 is consedered conflicting as it goes later in merged chain
def test_simple(self): dag = Dag(0) block_hash, block_reward = ChainGenerator.insert_dummy_with_payments( dag, [dag.genesis_hash()], [], 1) payment = TransactionFactory.create_payment(block_reward, 0, [os.urandom(32)], [12]) block2_hash, block2_reward = ChainGenerator.insert_dummy_with_payments( dag, [block_hash], [payment], 2) iterator = MergingIter(dag, block2_hash) payments = [block.block.payment_txs for block in iterator] payments = list(reversed(payments)) spent, unspent = Resolver.resolve(payments) self.assertEqual(len(spent), 0) self.assertIn(Entry(block2_reward, 0), unspent) self.assertIn(Entry(payment.get_hash(), 0), unspent)
def test_complex_merge(self): dag = Dag(0) genesis_hash = dag.genesis_block().get_hash() ChainGenerator.fill_with_dummies_and_skips(dag, genesis_hash, range(1, 10), [2, 5, 7, 8]) first_block = dag.blocks_by_number[1][0].get_hash() ChainGenerator.fill_with_dummies_and_skips(dag, first_block, range(2, 10), [3, 4, 6, 7, 8, 9]) second_block = dag.blocks_by_number[2][0].get_hash() ChainGenerator.fill_with_dummies_and_skips(dag, second_block, range(3, 10), [3, 4, 5, 6, 9]) merger = Merger(dag) res = merger.merge(dag.get_top_hashes()) res = res.filter_out_skipped_blocks() # res.debug_print_block_numbers(dag) self.assertEqual(res[0], dag.blocks_by_number[1][0]) self.assertEqual(res[1], dag.blocks_by_number[3][0]) self.assertEqual(res[2], dag.blocks_by_number[4][0]) self.assertEqual(res[3], dag.blocks_by_number[6][0]) self.assertEqual(res[4], dag.blocks_by_number[9][0]) self.assertEqual( res[5], dag.blocks_by_number[2][0]) # TODO find out why is this 2 here self.assertEqual(res[6], dag.blocks_by_number[7][0]) self.assertEqual(res[7], dag.blocks_by_number[8][0]) self.assertEqual(res[8], dag.blocks_by_number[5][0])
def test_top_blocks_in_range(self): dag = Dag(0) prev_hash = dag.genesis_block().get_hash() ChainGenerator.fill_with_dummies_and_skips(dag, prev_hash, range(1, 8), [3, 5]) ChainGenerator.fill_with_dummies_and_skips(dag, prev_hash, range(1, 8), [4]) ChainGenerator.fill_with_dummies_and_skips(dag, prev_hash, range(1, 7), [4, 5]) tops = dag.get_branches_for_timeslot_range(3, 6) self.assertEqual(len(tops), 3) self.assertIn(dag.blocks_by_number[4][0].get_hash(), tops) self.assertIn(dag.blocks_by_number[5][0].get_hash(), tops) self.assertIn(dag.blocks_by_number[3][1].get_hash(), tops) tops = dag.get_branches_for_timeslot_range(4, 5) self.assertEqual(len(tops), 1) self.assertIn(dag.blocks_by_number[4][0].get_hash(), tops) tops = dag.get_branches_for_timeslot_range(3, 5) self.assertEqual(len(tops), 3) self.assertIn(dag.blocks_by_number[4][0].get_hash(), tops) self.assertIn(dag.blocks_by_number[3][0].get_hash(), tops) self.assertIn(dag.blocks_by_number[3][1].get_hash(), tops)
def test_recursive_sequence_skips(self): dag = Dag(0) conf_req = ConfirmationRequirement(dag) genesis_hash = dag.genesis_block().get_hash() block_hash = ChainGenerator.insert_dummy(dag, [genesis_hash], 1) conf_req.blocks[block_hash] = 5 block_hash = ChainGenerator.insert_dummy(dag, [block_hash], 5) # confirmation requirement decreases because we have large skip confirmation_requirement = conf_req.get_confirmation_requirement( block_hash) self.assertEqual(confirmation_requirement, 4) block_hash = ChainGenerator.insert_dummy(dag, [block_hash], 9) # DagVisualizer.visualize(dag, True) # take a look to understand what's going on confirmation_requirement = conf_req.get_confirmation_requirement( block_hash) self.assertEqual(confirmation_requirement, 3)
def test_private_keys_extraction(self): dag = Dag(0) epoch = Epoch(dag) node_private = Private.generate() prev_hash = dag.genesis_block().get_hash() round_start, round_end = Epoch.get_round_bounds(1, Round.PRIVATE) for i in range(1, round_start): block = BlockFactory.create_block_with_timestamp([prev_hash], BLOCK_TIME * i) signed_block = BlockFactory.sign_block(block, node_private) dag.add_signed_block(i, signed_block) prev_hash = block.get_hash() generated_private_keys = [] for i in range(round_start, round_end): # intentionally skip last block of round generated_private = Private.generate() generated_private_keys.append(Keys.to_bytes(generated_private)) private_key_tx = PrivateKeyTransaction() private_key_tx.key = Keys.to_bytes(generated_private) block = Block() block.system_txs = [private_key_tx] block.prev_hashes = dag.get_top_blocks_hashes() block.timestamp = i * BLOCK_TIME signed_block = BlockFactory.sign_block(block, node_private) dag.add_signed_block(i, signed_block) prev_hash = block.get_hash() ChainGenerator.fill_with_dummies(dag, prev_hash, Epoch.get_round_range(1, Round.FINAL)) epoch_hash = dag.blocks_by_number[ROUND_DURATION * 6 + 1][0].get_hash() extracted_privates = epoch.get_private_keys_for_epoch(epoch_hash) for i in range(0, ROUND_DURATION - 1): self.assertEqual(extracted_privates[i], generated_private_keys[i])
def test_explicit_conflict_right_on_common_ancestor(self): dag = Dag(0) watcher = ConflictWatcher(dag) actor2 = Private.publickey(Private.generate()) #consider common ancestor already resolved block1_hash = ChainGenerator.insert_dummy(dag, [dag.genesis_hash()], 1) watcher.on_new_block_by_validator(block1_hash, 1, actor2) block2_hash = ChainGenerator.insert_dummy(dag, [block1_hash], 2) watcher.on_new_block_by_validator(block2_hash, 1, actor2) block2c_hash = ChainGenerator.insert_dummy(dag, [block1_hash], 2) watcher.on_new_block_by_validator(block2c_hash, 1, actor2) tops = dag.get_top_hashes() explicits, candidates = watcher.find_conflicts_in_between(tops) self.assertEqual(len(explicits), 2) self.assertEqual(len(candidates), 0) self.assertIn(block2_hash, explicits) self.assertIn(block2c_hash, explicits)
def test_two_tops_on_next_epoch_middle(self): # generate two blocks on epoch end # 1 --- [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,27,29] # 2 --- [ , , ,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,27,29] epoch_range = range(1, 30) epoch_range_2 = range(3, 30) dag = Dag(0) ChainGenerator.fill_with_dummies_and_skips( dag=dag, prev_hash=dag.genesis_block().get_hash(), range=epoch_range, indices_to_skip=[]) ChainGenerator.fill_with_dummies_and_skips( dag=dag, prev_hash=dag.blocks_by_number[2][0].get_hash(), range=epoch_range_2, indices_to_skip=[]) # DagVisualizer.visualize(dag) # uncomment for discover in visualization folder tops = dag.get_top_blocks_hashes() found_intersection = dag.get_common_ancestor([tops[0], tops[1]]) expected_intersection = dag.blocks_by_number[2][0].get_hash() self.assertEqual(expected_intersection, found_intersection)
def test_recursive_sequence_found(self): dag = Dag(0) conf_req = ConfirmationRequirement(dag) genesis_hash = dag.genesis_block().get_hash() block_hash = ChainGenerator.insert_dummy(dag, [genesis_hash], 1) conf_req.blocks[block_hash] = 2 block_hash = ChainGenerator.insert_dummy(dag, [genesis_hash], 1) conf_req.blocks[block_hash] = 3 block_hash = ChainGenerator.insert_dummy(dag, dag.get_top_hashes(), 2) conf_req.blocks[block_hash] = 3 last_block_in_seq_hash = ChainGenerator.insert_dummy( dag, [genesis_hash], 1) conf_req.blocks[last_block_in_seq_hash] = 3 block_hash = ChainGenerator.insert_dummy(dag, [genesis_hash], 1) conf_req.blocks[block_hash] = 3 block_hash = ChainGenerator.insert_dummy( dag, [last_block_in_seq_hash, block_hash], 2) conf_req.blocks[block_hash] = 4 block_hash = ChainGenerator.insert_dummy(dag, dag.get_top_hashes(), 3) conf_req.blocks[block_hash] = 4 block_hash = ChainGenerator.insert_dummy(dag, dag.get_top_hashes(), 4) # DagVisualizer.visualize(dag, True) # take a look to understand what's going on # there will be only two blocks with consecutive 4 behind, which is not enough to increase best_requirement = conf_req.choose_next_best_requirement(block_hash) self.assertEqual(best_requirement, 4) # make three blocks with consecutive 4 behind # it should lead to following block increasing requirement value conf_req.blocks[last_block_in_seq_hash] = 4 best_requirement = conf_req.choose_next_best_requirement(block_hash) self.assertEqual(best_requirement, 5)
def test_penalty(self): dag = Dag(0) epoch = Epoch(dag) permissions = Permissions(epoch) node_private = Private.generate() initial_validators = Validators.read_genesis_validators_from_file() genesis_hash = dag.genesis_block().get_hash() last_block_number = Epoch.get_epoch_end_block_number(1) prev_hash = ChainGenerator.fill_with_dummies( dag, genesis_hash, range(1, last_block_number)) block = BlockFactory.create_block_with_timestamp( [prev_hash], BLOCK_TIME * last_block_number) tx = PenaltyTransaction() tx.conflicts = [prev_hash] tx.signature = Private.sign(tx.get_hash(), node_private) block.system_txs = [tx] signed_block = BlockFactory.sign_block(block, node_private) dag.add_signed_block(last_block_number, signed_block) initial_validators_order = permissions.get_signers_indexes( genesis_hash) # we substract two here: one because it is last but one block # and one, because epoch starts from 1 validator_index_to_penalize = initial_validators_order[ last_block_number - 2] resulting_validators = permissions.get_validators(block.get_hash()) self.assertNotEqual(len(initial_validators), len(resulting_validators)) initial_validators.pop(validator_index_to_penalize) init_pubkeys = list( map(lambda validator: validator.public_key, initial_validators)) result_pubkeys = list( map(lambda validator: validator.public_key, resulting_validators)) self.assertEqual(init_pubkeys, result_pubkeys)
def test_merged_dag(self): dag = Dag(0) # generate test case # time_slot [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # ---------------------------------------- # 1 ------- [0, 1, 2, 3, 4] # 2 ------- [-, -, 2, s, s] # 3 ------- [-, -, 2, s, s] # 4 ------- [-, -, 2, 3, s] # s - same block (merged) conf_req = ConfirmationRequirement(dag) genesis_hash = dag.genesis_block().get_hash() block_hash = ChainGenerator.insert_dummy(dag, [genesis_hash], 1) conf_req.blocks[block_hash] = 2 block_hash = ChainGenerator.insert_dummy(dag, [genesis_hash], 1) conf_req.blocks[block_hash] = 3 block_hash = ChainGenerator.insert_dummy(dag, dag.get_top_hashes(), 2) conf_req.blocks[block_hash] = 3 last_block_in_seq_hash = ChainGenerator.insert_dummy(dag, [genesis_hash], 1) conf_req.blocks[last_block_in_seq_hash] = 3 block_hash = ChainGenerator.insert_dummy(dag, [genesis_hash], 1) conf_req.blocks[block_hash] = 3 block_hash = ChainGenerator.insert_dummy(dag, [last_block_in_seq_hash, block_hash], 2) conf_req.blocks[block_hash] = 4 block_hash = ChainGenerator.insert_dummy(dag, dag.get_top_hashes(), 3) conf_req.blocks[block_hash] = 4 top_hash = ChainGenerator.insert_dummy(dag, dag.get_top_hashes(), 4) # DagVisualizer.visualize(dag) conflict_finder = ConflictFinder(dag) top_blocks = list(dag.get_top_blocks().keys()) top, conflicts = conflict_finder.find_conflicts(top_blocks) self.assertEqual(top, top_hash) # strong determined top # CHAIN ALREADY MERGED self.assertEqual(len(conflicts), 0)
def test_merge_in_merge(self): dag = Dag(0) genesis_hash = dag.genesis_block().get_hash() ChainGenerator.fill_with_dummies_and_skips(dag, genesis_hash, range(1, 5), [1, 3]) second_block = dag.blocks_by_number[2][0].get_hash() ChainGenerator.fill_with_dummies_and_skips(dag, second_block, range(3, 4), []) tops = dag.get_top_hashes() merging_block = BlockFactory.create_block_with_timestamp( tops, BLOCK_TIME * 5) merging_signed_block = BlockFactory.sign_block(merging_block, Private.generate()) dag.add_signed_block(5, merging_signed_block) ChainGenerator.fill_with_dummies_and_skips(dag, genesis_hash, range(1, 7), [2, 3, 4, 5]) tops = dag.get_top_hashes() merging_block = BlockFactory.create_block_with_timestamp( tops, BLOCK_TIME * 7) merging_signed_block = BlockFactory.sign_block(merging_block, Private.generate()) dag.add_signed_block(7, merging_signed_block) # DagVisualizer.visualize(dag, True) iterator = MergingIter(dag, merging_block.get_hash()) #shortest chain goes last # 3 and 4 are swapped because 4 has a priority #TODO But is it okay? Maybe we should sometimes give priority to earlier blocks if equal?1 self.assertEqual(iterator.next().get_hash(), dag.blocks_by_number[7][0].get_hash()) self.assertEqual(iterator.next().get_hash(), dag.blocks_by_number[6][0].get_hash()) self.assertEqual(iterator.next().get_hash(), dag.blocks_by_number[1][0].get_hash()) self.assertEqual(iterator.next().get_hash(), dag.blocks_by_number[5][0].get_hash()) self.assertEqual(iterator.next().get_hash(), dag.blocks_by_number[3][0].get_hash()) self.assertEqual(iterator.next().get_hash(), dag.blocks_by_number[4][0].get_hash()) self.assertEqual(iterator.next().get_hash(), dag.blocks_by_number[2][0].get_hash()) self.assertEqual(iterator.next().get_hash(), dag.blocks_by_number[0][0].get_hash())
def test_skips_and_restoring(self): dag = Dag(0) conf_req = ConfirmationRequirement(dag) genesis_hash = dag.genesis_block().get_hash() block_hash = ChainGenerator.insert_dummy(dag, [genesis_hash], 1) conf_req.blocks[block_hash] = 5 block_hash = ChainGenerator.insert_dummy(dag, [block_hash], 5) # confirmation requirement decreases because we have large skip confirmation_requirement = conf_req.get_confirmation_requirement( block_hash) self.assertEqual(confirmation_requirement, 4) block_hash = ChainGenerator.insert_dummy(dag, [block_hash], 6) block_hash = ChainGenerator.insert_dummy(dag, [block_hash], 7) #we still have 4 here confirmation_requirement = conf_req.get_confirmation_requirement( block_hash) self.assertEqual(confirmation_requirement, 4) block_hash = ChainGenerator.insert_dummy(dag, [block_hash], 8) #but we have restored to 5 here, because of 3 previous consecutive blocks confirmation_requirement = conf_req.get_confirmation_requirement( block_hash) self.assertEqual(confirmation_requirement, 5) #let's skip one block_hash = ChainGenerator.insert_dummy(dag, [block_hash], 10) block_hash = ChainGenerator.insert_dummy(dag, [block_hash], 11) # DagVisualizer.visualize(dag, True) # take a look to understand what's going on #not affected by small skip confirmation_requirement = conf_req.get_confirmation_requirement( block_hash) self.assertEqual(confirmation_requirement, 5)
def test_simple_merge(self): dag = Dag(0) genesis_hash = dag.genesis_block().get_hash() ChainGenerator.fill_with_dummies_and_skips(dag, genesis_hash, range(1, 10), [2, 5, 7, 8]) first_block = dag.blocks_by_number[1][0].get_hash() ChainGenerator.fill_with_dummies_and_skips(dag, first_block, range(2, 10), [3, 4, 6, 7, 8, 9]) second_block = dag.blocks_by_number[2][0].get_hash() ChainGenerator.fill_with_dummies_and_skips(dag, second_block, range(3, 10), [3, 4, 5, 6, 9]) hanging_tips = dag.get_top_hashes() merging_block = BlockFactory.create_block_with_timestamp( hanging_tips, BLOCK_TIME * 10) merging_signed_block = BlockFactory.sign_block(merging_block, Private.generate()) dag.add_signed_block(10, merging_signed_block) # DagVisualizer.visualize(dag, True) iterator = MergingIter(dag, merging_block.get_hash()) self.assertEqual(iterator.next().get_hash(), merging_block.get_hash()) self.assertEqual(iterator.next().get_hash(), dag.blocks_by_number[5][0].get_hash()) self.assertEqual(iterator.next().get_hash(), dag.blocks_by_number[8][0].get_hash()) self.assertEqual(iterator.next().get_hash(), dag.blocks_by_number[7][0].get_hash()) self.assertEqual(iterator.next().get_hash(), dag.blocks_by_number[2] [0].get_hash()) #TODO find out why is this 2 here self.assertEqual(iterator.next().get_hash(), dag.blocks_by_number[9][0].get_hash()) self.assertEqual(iterator.next().get_hash(), dag.blocks_by_number[6][0].get_hash()) self.assertEqual(iterator.next().get_hash(), dag.blocks_by_number[4][0].get_hash()) self.assertEqual(iterator.next().get_hash(), dag.blocks_by_number[3][0].get_hash()) self.assertEqual(iterator.next().get_hash(), dag.blocks_by_number[1][0].get_hash()) self.assertEqual(iterator.next().get_hash(), dag.blocks_by_number[0][0].get_hash())