Class: Zold::Propagate

Inherits:
Object
  • Object
show all
Defined in:
lib/zold/commands/propagate.rb

Overview

PROPAGATE pulling command

Instance Method Summary collapse

Constructor Details

#initialize(wallets:, log: Log::Quiet.new) ⇒ Propagate

Returns a new instance of Propagate.



36
37
38
39
# File 'lib/zold/commands/propagate.rb', line 36

def initialize(wallets:, log: Log::Quiet.new)
  @wallets = wallets
  @log = log
end

Instance Method Details

#propagate(wallet, _) ⇒ Object

Returns list of Wallet IDs which were affected



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
# File 'lib/zold/commands/propagate.rb', line 58

def propagate(wallet, _)
  me = wallet.id
  modified = []
  wallet.txns.select { |t| t.amount.negative? }.each do |t|
    target = @wallets.find(t.bnf)
    unless target.exists?
      @log.debug("#{t.amount * -1} to #{t.bnf}: wallet is absent")
      next
    end
    unless target.network == wallet.network
      @log.error("#{t.amount * -1} to #{t.bnf}: network mismatch, '#{target.network}'!='#{wallet.network}'")
      next
    end
    next if target.has?(t.id, me)
    unless Prefixes.new(target).valid?(t.prefix)
      @log.error("#{t.amount * -1} to #{t.bnf}: wrong prefix")
      next
    end
    target.add(t.inverse(me))
    @log.info("#{t.amount * -1} arrived to #{t.bnf}: #{t.details}")
    modified << t.bnf
  end
  modified.uniq!
  @log.debug("Wallet #{me} propagated successfully, #{modified.count} wallets affected")
  modified
end

#run(args = []) ⇒ Object

Returns list of Wallet IDs which were affected



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/zold/commands/propagate.rb', line 42

def run(args = [])
  opts = Slop.parse(args, help: true, suppress_errors: true) do |o|
    o.banner = "Usage: zold propagate [ID...] [options]
Available options:"
    o.bool '--help', 'Print instructions'
  end
  mine = Args.new(opts, @log).take || return
  mine = @wallets.all if mine.empty?
  modified = []
  mine.each do |id|
    modified += propagate(@wallets.find(id), opts)
  end
  modified
end