forked from steemit/steem-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
332 lines (286 loc) · 10.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
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
require 'bundler/gem_tasks'
require 'rake/testtask'
require 'yard'
require 'dpay'
Rake::TestTask.new(test: ['clean:vcr', 'test:threads']) 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
namespace :test do
Rake::TestTask.new(static: 'clean:vcr') do |t|
t.description = <<-EOD
Run static tests, which are those that have static request/responses.
These are tests that are typically read-only and do not require heavy
matches on the json-rpc request body. Often, the only difference between
one execution and another is the json-rpc-id.
EOD
t.libs << 'test'
t.libs << 'lib'
t.test_files = [
'test/dpay/account_by_key_api_test.rb',
'test/dpay/account_history_api_test.rb',
'test/dpay/block_api_test.rb',
'test/dpay/database_api_test.rb',
'test/dpay/follow_api_test.rb',
'test/dpay/jsonrpc_test.rb',
'test/dpay/market_history_api_test.rb',
'test/dpay/tags_api_test.rb',
'test/dpay/witness_api_test.rb'
]
t.ruby_opts << if ENV['HELL_ENABLED']
'-W2'
else
'-W1'
end
end
Rake::TestTask.new(broadcast: 'clean:vcr') do |t|
t.description = <<-EOD
Run broadcast tests, which are those that only use network_broadcast_api
and/or database_api.verify_authority (pretend: true).
EOD
t.libs << 'test'
t.libs << 'lib'
t.test_files = [
'test/dpay/broadcast_test.rb',
'test/dpay/transaction_builder_test.rb'
]
t.ruby_opts << if ENV['HELL_ENABLED']
'-W2'
else
'-W1'
end
end
Rake::TestTask.new(testnet: 'clean:vcr') do |t|
t.description = <<-EOD
Run testnet tests, which are those that use network_broadcast_api to do
actual broadcast operations, on a specified (or default) testnet.
EOD
t.libs << 'test'
t.libs << 'lib'
t.test_files = [
'test/dpay/testnet_test.rb'
]
t.ruby_opts << if ENV['HELL_ENABLED']
'-W2'
else
'-W1'
end
end
desc 'Tests the API using multiple threads.'
task :threads do
next if !!ENV['TEST']
threads = []
api = DPay::Api.new(url: ENV['TEST_NODE'])
database_api = DPay::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
bbd_exchange_rate = witness[:bbd_exchange_rate]
base = bbd_exchange_rate[:base].to_f
if (quote = bbd_exchange_rate[:quote].to_f) > 0
rate = (base / quote).round(3)
witnesses[witness.owner][:bbd_exchange_rate] = rate
else
witnesses[witness.owner][:bbd_exchange_rate] = nil
end
last_bbd_exchange_update = witness[:last_bbd_exchange_update]
last_bbd_exchange_update = Time.parse(last_bbd_exchange_update + 'Z')
last_bbd_exchange_elapsed = '%.2f hours ago' % ((Time.now.utc - last_bbd_exchange_update) / 60)
witnesses[witness.owner][:last_bbd_exchange_elapsed] = last_bbd_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 = DPay::Stream.new(url: ENV['TEST_NODE'], mode: mode)
api = DPay::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 = DPay::Stream.new(url: ENV['TEST_NODE'], mode: mode)
api = DPay::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 = DPay::Stream.new(url: ENV['TEST_NODE'], mode: mode)
api = DPay::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 = DPay::Stream.new(url: ENV['TEST_NODE'], mode: mode)
api = DPay::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 = DPay::Stream.new(url: ENV['TEST_NODE'], mode: mode)
api = DPay::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
YARD::Rake::YardocTask.new do |t|
t.files = ['lib/**/*.rb']
end
task default: :test
desc 'Ruby console with dpay already required.'
task :console do
exec 'irb -r dpay -I ./lib'
end
namespace :clean do
desc 'Remove test/fixtures/vcr_cassettes/*.yml so they can be rebuilt fresh.'
task :vcr do |t|
cmd = 'echo Cleaned cassettes: $(rm -v test/fixtures/vcr_cassettes/*.yml | wc -l)'
system cmd
end
end
namespace :show do
desc 'Shows known API names.'
task :apis do
url = ENV['URL']
jsonrpc = DPay::Jsonrpc.new(url: url)
api_methods = jsonrpc.get_api_methods
puts api_methods.keys
end
desc 'Shows known method names for specified API.'
task :methods, [:api] do |t, args|
url = ENV['URL']
jsonrpc = DPay::Jsonrpc.new(url: url)
api_methods = jsonrpc.get_api_methods
api_methods[args[:api]].each do |method|
jsonrpc.get_signature(method: "#{args[:api]}.#{method}") do |signature|
print "#{method} "
params = signature.args.map do |k, v|
if v =~ /\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1])T(2[0-3]|[01]\d):[0-5]\d:[0-5]\d/
"#{k}: Time"
elsif v.class == Hashie::Array
"#{k}: []"
elsif v.class == Hashie::Mash
"#{k}: {}"
else
"#{k}: #{v.class}"
end
end
puts params.join(', ')
end
end
end
end