Class: Zold::Entrance

Inherits:
Object
  • Object
show all
Defined in:
lib/zold/node/entrance.rb

Overview

The entrance

Instance Method Summary collapse

Constructor Details

#initialize(wallets, remotes, copies, address, log: Log::Quiet.new) ⇒ Entrance

Returns a new instance of Entrance.



40
41
42
43
44
45
46
47
# File 'lib/zold/node/entrance.rb', line 40

def initialize(wallets, remotes, copies, address, log: Log::Quiet.new)
  @wallets = wallets
  @remotes = remotes
  @copies = copies
  @address = address
  @log = log
  @semaphores = Concurrent::Map.new
end

Instance Method Details

#check(body) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/zold/node/entrance.rb', line 57

def check(body)
  Tempfile.open do |f|
    File.write(f.path, body)
    wallet = Wallet.new(f)
    break unless wallet.network == Wallet::MAIN_NETWORK
    balance = wallet.balance
    raise "The balance #{balance} is negative and it's not a root wallet" if balance.negative? && !wallet.root?
    Emission.new(wallet).check
    tax = Tax.new(wallet)
    if tax.in_debt?
      raise "Taxes are not paid, can't accept the wallet; the debt is #{tax.debt} (#{tax.debt.to_i} zents)"
    end
  end
end

#push(id, body) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/zold/node/entrance.rb', line 49

def push(id, body)
  check(body)
  @semaphores.put_if_absent(id, Mutex.new)
  @semaphores.get(id).synchronize do
    push_unsafe(id, body)
  end
end

#push_unsafe(id, body) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/zold/node/entrance.rb', line 72

def push_unsafe(id, body)
  copies = Copies.new(File.join(@copies, id.to_s))
  copies.add(body, 'remote', Remotes::PORT, 0)
  Fetch.new(
    wallets: @wallets, remotes: @remotes, copies: copies.root, log: @log
  ).run(['fetch', id.to_s, "--ignore-node=#{@address}"])
  modified = Merge.new(
    wallets: @wallets, copies: copies.root, log: @log
  ).run(['merge', id.to_s])
  copies.remove('remote', Remotes::PORT)
  Push.new(
    wallets: @wallets, remotes: @remotes, log: @log
  ).run(['push'] + modified.map(&:to_s))
  Clean.new(copies: copies.root, log: @log).run(['clean', id.to_s])
  modified
end