Class: Bitcoin::Network::Pool

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/bitcoin/network/pool.rb

Overview

peer pool class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, chain, configuration) ⇒ Pool

Returns a new instance of Pool.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bitcoin/network/pool.rb', line 26

def initialize(node, chain, configuration)
  @node = node
  @peers = []
  @pending_peers = []
  @max_outbound = MAX_OUTBOUND_CONNECTIONS
  @chain = chain
  @logger = Bitcoin::Logger.create(:debug)
  @configuration = configuration
  @peer_discovery = PeerDiscovery.new(configuration)
  @started = false
  @mutex = Mutex.new
end

Instance Attribute Details

#chainObject (readonly)

Returns the value of attribute chain.



19
20
21
# File 'lib/bitcoin/network/pool.rb', line 19

def chain
  @chain
end

#loggerObject (readonly)

Returns the value of attribute logger.



21
22
23
# File 'lib/bitcoin/network/pool.rb', line 21

def logger
  @logger
end

#max_outboundObject (readonly)

Returns the value of attribute max_outbound.



20
21
22
# File 'lib/bitcoin/network/pool.rb', line 20

def max_outbound
  @max_outbound
end

#mutexObject (readonly)

Returns the value of attribute mutex.



24
25
26
# File 'lib/bitcoin/network/pool.rb', line 24

def mutex
  @mutex
end

#nodeObject (readonly)

Returns the value of attribute node.



18
19
20
# File 'lib/bitcoin/network/pool.rb', line 18

def node
  @node
end

#peer_discoveryObject (readonly)

Returns the value of attribute peer_discovery.



22
23
24
# File 'lib/bitcoin/network/pool.rb', line 22

def peer_discovery
  @peer_discovery
end

#peersObject (readonly)

active peers



16
17
18
# File 'lib/bitcoin/network/pool.rb', line 16

def peers
  @peers
end

#pending_peersObject (readonly)

currently connecting peer



17
18
19
# File 'lib/bitcoin/network/pool.rb', line 17

def pending_peers
  @pending_peers
end

#startedObject

Returns the value of attribute started.



23
24
25
# File 'lib/bitcoin/network/pool.rb', line 23

def started
  @started
end

Instance Method Details

#broadcast(tx) ⇒ Object

broadcast tx to connecting peer.



82
83
84
# File 'lib/bitcoin/network/pool.rb', line 82

def broadcast(tx)
  peers.each { |peer| peer.broadcast_tx(tx) }
end

#filter_add(element) ⇒ Object

add element to bloom filter.



92
93
94
# File 'lib/bitcoin/network/pool.rb', line 92

def filter_add(element)
  peers.each { |peer| peer.send_filter_add(element) }
end

#filter_clearObject

clear bloom filter.



97
98
99
# File 'lib/bitcoin/network/pool.rb', line 97

def filter_clear
  peers.each { |peer| peer.send_filter_clear }
end

#filter_load(peer) ⇒ Object

new bloom filter.



87
88
89
# File 'lib/bitcoin/network/pool.rb', line 87

def filter_load(peer)
  peer.send_filter_load(node.bloom)
end

#handle_close_peer(peer) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/bitcoin/network/pool.rb', line 65

def handle_close_peer(peer)
  return unless started
  peers.delete(peer)
  pending_peers.delete(peer)
  addr_list = peer_discovery.peers - peers.map(&:host) - pending_peers.map(&:host) - [peer.host]
  connect(addr_list)
end

#handle_error(e) ⇒ Object



101
102
103
# File 'lib/bitcoin/network/pool.rb', line 101

def handle_error(e)
  terminate
end

#handle_new_peer(peer) ⇒ Object

detect new peer connection.



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bitcoin/network/pool.rb', line 51

def handle_new_peer(peer)
  logger.debug "connected new peer #{peer.addr}."
  mutex.synchronize do
    peer.id = allocate_peer_id
    unless peers.find(&:primary?)
      peer.primary = true
      peer.start_block_header_download
    end
    peers << peer
  end
  pending_peers.delete(peer)
  filter_load(peer) if node.wallet
end

#startObject

connecting other peers and begin network activity.



40
41
42
43
44
45
46
47
48
# File 'lib/bitcoin/network/pool.rb', line 40

def start
  raise 'Cannot start a peer pool twice.' if started
  logger.debug 'Start connecting other pears.'
  addr_list = peer_discovery.peers

  connect(addr_list)

  @started = true
end

#terminateObject

terminate peers.



74
75
76
77
78
79
# File 'lib/bitcoin/network/pool.rb', line 74

def terminate
  peers.each { |peer| peer.close('terminate') }
  pending_peers.each { |peer| peer.close('terminate') }
  @peers = []
  @started = false
end