Class: Dcha::MPT::Node
- Inherits:
-
Array
- Object
- Array
- Dcha::MPT::Node
show all
- Includes:
- Deletable, Editable, Findable, ToHashable
- Defined in:
- lib/dcha/mpt/node.rb,
lib/dcha/mpt/node/editable.rb,
lib/dcha/mpt/node/findable.rb,
lib/dcha/mpt/node/deletable.rb,
lib/dcha/mpt/node/to_hashable.rb
Overview
Defined Under Namespace
Modules: Deletable, Editable, Findable, ToHashable
Constant Summary
collapse
- BLANK =
new(['']).freeze
- BRANCH_CARDINAL =
16
- BRANCH_WIDTH =
BRANCH_CARDINAL + 1
- KV_WIDTH =
2
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from ToHashable
#to_h
Methods included from Deletable
#delete
Methods included from Editable
#update
Methods included from Findable
#find
Class Method Details
.decode(encoded) ⇒ Object
11
12
13
14
15
16
|
# File 'lib/dcha/mpt/node.rb', line 11
def decode(encoded)
return BLANK if encoded == BLANK.first
return encoded if encoded.is_a?(Node)
return new encoded if encoded.is_a?(Array)
new RLP.decode(Config.store[encoded])
end
|
Instance Method Details
#save(force = false) ⇒ Object
53
54
55
56
57
58
59
60
61
|
# File 'lib/dcha/mpt/node.rb', line 53
def save(force = false)
value = RLP.encode self
return self if value.size < 32 && !force
key = Config.digest.hexdigest(value)
Config.store[key] = value
key
end
|
#tree_size ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/dcha/mpt/node.rb', line 41
def tree_size
case type
when :branch
children = take(BRANCH_CARDINAL).map(&:tree_size)
children.push(last != BLANK ? 0 : 1)
size.reduce(0, &:+)
when :extension then Node.decode(self[1]).tree_size
when :leaf then 1
when :blank then 0
end
end
|
#type ⇒ Object
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/dcha/mpt/node.rb', line 30
def type
return :blank if size == 1 && first.empty?
case size
when KV_WIDTH
NibbleKey.decode(first).terminate? ? :leaf : :extension
when BRANCH_WIDTH
:branch
end
end
|