|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BitWasp\Bitcoin\Networking\Structure; |
|
6
|
|
|
|
|
7
|
|
|
use BitWasp\Bitcoin\Networking\Serializer\Structure\InventorySerializer; |
|
8
|
|
|
use BitWasp\Bitcoin\Serializable; |
|
9
|
|
|
use BitWasp\Buffertools\BufferInterface; |
|
10
|
|
|
|
|
11
|
|
|
class Inventory extends Serializable |
|
12
|
|
|
{ |
|
13
|
|
|
const ERROR = 0; |
|
14
|
|
|
const MSG_TX = 1; |
|
15
|
|
|
const MSG_BLOCK = 2; |
|
16
|
|
|
const MSG_FILTERED_BLOCK = 3; |
|
17
|
|
|
const MSG_WITNESS_TX = (1 << 30) + self::MSG_TX; |
|
18
|
|
|
const MSG_WITNESS_BLOCK = (1 << 30) + self::MSG_BLOCK; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var int |
|
22
|
|
|
*/ |
|
23
|
|
|
private $type; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var BufferInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $hash; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
39 |
|
* @param int $type |
|
32
|
|
|
* @param BufferInterface $hash |
|
33
|
39 |
|
*/ |
|
34
|
3 |
|
public function __construct(int $type, BufferInterface $hash) |
|
35
|
|
|
{ |
|
36
|
|
|
if (!$this->checkType($type)) { |
|
37
|
36 |
|
throw new \InvalidArgumentException('Invalid type in InventoryVector'); |
|
38
|
3 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
if (32 !== $hash->getSize()) { |
|
41
|
33 |
|
throw new \InvalidArgumentException('Hash size must be 32 bytes'); |
|
42
|
33 |
|
} |
|
43
|
33 |
|
|
|
44
|
|
|
$this->type = $type; |
|
45
|
|
|
$this->hash = $hash; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
3 |
|
* @param BufferInterface $hash |
|
50
|
|
|
* @return Inventory |
|
51
|
3 |
|
*/ |
|
52
|
|
|
public static function tx(BufferInterface $hash): Inventory |
|
53
|
|
|
{ |
|
54
|
|
|
return new self(self::MSG_TX, $hash); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
6 |
|
* @param BufferInterface $hash |
|
59
|
|
|
* @return Inventory |
|
60
|
6 |
|
*/ |
|
61
|
|
|
public static function witnessTx(BufferInterface $hash): Inventory |
|
62
|
|
|
{ |
|
63
|
|
|
return new self(self::MSG_WITNESS_TX, $hash); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
3 |
|
* @param BufferInterface $hash |
|
68
|
|
|
* @return Inventory |
|
69
|
3 |
|
*/ |
|
70
|
|
|
public static function block(BufferInterface $hash): Inventory |
|
71
|
|
|
{ |
|
72
|
|
|
return new self(self::MSG_BLOCK, $hash); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
21 |
|
/** |
|
76
|
|
|
* @param BufferInterface $hash |
|
77
|
21 |
|
* @return Inventory |
|
78
|
|
|
*/ |
|
79
|
|
|
public static function witnessBlock(BufferInterface $hash): Inventory |
|
80
|
|
|
{ |
|
81
|
|
|
return new self(self::MSG_WITNESS_BLOCK, $hash); |
|
82
|
|
|
} |
|
83
|
3 |
|
|
|
84
|
|
|
/** |
|
85
|
3 |
|
* @param BufferInterface $hash |
|
86
|
|
|
* @return Inventory |
|
87
|
|
|
*/ |
|
88
|
|
|
public static function filteredBlock(BufferInterface $hash): Inventory |
|
89
|
|
|
{ |
|
90
|
|
|
return new self(self::MSG_FILTERED_BLOCK, $hash); |
|
91
|
3 |
|
} |
|
92
|
|
|
|
|
93
|
3 |
|
/** |
|
94
|
|
|
* @return int |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getType(): int |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->type; |
|
99
|
3 |
|
} |
|
100
|
|
|
|
|
101
|
3 |
|
/** |
|
102
|
|
|
* @return bool |
|
103
|
|
|
*/ |
|
104
|
|
|
public function isError(): bool |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->type === self::ERROR; |
|
107
|
3 |
|
} |
|
108
|
|
|
|
|
109
|
3 |
|
/** |
|
110
|
|
|
* @return bool |
|
111
|
|
|
*/ |
|
112
|
|
|
public function isTx(): bool |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->type === self::MSG_TX; |
|
115
|
18 |
|
} |
|
116
|
|
|
|
|
117
|
18 |
|
/** |
|
118
|
|
|
* @return bool |
|
119
|
|
|
*/ |
|
120
|
|
|
public function isWitnessTx(): bool |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->type === self::MSG_WITNESS_TX; |
|
123
|
|
|
} |
|
124
|
39 |
|
|
|
125
|
|
|
/** |
|
126
|
39 |
|
* @return bool |
|
127
|
|
|
*/ |
|
128
|
|
|
public function isBlock(): bool |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->type === self::MSG_BLOCK; |
|
131
|
|
|
} |
|
132
|
15 |
|
|
|
133
|
|
|
/** |
|
134
|
15 |
|
* @return bool |
|
135
|
|
|
*/ |
|
136
|
|
|
public function isWitnessBlock(): bool |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->type === self::MSG_WITNESS_BLOCK; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @return bool |
|
143
|
|
|
*/ |
|
144
|
|
|
public function isFilteredBlock(): bool |
|
145
|
|
|
{ |
|
146
|
|
|
return $this->type === self::MSG_FILTERED_BLOCK; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @return BufferInterface |
|
151
|
|
|
*/ |
|
152
|
|
|
public function getHash(): BufferInterface |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->hash; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @param int $type |
|
159
|
|
|
* @return bool |
|
160
|
|
|
*/ |
|
161
|
|
|
private function checkType(int $type): bool |
|
162
|
|
|
{ |
|
163
|
|
|
return in_array($type, [self::ERROR, self::MSG_TX, self::MSG_BLOCK, self::MSG_FILTERED_BLOCK]); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @return BufferInterface |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getBuffer(): BufferInterface |
|
170
|
|
|
{ |
|
171
|
|
|
return (new InventorySerializer())->serialize($this); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|