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
|
|
|
/** |
44
|
|
|
* @param int $version |
45
|
|
|
* @param BufferInterface $prevBlock |
46
|
|
|
* @param BufferInterface $merkleRoot |
47
|
|
|
* @param int $timestamp |
48
|
|
|
* @param int $bits |
49
|
|
|
* @param int $nonce |
50
|
|
|
*/ |
51
|
63 |
|
public function __construct($version, BufferInterface $prevBlock, BufferInterface $merkleRoot, $timestamp, $bits, $nonce) |
52
|
|
|
{ |
53
|
63 |
|
if ($prevBlock->getSize() !== 32) { |
54
|
|
|
throw new \InvalidArgumentException('BlockHeader prevBlock must be a 32-byte Buffer'); |
55
|
|
|
} |
56
|
|
|
|
57
|
63 |
|
if ($merkleRoot->getSize() !== 32) { |
58
|
|
|
throw new \InvalidArgumentException('BlockHeader merkleRoot must be a 32-byte Buffer'); |
59
|
|
|
} |
60
|
|
|
|
61
|
63 |
|
$this->version = $version; |
62
|
63 |
|
$this->prevBlock = $prevBlock; |
63
|
63 |
|
$this->merkleRoot = $merkleRoot; |
64
|
63 |
|
$this->timestamp = $timestamp; |
65
|
63 |
|
$this->bits = $bits; |
66
|
63 |
|
$this->nonce = $nonce; |
67
|
63 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return BufferInterface |
71
|
|
|
*/ |
72
|
573 |
|
public function getHash() |
73
|
|
|
{ |
74
|
573 |
|
return Hash::sha256d($this->getBuffer())->flip(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get the version for this block |
79
|
|
|
* |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
* @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getVersion() |
82
|
|
|
*/ |
83
|
600 |
|
public function getVersion() |
84
|
|
|
{ |
85
|
600 |
|
return $this->version; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
* @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getPrevBlock() |
91
|
|
|
*/ |
92
|
600 |
|
public function getPrevBlock() |
93
|
|
|
{ |
94
|
600 |
|
return $this->prevBlock; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
* @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getMerkleRoot() |
100
|
|
|
*/ |
101
|
609 |
|
public function getMerkleRoot() |
102
|
|
|
{ |
103
|
609 |
|
return $this->merkleRoot; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
* @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getBits() |
109
|
|
|
*/ |
110
|
597 |
|
public function getBits() |
111
|
|
|
{ |
112
|
597 |
|
return $this->bits; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
* @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getNonce() |
118
|
|
|
*/ |
119
|
600 |
|
public function getNonce() |
120
|
|
|
{ |
121
|
600 |
|
return $this->nonce; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get the timestamp for this block |
126
|
|
|
* |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
* @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getTimestamp() |
129
|
|
|
*/ |
130
|
600 |
|
public function getTimestamp() |
131
|
|
|
{ |
132
|
600 |
|
return $this->timestamp; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param BlockHeaderInterface $other |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
|
|
public function equals(BlockHeaderInterface $other) |
140
|
|
|
{ |
141
|
|
|
return $this->version === $other->getVersion() |
142
|
|
|
&& $this->prevBlock->equals($other->getPrevBlock()) |
|
|
|
|
143
|
|
|
&& $this->merkleRoot->equals($other->getMerkleRoot()) |
|
|
|
|
144
|
|
|
&& $this->timestamp === $other->getTimestamp() |
145
|
|
|
&& $this->bits === $other->getBits() |
146
|
|
|
&& $this->nonce === $other->getNonce(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritdoc} |
151
|
|
|
* @see \BitWasp\Buffertools\SerializableInterface::getBuffer() |
152
|
|
|
*/ |
153
|
576 |
|
public function getBuffer() |
154
|
|
|
{ |
155
|
576 |
|
return (new BlockHeaderSerializer())->serialize($this); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: