Class: LiabilityProof::Verifier

Inherits:
Object
  • Object
show all
Defined in:
lib/liability-proof/verifier.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Verifier

Returns a new instance of Verifier.



4
5
6
7
# File 'lib/liability-proof/verifier.rb', line 4

def initialize(options)
  @root_path = options.delete(:root) || 'root.json'
  @partial_tree_path = options.delete(:file)
end

Instance Method Details

#expect_rootObject



17
18
19
# File 'lib/liability-proof/verifier.rb', line 17

def expect_root
  root_json['root']
end

#match?Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
# File 'lib/liability-proof/verifier.rb', line 21

def match?
  partial_tree = partial_tree_json['partial_tree']

  @reduced_root = reduce(partial_tree).as_json
  @reduced_root == expect_root
end

#partial_tree_jsonObject



13
14
15
# File 'lib/liability-proof/verifier.rb', line 13

def partial_tree_json
  @partial_tree_json ||= JSON.parse File.read(@partial_tree_path)
end

#root_jsonObject



9
10
11
# File 'lib/liability-proof/verifier.rb', line 9

def root_json
  @root_json ||= JSON.parse File.read(@root_path)
end

#verify!Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/liability-proof/verifier.rb', line 28

def verify!
  if match?
    puts "Partial tree verified successfully!\n\n"
    puts "User: #{@user_node.user}"
    puts "Balance: #{@user_node.sum_string}"
  else
    raise "Mismatch! Expected root: #{expect_root.inspect}, calculated root: #{@reduced_root.inspect}"
  end
rescue
  puts "INVALID partial tree!"
  puts "ERROR: #{$!}"
  puts "Expected root:"
  ap expect_root
  if @reduced_root
    puts "Calculated root:"
    ap @reduced_root
  end
end