Sha256: 099fad1a1f15e5ae7e18f2bb56e010598ad54c2f9c848c7c84a3b1647017df6b

Contents?: true

Size: 1.88 KB

Versions: 5

Compression:

Stored size: 1.88 KB

Contents

#!/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])

    refresh_wallet
    node = Bitcoin::Node::SPV.new(conf)
    publisher = Bitcoin::Wallet::Publisher.spawn(:publisher)
    utxo_handler = Bitcoin::Wallet::UtxoHandler.spawn(:utxo_handler, node, publisher)
    asset_handler = Bitcoin::Wallet::AssetHandler.spawn(:asset_handler, node, publisher)
    Thread.new { Bitcoin::Grpc::Server.run(node, publisher, utxo_handler, asset_handler) }
    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

    require 'bitcoin/grpc'

    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)

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
bitcoinrb-grpc-0.1.9 bin/bitcoinrbd
bitcoinrb-grpc-0.1.8 bin/bitcoinrbd
bitcoinrb-grpc-0.1.7 bin/bitcoinrbd
bitcoinrb-grpc-0.1.6 bin/bitcoinrbd
bitcoinrb-grpc-0.1.5 bin/bitcoinrbd