Completed
Push — master ( d80e29...4963c9 )
by thomas
26:06
created

BlockHeader::getNonce()   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
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace BitWasp\Bitcoin\Block;
4
5
use BitWasp\Buffertools\Buffer;
6
use BitWasp\Bitcoin\Crypto\Hash;
7
use BitWasp\Bitcoin\Serializable;
8
use BitWasp\Bitcoin\Serializer\Block\BlockHeaderSerializer;
9
use BitWasp\Buffertools\BufferInterface;
10
use BitWasp\CommonTrait\FunctionAliasArrayAccess;
11
12
class BlockHeader extends Serializable implements BlockHeaderInterface
13
{
14
    use FunctionAliasArrayAccess;
15
16
    /**
17
     * @var int|string
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|string
33
     */
34
    private $timestamp;
35
36
    /**
37
     * @var BufferInterface
38
     */
39
    private $bits;
40
41
    /**
42
     * @var int|string
43
     */
44
    private $nonce;
45
46
    /**
47
     * @param int|string $version
48
     * @param BufferInterface $prevBlock
49
     * @param BufferInterface $merkleRoot
50
     * @param int|string $timestamp
51
     * @param BufferInterface $bits
52
     * @param int|string $nonce
53
     */
54 150
    public function __construct($version, BufferInterface $prevBlock, BufferInterface $merkleRoot, $timestamp, BufferInterface $bits, $nonce)
55
    {
56 150
        if ($prevBlock->getSize() !== 32) {
57 6
            throw new \InvalidArgumentException('Block header prevBlock must be a 32-byte Buffer');
58
        }
59
60 144
        if ($merkleRoot->getSize() !== 32) {
61
            throw new \InvalidArgumentException('Block header prevBlock must be a 32-byte Buffer');
62
        }
63
64 144
        $this->version = $version;
65 144
        $this->prevBlock = $prevBlock;
66 144
        $this->merkleRoot = $merkleRoot;
67 144
        $this->timestamp = $timestamp;
68 144
        $this->bits = $bits;
69 144
        $this->nonce = $nonce;
70
71 144
        $this
72 144
            ->initFunctionAlias('version', 'getVersion')
73 144
            ->initFunctionAlias('prevBlock', 'getPrevBlock')
74 144
            ->initFunctionAlias('merkleRoot', 'getMerkleRoot')
75 144
            ->initFunctionAlias('timestamp', 'getTimestamp')
76 144
            ->initFunctionAlias('bits', 'getBits')
77 144
            ->initFunctionAlias('nonce', 'getNonce');
78 144
    }
79
80
    /**
81
     * @return BufferInterface
82
     */
83 1158
    public function getHash()
84
    {
85 1158
        return Hash::sha256d($this->getBuffer())->flip();
86
    }
87
88
    /**
89
     * Get the version for this block
90
     *
91
     * {@inheritdoc}
92
     * @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getVersion()
93
     */
94 1218
    public function getVersion()
95
    {
96 1218
        return $this->version;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     * @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getPrevBlock()
102
     */
103 1212
    public function getPrevBlock()
104
    {
105 1212
        return $this->prevBlock;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     * @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getMerkleRoot()
111
     */
112 1230
    public function getMerkleRoot()
113
    {
114 1230
        return $this->merkleRoot;
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     * @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getBits()
120
     */
121 1206
    public function getBits()
122
    {
123 1206
        return $this->bits;
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     * @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getNonce()
129
     */
130 1212
    public function getNonce()
131
    {
132 1212
        return $this->nonce;
133
    }
134
135
    /**
136
     * Get the timestamp for this block
137
     *
138
     * {@inheritdoc}
139
     * @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getTimestamp()
140
     */
141 1212
    public function getTimestamp()
142
    {
143 1212
        return $this->timestamp;
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     * @see \BitWasp\Buffertools\SerializableInterface::getBuffer()
149
     */
150 1164
    public function getBuffer()
151
    {
152 1164
        return (new BlockHeaderSerializer())->serialize($this);
153
    }
154
}
155