def test_resync_removes_old_contact(self):
        p1 = Network(wallet=self.wallet,
                     ctx=self.ctx,
                     socket_base='tcp://127.0.0.1')

        p1.peer_service.table = self.peer_table

        new_wallet = Wallet()

        p1.peer_service.table[
            new_wallet.verifying_key().hex()] = 'ipc:///tmp/n3'
        self.contacts.masternodes.append(new_wallet.verifying_key().hex())
        self.authenticator.sync_certs()

        p = Peers(wallet=self.wallet,
                  ctx=self.ctx,
                  parameters=self.paramaters,
                  node_type=MN,
                  service_type=ServiceType.INCOMING_WORK)

        async def late_refresh():
            await asyncio.sleep(0.3)
            await self.paramaters.refresh()
            p.sync_sockets()

        async def stop():
            await asyncio.sleep(0.5)
            p1.stop()

        tasks = asyncio.gather(p1.start(discover=False), late_refresh(),
                               stop())

        self.loop.run_until_complete(tasks)

        socket_1 = p.sockets.get(new_wallet.verifying_key().hex())
        self.assertIsNotNone(socket_1)

        del p1.peer_service.table[new_wallet.verifying_key().hex()]
        self.contacts.masternodes.remove(new_wallet.verifying_key().hex())

        self.authenticator.sync_certs()

        async def late_refresh():
            await asyncio.sleep(0.3)
            await self.paramaters.refresh()
            p.sync_sockets()

        async def stop():
            await asyncio.sleep(0.5)
            p1.stop()

        tasks = asyncio.gather(p1.start(discover=False), late_refresh(),
                               stop())

        self.loop.run_until_complete(tasks)

        socket_1 = p.sockets.get(new_wallet.verifying_key().hex())
        self.assertIsNone(socket_1)
Пример #2
0
    def test_network_start(self):
        # 4 nodes
        # 2 bootnodes
        # 2 mns, 2 delegates

        bootnodes = ['ipc:///tmp/n1', 'ipc:///tmp/n3']

        mnw1 = Wallet()
        mnw2 = Wallet()
        masternodes = [mnw1.verifying_key().hex(), mnw2.verifying_key().hex()]

        dw1 = Wallet()
        dw2 = Wallet()
        delegates = [dw1.verifying_key().hex(), dw2.verifying_key().hex()]

        n1 = '/tmp/n1'
        make_ipc(n1)
        mn1 = Network(wallet=mnw1,
                      ctx=self.ctx,
                      socket_base=f'ipc://{n1}',
                      bootnodes=bootnodes,
                      mn_to_find=masternodes,
                      del_to_find=delegates)

        n2 = '/tmp/n2'
        make_ipc(n2)
        mn2 = Network(wallet=mnw2,
                      ctx=self.ctx,
                      socket_base=f'ipc://{n2}',
                      bootnodes=bootnodes,
                      mn_to_find=masternodes,
                      del_to_find=delegates)

        n3 = '/tmp/n3'
        make_ipc(n3)
        d1 = Network(wallet=dw1,
                     ctx=self.ctx,
                     socket_base=f'ipc://{n3}',
                     bootnodes=bootnodes,
                     mn_to_find=masternodes,
                     del_to_find=delegates)

        n4 = '/tmp/n4'
        make_ipc(n4)
        d2 = Network(wallet=dw2,
                     ctx=self.ctx,
                     socket_base=f'ipc://{n4}',
                     bootnodes=bootnodes,
                     mn_to_find=masternodes,
                     del_to_find=delegates)

        # should test to see all ready signals are recieved
        tasks = asyncio.gather(mn1.start(), mn2.start(), d1.start(),
                               d2.start())

        loop = asyncio.get_event_loop()
        loop.run_until_complete(tasks)
    def test_sync_adds_del_if_peers_mn(self):
        p1 = Network(wallet=self.wallet,
                     ctx=self.ctx,
                     socket_base='tcp://127.0.0.1')

        p1.peer_service.table = self.peer_table

        p = Peers(wallet=self.wallet,
                  ctx=self.ctx,
                  parameters=self.paramaters,
                  node_type=MN,
                  service_type=ServiceType.INCOMING_WORK)

        self.authenticator.sync_certs()

        async def late_refresh():
            await asyncio.sleep(0.3)
            await self.paramaters.refresh()
            p.sync_sockets()

        async def stop():
            await asyncio.sleep(0.5)
            p1.stop()

        tasks = asyncio.gather(p1.start(discover=False), late_refresh(),
                               stop())

        self.loop.run_until_complete(tasks)

        socket_1 = p.sockets.get(self.test_wallet_1.verifying_key().hex())

        self.assertIsNotNone(socket_1)
    def test_get_sockets_with_service(self):
        constitution = self.get_vkbook_args()
        sync.submit_from_genesis_json_file(cilantro_ee.contracts.__path__[0] +
                                           '/genesis.json',
                                           client=self.client)
        sync.submit_node_election_contracts(
            initial_masternodes=constitution['masternodes'],
            boot_mns=constitution['masternode_min_quorum'],
            initial_delegates=constitution['delegates'],
            boot_dels=constitution['delegate_min_quorum'],
            client=self.client)

        PhoneBook = VKBook(client=self.client)

        w1 = Wallet()

        p1 = Network(wallet=w1, ctx=self.ctx, socket_base='tcp://127.0.0.1')

        # 'tcp://127.0.0.1:10003'

        raw = {'stu': 'tcp://127.0.0.1', 'raghu': 'tcp://127.0.0.2'}
        p1.peer_service.table = raw

        expected = {
            'stu': _socket('tcp://127.0.0.1:19003'),
            'raghu': _socket('tcp://127.0.0.2:19003')
        }

        # CHANGE CLIENT TO SOCKET
        masternodes = Parameters(socket_base='tcp://127.0.0.1',
                                 wallet=w1,
                                 contacts=MockContacts(['stu'],
                                                       delegates=['raghu']),
                                 ctx=self.ctx)

        self.assertDictEqual(masternodes.sockets, {})

        async def late_refresh():
            await asyncio.sleep(0.3)
            await masternodes.refresh()

        async def stop():
            await asyncio.sleep(0.5)
            p1.stop()

        tasks = asyncio.gather(p1.start(discover=False), late_refresh(),
                               stop())

        self.loop.run_until_complete(tasks)

        self.assertEqual(
            masternodes.get_all_sockets(service=ServiceType.EVENT), expected)
Пример #5
0
    def test_refresh(self):
        vkbook_args = self.get_vkbook_args()
        sync.submit_vkbook(vkbook_args, overwrite=True)

        PhoneBook = VKBook()

        w1 = Wallet()

        p1 = Network(wallet=w1, ctx=self.ctx, socket_base='tcp://127.0.0.1')

        #'tcp://127.0.0.1:10003'

        raw = {'stu': 'tcp://127.0.0.1', 'raghu': 'tcp://127.0.0.2'}
        p1.peer_service.table.peers = raw

        expected = {
            'stu': _socket('tcp://127.0.0.1:{}'.format(EVENT_PORT)),
            'raghu': _socket('tcp://127.0.0.2:{}'.format(EVENT_PORT))
        }

        # CHANGE CLIENT TO SOCKET
        masternodes = SocketBook(
            socket_base='tcp://127.0.0.1',
            service_type=ServiceType.EVENT,
            ctx=self.ctx,
            phonebook_function=PhoneBook.contract.get_masternodes)

        self.assertDictEqual(masternodes.sockets, {})

        async def late_refresh():
            await asyncio.sleep(0.3)
            await masternodes.refresh()

        async def stop():
            await asyncio.sleep(0.5)
            p1.stop()

        tasks = asyncio.gather(p1.start(discover=False), late_refresh(),
                               stop())

        self.loop.run_until_complete(tasks)

        self.assertDictEqual(masternodes.sockets, expected)
Пример #6
0
    def test_contacts_added_if_network_starts_up_and_mn_seed_provided(self):
        n1 = '/tmp/n1'
        make_ipc(n1)

        mnw1 = Wallet()
        mn1 = Network(wallet=mnw1, ctx=self.ctx, socket_base=f'ipc://{n1}')

        mn1.peer_service.table = {'a': 'b', 'c': 'd', 'e': 'f'}

        mnw2 = Wallet()
        n2 = '/tmp/n2'
        make_ipc(n2)
        mn2 = Network(wallet=mnw2,
                      ctx=self.ctx,
                      socket_base=f'ipc://{n2}',
                      mn_seed='ipc:///tmp/n1')

        tasks = asyncio.gather(mn1.peer_service.start(), mn2.start(),
                               stop_server(mn1.peer_service, 1))

        loop = asyncio.get_event_loop()
        loop.run_until_complete(tasks)
Пример #7
0
    def test_refresh_remove_old_nodes(self):
        vkbook_args = self.get_vkbook_args()
        sync.submit_vkbook(vkbook_args, overwrite=True)

        PhoneBook = VKBook()

        w1 = Wallet()
        p1 = Network(wallet=w1, ctx=self.ctx, socket_base='tcp://127.0.0.1')

        peeps = {
            'stu': 'tcp://127.0.0.1',
            'raghu': 'tcp://127.0.0.8',
            'tejas': 'tcp://127.0.2.1',
            'steve': 'tcp://127.0.54.6'
        }

        p1.peer_service.table.peers = peeps

        ctx2 = zmq.asyncio.Context()

        masternodes = SocketBook(
            socket_base='tcp://127.0.0.1',
            service_type=ServiceType.EVENT,
            ctx=ctx2,
            phonebook_function=PhoneBook.contract.get_masternodes)

        self.assertDictEqual(masternodes.sockets, {})

        async def late_refresh():
            await asyncio.sleep(0.3)
            await masternodes.refresh()

        async def stop():
            await asyncio.sleep(0.5)
            p1.stop()

        tasks = asyncio.gather(p1.start(discover=False), late_refresh(),
                               stop())

        self.loop.run_until_complete(tasks)

        expected = {
            'stu': _socket('tcp://127.0.0.1:{}'.format(EVENT_PORT)),
            'raghu': _socket('tcp://127.0.0.8:{}'.format(EVENT_PORT))
        }

        self.assertDictEqual(masternodes.sockets, expected)

        self.ctx.destroy()
        self.loop.close()

        self.ctx = zmq.asyncio.Context()
        self.loop = asyncio.new_event_loop()
        asyncio.set_event_loop(self.loop)

        w1 = Wallet()
        p1 = Network(wallet=w1, ctx=self.ctx, socket_base='tcp://127.0.0.1')

        peeps = {
            'stu': 'tcp://127.0.2.1',
            'raghu': 'tcp://127.0.0.8',
            'tejas': 'tcp://127.0.2.1',
            'steve': 'tcp://127.0.54.6'
        }

        p1.peer_service.table.peers = peeps

        vkbook_args = self.get_vkbook_args(mns=['stu', 'tejas'])
        sync.submit_vkbook(vkbook_args, overwrite=True)

        async def late_refresh():
            await asyncio.sleep(0.3)
            await masternodes.refresh()

        async def stop():
            await asyncio.sleep(1)
            p1.stop()

        tasks = asyncio.gather(p1.start(discover=False), late_refresh(),
                               stop())

        self.loop.run_until_complete(tasks)

        expected = {
            'stu': _socket('tcp://127.0.2.1:{}'.format(EVENT_PORT)),
            'tejas': _socket('tcp://127.0.2.1:{}'.format(EVENT_PORT)),
        }

        self.assertDictEqual(masternodes.sockets, expected)
    def test_refresh_remove_old_nodes(self):
        constitution = self.get_vkbook_args()
        sync.submit_from_genesis_json_file(cilantro_ee.contracts.__path__[0] +
                                           '/genesis.json',
                                           client=self.client)
        sync.submit_node_election_contracts(
            initial_masternodes=constitution['masternodes'],
            boot_mns=constitution['masternode_min_quorum'],
            initial_delegates=constitution['delegates'],
            boot_dels=constitution['delegate_min_quorum'],
            client=self.client)

        PhoneBook = VKBook(client=self.client)

        w1 = Wallet()
        p1 = Network(wallet=w1, ctx=self.ctx, socket_base='tcp://127.0.0.1')

        peeps = {
            'stu': 'tcp://127.0.0.1',
            'raghu': 'tcp://127.0.0.8',
            'tejas': 'tcp://127.0.2.1',
            'steve': 'tcp://127.0.54.6'
        }

        p1.peer_service.table = peeps

        ctx2 = zmq.asyncio.Context()

        masternodes = Parameters(socket_base='tcp://127.0.0.1',
                                 wallet=w1,
                                 contacts=MockContacts(['stu', 'raghu'],
                                                       ['tejas', 'steve']),
                                 ctx=ctx2)

        self.assertDictEqual(masternodes.sockets, {})

        async def late_refresh():
            await asyncio.sleep(0.3)
            await masternodes.refresh()

        async def stop():
            await asyncio.sleep(0.5)
            p1.stop()

        tasks = asyncio.gather(p1.start(discover=False), late_refresh(),
                               stop())

        self.loop.run_until_complete(tasks)

        expected = {'stu': 'tcp://127.0.0.1', 'raghu': 'tcp://127.0.0.8'}

        self.assertDictEqual(masternodes.sockets, expected)

        self.ctx.destroy()
        self.loop.close()

        self.ctx = zmq.asyncio.Context()
        self.loop = asyncio.new_event_loop()
        asyncio.set_event_loop(self.loop)

        w1 = Wallet()
        p1 = Network(wallet=w1, ctx=self.ctx, socket_base='tcp://127.0.0.1')

        peeps = {
            'stu': 'tcp://127.0.2.1',
            'raghu': 'tcp://127.0.0.8',
            'tejas': 'tcp://127.0.2.1',
            'steve': 'tcp://127.0.54.6'
        }

        p1.peer_service.table.peers = peeps

        vkbook_args = self.get_vkbook_args(mns=['stu', 'tejas'])
        sync.submit_vkbook(vkbook_args, overwrite=True)

        async def late_refresh():
            await asyncio.sleep(0.3)
            await masternodes.refresh()

        async def stop():
            await asyncio.sleep(1)
            p1.stop()

        tasks = asyncio.gather(p1.start(discover=False), late_refresh(),
                               stop())

        self.loop.run_until_complete(tasks)

        expected = {
            'stu': 'tcp://127.0.2.1',
            'tejas': 'tcp://127.0.2.1',
        }

        self.assertDictEqual(masternodes.sockets, expected)
    def test_network_start(self):
        # 4 nodes
        # 2 bootnodes
        # 2 mns, 2 delegates

        bootnodes = ['ipc:///tmp/n1', 'ipc:///tmp/n3']

        mnw1 = Wallet()
        mnw2 = Wallet()
        masternodes = [mnw1.verifying_key().hex(), mnw2.verifying_key().hex()]

        dw1 = Wallet()
        dw2 = Wallet()
        delegates = [dw1.verifying_key().hex(), dw2.verifying_key().hex()]

        constitution = {
            "masternodes": [
                    mnw1.verifying_key().hex(),
                    mnw2.verifying_key().hex()
                ],
            "masternode_min_quorum": 1,
            "delegates": [
                    dw1.verifying_key().hex(),
                    dw2.verifying_key().hex()
                ],
            "delegate_min_quorum": 1,
            "witnesses": {},
            "schedulers": {},
            "notifiers": {},
            "enable_stamps": False,
            "enable_nonces": False
        }

        n1 = '/tmp/n1'
        make_ipc(n1)
        mn1 = Masternode(wallet=mnw1, ctx=self.ctx, socket_base=f'ipc://{n1}', bootnodes=bootnodes,
                         constitution=constitution, webserver_port=8080)

        n2 = '/tmp/n2'
        make_ipc(n2)
        mn2 = Masternode(wallet=mnw2, ctx=self.ctx, socket_base=f'ipc://{n2}', bootnodes=bootnodes,
                         constitution=constitution, webserver_port=8081)

        n3 = '/tmp/n3'
        make_ipc(n3)
        d1 = Network(wallet=dw1, ctx=self.ctx, socket_base=f'ipc://{n3}',
                     bootnodes=bootnodes, mn_to_find=masternodes, del_to_find=delegates)

        n4 = '/tmp/n4'
        make_ipc(n4)
        d2 = Network(wallet=dw2, ctx=self.ctx, socket_base=f'ipc://{n4}',
                     bootnodes=bootnodes, mn_to_find=masternodes, del_to_find=delegates)


        # should test to see all ready signals are recieved
        tasks = asyncio.gather(
            mn1.start(),
            mn2.start(),
            d1.start(),
            d2.start()
        )

        async def run():
            await tasks
            await asyncio.sleep(5)
            mn1.stop()
            mn2.stop()
            d1.stop()
            d2.stop()

        loop = asyncio.get_event_loop()
        loop.run_until_complete(run())