| Conditions | 1 |
| Paths | 1 |
| Total Lines | 89 |
| Code Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 185 | public function testCanReplaceIfOptin() |
||
| 186 | { |
||
| 187 | $bitcoind = $this->rpcFactory->startBitcoind(); |
||
| 188 | $this->assertTrue($bitcoind->isRunning()); |
||
| 189 | |||
| 190 | $destKey = PrivateKeyFactory::create(true); |
||
| 191 | $destSPK = ScriptFactory::scriptPubKey()->p2wkh($destKey->getPubKeyHash()); |
||
| 192 | |||
| 193 | $changeKey = PrivateKeyFactory::create(true); |
||
| 194 | $changeSPK = ScriptFactory::scriptPubKey()->p2wkh($changeKey->getPubKeyHash()); |
||
| 195 | |||
| 196 | $privateKey = PrivateKeyFactory::create(true); |
||
| 197 | $scriptPubKey = ScriptFactory::scriptPubKey()->p2wkh($privateKey->getPubKeyHash()); |
||
| 198 | $amount = 100000000; |
||
| 199 | |||
| 200 | /** @var Utxo[] $utxos */ |
||
| 201 | $utxos = [ |
||
| 202 | $bitcoind->fundOutput($amount, $scriptPubKey), |
||
| 203 | $bitcoind->fundOutput($amount, $scriptPubKey), |
||
| 204 | $bitcoind->fundOutput($amount, $scriptPubKey), |
||
| 205 | $bitcoind->fundOutput($amount, $scriptPubKey), |
||
| 206 | ]; |
||
| 207 | |||
| 208 | |||
| 209 | // Part 1: replacable tx[#1: replaceable 2] |
||
| 210 | $nIn = 1; |
||
| 211 | $tx = $this->createTransaction( |
||
| 212 | array_slice($utxos, 0, $nIn), |
||
| 213 | array_fill(0, $nIn, $privateKey), |
||
| 214 | [ |
||
| 215 | new TransactionOutput(25000000, $destSPK), |
||
| 216 | new TransactionOutput(74990000, $changeSPK), |
||
| 217 | ], |
||
| 218 | array_fill(0, $nIn, TransactionInput::SEQUENCE_FINAL - 3) |
||
| 219 | ); |
||
| 220 | |||
| 221 | $result = $bitcoind->makeRpcRequest('sendrawtransaction', [$tx->getHex()]); |
||
| 222 | $this->assertSendRawTransaction($result); |
||
| 223 | |||
| 224 | |||
| 225 | // Part 2: replace tx[#1: replaceable 1 | #2: replaceable 1] |
||
| 226 | $nIn = 2; |
||
| 227 | $tx = $this->createTransaction( |
||
| 228 | array_slice($utxos, 0, $nIn), |
||
| 229 | array_fill(0, $nIn, $privateKey), |
||
| 230 | [ |
||
| 231 | new TransactionOutput(25000000, $destSPK), |
||
| 232 | new TransactionOutput($amount + 74950000, $changeSPK), |
||
| 233 | ], |
||
| 234 | array_fill(0, $nIn, TransactionInput::SEQUENCE_FINAL - 2) |
||
| 235 | ); |
||
| 236 | |||
| 237 | $result = $bitcoind->makeRpcRequest('sendrawtransaction', [$tx->getHex()]); |
||
| 238 | $this->assertSendRawTransaction($result); |
||
| 239 | |||
| 240 | |||
| 241 | // Part 3: replace tx[#1: replaceable 0 | #2: replaceable 0 | #3: replaceable 0] |
||
| 242 | $nIn = 3; |
||
| 243 | $tx = $this->createTransaction( |
||
| 244 | array_slice($utxos, 0, $nIn), |
||
| 245 | array_fill(0, $nIn, $privateKey), |
||
| 246 | [ |
||
| 247 | new TransactionOutput(25000000, $destSPK), |
||
| 248 | new TransactionOutput((2 * $amount) + 74920000, $changeSPK), |
||
| 249 | ], |
||
| 250 | array_fill(0, $nIn, TransactionInput::SEQUENCE_FINAL - 1) |
||
| 251 | ); |
||
| 252 | |||
| 253 | $result = $bitcoind->makeRpcRequest('sendrawtransaction', [$tx->getHex()]); |
||
| 254 | $this->assertSendRawTransaction($result); |
||
| 255 | |||
| 256 | |||
| 257 | // Part 4: this one won't work, inputs are all irreplacable |
||
| 258 | $nIn = 4; |
||
| 259 | $tx = $this->createTransaction( |
||
| 260 | array_slice($utxos, 0, $nIn), |
||
| 261 | array_fill(0, $nIn, $privateKey), |
||
| 262 | [ |
||
| 263 | new TransactionOutput(25000000, $destSPK), |
||
| 264 | new TransactionOutput((3 * $amount) + 74900000, $changeSPK), |
||
| 265 | ], |
||
| 266 | array_fill(0, $nIn, TransactionInput::SEQUENCE_FINAL) |
||
| 267 | ); |
||
| 268 | |||
| 269 | $result = $bitcoind->makeRpcRequest('sendrawtransaction', [$tx->getHex()]); |
||
| 270 | $this->assertBitcoindError(RpcServer::ERROR_TX_MEMPOOL_CONFLICT, $result); |
||
| 271 | |||
| 272 | $bitcoind->destroy(); |
||
| 273 | } |
||
| 274 | } |
||
| 275 |