#!/usr/bin/env ruby require 'bundler/setup' require 'bitcoin' require 'thor' require 'daemon_spawn' class BitcoinDaemon < DaemonSpawn::Base def start(args) puts "Bitcoinrb daemon start : #{Time.now} #{args}" conf = Bitcoin::Node::Configuration.new(network: args.first[:network]) require 'bitcoin/grpc' require 'extensions/bitcoin/rpc/request_handler' require 'extensions/bitcoin/wallet/base' require 'extensions/bitcoin/tx' refresh_wallet node = Bitcoin::Node::SPV.new(conf) Thread.new { Bitcoin::Grpc::Server.run(node) } node.run end def stop(args) puts "Bitcoinrb daemon stop : #{Time.now} #{args}" end end def refresh_wallet default_wallet = Bitcoin::Wallet::Base.current_wallet || Bitcoin::Wallet::Base.create(1) default_wallet.close end class Bitcoinrbd < Thor class_option :network, aliases: '-n', default: :mainnet desc 'start', 'start bitcoinrb daemon.' def start network = options['network'] ? options['network'].to_sym : :mainnet Bitcoin.chain_params = network FileUtils.mkdir_p(Bitcoin.base_dir) execute_daemon(['start', network: network]) end desc 'stop', 'stop bitcoinrb daemon.' def stop network = options['network'] ? options['network'].to_sym : :mainnet Bitcoin.chain_params = network execute_daemon(['stop', network: network]) end private def execute_daemon(cmd_args) BitcoinDaemon.spawn!({working_dir: Bitcoin.base_dir, log_file: "#{Bitcoin.base_dir}/log/debug.log", pid_file: "#{Bitcoin.base_dir}/bitcoinrbd-grpc.pid", sync_log: true, singleton: true}, cmd_args) end end Bitcoinrbd.start(ARGV)