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

Returns a new instance of Entrance.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/zold/node/entrance.rb', line 41

def initialize(wallets, remotes, copies, address, log: Log::NULL, network: 'test')
  @wallets = wallets
  @remotes = remotes
  @copies = copies
  @address = address
  @log = log
  @network = network
  @history = []
  @speed = []
  @mutex = Mutex.new
end

Instance Method Details

#push(id, body) ⇒ Object

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



67
68
69
70
71
72
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
# File 'lib/zold/node/entrance.rb', line 67

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, remotes: @remotes, copies: copies.root, log: @log
  ).run(['merge', id.to_s, '--no-baseline'])
  Clean.new(wallets: @wallets, copies: copies.root, log: @log).run(['clean', id.to_s])
  copies.remove(localhost, 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
  sec = (Time.now - start).round(2)
  @mutex.synchronize do
    @history.shift if @history.length >= 16
    @speed.shift if @speed.length >= 64
    @wallets.acq(id) do |wallet|
      @history << "#{sec}/#{modified.count}/#{wallet.mnemo}"
    end
    @speed << sec
  end
  modified
end

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

Yields:

  • (_self)

Yield Parameters:



53
54
55
56
# File 'lib/zold/node/entrance.rb', line 53

def start
  raise 'Block must be given to start()' unless block_given?
  yield(self)
end

#to_jsonObject



58
59
60
61
62
63
64
# File 'lib/zold/node/entrance.rb', line 58

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