Class: MerkleTree::Node Private

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/merkle_tree/node.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Holds information about intermediate hashes

Direct Known Subclasses

EmptyNode

Defined Under Namespace

Classes: EmptyNode

Constant Summary collapse

UNDEFINED =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Module.new
EMPTY =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

EmptyNode

EmptyNode.new.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, left, right, height, left_index, right_index) ⇒ Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a node



37
38
39
40
41
42
43
44
# File 'lib/merkle_tree/node.rb', line 37

def initialize(value, left, right, height, left_index, right_index)
  @value  = value
  @left   = left
  @right  = right
  @height = height
  @left_index  = left_index
  @right_index = right_index
end

Instance Attribute Details

#heightObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The node height in the tree



18
19
20
# File 'lib/merkle_tree/node.rb', line 18

def height
  @height
end

#leftObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



13
14
15
# File 'lib/merkle_tree/node.rb', line 13

def left
  @left
end

#left_indexObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The sequential position in the tree



21
22
23
# File 'lib/merkle_tree/node.rb', line 21

def left_index
  @left_index
end

#rightObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



15
16
17
# File 'lib/merkle_tree/node.rb', line 15

def right
  @right
end

#right_indexObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



23
24
25
# File 'lib/merkle_tree/node.rb', line 23

def right_index
  @right_index
end

#valueObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



11
12
13
# File 'lib/merkle_tree/node.rb', line 11

def value
  @value
end

Class Method Details

.build(left, right, digest: MerkleTree.default_digest) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



25
26
27
28
29
30
31
32
# File 'lib/merkle_tree/node.rb', line 25

def self.build(left, right, digest: MerkleTree.default_digest)
  value  = digest.(left.value + right.value)
  height = left.height + 1
  left_index  = left.left_index
  right_index = right.right_index

  new(value, left, right, height, left_index, right_index)
end

Instance Method Details

#<=>(other) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



88
89
90
91
92
# File 'lib/merkle_tree/node.rb', line 88

def <=>(other)
  value <=> other.value &&
    left_index <=> other.left_index &&
    right_index <=> other.right_index
end

#child(index) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def child(index)
  if left.include?(index)
    left
  else
    right.include?(index) ? right : EMPTY
  end
end

#include?(index) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


54
55
56
# File 'lib/merkle_tree/node.rb', line 54

def include?(index)
  (left_index..right_index).cover?(index)
end

#leaf?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


46
47
48
# File 'lib/merkle_tree/node.rb', line 46

def leaf?
  false
end

#sibling(index) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Find sibling child node for the index



71
72
73
74
75
76
77
# File 'lib/merkle_tree/node.rb', line 71

def sibling(index)
  if left.include?(index)
    [:right, right.value]
  else
    right.include?(index) ? [:left, left.value] : EMPTY
  end
end

#sizeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



50
51
52
# File 'lib/merkle_tree/node.rb', line 50

def size
  left.size + 1 + right.size
end

#subtree(index) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Find subtree that matches the index



80
81
82
83
84
85
86
# File 'lib/merkle_tree/node.rb', line 80

def subtree(index)
  if left.include?(index)
    left
  else
    right.include?(index) ? right : EMPTY
  end
end

#to_hObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



94
95
96
# File 'lib/merkle_tree/node.rb', line 94

def to_h
  { value: value, left: left.to_h, right: right.to_h }
end

#to_s(indent = "") ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



98
99
100
101
102
# File 'lib/merkle_tree/node.rb', line 98

def to_s(indent = "")
  indent + value.to_s + $RS +
    left.to_s(indent + "  ") + $RS +
    right.to_s(indent + "  ")
end

#update(digest) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



58
59
60
# File 'lib/merkle_tree/node.rb', line 58

def update(digest)
  @value = digest.(left.value + right.value)
end