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, network: 'test') ⇒ Entrance

Returns a new instance of Entrance.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/zold/node/entrance.rb', line 40

def initialize(wallets, remotes, copies, address, log: Log::Quiet.new, network: 'test')
  raise 'Wallets can\'t be nil' if wallets.nil?
  raise 'Wallets must implement the contract of Wallets: method #find is required' unless wallets.respond_to?(:find)
  @wallets = wallets
  raise 'Remotes can\'t be nil' if remotes.nil?
  raise "Remotes must be of type Remotes: #{remotes.class.name}" unless remotes.is_a?(Remotes)
  @remotes = remotes
  raise 'Copies can\'t be nil' if copies.nil?
  @copies = copies
  raise 'Address can\'t be nil' if address.nil?
  @address = address
  raise 'Log can\'t be nil' if log.nil?
  @log = log
  raise 'Network can\'t be nil' if network.nil?
  @network = network
  @history = []
  @speed = []
  @mutex = Mutex.new
end

Instance Method Details

#push(id, body) ⇒ Object

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/zold/node/entrance.rb', line 73

def push(id, body)
  raise 'Id can\'t be nil' if id.nil?
  raise 'Id must be of type Id' unless id.is_a?(Id)
  raise 'Body can\'t be nil' if body.nil?
  start = Time.now
  copies = Copies.new(File.join(@copies, id.to_s))
  localhost = '0.0.0.0'
  copies.add(body, localhost, 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.new(
    wallets: @wallets, copies: copies.root, log: @log
  ).run(['merge', id.to_s])
  Clean.new(wallets: @wallets, copies: copies.root, log: @log).run(['clean', id.to_s])
  copies.remove(localhost, Remotes::PORT)
  sec = (Time.now - start).round(2)
  if modified.empty?
    @log.info("Accepted #{id} in #{sec}s and not modified anything")
  else
    @log.info("Accepted #{id} in #{sec}s and modified #{modified.join(', ')}")
  end
  @mutex.synchronize do
    @history.shift if @history.length >= 16
    @speed.shift if @speed.length >= 64
    @wallets.find(id) do |wallet|
      @history << "#{id}/#{sec}/#{modified.count}/#{wallet.balance.to_zld}/#{wallet.txns.count}t"
    end
    @speed << sec
  end
  modified
end

#start {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



60
61
62
# File 'lib/zold/node/entrance.rb', line 60

def start
  yield(self)
end

#to_jsonObject



64
65
66
67
68
69
70
# File 'lib/zold/node/entrance.rb', line 64

def to_json
  {
    'history': @history.join(', '),
    'history_size': @history.count,
    'speed': @speed.empty? ? 0 : (@speed.inject(&:+) / @speed.count)
  }
end