Conditions | 1 |
Paths | 1 |
Total Lines | 52 |
Code Lines | 33 |
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 |
||
96 | public function testCanReplaceTransactionOnce() |
||
97 | { |
||
98 | $bitcoind = $this->rpcFactory->startBitcoind(); |
||
99 | |||
100 | $this->assertTrue($bitcoind->isRunning()); |
||
101 | |||
102 | $destKey = PrivateKeyFactory::create(true); |
||
103 | $destSPK = ScriptFactory::scriptPubKey()->p2wkh($destKey->getPubKeyHash()); |
||
104 | |||
105 | $changeKey = PrivateKeyFactory::create(true); |
||
106 | $changeSPK = ScriptFactory::scriptPubKey()->p2wkh($changeKey->getPubKeyHash()); |
||
107 | |||
108 | $privateKey = PrivateKeyFactory::create(true); |
||
109 | $scriptPubKey = ScriptFactory::scriptPubKey()->p2wkh($privateKey->getPubKeyHash()); |
||
110 | $amount = 100000000; |
||
111 | |||
112 | /** @var Utxo[] $utxos */ |
||
113 | $utxos = [ |
||
114 | $bitcoind->fundOutput($amount, $scriptPubKey), |
||
115 | $bitcoind->fundOutput($amount, $scriptPubKey), |
||
116 | $bitcoind->fundOutput($amount, $scriptPubKey), |
||
117 | ]; |
||
118 | |||
119 | // replacable tx |
||
120 | $tx = $this->createTransaction([$utxos[0]], [$privateKey], [ |
||
121 | new TransactionOutput(25000000, $destSPK), |
||
122 | new TransactionOutput(74990000, $changeSPK), |
||
123 | ], [TransactionInterface::MAX_LOCKTIME - 2]); |
||
124 | |||
125 | $result = $bitcoind->makeRpcRequest('sendrawtransaction', [$tx->getHex()]); |
||
126 | $this->assertSendRawTransaction($result); |
||
127 | |||
128 | // replace tx |
||
129 | $tx = $this->createTransaction([$utxos[0], $utxos[1]], [$privateKey, $privateKey], [ |
||
130 | new TransactionOutput(25000000, $destSPK), |
||
131 | new TransactionOutput($utxos[1]->getOutput()->getValue() + 74950000, $changeSPK), |
||
132 | ], [TransactionInterface::MAX_LOCKTIME - 1, TransactionInterface::MAX_LOCKTIME]); |
||
133 | |||
134 | $result = $bitcoind->makeRpcRequest('sendrawtransaction', [$tx->getHex()]); |
||
135 | $this->assertSendRawTransaction($result); |
||
136 | |||
137 | // cant replace again, mempool tx has final input |
||
138 | $tx = $this->createTransaction($utxos, [$privateKey, $privateKey, $privateKey], [ |
||
139 | new TransactionOutput(25000000, $destSPK), |
||
140 | new TransactionOutput((2 * $utxos[1]->getOutput()->getValue()) + 74920000, $changeSPK), |
||
141 | ], [TransactionInterface::MAX_LOCKTIME, TransactionInterface::MAX_LOCKTIME, TransactionInterface::MAX_LOCKTIME]); |
||
142 | |||
143 | $result = $bitcoind->makeRpcRequest('sendrawtransaction', [$tx->getHex()]); |
||
144 | $this->assertBitcoindError(RpcServer::ERROR_TX_MEMPOOL_CONFLICT, $result); |
||
145 | |||
146 | $bitcoind->destroy(); |
||
147 | } |
||
148 | } |
||
149 |