Class: Bitcoin::Network::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/bitcoin/network/node.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Node) initialize

A new instance of Node



10
11
12
13
14
15
16
17
# File 'lib/bitcoin/network/node.rb', line 10

def initialize
  @log = Bitcoin::Logger.create("network")
  @log.level = :debug
  @connections = []
  @queue = []
  @block = 0 # temp hack to store block depth of connected node
  @store = Bitcoin::Storage::Backends::Dummy.new
end

Instance Attribute Details

- (Object) block

Returns the value of attribute block



8
9
10
# File 'lib/bitcoin/network/node.rb', line 8

def block
  @block
end

- (Object) connections (readonly)

Returns the value of attribute connections



7
8
9
# File 'lib/bitcoin/network/node.rb', line 7

def connections
  @connections
end

- (Object) host (readonly)

Returns the value of attribute host



7
8
9
# File 'lib/bitcoin/network/node.rb', line 7

def host
  @host
end

- (Object) log (readonly)

Returns the value of attribute log



7
8
9
# File 'lib/bitcoin/network/node.rb', line 7

def log
  @log
end

- (Object) port (readonly)

Returns the value of attribute port



7
8
9
# File 'lib/bitcoin/network/node.rb', line 7

def port
  @port
end

- (Object) queue (readonly)

Returns the value of attribute queue



7
8
9
# File 'lib/bitcoin/network/node.rb', line 7

def queue
  @queue
end

- (Object) store (readonly)

Returns the value of attribute store



7
8
9
# File 'lib/bitcoin/network/node.rb', line 7

def store
  @store
end

Instance Method Details

- (Object) check_query_blocks



45
46
47
48
49
50
51
52
# File 'lib/bitcoin/network/node.rb', line 45

def check_query_blocks
  log.debug { "Checking queue" }
  return  unless @connections.any?
  log.info { "Querying blocks" }
  if @queue.size < 128
    @connections[rand(@connections.size)].query_blocks
  end
end

- (Object) check_queue



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/bitcoin/network/node.rb', line 54

def check_queue
  log.debug { "Checking queue" }
  return unless @queue.any?
  while block = @queue.shift
    next  unless block
    log.debug { "Processing queue item #{block.hash} (#{block.payload.size} bytes)" }

    @store.store_block(block)
  end
  check_query_blocks
  log.info { "Queue empty" }
rescue Exception
  p $!
  puts *$@
  exit 1
end

- (Object) run



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bitcoin/network/node.rb', line 19

def run
  EM.run do
    EventMachine::add_periodic_timer(10) { check_query_blocks }
    EventMachine::add_periodic_timer(0.5) { check_queue }
    begin
      log.info { "Attempting to connect to #{@host}:#{@port}" }
      h, p = "127.0.0.1", Bitcoin::network[:default_port]
      EM.connect(h, p, Handler, self, h, p)
    rescue Exception
      log.error { $!.inspect }
      puts $@
      exit 1
    end
    Signal.trap("INT") do
      log.info { "Shutting down..." }
      Signal.trap("INT") do
        log.warn { "Force Exit" }
        exit 1
      end
      EM.stop_event_loop
    end

  end
  log.info { "Bye" }
end