1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BitWasp\Bitcoin\Networking\Serializer\Message; |
6
|
|
|
|
7
|
|
|
use BitWasp\Bitcoin\Networking\Messages\GetBlocks; |
8
|
|
|
use BitWasp\Bitcoin\Serializer\Chain\BlockLocatorSerializer; |
9
|
|
|
use BitWasp\Bitcoin\Serializer\Types; |
10
|
|
|
use BitWasp\Buffertools\Buffer; |
11
|
|
|
use BitWasp\Buffertools\BufferInterface; |
12
|
|
|
use BitWasp\Buffertools\Buffertools; |
13
|
|
|
use BitWasp\Buffertools\Parser; |
14
|
|
|
|
15
|
|
|
class GetBlocksSerializer |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var BlockLocatorSerializer |
19
|
|
|
*/ |
20
|
|
|
private $locator; |
21
|
96 |
|
|
22
|
|
|
/** |
23
|
96 |
|
* @var \BitWasp\Buffertools\Types\Uint32 |
24
|
96 |
|
*/ |
25
|
|
|
private $uint32le; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param BlockLocatorSerializer $locatorSerializer |
29
|
3 |
|
*/ |
30
|
|
|
public function __construct(BlockLocatorSerializer $locatorSerializer) |
31
|
3 |
|
{ |
32
|
3 |
|
$this->uint32le = Types::uint32le(); |
33
|
3 |
|
$this->locator = $locatorSerializer; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Parser $parser |
38
|
|
|
* @return GetBlocks |
39
|
|
|
*/ |
40
|
3 |
|
public function fromParser(Parser $parser): GetBlocks |
41
|
|
|
{ |
42
|
3 |
|
return new GetBlocks( |
43
|
3 |
|
(int) $this->uint32le->read($parser), |
44
|
|
|
$this->locator->fromParser($parser) |
45
|
3 |
|
); |
46
|
2 |
|
} |
47
|
|
|
|
48
|
2 |
|
/** |
49
|
|
|
* @param BufferInterface $data |
50
|
|
|
* @return GetBlocks |
51
|
|
|
*/ |
52
|
|
|
public function parse(BufferInterface $data): GetBlocks |
53
|
|
|
{ |
54
|
|
|
return $this->fromParser(new Parser($data)); |
55
|
3 |
|
} |
56
|
|
|
|
57
|
3 |
|
/** |
58
|
|
|
* @param GetBlocks $msg |
59
|
|
|
* @return BufferInterface |
60
|
|
|
*/ |
61
|
|
|
public function serialize(GetBlocks $msg): BufferInterface |
62
|
|
|
{ |
63
|
|
|
return Buffertools::concat( |
64
|
3 |
|
new Buffer($this->uint32le->write($msg->getVersion())), |
65
|
|
|
$this->locator->serialize($msg->getLocator()) |
66
|
3 |
|
); |
67
|
3 |
|
} |
68
|
|
|
} |
69
|
|
|
|