-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
234 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\drupol\phptree\Node; | ||
|
||
use drupol\phpmerkle\Hasher\DoubleSha256; | ||
use drupol\phptree\Node\MerkleNode; | ||
use drupol\phpmerkle\Hasher\DummyHasher; | ||
use PhpSpec\ObjectBehavior; | ||
|
||
class MerkleNodeSpec extends ObjectBehavior | ||
{ | ||
public function it_can_get_a_hash() | ||
{ | ||
$this | ||
->beConstructedWith('root', 2, new DoubleSha256()); | ||
|
||
$input = [ | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
'Science', | ||
'is', | ||
'made', | ||
'up', | ||
'of', | ||
'so', | ||
'many', | ||
'things', | ||
'that', | ||
'appear', | ||
'obvious', | ||
'after', | ||
'they', | ||
'are', | ||
'explained', | ||
'.', | ||
]; | ||
|
||
foreach ($input as $word) { | ||
$nodes[] = new MerkleNode($word, 2, new DoubleSha256()); | ||
} | ||
|
||
$this | ||
->add(...$nodes) | ||
->getValue() | ||
->shouldReturn('c689102cdf2a5b30c2e21fdad85e4bb401085227aff672a7240ceb3410ff1fb6'); | ||
} | ||
|
||
public function it_can_get_the_value_of_a_tree_with_a_single_node() | ||
{ | ||
$this | ||
->getValue() | ||
->shouldReturn('root'); | ||
} | ||
|
||
public function it_can_get_the_value_of_a_tree_with_four_nodes() | ||
{ | ||
$nodes = [ | ||
new MerkleNode(null, 2, new DummyHasher()), | ||
new MerkleNode(null, 2, new DummyHasher()), | ||
new MerkleNode('a', 2, new DummyHasher()), | ||
new MerkleNode('b', 2, new DummyHasher()), | ||
new MerkleNode('c', 2, new DummyHasher()), | ||
]; | ||
|
||
$this | ||
->add(...$nodes) | ||
->getValue() | ||
->shouldReturn('abcc'); | ||
} | ||
|
||
public function it_can_get_the_value_of_a_tree_with_three_nodes() | ||
{ | ||
$nodes = [ | ||
new MerkleNode('a', 2, new DummyHasher()), | ||
new MerkleNode('b', 2, new DummyHasher()), | ||
]; | ||
|
||
$this | ||
->add(...$nodes) | ||
->getValue() | ||
->shouldReturn('ab'); | ||
} | ||
|
||
public function it_can_get_the_value_of_a_tree_with_two_nodes() | ||
{ | ||
$node = new MerkleNode('a', 2, new DummyHasher()); | ||
|
||
$this | ||
->add($node) | ||
->getValue() | ||
->shouldReturn('aa'); | ||
} | ||
|
||
public function it_is_initializable() | ||
{ | ||
$this->shouldHaveType(MerkleNode::class); | ||
} | ||
|
||
public function let() | ||
{ | ||
$this | ||
->beConstructedWith('root', 2, new DummyHasher()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace drupol\phptree\Node; | ||
|
||
use drupol\phpmerkle\Hasher\DoubleSha256; | ||
use drupol\phpmerkle\Hasher\HasherInterface; | ||
|
||
/** | ||
* Class MerkleNode. | ||
*/ | ||
class MerkleNode extends ValueNode | ||
{ | ||
/** | ||
* @var \drupol\phpmerkle\Hasher\HasherInterface | ||
*/ | ||
private $hasher; | ||
|
||
/** | ||
* MerkleNode constructor. | ||
* | ||
* @param mixed $value | ||
* @param int $capacity | ||
* @param \drupol\phpmerkle\Hasher\HasherInterface $hasher | ||
*/ | ||
public function __construct( | ||
$value, | ||
int $capacity = 2, | ||
?HasherInterface $hasher = null | ||
) { | ||
parent::__construct($value, $capacity, null, null); | ||
|
||
$this->hasher = $hasher ?? new DoubleSha256(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getValue() | ||
{ | ||
if (true === $this->isLeaf()) { | ||
return parent::getValue(); | ||
} | ||
|
||
return $this->hash(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
private function doHash(): string | ||
{ | ||
// If node is a leaf, then compute its hash from its value. | ||
if (true === $this->isLeaf()) { | ||
$value = $this->getValue(); | ||
|
||
if (null === $value) { | ||
return ''; | ||
} | ||
|
||
return $this->hasher->hash($value); | ||
} | ||
|
||
// Remove all nodes with null value. | ||
if (null !== $parent = $this->getParent()) { | ||
/** @var \drupol\phptree\Node\MerkleNode $node */ | ||
foreach ($parent->all() as $node) { | ||
if (false === $node->isLeaf()) { | ||
continue; | ||
} | ||
|
||
if (null !== $node->getValue()) { | ||
continue; | ||
} | ||
|
||
$node->getParent()->remove($node); | ||
|
||
return $this->hash(); | ||
} | ||
} | ||
|
||
// If node with children is not fulfilled, make sure it is complete. | ||
if ($this->degree() !== $this->capacity()) { | ||
$children = iterator_to_array($this->children()); | ||
|
||
if ([] !== $children) { | ||
$this->add(current($children)->clone()); | ||
} | ||
} | ||
|
||
$hash = array_reduce( | ||
iterator_to_array($this->children()), | ||
static function ($carry, MerkleNode $node): string { | ||
return $carry . $node->doHash(); | ||
}, | ||
'' | ||
); | ||
|
||
return $this->hasher->hash($hash); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
private function hash(): string | ||
{ | ||
return $this->hasher->unpack($this->doHash()); | ||
} | ||
} |