-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockchain.php
75 lines (61 loc) · 1.72 KB
/
blockchain.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/**
* Created by PhpStorm.
* User: ttt
* Date: 2018/2/20
* Time: 17:36
*/
declare(ticks=1);
/**
* Class SimpleBlock
*/
class SimpleBlock
{
protected $index;
protected $timestamp;
protected $data;
protected $previous_hash;
protected $hash;
public function __construct(int $index, string $data, string $previous_hash)
{
$this->index = $index;
$this->data = $data;
$this->previous_hash = $previous_hash;
$this->timestamp = microtime(true);
$this->hash = $this->calcHash();
}
/**
* @return string
*/
public function calcHash(): string
{
$sha = hash_init('sha256');
hash_update($sha, $this->index . $this->timestamp . $this->data . $this->previous_hash);
return hash_final($sha);
}
/**
* @param string $data
* @param string $previous_hash
* @return static
*/
public static function first(string $data = 'ainiyiwannian', string $previous_hash = '0'): SimpleBlock
{
return new static(0, $data, $previous_hash);
}
/**
* @param SimpleBlock $previous
* @param string $data
* @return static
*/
public static function next(SimpleBlock $previous, string $data): SimpleBlock
{
return new static($previous->index + 1, $data, $previous->hash);
}
}
$b0 = SimpleBlock::first('Genesis block');
$b1 = SimpleBlock::next($b0, 'Hello, I am block 1');
$b2 = SimpleBlock::next($b1, 'Hello, I am block 2');
$b3 = SimpleBlock::next($b2, 'Hello, I am block 3');
$b4 = SimpleBlock::next($b3, 'Hello, I am block 4');
$blockchain = [$b0, $b1, $b2, $b3, $b4];
var_dump($blockchain);