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, ledger: '/dev/null', log: Log::NULL, network: 'test') ⇒ Entrance

Returns a new instance of Entrance.



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

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

Instance Method Details

#merge(id, copies) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/zold/node/entrance.rb', line 108

def merge(id, copies)
  Tempfile.open do |f|
    modified = Merge.new(
      wallets: @wallets, remotes: @remotes, copies: copies.root, log: @log
    ).run(['merge', id.to_s, "--ledger=#{f.path}", "--network=#{@network}"])
    @mutex.synchronize do
      txns = File.exist?(@ledger) ? IO.read(@ledger).strip.split("\n") : []
      txns += IO.read(f.path).strip.split("\n")
      IO.write(
        @ledger,
        txns.map { |t| t.split(';') }
          .uniq { |t| "#{t[1]}-#{t[3]}" }
          .reject { |t| Txn.parse_time(t[0]) < Time.now - 24 * 60 * 60 }
          .map { |t| t.join(';') }
          .join("\n")
          .strip
      )
    end
    modified
  end
end

#push(id, body) ⇒ Object

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



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
101
102
103
104
105
106
# File 'lib/zold/node/entrance.rb', line 71

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))
  host = '0.0.0.0'
  copies.add(body, host, 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(id, copies)
  Clean.new(wallets: @wallets, copies: copies.root, log: @log).run(
    ['clean', id.to_s, '--max-age=1']
  )
  copies.remove(host, Remotes::PORT)
  modified += Rebase.new(wallets: @wallets, log: @log).run(['rebase', id.to_s])
  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
  modified << id if copies.all.count > 1
  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:



56
57
58
59
# File 'lib/zold/node/entrance.rb', line 56

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

#to_jsonObject



61
62
63
64
65
66
67
68
# File 'lib/zold/node/entrance.rb', line 61

def to_json
  {
    'history': @history.join(', '),
    'history_size': @history.count,
    'speed': @speed.empty? ? 0 : (@speed.inject(&:+) / @speed.count),
    'ledger': File.exist?(@ledger) ? IO.read(@ledger).split("\n").count : 0
  }
end