|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Serializer\Block; |
|
4
|
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Block\Block; |
|
6
|
|
|
use BitWasp\Bitcoin\Block\BlockInterface; |
|
7
|
|
|
use BitWasp\Bitcoin\Math\Math; |
|
8
|
|
|
use BitWasp\Bitcoin\Serializer\Transaction\TransactionSerializerInterface; |
|
9
|
|
|
use BitWasp\Bitcoin\Serializer\Types; |
|
10
|
|
|
use BitWasp\Buffertools\Exceptions\ParserOutOfRange; |
|
11
|
|
|
use BitWasp\Buffertools\Parser; |
|
12
|
|
|
|
|
13
|
|
|
class BlockSerializer implements BlockSerializerInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var Math |
|
17
|
|
|
*/ |
|
18
|
|
|
private $math; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var BlockHeaderSerializer |
|
22
|
|
|
*/ |
|
23
|
|
|
private $headerSerializer; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var \BitWasp\Buffertools\Types\VarInt |
|
27
|
|
|
*/ |
|
28
|
|
|
private $varint; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var TransactionSerializerInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $txSerializer; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param Math $math |
|
37
|
|
|
* @param BlockHeaderSerializer $headerSerializer |
|
38
|
|
|
* @param TransactionSerializerInterface $txSerializer |
|
39
|
|
|
*/ |
|
40
|
28 |
|
public function __construct(Math $math, BlockHeaderSerializer $headerSerializer, TransactionSerializerInterface $txSerializer) |
|
41
|
|
|
{ |
|
42
|
28 |
|
$this->math = $math; |
|
43
|
28 |
|
$this->headerSerializer = $headerSerializer; |
|
44
|
28 |
|
$this->varint = Types::varint(); |
|
45
|
28 |
|
$this->txSerializer = $txSerializer; |
|
46
|
28 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param Parser $parser |
|
50
|
|
|
* @return BlockInterface |
|
51
|
|
|
* @throws ParserOutOfRange |
|
52
|
|
|
*/ |
|
53
|
26 |
|
public function fromParser(Parser $parser) |
|
54
|
|
|
{ |
|
55
|
|
|
try { |
|
56
|
26 |
|
$header = $this->headerSerializer->fromParser($parser); |
|
57
|
24 |
|
$nTx = $this->varint->read($parser); |
|
58
|
24 |
|
$vTx = []; |
|
59
|
24 |
|
for ($i = 0; $i < $nTx; $i++) { |
|
60
|
22 |
|
$vTx[] = $this->txSerializer->fromParser($parser); |
|
61
|
|
|
} |
|
62
|
24 |
|
return new Block($this->math, $header, $vTx); |
|
63
|
2 |
|
} catch (ParserOutOfRange $e) { |
|
64
|
2 |
|
throw new ParserOutOfRange('Failed to extract full block header from parser'); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param \BitWasp\Buffertools\BufferInterface|string $string |
|
70
|
|
|
* @return BlockInterface |
|
71
|
|
|
* @throws ParserOutOfRange |
|
72
|
|
|
*/ |
|
73
|
22 |
|
public function parse($string) |
|
74
|
|
|
{ |
|
75
|
22 |
|
return $this->fromParser(new Parser($string)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param BlockInterface $block |
|
80
|
|
|
* @return \BitWasp\Buffertools\BufferInterface |
|
81
|
|
|
*/ |
|
82
|
6 |
|
public function serialize(BlockInterface $block) |
|
83
|
|
|
{ |
|
84
|
6 |
|
$parser = new Parser($this->headerSerializer->serialize($block->getHeader())); |
|
85
|
6 |
|
$parser->appendBinary($this->varint->write(count($block->getTransactions()))); |
|
|
|
|
|
|
86
|
6 |
|
foreach ($block->getTransactions() as $tx) { |
|
87
|
6 |
|
$parser->appendBuffer($this->txSerializer->serialize($tx)); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
6 |
|
return $parser->getBuffer(); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.