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
48
49
50
51
# 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
  @push_mutex = Mutex.new
  @modified = Set.new
  @pool = Concurrent::FixedThreadPool.new(16, max_queue: 64, fallback_policy: :abort)
  @pushes = Concurrent::FixedThreadPool.new(1, max_queue: 64, fallback_policy: :abort)
end

Instance Method Details

#check(body) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/zold/node/entrance.rb', line 83

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
    if balance.negative? && !wallet.root?
      raise "The balance #{balance} of #{wallet.id} is negative and it's not a root wallet"
    end
    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, sync: true) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/zold/node/entrance.rb', line 72

def push(id, body, sync: true)
  check(body)
  if sync
    push_sync(id, body)
  else
    @pool.post do
      push_sync(id, body)
    end
  end
end

#to_jsonObject



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

def to_json
  {
    'semaphores': @semaphores.size,
    'modified': @modified.length,
    'pool': {
      'completed_task_count': @pool.completed_task_count,
      'largest_length': @pool.largest_length,
      'length': @pool.length,
      'queue_length': @pool.queue_length
    },
    'pushes': {
      'completed_task_count': @pushes.completed_task_count,
      'largest_length': @pushes.largest_length,
      'length': @pushes.length,
      'queue_length': @pushes.queue_length
    }
  }
end