Complex classes like SizeEstimation 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 SizeEstimation, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class SizeEstimation |
||
| 15 | { |
||
| 16 | const SIZE_DER_SIGNATURE = 72; |
||
| 17 | const SIZE_V0_P2WSH = 36; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @param bool $compressed |
||
| 21 | * @return int |
||
| 22 | */ |
||
| 23 | public static function getPublicKeySize($compressed = true) { |
||
| 24 | return $compressed ? PublicKeyInterface::LENGTH_COMPRESSED : PublicKeyInterface::LENGTH_UNCOMPRESSED; |
||
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @param int $length |
||
| 29 | * @return int |
||
| 30 | */ |
||
| 31 | public static function getLengthOfScriptLengthElement($length) { |
||
| 32 | if ($length < 75) { |
||
| 33 | return 1; |
||
| 34 | } else if ($length <= 0xff) { |
||
| 35 | return 2; |
||
| 36 | } else if ($length <= 0xffff) { |
||
| 37 | return 3; |
||
| 38 | } else if ($length <= 0xffffffff) { |
||
| 39 | return 5; |
||
| 40 | } else { |
||
| 41 | throw new \RuntimeException('Size of pushdata too large'); |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param int $length |
||
| 47 | * @return int |
||
| 48 | */ |
||
| 49 | public static function getLengthOfVarInt($length) { |
||
| 50 | if ($length < 253) { |
||
| 51 | return 1; |
||
| 52 | } else if ($length < 65535) { |
||
| 53 | $num_bytes = 2; |
||
| 54 | } else if ($length < 4294967295) { |
||
| 55 | $num_bytes = 4; |
||
| 56 | } else if ($length < 18446744073709551615) { |
||
| 57 | $num_bytes = 8; |
||
| 58 | } else { |
||
| 59 | throw new \InvalidArgumentException("Invalid decimal"); |
||
| 60 | } |
||
| 61 | return 1 + $num_bytes; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param array $vectorSizes |
||
| 66 | * @return int|mixed |
||
| 67 | */ |
||
| 68 | public static function getLengthOfVector(array $vectorSizes) { |
||
| 69 | $vectorSize = self::getLengthOfVarInt(count($vectorSizes)); |
||
| 70 | foreach ($vectorSizes as $size) { |
||
| 71 | $vectorSize += self::getLengthOfVarInt($size) + $size; |
||
| 72 | } |
||
| 73 | return $vectorSize; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param Multisig $multisig |
||
| 78 | * @return array - first is array of stack sizes, second is script len |
||
| 79 | */ |
||
| 80 | public static function estimateMultisigStackSize(Multisig $multisig) { |
||
| 81 | $stackSizes = [0]; |
||
| 82 | for ($i = 0; $i < $multisig->getRequiredSigCount(); $i++) { |
||
|
|
|||
| 83 | $stackSizes[] = self::SIZE_DER_SIGNATURE; |
||
| 84 | } |
||
| 85 | |||
| 86 | $scriptSize = 1; // OP_$m |
||
| 87 | $keys = $multisig->getKeyBuffers(); |
||
| 88 | for ($i = 0, $n = $multisig->getKeyCount(); $i < $n; $i++) { |
||
| 89 | $scriptSize += 1 + $keys[$i]->getSize(); |
||
| 90 | } |
||
| 91 | |||
| 92 | $scriptSize += 1 + 1; // OP_$n OP_CHECKMULTISIG |
||
| 93 | return [$stackSizes, $scriptSize]; |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param PayToPubKey $info |
||
| 98 | * @return array - first is array of stack sizes, second is script len |
||
| 99 | */ |
||
| 100 | public static function estimateP2PKStackSize(PayToPubKey $info) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param bool $isCompressed |
||
| 110 | * @return array - first is array of stack sizes, second is script len |
||
| 111 | */ |
||
| 112 | public static function estimateP2PKHStackSize($isCompressed = true) { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param array $stackSizes - array of integer size of a value (for scriptSig or witness) |
||
| 122 | * @param bool $isWitness |
||
| 123 | * @param ScriptInterface $redeemScript |
||
| 124 | * @param ScriptInterface $witnessScript |
||
| 125 | * @return array |
||
| 126 | */ |
||
| 127 | public static function estimateSizeForStack(array $stackSizes, $isWitness, ScriptInterface $redeemScript = null, ScriptInterface $witnessScript = null) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param ScriptInterface $script - not the scriptPubKey, might be SPK,RS,WS |
||
| 169 | * @param ScriptInterface|null $redeemScript |
||
| 170 | * @param ScriptInterface $witnessScript |
||
| 171 | * @param bool $isWitness |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | public static function estimateInputFromScripts(ScriptInterface $script, ScriptInterface $redeemScript = null, ScriptInterface $witnessScript = null, $isWitness) { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param UTXO $utxo |
||
| 193 | * @return array |
||
| 194 | */ |
||
| 195 | public static function estimateUtxo(UTXO $utxo) { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param ScriptInterface $scriptPubKey |
||
| 201 | * @param ScriptInterface $redeemScript |
||
| 202 | * @param ScriptInterface $witnessScript |
||
| 203 | * @return array |
||
| 204 | */ |
||
| 205 | public static function estimateUtxoFromScripts( |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param UTXO[] $utxos |
||
| 247 | * @param bool $withWitness |
||
| 248 | * @return integer |
||
| 249 | */ |
||
| 250 | public static function estimateInputsSize(array $utxos, $withWitness) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param array $outputs |
||
| 272 | * @return int |
||
| 273 | */ |
||
| 274 | public static function estimateOutputsSize(array $outputs) { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @param UTXO[] $utxos |
||
| 302 | * @param array $outputs |
||
| 303 | * @return int |
||
| 304 | */ |
||
| 305 | public static function estimateVsize(array $utxos, array $outputs) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param UTXO[] $utxos |
||
| 311 | * @param array $outputs |
||
| 312 | * @return int |
||
| 313 | */ |
||
| 314 | public static function estimateWeight(array $utxos, array $outputs) { |
||
| 328 | } |
||
| 329 |
If you have a function call in the test part of a
forloop, this function is executed on each iteration. Often such a function, can be moved to the initialization part and be cached.