Completed
Pull Request — master (#489)
by thomas
73:20
created

BlockHeader::hasBip9Prefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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