コード例 #1
0
async def setup_introducer(port, dic={}):
    net_config = load_config(root_path, "config.yaml")
    ping_interval = net_config.get("ping_interval")
    network_id = net_config.get("network_id")

    config = load_config(root_path, "config.yaml", "introducer")

    introducer = Introducer(config["max_peers_to_send"],
                            config["recent_peer_threshold"])
    assert ping_interval is not None
    assert network_id is not None
    server = ChiaServer(
        port,
        introducer,
        NodeType.INTRODUCER,
        ping_interval,
        network_id,
        bt.root_path,
        config,
        f"introducer_server_{port}",
    )
    _ = await start_server(server)

    yield (introducer, server)

    _.close()
    server.close_all()
    await server.await_closed()
コード例 #2
0
async def main():
    timelord = Timelord()
    host, port = parse_host_port(timelord)
    server = ChiaServer(port, timelord, NodeType.TIMELORD)
    _ = await server.start_server(host, None)

    def signal_received():
        server.close_all()
        asyncio.create_task(timelord._shutdown())

    asyncio.get_running_loop().add_signal_handler(signal.SIGINT,
                                                  signal_received)
    asyncio.get_running_loop().add_signal_handler(signal.SIGTERM,
                                                  signal_received)

    full_node_peer = PeerInfo(
        timelord.config["full_node_peer"]["host"],
        timelord.config["full_node_peer"]["port"],
    )

    await asyncio.sleep(1)  # Prevents TCP simultaneous connect with full node
    await server.start_client(full_node_peer, None)

    async for msg in timelord._manage_discriminant_queue():
        server.push_message(msg)

    await server.await_closed()
コード例 #3
0
async def setup_introducer(port, dic={}):
    net_config = load_config(root_path, "config.yaml")
    ping_interval = net_config.get("ping_interval")
    network_id = net_config.get("network_id")

    config = load_config(root_path, "config.yaml", "introducer")

    introducer = Introducer(config)
    assert ping_interval is not None
    assert network_id is not None
    server = ChiaServer(
        port,
        introducer,
        NodeType.INTRODUCER,
        ping_interval,
        network_id,
        bt.root_path,
        config,
    )
    _ = await server.start_server(None)

    yield (introducer, server)

    server.close_all()
    await server.await_closed()
コード例 #4
0
async def main():
    config = load_config_cli("config.yaml", "timelord")

    initialize_logging("Timelord %(name)-23s", config["logging"])
    log = logging.getLogger(__name__)
    setproctitle("chia_timelord")

    timelord = Timelord(config)
    server = ChiaServer(config["port"], timelord, NodeType.TIMELORD)
    _ = await server.start_server(config["host"], None)

    def signal_received():
        server.close_all()
        asyncio.create_task(timelord._shutdown())

    asyncio.get_running_loop().add_signal_handler(signal.SIGINT, signal_received)
    asyncio.get_running_loop().add_signal_handler(signal.SIGTERM, signal_received)

    full_node_peer = PeerInfo(
        timelord.config["full_node_peer"]["host"],
        timelord.config["full_node_peer"]["port"],
    )

    await asyncio.sleep(1)  # Prevents TCP simultaneous connect with full node
    await server.start_client(full_node_peer, None)

    async for msg in timelord._manage_discriminant_queue():
        server.push_message(msg)

    await server.await_closed()
    log.info("Timelord fully closed.")
コード例 #5
0
ファイル: start_service.py プロジェクト: spring3th/Exodus
    def __init__(
        self,
        root_path,
        api: Any,
        node_type: NodeType,
        advertised_port: int,
        service_name: str,
        server_listen_ports: List[int] = [],
        connect_peers: List[PeerInfo] = [],
        on_connect_callback: Optional[OutboundMessage] = None,
        rpc_start_callback_port: Optional[Tuple[Callable, int]] = None,
        start_callback: Optional[Callable] = None,
        stop_callback: Optional[Callable] = None,
        await_closed_callback: Optional[Callable] = None,
        periodic_introducer_poll: Optional[Tuple[PeerInfo, int, int]] = None,
    ):
        net_config = load_config(root_path, "config.yaml")
        ping_interval = net_config.get("ping_interval")
        network_id = net_config.get("network_id")
        assert ping_interval is not None
        assert network_id is not None

        self._node_type = node_type

        proctitle_name = f"chia_{service_name}"
        setproctitle(proctitle_name)
        self._log = logging.getLogger(service_name)

        config = load_config_cli(root_path, "config.yaml", service_name)
        initialize_logging(f"{service_name:<30s}", config["logging"],
                           root_path)

        self._rpc_start_callback_port = rpc_start_callback_port

        self._server = ChiaServer(
            config["port"],
            api,
            node_type,
            ping_interval,
            network_id,
            root_path,
            config,
        )
        for _ in ["set_server", "_set_server"]:
            f = getattr(api, _, None)
            if f:
                f(self._server)

        self._connect_peers = connect_peers
        self._server_listen_ports = server_listen_ports

        self._api = api
        self._task = None
        self._is_stopping = False

        self._periodic_introducer_poll = periodic_introducer_poll
        self._on_connect_callback = on_connect_callback
        self._start_callback = start_callback
        self._stop_callback = stop_callback
        self._await_closed_callback = await_closed_callback
コード例 #6
0
async def setup_harvester(port, dic={}):
    config = load_config(root_path, "config.yaml", "harvester")

    harvester = await Harvester.create(config, bt.plot_config)

    net_config = load_config(root_path, "config.yaml")
    ping_interval = net_config.get("ping_interval")
    network_id = net_config.get("network_id")
    assert ping_interval is not None
    assert network_id is not None
    server = ChiaServer(
        port,
        harvester,
        NodeType.HARVESTER,
        ping_interval,
        network_id,
        root_path,
        config,
        f"harvester_server_{port}",
    )

    yield (harvester, server)

    harvester._shutdown()
    server.close_all()
    await harvester._await_shutdown()
    await server.await_closed()
コード例 #7
0
async def setup_farmer(port, dic={}):
    config = load_config(root_path, "config.yaml", "farmer")
    pool_sk = bt.pool_sk
    pool_target = create_puzzlehash_for_pk(
        BLSPublicKey(bytes(pool_sk.get_public_key())))
    wallet_sk = bt.wallet_sk
    wallet_target = create_puzzlehash_for_pk(
        BLSPublicKey(bytes(wallet_sk.get_public_key())))

    key_config = {
        "wallet_sk": bytes(wallet_sk).hex(),
        "wallet_target": wallet_target.hex(),
        "pool_sks": [bytes(pool_sk).hex()],
        "pool_target": pool_target.hex(),
    }
    test_constants_copy = test_constants.copy()
    for k in dic.keys():
        test_constants_copy[k] = dic[k]

    net_config = load_config(root_path, "config.yaml")
    ping_interval = net_config.get("ping_interval")
    network_id = net_config.get("network_id")

    farmer = Farmer(config, key_config, test_constants_copy)
    assert ping_interval is not None
    assert network_id is not None
    server = ChiaServer(port, farmer, NodeType.FARMER, ping_interval,
                        network_id)
    _ = await server.start_server(farmer._on_connect)

    yield (farmer, server)

    server.close_all()
    await server.await_closed()
コード例 #8
0
async def setup_full_node(db_name, port, introducer_port=None, dic={}):
    # SETUP
    test_constants_copy = test_constants.copy()
    for k in dic.keys():
        test_constants_copy[k] = dic[k]

    db_path = Path(db_name)
    connection = await aiosqlite.connect(db_path)
    store_1 = await FullNodeStore.create(connection)
    await store_1._clear_database()
    unspent_store_1 = await CoinStore.create(connection)
    await unspent_store_1._clear_database()
    mempool_1 = MempoolManager(unspent_store_1, test_constants_copy)

    b_1: Blockchain = await Blockchain.create(unspent_store_1, store_1,
                                              test_constants_copy)
    await mempool_1.new_tips(await b_1.get_full_tips())

    await store_1.add_block(
        FullBlock.from_bytes(test_constants_copy["GENESIS_BLOCK"]))

    net_config = load_config(root_path, "config.yaml")
    ping_interval = net_config.get("ping_interval")
    network_id = net_config.get("network_id")

    config = load_config(root_path, "config.yaml", "full_node")
    if introducer_port is not None:
        config["introducer_peer"]["host"] = "127.0.0.1"
        config["introducer_peer"]["port"] = introducer_port
    full_node_1 = FullNode(
        store_1,
        b_1,
        config,
        mempool_1,
        unspent_store_1,
        f"full_node_{port}",
        test_constants_copy,
    )
    assert ping_interval is not None
    assert network_id is not None
    server_1 = ChiaServer(
        port,
        full_node_1,
        NodeType.FULL_NODE,
        ping_interval,
        network_id,
        root_path,
        config,
    )
    _ = await server_1.start_server(full_node_1._on_connect)
    full_node_1._set_server(server_1)

    yield (full_node_1, server_1)

    # TEARDOWN
    full_node_1._shutdown()
    server_1.close_all()
    await connection.close()
    Path(db_name).unlink()
コード例 #9
0
async def main():
    root_path = DEFAULT_ROOT_PATH
    net_config = load_config(root_path, "config.yaml")
    config = load_config_cli(root_path, "config.yaml", "timelord")
    initialize_logging("Timelord %(name)-23s", config["logging"])
    log = logging.getLogger(__name__)
    setproctitle("chia_timelord")

    timelord = Timelord(config, constants)
    ping_interval = net_config.get("ping_interval")
    network_id = net_config.get("network_id")
    assert ping_interval is not None
    assert network_id is not None
    server = ChiaServer(config["port"], timelord, NodeType.TIMELORD,
                        ping_interval, network_id)
    _ = await server.start_server(None, config)

    timelord_shutdown_task: Optional[asyncio.Task] = None

    coro = asyncio.start_server(
        timelord._handle_client,
        config["vdf_server"]["host"],
        config["vdf_server"]["port"],
        loop=asyncio.get_running_loop(),
    )

    def signal_received():
        nonlocal timelord_shutdown_task
        server.close_all()
        timelord_shutdown_task = asyncio.create_task(timelord._shutdown())

    asyncio.get_running_loop().add_signal_handler(signal.SIGINT,
                                                  signal_received)
    asyncio.get_running_loop().add_signal_handler(signal.SIGTERM,
                                                  signal_received)

    full_node_peer = PeerInfo(
        timelord.config["full_node_peer"]["host"],
        timelord.config["full_node_peer"]["port"],
    )

    await asyncio.sleep(1)  # Prevents TCP simultaneous connect with full node
    await server.start_client(full_node_peer, None, config)

    vdf_server = asyncio.ensure_future(coro)

    async for msg in timelord._manage_discriminant_queue():
        server.push_message(msg)

    log.info("Closed discriminant queue.")
    if timelord_shutdown_task is not None:
        await timelord_shutdown_task
    log.info("Shutdown timelord.")

    await server.await_closed()
    vdf_server.cancel()
    log.info("Timelord fully closed.")
コード例 #10
0
async def setup_full_node_simulator(db_name,
                                    port,
                                    introducer_port=None,
                                    dic={}):
    # SETUP
    test_constants_copy = test_constants.copy()
    for k in dic.keys():
        test_constants_copy[k] = dic[k]

    db_path = root_path / f"{db_name}"
    if db_path.exists():
        db_path.unlink()

    net_config = load_config(root_path, "config.yaml")
    ping_interval = net_config.get("ping_interval")
    network_id = net_config.get("network_id")

    config = load_config(root_path, "config.yaml", "full_node")
    config["database_path"] = str(db_path)

    if introducer_port is not None:
        config["introducer_peer"]["host"] = "127.0.0.1"
        config["introducer_peer"]["port"] = introducer_port
    full_node_1 = await FullNodeSimulator.create(
        config=config,
        name=f"full_node_{port}",
        root_path=root_path,
        override_constants=test_constants_copy,
    )
    assert ping_interval is not None
    assert network_id is not None
    server_1 = ChiaServer(
        port,
        full_node_1,
        NodeType.FULL_NODE,
        ping_interval,
        network_id,
        bt.root_path,
        config,
        "full-node-simulator-server",
    )
    _ = await start_server(server_1, full_node_1._on_connect)
    full_node_1._set_server(server_1)

    yield (full_node_1, server_1)

    # TEARDOWN
    _.close()
    server_1.close_all()
    full_node_1._close()
    await server_1.await_closed()
    await full_node_1._await_closed()
    db_path.unlink()
コード例 #11
0
async def setup_wallet_node(port,
                            introducer_port=None,
                            key_seed=b"setup_wallet_node",
                            dic={}):
    config = load_config(root_path, "config.yaml", "wallet")
    if "starting_height" in dic:
        config["starting_height"] = dic["starting_height"]

    keychain = Keychain(key_seed.hex(), True)
    keychain.add_private_key_seed(key_seed)
    private_key = keychain.get_all_private_keys()[0][0]
    test_constants_copy = test_constants.copy()
    for k in dic.keys():
        test_constants_copy[k] = dic[k]
    db_path = root_path / f"test-wallet-db-{port}.db"
    if db_path.exists():
        db_path.unlink()
    config["database_path"] = str(db_path)

    net_config = load_config(root_path, "config.yaml")
    ping_interval = net_config.get("ping_interval")
    network_id = net_config.get("network_id")

    wallet = await WalletNode.create(
        config,
        private_key,
        root_path,
        override_constants=test_constants_copy,
        name="wallet1",
    )
    assert ping_interval is not None
    assert network_id is not None
    server = ChiaServer(
        port,
        wallet,
        NodeType.WALLET,
        ping_interval,
        network_id,
        root_path,
        config,
        "wallet-server",
    )
    wallet.set_server(server)

    yield (wallet, server)

    server.close_all()
    await wallet.wallet_state_manager.clear_all_stores()
    await wallet.wallet_state_manager.close_all_stores()
    wallet.wallet_state_manager.unlink_db()
    await server.await_closed()
コード例 #12
0
ファイル: setup_nodes.py プロジェクト: Shar2216/Virtual
async def setup_two_nodes():
    """
    Setup and teardown of two full nodes, with blockchains and separate DBs.
    """

    # SETUP
    store_1 = await FullNodeStore.create("blockchain_test")
    store_2 = await FullNodeStore.create("blockchain_test_2")
    await store_1._clear_database()
    await store_2._clear_database()
    b_1: Blockchain = await Blockchain.create({}, test_constants)
    b_2: Blockchain = await Blockchain.create({}, test_constants)
    await store_1.add_block(FullBlock.from_bytes(test_constants["GENESIS_BLOCK"]))
    await store_2.add_block(FullBlock.from_bytes(test_constants["GENESIS_BLOCK"]))

    full_node_1 = FullNode(store_1, b_1)
    server_1 = ChiaServer(21234, full_node_1, NodeType.FULL_NODE)
    _ = await server_1.start_server("127.0.0.1", full_node_1._on_connect)
    full_node_1._set_server(server_1)

    full_node_2 = FullNode(store_2, b_2)
    server_2 = ChiaServer(21235, full_node_2, NodeType.FULL_NODE)
    full_node_2._set_server(server_2)

    yield (full_node_1, full_node_2, server_1, server_2)

    # TEARDOWN
    full_node_1._shutdown()
    full_node_2._shutdown()
    server_1.close_all()
    server_2.close_all()
    await server_1.await_closed()
    await server_2.await_closed()
    await store_1.close()
    await store_2.close()
コード例 #13
0
async def setup_timelord(port, dic={}):
    config = load_config(root_path, "config.yaml", "timelord")

    test_constants_copy = test_constants.copy()
    for k in dic.keys():
        test_constants_copy[k] = dic[k]
    timelord = Timelord(config, test_constants_copy)

    net_config = load_config(root_path, "config.yaml")
    ping_interval = net_config.get("ping_interval")
    network_id = net_config.get("network_id")
    assert ping_interval is not None
    assert network_id is not None
    server = ChiaServer(
        port,
        timelord,
        NodeType.TIMELORD,
        ping_interval,
        network_id,
        bt.root_path,
        config,
        f"timelord_server_{port}",
    )

    coro = asyncio.start_server(
        timelord._handle_client,
        config["vdf_server"]["host"],
        config["vdf_server"]["port"],
        loop=asyncio.get_running_loop(),
    )

    vdf_server = asyncio.ensure_future(coro)

    timelord.set_server(server)
    timelord._start_bg_tasks()

    async def run_timelord():
        async for msg in timelord._manage_discriminant_queue():
            server.push_message(msg)

    timelord_task = asyncio.create_task(run_timelord())

    yield (timelord, server)

    vdf_server.cancel()
    server.close_all()
    await timelord._shutdown()
    await timelord_task
    await server.await_closed()
コード例 #14
0
async def main():
    config = load_config_cli("config.yaml", "harvester")
    try:
        key_config = load_config("keys.yaml")
    except FileNotFoundError:
        raise RuntimeError(
            "Keys not generated. Run python3 ./scripts/regenerate_keys.py.")
    try:
        plot_config = load_config("plots.yaml")
    except FileNotFoundError:
        raise RuntimeError(
            "Plots not generated. Run python3.7 ./scripts/create_plots.py.")

    initialize_logging("Harvester %(name)-22s", config["logging"])
    log = logging.getLogger(__name__)
    setproctitle("chia_harvester")

    harvester = Harvester(config, key_config, plot_config)
    server = ChiaServer(config["port"], harvester, NodeType.HARVESTER)
    _ = await server.start_server(config["port"], None)

    asyncio.get_running_loop().add_signal_handler(signal.SIGINT,
                                                  server.close_all)
    asyncio.get_running_loop().add_signal_handler(signal.SIGTERM,
                                                  server.close_all)

    peer_info = PeerInfo(harvester.config["farmer_peer"]["host"],
                         harvester.config["farmer_peer"]["port"])

    _ = await server.start_client(peer_info, None)
    await server.await_closed()
    harvester._shutdown()
    await harvester._await_shutdown()
    log.info("Harvester fully closed.")
コード例 #15
0
ファイル: start_farmer.py プロジェクト: Shar2216/Virtual
async def main():
    farmer = Farmer()
    harvester_peer = PeerInfo(farmer.config["harvester_peer"]["host"],
                              farmer.config["harvester_peer"]["port"])
    full_node_peer = PeerInfo(farmer.config["full_node_peer"]["host"],
                              farmer.config["full_node_peer"]["port"])
    host, port = parse_host_port(farmer)
    server = ChiaServer(port, farmer, NodeType.FARMER)

    asyncio.get_running_loop().add_signal_handler(signal.SIGINT,
                                                  server.close_all)
    asyncio.get_running_loop().add_signal_handler(signal.SIGTERM,
                                                  server.close_all)

    async def on_connect():
        # Sends a handshake to the harvester
        pool_sks: List[PrivateKey] = [
            PrivateKey.from_bytes(bytes.fromhex(ce))
            for ce in farmer.key_config["pool_sks"]
        ]
        msg = HarvesterHandshake([sk.get_public_key() for sk in pool_sks])
        yield OutboundMessage(NodeType.HARVESTER,
                              Message("harvester_handshake", msg),
                              Delivery.BROADCAST)

    _ = await server.start_server(host, on_connect)
    await asyncio.sleep(1)  # Prevents TCP simultaneous connect with harvester
    _ = await server.start_client(harvester_peer, None)
    _ = await server.start_client(full_node_peer, None)

    await server.await_closed()
コード例 #16
0
async def main():
    root_path = DEFAULT_ROOT_PATH
    net_config = load_config(root_path, "config.yaml")
    config = load_config_cli(root_path, "config.yaml", "introducer")
    initialize_logging("Introducer %(name)-21s", config["logging"])
    log = logging.getLogger(__name__)
    setproctitle("chia_introducer")

    introducer = Introducer(config)
    ping_interval = net_config.get("ping_interval")
    network_id = net_config.get("network_id")
    assert ping_interval is not None
    assert network_id is not None
    server = ChiaServer(config["port"], introducer, NodeType.INTRODUCER,
                        ping_interval, network_id)
    introducer.set_server(server)
    _ = await server.start_server(None, config)

    asyncio.get_running_loop().add_signal_handler(signal.SIGINT,
                                                  server.close_all)
    asyncio.get_running_loop().add_signal_handler(signal.SIGTERM,
                                                  server.close_all)

    await server.await_closed()
    log.info("Introducer fully closed.")
コード例 #17
0
async def main():
    root_path = DEFAULT_ROOT_PATH
    net_config = load_config(root_path, "config.yaml")
    config = load_config_cli(root_path, "config.yaml", "harvester")
    try:
        plot_config = load_config(root_path, "plots.yaml")
    except FileNotFoundError:
        raise RuntimeError("Plots not generated. Run chia-create-plots")

    initialize_logging("Harvester %(name)-22s", config["logging"])
    log = logging.getLogger(__name__)
    setproctitle("chia_harvester")

    harvester = Harvester(config, plot_config)
    ping_interval = net_config.get("ping_interval")
    network_id = net_config.get("network_id")
    assert ping_interval is not None
    assert network_id is not None
    server = ChiaServer(config["port"], harvester, NodeType.HARVESTER,
                        ping_interval, network_id)
    _ = await server.start_server(None, config)

    asyncio.get_running_loop().add_signal_handler(signal.SIGINT,
                                                  server.close_all)
    asyncio.get_running_loop().add_signal_handler(signal.SIGTERM,
                                                  server.close_all)

    peer_info = PeerInfo(harvester.config["farmer_peer"]["host"],
                         harvester.config["farmer_peer"]["port"])

    _ = await server.start_client(peer_info, None, config)
    await server.await_closed()
    harvester._shutdown()
    await harvester._await_shutdown()
    log.info("Harvester fully closed.")
コード例 #18
0
async def async_main():
    root_path = DEFAULT_ROOT_PATH
    net_config = load_config(root_path, "config.yaml")
    config = load_config_cli(root_path, "config.yaml", "farmer")

    # TOD: Remove once we have pool server
    config_pool = load_config_cli(root_path, "config.yaml", "pool")

    initialize_logging("Farmer %(name)-25s", config["logging"], root_path)
    log = logging.getLogger(__name__)
    setproctitle("chia_farmer")

    keychain = Keychain()

    farmer = Farmer(config, config_pool, keychain)

    ping_interval = net_config.get("ping_interval")
    network_id = net_config.get("network_id")
    assert ping_interval is not None
    assert network_id is not None
    server = ChiaServer(
        config["port"],
        farmer,
        NodeType.FARMER,
        ping_interval,
        network_id,
        root_path,
        config,
    )

    try:
        asyncio.get_running_loop().add_signal_handler(signal.SIGINT,
                                                      server.close_all)
        asyncio.get_running_loop().add_signal_handler(signal.SIGTERM,
                                                      server.close_all)
    except NotImplementedError:
        log.info("signal handlers unsupported")

    _ = await server.start_server(farmer._on_connect)
    farmer.set_server(server)

    rpc_cleanup = None
    if config["start_rpc_server"]:
        # Starts the RPC server
        rpc_cleanup = await start_farmer_rpc_server(farmer, server.close_all,
                                                    config["rpc_port"])

    await asyncio.sleep(10)  # Allows full node to startup
    farmer._start_bg_tasks()

    await server.await_closed()
    farmer._shut_down = True

    # Waits for the rpc server to close
    if rpc_cleanup is not None:
        await rpc_cleanup()
    log.info("Closed RPC server.")

    log.info("Farmer fully closed.")
コード例 #19
0
ファイル: start_timelord.py プロジェクト: spring3th/Exodus
async def async_main():
    root_path = DEFAULT_ROOT_PATH
    net_config = load_config(root_path, "config.yaml")
    config = load_config_cli(root_path, "config.yaml", "timelord")
    initialize_logging("Timelord %(name)-23s", config["logging"], root_path)
    log = logging.getLogger(__name__)
    setproctitle("chia_timelord")

    timelord = Timelord(config, constants)
    ping_interval = net_config.get("ping_interval")
    network_id = net_config.get("network_id")
    assert ping_interval is not None
    assert network_id is not None
    server = ChiaServer(
        config["port"],
        timelord,
        NodeType.TIMELORD,
        ping_interval,
        network_id,
        DEFAULT_ROOT_PATH,
        config,
    )
    timelord.set_server(server)

    coro = asyncio.start_server(
        timelord._handle_client,
        config["vdf_server"]["host"],
        config["vdf_server"]["port"],
        loop=asyncio.get_running_loop(),
    )

    def stop_all():
        server.close_all()
        timelord._shutdown()

    try:
        asyncio.get_running_loop().add_signal_handler(signal.SIGINT, stop_all)
        asyncio.get_running_loop().add_signal_handler(signal.SIGTERM, stop_all)
    except NotImplementedError:
        log.info("signal handlers unsupported")

    await asyncio.sleep(10)  # Allows full node to startup

    peer_info = PeerInfo(config["full_node_peer"]["host"],
                         config["full_node_peer"]["port"])
    bg_task = start_timelord_bg_task(server, peer_info, log)

    vdf_server = asyncio.ensure_future(coro)

    await timelord._manage_discriminant_queue()

    log.info("Closed discriminant queue.")
    log.info("Shutdown timelord.")

    await server.await_closed()
    vdf_server.cancel()
    bg_task.cancel()
    log.info("Timelord fully closed.")
コード例 #20
0
async def async_main():
    root_path = DEFAULT_ROOT_PATH
    net_config = load_config(root_path, "config.yaml")
    config = load_config_cli(root_path, "config.yaml", "harvester")
    try:
        plot_config = load_config(root_path, "plots.yaml")
    except FileNotFoundError:
        raise RuntimeError("Plots not generated. Run chia-create-plots")

    initialize_logging("Harvester %(name)-22s", config["logging"], root_path)
    log = logging.getLogger(__name__)
    setproctitle("chia_harvester")

    harvester = await Harvester.create(config, plot_config, root_path)
    ping_interval = net_config.get("ping_interval")
    network_id = net_config.get("network_id")
    assert ping_interval is not None
    assert network_id is not None
    server = ChiaServer(
        config["port"],
        harvester,
        NodeType.HARVESTER,
        ping_interval,
        network_id,
        DEFAULT_ROOT_PATH,
        config,
    )

    try:
        asyncio.get_running_loop().add_signal_handler(signal.SIGINT, server.close_all)
        asyncio.get_running_loop().add_signal_handler(signal.SIGTERM, server.close_all)
    except NotImplementedError:
        log.info("signal handlers unsupported")

    rpc_cleanup = None
    if config["start_rpc_server"]:
        # Starts the RPC server
        rpc_cleanup = await start_harvester_rpc_server(
            harvester, server.close_all, config["rpc_port"]
        )

    harvester.set_server(server)
    await asyncio.sleep(1)
    harvester._start_bg_tasks()
    await server.await_closed()
    harvester._shutdown()
    await harvester._await_shutdown()

    # Waits for the rpc server to close
    if rpc_cleanup is not None:
        await rpc_cleanup()
    log.info("Closed RPC server.")

    log.info("Harvester fully closed.")
コード例 #21
0
async def setup_farmer(port, dic={}):
    print("root path", root_path)
    config = load_config(root_path, "config.yaml", "farmer")
    config_pool = load_config(root_path, "config.yaml", "pool")
    test_constants_copy = test_constants.copy()
    for k in dic.keys():
        test_constants_copy[k] = dic[k]

    net_config = load_config(root_path, "config.yaml")
    ping_interval = net_config.get("ping_interval")
    network_id = net_config.get("network_id")

    config["xch_target_puzzle_hash"] = bt.fee_target.hex()
    config["pool_public_keys"] = [
        bytes(epk.get_public_key()).hex()
        for epk in bt.keychain.get_all_public_keys()
    ]
    config_pool["xch_target_puzzle_hash"] = bt.fee_target.hex()

    farmer = Farmer(config, config_pool, bt.keychain, test_constants_copy)
    assert ping_interval is not None
    assert network_id is not None
    server = ChiaServer(
        port,
        farmer,
        NodeType.FARMER,
        ping_interval,
        network_id,
        root_path,
        config,
        f"farmer_server_{port}",
    )
    farmer.set_server(server)
    _ = await start_server(server, farmer._on_connect)

    yield (farmer, server)

    _.close()
    server.close_all()
    await server.await_closed()
コード例 #22
0
async def main():
    introducer = Introducer()
    host, port = parse_host_port(introducer)
    server = ChiaServer(port, introducer, NodeType.INTRODUCER)
    introducer.set_server(server)
    _ = await server.start_server(host, None)

    asyncio.get_running_loop().add_signal_handler(signal.SIGINT,
                                                  server.close_all)
    asyncio.get_running_loop().add_signal_handler(signal.SIGTERM,
                                                  server.close_all)

    await server.await_closed()
コード例 #23
0
async def setup_wallet_node(port, introducer_port=None, key_seed=b"", dic={}):
    config = load_config(root_path, "config.yaml", "wallet")
    if "starting_height" in dic:
        config["starting_height"] = dic["starting_height"]
    key_config = {
        "wallet_sk": bytes(blspy.ExtendedPrivateKey.from_seed(key_seed)).hex(),
    }
    test_constants_copy = test_constants.copy()
    for k in dic.keys():
        test_constants_copy[k] = dic[k]
    db_path = root_path / ("test-wallet-db%s.db" % token_bytes(32).hex())
    if db_path.exists():
        db_path.unlink()
    config["database_path"] = str(db_path)

    net_config = load_config(root_path, "config.yaml")
    ping_interval = net_config.get("ping_interval")
    network_id = net_config.get("network_id")

    wallet = await WalletNode.create(
        config,
        key_config,
        override_constants=test_constants_copy,
        name="wallet1",
    )
    assert ping_interval is not None
    assert network_id is not None
    server = ChiaServer(port, wallet, NodeType.WALLET, ping_interval,
                        network_id, "wallet-server")
    wallet.set_server(server)

    yield (wallet, server)

    server.close_all()
    await wallet.wallet_state_manager.clear_all_stores()
    await wallet.wallet_state_manager.close_all_stores()
    wallet.wallet_state_manager.unlink_db()
    await server.await_closed()
コード例 #24
0
async def async_main():
    root_path = DEFAULT_ROOT_PATH
    net_config = load_config(root_path, "config.yaml")
    config = load_config_cli(root_path, "config.yaml", "farmer")
    try:
        check_keys(root_path)
        key_config = load_config(root_path, "keys.yaml")
    except FileNotFoundError:
        raise RuntimeError("Keys not generated. Run `chia generate keys`")

    initialize_logging("Farmer %(name)-25s", config["logging"], root_path)
    log = logging.getLogger(__name__)
    setproctitle("chia_farmer")

    farmer = Farmer(config, key_config)

    ping_interval = net_config.get("ping_interval")
    network_id = net_config.get("network_id")
    assert ping_interval is not None
    assert network_id is not None
    server = ChiaServer(
        config["port"],
        farmer,
        NodeType.FARMER,
        ping_interval,
        network_id,
        root_path,
        config,
    )

    try:
        asyncio.get_running_loop().add_signal_handler(signal.SIGINT, server.close_all)
        asyncio.get_running_loop().add_signal_handler(signal.SIGTERM, server.close_all)
    except NotImplementedError:
        log.info("signal handlers unsupported")

    _ = await server.start_server(farmer._on_connect)

    farmer.set_server(server)
    await asyncio.sleep(10)  # Allows full node to startup
    farmer._start_bg_tasks()

    await server.await_closed()
    farmer._shut_down = True
    log.info("Farmer fully closed.")
コード例 #25
0
ファイル: start_harvester.py プロジェクト: Shar2216/Virtual
async def main():
    harvester = Harvester()
    host, port = parse_host_port(harvester)
    server = ChiaServer(port, harvester, NodeType.HARVESTER)
    _ = await server.start_server(host, None)

    asyncio.get_running_loop().add_signal_handler(signal.SIGINT,
                                                  server.close_all)
    asyncio.get_running_loop().add_signal_handler(signal.SIGTERM,
                                                  server.close_all)

    peer_info = PeerInfo(harvester.config["farmer_peer"]["host"],
                         harvester.config["farmer_peer"]["port"])

    _ = await server.start_client(peer_info, None)
    await server.await_closed()
    harvester._shutdown()
    await harvester._await_shutdown()
コード例 #26
0
async def main():
    config = load_config_cli("config.yaml", "introducer")

    initialize_logging("Introducer %(name)-21s", config["logging"])
    log = logging.getLogger(__name__)
    setproctitle("chia_introducer")

    introducer = Introducer(config)
    server = ChiaServer(config["port"], introducer, NodeType.INTRODUCER)
    introducer.set_server(server)
    _ = await server.start_server(config["host"], None)

    asyncio.get_running_loop().add_signal_handler(signal.SIGINT,
                                                  server.close_all)
    asyncio.get_running_loop().add_signal_handler(signal.SIGTERM,
                                                  server.close_all)

    await server.await_closed()
    log.info("Introducer fully closed.")
コード例 #27
0
async def main():
    config = load_config_cli("config.yaml", "farmer")
    try:
        key_config = load_config("keys.yaml")
    except FileNotFoundError:
        raise RuntimeError(
            "Keys not generated. Run python3 ./scripts/regenerate_keys.py.")
    initialize_logging("Farmer %(name)-25s", config["logging"])
    log = logging.getLogger(__name__)
    setproctitle("chia_farmer")

    farmer = Farmer(config, key_config)

    harvester_peer = PeerInfo(config["harvester_peer"]["host"],
                              config["harvester_peer"]["port"])
    full_node_peer = PeerInfo(config["full_node_peer"]["host"],
                              config["full_node_peer"]["port"])
    server = ChiaServer(config["port"], farmer, NodeType.FARMER)

    asyncio.get_running_loop().add_signal_handler(signal.SIGINT,
                                                  server.close_all)
    asyncio.get_running_loop().add_signal_handler(signal.SIGTERM,
                                                  server.close_all)

    async def on_connect():
        # Sends a handshake to the harvester
        pool_sks: List[PrivateKey] = [
            PrivateKey.from_bytes(bytes.fromhex(ce))
            for ce in key_config["pool_sks"]
        ]
        msg = HarvesterHandshake([sk.get_public_key() for sk in pool_sks])
        yield OutboundMessage(NodeType.HARVESTER,
                              Message("harvester_handshake", msg),
                              Delivery.BROADCAST)

    _ = await server.start_server(config["host"], on_connect)
    await asyncio.sleep(1)  # Prevents TCP simultaneous connect with harvester
    _ = await server.start_client(harvester_peer, None)
    _ = await server.start_client(full_node_peer, None)

    await server.await_closed()
    log.info("Farmer fully closed.")
コード例 #28
0
async def main():
    root_path = DEFAULT_ROOT_PATH
    net_config = load_config(root_path, "config.yaml")
    config = load_config_cli(root_path, "config.yaml", "farmer")
    try:
        key_config = load_config(root_path, "keys.yaml")
    except FileNotFoundError:
        raise RuntimeError("Keys not generated. Run chia-generate-keys")
    initialize_logging("Farmer %(name)-25s", config["logging"])
    log = logging.getLogger(__name__)
    setproctitle("chia_farmer")

    farmer = Farmer(config, key_config)

    harvester_peer = PeerInfo(config["harvester_peer"]["host"],
                              config["harvester_peer"]["port"])
    full_node_peer = PeerInfo(config["full_node_peer"]["host"],
                              config["full_node_peer"]["port"])
    ping_interval = net_config.get("ping_interval")
    network_id = net_config.get("network_id")
    assert ping_interval is not None
    assert network_id is not None
    server = ChiaServer(config["port"], farmer, NodeType.FARMER, ping_interval,
                        network_id)

    asyncio.get_running_loop().add_signal_handler(signal.SIGINT,
                                                  server.close_all)
    asyncio.get_running_loop().add_signal_handler(signal.SIGTERM,
                                                  server.close_all)

    _ = await server.start_server(farmer._on_connect, config)
    await asyncio.sleep(2)  # Prevents TCP simultaneous connect with harvester
    _ = await server.start_client(harvester_peer, None, config)
    _ = await server.start_client(full_node_peer, None, config)

    farmer.set_server(server)
    farmer._start_bg_tasks()

    await server.await_closed()
    farmer._shut_down = True
    log.info("Farmer fully closed.")
コード例 #29
0
async def async_main():
    root_path = DEFAULT_ROOT_PATH
    config = load_config_cli(root_path, "config.yaml", "full_node")
    net_config = load_config(root_path, "config.yaml")
    setproctitle("chia_full_node")
    initialize_logging("FullNode %(name)-23s", config["logging"], root_path)

    log = logging.getLogger(__name__)
    server_closed = False

    full_node = await FullNode.create(config, root_path=root_path)

    if config["enable_upnp"]:
        log.info(f"Attempting to enable UPnP (open up port {config['port']})")
        try:
            upnp = miniupnpc.UPnP()
            upnp.discoverdelay = 5
            upnp.discover()
            upnp.selectigd()
            upnp.addportmapping(config["port"], "TCP", upnp.lanaddr,
                                config["port"], "chia", "")
            log.info(f"Port {config['port']} opened with UPnP.")
        except Exception:
            log.warning(
                "UPnP failed. This is not required to run chia, but it allows incoming connections from other peers."
            )

    # Starts the full node server (which full nodes can connect to)
    ping_interval = net_config.get("ping_interval")
    network_id = net_config.get("network_id")
    assert ping_interval is not None
    assert network_id is not None
    server = ChiaServer(
        config["port"],
        full_node,
        NodeType.FULL_NODE,
        ping_interval,
        network_id,
        DEFAULT_ROOT_PATH,
        config,
    )
    full_node._set_server(server)
    _ = await server.start_server(full_node._on_connect)
    rpc_cleanup = None

    def master_close_cb():
        nonlocal server_closed
        if not server_closed:
            # Called by the UI, when node is closed, or when a signal is sent
            log.info("Closing all connections, and server...")
            server.close_all()
            full_node._close()
            server_closed = True

    if config["start_rpc_server"]:
        # Starts the RPC server
        rpc_cleanup = await start_full_node_rpc_server(full_node,
                                                       master_close_cb,
                                                       config["rpc_port"])

    try:
        asyncio.get_running_loop().add_signal_handler(signal.SIGINT,
                                                      master_close_cb)
        asyncio.get_running_loop().add_signal_handler(signal.SIGTERM,
                                                      master_close_cb)
    except NotImplementedError:
        log.info("signal handlers unsupported")

    full_node._start_bg_tasks()

    # Awaits for server and all connections to close
    await server.await_closed()
    log.info("Closed all node servers.")

    # Stops the full node and closes DBs
    await full_node._await_closed()

    # Waits for the rpc server to close
    if rpc_cleanup is not None:
        await rpc_cleanup()
    log.info("Closed RPC server.")

    await asyncio.get_running_loop().shutdown_asyncgens()
    log.info("Node fully closed.")
コード例 #30
0
    async def test1(self):
        test_node_1_port = 21234
        test_node_2_port = 21235
        test_rpc_port = 21236
        db_filename = "blockchain_test"

        if os.path.isfile(db_filename):
            os.remove(db_filename)
        store = await FullNodeStore.create(db_filename)
        await store._clear_database()
        blocks = bt.get_consecutive_blocks(test_constants, 10, [], 10)
        b: Blockchain = await Blockchain.create({}, test_constants)
        await store.add_block(blocks[0])
        for i in range(1, 9):
            assert (await b.receive_block(
                blocks[i],
                blocks[i -
                       1].header_block)) == ReceiveBlockResult.ADDED_TO_HEAD
            await store.add_block(blocks[i])

        config = load_config("config.yaml", "full_node")
        full_node_1 = FullNode(store, b, config)
        server_1 = ChiaServer(test_node_1_port, full_node_1,
                              NodeType.FULL_NODE)
        _ = await server_1.start_server("127.0.0.1", None)
        full_node_1._set_server(server_1)

        def stop_node_cb():
            full_node_1._shutdown()
            server_1.close_all()

        rpc_cleanup = await start_rpc_server(full_node_1, stop_node_cb,
                                             test_rpc_port)

        try:
            client = await RpcClient.create(test_rpc_port)
            state = await client.get_blockchain_state()
            assert state["lca"].header_hash is not None
            assert not state["sync_mode"]
            assert len(state["tips"]) > 0
            assert state["difficulty"] > 0
            assert state["ips"] > 0

            block = await client.get_block(state["lca"].header_hash)
            assert block == blocks[6]
            assert (await client.get_block(bytes([1] * 32))) is None

            small_header_block = await client.get_header(
                state["lca"].header_hash)
            assert small_header_block.header == blocks[6].header_block.header

            assert len(await client.get_pool_balances()) > 0
            assert len(await client.get_connections()) == 0

            full_node_2 = FullNode(store, b, config)
            server_2 = ChiaServer(test_node_2_port, full_node_2,
                                  NodeType.FULL_NODE)
            full_node_2._set_server(server_2)

            _ = await server_2.start_server("127.0.0.1", None)
            await asyncio.sleep(2)  # Allow server to start
            cons = await client.get_connections()
            assert len(cons) == 0

            # Open a connection through the RPC
            await client.open_connection(host="127.0.0.1",
                                         port=test_node_2_port)
            cons = await client.get_connections()
            assert len(cons) == 1

            # Close a connection through the RPC
            await client.close_connection(cons[0]["node_id"])
            cons = await client.get_connections()
            assert len(cons) == 0
        except AssertionError:
            # Checks that the RPC manages to stop the node
            await client.stop_node()
            client.close()
            await client.await_closed()
            server_2.close_all()
            await server_1.await_closed()
            await server_2.await_closed()
            await rpc_cleanup()
            await store.close()
            raise

        await client.stop_node()
        client.close()
        await client.await_closed()
        server_2.close_all()
        await server_1.await_closed()
        await server_2.await_closed()
        await rpc_cleanup()
        await store.close()