forked from steemit/steem-mechanize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
211 lines (173 loc) · 7.5 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
require 'bundler/gem_tasks'
require 'steem-mechanize'
task default: 'test:threads'
desc 'Ruby console with steem-mechanize already required.'
task :console do
exec 'irb -r steem-mechanize -I ./lib'
end
namespace :test do
desc 'Tests the mechanized API using multiple threads.'
task :threads do
next if !!ENV['TEST']
threads = []
api = Steem::Api.new(url: ENV['TEST_NODE'])
database_api = Steem::DatabaseApi.new(url: ENV['TEST_NODE'])
witnesses = {}
keys = %i(created url total_missed props running_version
hardfork_version_vote hardfork_time_vote)
if defined? Thread.report_on_exception
Thread.report_on_exception = true
end
database_api.get_active_witnesses do |result|
print "Found #{result.witnesses.size} witnesses ..."
result.witnesses.each do |witness_name|
threads << Thread.new do
api.get_witness_by_account(witness_name) do |witness|
witnesses[witness.owner] = witness.map do |k, v|
[k, v] if keys.include? k.to_sym
end.compact.to_h
sbd_exchange_rate = witness[:sbd_exchange_rate]
base = sbd_exchange_rate[:base].to_f
if (quote = sbd_exchange_rate[:quote].to_f) > 0
rate = (base / quote).round(3)
witnesses[witness.owner][:sbd_exchange_rate] = rate
else
witnesses[witness.owner][:sbd_exchange_rate] = nil
end
last_sbd_exchange_update = witness[:last_sbd_exchange_update]
last_sbd_exchange_update = Time.parse(last_sbd_exchange_update + 'Z')
last_sbd_exchange_elapsed = '%.2f hours ago' % ((Time.now.utc - last_sbd_exchange_update) / 60)
witnesses[witness.owner][:last_sbd_exchange_elapsed] = last_sbd_exchange_elapsed
end
end
end
end
threads.each do |thread|
print '.'
thread.join
end
puts ' done!'
if threads.size != witnesses.size
puts "Bug: expected #{threads.size} witnesses, only found #{witnesses.size}."
else
puts JSON.pretty_generate witnesses rescue puts witnesses
end
end
end
namespace :stream do
desc 'Test the ability to stream a block range.'
task :block_range, [:mode, :at_block_num] do |t, args|
mode = (args[:mode] || 'irreversible').to_sym
first_block_num = args[:at_block_num].to_i if !!args[:at_block_num]
stream = Steem::Stream.new(url: ENV['TEST_NODE'], mode: mode)
api = Steem::Api.new(url: ENV['TEST_NODE'])
last_block_num = nil
last_timestamp = nil
range_complete = false
api.get_dynamic_global_properties do |properties|
current_block_num = if mode == :head
properties.head_block_number
else
properties.last_irreversible_block_num
end
# First pass replays latest a random number of blocks to test chunking.
first_block_num ||= current_block_num - (rand * 200).to_i
range = first_block_num..current_block_num
puts "Initial block range: #{range.size}"
stream.blocks(at_block_num: range.first) do |block, block_num|
current_timestamp = Time.parse(block.timestamp + 'Z')
if !range_complete && block_num > range.last
puts 'Done with initial range.'
range_complete = true
end
if !!last_timestamp && block_num != last_block_num + 1
puts "Bug: Last block number was #{last_block_num} then jumped to: #{block_num}"
exit
end
if !!last_timestamp && current_timestamp < last_timestamp
puts "Bug: Went back in time. Last timestamp was #{last_timestamp}, then jumped back to #{current_timestamp}"
exit
end
puts "\t#{block_num} Timestamp: #{current_timestamp}, witness: #{block.witness}"
last_block_num = block_num
last_timestamp = current_timestamp
end
end
end
desc 'Test the ability to stream a block range of transactions.'
task :trx_range, [:mode, :at_block_num] do |t, args|
mode = (args[:mode] || 'irreversible').to_sym
first_block_num = args[:at_block_num].to_i if !!args[:at_block_num]
stream = Steem::Stream.new(url: ENV['TEST_NODE'], mode: mode)
api = Steem::Api.new(url: ENV['TEST_NODE'])
api.get_dynamic_global_properties do |properties|
current_block_num = if mode == :head
properties.head_block_number
else
properties.last_irreversible_block_num
end
# First pass replays latest a random number of blocks to test chunking.
first_block_num ||= current_block_num - (rand * 200).to_i
stream.transactions(at_block_num: first_block_num) do |trx, trx_id, block_num|
puts "#{block_num} :: #{trx_id}; ops: #{trx.operations.map(&:type).join(', ')}"
end
end
end
desc 'Test the ability to stream a block range of operations.'
task :op_range, [:mode, :at_block_num] do |t, args|
mode = (args[:mode] || 'irreversible').to_sym
first_block_num = args[:at_block_num].to_i if !!args[:at_block_num]
stream = Steem::Stream.new(url: ENV['TEST_NODE'], mode: mode)
api = Steem::Api.new(url: ENV['TEST_NODE'])
api.get_dynamic_global_properties do |properties|
current_block_num = if mode == :head
properties.head_block_number
else
properties.last_irreversible_block_num
end
# First pass replays latest a random number of blocks to test chunking.
first_block_num ||= current_block_num - (rand * 200).to_i
stream.operations(at_block_num: first_block_num) do |op, trx_id, block_num|
puts "#{block_num} :: #{trx_id}; op: #{op.type}"
end
end
end
desc 'Test the ability to stream a block range of virtual operations.'
task :vop_range, [:mode, :at_block_num] do |t, args|
mode = (args[:mode] || 'irreversible').to_sym
first_block_num = args[:at_block_num].to_i if !!args[:at_block_num]
stream = Steem::Stream.new(url: ENV['TEST_NODE'], mode: mode)
api = Steem::Api.new(url: ENV['TEST_NODE'])
api.get_dynamic_global_properties do |properties|
current_block_num = if mode == :head
properties.head_block_number
else
properties.last_irreversible_block_num
end
# First pass replays latest a random number of blocks to test chunking.
first_block_num ||= current_block_num - (rand * 200).to_i
stream.operations(at_block_num: first_block_num, only_virtual: true) do |op, trx_id, block_num|
puts "#{block_num} :: #{trx_id}; op: #{op.type}"
end
end
end
desc 'Test the ability to stream a block range of all operations (including virtual).'
task :all_op_range, [:mode, :at_block_num] do |t, args|
mode = (args[:mode] || 'irreversible').to_sym
first_block_num = args[:at_block_num].to_i if !!args[:at_block_num]
stream = Steem::Stream.new(url: ENV['TEST_NODE'], mode: mode)
api = Steem::Api.new(url: ENV['TEST_NODE'])
api.get_dynamic_global_properties do |properties|
current_block_num = if mode == :head
properties.head_block_number
else
properties.last_irreversible_block_num
end
# First pass replays latest a random number of blocks to test chunking.
first_block_num ||= current_block_num - (rand * 200).to_i
stream.operations(at_block_num: first_block_num, include_virtual: true) do |op, trx_id, block_num|
puts "#{block_num} :: #{trx_id}; op: #{op.type}"
end
end
end
end