Commit 0328bb95cd5e60729e06ba4ecbc0b63352b14de8

Authored by Marius Hanne
1 parent 76abf2d2bc

use new config system in some commands

Showing 5 changed files with 77 additions and 22 deletions Side-by-side Diff

... ... @@ -136,11 +136,12 @@
136 136 all:
137 137 network: bitcoin
138 138 storage: sequel::postgres:/bitcoin
139   - blockchain:
140 139 command: 127.0.0.1:1234
  140 + blockchain:
  141 + max:
  142 + connect: 30
141 143 wallet:
142 144 keystore: "simple::file=keys.json"
143   - node: 127.0.0.1:1234
144 145  
145 146 Options in the +all+ category are loaded by every command, and are loaded first
146 147 (so command-specific options will override them).
... ... @@ -4,13 +4,21 @@
4 4 require 'bitcoin'
5 5 require 'optparse'
6 6  
7   -options = {
  7 +defaults = {
8 8 :network => "testnet",
9   - :storage => "Dummy",
  9 + :storage => "dummy",
10 10 :list => false,
11 11 }
  12 +
  13 +options = Bitcoin::Config.load(defaults, :balance)
  14 +
12 15 optparse = OptionParser.new do|opts|
13 16 opts.banner = "Usage: bitcoin_balance [options] <address>"
  17 +
  18 + opts.on("-c", "--config [FILE]",
  19 + "Config file (default: #{Bitcoin::Config::CONFIG_PATHS})") do |file|
  20 + options = Bitcoin::Config.load_file(options, file, :wallet)
  21 + end
14 22  
15 23 opts.on("-n", "--network [NETWORK]", "User Network (default: testnet)") do |network|
16 24 options[:network] = network
bin/bitcoin_blockchain
... ... @@ -4,10 +4,10 @@
4 4 require 'bitcoin'
5 5 require 'optparse'
6 6  
7   -options = {
  7 +defaults = {
8 8 :network => "testnet",
9 9 :command => "127.0.0.1:9999",
10   - :listen => "0.0.0.0:18332",
  10 + :listen => "0.0.0.0:8332",
11 11 :connect => "",
12 12 :storage => "dummy",
13 13 :headers_only => false,
14 14  
... ... @@ -32,9 +32,19 @@
32 32 :connect => 15,
33 33 },
34 34 }
35   -optparse = OptionParser.new do|opts|
  35 +
  36 +options = Bitcoin::Config.load(defaults, :blockchain)
  37 +
  38 +optparse = OptionParser.new do |opts|
36 39 opts.banner = "Usage: bitcoin_node [options]"
37 40  
  41 + opts.separator("\nAvailable options:\n")
  42 +
  43 + opts.on("-c", "--config FILE",
  44 + "Config file (default: #{Bitcoin::Config::CONFIG_PATHS})") do |file|
  45 + options = Bitcoin::Config.load_file(options, file, :blockchain)
  46 + end
  47 +
38 48 opts.on("-n", "--network [NETWORK]",
39 49 "User Network (default: #{options[:network]})") do |network|
40 50 options[:network] = network
... ... @@ -50,7 +60,7 @@
50 60 options[:listen] = listen
51 61 end
52 62  
53   - opts.on("-c", "--connect [HOST:PORT[,HOST:PORT[...]]]",
  63 + opts.on("--connect [HOST:PORT[,HOST:PORT[...]]]",
54 64 "Hosts to connect to (default: #{options[:connect]})") do |connect|
55 65 options[:connect] = connect
56 66 end
... ... @@ -117,8 +127,7 @@
117 127 end
118 128  
119 129 opts.on( '-h', '--help', 'Display this screen' ) do
120   - puts opts
121   - exit
  130 + puts opts; exit
122 131 end
123 132 end
124 133  
1 1 #!/usr/bin/env ruby
2 2 $:.unshift( File.expand_path("../../lib", __FILE__) )
  3 +
3 4 require 'eventmachine'
4 5 require 'json'
5 6 require 'bitcoin'
  7 +require 'optparse'
6 8  
7   -host, port = "127.0.0.1", 9999
8   -if ARGV[0] == "-s"
9   - host, port = ARGV[1].split(":")
10   - ARGV.shift; ARGV.shift
  9 +defaults = {
  10 + :command => "127.0.0.1:9999"
  11 +}
  12 +options = Bitcoin::Config.load(defaults, :command)
  13 +
  14 +optparse = OptionParser.new do |opts|
  15 + opts.banner =
  16 + "Usage: bitcoin_command [options] <command> [<command options>]\n"
  17 +
  18 + opts.separator("\nAvailable options:\n")
  19 +
  20 + opts.on("-c", "--config FILE",
  21 + "Config file (default: #{Bitcoin::Config::CONFIG_PATHS})") do |file|
  22 + options = Bitcoin::Config.load_file(options, file, :wallet)
  23 + end
  24 +
  25 + opts.on("--command HOST:PORT",
  26 + "Node command socket (default: #{options[:command]})") do |command|
  27 + options[:command] = command
  28 + end
  29 +
  30 + opts.on("-h", "--help", "Display this help") do
  31 + puts opts; exit
  32 + end
  33 +
  34 + opts.separator "\nAvailable commands:\n" +
  35 + "run `bitcoin_command help` to see all available commands."
11 36 end
12 37  
  38 +optparse.parse!
  39 +host, port = options[:command].split(":")
  40 +unless ARGV.any?
  41 + puts optparse; exit
  42 +end
13 43  
14 44 EM.run do
  45 +
15 46 EM.connect(host, port) do |connection|
16 47  
17 48 connection.send_data([ARGV[0], ARGV[1..-1].join(" ")].to_json)
... ... @@ -9,7 +9,7 @@
9 9 :network => "testnet",
10 10 :storage => "dummy",
11 11 :keystore => "dummy",
12   - :node => "127.0.0.1:9999"
  12 + :command => "127.0.0.1:9999"
13 13 }
14 14 options = Bitcoin::Config.load(defaults, :wallet)
15 15  
16 16  
17 17  
18 18  
... ... @@ -18,23 +18,25 @@
18 18 "Usage: bitcoin_wallet [options] <command> [<command options>]\n"
19 19  
20 20 opts.separator("\nAvailable options:\n")
21   - opts.on("-c", "--config [FILE]",
  21 +
  22 + opts.on("-c", "--config FILE",
22 23 "Config file (default: #{Bitcoin::Config::CONFIG_PATHS})") do |file|
23 24 options = Bitcoin::Config.load_file(options, file, :wallet)
24 25 end
25   - opts.on("-n", "--network [NETWORK]",
  26 +
  27 + opts.on("-n", "--network NETWORK",
26 28 "User Network (default: #{options[:network]})") do |network|
27 29 options[:network] = network
28 30 end
29 31  
30   - opts.on("-s", "--storage [BACKEND::CONFIG]",
  32 + opts.on("-s", "--storage BACKEND::CONFIG",
31 33 "Use storage backend (default: #{options[:storage]})") do |storage|
32 34 options[:storage] = storage
33 35 end
34 36  
35   - opts.on("--node [HOST:PORT]",
36   - "Node command socket (default: #{options[:node]})") do |node|
37   - options[:node] = node
  37 + opts.on("--command [HOST:PORT]",
  38 + "Node command socket (default: #{options[:command]})") do |command|
  39 + options[:command] = command
38 40 end
39 41  
40 42 opts.on("-k", "--keystore [backend::<config>]",
... ... @@ -42,6 +44,10 @@
42 44 options[:keystore] = store
43 45 end
44 46  
  47 + opts.on("-h", "--help", "Display this help") do
  48 + puts opts; exit
  49 + end
  50 +
45 51 opts.separator "\nAvailable commands:\n" +
46 52 " balance [<addr>] - display balance for given addr or whole wallet\n" +
47 53 " list <addr> - list transaction history for address\n" +
... ... @@ -195,7 +201,7 @@
195 201 puts "Aborted."; exit
196 202 end
197 203  
198   - host, port = *options[:node].split(":")
  204 + host, port = *options[:command].split(":")
199 205 s = TCPSocket.new(host, port.to_i)
200 206 s.puts "relay_tx " + tx.to_payload.unpack("H*")[0]
201 207