class Zold::Pipeline

The pipeline

Public Class Methods

new(remotes, copies, address, ledger: '/dev/null', network: 'test') click to toggle source
# File lib/zold/node/pipeline.rb, line 42
def initialize(remotes, copies, address, ledger: '/dev/null', network: 'test')
  @remotes = remotes
  @copies = copies
  @address = address
  @network = network
  @history = []
  @speed = []
  @mutex = Mutex.new
  @ledger = ledger
end

Public Instance Methods

push(id, body, wallets, log) click to toggle source

Returns a list of modifed wallets (as Zold::Id)

# File lib/zold/node/pipeline.rb, line 61
def push(id, body, wallets, log)
  start = Time.now
  copies = Copies.new(File.join(@copies, id.to_s))
  host = '0.0.0.0'
  copies.add(body, host, Remotes::PORT, 0)
  unless @remotes.all.empty?
    Fetch.new(
      wallets: wallets, remotes: @remotes, copies: copies.root, log: log
    ).run(['fetch', id.to_s, "--ignore-node=#{@address}", "--network=#{@network}", '--quiet-if-absent'])
  end
  modified = merge(id, copies, wallets, log)
  Clean.new(wallets: wallets, copies: copies.root, log: log).run(
    ['clean', id.to_s, '--max-age=1']
  )
  copies.remove(host, Remotes::PORT)
  if modified.empty?
    log.info("Accepted #{id} in #{Age.new(start, limit: 1)} and not modified anything")
  else
    log.info("Accepted #{id} in #{Age.new(start, limit: 1)} and modified #{modified.join(', ')}")
  end
  modified << id if copies.all.count > 1
  modified
end
to_json() click to toggle source

Show its internals.

# File lib/zold/node/pipeline.rb, line 54
def to_json
  {
    'ledger': File.exist?(@ledger) ? IO.read(@ledger).split("\n").count : 0
  }
end

Private Instance Methods

merge(id, copies, wallets, log) click to toggle source
# File lib/zold/node/pipeline.rb, line 87
def merge(id, copies, wallets, log)
  Tempfile.open do |f|
    modified = Tempfile.open do |t|
      host, port = @address.split(':')
      Merge.new(wallets: wallets, remotes: @remotes, copies: copies.root, log: log).run(
        ['merge', id.to_s, "--ledger=#{Shellwords.escape(f.path)}"] +
        ["--trusted=#{Shellwords.escape(t.path)}"] +
        ["--network=#{Shellwords.escape(@network)}"] +
        (@remotes.master?(host, port.to_i) ? ['--no-baseline', '--depth=4'] : [])
      )
    end
    @mutex.synchronize do
      txns = File.exist?(@ledger) ? IO.read(@ledger).strip.split("\n") : []
      txns += IO.read(f.path).strip.split("\n")
      IO.write(
        @ledger,
        txns.map { |t| t.split(';') }
          .uniq { |t| "#{t[1]}-#{t[3]}" }
          .reject { |t| Txn.parse_time(t[0]) < Time.now - 24 * 60 * 60 }
          .map { |t| t.join(';') }
          .join("\n")
          .strip
      )
    end
    modified
  end
end