Completed
Push — master ( cf8140...31933a )
by thomas
24:21
created

TransactionInput::isLockedToBlock()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Transaction;
4
5
use BitWasp\Bitcoin\Bitcoin;
6
use BitWasp\Bitcoin\Script\ScriptInterface;
7
use BitWasp\Bitcoin\Serializable;
8
use BitWasp\Bitcoin\Serializer\Transaction\OutPointSerializer;
9
use BitWasp\Bitcoin\Serializer\Transaction\TransactionInputSerializer;
10
use BitWasp\Buffertools\BufferInterface;
11
12
class TransactionInput extends Serializable implements TransactionInputInterface
13
{
14
15
    /**
16
     * @var OutPointInterface
17
     */
18
    private $outPoint;
19
20
    /**
21
     * @var ScriptInterface
22
     */
23
    private $script;
24
25
    /**
26
     * @var string|int
27
     */
28
    private $sequence;
29
30
    /**
31
     * @param OutPointInterface $outPoint
32
     * @param ScriptInterface $script
33
     * @param int $sequence
34
     */
35 1370
    public function __construct(OutPointInterface $outPoint, ScriptInterface $script, $sequence = self::SEQUENCE_FINAL)
36
    {
37 1370
        $this->outPoint = $outPoint;
38 1370
        $this->script = $script;
39 1370
        $this->sequence = $sequence;
40 1370
    }
41
42
    /**
43
     * @return OutPointInterface
44
     */
45 1338
    public function getOutPoint()
46
    {
47 1338
        return $this->outPoint;
48
    }
49
50
    /**
51
     * @return ScriptInterface
52
     */
53 1366
    public function getScript()
54
    {
55 1366
        return $this->script;
56
    }
57
58
    /**
59
     * @return int
60
     */
61 1334
    public function getSequence()
62
    {
63 1334
        return $this->sequence;
64
    }
65
66
    /**
67
     * @param TransactionInputInterface $other
68
     * @return bool
69
     */
70 16
    public function equals(TransactionInputInterface $other)
71
    {
72 16
        if (!$this->outPoint->equals($other->getOutPoint())) {
73 4
            return false;
74
        }
75
76 16
        if (!$this->script->equals($other->getScript())) {
77 2
            return false;
78
        }
79
80 16
        return gmp_cmp(gmp_init($this->sequence), gmp_init($other->getSequence())) === 0;
81
    }
82
83
    /**
84
     * Check whether this transaction is a Coinbase transaction
85
     *
86
     * @return boolean
87
     */
88 8
    public function isCoinbase()
89
    {
90 8
        $outpoint = $this->outPoint;
91 8
        return $outpoint->getTxId()->getBinary() === str_pad('', 32, "\x00")
92 8
            && $outpoint->getVout() == 0xffffffff;
93
    }
94
95
    /**
96
     * @return bool
97
     */
98 2
    public function isFinal()
99
    {
100 2
        $math = Bitcoin::getMath();
101 2
        return $math->cmp(gmp_init($this->getSequence()), gmp_init(self::SEQUENCE_FINAL)) === 0;
102
    }
103
104
    /**
105
     * @return int
106
     */
107 4
    public function isSequenceLockDisabled()
108
    {
109 4
        if ($this->isCoinbase()) {
110 2
            return true;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return true; (boolean) is incompatible with the return type declared by the interface BitWasp\Bitcoin\Transact...:isSequenceLockDisabled of type integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
111
        }
112
113 4
        return ($this->sequence & self::SEQUENCE_LOCKTIME_DISABLE_FLAG) !== 0;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return ($this->sequence ...ME_DISABLE_FLAG) !== 0; (boolean) is incompatible with the return type declared by the interface BitWasp\Bitcoin\Transact...:isSequenceLockDisabled of type integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
114
    }
115
116
    /**
117
     * @return bool
118
     */
119 2
    public function isLockedToTime()
120
    {
121 2
        return !$this->isSequenceLockDisabled() && (($this->sequence & self::SEQUENCE_LOCKTIME_TYPE_FLAG) === self::SEQUENCE_LOCKTIME_TYPE_FLAG);
122
    }
123
124
    /**
125
     * @return bool
126
     */
127 2
    public function isLockedToBlock()
128
    {
129 2
        return !$this->isSequenceLockDisabled() && (($this->sequence & self::SEQUENCE_LOCKTIME_TYPE_FLAG) === 0);
130
    }
131
132
    /**
133
     * @return int
134
     */
135 2
    public function getRelativeTimeLock()
136
    {
137 2
        if (!$this->isLockedToTime()) {
138
            throw new \RuntimeException('Cannot decode time based locktime when disable flag set/timelock flag unset/tx is coinbase');
139
        }
140
141
        // Multiply by 512 to convert locktime to seconds
142 2
        return ($this->sequence & self::SEQUENCE_LOCKTIME_MASK) * 512;
143
    }
144
145
    /**
146
     * @return int
147
     */
148 2
    public function getRelativeBlockLock()
149
    {
150 2
        if (!$this->isLockedToBlock()) {
151
            throw new \RuntimeException('Cannot decode block locktime when disable flag set/timelock flag set/tx is coinbase');
152
        }
153
154 2
        return $this->sequence & self::SEQUENCE_LOCKTIME_MASK;
155
    }
156
157
    /**
158
     * @return BufferInterface
159
     */
160 2
    public function getBuffer()
161
    {
162 2
        return (new TransactionInputSerializer(new OutPointSerializer()))->serialize($this);
163
    }
164
}
165