Skip to content

Commit

Permalink
#1 vlog
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Nov 8, 2018
1 parent 2e054f3 commit 208d315
Show file tree
Hide file tree
Showing 12 changed files with 244 additions and 223 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ coverage/
rdoc/
Gemfile.lock
.zoldata/
home/
54 changes: 30 additions & 24 deletions bin/zold-stress
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ require 'zold/sync_wallets'
require 'zold/cached_wallets'
require 'zold/remotes'
require_relative '../lib/zold/stress/round'
require_relative '../lib/zold/stress/stats'
require_relative '../lib/zold/stress/air'

Thread.current.name = 'main'

Expand All @@ -47,17 +49,22 @@ begin
opts = Slop.parse(ARGV, strict: false, suppress_errors: true) do |o|
o.banner = "Usage: zold-stress [options]
Available options:"
o.string '--wallet',
'Wallet ID where we start from',
required: true
o.integer '-r', '--rounds',
'Total amount of paying rounds to complete (default: 16)',
default: 16
o.integer '-p', '--pool',
'From how many wallets to send payments (default: 8)',
default: 8
o.integer '-t', '--threads',
"How many threads to use for each operation (default: #{Concurrent.processor_count * 8})",
default: Concurrent.processor_count * 8
o.integer '-b', '--batch',
"How many transactions to send in each round (default: 64)",
default: 64
o.string '--private-key',
'The location of RSA private key (default: ~/.ssh/id_rsa)',
require: true,
default: File.expand_path('~/.ssh/id_rsa')
o.string '--public-key',
'The location of RSA public key (default: ~/.ssh/id_rsa.pub)',
require: true,
default: File.expand_path('~/.ssh/id_rsa.pub')
o.string '--home',
"Home directory (default: #{Dir.pwd})",
default: Dir.pwd
Expand All @@ -69,9 +76,6 @@ Available options:"
o.on '--no-colors', 'Disable colors in the ouput' do
Rainbow.enabled = false
end
o.on '--verbose', 'Enable extra logging information' do
log = Zold::Log::Verbose.new
end
end

log = Zold::Log::Sync.new(log)
Expand All @@ -84,7 +88,6 @@ Available options:"
home = File.expand_path(opts[:home])
FileUtils.mkdir_p(home)
Dir.chdir(home)
log.debug("Home directory: #{home}")

zoldata = File.join(home, '.zoldata')

Expand All @@ -96,21 +99,24 @@ Available options:"
remotes.defaults
copies = File.join(zoldata, 'copies')

log.debug("Network: #{opts['network']} (#{opts['network'] == Zold::Wallet::MAIN_NETWORK ? 'main' : 'test'} net)")

Zold::Stress::Round.new(
id: Zold::Id.new(opts['wallet']),
pub: Zold::Key.new(file: opts['public-key']),
stats = Zold::Stress::Stats.new
air = Zold::Stress::Air.new
round = Zold::Stress::Round.new(
pvt: Zold::Key.new(file: opts['private-key']),
wallets: wallets,
remotes: remotes,
copies: copies,
log: log
).run
wallets: wallets, remotes: remotes, copies: copies,
stats: stats, air: air, log: log, opts: opts
)
opts['rounds'].times do |r|
round.update
round.prepare
# round.send
# round.pull
# round.match
log.info(stats.to_console)
end
rescue StandardError => ex
log.error("#{ex.message} (#{ex.class.name})")
puts(ex.backtrace)
log.error(Backtrace.new(ex))
exit(-1)
end

log.debug("Successfully finished in #{Zold::Age.new(start)}")
log.info("Successfully finished in #{Zold::Age.new(start)}")
6 changes: 0 additions & 6 deletions lib/zold/stress/air.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ def initialize
@all = []
end

def to_json
{
'total': @all.count
}
end

def fetch
@all
end
Expand Down
35 changes: 22 additions & 13 deletions lib/zold/stress/pmnts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@
module Zold::Stress
# Payments to send in a batch.
class Pmnts
def initialize(pvt:, wallets:, remotes:, stats:, log: Zold::Log::Quiet.new)
raise 'Private RSA key can\'t be nil' if pvt.nil?
raise 'Private RSA key must be of type Key' unless pvt.is_a?(Zold::Key)
def initialize(pvt:, wallets:, remotes:, stats:, opts:,
vlog: Zold::Log::Quiet.new, log: Zold::Log::Quiet.new)
@pvt = pvt
raise 'Wallets can\'t be nil' if wallets.nil?
@wallets = wallets
@remotes = remotes
raise 'Log can\'t be nil' if log.nil?
@log = log
@vlog = vlog
@opts = opts
@stats = stats
end

Expand All @@ -51,15 +50,19 @@ def send
paid = []
Tempfile.open do |f|
File.write(f, @pvt.to_s)
@wallets.all.each do |source|
loop do
source = @wallets.all.sample
balance = @wallets.find(source, &:balance)
next if balance.negative? || balance.zero?
amount = balance / @wallets.all.count
next if amount < Zold::Amount.new(zld: 0.0001)
@wallets.all.each do |target|
loop do
target = @wallets.all.sample
next if source == target
paid << pay_one(source, target, amount, f.path)
break
end
break if paid.count >= @opts['batch']
end
end
paid
Expand All @@ -68,20 +71,26 @@ def send
private

def pay_one(source, target, amount, pvt)
Zold::Taxes.new(wallets: @wallets, remotes: @remotes, log: @log).run(
['taxes', 'pay', source.to_s, "--network=#{@network}", "--private-key=#{pvt}", '--ignore-nodes-absence']
Zold::Taxes.new(wallets: @wallets, remotes: @remotes, log: @vlog).run(
[
'taxes', 'pay', source.to_s, "--network=#{@opts['network']}",
"--private-key=#{pvt}", '--ignore-nodes-absence'
]
)
if @wallets.find(source) { |w| Zold::Tax.new(w).in_debt? }
@log.error("The wallet #{source} is still in debt and we can't pay taxes")
@log.error("The wallet #{source} is in debt and we can't pay taxes")
return
end
details = SecureRandom.uuid
@stats.exec('paid', swallow: false) do
Zold::Pay.new(wallets: @wallets, remotes: @remotes, log: @log).run(
['pay', source.to_s, target.to_s, amount.to_zld(4), details, "--network=#{@network}", "--private-key=#{pvt}"]
Zold::Pay.new(wallets: @wallets, remotes: @remotes, log: @vlog).run(
[
'pay', source.to_s, target.to_s, amount.to_zld(6), details,
"--network=#{@opts['network']}", "--private-key=#{pvt}"
]
)
end
{ start: Time.now, source: source, target: target, details: details }
{ start: Time.now, source: source, target: target, amount: amount, details: details }
end
end
end
47 changes: 19 additions & 28 deletions lib/zold/stress/pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,46 +37,37 @@
module Zold::Stress
# Pool of wallets.
class Pool
def initialize(id:, pub:, wallets:, remotes:, copies:, stats:, log: Zold::Log::Quiet.new)
@id = id
@pub = pub
def initialize(wallets:, remotes:, copies:, stats:, opts:,
log: Zold::Log::Quiet.new, vlog: Zold::Log::Quiet.new)
@wallets = wallets
@remotes = remotes
@copies = copies
@log = log
@vlog = vlog
@opts = opts
@stats = stats
end

def rebuild(size, opts = [])
candidates = [@id]
@wallets.all.each do |id|
@wallets.find(id, &:txns).each do |t|
next unless t.amount.negative?
candidates << t.bnf
end
end
candidates.uniq.shuffle.each do |id|
@wallets.all.each do |w|
next if @wallets.find(w, &:balance) > Zold::Amount.new(zld: 0.01)
Zold::Remove.new(wallets: @wallets, log: @log).run(
['remove', w.to_s]
)
end
break if @wallets.all.count > size
@stats.exec('pull') do
Zold::Pull.new(wallets: @wallets, remotes: @remotes, copies: @copies, log: @log).run(
['pull', id.to_s] + opts
)
end
def rebuild
raise "There are no wallets in the pool at #{@wallets.path}, at least one is needed" if @wallets.all.empty?
balances = @wallets.all
.map { |id| {id: id, balance: @wallets.find(id, &:balance)} }
.sort_by { |h| h[:balance] }
.reverse
balances.last([balances.count - @opts['pool'], 0].max).each do |h|
Zold::Remove.new(wallets: @wallets, log: @vlog).run(
['remove', h[:id].to_s]
)
end
Tempfile.open do |f|
File.write(f, @pub.to_s)
while @wallets.all.count < size
Zold::Create.new(wallets: @wallets, log: @log).run(
['create', "--public-key=#{f.path}"] + opts
File.write(f, @wallets.find(balances[0][:id], &:key).to_s)
while @wallets.all.count < @opts['pool']
Zold::Create.new(wallets: @wallets, log: @vlog).run(
['create', "--public-key=#{f.path}", "--network=#{@opts['network']}"] + @opts.arguments
)
end
end
raise "There is no money in the pool of #{balances.count} wallets at #{@wallets.path}" if balances[0][:balance].zero?
end
end
end
Loading

0 comments on commit 208d315

Please sign in to comment.