use of co.rsk.bitcoinj.wallet.SendRequest in project rskj by rsksmart.
the class ReleaseTransactionBuilderTest method buildAmountTo_utxoProviderException.
@Test
public void buildAmountTo_utxoProviderException() throws InsufficientMoneyException, UTXOProviderException {
Context btcContext = new Context(NetworkParameters.fromID(NetworkParameters.ID_REGTEST));
Address to = mockAddress(123);
Coin amount = Coin.CENT.multiply(3);
List<UTXO> availableUTXOs = Arrays.asList(mockUTXO("one", 0, Coin.COIN), mockUTXO("one", 1, Coin.COIN.multiply(2)), mockUTXO("two", 1, Coin.COIN.divide(2)), mockUTXO("two", 2, Coin.FIFTY_COINS), mockUTXO("two", 0, Coin.MILLICOIN.times(7)), mockUTXO("three", 0, Coin.CENT.times(3)));
UTXOProvider utxoProvider = mock(UTXOProvider.class);
when(wallet.getUTXOProvider()).thenReturn(utxoProvider);
when(wallet.getWatchedAddresses()).thenReturn(Arrays.asList(changeAddress));
when(utxoProvider.getOpenTransactionOutputs(any(List.class))).then((InvocationOnMock m) -> {
List<Address> addresses = m.getArgumentAt(0, List.class);
Assert.assertEquals(Arrays.asList(changeAddress), addresses);
throw new UTXOProviderException();
});
Mockito.doAnswer((InvocationOnMock m) -> {
SendRequest sr = m.getArgumentAt(0, SendRequest.class);
Assert.assertEquals(Coin.MILLICOIN.multiply(2), sr.feePerKb);
Assert.assertEquals(Wallet.MissingSigsMode.USE_OP_ZERO, sr.missingSigsMode);
Assert.assertEquals(changeAddress, sr.changeAddress);
Assert.assertFalse(sr.shuffleOutputs);
Assert.assertTrue(sr.recipientsPayFees);
BtcTransaction tx = sr.tx;
Assert.assertEquals(1, tx.getOutputs().size());
Assert.assertEquals(amount, tx.getOutput(0).getValue());
Assert.assertEquals(to, tx.getOutput(0).getAddressFromP2PKHScript(NetworkParameters.fromID(NetworkParameters.ID_REGTEST)));
tx.addInput(mockUTXOHash("two"), 2, mock(Script.class));
tx.addInput(mockUTXOHash("three"), 0, mock(Script.class));
return null;
}).when(wallet).completeTx(any(SendRequest.class));
Optional<ReleaseTransactionBuilder.BuildResult> result = builder.buildAmountTo(to, amount);
verify(wallet, times(1)).completeTx(any(SendRequest.class));
Assert.assertFalse(result.isPresent());
}
use of co.rsk.bitcoinj.wallet.SendRequest in project rskj by rsksmart.
the class ReleaseTransactionBuilderTest method mockCompleteTxWithThrowForEmptying.
private void mockCompleteTxWithThrowForEmptying(Wallet wallet, Address expectedAddress, Throwable t) throws InsufficientMoneyException {
Mockito.doAnswer((InvocationOnMock m) -> {
SendRequest sr = m.getArgumentAt(0, SendRequest.class);
Assert.assertEquals(Coin.MILLICOIN.multiply(2), sr.feePerKb);
Assert.assertEquals(Wallet.MissingSigsMode.USE_OP_ZERO, sr.missingSigsMode);
Assert.assertEquals(expectedAddress, sr.changeAddress);
Assert.assertFalse(sr.shuffleOutputs);
Assert.assertTrue(sr.recipientsPayFees);
Assert.assertTrue(sr.emptyWallet);
BtcTransaction tx = sr.tx;
Assert.assertEquals(1, tx.getOutputs().size());
Assert.assertEquals(Coin.ZERO, tx.getOutput(0).getValue());
Assert.assertEquals(expectedAddress, tx.getOutput(0).getAddressFromP2PKHScript(NetworkParameters.fromID(NetworkParameters.ID_REGTEST)));
throw t;
}).when(wallet).completeTx(any(SendRequest.class));
}
use of co.rsk.bitcoinj.wallet.SendRequest in project rskj by rsksmart.
the class BridgeSupport method createMigrationTransaction.
private Pair<BtcTransaction, List<UTXO>> createMigrationTransaction(Wallet originWallet, Address destinationAddress) {
Coin expectedMigrationValue = originWallet.getBalance();
for (; ; ) {
BtcTransaction migrationBtcTx = new BtcTransaction(originWallet.getParams());
migrationBtcTx.addOutput(expectedMigrationValue, destinationAddress);
SendRequest sr = SendRequest.forTx(migrationBtcTx);
sr.changeAddress = destinationAddress;
sr.feePerKb = getFeePerKb();
sr.missingSigsMode = Wallet.MissingSigsMode.USE_OP_ZERO;
sr.recipientsPayFees = true;
try {
originWallet.completeTx(sr);
for (TransactionInput transactionInput : migrationBtcTx.getInputs()) {
transactionInput.disconnect();
}
List<UTXO> selectedUTXOs = originWallet.getUTXOProvider().getOpenTransactionOutputs(originWallet.getWatchedAddresses()).stream().filter(utxo -> migrationBtcTx.getInputs().stream().anyMatch(input -> input.getOutpoint().getHash().equals(utxo.getHash()) && input.getOutpoint().getIndex() == utxo.getIndex())).collect(Collectors.toList());
return Pair.of(migrationBtcTx, selectedUTXOs);
} catch (InsufficientMoneyException | Wallet.ExceededMaxTransactionSize | Wallet.CouldNotAdjustDownwards e) {
expectedMigrationValue = expectedMigrationValue.divide(2);
} catch (Wallet.DustySendRequested e) {
throw new IllegalStateException("Retiring federation wallet cannot be emptied", e);
} catch (UTXOProviderException e) {
throw new RuntimeException("Unexpected UTXO provider error", e);
}
}
}
Aggregations