-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added
steem:witness:schedule
channel
- Loading branch information
1 parent
4b4312b
commit 9999b7b
Showing
6 changed files
with
265 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
module Meeseeker | ||
class WitnessScheduleJob | ||
def perform(options = {}) | ||
database_api = Steem::DatabaseApi.new(url: Meeseeker.node_url) | ||
redis = Meeseeker.redis | ||
mode = options.delete(:mode) || Meeseeker.stream_mode | ||
schedule = nil | ||
last_shuffle_block_num = nil | ||
|
||
loop do | ||
# Using hammer assignment will ensure we only request a new schedule | ||
# after we've published. | ||
|
||
schedule ||= catch :witness_schedule do | ||
database_api.get_witness_schedule do |result| | ||
throw :witness_schedule if result.nil? | ||
|
||
result | ||
end | ||
end | ||
|
||
next_shuffle_block_num = schedule.next_shuffle_block_num | ||
block_num = catch :dynamic_global_properties do | ||
database_api.get_dynamic_global_properties do |dgpo| | ||
throw :dynamic_global_properties if dgpo.nil? | ||
|
||
case mode | ||
when :head then dgpo.head_block_number | ||
when :irreversible then dgpo.last_irreversible_block_num | ||
else; abort "Unknown stream mode: #{mode}" | ||
end | ||
end | ||
end | ||
|
||
# Find out how far away we are from the next schedule. | ||
|
||
remaining_blocks = [next_shuffle_block_num - block_num - 1.5, 0].max | ||
|
||
# It's better for the schedule to publish a little late than to miss | ||
# an entire schedule, so we subtract 1.5 blocks from the total. | ||
# Sometimes we check a little early and sometimes we check a little | ||
# late. But it all averages out. | ||
|
||
if remaining_blocks > 0 | ||
delay = [remaining_blocks * 3.0, 0.25].max | ||
puts "Sleeping for #{delay} seconds (remaining blocks: #{remaining_blocks})." | ||
sleep delay | ||
next | ||
end | ||
|
||
# Now that we've reached the current schedule, check if we've published | ||
# it already. If not, publish and reset for the next schedule. | ||
|
||
if next_shuffle_block_num != last_shuffle_block_num | ||
puts "next_shuffle_block_num: #{next_shuffle_block_num}; current_shuffled_witnesses: #{schedule.current_shuffled_witnesses.join(', ')}" | ||
redis.publish('steem:witness:schedule', schedule.to_json) | ||
last_shuffle_block_num = next_shuffle_block_num | ||
end | ||
|
||
schedule = nil # re-enabled hammer assignment | ||
|
||
if !!options[:until_block_num] | ||
break if block_num >= options[:until_block_num].to_i | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters