Module: Meeseeker

Extended by:
Meeseeker
Included in:
Meeseeker
Defined in:
lib/meeseeker.rb,
lib/meeseeker/version.rb,
lib/meeseeker/block_follower_job.rb,
lib/meeseeker/witness_schedule_job.rb

Defined Under Namespace

Modules: HiveEngine, SteemEngine Classes: BlockFollowerJob, WitnessScheduleJob

Constant Summary collapse

STEEM_CHAIN_ID =
'0000000000000000000000000000000000000000000000000000000000000000'
HIVE_LEGACY_CHAIN_ID =
'0000000000000000000000000000000000000000000000000000000000000000'
HIVE_CHAIN_ID =
'beeab0de00000000000000000000000000000000000000000000000000000000'
STEEM_CHAIN_KEY_PREFIX =
'steem'
HIVE_CHAIN_KEY_PREFIX =
'hive'
STEEM_ENGINE_CHAIN_KEY_PREFIX =
'steem_engine'
HIVE_ENGINE_CHAIN_KEY_PREFIX =
'hive_engine'
LAST_BLOCK_NUM_KEY_SUFFIX =
':meeseeker:last_block_num'
LAST_STEEM_ENGINE_BLOCK_NUM_KEY_SUFFIX =
':meeseeker:last_block_num'
BLOCKS_PER_DAY =
28800
VIRTUAL_TRX_ID =
'0000000000000000000000000000000000000000'
BLOCK_INTERVAL =
3
SHUFFLE_URL =
'shuffle'
DEFAULT_STEEM_URL =
'https://api.steemit.com'
DEFAULT_STEEM_FAILOVER_URLS =
[
  DEFAULT_STEEM_URL,
  # 'https://steemd.minnowsupportproject.org',
  # 'https://anyx.io',
  # 'http://anyx.io',
  # 'https://steemd.privex.io',
  # 'https://api.steem.house'
]
DEFAULT_HIVE_URL =
'https://api.openhive.network'
DEFAULT_HIVE_FAILOVER_URLS =
[
  DEFAULT_HIVE_URL,
  'https://api.hivekings.com',
  'https://anyx.io',
  'http://anyx.io',
  'https://techcoderx.com',
  'https://rpc.esteem.app',
  'https://hived.privex.io',
  'https://api.pharesim.me',
  'https://api.hive.blog',
  'https://rpc.ausbit.dev'
]
VERSION =
'2.0.0'
AGENT_ID =
"meeseeker/#{VERSION}"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#expire_keysObject

Returns the value of attribute expire_keys.



104
105
106
# File 'lib/meeseeker.rb', line 104

def expire_keys
  @expire_keys
end

#hive_engine_node_urlObject

Returns the value of attribute hive_engine_node_url.



104
105
106
# File 'lib/meeseeker.rb', line 104

def hive_engine_node_url
  @hive_engine_node_url
end

#include_block_headerObject

Returns the value of attribute include_block_header.



104
105
106
# File 'lib/meeseeker.rb', line 104

def include_block_header
  @include_block_header
end

#include_virtualObject

Returns the value of attribute include_virtual.



104
105
106
# File 'lib/meeseeker.rb', line 104

def include_virtual
  @include_virtual
end

#max_keysObject

Returns the value of attribute max_keys.



104
105
106
# File 'lib/meeseeker.rb', line 104

def max_keys
  @max_keys
end

#node_urlObject

Returns the value of attribute node_url.



104
105
106
# File 'lib/meeseeker.rb', line 104

def node_url
  @node_url
end

#publish_op_custom_idObject

Returns the value of attribute publish_op_custom_id.



104
105
106
# File 'lib/meeseeker.rb', line 104

def publish_op_custom_id
  @publish_op_custom_id
end

#redisObject

Returns the value of attribute redis.



104
105
106
# File 'lib/meeseeker.rb', line 104

def redis
  @redis
end

#steem_engine_node_urlObject

Returns the value of attribute steem_engine_node_url.



104
105
106
# File 'lib/meeseeker.rb', line 104

def steem_engine_node_url
  @steem_engine_node_url
end

#stream_modeObject

Returns the value of attribute stream_mode.



104
105
106
# File 'lib/meeseeker.rb', line 104

def stream_mode
  @stream_mode
end

Class Method Details

.api_class(chain = default_chain_key_prefix) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/meeseeker.rb', line 117

def self.api_class(chain = default_chain_key_prefix)
  case chain.to_s
  when STEEM_CHAIN_KEY_PREFIX then Steem::Api
  when HIVE_CHAIN_KEY_PREFIX then Hive::Api
  else
    raise "Unknown chain: #{chain}"
  end
end

.block_api_class(chain = default_chain_key_prefix) ⇒ Object



135
136
137
138
139
140
141
142
# File 'lib/meeseeker.rb', line 135

def self.block_api_class(chain = default_chain_key_prefix)
  case chain.to_s
  when STEEM_CHAIN_KEY_PREFIX then Steem::BlockApi
  when HIVE_CHAIN_KEY_PREFIX then Hive::BlockApi
  else
    raise "Unknown chain: #{chain}"
  end
end

.chain_key_prefixObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/meeseeker.rb', line 53

def self.chain_key_prefix
  @chain_key_prefix ||= {}
  url = default_url(HIVE_CHAIN_KEY_PREFIX)
  
  return @chain_key_prefix[url] if !!@chain_key_prefix[url]
  
  # Just use the Hive API for either chain, until we know which one we're
  # using.
  api = Hive::DatabaseApi.new(url: url)
  
  api.get_config do |config|
    @chain_key_prefix[node_url] = if !!config.HIVE_CHAIN_ID && config.HIVE_CHAIN_ID == HIVE_CHAIN_ID
      HIVE_CHAIN_KEY_PREFIX
    elsif !!config.HIVE_CHAIN_ID && config.HIVE_CHAIN_ID == HIVE_LEGACY_CHAIN_ID
      HIVE_CHAIN_KEY_PREFIX
    elsif !!config.STEEM_CHAIN_ID && config.STEEM_CHAIN_ID == STEEM_CHAIN_ID
      STEEM_CHAIN_KEY_PREFIX
    else
      config.keys.find{|k| k.end_with? '_CHAIN_ID'}.split('_').first.downcase.tap do |guess|
        warn "Guessing chain_key_prefix = '#{guess}' for unknown chain on: #{node_url}"
      end
    end
  end
end

.condenser_api_class(chain = default_chain_key_prefix) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/meeseeker.rb', line 126

def self.condenser_api_class(chain = default_chain_key_prefix)
  case chain.to_s
  when STEEM_CHAIN_KEY_PREFIX then Steem::CondenserApi
  when HIVE_CHAIN_KEY_PREFIX then Hive::CondenserApi
  else
    raise "Unknown chain: #{chain}"
  end
end

.database_api_class(chain = default_chain_key_prefix) ⇒ Object



144
145
146
147
148
149
150
151
# File 'lib/meeseeker.rb', line 144

def self.database_api_class(chain = default_chain_key_prefix)
  case chain.to_s
  when STEEM_CHAIN_KEY_PREFIX then Steem::DatabaseApi
  when HIVE_CHAIN_KEY_PREFIX then Hive::DatabaseApi
  else
    raise "Unknown chain: #{chain}"
  end
end

.default_url(chain = default_chain_key_prefix) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/meeseeker.rb', line 78

def self.default_url(chain = default_chain_key_prefix)
  ENV.fetch('MEESEEKER_NODE_URL') do
    case chain.to_s
    when STEEM_CHAIN_KEY_PREFIX then DEFAULT_STEEM_URL
    when HIVE_CHAIN_KEY_PREFIX then DEFAULT_HIVE_URL
    else
      raise "Unknown chain: #{chain}"
    end
  end
end

.shuffle_node_url(chain = ENV.fetch('MEESEEKER_CHAIN_KEY_PREFIX', HIVE_CHAIN_KEY_PREFIX)) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/meeseeker.rb', line 108

def self.shuffle_node_url(chain = ENV.fetch('MEESEEKER_CHAIN_KEY_PREFIX', HIVE_CHAIN_KEY_PREFIX))
  chain = chain.to_s
  node_url = ENV.fetch('MEESEEKER_NODE_URL', default_url(ENV.fetch('MEESEEKER_CHAIN_KEY_PREFIX', chain)))
  return node_url unless node_url == SHUFFLE_URL
  
  @problem_node_urls = [] if rand(1..1000) == 13
  shuffle_node_url!(chain)
end

.shuffle_node_url!(chain = ENV.fetch('MEESEEKER_CHAIN_KEY_PREFIX', HIVE_CHAIN_KEY_PREFIX)) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/meeseeker.rb', line 162

def self.shuffle_node_url!(chain = ENV.fetch('MEESEEKER_CHAIN_KEY_PREFIX', HIVE_CHAIN_KEY_PREFIX))
  chain = chain.to_s
  failover_urls = case chain
    when STEEM_CHAIN_KEY_PREFIX then DEFAULT_STEEM_FAILOVER_URLS - @problem_node_urls
    when HIVE_CHAIN_KEY_PREFIX then DEFAULT_HIVE_FAILOVER_URLS - @problem_node_urls
    else; []
  end
  url = failover_urls.sample
  api = api_class(chain).new(url: url)
  
  api.get_accounts(['fullnodeupdate']) do |accounts|
    fullnodeupdate = accounts.first
     = (JSON[fullnodeupdate.] rescue nil) || {}
    
    nodes = .fetch('report', []).map do |report|
      next if chain == HIVE_CHAIN_KEY_PREFIX && !report[HIVE_CHAIN_KEY_PREFIX]
      next if chain != HIVE_CHAIN_KEY_PREFIX && !!report[HIVE_CHAIN_KEY_PREFIX]
      
      report['node']
    end.compact.uniq
    
    nodes -= @problem_node_urls
    
    if nodes.any?
      nodes.sample
    else
      @node_url = failover_urls.sample
    end
  end
rescue => e
  puts "#{url}: #{e}"
  
  @problem_node_urls << url
  failover_urls -= @problem_node_urls
  failover_urls.sample
end

.stream_class(chain = default_chain_key_prefix) ⇒ Object



153
154
155
156
157
158
159
160
# File 'lib/meeseeker.rb', line 153

def self.stream_class(chain = default_chain_key_prefix)
  case chain.to_s
  when STEEM_CHAIN_KEY_PREFIX then Steem::Stream
  when HIVE_CHAIN_KEY_PREFIX then Hive::Stream
  else
    raise "Unknown chain: #{chain}"
  end
end

Instance Method Details

#default_chain_key_prefixObject



49
50
51
# File 'lib/meeseeker.rb', line 49

def default_chain_key_prefix
  ENV.fetch('MEESEEKER_CHAIN_KEY_PREFIX', chain_key_prefix)
end