示例#1
0
    def test_node_connect_accept(self):
        log = stdout_logger()
        params1 = NodeParams(runmode=MAIN, port=8333)
        params2 = NodeParams(runmode=MAIN, port=8334)

        node1 = Node(params1, log)
        node2 = Node(params2, log)
        node1.connect_peer(SockAddr("localhost", 8334))
        node2.subscribe(Node.EVT_CONNECTED, lambda event: reactor.stop())
        reactor.run()
示例#2
0
文件: coinpyd.py 项目: mrkovec/coinpy
 def __init__(self, args: Any) -> None:
     config = {}
     if args.addnode is not None:
         config['neighbors'] = [
             PeerAddr((host, int(port))) for _, host, port in args.addnode
         ]
     config['gen'] = args.gen
     # config['gen_ext'] = args.genext
     if args.rpcbind is not None:
         config['rpcbind'] = args.rpcbind
     # node
     self.__io_loop = asyncio.get_event_loop()
     self.__io_loop.set_debug(True)
     _, host, port = args.bind
     self.__node = Node(self.__io_loop,
                        addr=PeerAddr((host, port)),
                        **config)
     self.__io_loop.run_until_complete(self.start())
     self.__tasks = [self.__io_loop.create_task(self.__node.msg_handle())]
     # rpc server
     self.__rpc.run()
     # mining handler
     if config['gen']:
         self.__tasks.append(
             self.__io_loop.create_task(self.__node.block_mine_handle()))
     # greet neighbors
     self.__tasks.append(
         self.__io_loop.create_task(
             self.__node.commnad_send_bulk(
                 GreetCommand(self.__node.last_block.height))))
     # run daemon
     completed, pending = self.__io_loop.run_until_complete(
         asyncio.wait(self.__tasks))
示例#3
0
def step_node_start(context, node_name):
    context.port_seq += 1
    context.node[node_name] = Node(
            context.async_loop,
            addr=PeerAddr(('127.0.0.1', context.port_seq)))
    start_task = context.async_loop.create_task(context.node[node_name].start())
    context.async_loop.run_until_complete(start_task)
    assert start_task.done() is True
示例#4
0
    def test_node_send_receive_message(self):
        log = stdout_logger()
        params1 = NodeParams(runmode=MAIN, port=8335)
        params2 = NodeParams(runmode=MAIN, port=8336)

        node1 = Node(params1, log)
        node2 = Node(params2, log)

        def on_connect(event):
            node1.send_message(event.handler, VerackMessage())

        node1.subscribe(Node.EVT_CONNECTED, on_connect)

        def on_message(event):
            assert event.message.type == MSG_VERACK
            reactor.stop()

        node2.subscribe(Node.EVT_BASIC_MESSAGE, on_message)
        node1.connect_peer(SockAddr("localhost", 8336))

        reactor.run()
示例#5
0
    def test_node_startup(self) -> None:
        loop = asyncio.get_event_loop()
        node = Node(loop, addr=PeerAddr(('127.0.0.1', 50011)))

        async def _run() -> None:
            start_t = loop.create_task(node.start())
            await start_t
            stop_t = loop.create_task(node.stop())
            await stop_t
            self.assertTrue(start_t.done())
            self.assertTrue(stop_t.done())

        loop.run_until_complete(_run())