Complex classes like Transaction often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Transaction, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Transaction extends Serializable implements TransactionInterface |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var int |
||
| 18 | */ |
||
| 19 | private $version; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var TransactionInputInterface[] |
||
| 23 | */ |
||
| 24 | private $inputs; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var TransactionOutputInterface[] |
||
| 28 | */ |
||
| 29 | private $outputs; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var ScriptWitnessInterface[] |
||
| 33 | */ |
||
| 34 | private $witness; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var int |
||
| 38 | */ |
||
| 39 | private $lockTime; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var BufferInterface |
||
| 43 | */ |
||
| 44 | private $wtxid; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var BufferInterface |
||
| 48 | */ |
||
| 49 | private $hash; |
||
| 50 | 2914 | ||
| 51 | /** |
||
| 52 | * Transaction constructor. |
||
| 53 | * |
||
| 54 | * @param int $nVersion |
||
| 55 | * @param TransactionInputInterface[] $vin |
||
| 56 | * @param TransactionOutputInterface[] $vout |
||
| 57 | 2914 | * @param ScriptWitnessInterface[] $vwit |
|
| 58 | 6 | * @param int $nLockTime |
|
| 59 | */ |
||
| 60 | public function __construct( |
||
| 61 | 2908 | $nVersion = TransactionInterface::DEFAULT_VERSION, |
|
| 62 | 6 | array $vin = [], |
|
| 63 | array $vout = [], |
||
| 64 | array $vwit = [], |
||
| 65 | 2902 | $nLockTime = 0 |
|
| 66 | 2902 | ) { |
|
| 67 | if ($nVersion < IntRange::I32_MIN || $nVersion > IntRange::I32_MAX) { |
||
| 68 | throw new \InvalidArgumentException('Transaction version is outside valid range'); |
||
| 69 | 2698 | } |
|
| 70 | 2902 | ||
| 71 | if ($nLockTime < 0 || $nLockTime > TransactionInterface::MAX_LOCKTIME) { |
||
| 72 | 2710 | throw new \InvalidArgumentException('Locktime must be positive and less than ' . TransactionInterface::MAX_LOCKTIME); |
|
| 73 | 2902 | } |
|
| 74 | 2902 | ||
| 75 | 2452 | $this->version = $nVersion; |
|
| 76 | 2902 | $this->lockTime = $nLockTime; |
|
| 77 | 2902 | ||
| 78 | $this->inputs = array_map(function (TransactionInputInterface $input) { |
||
| 79 | return $input; |
||
| 80 | }, $vin); |
||
| 81 | $this->outputs = array_map(function (TransactionOutputInterface $output) { |
||
| 82 | 304 | return $output; |
|
| 83 | }, $vout); |
||
| 84 | $this->witness = array_map(function (ScriptWitnessInterface $scriptWitness) { |
||
| 85 | return $scriptWitness; |
||
| 86 | 304 | }, $vwit); |
|
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return BufferInterface |
||
| 91 | 2380 | */ |
|
| 92 | public function getTxHash() |
||
| 93 | 2380 | { |
|
| 94 | if (null === $this->hash) { |
||
| 95 | $this->hash = Hash::sha256d($this->getBaseSerialization()); |
||
| 96 | } |
||
| 97 | return $this->hash; |
||
| 98 | } |
||
| 99 | 2380 | ||
| 100 | /** |
||
| 101 | 2380 | * @return BufferInterface |
|
| 102 | */ |
||
| 103 | public function getTxId() |
||
| 104 | { |
||
| 105 | return $this->getTxHash()->flip(); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return BufferInterface |
||
| 110 | */ |
||
| 111 | public function getWitnessTxId() |
||
| 112 | { |
||
| 113 | if (null === $this->wtxid) { |
||
| 114 | $this->wtxid = Hash::sha256d($this->getBuffer())->flip(); |
||
| 115 | 2644 | } |
|
| 116 | |||
| 117 | 2644 | return $this->wtxid; |
|
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @return int |
||
| 122 | */ |
||
| 123 | public function getVersion() |
||
| 124 | { |
||
| 125 | 2698 | return $this->version; |
|
| 126 | } |
||
| 127 | 2698 | ||
| 128 | /** |
||
| 129 | * Get the array of inputs in the transaction |
||
| 130 | * |
||
| 131 | * @return TransactionInputInterface[] |
||
| 132 | */ |
||
| 133 | public function getInputs() |
||
| 134 | 262 | { |
|
| 135 | return $this->inputs; |
||
| 136 | 262 | } |
|
| 137 | 6 | ||
| 138 | /** |
||
| 139 | 256 | * @param int $index |
|
| 140 | * @return TransactionInputInterface |
||
| 141 | */ |
||
| 142 | public function getInput($index) |
||
| 143 | { |
||
| 144 | if (!isset($this->inputs[$index])) { |
||
| 145 | throw new \RuntimeException('No input at this index'); |
||
| 146 | } |
||
| 147 | 2674 | return $this->inputs[$index]; |
|
| 148 | } |
||
| 149 | 2674 | ||
| 150 | /** |
||
| 151 | * Get Outputs |
||
| 152 | * |
||
| 153 | * @return TransactionOutputInterface[] |
||
| 154 | */ |
||
| 155 | public function getOutputs() |
||
| 156 | 2368 | { |
|
| 157 | return $this->outputs; |
||
| 158 | 2368 | } |
|
| 159 | 6 | ||
| 160 | /** |
||
| 161 | 2362 | * @param int $vout |
|
| 162 | * @return TransactionOutputInterface |
||
| 163 | */ |
||
| 164 | public function getOutput($vout) |
||
| 165 | { |
||
| 166 | if (!isset($this->outputs[$vout])) { |
||
| 167 | throw new \RuntimeException('No output at this index'); |
||
| 168 | } |
||
| 169 | return $this->outputs[$vout]; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @return bool |
||
| 174 | */ |
||
| 175 | public function hasWitness() |
||
| 176 | { |
||
| 177 | for ($l = count($this->inputs), $i = 0; $i < $l; $i++) { |
||
| 178 | if (isset($this->witness[$i]) && count($this->witness[$i]) > 0) { |
||
| 179 | return true; |
||
| 180 | } |
||
| 181 | 322 | } |
|
| 182 | |||
| 183 | 322 | return false; |
|
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @return ScriptWitnessInterface[] |
||
| 188 | */ |
||
| 189 | public function getWitnesses() |
||
| 190 | 50 | { |
|
| 191 | return $this->witness; |
||
| 192 | 50 | } |
|
| 193 | |||
| 194 | /** |
||
| 195 | 50 | * @param int $index |
|
| 196 | * @return ScriptWitnessInterface |
||
| 197 | */ |
||
| 198 | public function getWitness($index) |
||
| 199 | { |
||
| 200 | if (!isset($this->witness[$index])) { |
||
| 201 | throw new \RuntimeException('No witness at this index'); |
||
| 202 | 2296 | } |
|
| 203 | return $this->witness[$index]; |
||
| 204 | 2296 | } |
|
| 205 | 2296 | ||
| 206 | /** |
||
| 207 | * @param int $vout |
||
| 208 | * @return OutPointInterface |
||
| 209 | */ |
||
| 210 | public function makeOutpoint($vout) |
||
| 211 | { |
||
| 212 | $this->getOutput($vout); |
||
| 213 | return new OutPoint($this->getTxId(), $vout); |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param int $vout |
||
| 218 | * @return Utxo |
||
| 219 | */ |
||
| 220 | public function makeUtxo($vout) |
||
| 221 | { |
||
| 222 | 2650 | return new Utxo(new OutPoint($this->getTxId(), $vout), $this->getOutput($vout)); |
|
| 223 | } |
||
| 224 | 2650 | ||
| 225 | /** |
||
| 226 | * Get Lock Time |
||
| 227 | * |
||
| 228 | * @return int |
||
| 229 | */ |
||
| 230 | 6 | public function getLockTime() |
|
| 231 | { |
||
| 232 | 6 | return $this->lockTime; |
|
| 233 | 6 | } |
|
| 234 | 6 | ||
| 235 | 6 | /** |
|
| 236 | 3 | * @return int|string |
|
| 237 | */ |
||
| 238 | 6 | public function getValueOut() |
|
| 239 | { |
||
| 240 | $math = Bitcoin::getMath(); |
||
| 241 | $value = gmp_init(0); |
||
| 242 | foreach ($this->outputs as $output) { |
||
| 243 | $value = $math->add($value, gmp_init($output->getValue())); |
||
| 244 | 6 | } |
|
| 245 | |||
| 246 | 6 | return gmp_strval($value, 10); |
|
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @return bool |
||
| 251 | */ |
||
| 252 | public function isCoinbase() |
||
| 253 | 6 | { |
|
| 254 | return count($this->inputs) === 1 && $this->getInput(0)->isCoinBase(); |
||
| 255 | 6 | } |
|
| 256 | 6 | ||
| 257 | 6 | /** |
|
| 258 | * @param TransactionInterface $tx |
||
| 259 | * @return bool |
||
| 260 | 6 | */ |
|
| 261 | 6 | public function equals(TransactionInterface $tx) |
|
| 262 | 6 | { |
|
| 263 | $version = gmp_cmp($this->version, $tx->getVersion()); |
||
| 264 | if ($version !== 0) { |
||
| 265 | 6 | return false; |
|
| 266 | 6 | } |
|
| 267 | |||
| 268 | $nIn = count($this->inputs); |
||
| 269 | $nOut = count($this->outputs); |
||
| 270 | 6 | $nWit = count($this->witness); |
|
| 271 | 6 | ||
| 272 | 6 | // Check the length of each field is equal |
|
| 273 | if ($nIn !== count($tx->getInputs()) || $nOut !== count($tx->getOutputs()) || $nWit !== count($tx->getWitnesses())) { |
||
| 274 | 3 | return false; |
|
| 275 | } |
||
| 276 | 6 | ||
| 277 | 6 | // Check each field |
|
| 278 | 6 | for ($i = 0; $i < $nIn; $i++) { |
|
| 279 | if (false === $this->getInput($i)->equals($tx->getInput($i))) { |
||
| 280 | 3 | return false; |
|
| 281 | } |
||
| 282 | 6 | } |
|
| 283 | |||
| 284 | for ($i = 0; $i < $nOut; $i++) { |
||
| 285 | if (false === $this->getOutput($i)->equals($tx->getOutput($i))) { |
||
| 286 | return false; |
||
| 287 | } |
||
| 288 | 6 | } |
|
| 289 | |||
| 290 | for ($i = 0; $i < $nWit; $i++) { |
||
| 291 | if (false === $this->getWitness($i)->equals($tx->getWitness($i))) { |
||
| 292 | return false; |
||
| 293 | } |
||
| 294 | 2590 | } |
|
| 295 | |||
| 296 | 2590 | return gmp_cmp($this->lockTime, $tx->getLockTime()) === 0; |
|
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @return BufferInterface |
||
| 301 | */ |
||
| 302 | 66 | public function getBuffer() |
|
| 306 | |||
| 307 | /** |
||
| 308 | * @return BufferInterface |
||
| 309 | */ |
||
| 310 | public function getBaseSerialization() |
||
| 311 | { |
||
| 312 | return (new TransactionSerializer())->serialize($this, TransactionSerializer::NO_WITNESS); |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @return BufferInterface |
||
| 317 | */ |
||
| 318 | public function getWitnessSerialization() |
||
| 319 | { |
||
| 320 | if (!$this->hasWitness()) { |
||
| 321 | throw new \RuntimeException('Cannot get witness serialization for transaction without witnesses'); |
||
| 322 | } |
||
| 323 | |||
| 324 | return $this->getBuffer(); |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * {@inheritdoc} |
||
| 329 | * @see TransactionInterface::getWitnessBuffer() |
||
| 330 | * @see TransactionInterface::getWitnessSerialization() |
||
| 331 | * @deprecated |
||
| 332 | */ |
||
| 333 | public function getWitnessBuffer() |
||
| 337 | } |
||
| 338 |