Last active
June 12, 2019 20:21
-
-
Save shal/f5d09a6f877b2cf5e9d45d7b08b679a0 to your computer and use it in GitHub Desktop.
Debugger for cryptonodes
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
#!/usr/bin/env ruby | |
# Author: Ali Shanaakh <hi@shal.dev> | |
# Usage: debug.rb [options] | |
# -c, --client=CLIENT | |
# -a, --address=ADDRESS | |
# -n, --amount=AMOUNT | |
# -v, --verbose | |
require 'net/http' | |
require 'json' | |
require 'optparse' | |
module Debugger | |
AMOUNT = 1000 | |
class Base | |
def initialize(attrs) | |
@address = attrs[:address] | |
@amount = attrs[:amount].to_i || AMOUNT | |
@verbose = attrs[:verbose] || false | |
@codes = {} | |
end | |
def headers | |
{ | |
'Content-Type': 'application/json' | |
} | |
end | |
def debug! | |
@amount.times do | |
uri = URI(@address) | |
request = Net::HTTP::Post.new(uri).tap do |req| | |
req.body = payload.to_json | |
req['Content-Type'] = 'application/json' | |
req.basic_auth(uri.user, uri.password) | |
end | |
res = Net::HTTP.start(uri.hostname, uri.port) { |http| http.request(request) } | |
if res.kind_of?(Net::HTTPSuccess) && @verbose | |
puts "OK: #{height(res.body)}" | |
elsif !res.kind_of?(Net::HTTPSuccess) && @verbose | |
puts "Got an error: #{res.code.to_s}" | |
end | |
@codes[res.code] = @codes[res.code].to_i + 1 | |
end | |
report | |
end | |
def report | |
puts "Benchmark: #{@codes}" | |
end | |
def height(attrs) | |
JSON.parse(attrs)['result'] | |
end | |
end | |
# Implementation for Ethereum. | |
class Ethereum < Base | |
def payload | |
{ | |
jsonrpc: '2.0', | |
method: 'eth_blockNumber', | |
params: [], | |
id: 1 | |
} | |
end | |
def height(attrs) | |
super.hex | |
end | |
end | |
# Implementation for Bitcoin. | |
class Bitcoin < Base | |
def payload | |
{ | |
jsonrpc: '1.0', | |
method: 'getblockcount', | |
params: [], | |
id: 'curltest' | |
} | |
end | |
end | |
# Alias for Litecoin. | |
Litecoin = Bitcoin | |
end | |
options = {} | |
option_parser = OptionParser.new do |opts| | |
opts.banner = 'Usage: debug.rb [options]' | |
opts.on '-c', '--client=CLIENT', String | |
opts.on '-a', '--address=ADDRESS', String | |
opts.on '-n', '--amount=AMOUNT', Integer | |
opts.on '-v', '--verbose' | |
end | |
option_parser.parse!(into: options) | |
if options[:client].nil? || !(options[:address] =~ URI::regexp) | |
puts option_parser | |
else | |
eval("Debugger::#{options[:client].capitalize}").new(options).debug! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment