Exemple #1
0
def start_bitcoin_qt():
    '''
        Start bitcoin-qt.

        >>> init_database()
        >>> start_bitcoin_qt()
        >>> stop_bitcoin_qt()
    '''

    bin_dir, data_dir = preferences.get_bitcoin_dirs()

    command_args = []
    cmd = os.path.join(bin_dir, bitcoin_utils.bitcoin_qt())
    command_args.append(cmd)

    data_dir = bitcoin_utils.strip_testnet_from_data_dir(data_dir=data_dir)
    command_args.append(f'-datadir={data_dir}')

    extra_args = preferences.get_extra_args()
    if len(extra_args) > 0:
        for extra_arg in extra_args:
            command_args.append(extra_arg)

    command_args.append('-daemon')

    command.background(*command_args)
    log(f'running : {command_args}')

    # give bitcoind time to start
    secs = 0
    while (not bitcoin_utils.is_bitcoin_qt_running() and secs < 5):
        sleep(1)
        secs += 1
Exemple #2
0
def send_bitcoin_cli_cmd(arg):
    '''
        Send a command via bitcoin-cli.

        >>> init_database()
        >>> start_bitcoind()
        >>> block_count = send_bitcoin_cli_cmd('getblockcount')
        >>> stop_bitcoind()
    '''

    bin_dir, data_dir = preferences.get_bitcoin_dirs()

    command_args = []
    command_args.append(os.path.join(bin_dir, bitcoin_utils.bitcoin_cli()))

    use_test_net = '-testnet' in preferences.get_extra_args()
    if use_test_net:
        command_args.append('-testnet')

    if data_dir is not None:
        data_dir = bitcoin_utils.strip_testnet_from_data_dir(data_dir=data_dir)
        command_args.append(f'-datadir={data_dir}')

    command_args.append(arg)
    log(f'running: {command_args}')

    try:
        result = command.run(*command_args).stdout
        log(f'result: {result}')
    except CalledProcessError as cpe:
        result = None

    return result
Exemple #3
0
    def get_bitcoin_cli_cmd(self, arg):
        '''
            Get a command for bitcoin-cli.

            >>> from blockchain_backup.bitcoin.tests import utils as test_utils
            >>> test_utils.init_database()
            >>> manager = BitcoinManager('blockchain_backup.bitcoin.manager.log')
            >>> manager.get_bitcoin_cli_cmd('getblockchaininfo')
            ['/tmp/bitcoin/bin/bitcoin-cli', '-testnet', '-datadir=/tmp/bitcoin/data', 'getblockchaininfo']
        '''

        command_args = []
        if self.bin_dir is None:
            command_args.append(utils.bitcoin_cli())
        else:
            command_args.append(os.path.join(self.bin_dir,
                                             utils.bitcoin_cli()))

        use_test_net = '-testnet' in get_extra_args()
        if use_test_net:
            command_args.append('-testnet')

        if self.data_dir is not None:
            data_dir = utils.strip_testnet_from_data_dir(
                data_dir=self.data_dir)
            command_args.append(f'-datadir={data_dir}')

        command_args.append(arg)

        return command_args
Exemple #4
0
    def get_launch_args(self):
        '''
            Get all the args to start bitcon-qt as a server.
        '''
        ok = False
        command_args = []

        if self.manager.bin_dir is None:
            command_args.append(bitcoin_utils.bitcoin_qt())
            ok = True
        else:
            cmd = os.path.join(self.manager.bin_dir, bitcoin_utils.bitcoin_qt())
            command_args.append(cmd)
            ok = os.path.exists(cmd)
            if not ok:
                self.log(f'{bitcoin_utils.bitcoin_qt()} does not exist in {self.manager.bin_dir}')

        if ok:
            command_args.append('-server')

            if self.manager.data_dir is not None:
                data_dir = bitcoin_utils.strip_testnet_from_data_dir(data_dir=self.manager.data_dir)
                command_args.append(f'-datadir={data_dir}')

            extra_args = preferences.get_extra_args()
            if extra_args:
                for extra_arg in extra_args:
                    command_args.append(extra_arg)
        else:
            command_args = None
            self.log(f'{bitcoin_utils.bitcion_qt()} does not exist in {self.manager.bin_dir}')

        return command_args
Exemple #5
0
def strip_testnet_from_data_dir(data_dir=None):
    '''
        Get the data dirname without
        the "testnet3" subdir, if appropriate.

        >>> from blockchain_backup.bitcoin.tests import utils as test_utils
        >>> test_utils.init_database()
        >>> strip_testnet_from_data_dir()
        '/tmp/bitcoin/data'
    '''
    from blockchain_backup.bitcoin.preferences import get_data_dir, get_extra_args

    if data_dir is None:
        data_dir = get_data_dir()

    use_test_net = constants.TESTNET_FLAG in get_extra_args()
    if use_test_net and data_dir.endswith(constants.TEST_NET_SUBDIR):
        new_data_dir = data_dir[:data_dir.rfind(constants.TEST_NET_SUBDIR)]

    else:
        new_data_dir = data_dir

    return new_data_dir
Exemple #6
0
def get_excluded_files():
    '''
        Get the files to exclude from backups and restores.

        >>> from blockchain_backup.bitcoin.tests import utils as test_utils
        >>> test_utils.init_database()
        >>> get_excluded_files()
        'wallets,wallet.dat,.walletlock,backups,blockchain_backup_database'
    '''
    from blockchain_backup.bitcoin.preferences import get_extra_args

    excluded_files = 'wallets,wallet.dat,.walletlock,backups,{}'.format(
        constants.BLOCKCHAIN_BACKUP_DB_DIR)

    use_test_net = constants.TESTNET_FLAG in get_extra_args()
    if not use_test_net:
        excluded_files += f',{constants.TEST_NET_DIR}'

    # add the subdirectory of the backup if its in the data directory
    backup_subdir = get_backup_subdir()
    if backup_subdir is not None and backup_subdir not in excluded_files:
        excluded_files += f',{backup_subdir}'

    return excluded_files
    def start_bitcoind(self):
        '''
            Start bitcoind as a daemon.

            >>> from blockchain_backup.bitcoin.tests import utils as test_utils
            >>> need_backup = False
            >>> test_utils.init_database()
            >>> update_task = UpdateTask()
            >>> update_task.manager = BitcoinManager(update_task.log_name)
            >>> bitcoind_process, bitcoind_pid = update_task.start_bitcoind()
            >>> update_task.stop_bitcoind(bitcoind_process, bitcoind_pid, need_backup)
            (False, ' Error opening block database.\\n')
        '''
        if is_bitcoind_running():
            bitcoind_process = None
            bitcoind_pid = get_pid(bitcoind())
            if bitcoind_pid is None:
                sleep(5)
                bitcoind_pid = get_pid(bitcoind())
            self.log(f'bitcoind is already running using pid: {bitcoind_pid}')
        else:
            bitcoind_pid = None
            command_args = []

            if self.manager.bin_dir is None:
                command_args.append(bitcoind())
                ok = True
            else:
                cmd = os.path.join(self.manager.bin_dir, bitcoind())
                command_args.append(cmd)
                ok = os.path.exists(cmd)

            if ok:
                extra_args = preferences.get_extra_args()
                use_test_net = '-testnet' in extra_args

                if self.manager.data_dir is not None:
                    data_dir = self.manager.data_dir
                    if use_test_net and data_dir.endswith(
                            constants.TEST_NET_SUBDIR):
                        data_dir = data_dir[:data_dir.rfind(constants.
                                                            TEST_NET_SUBDIR)]
                    command_args.append(f'-datadir={data_dir}')

                # don't allow any interaction with the user's wallet
                command_args.append('-disablewallet')

                if extra_args:
                    for extra_arg in extra_args:
                        command_args.append(extra_arg)

                command_args.append('-daemon')

                try:
                    bitcoind_process = Popen(command_args)
                    self.log(
                        f'bitcoind started: {bitcoind_process is not None}')
                except FileNotFoundError as fnfe:
                    raise BitcoinException(str(fnfe))

            else:
                bitcoind_process = None
                self.log(
                    f'{bitcoind()} does not exist in {self.manager.bin_dir}')

        state.set_start_access_time(now())

        return bitcoind_process, bitcoind_pid