|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Serializer\Block; |
|
4
|
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Block\BlockInterface; |
|
6
|
|
|
use BitWasp\Bitcoin\Network\NetworkInterface; |
|
7
|
|
|
use BitWasp\Bitcoin\Serializer\Types; |
|
8
|
|
|
use BitWasp\Buffertools\Buffer; |
|
9
|
|
|
use BitWasp\Buffertools\Buffertools; |
|
10
|
|
|
use BitWasp\Buffertools\Parser; |
|
11
|
|
|
|
|
12
|
|
|
class BitcoindBlockSerializer |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var NetworkInterface |
|
16
|
|
|
*/ |
|
17
|
|
|
private $network; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var BlockSerializer |
|
21
|
|
|
*/ |
|
22
|
|
|
private $blockSerializer; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param NetworkInterface $network |
|
26
|
|
|
* @param BlockSerializer $blockSerializer |
|
27
|
18 |
|
*/ |
|
28
|
|
|
public function __construct(NetworkInterface $network, BlockSerializer $blockSerializer) |
|
29
|
18 |
|
{ |
|
30
|
18 |
|
$this->blockSerializer = $blockSerializer; |
|
31
|
18 |
|
$this->magic = Types::bytestringle(4); |
|
|
|
|
|
|
32
|
|
|
$this->size = Types::uint32le(); |
|
|
|
|
|
|
33
|
|
|
$this->network = $network; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
18 |
|
/** |
|
37
|
|
|
* @param BlockInterface $block |
|
38
|
18 |
|
* @return \BitWasp\Buffertools\BufferInterface |
|
39
|
18 |
|
*/ |
|
40
|
18 |
|
public function serialize(BlockInterface $block) |
|
41
|
18 |
|
{ |
|
42
|
|
|
$buffer = $this->blockSerializer->serialize($block); |
|
43
|
|
|
$size = $buffer->getSize(); |
|
44
|
|
|
return new Buffer( |
|
45
|
|
|
Buffertools::flipBytes(pack("H*", $this->network->getNetMagicBytes())) . |
|
46
|
|
|
$this->size->write($size) . |
|
47
|
|
|
$buffer->getBinary() |
|
48
|
6 |
|
); |
|
49
|
|
|
} |
|
50
|
6 |
|
|
|
51
|
6 |
|
/** |
|
52
|
6 |
|
* @param Parser $parser |
|
53
|
6 |
|
* @return BlockInterface |
|
54
|
3 |
|
* @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange |
|
55
|
3 |
|
*/ |
|
56
|
|
|
public function fromParser(Parser $parser) |
|
57
|
6 |
|
{ |
|
58
|
|
|
/** @var Buffer $bytes */ |
|
59
|
6 |
|
/** @var int|string $blockSize */ |
|
60
|
|
|
list ($bytes, $blockSize) = [$this->magic->read($parser), $this->size->read($parser)]; |
|
61
|
|
|
if ($bytes->getHex() !== $this->network->getNetMagicBytes()) { |
|
62
|
|
|
throw new \RuntimeException('Block version bytes did not match network'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $this->blockSerializer->fromParser(new Parser($parser->readBytes($blockSize))); |
|
66
|
|
|
} |
|
67
|
18 |
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param \BitWasp\Buffertools\BufferInterface|string $data |
|
70
|
|
|
* @return BlockInterface |
|
71
|
18 |
|
*/ |
|
72
|
18 |
|
public function parse($data) |
|
73
|
6 |
|
{ |
|
74
|
|
|
return $this->fromParser(new Parser($data)); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: