Repository: Naruno/Naruno (language: python)

Issue:
tests: Adding unit test for ongoing_main function



Discussion / hints:

Fix patch:
diff --git a/decentra_network/consensus/ongoing/ongoing_main.py b/decentra_network/consensus/ongoing/ongoing_main.py
index daf9a836..0708683c 100644
--- a/decentra_network/consensus/ongoing/ongoing_main.py
+++ b/decentra_network/consensus/ongoing/ongoing_main.py
@@ -5,22 +5,58 @@
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at https://mozilla.org/MPL/2.0/.
 from decentra_network.blockchain.block.block_main import Block
+from decentra_network.blockchain.candidate_block.candidate_block_main import \
+    candidate_block
 from decentra_network.consensus.rounds.round_1.round_1_main import \
     consensus_round_1
 from decentra_network.consensus.rounds.round_2.round_2_main import \
     consensus_round_2
 from decentra_network.lib.log import get_logger
+from decentra_network.node.client.client import client
+from decentra_network.node.server.server import server
 
 logger = get_logger("CONSENSUS")
 
 
-def ongoing_main(block: Block) -> None:
+def ongoing_main(
+    block: Block,
+    custom_candidate_class: candidate_block = None,
+    custom_unl_nodes: dict = None,
+    custom_UNL_NODES_PATH: str = None,
+    custom_server: server = None,
+    custom_unl: client = None,
+    custom_TEMP_ACCOUNTS_PATH: str = None,
+    custom_TEMP_BLOCK_PATH: str = None,
+    custom_TEMP_BLOCKSHASH_PATH: str = None,
+    custom_TEMP_BLOCKSHASH_PART_PATH: str = None,
+) -> Block:
 
     if not block.round_1:
         logger.info("First round is starting")
-        consensus_round_1(block)
+        consensus_round_1(
+            block,
+            custom_candidate_class=custom_candidate_class,
+            custom_unl_nodes=custom_unl_nodes,
+            custom_UNL_NODES_PATH=custom_UNL_NODES_PATH,
+            custom_server=custom_server,
+            custom_TEMP_ACCOUNTS_PATH=custom_TEMP_ACCOUNTS_PATH,
+            custom_TEMP_BLOCK_PATH=custom_TEMP_BLOCK_PATH,
+            custom_TEMP_BLOCKSHASH_PATH=custom_TEMP_BLOCKSHASH_PATH,
+            custom_TEMP_BLOCKSHASH_PART_PATH=custom_TEMP_BLOCKSHASH_PART_PATH,
+        )
         logger.info("First round is done")
     elif not block.round_2:
         logger.info("Second round is starting")
-        consensus_round_2(block)
+        consensus_round_2(
+            block,
+            candidate_class=custom_candidate_class,
+            unl_nodes=custom_unl_nodes,
+            custom_server=custom_server,
+            custom_unl=custom_unl,
+            custom_TEMP_BLOCK_PATH=custom_TEMP_BLOCK_PATH,
+            custom_TEMP_ACCOUNTS_PATH=custom_TEMP_ACCOUNTS_PATH,
+            custom_TEMP_BLOCKSHASH_PATH=custom_TEMP_BLOCKSHASH_PATH,
+            custom_TEMP_BLOCKSHASH_PART_PATH=custom_TEMP_BLOCKSHASH_PART_PATH,
+        )
         logger.info("Second round is done")
+    return block


Test patch:
diff --git a/tests/unit_tests/test_consensus.py b/tests/unit_tests/test_consensus.py
index d3dc4411..3a2cc0ad 100644
--- a/tests/unit_tests/test_consensus.py
+++ b/tests/unit_tests/test_consensus.py
@@ -30,6 +30,7 @@
 from decentra_network.consensus.finished.finished_main import finished_main
 from decentra_network.consensus.finished.transactions.transactions_main import \
     transactions_main as transactions_main_finished
+from decentra_network.consensus.ongoing.ongoing_main import ongoing_main
 from decentra_network.consensus.rounds.round_1.checks.candidate_blocks.candidate_blocks_main import \
     candidate_blocks_check
 from decentra_network.consensus.rounds.round_1.checks.checks_main import \
@@ -1735,5 +1736,147 @@ def test_consensus_round_2(self):
         self.assertEqual(block.round_2, True)
         self.assertNotEqual(old_block.validated_time, block.validated_time)
 
+    def test_ongoing_main_round_1(self):
+        custom_TEMP_BLOCK_PATH = "db/test_consensus_round_1_TEMP_BLOCK_PATH.json"
+        custom_TEMP_ACCOUNTS_PATH = "db/test_consensus_round_1_TEMP_ACCOUNTS_PATH.json"
+        custom_TEMP_BLOCKSHASH_PATH = (
+            "db/test_consensus_round_1_TEMP_BLOCKSHASH_PATH.json")
+        custom_TEMP_BLOCKSHASH_PART_PATH = (
+            "db/test_consensus_round_1_TEMP_BLOCKSHASH_PART_PATH.json")
+        custom_UNL_NODES_PATH = UNL_NODES_PATH.replace(".json", "_test.json")
+
+        custom_server = None
+
+        the_transaction_json = {
+            "sequance_number": 1,
+            "signature":
+            "MEUCIHABt7ypkpvFlpqL4SuogwVuzMu2gGynVkrSw6ohZ/GyAiEAg2O3iOei1Ft/vQRpboX7Sm1OOey8a3a67wPJaH/FmVE=",
+            "fromUser":
+            "MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAE0AYA7B+neqfUA17wKh3OxC67K8UlIskMm9T2qAR+pl+kKX1SleqqvLPM5bGykZ8tqq4RGtAcGtrtvEBrB9DTPg==",
+            "toUser": "onur",
+            "data": "blockchain-lab",
+            "amount": 5000.0,
+            "transaction_fee": 0.02,
+            "transaction_time": 1656764224,
+        }
+        the_transaction = Transaction.load_json(the_transaction_json)
+        validating_list = [the_transaction_json, the_transaction_json]
+
+        data_block = {
+            "transaction": validating_list,
+            "sequance_number": 58,
+        }
+
+        data_block_hash = {
+            "action": "myblockhash",
+            "hash": "onur from tests",
+            "sequance_number": 58,
+        }
+
+        CandidateBlock = candidate_block([data_block for i in range(8)],
+                                         [data_block_hash for i in range(7)])
+        validating_list = [the_transaction, the_transaction]
+
+        data_block = {
+            "signature": -1,
+            "transaction": validating_list,
+            "sequance_number": 58,
+        }
+        new_list = []
+        for i in range(8):
+            new_block = copy.copy(data_block)
+            new_block["signature"] = i
+            new_list.append(new_block)
+        CandidateBlock.candidate_blocks = new_list
+        CandidateBlock.candidate_blocks_hash = [
+            data_block_hash for i in range(7)
+        ]
+        unl_nodes = [i for i in range(10)]
+        block = Block("Onur")
+
+        block.start_time = time.time()
+        block.round_1_time = 2
+        old_block = copy.copy(block)
+        time.sleep(4)
+        result = ongoing_main(
+            block,
+            CandidateBlock,
+            unl_nodes,
+            custom_UNL_NODES_PATH=custom_UNL_NODES_PATH,
+            custom_server=custom_server,
+            custom_TEMP_BLOCK_PATH=custom_TEMP_BLOCK_PATH,
+            custom_TEMP_ACCOUNTS_PATH=custom_TEMP_ACCOUNTS_PATH,
+            custom_TEMP_BLOCKSHASH_PATH=custom_TEMP_BLOCKSHASH_PATH,
+            custom_TEMP_BLOCKSHASH_PART_PATH=custom_TEMP_BLOCKSHASH_PART_PATH,
+        )
+        self.assertEqual(len(result.validating_list), 1)
+        self.assertEqual(result.validating_list[0].dump_json(),
+                         the_transaction.dump_json())
+        self.assertEqual(result.round_1, True)
+        self.assertNotEqual(result.round_2_starting_time,
+                            old_block.round_2_starting_time)
+        self.assertNotEqual(result.hash, old_block.hash)
+
+    def test_ongoing_main_round_2(self):
+        the_transaction_json = {
+            "sequance_number": 1,
+            "signature":
+            "MEUCIHABt7ypkpvFlpqL4SuogwVuzMu2gGynVkrSw6ohZ/GyAiEAg2O3iOei1Ft/vQRpboX7Sm1OOey8a3a67wPJaH/FmVE=",
+            "fromUser":
+            "MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAE0AYA7B+neqfUA17wKh3OxC67K8UlIskMm9T2qAR+pl+kKX1SleqqvLPM5bGykZ8tqq4RGtAcGtrtvEBrB9DTPg==",
+            "toUser": "onur",
+            "data": "blockchain-lab",
+            "amount": 5000.0,
+            "transaction_fee": 0.02,
+            "transaction_time": 1656764224,
+        }
+        validating_list = [the_transaction_json, the_transaction_json]
+
+        data_block = {
+            "transaction": validating_list,
+            "sequance_number": 58,
+        }
+
+        data_block_hash = {
+            "action": "myblockhash",
+            "hash": "onur from tests",
+            "sequance_number": 58,
+            "sender": self.node_0.id,
+        }
+
+        CandidateBlock = candidate_block([data_block for i in range(7)],
+                                         [data_block_hash for i in range(8)])
+        the_new_list = []
+        for i in range(8):
+            the_new_object = copy.copy(data_block_hash)
+            the_new_object["sender"] = i
+            the_new_list.append(the_new_object)
+        CandidateBlock.candidate_block_hashes = the_new_list
+        unl_nodes = [i for i in range(10)]
+        block = Block("Onur")
+        block.round_1 = True
+        block.hash = "onur from tests"
+        old_block = copy.copy(block)
+
+        block.round_2_starting_time = time.time()
+        block.round_2_time = 2
+        time.sleep(4)
+        result = ongoing_main(
+            block,
+            CandidateBlock,
+            unl_nodes,
+            custom_server=self.node_1,
+            custom_unl=self.node_1.clients[0],
+            custom_TEMP_BLOCK_PATH=self.custom_TEMP_BLOCK_PATH1,
+            custom_TEMP_ACCOUNTS_PATH=self.custom_TEMP_ACCOUNTS_PATH1,
+            custom_TEMP_BLOCKSHASH_PATH=self.custom_TEMP_BLOCKSHASH_PATH1,
+            custom_TEMP_BLOCKSHASH_PART_PATH=self.
+            custom_TEMP_BLOCKSHASH_PART_PATH1,
+        )
+        self.assertTrue(result)
+        self.assertEqual(block.validated, True)
+        self.assertEqual(block.round_2, True)
+        self.assertNotEqual(old_block.validated_time, block.validated_time)
+
 
 unittest.main(exit=False)