1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BitWasp\Bitcoin\Networking\Messages; |
6
|
|
|
|
7
|
|
|
use BitWasp\Bitcoin\Networking\Message; |
8
|
|
|
use BitWasp\Bitcoin\Networking\NetworkSerializable; |
9
|
|
|
use BitWasp\Bitcoin\Networking\Serializer\Message\RejectSerializer; |
10
|
|
|
use BitWasp\Buffertools\Buffer; |
11
|
|
|
use BitWasp\Buffertools\BufferInterface; |
12
|
|
|
|
13
|
|
|
class Reject extends NetworkSerializable |
14
|
|
|
{ |
15
|
|
|
const REJECT_MALFORMED = 0x01; |
16
|
|
|
const REJECT_INVALID = 0x10; |
17
|
|
|
const REJECT_OBSOLETE = 0x11; |
18
|
|
|
const REJECT_DUPLICATE = 0x12; |
19
|
|
|
const REJECT_NONSTANDARD = 0x40; |
20
|
|
|
const REJECT_DUST = 0x41; |
21
|
|
|
const REJECT_INSUFFICIENTFEE = 0x42; |
22
|
|
|
const REJECT_CHECKPOINT = 0x43; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var BufferInterface |
26
|
|
|
*/ |
27
|
|
|
private $message; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
private $ccode; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var BufferInterface |
36
|
|
|
*/ |
37
|
|
|
private $reason; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var BufferInterface |
41
|
|
|
*/ |
42
|
|
|
private $data; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param BufferInterface $message |
46
|
|
|
* @param int $ccode |
47
|
|
|
* @param BufferInterface $reason |
48
|
9 |
|
* @param BufferInterface|null $data - can be any data, but Bitcoin Core only uses this for a missed hash |
49
|
|
|
*/ |
50
|
|
|
public function __construct( |
51
|
|
|
BufferInterface $message, |
52
|
|
|
int $ccode, |
53
|
|
|
BufferInterface $reason, |
54
|
9 |
|
BufferInterface $data = null |
55
|
3 |
|
) { |
56
|
|
|
if (false === $this->checkCCode($ccode)) { |
57
|
|
|
throw new \InvalidArgumentException('Invalid code provided to reject message'); |
58
|
6 |
|
} |
59
|
6 |
|
|
60
|
6 |
|
$this->message = $message; |
61
|
6 |
|
$this->ccode = $ccode; |
62
|
6 |
|
$this->reason = $reason; |
63
|
|
|
$this->data = $data ?: new Buffer(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
3 |
|
* @return string |
68
|
|
|
* @see https://en.bitcoin.it/wiki/Protocol_documentation#reject |
69
|
3 |
|
*/ |
70
|
|
|
public function getNetworkCommand(): string |
71
|
|
|
{ |
72
|
|
|
return Message::REJECT; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
9 |
|
* @param int $code |
77
|
|
|
* @return bool |
78
|
9 |
|
*/ |
79
|
6 |
|
private function checkCCode(int $code): bool |
80
|
|
|
{ |
81
|
9 |
|
return in_array($code, [ |
82
|
9 |
|
self::REJECT_MALFORMED, self::REJECT_INVALID, |
83
|
9 |
|
self::REJECT_OBSOLETE, self::REJECT_DUPLICATE, |
84
|
9 |
|
self::REJECT_NONSTANDARD, self::REJECT_DUST, |
85
|
6 |
|
self::REJECT_INSUFFICIENTFEE, self::REJECT_CHECKPOINT |
86
|
9 |
|
], true); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return BufferInterface |
91
|
|
|
*/ |
92
|
3 |
|
public function getMessage(): BufferInterface |
93
|
|
|
{ |
94
|
3 |
|
return $this->message; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return int |
99
|
|
|
*/ |
100
|
3 |
|
public function getCode(): int |
101
|
|
|
{ |
102
|
3 |
|
return $this->ccode; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return BufferInterface |
107
|
|
|
*/ |
108
|
3 |
|
public function getReason(): BufferInterface |
109
|
|
|
{ |
110
|
3 |
|
return $this->reason; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return BufferInterface |
115
|
|
|
*/ |
116
|
6 |
|
public function getData(): BufferInterface |
117
|
|
|
{ |
118
|
6 |
|
return $this->data; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return BufferInterface |
123
|
|
|
*/ |
124
|
3 |
|
public function getBuffer(): BufferInterface |
125
|
|
|
{ |
126
|
3 |
|
return (new RejectSerializer())->serialize($this); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|