Completed
Pull Request — master (#618)
by thomas
61:26 queued 48:39
created

BlockHeader::getBits()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Block;
6
7
use BitWasp\Bitcoin\Crypto\Hash;
8
use BitWasp\Bitcoin\Exceptions\InvalidHashLengthException;
9
use BitWasp\Bitcoin\Serializable;
10
use BitWasp\Bitcoin\Serializer\Block\BlockHeaderSerializer;
11
use BitWasp\Buffertools\BufferInterface;
12
13
class BlockHeader extends Serializable implements BlockHeaderInterface
14
{
15
16
    /**
17
     * @var int
18
     */
19
    private $version;
20
21
    /**
22
     * @var BufferInterface
23
     */
24
    private $prevBlock;
25
26
    /**
27
     * @var BufferInterface
28
     */
29
    private $merkleRoot;
30
31
    /**
32
     * @var int
33
     */
34
    private $timestamp;
35
36
    /**
37
     * @var int
38
     */
39
    private $bits;
40
41
    /**
42
     * @var int
43
     */
44
    private $nonce;
45
46
    const BIP9_PREFIX = 1 << 29;
47
48
    /**
49
     * @param int $version
50
     * @param BufferInterface $prevBlock
51
     * @param BufferInterface $merkleRoot
52
     * @param int $timestamp
53
     * @param int $bits
54
     * @param int $nonce
55
     */
56 25
    public function __construct(int $version, BufferInterface $prevBlock, BufferInterface $merkleRoot, int $timestamp, int $bits, int $nonce)
57
    {
58 25
        if ($prevBlock->getSize() !== 32) {
59 1
            throw new InvalidHashLengthException('BlockHeader prevBlock must be a 32-byte Buffer');
60
        }
61
62 24
        if ($merkleRoot->getSize() !== 32) {
63 1
            throw new InvalidHashLengthException('BlockHeader merkleRoot must be a 32-byte Buffer');
64
        }
65
66 23
        $this->version = $version;
67 23
        $this->prevBlock = $prevBlock;
68 23
        $this->merkleRoot = $merkleRoot;
69 23
        $this->timestamp = $timestamp;
70 23
        $this->bits = $bits;
71 23
        $this->nonce = $nonce;
72 23
    }
73
74
    /**
75
     * @return BufferInterface
76
     */
77 191
    public function getHash(): BufferInterface
78
    {
79 191
        return Hash::sha256d($this->getBuffer())->flip();
80
    }
81
82
    /**
83
     * Get the version for this block
84
     *
85
     * {@inheritdoc}
86
     * @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getVersion()
87
     */
88 201
    public function getVersion(): int
89
    {
90 201
        return $this->version;
91
    }
92
93
    /**
94
     * @return bool
95
     */
96 16
    public function hasBip9Prefix(): bool
97
    {
98 16
        return ($this->version & self::BIP9_PREFIX) != 0;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     * @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getPrevBlock()
104
     */
105 201
    public function getPrevBlock(): BufferInterface
106
    {
107 201
        return $this->prevBlock;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     * @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getMerkleRoot()
113
     */
114 204
    public function getMerkleRoot(): BufferInterface
115
    {
116 204
        return $this->merkleRoot;
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     * @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getBits()
122
     */
123 200
    public function getBits(): int
124
    {
125 200
        return $this->bits;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     * @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getNonce()
131
     */
132 201
    public function getNonce(): int
133
    {
134 201
        return $this->nonce;
135
    }
136
137
    /**
138
     * Get the timestamp for this block
139
     *
140
     * {@inheritdoc}
141
     * @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getTimestamp()
142
     */
143 201
    public function getTimestamp(): int
144
    {
145 201
        return $this->timestamp;
146
    }
147
148
    /**
149
     * @param BlockHeaderInterface $other
150
     * @return bool
151
     */
152 1
    public function equals(BlockHeaderInterface $other): bool
153
    {
154 1
        return $this->version === $other->getVersion()
155 1
            && $this->prevBlock->equals($other->getPrevBlock())
156 1
            && $this->merkleRoot->equals($other->getMerkleRoot())
157 1
            && $this->timestamp === $other->getTimestamp()
158 1
            && $this->bits === $other->getBits()
159 1
            && $this->nonce === $other->getNonce();
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     * @see \BitWasp\Buffertools\SerializableInterface::getBuffer()
165
     */
166 192
    public function getBuffer(): BufferInterface
167
    {
168 192
        return (new BlockHeaderSerializer())->serialize($this);
169
    }
170
}
171