|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BitWasp\Bitcoin\Block; |
|
6
|
|
|
|
|
7
|
|
|
use BitWasp\Bitcoin\Crypto\Hash; |
|
8
|
|
|
use BitWasp\Bitcoin\Exceptions\InvalidHashLengthException; |
|
9
|
|
|
use BitWasp\Bitcoin\Serializable; |
|
10
|
|
|
use BitWasp\Bitcoin\Serializer\Block\BlockHeaderSerializer; |
|
11
|
|
|
use BitWasp\Buffertools\BufferInterface; |
|
12
|
|
|
|
|
13
|
|
|
class BlockHeader extends Serializable implements BlockHeaderInterface |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var int |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $version; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var BufferInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $prevBlock; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var BufferInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $merkleRoot; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var int |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $timestamp; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var int |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $bits; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var int |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $nonce; |
|
45
|
|
|
|
|
46
|
|
|
const BIP9_PREFIX = 1 << 29; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param int $version |
|
50
|
|
|
* @param BufferInterface $prevBlock |
|
51
|
|
|
* @param BufferInterface $merkleRoot |
|
52
|
|
|
* @param int $timestamp |
|
53
|
|
|
* @param int $bits |
|
54
|
|
|
* @param int $nonce |
|
55
|
|
|
* @throws InvalidHashLengthException |
|
56
|
25 |
|
*/ |
|
57
|
|
|
public function __construct(int $version, BufferInterface $prevBlock, BufferInterface $merkleRoot, int $timestamp, int $bits, int $nonce) |
|
58
|
25 |
|
{ |
|
59
|
1 |
|
$this->version = $version; |
|
60
|
|
|
$this->prevBlock = $prevBlock; |
|
61
|
|
|
$this->merkleRoot = $merkleRoot; |
|
62
|
24 |
|
$this->timestamp = $timestamp; |
|
63
|
1 |
|
$this->bits = $bits; |
|
64
|
|
|
$this->nonce = $nonce; |
|
65
|
|
|
|
|
66
|
23 |
|
$this->validate(); |
|
67
|
23 |
|
} |
|
68
|
23 |
|
|
|
69
|
23 |
|
/** |
|
70
|
23 |
|
* @return BufferInterface |
|
71
|
23 |
|
*/ |
|
72
|
23 |
|
public function getHash(): BufferInterface |
|
73
|
|
|
{ |
|
74
|
|
|
return Hash::sha256d($this->getBuffer())->flip(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
191 |
|
/** |
|
78
|
|
|
* Get the version for this block |
|
79
|
191 |
|
* |
|
80
|
|
|
* {@inheritdoc} |
|
81
|
|
|
* @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getVersion() |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getVersion(): int |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->version; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
201 |
|
/** |
|
89
|
|
|
* @return bool |
|
90
|
201 |
|
*/ |
|
91
|
|
|
public function hasBip9Prefix(): bool |
|
92
|
|
|
{ |
|
93
|
|
|
return ($this->version & self::BIP9_PREFIX) != 0; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
16 |
|
/** |
|
97
|
|
|
* {@inheritdoc} |
|
98
|
16 |
|
* @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getPrevBlock() |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getPrevBlock(): BufferInterface |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->prevBlock; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
201 |
|
/** |
|
106
|
|
|
* {@inheritdoc} |
|
107
|
201 |
|
* @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getMerkleRoot() |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getMerkleRoot(): BufferInterface |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->merkleRoot; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
204 |
|
/** |
|
115
|
|
|
* {@inheritdoc} |
|
116
|
204 |
|
* @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getBits() |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getBits(): int |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->bits; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
200 |
|
/** |
|
124
|
|
|
* {@inheritdoc} |
|
125
|
200 |
|
* @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getNonce() |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getNonce(): int |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->nonce; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
201 |
|
/** |
|
133
|
|
|
* Get the timestamp for this block |
|
134
|
201 |
|
* |
|
135
|
|
|
* {@inheritdoc} |
|
136
|
|
|
* @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getTimestamp() |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getTimestamp(): int |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->timestamp; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
201 |
|
/** |
|
144
|
|
|
* @param BlockHeaderInterface $other |
|
145
|
201 |
|
* @return bool |
|
146
|
|
|
*/ |
|
147
|
|
|
public function equals(BlockHeaderInterface $other): bool |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->version === $other->getVersion() |
|
150
|
|
|
&& $this->prevBlock->equals($other->getPrevBlock()) |
|
151
|
|
|
&& $this->merkleRoot->equals($other->getMerkleRoot()) |
|
152
|
1 |
|
&& $this->timestamp === $other->getTimestamp() |
|
153
|
|
|
&& $this->bits === $other->getBits() |
|
154
|
1 |
|
&& $this->nonce === $other->getNonce(); |
|
155
|
1 |
|
} |
|
156
|
1 |
|
|
|
157
|
1 |
|
/** |
|
158
|
1 |
|
* {@inheritdoc} |
|
159
|
1 |
|
* @see \BitWasp\Buffertools\SerializableInterface::getBuffer() |
|
160
|
|
|
*/ |
|
161
|
|
|
public function getBuffer(): BufferInterface |
|
162
|
|
|
{ |
|
163
|
|
|
return (new BlockHeaderSerializer())->serialize($this); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
192 |
|
/** |
|
167
|
|
|
* @throws InvalidHashLengthException |
|
168
|
192 |
|
*/ |
|
169
|
|
|
protected function validate() |
|
170
|
|
|
{ |
|
171
|
|
|
if ($this->prevBlock->getSize() !== 32) { |
|
172
|
|
|
throw new InvalidHashLengthException('BlockHeader prevBlock must be a 32-byte Buffer'); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
if ($this->merkleRoot->getSize() !== 32) { |
|
176
|
|
|
throw new InvalidHashLengthException('BlockHeader merkleRoot must be a 32-byte Buffer'); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|