Class: Avalon::Eloipool

Inherits:
Node
  • Object
show all
Defined in:
lib/avalon/eloipool.rb

Overview

Pool is a node encapsulating pool software

Instance Attribute Summary

Attributes inherited from Node

#data, #ip

Instance Method Summary collapse

Methods inherited from Node

#[], #[]=, create, #num, #pool_hash, #reset, #unit_hash

Methods included from Utils

#alarm, #duration, #find_file, #ping, #play, #system

Constructor Details

#initialize(monitor, ip, frequency) ⇒ Eloipool

Returns a new instance of Eloipool.



5
6
7
8
9
10
11
# File 'lib/avalon/eloipool.rb', line 5

def initialize monitor, ip, frequency
  @ip, @frequency = ip, frequency
  @update_num = 0
  @block_file = Avalon::Config[:block_file]
  @blocks = load_blocks || {}
  super()
end

Instance Method Details

#add_new_blocks(pool_log, print = true) ⇒ Object

Add new blocks from pool log



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/avalon/eloipool.rb', line 80

def add_new_blocks pool_log, print = true
  Block.print_headers if print
  pool_log.split(/\n/).each do |line|
    hash = line.chomp.match(/\h*$/).to_s
    unless @blocks[hash]
      @blocks[hash] = Block.new(hash)
      puts @blocks[hash] if print
      @pending_hash = hash
    end
  end
  save_blocks
end

#inspectObject



97
98
99
# File 'lib/avalon/eloipool.rb', line 97

def inspect
  @data.map {|name, value| "#{name}:#{value}"}.join(" ")
end

#load_blocks(print = true) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/avalon/eloipool.rb', line 42

def load_blocks print = true
  if File.exist?(@block_file)
    Block.print_headers
    dump = YAML::load_file(@block_file)
    Hash[
      *dump.map do |data|
        block = Block.new data
        puts block
        [data[:hash], block]
      end.flatten
    ]
  end
end

#poll(verbose = true) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/avalon/eloipool.rb', line 13

def poll verbose=true
  self[:ping] = ping @ip

  self[:found] = `ssh #{@ip} "cat solo/logs/pool.log | grep BLKHASH | wc -l"`.to_i

  update_old_block

  puts "#{self}" if verbose
end

#reportObject

Check for any exceptional situations, sound alarm if any



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/avalon/eloipool.rb', line 24

def report
  if self[:ping].nil?
    alarm "Eloipool at #{@ip} not responding to ping"
  elsif self[:found] > @blocks.size
    add_new_blocks `ssh #{@ip} "cat solo/logs/pool.log | grep BLKHASH"`
    alarm "Eloipool found #{@found} blocks", :block_found
  elsif @blocks[@blocks.keys.last].pending?
    update_block @blocks[@blocks.keys.last] do
      alarm "Eloipool last block updated", :block_updated
    end
  end
end

#save_blocksObject



37
38
39
40
# File 'lib/avalon/eloipool.rb', line 37

def save_blocks
  dump = @blocks.values.map(&:data)
  File.open(@block_file, "w") {|file| YAML.dump(dump, file)}
end

#to_sObject



93
94
95
# File 'lib/avalon/eloipool.rb', line 93

def to_s
  "Eloipool: " + @data.map {|name, value| "#{name}:#{value}"}.join(" ")
end

#update_block(block, print = true) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/avalon/eloipool.rb', line 68

def update_block block, print = true
  if (block.pending? ? block.blockchain_update : block.bitcoind_update )
    if print
      Block.print_headers
      puts block
    end
    save_blocks
    yield block if block_given?
  end
end

#update_old_blockObject



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/avalon/eloipool.rb', line 56

def update_old_block
  if rand(@frequency) == 0 # update once per @frequency polls
    hash = @blocks.keys[@update_num]
    if @blocks[hash]
      @update_num += 1
      update_block(@blocks[hash], true)
    else
      @update_num = 0
    end
  end
end