[code]<php[/code]
=inherit=inheritclass =inheritBlock {
=inheritpublic $prevHash;
=inheritpublic $hash;
=inheritpublic $timeStamp;
=inheritpublic $data;
}
[code]<php[/code]
=inherit=inheritclass =inheritBlock {
...
=inheritpublic =inherit=inheritfunction =inheritsetBlockHash=inherit() {
$data = serialize(=inherit$this);
=inherit$this->hash = hash(=inherit'sha256', $data);
}
}
[code]<php[/code]
=inherit=inheritclass =inheritBlock {
...
=inheritpublic =inherit=inheritfunction =inherit__construct=inherit($prevHash, $data) {
=inherit$this->prevHash = $prevHash;
=inherit$this->timeStamp = time();
=inherit$this->data = $data;
=inherit$this->setBlockHash();
}
}
[code]=inherit<?php[/code]
=inherit=inheritclass =inheritBlock
{
...
=inheritpublic =inherit=inheritfunction =inheritgetBlockHash=inherit()
{
=inheritreturn =inherit$this->hash;
}
}
[code]=inherit<?php
[/code][code]=inheritinclude(=inherit'block.php');[/code]
=inherit=inheritclass =inheritBlockchain
{
=inheritpublic $blocks = [];
}
[code]=inherit<?php[/code]
=inheritinclude(=inherit'block.php');
=inherit=inheritclass =inheritBlockchain
{
...
=inheritpublic =inherit=inheritfunction =inheritaddBlock=inherit($data)
{
$prevBlock = =inherit$this->blocks[count(=inherit$this->blocks)=inherit-1];
=inherit$this->blocks[] = =inheritnew Block($prevBlock->getBlockHash(), $data);
}
}
[code]=inherit<?php[/code]
=inheritinclude(=inherit'block.php');
=inherit=inheritclass =inheritBlockchain
{
...
=inheritpublic =inherit=inheritfunction =inherit__construct=inherit()
{
=inherit$this->blocks[] = =inheritnew Block(=inherit'', =inherit'Genesis Block');
}
}
[code]=inherit<?php[/code]
=inheritinclude(=inherit'blockchain.php');
$bc = =inheritnew Blockchain();
$bc->addBlock(=inherit'This is block1');
$bc->addBlock(=inherit'This is block2');
=inheritforeach ($bc->blocks as $block) {
printf(=inherit"PrevHash: %s\n", $block->prevHash);
printf(=inherit"Hash: %s\n", $block->hash);
printf(=inherit"Data: %s\n", $block->data);
printf(=inherit"\n");
}
本文为 @ 21CTO 创作并授权 21CTO 发布,未经许可,请勿转载。
内容授权事宜请您联系 webmaster@21cto.com或关注 21CTO 公众号。
该文观点仅代表作者本人,21CTO 平台仅提供信息存储空间服务。