Version   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 195
Duplicated Lines 0 %

Test Coverage

Coverage 90.7%

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 195
ccs 39
cts 43
cp 0.907
rs 10
c 0
b 0
f 0
wmc 14

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getStartHeight() 0 3 1
A getVersion() 0 3 1
A getServices() 0 3 1
A getUserAgent() 0 3 1
A getTimestamp() 0 3 1
A getRelay() 0 3 1
A getSenderAddress() 0 3 1
A getBuffer() 0 3 1
A getNonce() 0 3 1
A getRecipientAddress() 0 3 1
A getNetworkCommand() 0 3 1
A __construct() 0 30 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Networking\Messages;
6
7
use BitWasp\Bitcoin\Crypto\Random\Random;
8
use BitWasp\Bitcoin\Networking\Message;
9
use BitWasp\Bitcoin\Networking\NetworkSerializable;
10
use BitWasp\Bitcoin\Networking\Serializer\Message\VersionSerializer;
11
use BitWasp\Bitcoin\Networking\Serializer\Structure\NetworkAddressSerializer;
12
use BitWasp\Bitcoin\Networking\Structure\NetworkAddress;
13
use BitWasp\Bitcoin\Networking\Structure\NetworkAddressTimestamp;
14
use BitWasp\Buffertools\BufferInterface;
15
16
class Version extends NetworkSerializable
17
{
18
    /**
19
     * Identifies protocol version being used by the node
20
     * @var int
21
     */
22
    private $version;
23
24
    /**
25
     * bitfield of features to be enabled for this connection
26
     * @var int
27
     */
28
    private $services;
29
30
    /**
31
     * standard UNIX timestamp in seconds
32
     * @var int
33
     */
34
    private $timestamp;
35
36
    /**
37
     * The network address of the node receiving this message
38
     * @var NetworkAddress
39
     */
40
    private $addrRecv;
41
42
    // The fields after this require version >= 106
43
44
    /**
45
     *  The network address of the node emitting this message
46
     * @var NetworkAddress
47
     */
48
    private $addrFrom;
49
50
    /**
51
     * Node random nonce, randomly generated every time a
52
     * version packet is sent. This nonce is used to detect
53
     * connections to self.
54
     * @var int
55
     */
56
    private $nonce;
57
58
    /**
59
     * User agent
60
     * @var BufferInterface
61
     */
62
    private $userAgent;
63
64
    /**
65
     * The last block received by the emitting node
66
     * @var int
67
     */
68
    private $startHeight;
69
70
    // Fields below require version >= 70001
71
72
    /**
73
     * Whether the remote peer should announce relayed transactions or not.
74 63
     * @var bool
75
     */
76
    private $relay;
77
78
    /**
79
     * Version constructor.
80
     * @param int $version
81
     * @param int $services
82
     * @param int $timestamp
83
     * @param NetworkAddress $addrRecv
84
     * @param NetworkAddress $addrFrom
85
     * @param int $nonce
86 63
     * @param BufferInterface $userAgent
87
     * @param int $startHeight
88
     * @param bool $relay
89 63
     * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure
90
     */
91
    public function __construct(
92
        int $version,
93 63
        int $services,
94 63
        int $timestamp,
95 63
        NetworkAddress $addrRecv,
96 63
        NetworkAddress $addrFrom,
97 63
        int $nonce,
98 63
        BufferInterface $userAgent,
99 63
        int $startHeight,
100 63
        bool $relay
101 63
    ) {
102 63
103 63
        if ($addrRecv instanceof NetworkAddressTimestamp) {
104 3
            $addrRecv = $addrRecv->withoutTimestamp();
105
        }
106 60
        if ($addrFrom instanceof NetworkAddressTimestamp) {
107 60
            $addrFrom = $addrFrom->withoutTimestamp();
108
        }
109
110
        $random = new Random();
111
        $this->nonce = (int) $random->bytes(8)->getInt();
112
        $this->version = $version;
113 24
        $this->services = $services;
114
        $this->timestamp = $timestamp;
115 24
        $this->addrRecv = $addrRecv;
116
        $this->nonce = $nonce;
117
        $this->addrFrom = $addrFrom;
118
        $this->userAgent = $userAgent;
119
        $this->startHeight = $startHeight;
120
        $this->relay = $relay;
121 24
    }
122
123 24
    /**
124
     * {@inheritdoc}
125
     * @see https://en.bitcoin.it/wiki/Protocol_documentation#version
126
     * @see \BitWasp\Bitcoin\Network\NetworkSerializableInterface::getNetworkCommand()
127
     */
128
    public function getNetworkCommand(): string
129 27
    {
130
        return Message::VERSION;
131 27
    }
132
133
    /**
134
     * @return int
135
     */
136
    public function getNonce(): int
137 24
    {
138
        return $this->nonce;
139 24
    }
140
141
    /**
142
     * @return int
143
     */
144
    public function getVersion(): int
145 27
    {
146
        return $this->version;
147 27
    }
148
149
    /**
150
     * @return int
151
     */
152
    public function getServices(): int
153 30
    {
154
        return $this->services;
155 30
    }
156
157
    /**
158
     * @return int
159
     */
160
    public function getTimestamp(): int
161 39
    {
162
        return $this->timestamp;
163 39
    }
164
165
    /**
166
     * @return NetworkAddress
167
     */
168
    public function getRecipientAddress(): NetworkAddress
169 27
    {
170
        return $this->addrRecv;
171 27
    }
172
173
    /**
174
     * @return NetworkAddress
175
     */
176
    public function getSenderAddress(): NetworkAddress
177 30
    {
178
        return $this->addrFrom;
179 30
    }
180
181
    /**
182
     * @return BufferInterface
183
     */
184
    public function getUserAgent(): BufferInterface
185 27
    {
186
        return $this->userAgent;
187 27
    }
188
189
    /**
190
     * @return int
191
     */
192
    public function getStartHeight(): int
193 21
    {
194
        return $this->startHeight;
195 21
    }
196
197
    /**
198
     * @return bool
199
     */
200
    public function getRelay(): bool
201
    {
202
        return $this->relay;
203
    }
204
205
    /**
206
     * @return BufferInterface
207
     */
208
    public function getBuffer(): BufferInterface
209
    {
210
        return (new VersionSerializer(new NetworkAddressSerializer()))->serialize($this);
211
    }
212
}
213