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)
Exemplo n.º 2
0
    def test_peer_server_returns_all_peers_if_doesnt_have_it_or_more_than_response_amount_ipc(
            self):
        w1 = Wallet()
        p1 = Network(wallet=w1, socket_base='ipc:///tmp', ctx=self.ctx)

        test_dict = {'test': 'value', 'another': 'one', 'something': 'else'}

        p1.peer_service.table = test_dict

        find_message = ['find', 'baloney']
        find_message = json.dumps(find_message).encode()

        tasks = asyncio.gather(
            p1.peer_service.serve(), stop_server(p1.peer_service, 0.3),
            services.get(_socket('ipc:///tmp/peers'),
                         msg=find_message,
                         ctx=self.ctx,
                         timeout=300))

        loop = asyncio.get_event_loop()
        res = loop.run_until_complete(tasks)

        response = res[-1]
        response = response.decode()
        response = json.loads(response)

        self.assertDictEqual(test_dict, response)
Exemplo n.º 3
0
    def test_peer_table_updated_on_join_command(self):
        # Network params issue
        w1 = Wallet()
        p1 = Network(wallet=w1, socket_base='tcp://127.0.0.1', ctx=self.ctx)

        w2 = Wallet()
        d = DiscoveryServer(wallet=w2,
                            socket_id=_socket('tcp://127.0.0.1:19000'),
                            pepper=PEPPER.encode(),
                            ctx=self.ctx,
                            linger=200)

        # 1. start network
        # 2. start discovery of other side
        # 3. send join request
        # 4. check to see if the data has been added

        join_message = ['join', (w2.verifying_key().hex(), 'tcp://127.0.0.1')]
        join_message = json.dumps(join_message).encode()

        tasks = asyncio.gather(
            p1.peer_service.serve(), d.serve(),
            services.get(_socket('tcp://127.0.0.1:10002'),
                         msg=join_message,
                         ctx=self.ctx,
                         timeout=1000), stop_server(p1.peer_service, 0.3),
            stop_server(d, 0.3))

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

        self.assertEqual(p1.peer_service.table[w2.verifying_key().hex()],
                         'tcp://127.0.0.1')
Exemplo n.º 4
0
    def test_peer_server_returns_peer_when_asked_ipc(self):
        w1 = Wallet()
        p1 = Network(wallet=w1, socket_base='ipc:///tmp', ctx=self.ctx)

        w2 = Wallet()

        p1.peer_service.table[w2.verifying_key().hex()] = 'inproc://goodtimes'

        find_message = ['find', w2.verifying_key().hex()]
        find_message = json.dumps(find_message).encode()

        tasks = asyncio.gather(
            p1.peer_service.serve(), stop_server(p1.peer_service, 0.3),
            services.get(_socket('ipc:///tmp/peers'),
                         msg=find_message,
                         ctx=self.ctx,
                         timeout=300))

        loop = asyncio.get_event_loop()
        res = loop.run_until_complete(tasks)

        response = res[-1]
        response = response.decode()
        response = json.loads(response)

        self.assertEqual(response.get(w2.verifying_key().hex()),
                         'inproc://goodtimes')
Exemplo n.º 5
0
    def test_current_contacts_joins_mn_seed_adds_table_to_joiner(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.discovery_server.serve(),
                               stop_server(mn2.discovery_server, 0.3),
                               stop_server(mn1.peer_service, 0.3),
                               mn2.get_current_contacts())

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

        self.assertDictEqual(mn1.peer_service.table, mn2.peer_service.table)
Exemplo n.º 6
0
    def test_ask_returns_table(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'}

        ask_message = ['ask', '']
        ask_message = json.dumps(ask_message).encode()

        async def get():
            return await services.get(_socket('ipc:///tmp/n1/peers'),
                                      msg=ask_message,
                                      ctx=self.ctx,
                                      timeout=500)

        tasks = asyncio.gather(mn1.peer_service.start(),
                               stop_server(mn1.peer_service, 0.3), get())

        loop = asyncio.get_event_loop()
        res = loop.run_until_complete(tasks)

        self.assertDictEqual(json.loads(res[-1]), mn1.peer_service.table)
Exemplo n.º 7
0
    def test_start_and_stopping_destroys_servers(self):
        # Create Network service
        w1 = Wallet()
        n1 = NetworkParameters(peer_port=10001, event_port=10002)
        p1 = Network(wallet=w1,
                     ctx=self.ctx,
                     socket_base='tcp://127.0.0.1',
                     params=n1)

        # Create Network service
        w2 = Wallet()
        n2 = NetworkParameters(peer_port=10003, event_port=10004)
        p2 = Network(wallet=w2,
                     ctx=self.ctx,
                     socket_base='tcp://127.0.0.1',
                     params=n2)

        async def stop(n: Network, s):
            await asyncio.sleep(s)
            n.peer_service.stop()

        tasks = asyncio.gather(p1.peer_service.start(),
                               p2.peer_service.start(), stop(p1, 0.3),
                               stop(p2, 0.3))

        loop = asyncio.get_event_loop()
        loop.run_until_complete(tasks)
Exemplo n.º 8
0
    def test_run_single_block_mock(self):
        b = Delegate(socket_base='ipc:///tmp/n2', wallet=Wallet(), ctx=self.ctx, bootnodes=bootnodes,
                     constitution=constitution)

        gb = lamden.nodes.masternode.masternode.get_genesis_block()
        gb = canonical.dict_to_capnp_block(gb)

        # Put the genesis block in here so we start immediately
        b.nbn_inbox.q.append(gb)

        b.running = True

        # Add a single peer that we control
        b.parameters.sockets = {
            mnw1.verifying_key: 'ipc:///tmp/n1'
        }

        put_test_contract(self.client)

        b.work_inbox.work[mnw1.verifying_key] = get_tx_batch()

        async def stop():
            await asyncio.sleep(1)
            b.running = False
            b.nbn_inbox.q.append(gb)

        loop = asyncio.get_event_loop()

        tasks = asyncio.gather(
            b.run(),
            stop()
        )

        loop.run_until_complete(tasks)
Exemplo n.º 9
0
    def test_pub_sub_multi_sockets_remove_one(self):
        pub1 = self.ctx.socket(zmq.PUB)
        pub1.bind('inproc://test1')

        pub2 = self.ctx.socket(zmq.PUB)
        pub2.bind('inproc://test2')

        s = SubscriptionService(ctx=self.ctx)

        s.add_subscription(SocketStruct.from_string('inproc://test1'))
        s.add_subscription(SocketStruct.from_string('inproc://test2'))

        async def remove():
            s.remove_subscription(SocketStruct.from_string('inproc://test2'))

        async def delayed_send():
            await asyncio.sleep(0.2)
            pub2.send(b'howdy2')

        tasks = asyncio.gather(
            s.serve(),
            pub1.send(b'howdy'),
            remove(),
            stop_server(s, 0.2),
            delayed_send()
        )

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

        self.assertListEqual(s.received, [(b'howdy', 'inproc://test1')])
        self.assertListEqual(s.to_remove, [])
    def test_run_server(self):
        d = DiscoveryServer(_socket('tcp://127.0.0.1:10999'), Wallet(), b'blah')

        tasks = asyncio.gather(timeout_bomb(), d.serve())
        run_silent_loop(tasks)

        d.ctx.destroy()
    def test_nbn_puts_messages_on_q(self):
        n = NBNInbox(
            contacts=VKBook(),
            driver=BlockchainDriver(),
            socket_id=_socket('tcp://127.0.0.1:8888'),
            ctx=self.ctx,
            linger=500,
            poll_timeout=500,
            verify=False
        )

        async def send():
            socket = self.ctx.socket(zmq.DEALER)
            socket.connect('tcp://127.0.0.1:8888')
            await socket.send(b'')

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

        tasks = asyncio.gather(
            n.serve(),
            send(),
            stop()
        )

        self.loop.run_until_complete(tasks)

        self.assertEqual(len(n.q), 1)
Exemplo n.º 12
0
    def test_sending_message_returns_it(self):
        m = services.AsyncInbox(services._socket('tcp://127.0.0.1:10000'),
                                self.ctx,
                                linger=500,
                                poll_timeout=500)

        async def get(msg):
            socket = self.ctx.socket(zmq.DEALER)
            socket.connect('tcp://127.0.0.1:10000')

            await socket.send(msg)

            res = await socket.recv()

            return res

        tasks = asyncio.gather(
            m.serve(),
            get(b'howdy'),
            stop_server(m, 0.2),
        )

        loop = asyncio.get_event_loop()
        res = loop.run_until_complete(tasks)

        self.assertEqual(res[1], b'howdy')
Exemplo n.º 13
0
    def test_send_work_sends_tx_batch_properly(self):
        make_ipc('/tmp/n2')
        m = Masternode(wallet=mnw1,
                       ctx=self.ctx,
                       socket_base='ipc:///tmp/n2',
                       bootnodes=bootnodes,
                       constitution=constitution,
                       webserver_port=8080,
                       overwrite=True)

        d = MockDelegate(self.ctx)

        m.parameters.sockets = {dw1.verifying_key().hex(): 'ipc:///tmp/n1'}

        tasks = asyncio.gather(m.send_work(), d.recv_work())

        tx = make_tx(mnw1.verifying_key().hex(),
                     contract_name='howdy',
                     function_name='yoohoo')

        m.tx_batcher.queue.append(tx)

        _, msg = self.loop.run_until_complete(tasks)

        self.assertEqual(msg.transactions[0].to_dict(), tx.to_dict())
Exemplo n.º 14
0
    def test_new_blockchain_boot_sends_genesis_block_if_tx_batch(self):
        m = Masternode(wallet=mnw1,
                       ctx=self.ctx,
                       socket_base='ipc:///tmp/n1',
                       bootnodes=bootnodes,
                       constitution=constitution,
                       webserver_port=8080,
                       overwrite=True)

        m.parameters.sockets = {dw1.verifying_key: 'ipc:///tmp/n2'}

        # For mocking
        async def add_tx_batch():
            m.tx_batcher.queue.append(b'123')

        async def recv_tx_batch():
            socket = self.ctx.socket(zmq.ROUTER)
            socket.bind('ipc:///tmp/n2/block_notifications')
            _id, msg = await socket.recv_multipart()
            return msg

        tasks = asyncio.gather(m.new_blockchain_boot(), add_tx_batch(),
                               recv_tx_batch())

        _, _, msg = self.loop.run_until_complete(tasks)

        self.assertEqual(
            msg,
            canonical.dict_to_msg_block(
                lamden.nodes.masternode.masternode.get_genesis_block()))
Exemplo n.º 15
0
    def test_join_quorum_updates_with_block_if_nbn_has_block(self):
        m = Masternode(wallet=mnw1,
                       ctx=self.ctx,
                       socket_base='ipc:///tmp/n1',
                       bootnodes=bootnodes,
                       constitution=constitution,
                       webserver_port=8080,
                       overwrite=True)

        block = random_txs.random_block()

        m.nbn_inbox.q.append(block.to_dict())

        k = block.subBlocks[0].transactions[0].state[0].key
        v = block.subBlocks[0].transactions[0].state[0].value

        self.assertIsNone(m.client.raw_driver.get_direct(k))

        async def add_tx_queue():
            await asyncio.sleep(0.3)
            m.tx_batcher.queue.append(b'blah')
            m.nbn_inbox.q.append(block.to_dict())

        tasks = asyncio.gather(m.join_quorum(), add_tx_queue())

        self.loop.run_until_complete(tasks)

        self.assertEqual(m.client.raw_driver.get_direct(k), v)
    def test_new_blockchain_boot_completes_if_nbn_inbox_recieved(self):
        m = Masternode(
            wallet=mnw1,
            ctx=self.ctx,
            socket_base='ipc:///tmp/n1',
            bootnodes=bootnodes,
            constitution=constitution,
            webserver_port=8080,
            overwrite=True
        )

        # For mocking
        m.nbn_inbox.verify = False

        async def send_nbn():
            await asyncio.sleep(0.3)
            socket = self.ctx.socket(zmq.DEALER)
            socket.connect('ipc:///tmp/n1/block_notifications')
            await socket.send(b'123')
            await asyncio.sleep(0.3)
            m.nbn_inbox.stop()

        tasks = asyncio.gather(
            m.nbn_inbox.serve(),
            m.new_blockchain_boot(),
            send_nbn()
        )

        self.loop.run_until_complete(tasks)
Exemplo n.º 17
0
    def test_request_none_returns_default_message(self):
        r = router.Router(socket_id='ipc:///tmp/router',
                          ctx=self.ctx,
                          linger=50)

        async def request(msg):
            msg = encode(msg).encode()

            socket = self.ctx.socket(zmq.DEALER)
            socket.connect('ipc:///tmp/router')

            await socket.send(msg)

            resp = await socket.recv()

            resp = decode(resp)

            return resp

        bad_message = {'service': 'hello', 'blah': 123}

        tasks = asyncio.gather(
            r.serve(),
            request(bad_message),
            stop_server(r, 1),
        )

        loop = asyncio.get_event_loop()
        res = loop.run_until_complete(tasks)

        self.assertEqual(res[1], router.OK)
    def test_discover_works_with_blend_of_tcp_and_ipc(self):
        addresses = [_socket('ipc:///tmp/discover1'), _socket('tcp://127.0.0.1:11999'),
                     _socket('ipc:///tmp/woohoo')]
        wallets = [Wallet(), Wallet(), Wallet()]
        pepper = b'CORRECT_PEPPER'
        server_timeout = 0.3

        servers = [DiscoveryServer(addresses[0], wallets[0], pepper, ctx=self.ctx),
                   DiscoveryServer(addresses[1], wallets[1], pepper, ctx=self.ctx),
                   DiscoveryServer(addresses[2], wallets[2], pepper, ctx=self.ctx)]

        async def stop_server(s, timeout):
            await asyncio.sleep(timeout)
            s.stop()

        tasks = asyncio.gather(
            servers[0].serve(),
            servers[1].serve(),
            servers[2].serve(),
            stop_server(servers[0], server_timeout),
            stop_server(servers[1], server_timeout),
            stop_server(servers[2], server_timeout),
            discover_nodes(ip_list=addresses, pepper=pepper, ctx=self.ctx)
        )

        loop = asyncio.get_event_loop()
        results = loop.run_until_complete(tasks)

        r = results[-1]

        self.assertEqual(r[str(addresses[0])], wallets[0].verifying_key().hex())
        self.assertEqual(r[str(addresses[1])], wallets[1].verifying_key().hex())
        self.assertEqual(r[str(addresses[2])], wallets[2].verifying_key().hex())
Exemplo n.º 19
0
    def test_start_and_stopping_destroys_servers_ipc(self):
        # Create Network service
        w1 = Wallet()
        n1 = NetworkParameters(peer_ipc='peers1',
                               event_ipc='events1',
                               discovery_ipc='discovery1')
        p1 = Network(wallet=w1,
                     ctx=self.ctx,
                     socket_base='ipc:///tmp',
                     params=n1)

        # Create Network service
        w2 = Wallet()
        n2 = NetworkParameters(peer_ipc='peers2', event_port='events2')
        p2 = Network(wallet=w2,
                     ctx=self.ctx,
                     socket_base='ipc:///tmp',
                     params=n2)

        async def stop(n: Network, s):
            await asyncio.sleep(s)
            n.peer_service.stop()

        tasks = asyncio.gather(p1.peer_service.start(),
                               p2.peer_service.start(), stop(p1, 0.3),
                               stop(p2, 0.3))

        loop = asyncio.get_event_loop()
        loop.run_until_complete(tasks)
    def test_discover_nodes_none_found(self):
        addresses = [_socket('tcp://127.0.0.1:10999'), _socket('tcp://127.0.0.1:11999'), _socket('tcp://127.0.0.1:12999')]
        addresses_wrong = [_socket('tcp://127.0.0.1:15999'), _socket('tcp://127.0.0.1:14999'), _socket('tcp://127.0.0.1:13999')]
        wallets = [Wallet(), Wallet(), Wallet()]
        pepper = b'CORRECT_PEPPER'
        server_timeout = 1

        servers = [DiscoveryServer(addresses[0], wallets[0], pepper, ctx=self.ctx),
                   DiscoveryServer(addresses[1], wallets[1], pepper, ctx=self.ctx),
                   DiscoveryServer(addresses[2], wallets[2], pepper, ctx=self.ctx)]

        async def stop_server(s, timeout):
            await asyncio.sleep(timeout)
            s.stop()

        tasks = asyncio.gather(
            servers[0].serve(),
            servers[1].serve(),
            servers[2].serve(),
            stop_server(servers[0], server_timeout),
            stop_server(servers[1], server_timeout),
            stop_server(servers[2], server_timeout),
            discover_nodes(ip_list=addresses_wrong, pepper=pepper, ctx=self.ctx, timeout=500, retries=3)
        )

        loop = asyncio.get_event_loop()
        results = loop.run_until_complete(tasks)

        r = results[-1]

        self.assertIsNone(r.get(str(addresses[0])))
        self.assertIsNone(r.get(str(addresses[1])))
        self.assertIsNone(r.get(str(addresses[2])))
    def test_discover_works_with_ipc_sockets(self):
        wallet = Wallet()

        d = DiscoveryServer(_socket('ipc:///tmp/discovery'), wallet, b'CORRECT_PEPPER', ctx=self.ctx)

        success_task = ping(_socket('ipc:///tmp/discovery'),
                            pepper=b'CORRECT_PEPPER',
                            ctx=self.ctx,
                            timeout=300)

        failure_task = ping(_socket('tcp://127.0.0.1:20999'),
                            pepper=b'CORRECT_PEPPER',
                            ctx=self.ctx,
                            timeout=300)

        async def stop_server(timeout):
            await asyncio.sleep(timeout)
            d.stop()

        tasks = asyncio.gather(success_task, failure_task, d.serve(), stop_server(0.3))

        loop = asyncio.get_event_loop()
        results = loop.run_until_complete(tasks)

        vk_ip1, vk_ip2, _, _ = results

        _, vk1 = vk_ip1
        _, vk2 = vk_ip2

        self.assertEqual(vk1.hex(), wallet.verifying_key().hex())
        self.assertIsNone(vk2)
    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)
Exemplo n.º 23
0
    def test_get_latest_block_height(self):
        storage.set_latest_block_height(1337, self.b.driver)

        vk = Wallet()
        w = Wallet()

        self.authenticator.add_verifying_key(vk.verifying_key)
        self.authenticator.add_verifying_key(w.verifying_key)
        self.authenticator.configure()

        mn_bootnode = 'tcp://127.0.0.1:18001'
        mn_router = router.Router(socket_id=mn_bootnode,
                                  ctx=self.ctx,
                                  secure=True,
                                  wallet=vk)

        mn_router.add_service(base.BLOCK_SERVICE, self.b)

        async def send_msg():
            res = await base.get_latest_block_height(ip=mn_bootnode,
                                                     vk=vk.verifying_key,
                                                     wallet=w,
                                                     ctx=self.ctx)
            return res

        tasks = asyncio.gather(mn_router.serve(), send_msg(),
                               stop_server(mn_router, 1))

        _, res, _ = self.loop.run_until_complete(tasks)

        self.assertEqual(res, 1337)
Exemplo n.º 24
0
def main():
    global L
    global COLLECTOR_TIMEOUT
    args = parse_args()
    COLLECTOR_TIMEOUT = args.collector_timeout * 1000
    setup_logging(args)
    init_zmq_sockets(args)
    # A ticker_id based lock is needed to synchronize
    # modify_subscriptions commands for each ticker.
    g.sub_locks = defaultdict(lambda: asyncio.Lock())
    L.debug("checking capabilities ...")
    g.loop.run_until_complete(init_check_capabilities(args))
    L.debug("capabilities checked")
    L.debug("fetching old subscriptions ...")
    g.loop.run_until_complete(init_get_subscriptions())
    L.info("{} old subscription(s)".format(len(g.subscriptions)))
    L.debug("fetching available quotes ...")
    g.loop.run_until_complete(init_list_available_quotes())
    L.debug("available quotes fetched")
    tasks = [
        g.sock_deal_pub.run(),
        run_ctl_interceptor(),
    ]
    tasks = [create_task(coro_obj) for coro_obj in tasks]
    g.loop.run_until_complete(asyncio.gather(*tasks))
Exemplo n.º 25
0
    def test_wait_for_work_does_not_block_if_skip_block_and_tx_batcher_not_empty(
            self):
        make_ipc('/tmp/n2')
        m = Masternode(wallet=mnw1,
                       ctx=self.ctx,
                       socket_base='ipc:///tmp/n2',
                       bootnodes=bootnodes,
                       constitution=constitution,
                       webserver_port=8080,
                       overwrite=True)

        skip = {'subBlocks': [{'transactions': []}]}

        m.tx_batcher.queue.append(b'work')

        async def wait_then_put_in_work():
            await asyncio.sleep(0.1)
            return time.time()

        async def wait_for_work_wrapped():
            await m.wait_for_work(skip)
            return time.time()

        tasks = asyncio.gather(wait_then_put_in_work(),
                               wait_for_work_wrapped())

        r, r2 = self.loop.run_until_complete(tasks)

        # Wait for work should finish after wait then put in work
        self.assertGreater(r, r2)
Exemplo n.º 26
0
    def test_bootstrap_nodes_ipc(self):
        w = Wallet()

        w1 = Wallet()
        w2 = Wallet()
        w3 = Wallet()
        w4 = Wallet()

        bootnodes = [
            _socket('ipc:///tmp/n1/discovery'),
            _socket('ipc:///tmp/n2/discovery'),
            _socket('ipc:///tmp/n3/discovery'),
            _socket('ipc:///tmp/n4/discovery')
        ]

        d1 = DiscoveryServer(socket_id=bootnodes[0],
                             wallet=w1,
                             pepper=PEPPER.encode(),
                             ctx=self.ctx,
                             linger=1000,
                             poll_timeout=1000)
        d2 = DiscoveryServer(socket_id=bootnodes[1],
                             wallet=w2,
                             pepper=PEPPER.encode(),
                             ctx=self.ctx,
                             linger=1000,
                             poll_timeout=1000)
        d3 = DiscoveryServer(socket_id=bootnodes[2],
                             wallet=w3,
                             pepper=PEPPER.encode(),
                             ctx=self.ctx,
                             linger=1000,
                             poll_timeout=1000)
        d4 = DiscoveryServer(socket_id=bootnodes[3],
                             wallet=w4,
                             pepper=PEPPER.encode(),
                             ctx=self.ctx,
                             linger=1000,
                             poll_timeout=1000)

        n = Network(wallet=w, ctx=self.ctx, bootnodes=bootnodes)

        tasks = asyncio.gather(d1.serve(), d2.serve(), d3.serve(), d4.serve(),
                               stop_server(d1, 1), stop_server(d2, 1),
                               stop_server(d3, 1), stop_server(d4, 1),
                               n.discover_bootnodes(bootnodes))

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

        expected_dict = {
            w.verifying_key().hex(): 'tcp://0.0.0.0',
            w1.verifying_key().hex(): 'ipc:///tmp/n1',
            w2.verifying_key().hex(): 'ipc:///tmp/n2',
            w3.verifying_key().hex(): 'ipc:///tmp/n3',
            w4.verifying_key().hex(): 'ipc:///tmp/n4'
        }

        self.assertDictEqual(n.peer_service.table, expected_dict)
Exemplo n.º 27
0
    def test_multicast_sends_to_multiple(self):
        authenticator = authentication.SocketAuthenticator(
            client=ContractingClient(), ctx=self.ctx)

        w1 = Wallet()
        w2 = Wallet()
        w3 = Wallet()

        authenticator.add_verifying_key(w1.verifying_key)
        authenticator.add_verifying_key(w2.verifying_key)
        authenticator.add_verifying_key(w3.verifying_key)
        authenticator.configure()

        m1 = router.Router(socket_id='tcp://127.0.0.1:10000',
                           ctx=self.ctx,
                           linger=2000,
                           poll_timeout=50,
                           secure=True,
                           wallet=w1)

        q1 = router.QueueProcessor()
        m1.add_service('something', q1)

        m2 = router.Router(socket_id='tcp://127.0.0.1:10001',
                           ctx=self.ctx,
                           linger=2000,
                           poll_timeout=50,
                           secure=True,
                           wallet=w2)

        q2 = router.QueueProcessor()
        m2.add_service('something', q2)

        async def get():
            peers = {
                w1.verifying_key: 'tcp://127.0.0.1:10000',
                w2.verifying_key: 'tcp://127.0.0.1:10001'
            }

            await router.secure_multicast(msg={'hello': 'there'},
                                          service='something',
                                          wallet=w3,
                                          peer_map=peers,
                                          ctx=self.ctx)

        tasks = asyncio.gather(
            m1.serve(),
            m2.serve(),
            get(),
            stop_server(m1, 1),
            stop_server(m2, 1),
        )

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

        self.assertEqual(q1.q[0], {'hello': 'there'})
        self.assertEqual(q2.q[0], {'hello': 'there'})
Exemplo n.º 28
0
    def test_event_service_triggered_when_new_node_added_ipc(self):
        # Create Network service
        w1 = Wallet()
        p1 = Network(wallet=w1, ctx=self.ctx, socket_base='ipc:///tmp')

        n1 = '/tmp/n1'
        try:
            os.mkdir('/tmp/n1')
        except:
            pass

        # Create Discovery Server
        w2 = Wallet()
        d = DiscoveryServer(wallet=w2,
                            socket_id=_socket('ipc:///tmp/n1/discovery'),
                            pepper=PEPPER.encode(),
                            ctx=self.ctx,
                            poll_timeout=2000,
                            linger=200)

        # Create raw subscriber
        subscriber = self.ctx.socket(zmq.SUB)
        subscriber.setsockopt(zmq.SUBSCRIBE, b'')
        subscriber.connect('ipc:///tmp/events')

        # TCP takes a bit longer to bind and is prone to dropping messages...
        sleep(0.3)

        # Construct the join RPC message
        join_message = ['join', (w2.verifying_key().hex(), 'ipc:///tmp/n1')]
        join_message = json.dumps(join_message).encode()

        # Wrap recv() in an async
        async def recv():
            msg = await subscriber.recv()
            return msg

        tasks = asyncio.gather(
            p1.peer_service.start(
            ),  # Start the PeerService which will process RPC and emit events
            d.serve(
            ),  # Start Discovery so PeerService can verify they are online
            services.get(_socket('ipc:///tmp/peers'),
                         msg=join_message,
                         ctx=self.ctx,
                         timeout=3000),  # Push out a join request
            stop_server(p1.peer_service, 1),
            stop_server(d, 1),
            recv()  # Collect the subscription result
        )

        loop = asyncio.get_event_loop()
        res = loop.run_until_complete(tasks)

        expected_list = ['join', [w2.verifying_key().hex(), 'ipc:///tmp/n1']]
        got_list = json.loads(res[-1].decode())

        self.assertListEqual(expected_list, got_list)
Exemplo n.º 29
0
    def test_acquire_work_2_masters_gathers_tx_batches_pads_work_and_waits_if_missing(
            self):
        ips = ['tcp://127.0.0.1:18001', 'tcp://127.0.0.1:18002']

        dw = Wallet()
        mw = Wallet()
        mw2 = Wallet()

        self.authenticator.add_verifying_key(mw.verifying_key)
        self.authenticator.add_verifying_key(dw.verifying_key)
        self.authenticator.configure()

        mn = masternode.Masternode(socket_base=ips[0],
                                   ctx=self.ctx,
                                   wallet=mw,
                                   constitution={
                                       'masternodes':
                                       [mw.verifying_key, mw2.verifying_key],
                                       'delegates': [dw.verifying_key]
                                   },
                                   driver=ContractDriver(driver=InMemDriver()))

        dl = delegate.Delegate(socket_base=ips[1],
                               ctx=self.ctx,
                               wallet=dw,
                               constitution={
                                   'masternodes':
                                   [mw.verifying_key, mw2.verifying_key],
                                   'delegates': [dw.verifying_key]
                               },
                               driver=ContractDriver(driver=InMemDriver()))

        peers = {
            mw.verifying_key: ips[0],
            dw.verifying_key: ips[1],
            mw2.verifying_key: 'tpc://127.0.0.1:18003'
        }

        mn.network.peers = peers
        dl.network.peers = peers

        tasks = asyncio.gather(
            mn.router.serve(),
            dl.router.serve(),
            mn.send_work(),
            dl.acquire_work(),
            stop_server(mn.router, 1),
            stop_server(dl.router, 1),
        )

        _, _, _, w, _, _ = self.loop.run_until_complete(tasks)

        self.assertEqual(len(w), 2)
        self.assertEqual(w[0]['sender'], mw.verifying_key)

        self.assertEqual(w[1]['sender'], mw2.verifying_key)
        self.assertEqual(w[1]['input_hash'], mw2.verifying_key)
        self.assertEqual(w[1]['signature'], '0' * 128)
Exemplo n.º 30
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)