Commit e3b9dea421d4e28b8d28f801c834248f847d828b

Authored by Marius Hanne
1 parent 916d4d48ce

dependencies/requires

Showing 11 changed files with 75 additions and 70 deletions Side-by-side Diff

1 1 #!/usr/bin/env ruby
2 2 $:.unshift( File.expand_path("../../lib", __FILE__) )
3 3  
4   -require 'ffi-gtk3'
5   -require 'eventmachine'
6 4 require 'bitcoin'
7 5 require 'optparse'
8   -require 'json'
9   -require 'monitor'
10 6  
  7 +Bitcoin.require_dependency 'ffi-gtk3', gem: "gir_ffi"
  8 +Bitcoin.require_dependency :eventmachine
  9 +Bitcoin.require_dependency :notify
  10 +Bitcoin.require_dependency :json
  11 +
  12 +
11 13 defaults = {
12   - :network => "testnet",
  14 + :network => "bitcoin",
13 15 :storage => "dummy",
14 16 :keystore => "simple::file=~/.bitcoin-ruby/keys.json",
15 17 :command => "127.0.0.1:9999"
... ... @@ -63,7 +65,7 @@
63 65  
64 66 optparse.parse!
65 67  
66   -
  68 +Bitcoin.network = options[:network]
67 69  
68 70 backend, config = options[:storage].split("::")
69 71 @storage = Bitcoin::Storage.send(backend, :db => config)
1 1 #!/usr/bin/env ruby
2 2 $:.unshift( File.expand_path("../../lib", __FILE__) )
3 3  
4   -require 'rubygems'
5 4 require 'bitcoin'
6 5 require 'optparse'
7   -require 'eventmachine'
8   -require 'json'
  6 +Bitcoin.require_dependency :eventmachine
  7 +Bitcoin.require_dependency :json
9 8  
10 9 defaults = {
11 10 :network => "bitcoin",
... ... @@ -130,10 +129,6 @@
130 129 options[:log].each_key {|k| options[:log][k] = :warn }
131 130 end
132 131  
133   - opts.on("--daemon", "Daemonize process") do
134   - options[:daemon] = true
135   - end
136   -
137 132 opts.on( '-h', '--help', 'Display this screen' ) do
138 133 puts opts; exit
139 134 end
... ... @@ -185,10 +180,6 @@
185 180 end
186 181 else
187 182 node = Bitcoin::Network::Node.new(options)
188   -# if options[:daemon]
189   -# Process.detach(fork { node.run })
190   -# else
191   - node.run
192   -# end
  183 + node.run
193 184 end
... ... @@ -39,6 +39,20 @@
39 39 autoload :Connection, 'bitcoin/gui/connection'
40 40 end
41 41  
  42 + def self.require_dependency name, opts = {}
  43 + begin
  44 + require name.to_s
  45 + rescue LoadError
  46 + print "Cannot load #{opts[:exit] == false ? 'optional' : 'required'} dependency '#{name}'"
  47 + (opts[:gem] == false) ? puts("") :
  48 + puts(" - install with `gem install #{opts[:gem] || name}`")
  49 + puts opts[:message] if opts[:message]
  50 + exit 1 unless opts[:exit] == false
  51 + return false
  52 + end
  53 + true
  54 + end
  55 +
42 56 module Util
43 57  
44 58 def hth(h); h.unpack("H*")[0]; end
lib/bitcoin/ffi/openssl.rb
1 1 # autoload when you need to re-generate a public_key from only its private_key.
2 2 # ported from: https://github.com/sipa/bitcoin/blob/2d40fe4da9ea82af4b652b691a4185431d6e47a8/key.h
3 3  
4   -begin
5   - require 'ffi'
6   -rescue LoadError
7   - puts "Cannot load 'ffi' for OpenSSL_EC.regenerate_key. - install with `gem install ffi`"
8   - exit 1
9   -end
  4 +Bitcoin.require_dependency :ffi, exit: false, message:
  5 + "Skipping FFI needed for OpenSSL_EC.regenerate_key."
10 6  
11 7 module Bitcoin
12 8 module OpenSSL_EC
lib/bitcoin/gui/em_gtk.rb
  1 +require "monitor"
  2 +
1 3 module Gtk
2 4 GTK_PENDING_BLOCKS = []
3 5 GTK_PENDING_BLOCKS_LOCK = Monitor.new
lib/bitcoin/logger.rb
1   -begin
2   - require 'log4r'
  1 +if Bitcoin.require_dependency :log4r, exit: false
3 2 # monkey-patch Log4r to accept level names as symbols
4 3 class Log4r::Logger
5   - def level= l
6   - @level = l.is_a?(Fixnum) ? l : Log4r::LNAMES.index(l)
  4 + def level= l = 0
  5 + _level = l.is_a?(Fixnum) ? l : Log4r::LNAMES.index(l.to_s.upcase)
  6 + Log4r::Log4rTools.validate_level(_level)
  7 + @level = _level
  8 + LoggerFactory.define_methods(self)
  9 + Log4r::Logger.log_internal {"Logger '#{@fullname}' set to #{LNAMES[@level]}"}
  10 + @level
7 11 end
8 12 end
9   -rescue LoadError
10   - # log4r not installed
11 13 end
12 14  
13 15 module Bitcoin
... ... @@ -31,7 +33,7 @@
31 33 define_method(level) do |*msg, &block|
32 34 return if LEVELS.index(level.to_sym) < LEVELS.index(@level.to_sym)
33 35 msg = block ? block.call : msg.join
34   - puts "#{level.to_s.upcase.ljust(5)} #{@name} #{msg}"
  36 + puts "#{level.to_s.upcase.ljust(5)} #{@name}: #{msg}"
35 37 end
36 38 end
37 39 end
38 40  
... ... @@ -47,10 +49,10 @@
47 49 # create a logger with given +name+. if log4r is installed, the logger
48 50 # will have a stdout and a fileout outputter to `log/<name>.log`.
49 51 # otherwise, the internal dummy logger is used which only logs to stdout.
50   - def self.create name
  52 + def self.create name, level = :info
51 53 if defined?(Log4r)
52 54 @log = Log4r::Logger.new(name.to_s)
53   - @log.level = 0
  55 + @log.level = level
54 56 @log.outputters << Log4r::Outputter.stdout
55 57 @log.outputters << Log4r::FileOutputter.new("fout", :filename => "log/#{name}.log")
56 58 else
lib/bitcoin/network/command_client.rb
... ... @@ -17,11 +17,8 @@
17 17 @connection_attempts = 0
18 18 end
19 19  
20   - def log
21   - return @log if @log
22   - @log = Bitcoin::Logger.create("command_client")
23   - @log.level = :info
24   - @log
  20 + def log;
  21 + @log ||= Bitcoin::Logger.create(:client)
25 22 end
26 23  
27 24 def self.connect host, port, *args, &block
lib/bitcoin/network/node.rb
1   -require 'eventmachine'
2   -require 'json'
  1 +Bitcoin.require_dependency :eventmachine
  2 +Bitcoin.require_dependency :json
3 3 require 'fileutils'
4 4  
5 5 module Bitcoin::Network
6 6  
... ... @@ -64,14 +64,13 @@
64 64 :inv_queue => 5,
65 65 :addrs => 5,
66 66 :connect => 15,
67   - :relay => 30,
  67 + :relay => 600,
68 68 },
69 69 }
70 70  
71 71 def initialize config = {}
72 72 @config = DEFAULT_CONFIG.deep_merge(config)
73   - @log = Bitcoin::Logger.create("network")
74   - @log.level = @config[:log][:network]
  73 + @log = Bitcoin::Logger.create(:network, @config[:log][:network])
75 74 @connections = []
76 75 @command_connections = []
77 76 @queue = []
78 77  
79 78  
... ... @@ -194,19 +193,19 @@
194 193 # get peer addrs from given dns +seed+ using em/dns_resolver.
195 194 # fallback to using `nslookup` if it is not installed or fails.
196 195 def connect_dns_resolver(seed)
197   - require 'em/dns_resolver'
198   - log.info { "Querying addresses from DNS seed: #{seed}" }
  196 + if Bitcoin.require_dependency "em/dns_resolver", gem: "em-dns", exit: false
  197 + log.info { "Querying addresses from DNS seed: #{seed}" }
199 198  
200   - dns = EM::DnsResolver.resolve(seed)
201   - dns.callback {|addrs| yield(addrs) }
202   - dns.errback do |*a|
203   - log.error { "Cannot resolve DNS seed #{seed}: #{a.inspect}" }
204   - connect_dns_nslookup(Bitcoin.network[:dns_seeds].sample) {|a| yield(a) }
  199 + dns = EM::DnsResolver.resolve(seed)
  200 + dns.callback {|addrs| yield(addrs) }
  201 + dns.errback do |*a|
  202 + log.error { "Cannot resolve DNS seed #{seed}: #{a.inspect}" }
  203 + connect_dns_nslookup(Bitcoin.network[:dns_seeds].sample) {|a| yield(a) }
  204 + end
  205 + else
  206 + log.info { "Falling back to nslookup resolver." }
  207 + connect_dns_nslookup(seed) {|a| yield(a) }
205 208 end
206   - rescue LoadError
207   - log.warn { "DNS resolver not installed. To install run: `gem install em-dns`" }
208   - log.info { "Falling back to nslookup resolver." }
209   - connect_dns_nslookup(seed) {|a| yield(a) }
210 209 end
211 210  
212 211 # get peers from dns via nslookup
lib/bitcoin/protocol/parser.rb
... ... @@ -2,14 +2,15 @@
2 2 module Protocol
3 3  
4 4 class Parser
5   -
  5 +
6 6 def initialize(handler=nil)
7 7 @h = handler || Handler.new
8 8 @buf = ""
9   - @log = Bitcoin::Logger.create("parser")
10 9 end
11   -
12   - def log; @log; end
  10 +
  11 + def log
  12 + @log ||= Bitcoin::Logger.create("parser")
  13 + end
13 14  
14 15 # handles inv/getdata packets
15 16 #
lib/bitcoin/storage/sequel.rb
1   -begin
2   - require 'sequel'
3   -rescue LoadError
4   - puts "Cannot load 'sequel' - install with `gem install sequel`"
5   - puts "Note: You will also need an adapter for your database like sqlite3, mysql2, postgresql"
6   - exit 1
7   -end
  1 +Bitcoin.require_dependency :sequel, message:
  2 + "Note: You will also need an adapter for your database like sqlite3, mysql2, postgresql"
8 3 require 'bitcoin/storage/sequel_store/sequel_migrations'
9 4  
10 5 module Bitcoin::Storage::Backends
... ... @@ -46,6 +41,13 @@
46 41  
47 42 # connect to database
48 43 def connect
  44 + {:sqlite => "sqlite3", :postgres => "pg", :mysql => "mysql",
  45 + }.each do |adapter, name|
  46 + if @config[:db].split(":").first == adapter.to_s
  47 + Bitcoin.require_dependency name, gem: name
  48 + end
  49 + end
  50 +
49 51 @db = Sequel.connect(@config[:db])
50 52 migrate
51 53 end
lib/bitcoin/storage/storage.rb
... ... @@ -8,7 +8,7 @@
8 8  
9 9 autoload :Models, 'bitcoin/storage/models'
10 10  
11   - @log = Bitcoin::Logger.create("storage")
  11 + @log = Bitcoin::Logger.create(:storage)
12 12 def self.log; @log; end
13 13  
14 14 BACKENDS = [:dummy, :sequel]
15 15  
... ... @@ -31,13 +31,12 @@
31 31 # inside the appropriate Bitcoin::Storage::Models class.
32 32 class StoreBase
33 33  
  34 + attr_reader :log
  35 +
34 36 def initialize(config = {})
35 37 @config = config
36 38 @log = config[:log] || Bitcoin::Storage.log
37 39 end
38   -
39   - # get the storage logger
40   - def log; @log; end
41 40  
42 41 # reset the store; delete all data
43 42 def reset