Class: Bitcoin::Network::Pool
- Includes:
- Observable
- Defined in:
- lib/bitcoin/network/pool.rb
Overview
peer pool class.
Instance Attribute Summary collapse
-
#chain ⇒ Object
readonly
Returns the value of attribute chain.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#max_outbound ⇒ Object
readonly
Returns the value of attribute max_outbound.
-
#node ⇒ Object
readonly
Returns the value of attribute node.
-
#peer_discovery ⇒ Object
readonly
Returns the value of attribute peer_discovery.
-
#peers ⇒ Object
readonly
active peers.
-
#pending_peers ⇒ Object
readonly
currently connecting peer.
-
#started ⇒ Object
Returns the value of attribute started.
Instance Method Summary collapse
-
#broadcast(tx) ⇒ Object
broadcast tx to connecting peer.
-
#filter_add(element) ⇒ Object
add element to bloom filter.
-
#filter_clear ⇒ Object
clear bloom filter.
-
#filter_load(peer) ⇒ Object
new bloom filter.
- #handle_close_peer(peer) ⇒ Object
- #handle_error(e) ⇒ Object
-
#handle_new_peer(peer) ⇒ Object
detect new peer connection.
-
#initialize(node, chain, configuration) ⇒ Pool
constructor
A new instance of Pool.
-
#start ⇒ Object
connecting other peers and begin network activity.
-
#terminate ⇒ Object
terminate peers.
Constructor Details
#initialize(node, chain, configuration) ⇒ Pool
Returns a new instance of Pool.
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/bitcoin/network/pool.rb', line 25 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 end |
Instance Attribute Details
#chain ⇒ Object (readonly)
Returns the value of attribute chain.
19 20 21 |
# File 'lib/bitcoin/network/pool.rb', line 19 def chain @chain end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
21 22 23 |
# File 'lib/bitcoin/network/pool.rb', line 21 def logger @logger end |
#max_outbound ⇒ Object (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 |
#node ⇒ Object (readonly)
Returns the value of attribute node.
18 19 20 |
# File 'lib/bitcoin/network/pool.rb', line 18 def node @node end |
#peer_discovery ⇒ Object (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 |
#peers ⇒ Object (readonly)
active peers
16 17 18 |
# File 'lib/bitcoin/network/pool.rb', line 16 def peers @peers end |
#pending_peers ⇒ Object (readonly)
currently connecting peer
17 18 19 |
# File 'lib/bitcoin/network/pool.rb', line 17 def pending_peers @pending_peers end |
#started ⇒ Object
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.
78 79 80 |
# File 'lib/bitcoin/network/pool.rb', line 78 def broadcast(tx) peers.each { |peer| peer.broadcast_tx(tx) } end |
#filter_add(element) ⇒ Object
add element to bloom filter.
88 89 90 |
# File 'lib/bitcoin/network/pool.rb', line 88 def filter_add(element) peers.each { |peer| peer.send_filter_add(element) } end |
#filter_clear ⇒ Object
clear bloom filter.
93 94 95 |
# File 'lib/bitcoin/network/pool.rb', line 93 def filter_clear peers.each { |peer| peer.send_filter_clear } end |
#filter_load(peer) ⇒ Object
new bloom filter.
83 84 85 |
# File 'lib/bitcoin/network/pool.rb', line 83 def filter_load(peer) peer.send_filter_load(node.bloom) end |
#handle_close_peer(peer) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/bitcoin/network/pool.rb', line 61 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
97 98 99 |
# File 'lib/bitcoin/network/pool.rb', line 97 def handle_error(e) terminate end |
#handle_new_peer(peer) ⇒ Object
detect new peer connection.
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/bitcoin/network/pool.rb', line 49 def handle_new_peer(peer) logger.debug "connected new peer #{peer.addr}." peer.id = allocate_peer_id unless peers.find(&:primary?) peer.primary = true peer.start_block_header_download end peers << peer pending_peers.delete(peer) filter_load(peer) if node.wallet end |
#start ⇒ Object
connecting other peers and begin network activity.
38 39 40 41 42 43 44 45 46 |
# File 'lib/bitcoin/network/pool.rb', line 38 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 |
#terminate ⇒ Object
terminate peers.
70 71 72 73 74 75 |
# File 'lib/bitcoin/network/pool.rb', line 70 def terminate peers.each { |peer| peer.close('terminate') } pending_peers.each { |peer| peer.close('terminate') } @peers = [] @started = false end |