Completed
Pull Request — master (#278)
by thomas
73:26 queued 01:49
created

TransactionInput::getSequence()   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 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
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\Transaction;
4
5
use BitWasp\Bitcoin\Bitcoin;
6
use BitWasp\Bitcoin\Serializer\Transaction\OutPointSerializer;
7
use BitWasp\Bitcoin\Script\Script;
8
use BitWasp\Bitcoin\Script\ScriptInterface;
9
use BitWasp\Bitcoin\Serializable;
10
use BitWasp\Bitcoin\Serializer\Transaction\TransactionInputSerializer;
11
use BitWasp\Buffertools\BufferInterface;
12
use BitWasp\CommonTrait\FunctionAliasArrayAccess;
13
14
class TransactionInput extends Serializable implements TransactionInputInterface
15
{
16
    use FunctionAliasArrayAccess;
17
18
    /**
19
     * @var OutPointInterface
20
     */
21
    private $outPoint;
22
23
    /**
24
     * @var ScriptInterface
25
     */
26
    private $script;
27
28
    /**
29
     * @var string|int
30
     */
31
    private $sequence;
32
33
    /**
34
     * @param OutPointInterface $outPoint
35
     * @param ScriptInterface $script
36
     * @param int $sequence
37
     */
38 1164
    public function __construct(OutPointInterface $outPoint, ScriptInterface $script, $sequence = self::SEQUENCE_FINAL)
39
    {
40
        $this->outPoint = $outPoint;
41 1164
        $this->script = $script;
42 1164
        $this->sequence = $sequence;
43 1164
44
        $this
45 1164
            ->initFunctionAlias('outpoint', 'getOutPoint')
46 1164
            ->initFunctionAlias('script', 'getScript')
47 1164
            ->initFunctionAlias('sequence', 'getSequence');
48 1164
49
    }
50 1164
51
    /**
52
     * @return TransactionInput
53
     */
54
    public function __clone()
55
    {
56
        $this->script = clone $this->script;
57
    }
58
59
    /**
60
     * @return OutPointInterface
61
     */
62
    public function getOutPoint()
63 360
    {
64
        return $this->outPoint;
65 360
    }
66
67
    /**
68
     * @return Script
69
     */
70
    public function getScript()
71 1020
    {
72
        return $this->script;
73 1020
    }
74
75
    /**
76
     * @return int
77
     */
78
    public function getSequence()
79 336
    {
80
        return $this->sequence;
81 336
    }
82
83
    /**
84
     * @param TransactionInputInterface $input
85
     * @return bool
86
     */
87
    public function equals(TransactionInputInterface $input)
88
    {
89 12
        $outPoint = $this->outPoint->equals($input->getOutPoint());
90
        if (!$outPoint) {
91 12
            return false;
92 12
        }
93 12
94 12
        $script = $this->script->equals($input->getScript());
95
        if (!$script) {
96
            return false;
97
        }
98
99
        return gmp_cmp($this->sequence, $input->getSequence()) === 0;
100 6
    }
101
102 6
    /**
103 6
     * Check whether this transaction is a Coinbase transaction
104
     *
105
     * @return boolean
106
     */
107
    public function isCoinbase()
108
    {
109 294
        $math = Bitcoin::getMath();
110
        $outpoint = $this->outPoint;
111 294
        return $outpoint->getTxId()->getBinary() === str_pad('', 32, "\x00")
112
            && $math->cmp($outpoint->getVout(), $math->hexDec('ffffffff')) === 0;
113
    }
114
115
    /**
116
     * @return bool
117
     */
118
    public function isFinal()
119
    {
120
        $math = Bitcoin::getMath();
121
        return $math->cmp($this->getSequence(), self::SEQUENCE_FINAL) === 0;
122
    }
123
124
    /**
125
     * @return BufferInterface
126
     */
127
    public function getBuffer()
128
    {
129
        return (new TransactionInputSerializer(new OutPointSerializer()))->serialize($this);
130
    }
131
}
132