class Zold::Stress::Pmnts

Payments to send in a batch.

Public Class Methods

new(pvt:, wallets:, remotes:, stats:, opts:, vlog: Zold::Log::NULL, log: Zold::Log::NULL) click to toggle source
# File lib/zold/stress/pmnts.rb, line 38
def initialize(pvt:, wallets:, remotes:, stats:, opts:,
  vlog: Zold::Log::NULL, log: Zold::Log::NULL)
  @pvt = pvt
  @wallets = wallets
  @remotes = remotes
  @log = log
  @vlog = vlog
  @opts = opts
  @stats = stats
end

Public Instance Methods

send() click to toggle source
# File lib/zold/stress/pmnts.rb, line 49
def send
  raise 'Too few wallets in the pool' if @wallets.all.count < 2
  paid = []
  all = @wallets.all
  Tempfile.open do |f|
    File.write(f, @pvt.to_s)
    loop do
      source = all.sample
      balance = @wallets.acq(source, &:balance)
      next if balance.negative? || balance.zero?
      amount = balance / all.count
      next if amount < Zold::Amount.new(zld: 0.0001)
      loop do
        target = 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
end

Private Instance Methods

pay_one(source, target, amount, pvt) click to toggle source
# File lib/zold/stress/pmnts.rb, line 75
def pay_one(source, target, amount, pvt)
  Zold::Taxes.new(wallets: @wallets, remotes: @remotes, log: @vlog).run(
    [
      'taxes', 'pay', source.to_s, "--network=#{@opts['network']}",
      "--private-key=#{pvt}", '--ignore-nodes-absence', '--skip-propagate'
    ]
  )
  if @wallets.acq(source) { |w| Zold::Tax.new(w).in_debt? }
    @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: @vlog, copies: nil).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, amount: amount, details: details }
end