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
|
1368 |
|
public function __construct(OutPointInterface $outPoint, ScriptInterface $script, $sequence = self::SEQUENCE_FINAL) |
36
|
|
|
{ |
37
|
1368 |
|
$this->outPoint = $outPoint; |
38
|
1368 |
|
$this->script = $script; |
39
|
1368 |
|
$this->sequence = $sequence; |
40
|
1368 |
|
} |
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
|
4 |
|
public function isCoinbase() |
89
|
|
|
{ |
90
|
4 |
|
$outpoint = $this->outPoint; |
91
|
4 |
|
return $outpoint->getTxId()->getBinary() === str_pad('', 32, "\x00") |
92
|
4 |
|
&& $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
|
2 |
|
public function isSequenceLockDisabled() |
108
|
|
|
{ |
109
|
2 |
|
if ($this->isCoinbase()) { |
110
|
|
|
return true; |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return ($this->sequence & self::SEQUENCE_LOCKTIME_DISABLE_FLAG) !== 0; |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return bool |
118
|
|
|
*/ |
119
|
|
|
public function isLockedToTime() |
120
|
|
|
{ |
121
|
|
|
return !$this->isSequenceLockDisabled() && (($this->sequence & self::SEQUENCE_LOCKTIME_TYPE_FLAG) === self::SEQUENCE_LOCKTIME_TYPE_FLAG); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
|
|
public function isLockedToBlock() |
128
|
|
|
{ |
129
|
|
|
return !$this->isSequenceLockDisabled() && (($this->sequence & self::SEQUENCE_LOCKTIME_TYPE_FLAG) === 0); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return int |
134
|
|
|
*/ |
135
|
|
|
public function getRelativeTimeLock() |
136
|
|
|
{ |
137
|
|
|
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
|
|
|
return ($this->sequence & self::SEQUENCE_LOCKTIME_MASK) * 512; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return int |
147
|
|
|
*/ |
148
|
|
|
public function getRelativeBlockLock() |
149
|
|
|
{ |
150
|
|
|
if (!$this->isLockedToBlock()) { |
151
|
|
|
throw new \RuntimeException('Cannot decode block locktime when disable flag set/timelock flag set/tx is coinbase'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $this->sequence & self::SEQUENCE_LOCKTIME_MASK; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return BufferInterface |
159
|
|
|
*/ |
160
|
|
|
public function getBuffer() |
161
|
|
|
{ |
162
|
|
|
return (new TransactionInputSerializer(new OutPointSerializer()))->serialize($this); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
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:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.