-
Notifications
You must be signed in to change notification settings - Fork 4
/
Rakefile
652 lines (521 loc) · 22.3 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
require "bundler/gem_tasks"
require "rake/testtask"
require 'meeseeker'
defined? Thread.report_on_exception and Thread.report_on_exception = true
Rake::TestTask.new(:test) do |t|
t.libs << 'test'
t.libs << 'lib'
t.test_files = FileList['test/**/*_test.rb']
t.ruby_opts << if ENV['HELL_ENABLED']
'-W2'
else
'-W1'
end
end
task :default => :test
task :console do
exec "irb -r meeseeker -I ./lib"
end
desc 'Build a new version of the meeseeker gem.'
task :build do
exec 'gem build meeseeker.gemspec'
end
desc 'Publish the current version of the meeseeker gem.'
task :push do
exec "gem push meeseeker-#{Meeseeker::VERSION}.gem"
end
desc 'Build a new version of the meeseeker docker image.'
task :docker_build do
exec 'docker build -t inertia/meeseeker:latest .'
end
desc 'Publish the current version of the meeseeker docker image.'
task :docker_push do
exec 'docker push inertia/meeseeker:latest'
end
task :check_schema do
begin
abort 'Unable to ping redis source.' unless Meeseeker.redis.ping == 'PONG'
rescue Redis::CommandError => e
puts e
rescue Redis::CannotConnectError => e
puts e
end
end
task(:sync, [:chain, :at_block_num] => [:check_schema]) do |t, args|
chain = args[:chain] if args[:chain]
chain ||= Meeseeker.default_chain_key_prefix
job = case chain.to_sym
when :steem_engine
Meeseeker::SteemEngine::FollowerJob.new
when :hive_engine
Meeseeker::HiveEngine::FollowerJob.new
else
Meeseeker::BlockFollowerJob.new
end
job.perform(chain: chain, at_block_num: args[:at_block_num])
end
namespace :witness do
desc 'Publish the witness schedule every minute or so (e.g.: hive:witness:schedule).'
task :schedule, [:chain] do |t, args|
chain = args[:chain] if args[:chain]
chain ||= Meeseeker.default_chain_key_prefix
job = Meeseeker::WitnessScheduleJob.new
job.perform(chain: chain)
end
end
task(:find, [:what, :key, :chain] => [:check_schema]) do |t, args|
chain = args[:chain] if args[:chain]
chain ||= Meeseeker.default_chain_key_prefix
redis = Meeseeker.redis
match = case args[:what].downcase.to_sym
when :block then "#{chain}:#{args[:key]}:*"
when :trx then "#{chain}:*:#{args[:key]}:*"
else; abort "Unknown lookup using #{args}"
end
puts "Looking for match on: #{match}"
keys = redis.keys(match)
keys.each do |key|
puts key
puts redis.get(key)
end
end
task :reset, [:chain] => [:check_schema] do |t, args|
chain = (args[:chain] || 'all').to_sym
keys = []
print 'Dropping keys for set: %s ...' % chain.to_s
case chain
when :steem_engine then keys += Meeseeker.redis.keys('steem_engine:*')
when :hive_engine then keys += Meeseeker.redis.keys('hive_engine:*')
when :all
keys += Meeseeker.redis.keys('steem:*')
keys += Meeseeker.redis.keys('hive:*')
keys += Meeseeker.redis.keys('steem_engine:*')
keys += Meeseeker.redis.keys('hive_engine:*')
else
keys += Meeseeker.redis.keys("#{chain}:*")
end
if keys.any?
print " found #{keys.size} keys ..."
dropped = Meeseeker.redis.del(*keys)
puts " dropped #{dropped} keys."
else
puts ' nothing to drop.'
end
end
namespace :verify do
desc 'Verifies transactions land where they should.'
task :block_org, [:chain, :max_blocks] do |t, args|
chain = args[:chain] if args[:chain]
chain ||= Meeseeker.default_chain_key_prefix
chain_key_prefix = chain.to_s
max_blocks = args[:max_blocks]
node_url = Meeseeker.shuffle_node_url(chain)
database_api = Meeseeker.database_api_class(chain).new(url: node_url)
mode = ENV.fetch('MEESEEKER_STREAM_MODE', 'head').to_sym
until_block_num = if !!max_blocks
database_api.get_dynamic_global_properties do |dgpo|
raise 'Got empty dynamic_global_properties result.' if dgpo.nil?
case mode
when :head then dgpo.head_block_number
when :irreversible then dgpo.last_irreversible_block_num
else; abort "Unknown block mode: #{mode}"
end
end + max_blocks.to_i + 1
end
Thread.new do
job = Meeseeker::BlockFollowerJob.new
loop do
begin
job.perform(chain: chain, mode: mode, until_block_num: until_block_num)
rescue => e
puts e.inspect
sleep 5
end
break # success
end
puts 'Background sync finished ...'
end
begin
block_api = Meeseeker.block_api_class(chain).new(url: node_url)
block_channel = "#{chain_key_prefix}:block"
redis_url = ENV.fetch('MEESEEKER_REDIS_URL', 'redis://127.0.0.1:6379/0')
subscription = Redis.new(url: redis_url)
ctx = Redis.new(url: redis_url)
timeout = (max_blocks).to_i * 3
subscribe_mode, subscribe_args = if timeout > 0
[:subscribe_with_timeout, [timeout, [block_channel]]]
else
[:subscribe, [[block_channel]]]
end
subscription.send(subscribe_mode, *subscribe_args) do |on|
on.subscribe do |channel, subscriptions|
puts "Subscribed to ##{channel} (subscriptions: #{subscriptions})"
end
on.message do |channel, message|
payload = JSON[message]
block_num = payload['block_num']
expected_witness = payload['witness']
next_block_num = block_num + 1
if !!max_blocks
if block_num >= until_block_num
# We're done trailing blocks. Typically, this is used by unit
# tests so the test can halt.
subscription.unsubscribe
next
end
end
5.times do
break unless ctx.keys("#{chain_key_prefix}:#{next_block_num}:*").size == 0
# This ensures at least the next block has been indexed before
# proceeding.
puts "Waiting for block (verify:block_org): #{next_block_num} ..."
sleep 6
end
if ctx.keys("#{chain_key_prefix}:#{next_block_num}:*").size == 0
puts "Gave up waiting for block (check current_aslot slippage): #{next_block_num}"
end
database_api.get_dynamic_global_properties do |dgpo|
raise 'Got empty dynamic_global_properties result.' if dgpo.nil?
(block_num - dgpo.last_irreversible_block_num).tap do |offset|
# This will block all channel callbacks until the first known block
# is irreversible. After that, the offsets should mostly go
# negative.
sleep offset * 3 if offset > 0
end
end
# In theory, we should have all the keys using this pattern.
keys = ctx.keys("#{chain_key_prefix}:#{block_num}:*")
# If we have all the keys, we should also have all transaction ids.
expected_ids = keys.map { |k| k.split(':')[2] }.uniq
expected_ids -= [Meeseeker::VIRTUAL_TRX_ID]
actual_ids, actual_witness = block_api.get_block(block_num: block_num) do |result|
raise 'Got empty block result.' if result.nil? || result.block.nil?
block = result.block
[block.transaction_ids, block.witness]
end
# We do an intersection to make sure there's no difference between
# the two copies, regardless of order, as opposed to just checking that
# the lengths match.
(actual_ids & expected_ids).tap do |intersection|
all_sizes = [intersection.size, expected_ids.size, actual_ids.size]
puts 'intersection: %d; expected: %d; actual: %d' % all_sizes
if all_sizes.min != all_sizes.max
puts "Expected witness: #{expected_witness}; actual witness: #{actual_witness}"
puts "Expected transaction ids:"
puts expected_ids
puts "Actual transaction ids:"
puts actual_ids
puts "actual_ids minus expected:"
puts actual_ids - expected_ids
puts "expected_ids minus actual:"
puts expected_ids - actual_ids
exit(-1)
end
end
end
on.unsubscribe do |channel, subscriptions|
puts "Unsubscribed from ##{channel} (subscriptions: #{subscriptions})"
end
end
end
end
desc 'Verifies Hive Engine transactions land where they should.'
task :hive_engine_block_org, [:max_blocks] do |t, args|
Rake::Task['verify:engine_block_org'].invoke('hive_engine', args[:max_blocks])
end
desc 'Verifies Steem Engine transactions land where they should.'
task :steem_engine_block_org, [:max_blocks] do |t, args|
Rake::Task['verify:engine_block_org'].invoke('steem_engine', args[:max_blocks])
end
desc 'Verifies Steem/Hive Engine transactions land where they should.'
task :engine_block_org, [:chain_key_prefix, :max_blocks] do |t, args|
chain_key_prefix = args[:chain_key_prefix]
max_blocks = args[:max_blocks]
case chain_key_prefix.to_sym
when :steem_engine
node_url = ENV.fetch('MEESEEKER_STEEM_ENGINE_NODE_URL', 'https://api.steem-engine.com/rpc')
agent = Meeseeker::SteemEngine::Agent.new(url: node_url)
job = Meeseeker::SteemEngine::FollowerJob.new
when :hive_engine
node_url = ENV.fetch('MEESEEKER_HIVE_ENGINE_NODE_URL', 'https://api.hive-engine.com/rpc')
agent = Meeseeker::HiveEngine::Agent.new(url: node_url)
job = Meeseeker::HiveEngine::FollowerJob.new
end
until_block_num = if !!max_blocks
agent.latest_block_info['blockNumber']
end
Thread.new do
loop do
begin
at_block_num = agent.latest_block_info["blockNumber"] - max_blocks.to_i
at_block_num = [at_block_num, 1].max
job.perform(at_block_num: at_block_num, until_block_num: until_block_num)
rescue => e
puts e.inspect
sleep 5
end
break # success
end
puts 'Background sync finished ...'
end
begin
block_channel = "#{chain_key_prefix}:block"
redis_url = ENV.fetch('MEESEEKER_REDIS_URL', 'redis://127.0.0.1:6379/0')
subscription = Redis.new(url: redis_url)
ctx = Redis.new(url: redis_url)
timeout = (max_blocks).to_i * 3
subscribe_mode, subscribe_args = if timeout > 0
[:subscribe_with_timeout, [timeout, [block_channel]]]
else
[:subscribe, [[block_channel]]]
end
subscription.send(subscribe_mode, *subscribe_args) do |on|
on.subscribe do |channel, subscriptions|
puts "Subscribed to ##{channel} (subscriptions: #{subscriptions})"
end
on.message do |channel, message|
payload = JSON[message]
block_num = payload['block_num']
next_block_num = block_num + 1
if !!max_blocks
if block_num >= until_block_num
# We're done trailing blocks. Typically, this is used by unit
# tests so the test can halt.
subscription.unsubscribe
next
end
end
while ctx.keys("#{chain_key_prefix}:#{next_block_num}:*").size == 0
# This ensures at least the next block has been indexed before
# proceeding.
puts "Waiting for block (verify:#{chain_key_prefix}_engine_block_org): #{next_block_num} ..."
sleep 6
end
# In theory, we should have all the keys using this pattern.
keys = ctx.keys("#{chain_key_prefix}:#{block_num}:*")
# If we have all the keys, we should also have all transaction ids.
expected_ids = keys.map { |k| k.split(':')[2] }.uniq
expected_ids -= [Meeseeker::VIRTUAL_TRX_ID]
actual_ids = nil
agent.block(block_num).tap do |block|
raise 'Got empty block result.' if block.nil?
actual_ids = block['transactions'].map{|trx| trx['transactionId'].to_s.split('-').first}.uniq
end
# We do an intersection to make sure there's no difference between
# the two copies, regardless of order, as opposed to just checking that
# the lengths match.
(actual_ids & expected_ids).tap do |intersection|
all_sizes = [intersection.size, expected_ids.size, actual_ids.size]
puts 'intersection: %d; expected: %d; actual: %d' % all_sizes
if all_sizes.min != all_sizes.max
puts "Expected transaction ids:"
puts expected_ids
puts "Actual transaction ids:"
puts actual_ids
puts "actual_ids minus expected:"
puts actual_ids - expected_ids
puts "expected_ids minus actual:"
puts expected_ids - actual_ids
exit(-1)
end
end
end
on.unsubscribe do |channel, subscriptions|
puts "Unsubscribed from ##{channel} (subscriptions: #{subscriptions})"
end
end
end
agent.shutdown
end
desc 'Verifies Hive Engine sidechain against the mainnet.'
task :hive_engine_ref_blocks do |t|
Rake::Task['verify:engine_ref_blocks'].invoke('hive_engine')
end
desc 'Verifies Steem Engine sidechain against the mainnet.'
task :steem_engine_ref_blocks do |t|
Rake::Task['verify:engine_ref_blocks'].invoke('steem_engine')
end
desc 'Verifies Steem/Hive Engine sidechain against the mainnet.'
task :engine_ref_blocks, [:chain_key_prefix] do |t, args|
chain_key_prefix = args[:chain_key_prefix]
redis_url = ENV.fetch('MEESEEKER_REDIS_URL', 'redis://127.0.0.1:6379/0')
ctx = ctx = Redis.new(url: redis_url)
keys = ctx.keys("#{chain_key_prefix}:*:*")
mainchain, mainchain_url = case chain_key_prefix
when 'steem_engine' then ['steem', 'https://api.steemit.com']
when 'hive_engine' then ['hive', 'https://api.openhive.network']
end
block_api = Meeseeker.block_api_class(mainchain).new(url: mainchain_url)
block_trxs = {}
puts "Checking #{chain_key_prefix} keys: #{keys.size}"
keys.each do |key|
transaction = JSON[ctx.get(key)]
next if transaction.class == Integer
block_num = case chain_key_prefix
when 'steem_engine' then transaction.fetch('refSteemBlockNumber')
when 'hive_engine' then transaction.fetch('refHiveBlockNumber')
end.to_i
block_trxs[block_num] ||= []
block_trxs[block_num] << transaction['transactionId'].to_s.split('-').first
end
puts "Related mainnet blocks: #{block_trxs.keys.size}"
skipped_blocks = []
next if block_trxs.empty?
block_api.get_blocks(block_range: block_trxs.keys) do |block, block_num|
if block.nil? || block[:transaction_ids].nil?
print 'S'
skipped_blocks << block_num
next
else
print '.'
end
trx_ids = block_trxs[block_num] - [Meeseeker::VIRTUAL_TRX_ID]
if trx_ids.any? && (block.transaction_ids & trx_ids).none?
puts "\nNo intersection in #{block_num}!"
puts "Expected the following sidechain trx_ids: #{trx_ids.join(', ')}"
end
end
puts "\nBlocks to retry: #{skipped_blocks.size}"
skipped_blocks.each do |block_num|
block_found = false
block_api.get_block(block_num: block_num) do |result|
break unless !!result.block
block = result.block
block_found = true
trx_ids = block_trxs[block_num] - [Meeseeker::VIRTUAL_TRX_ID]
if trx_ids.any? && (block.transaction_ids & trx_ids).none?
puts "No intersection in #{block_num}!"
puts "Expected the following sidechain trx_ids: #{trx_ids.join(', ')}"
end
end
redo unless block_found
end
puts "Done."
end
namespace :witness do
desc 'Verifies witnessses in the schedule produced a block.'
task :schedule, [:chain, :max_blocks] do |t, args|
chain = args[:chain] if !!args[:chain]
chain ||= Meeseeker.default_chain_key_prefix
max_blocks = args[:max_blocks]
node_url = Meeseeker.shuffle_node_url(chain)
database_api = Meeseeker.database_api_class(chain).new(url: node_url)
mode = ENV.fetch('MEESEEKER_STREAM_MODE', 'head').to_sym
until_block_num = if !!max_blocks
database_api.get_dynamic_global_properties do |dgpo|
raise 'Got empty dynamic_global_properties result.' if dgpo.nil?
case mode
when :head then dgpo.head_block_number
when :irreversible then dgpo.last_irreversible_block_num
else; abort "Unknown block mode: #{mode}"
end
end + max_blocks.to_i
end
Thread.new do
job = Meeseeker::WitnessScheduleJob.new
loop do
begin
job.perform(chain: chain, mode: mode, until_block_num: until_block_num)
rescue => e
puts e.inspect
sleep 5
end
break # success
end
puts 'Background sync finished ...'
end
begin
block_api = Meeseeker.block_api_class(chain).new(url: node_url)
chain_key_prefix = chain.to_s if args[:chain]
chain_key_prefix ||= Meeseeker.default_chain_key_prefix
schedule_channel = "#{chain_key_prefix}:witness:schedule"
redis_url = ENV.fetch('MEESEEKER_REDIS_URL', 'redis://127.0.0.1:6379/0')
subscription = Redis.new(url: redis_url)
timeout = (max_blocks).to_i * 3
subscribe_mode, subscribe_args = if timeout > 0
[:subscribe_with_timeout, [timeout, [schedule_channel]]]
else
[:subscribe, [[schedule_channel]]]
end
# Check if the redis context is still available right before we
# subscribe.
break unless subscription.ping == 'PONG'
subscription.send(subscribe_mode, *subscribe_args) do |on|
on.subscribe do |channel, subscriptions|
puts "Subscribed to ##{channel} (subscriptions: #{subscriptions})"
end
on.message do |channel, message|
payload = JSON[message]
next_shuffle_block_num = payload['next_shuffle_block_num']
current_shuffled_witnesses = payload['current_shuffled_witnesses']
num_witnesses = current_shuffled_witnesses.size
from_block_num = next_shuffle_block_num - num_witnesses + 1
to_block_num = from_block_num + num_witnesses - 1
block_range = from_block_num..to_block_num # typically 21 blocks
if !!max_blocks
if block_range.include? until_block_num
# We're done trailing blocks. Typically, this is used by unit
# tests so the test can halt.
subscription.unsubscribe
end
end
begin
# We write witnesses to this hash until all 21 produce blocks.
actual_witnesses = {}
tries = 0
while actual_witnesses.size != num_witnesses
# Allow the immediate node to catch up in case it's behind by a
# block.
sleep 3
# Typically, nodes will allow up to 50 block headers in one
# request, if backed by jussi. We only need 21, so each
# request should only make a single response with the entire
# round. Under normal circumstances, this call happens only
# once. But if the there's additional p2p or cache latency,
# it might have missing headers.
block_api.get_block_headers(block_range: block_range) do |header, block_num|
unless !!header
# Can happen when there's excess p2p latency and/or jussi
# cache is under load.
puts "Waiting for block header (witness:schedule): #{block_num}"
node_url = Meeseeker.shuffle_node_url(chain)
block_api = Meeseeker.block_api_class(chain).new(url: node_url)
next
end
actual_witnesses[header.witness] = block_num
end
break if (tries += 1) > 5
end
# If there are multiple tries due to high p2p latency, even though
# we got all 21 block headers, seeing this message could be an
# early-warning of other problems on the blockchain.
# If there's a missing block header, this will always show 5
# tries.
puts "Tries: #{tries}" if tries > 1
missing_witnesses = current_shuffled_witnesses - actual_witnesses.keys
extra_witnesses = actual_witnesses.keys - current_shuffled_witnesses
if missing_witnesses.any? || extra_witnesses.any?
puts "Expected only these witness to produce a block in #{block_range}."
puts "Missing witnesses: #{missing_witnesses.join(', ')}"
puts "Extra witnesses: #{extra_witnesses.join(', ')}"
puts "\nWitnesses and block numbers in range:"
actual_witnesses.sort_by{ |k, v| v }.each do |k, v|
puts "#{v}: #{k}"
end
puts "Count: #{actual_witnesses.size}"
# Non-zero exit to notify the shell caller that there's a
# problem.
exit(-(missing_witnesses.size + extra_witnesses.size))
end
end
# Perfect round.
puts "Found all #{num_witnesses} expected witnesses in block range #{block_range}: √"
end
on.unsubscribe do |channel, subscriptions|
puts "Unsubscribed from ##{channel} (subscriptions: #{subscriptions})"
end
end
end
end
end
end