Commit b4547a3b78f5c979ae3385ba34960a4caddac7ec

Authored by Marius Hanne
1 parent 17d7c2a9c1

try to detect if blockchain is in sync with the network

if not, send +getblocks+ repeatedly, don't accept tx invs, and don't relay tx

Showing 1 changed file with 5 additions and 1 deletions Side-by-side Diff

lib/bitcoin/network/node.rb
... ... @@ -82,6 +82,7 @@
82 82 @timers = {}
83 83 @inv_cache = []
84 84 @notifiers = Hash[[:block, :tx, :connection, :addr].map {|n| [n, EM::Channel.new]}]
  85 + @in_sync = false
85 86 end
86 87  
87 88 def set_store
... ... @@ -265,7 +266,7 @@
265 266 @log.debug { "queue worker running" }
266 267 EM.defer(nil, proc { work_queue }) do
267 268 if @queue.size == 0
268   - getblocks if @inv_queue.size == 0# TODO: stop when up to date
  269 + getblocks if @inv_queue.size == 0 && !@in_sync
269 270 sleep @config[:intervals][:queue]
270 271 end
271 272 while obj = @queue.shift
... ... @@ -283,6 +284,7 @@
283 284 puts *$@
284 285 end
285 286 end
  287 + @in_sync = (@store.get_head && (Time.now - @store.get_head.time).to_i < 3600) ? true : false
286 288 end
287 289 end
288 290  
... ... @@ -294,6 +296,7 @@
294 296 @log.debug { "inv queue worker running" }
295 297 next if @queue.size >= @config[:max][:queue]
296 298 while inv = @inv_queue.shift
  299 + next if !@in_sync && inv[0] == :tx
297 300 # next if @store.send("has_#{inv[0]}", inv[1])
298 301 inv[2].send("send_getdata_#{inv[0]}", inv[1])
299 302 end
... ... @@ -321,6 +324,7 @@
321 324 end
322 325  
323 326 def relay_tx(tx)
  327 + return false unless @in_sync
324 328 @store.store_tx(tx)
325 329 @connections.sample((@connections.size / 2) + 1).each do |peer|
326 330 peer.send_inv(:tx, tx)