pleonasm/merkle-tree
Composer 安装命令:
composer require pleonasm/merkle-tree
包简介
An implementation of a Merkle Tree in PHP
README 文档
README
This is an implementation of a hash tree or Merkle Tree for PHP.
Install
Install via Composer (make sure you have composer in your path or in your project).
Put the following in your package.json:
{ "require": { "pleonasm/merkle-tree": "*" } }
Then run composer install.
Usage For A Fixed Size Tree
Usage of a hash tree requires a user to provide a hashing function that each node will use to do its hashing. This function must accept a single string argument and must always return a string. It should never throw an exception.
If desired, you can also provide a callback for when the entire hash tree has been actually finished, rather than checking for it each time yourself.
<?php use Pleo\Merkle\FixedSizeTree; require 'vendor/autoload.php'; // basically the same thing bitcoin merkle tree hashing does $hasher = function ($data) { return hash('sha256', hash('sha256', $data, true), true); }; $finished = function ($hash) { echo implode('', unpack('H*', $hash)) . "\n"; }; $tree = new FixedSizeTree(16, $hasher, $finished); $tree->set(0, 'Science'); $tree->set(1, 'is'); $tree->set(2, 'made'); $tree->set(3, 'up'); $tree->set(4, 'of'); $tree->set(5, 'so'); $tree->set(6, 'many'); $tree->set(7, 'things'); $tree->set(8, 'that'); $tree->set(9, 'appear'); $tree->set(10, 'obvious'); $tree->set(11, 'after'); $tree->set(12, 'they'); $tree->set(13, 'are'); $tree->set(14, 'explained'); $tree->set(15, '.'); // this will echo the string 'c689102cdf2a5b30c2e21fdad85e4bb401085227aff672a7240ceb3410ff1fb6'
The FixedSizeTree implements a Merkle Tree the same way bitcoins do. There are other ways to actually deal with a tree width that is not a perfect square.
If there is a need for the other method, I would not be opposed to adding it.
Upgrading from 1.0
The difference between 1.0 and 2.0 from the outside should be very minimal. If you're only using the public interface of FixedSizeTree minus a few error handling changes, then you should be good to go.
The new implementation is greatly simplified and is far faster for large trees. My latest benchmark put 1.0 taking 100 seconds to fill a 10000 item tree while 2.0 takes 0.2 seconds.
That said, it is technically a backwards incompatible change so a major version bump is in order.
License
You can find the license for this code in the LICENSE file
pleonasm/merkle-tree 适用场景与选型建议
pleonasm/merkle-tree 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 803.96k 次下载、GitHub Stars 达 38, 最近一次更新时间为 2013 年 05 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 pleonasm/merkle-tree 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pleonasm/merkle-tree 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 803.96k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 39
- 点击次数: 24
- 依赖项目数: 39
- 推荐数: 0
其他信息
- 授权协议: BSD-2-Clause
- 更新时间: 2013-05-07