|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BitWasp\Bitcoin\Block; |
|
6
|
|
|
|
|
7
|
|
|
use BitWasp\Bitcoin\Bloom\BloomFilter; |
|
8
|
|
|
use BitWasp\Bitcoin\Math\Math; |
|
9
|
|
|
use BitWasp\Bitcoin\Serializable; |
|
10
|
|
|
use BitWasp\Bitcoin\Serializer\Block\BlockHeaderSerializer; |
|
11
|
|
|
use BitWasp\Bitcoin\Serializer\Block\BlockSerializer; |
|
12
|
|
|
use BitWasp\Bitcoin\Serializer\Transaction\TransactionSerializer; |
|
13
|
|
|
use BitWasp\Bitcoin\Transaction\TransactionInterface; |
|
14
|
|
|
use BitWasp\Buffertools\BufferInterface; |
|
15
|
|
|
|
|
16
|
|
|
class Block extends Serializable implements BlockInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var Math |
|
20
|
|
|
*/ |
|
21
|
|
|
private $math; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var BlockHeaderInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $header; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var TransactionInterface[] |
|
30
|
|
|
*/ |
|
31
|
|
|
private $transactions; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var MerkleRoot |
|
35
|
|
|
*/ |
|
36
|
|
|
private $merkleRoot; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Block constructor. |
|
40
|
|
|
* @param Math $math |
|
41
|
|
|
* @param BlockHeaderInterface $header |
|
42
|
|
|
* @param TransactionInterface[] ...$transactions |
|
43
|
|
|
*/ |
|
44
|
18 |
|
public function __construct(Math $math, BlockHeaderInterface $header, TransactionInterface ...$transactions) |
|
45
|
|
|
{ |
|
46
|
18 |
|
$this->math = $math; |
|
47
|
18 |
|
$this->header = $header; |
|
48
|
18 |
|
$this->transactions = $transactions; |
|
|
|
|
|
|
49
|
18 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
* @see \BitWasp\Bitcoin\Block\BlockInterface::getHeader() |
|
54
|
|
|
*/ |
|
55
|
14 |
|
public function getHeader(): BlockHeaderInterface |
|
56
|
|
|
{ |
|
57
|
14 |
|
return $this->header; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritdoc} |
|
62
|
|
|
* @see \BitWasp\Bitcoin\Block\BlockInterface::getMerkleRoot() |
|
63
|
|
|
* @throws \BitWasp\Bitcoin\Exceptions\MerkleTreeEmpty |
|
64
|
|
|
*/ |
|
65
|
4 |
|
public function getMerkleRoot(): BufferInterface |
|
66
|
|
|
{ |
|
67
|
4 |
|
if (null === $this->merkleRoot) { |
|
68
|
4 |
|
$this->merkleRoot = new MerkleRoot($this->math, $this->getTransactions()); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
4 |
|
return $this->merkleRoot->calculateHash(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @see \BitWasp\Bitcoin\Block\BlockInterface::getTransactions() |
|
76
|
|
|
* @return TransactionInterface[] |
|
77
|
|
|
*/ |
|
78
|
14 |
|
public function getTransactions(): array |
|
79
|
|
|
{ |
|
80
|
14 |
|
return $this->transactions; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @see \BitWasp\Bitcoin\Block\BlockInterface::getTransaction() |
|
85
|
|
|
* @param int $i |
|
86
|
|
|
* @return TransactionInterface |
|
87
|
|
|
*/ |
|
88
|
2 |
|
public function getTransaction(int $i): TransactionInterface |
|
89
|
|
|
{ |
|
90
|
2 |
|
if (!array_key_exists($i, $this->transactions)) { |
|
91
|
1 |
|
throw new \InvalidArgumentException("No transaction in the block with this index"); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
2 |
|
return $this->transactions[$i]; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param BloomFilter $filter |
|
99
|
|
|
* @return FilteredBlock |
|
100
|
|
|
*/ |
|
101
|
6 |
|
public function filter(BloomFilter $filter): FilteredBlock |
|
102
|
|
|
{ |
|
103
|
6 |
|
$vMatch = []; |
|
104
|
6 |
|
$vHashes = []; |
|
105
|
6 |
|
foreach ($this->getTransactions() as $tx) { |
|
106
|
6 |
|
$vMatch[] = $filter->isRelevantAndUpdate($tx); |
|
107
|
6 |
|
$vHashes[] = $tx->getTxHash(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
6 |
|
return new FilteredBlock( |
|
111
|
6 |
|
$this->getHeader(), |
|
112
|
6 |
|
PartialMerkleTree::create(count($this->getTransactions()), $vHashes, $vMatch) |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* {@inheritdoc} |
|
118
|
|
|
* @see \BitWasp\Buffertools\SerializableInterface::getBuffer() |
|
119
|
|
|
*/ |
|
120
|
2 |
|
public function getBuffer(): BufferInterface |
|
121
|
|
|
{ |
|
122
|
2 |
|
return (new BlockSerializer($this->math, new BlockHeaderSerializer(), new TransactionSerializer()))->serialize($this); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..