Ejemplo n.º 1
0
def test_main():
    parser = DefaultArgumentParser()
    args = parser.parse_args(args=[])
    configure_logging_from_args(args)

    # Initially were using an empty chain for this test.
    # And then we found that the performance of newer blocks is different!
    # coinstate = CoinState.zero()
    check_chain_dir()
    coinstate = read_chain_from_disk()

    # we need to run in the current thread to profile it
    nt = NetworkingThread(coinstate, port=None)

    def runner():
        started = datetime.now()
        nt.local_peer.running = True
        print("start height = %d" %
              nt.local_peer.chain_manager.coinstate.head().height)
        while (datetime.now() - started).seconds <= 100:
            current_time = int(time())
            nt.local_peer.step_managers(current_time)
            nt.local_peer.handle_selector_events()
        print("final height = %d" %
              nt.local_peer.chain_manager.coinstate.head().height)

    with cProfile.Profile() as pr:
        pr.runcall(runner)
        pr.print_stats()
Ejemplo n.º 2
0
def main() -> None:
    config_file, history_file = get_config_and_history_file(EverythingIsNone())

    parser = DefaultArgumentParser()
    parser.add_argument("--vi-mode", help="Vi mode", action="store_true")
    args = parser.parse_args()
    configure_logging_from_args(args)

    check_chain_dir()
    coinstate = read_chain_from_disk()
    wallet = open_or_init_wallet()
    thread = start_networking_peer_in_background(args, coinstate)
    thread.local_peer.show_stats()

    print("Starting REPL, exit with exit()")
    try:
        globals = {
            'thread': thread,
            'local_peer': thread.local_peer,
            'show_stats': thread.local_peer.show_stats,
            'wallet': wallet,
            'get_coinstate': lambda: thread.local_peer.chain_manager.coinstate,
            'SASHIMI_PER_COIN': SASHIMI_PER_COIN,
        }

        for module in [
                skepticoin.datatypes, skepticoin.networking.messages,
                skepticoin.signing, skepticoin.humans
        ]:
            for attr in module.__all__:  # type: ignore
                globals[attr] = getattr(module, attr)

        def configure(repl: PythonRepl) -> None:
            if os.path.exists(config_file):
                run_config(repl, config_file)
            else:
                # from https://github.com/prompt-toolkit/ptpython/blob/master/examples/ptpython_config/config.py
                # Ask for confirmation on exit.
                repl.confirm_exit = False

            # embedded in other applications.
            repl.title = "Skepticoin %s " % __version__

        embed(
            vi_mode=args.vi_mode,
            globals=globals,
            configure=configure,
            history_filename=history_file,
            patch_stdout=True,
        )

    finally:
        print("Stopping networking thread")
        thread.stop()
        print("Waiting for networking thread to stop")
        thread.join()
        print("Done; waiting for Python-exit")
Ejemplo n.º 3
0
def test_read_chain_from_disk():

    check_chain_dir()

    with cProfile.Profile() as pr:
        coinstate = pr.runcall(read_chain_from_disk)
        pr.print_stats()

    with open('test.tmp', 'wb') as file:
        coinstate.dump(lambda data: pickle.dump(data, file))
Ejemplo n.º 4
0
    def __call__(self) -> None:
        configure_logging_from_args(self.args)

        check_chain_dir()
        self.coinstate = read_chain_from_disk()

        self.wallet = open_or_init_wallet()
        self.start_balance = self.wallet.get_balance(
            self.coinstate) / Decimal(SASHIMI_PER_COIN)
        self.balance = self.start_balance

        self.network_thread = start_networking_peer_in_background(
            self.args, self.coinstate)

        self.network_thread.local_peer.show_stats()

        if check_for_fresh_chain(self.network_thread):
            self.network_thread.local_peer.show_stats()

        if self.network_thread.local_peer.chain_manager.coinstate.head(
        ).height <= MAX_KNOWN_HASH_HEIGHT:
            print("Your blockchain is not just old, it is ancient; ABORTING")
            exit(1)

        self.public_key = self.wallet.get_annotated_public_key(
            "reserved for potentially mined block")
        save_wallet(self.wallet)

        for miner_id in range(self.args.n):
            if miner_id > 0:
                self.args.dont_listen = True

            send_queue: Queue = Queue()
            process = Process(target=run_miner,
                              daemon=True,
                              args=(self.args, self.recv_queue, send_queue,
                                    miner_id))
            process.start()
            self.processes.append(process)
            self.send_queues.append(send_queue)

        self.start_time = datetime.now() - timedelta(
            seconds=1)  # prevent negative uptime due to second rounding

        try:
            while True:
                queue_item: Tuple[int, str, Any] = self.recv_queue.get()
                self.handle_received_message(queue_item)

        except KeyboardInterrupt:
            pass

        except Exception:
            print("Error in MinerWatcher message loop: " +
                  traceback.format_exc())

        finally:
            print("Restoring unused public key")
            self.wallet.restore_annotated_public_key(
                self.public_key, "reserved for potentially mined block")

            print("Stopping networking thread")
            self.network_thread.stop()

            print("Waiting for networking thread to stop")
            self.network_thread.join()

            for process in self.processes:
                process.join()