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