Class: Bitcoin::Network::Pool
- Inherits:
-
Object
- Object
- 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.
-
#mutex ⇒ Object
readonly
Returns the value of attribute mutex.
-
#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.
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
#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 |
#mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
24 25 26 |
# File 'lib/bitcoin/network/pool.rb', line 24 def mutex @mutex 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.
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_clear ⇒ Object
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 |
#start ⇒ Object
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 |
#terminate ⇒ Object
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 |