def test_shard_synchronizer_with_fork(self):
    id1 = Identity.create_random_identity()
    acc1 = Address.create_from_identity(id1, full_shard_key=0)
    with ClusterContext(2, acc1) as clusters:
        clusters[1].peer.close()
        block_list = []
        shard_state0 = clusters[0].get_shard_state(2)
        for i in range(13):
            block = _tip_gen(shard_state0)
            add_result = call_async(clusters[0].master.add_raw_minor_block(block.header.branch, block.serialize()))
            self.assertTrue(add_result)
            block_list.append(block)
        self.assertEqual(clusters[0].get_shard_state(2).header_tip.height, 13)
        shard_state0 = clusters[1].get_shard_state(2)
        for i in range(12):
            block = _tip_gen(shard_state0)
            add_result = call_async(clusters[1].master.add_raw_minor_block(block.header.branch, block.serialize()))
            self.assertTrue(add_result)
        self.assertEqual(clusters[1].get_shard_state(2).header_tip.height, 12)
        call_async(clusters[1].network.connect('127.0.0.1', clusters[0].master.env.cluster_config.SIMPLE_NETWORK.BOOTSTRAP_PORT))
        shard_state0 = clusters[0].get_shard_state(2)
        block = _tip_gen(shard_state0)
        add_result = call_async(clusters[0].master.add_raw_minor_block(block.header.branch, block.serialize()))
        self.assertTrue(add_result)
        block_list.append(block)
        for block in block_list:
            assert_true_with_timeout((lambda : clusters[1].slave_list[0].shards[Branch(2)].state.contain_block_by_hash(block.header.get_hash())))
            assert_true_with_timeout((lambda : clusters[1].master.root_state.db.contain_minor_block_by_hash(block.header.get_hash())))
        self.assertEqual(clusters[1].get_shard_state(2).header_tip, clusters[0].get_shard_state(2).header_tip)
def test_shard_synchronizer_with_fork(self):
    id1 = Identity.create_random_identity()
    acc1 = Address.create_from_identity(id1, full_shard_key=0)
    with ClusterContext(2, acc1) as clusters:
        clusters[1].peer.close()
        block_list = []
        shard_state0 = clusters[0].get_shard_state(2)
        for i in range(13):
            block = _tip_gen(shard_state0)
            add_result = call_async(clusters[0].master.add_raw_minor_block(block.header.branch, block.serialize()))
            self.assertTrue(add_result)
            block_list.append(block)
        self.assertEqual(clusters[0].get_shard_state(2).header_tip.height, 13)
        shard_state0 = clusters[1].get_shard_state(2)
        for i in range(12):
            block = _tip_gen(shard_state0)
            add_result = call_async(clusters[1].master.add_raw_minor_block(block.header.branch, block.serialize()))
            self.assertTrue(add_result)
        self.assertEqual(clusters[1].get_shard_state(2).header_tip.height, 12)
        call_async(clusters[1].network.connect('127.0.0.1', clusters[0].master.env.cluster_config.SIMPLE_NETWORK.BOOTSTRAP_PORT))
        shard_state0 = clusters[0].get_shard_state(2)
        block = _tip_gen(shard_state0)
        add_result = call_async(clusters[0].master.add_raw_minor_block(block.header.branch, block.serialize()))
        self.assertTrue(add_result)
        block_list.append(block)
        for block in block_list:
            assert_true_with_timeout((lambda : clusters[1].slave_list[0].shards[Branch(2)].state.contain_block_by_hash(block.header.get_hash())))
            assert_true_with_timeout((lambda : clusters[1].master.root_state.db.contain_minor_block_by_hash(block.header.get_hash())))
        self.assertEqual(clusters[1].get_shard_state(2).header_tip, clusters[0].get_shard_state(2).header_tip)