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.



37
38
39
40
41
42
43
44
# File 'lib/zold/node/entrance.rb', line 37

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

#push(id, body) ⇒ Object



46
47
48
49
50
51
# File 'lib/zold/node/entrance.rb', line 46

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

#push_unsafe(id, body) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/zold/node/entrance.rb', line 53

def push_unsafe(id, body)
  copies = Copies.new(File.join(@copies, id.to_s))
  copies.add(body, 'remote', Remotes::PORT, 0)
  Fetch.new(
    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])
  debt = Tax.new(@wallets.find(id)).debt
  if debt > Tax::TRIAL
    raise "Taxes are not paid, the debt is #{debt} (#{debt.to_i} zents), won't promote the wallet"
  end
  copies.remove('remote', Remotes::PORT)
  modified.each do |m|
    Push.new(
      wallets: @wallets, remotes: @remotes, log: @log
    ).run(['push', m.to_s])
  end
  modified
end