예제 #1
0
    def setUp(self):
        if self.network_definition is None:
            self.skipTest("This class doesn't represent a real test case.")
            raise NotImplementedError("A network definition path needs to be "
                                      "given.")

        self.testnet = Network(
            binary_folder=bin_dir,
            network_definition_location=self.network_definition,
            nodedata_folder=test_data_dir,
            node_limit='H',
            from_scratch=True
        )
        self.testnet.run_nocleanup()
        # to run the lightning network in the background and do some testing
        # here, run:
        # $ lnregtest --nodedata_folder /path/to/lndmanage/test/test_data/
        # self.testnet.run_from_background()

        # logger.info("Generated network information:")
        # logger.info(format_dict(self.testnet.node_mapping))
        # logger.info(format_dict(self.testnet.channel_mapping))
        # logger.info(format_dict(self.testnet.assemble_graph()))

        master_node_data_dir = self.testnet.master_node.data_dir
        master_node_port = self.testnet.master_node._grpc_port
        self.master_node_networkinfo = self.testnet.master_node.getnetworkinfo()

        self.lndnode = LndNode(
            lnd_home=master_node_data_dir,
            lnd_host='localhost:' + str(master_node_port),
            regtest=True
        )
        self.graph_test()
예제 #2
0
def main():
    opts = parse_args()

    testnet = Network(from_scratch=opts.from_scratch,
                      node_limit=opts.node_limit,
                      network_definition_location=opts.network_definition,
                      nodedata_folder=opts.nodedata_folder)
    testnet.run_continuously()
예제 #3
0
class TestNetwork(TestCase):
    """
    Class for spinning up simulated Lightning Networks to do integration
    testing.

    The implementation inheriting from this class needs to implement the
    graph_test method, which tests properties specific to the chosen test
    network graph. The attribute network_definition is a string that points
    to the file location of a network graph definition in terms of a dict.
    """
    network_definition = None

    def setUp(self):
        if self.network_definition is None:
            self.skipTest("This class doesn't represent a real test case.")
            raise NotImplementedError("A network definition path needs to be "
                                      "given.")

        self.testnet = Network(
            binary_folder=bin_dir,
            network_definition_location=self.network_definition,
            nodedata_folder=test_data_dir,
            node_limit='H',
            from_scratch=True
        )
        self.testnet.run_nocleanup()
        # to run the lightning network in the background and do some testing
        # here, run:
        # $ lnregtest --nodedata_folder /path/to/lndmanage/test/test_data/
        # self.testnet.run_from_background()

        # logger.info("Generated network information:")
        # logger.info(format_dict(self.testnet.node_mapping))
        # logger.info(format_dict(self.testnet.channel_mapping))
        # logger.info(format_dict(self.testnet.assemble_graph()))

        master_node_data_dir = self.testnet.master_node.data_dir
        master_node_port = self.testnet.master_node._grpc_port
        self.master_node_networkinfo = self.testnet.master_node.getnetworkinfo()

        self.lndnode = LndNode(
            lnd_home=master_node_data_dir,
            lnd_host='localhost:' + str(master_node_port),
            regtest=True
        )
        self.graph_test()

    def tearDown(self):
        self.testnet.cleanup()

    def graph_test(self):
        """
        graph_test should be implemented by each subclass test and check,
        whether the test graph has the correct shape.
        """
        raise NotImplementedError
예제 #4
0
    def dummy_test_run_from_background(self):
        """
        If you want to develop tests, you can run the network in the background
        via the executable in order to save time to not have to restart the
        network many times.
        Make sure the network definition folder argument in the test and in the
        execution are the same, e.g.:

        $ lnregtestnet --nodedata_folder /path/to/lnregtestnet/test/test_data
        """
        testnet = Network(network_definition_location='star_ring_electrum',
                          nodedata_folder=test_data_dir,
                          from_scratch=True,
                          node_limit='Z')
        testnet.run_nocleanup()
예제 #5
0
    def test_restart_network(self):
        """
        Test for creating and restoring a network.

        To be able to restore from a previously created network,
        a nodedata_folder has to be given.
        """

        # create network fixture
        testnet_from_scratch = Network(nodedata_folder=test_data_dir,
                                       network_definition_location='star_ring',
                                       from_scratch=True,
                                       node_limit='C')
        # run_once() just initializes, runs, stores and stops the network
        testnet_from_scratch.run_once()

        # use fixture to start up again
        testnet_loaded = Network(nodedata_folder=test_data_dir,
                                 network_definition_location='star_ring',
                                 from_scratch=False,
                                 node_limit='C')
        testnet_loaded.run_once()

        # Channel ids and node pub keys are random data, therefore a
        # human readable mapping needs to be defined in order those objects
        # to be addressable by developers.
        # These mappings can seen here:
        logger.info("Channel mappings:")
        logger.info(format_dict(testnet_loaded.channel_mapping))
        logger.info("Node mappings:")
        logger.info(format_dict(testnet_loaded.node_mapping))

        # Finally test if the network was restored correctly
        self.assertEqual(testnet_from_scratch.channel_mapping,
                         testnet_loaded.channel_mapping)

        self.assertEqual(testnet_from_scratch.node_mapping,
                         testnet_loaded.node_mapping)
예제 #6
0
    def test_network_start(self):
        """
        Each node has a different view of the network, which is why the
        graph has to be assembled from all the nodes via the listchannels
        command.
        """
        graph_fixture = \
            {
                "A": {
                    "1": {
                        "remote_name": "B",
                        "capacity": 4000000,
                        "local_balance": 2105264,
                        "remote_balance": 1894736,
                        "commit_fee": 0,
                        "initiator": True
                    },
                    "2": {
                        "remote_name": "C",
                        "capacity": 5000000,
                        "local_balance": 2631579,
                        "remote_balance": 2368421,
                        "commit_fee": 0,
                        "initiator": True
                    }
                },
                "B": {
                    "1": {
                        "remote_name": "A",
                        "capacity": 4000000,
                        "local_balance": 1894736,
                        "remote_balance": 2072684,
                        "commit_fee": 32580,
                        "initiator": False
                    },
                    "3": {
                        "remote_name": "C",
                        "capacity": 100000,
                        "local_balance": 96530,
                        "remote_balance": 0,
                        "commit_fee": 3140,
                        "initiator": True
                    }
                },
                "C": {
                    "3": {
                        "remote_name": "B",
                        "capacity": 100000,
                        "local_balance": 0,
                        "remote_balance": 96530,
                        "commit_fee": 3140,
                        "initiator": False
                    },
                    "2": {
                        "remote_name": "A",
                        "capacity": 5000000,
                        "local_balance": 2368421,
                        "remote_balance": 2598999,
                        "commit_fee": 32580,
                        "initiator": False
                    }
                }
            }

        testnet = Network(network_definition_location='star_ring_electrum',
                          from_scratch=True,
                          node_limit='C')

        # this try-finally construct has to be employed to keep a network
        # running asynchronously, while accessing some of its properties
        try:
            testnet.run_nocleanup()
            graph_dict = testnet.assemble_graph()
            # to create a fixture, convert lower-case bool output to proper
            # python bools:
            logger.info("Complete assembled channel graph:")
            logger.info(format_dict(graph_dict))
            self.assertTrue(
                dict_comparison(graph_dict, graph_fixture, show_diff=True))
        finally:
            testnet.cleanup()
예제 #7
0
 def setUpClass(cls):
     cls.testnet = Network(network_definition_location='star_ring',
                           from_scratch=True,
                           node_limit='H')
     cls.testnet.run_nocleanup()