Completed
Pull Request — master (#313)
by thomas
74:13 queued 03:37
created

Locktime::toBlockHeight()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 3
cts 3
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin;
4
5
class Locktime
6
{
7
    const INT_MAX = 0xffffffff;
8
9
    /**
10
     * Maximum block height that can be used in locktime, as beyond
11
     * this is reserved for timestamp locktimes
12
     */
13
    const BLOCK_MAX = 500000000;
14
15
    /**
16
     * Maximum timestamp that can be encoded in locktime
17
     * (TIME_MAX + BLOCK_MAX = INT_MAX)
18
     */
19
    const TIME_MAX = self::INT_MAX - self::BLOCK_MAX;
20
21
    /**
22
     * Convert a $timestamp to a locktime.
23
     * Max timestamp is 3794967296 - 04/04/2090 @ 5:34am (UTC)
24
     *
25
     * @param int $timestamp
26
     * @return int
27
     * @throws \Exception
28
     */
29
    public function fromTimestamp($timestamp)
30
    {
31
        if ($timestamp > self::TIME_MAX) {
32
            throw new \Exception('Timestamp out of range');
33
        }
34
35 40
        $locktime = self::BLOCK_MAX + $timestamp;
36
        return $locktime;
37 40
    }
38 40
39
    /**
40
     * Convert a lock time to the timestamp it's locked to.
41
     * Throws an exception when:
42
     *  - Lock time appears to be in the block locktime range ( < Locktime::BLOCK_MAX )
43
     *  - When the lock time exceeds the max possible lock time ( > Locktime::INT_MAX )
44
     *
45
     * @param int $lockTime
46
     * @return int
47
     * @throws \Exception
48 12
     */
49
    public function toTimestamp($lockTime)
50 12
    {
51 4
        if ($lockTime <= self::BLOCK_MAX) {
52
            throw new \Exception('Lock time out of range for timestamp');
53
        }
54 12
55 12
        if ($lockTime > self::INT_MAX) {
56
            throw new \Exception('Lock time too large');
57
        }
58
59
        $timestamp = $lockTime - self::BLOCK_MAX;
60
        return $timestamp;
61
    }
62
63
    /**
64
     * Convert $blockHeight to lock time. Doesn't convert anything really,
65
     * but does check the bounds of the given block height.
66
     *
67
     * @param int $blockHeight
68 12
     * @return int
69
     * @throws \Exception
70 12
     */
71 4
    public function fromBlockHeight($blockHeight)
72
    {
73
        if ($blockHeight > self::BLOCK_MAX) {
74 8
            throw new \Exception('This block height is too high');
75 4
        }
76
77
        return $blockHeight;
78 8
    }
79 8
80
    /**
81
     * Convert locktime to block height tx is locked to. Doesn't convert anything
82
     * really, but does check the bounds of the supplied locktime.
83
     *
84
     * @param int $lockTime
85
     * @return int
86
     * @throws \Exception
87
     */
88
    public function toBlockHeight($lockTime)
89
    {
90 12
        if ($lockTime >= self::BLOCK_MAX) {
91
            throw new \Exception('This locktime is out of range for a block height');
92 12
        }
93 4
94
        return $lockTime;
95
    }
96
}
97