Commit 7c20195ad48edcd1d0c260928e8b427dccf4c471

Authored by Marius Hanne
1 parent dec22a912c

forgotten file

Showing 1 changed file with 193 additions and 0 deletions Side-by-side Diff

  1 +#!/usr/bin/env ruby
  2 +$:.unshift( File.expand_path("../../lib", __FILE__) )
  3 +
  4 +require 'rubygems'
  5 +require 'bitcoin'
  6 +require 'optparse'
  7 +require 'eventmachine'
  8 +require 'json'
  9 +
  10 +defaults = {
  11 + :network => "bitcoin",
  12 + :command => "127.0.0.1:9999",
  13 + :listen => "0.0.0.0:8332",
  14 + :connect => "",
  15 + :storage => "sequel::sqlite://bitcoin.db",
  16 + :headers_only => false,
  17 + :dns => true,
  18 + :epoll => false,
  19 + :epoll_limit => 10000,
  20 + :epoll_user => nil,
  21 + :log => {
  22 + :network => :info,
  23 + :storage => :info,
  24 + },
  25 + :max => {
  26 + :connections => 32,
  27 + :addr => 1024,
  28 + :queue => 500,
  29 + :inv => 500,
  30 + },
  31 + :intervals => {
  32 + :queue => 5,
  33 + :inv_queue => 5,
  34 + :addrs => 15,
  35 + :connect => 30,
  36 + },
  37 + :daemon => false,
  38 +}
  39 +
  40 +options = Bitcoin::Config.load(defaults, :blockchain)
  41 +
  42 +optparse = OptionParser.new do |opts|
  43 + opts.banner = "Usage: bitcoin_node [options]"
  44 +
  45 + opts.separator("\nAvailable options:\n")
  46 +
  47 + opts.on("-c", "--config FILE",
  48 + "Config file (default: #{Bitcoin::Config::CONFIG_PATHS})") do |file|
  49 + options = Bitcoin::Config.load_file(options, file, :blockchain)
  50 + end
  51 +
  52 + opts.on("-n", "--network [NETWORK]",
  53 + "User Network (default: #{options[:network]})") do |network|
  54 + options[:network] = network
  55 + end
  56 +
  57 + opts.on("--command [HOST:PORT]",
  58 + "Command socket (default: #{options[:command]})") do |command|
  59 + options[:command] = command
  60 + end
  61 +
  62 + opts.on("-l", "--listen [HOST:PORT]",
  63 + "Listen address/port (default: #{options[:listen]})") do |listen|
  64 + options[:listen] = listen
  65 + end
  66 +
  67 + opts.on("--connect [HOST:PORT[,HOST:PORT[...]]]",
  68 + "Hosts to connect to (default: #{options[:connect]})") do |connect|
  69 + options[:connect] = connect
  70 + end
  71 +
  72 + opts.on("-s", "--storage [BACKEND::CONFIG]",
  73 + "Use storage backend (default: #{options[:storage]})") do |storage|
  74 + options[:storage] = storage
  75 + end
  76 +
  77 + opts.on("--ho", "--headers-only",
  78 + "Download only block headers") do
  79 + options[:headers_only] = true
  80 + end
  81 +
  82 + opts.on("-d", "--dns", "Use DNS seeds (default)") do
  83 + options[:dns] = true
  84 + end
  85 +
  86 + opts.on("--nd", "--no-dns", "Don't use DNS seeds") do
  87 + options[:dns] = false
  88 + end
  89 +
  90 + opts.on("--epoll", "Enable epoll support") do
  91 + options[:epoll] = true
  92 + end
  93 +
  94 + opts.on("--epoll-limit [NUM]",
  95 + "Increase socket descriptor limit to NUM") do |num|
  96 + options[:epoll_limit] = num.to_i
  97 + end
  98 +
  99 + opts.on("--epoll-user [NAME]",
  100 + "Set effective user after increasing socket descriptor limit") do |user|
  101 + options[:epoll_user] = user
  102 + end
  103 +
  104 + [:connections, :addr, :queue, :inv].each do |name|
  105 + opts.on("--m#{name.to_s[0]}", "--max-#{name} [COUNT]",
  106 + "Max #{name} (default: #{options[:max][name]})") do |count|
  107 + options[:max][name] = count.to_i
  108 + end
  109 + end
  110 +
  111 + [:queue, :inv_queue, :blocks, :addrs, :connect].each do |name|
  112 + opts.on("--i#{name.to_s[0]}", "--interval-#{name} [SECONDS]",
  113 + "Interval for #{name} worker (default: #{options[:intervals][name]})") do |sec|
  114 + options[:intervals][name] = sec.to_i
  115 + end
  116 + end
  117 +
  118 + [:network, :storage].each do |name|
  119 + opts.on("--l#{name.to_s[0]}", "--log-#{name} [LEVEL]",
  120 + "Log level for #{name} (default: #{options[:log][name]})") do |level|
  121 + options[:log][name] = level.to_sym
  122 + end
  123 + end
  124 +
  125 + opts.on("-v", "--verbose", "Set all loggers to debug") do
  126 + options[:log].each_key {|k| options[:log][k] = :debug }
  127 + end
  128 +
  129 + opts.on("-q", "--quiet", "Set all loggers to warn") do
  130 + options[:log].each_key {|k| options[:log][k] = :warn }
  131 + end
  132 +
  133 + opts.on("--daemon", "Daemonize process") do
  134 + options[:daemon] = true
  135 + end
  136 +
  137 + opts.on( '-h', '--help', 'Display this screen' ) do
  138 + puts opts; exit
  139 + end
  140 +end
  141 +
  142 +optparse.parse!
  143 +
  144 +Bitcoin.network = options[:network]
  145 +options[:command] = options[:command] == "" ? nil : options[:command].split(':')
  146 +options[:listen] = options[:listen] == "" ? nil : options[:listen].split(':')
  147 +options[:connect] = options[:connect] == "" ? [] : options[:connect].split(',').map{|h| h.split(':')}
  148 +
  149 +if ARGV.any?
  150 + EM.run do
  151 + Bitcoin::Network::CommandClient.connect(*options[:command]) do
  152 +
  153 + on_response do |cmd, data|
  154 + EM.stop unless cmd == "monitor"
  155 + end
  156 + on_info do |info|
  157 + puts JSON.pretty_generate(info)
  158 + end
  159 + on_connections do |connections|
  160 + puts *connections
  161 + end
  162 + [:connect, :disconnect, :getblocks, :getaddr, :relay_tx, :stop].each do |req|
  163 + send("on_#{req}") {|res| p res}
  164 + end
  165 +
  166 + on_block do |block, depth|
  167 + puts "block: #{block['hash']} (#{depth})"
  168 + end
  169 + on_tx do |tx|
  170 + puts "tx: #{tx['hash']}"
  171 + end
  172 + on_connection do |type, host|
  173 + if type == "connected"
  174 + puts "Connected: #{host['host']}:#{host['port']}"
  175 + else
  176 + puts "Disconnected: #{host.inspect}"
  177 + end
  178 + end
  179 +
  180 + on_connected do
  181 + request(ARGV[0], *(ARGV[1] || "").split(" "))
  182 + end
  183 +
  184 + end
  185 + end
  186 +else
  187 + node = Bitcoin::Network::Node.new(options)
  188 +# if options[:daemon]
  189 +# Process.detach(fork { node.run })
  190 +# else
  191 + node.run
  192 +# end
  193 +end