use of co.rsk.bitcoinj.wallet.SendRequest in project rskj by rsksmart.
the class ReleaseTransactionBuilderTest method mockCompleteTxWithThrowForBuildToAmount.
private void mockCompleteTxWithThrowForBuildToAmount(Wallet wallet, Coin expectedAmount, 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(changeAddress, sr.changeAddress);
Assert.assertFalse(sr.shuffleOutputs);
Assert.assertTrue(sr.recipientsPayFees);
BtcTransaction tx = sr.tx;
Assert.assertEquals(1, tx.getOutputs().size());
Assert.assertEquals(expectedAmount, 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 ReleaseTransactionBuilderTest method buildEmptyWalletTo_ok.
@Test
public void buildEmptyWalletTo_ok() throws InsufficientMoneyException, UTXOProviderException {
Context btcContext = new Context(NetworkParameters.fromID(NetworkParameters.ID_REGTEST));
Address to = mockAddress(123);
List<UTXO> availableUTXOs = Arrays.asList(mockUTXO("one", 0, Coin.COIN), mockUTXO("two", 2, Coin.FIFTY_COINS), mockUTXO("two", 0, Coin.COIN.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(to));
when(utxoProvider.getOpenTransactionOutputs(any(List.class))).then((InvocationOnMock m) -> {
List<Address> addresses = m.getArgumentAt(0, List.class);
Assert.assertEquals(Arrays.asList(to), addresses);
return availableUTXOs;
});
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(to, 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(to, tx.getOutput(0).getAddressFromP2PKHScript(NetworkParameters.fromID(NetworkParameters.ID_REGTEST)));
tx.addInput(mockUTXOHash("one"), 0, mock(Script.class));
tx.addInput(mockUTXOHash("two"), 2, mock(Script.class));
tx.addInput(mockUTXOHash("two"), 0, mock(Script.class));
tx.addInput(mockUTXOHash("three"), 0, mock(Script.class));
tx.getOutput(0).setValue(Coin.FIFTY_COINS);
return null;
}).when(wallet).completeTx(any(SendRequest.class));
Optional<ReleaseTransactionBuilder.BuildResult> result = builder.buildEmptyWalletTo(to);
Assert.assertTrue(result.isPresent());
BtcTransaction tx = result.get().getBtcTx();
List<UTXO> selectedUTXOs = result.get().getSelectedUTXOs();
Assert.assertEquals(1, tx.getOutputs().size());
Assert.assertEquals(Coin.FIFTY_COINS, tx.getOutput(0).getValue());
Assert.assertEquals(to, tx.getOutput(0).getAddressFromP2PKHScript(NetworkParameters.fromID(NetworkParameters.ID_REGTEST)));
Assert.assertEquals(4, tx.getInputs().size());
Assert.assertEquals(mockUTXOHash("one"), tx.getInput(0).getOutpoint().getHash());
Assert.assertEquals(0, tx.getInput(0).getOutpoint().getIndex());
Assert.assertEquals(mockUTXOHash("two"), tx.getInput(1).getOutpoint().getHash());
Assert.assertEquals(2, tx.getInput(1).getOutpoint().getIndex());
Assert.assertEquals(mockUTXOHash("two"), tx.getInput(2).getOutpoint().getHash());
Assert.assertEquals(0, tx.getInput(2).getOutpoint().getIndex());
Assert.assertEquals(mockUTXOHash("three"), tx.getInput(3).getOutpoint().getHash());
Assert.assertEquals(0, tx.getInput(3).getOutpoint().getIndex());
Assert.assertEquals(4, selectedUTXOs.size());
Assert.assertEquals(mockUTXOHash("one"), selectedUTXOs.get(0).getHash());
Assert.assertEquals(0, selectedUTXOs.get(0).getIndex());
Assert.assertEquals(mockUTXOHash("two"), selectedUTXOs.get(1).getHash());
Assert.assertEquals(2, selectedUTXOs.get(1).getIndex());
Assert.assertEquals(mockUTXOHash("two"), selectedUTXOs.get(2).getHash());
Assert.assertEquals(0, selectedUTXOs.get(2).getIndex());
Assert.assertEquals(mockUTXOHash("three"), selectedUTXOs.get(3).getHash());
Assert.assertEquals(0, selectedUTXOs.get(3).getIndex());
}
use of co.rsk.bitcoinj.wallet.SendRequest in project rskj by rsksmart.
the class ReleaseTransactionBuilderTest method buildAmountTo_ok.
@Test
public void buildAmountTo_ok() 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);
return availableUTXOs;
});
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);
Assert.assertTrue(result.isPresent());
BtcTransaction tx = result.get().getBtcTx();
List<UTXO> selectedUTXOs = result.get().getSelectedUTXOs();
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)));
Assert.assertEquals(2, tx.getInputs().size());
Assert.assertEquals(mockUTXOHash("two"), tx.getInput(0).getOutpoint().getHash());
Assert.assertEquals(2, tx.getInput(0).getOutpoint().getIndex());
Assert.assertEquals(mockUTXOHash("three"), tx.getInput(1).getOutpoint().getHash());
Assert.assertEquals(0, tx.getInput(1).getOutpoint().getIndex());
Assert.assertEquals(2, selectedUTXOs.size());
Assert.assertEquals(mockUTXOHash("two"), selectedUTXOs.get(0).getHash());
Assert.assertEquals(2, selectedUTXOs.get(0).getIndex());
Assert.assertEquals(mockUTXOHash("three"), selectedUTXOs.get(1).getHash());
Assert.assertEquals(0, selectedUTXOs.get(1).getIndex());
}
use of co.rsk.bitcoinj.wallet.SendRequest in project rskj by rsksmart.
the class ReleaseTransactionBuilder method buildWithConfiguration.
private Optional<BuildResult> buildWithConfiguration(SendRequestConfigurator sendRequestConfigurator, String operationDescription) {
// Build a tx and send request and configure it
BtcTransaction btcTx = new BtcTransaction(params);
SendRequest sr = SendRequest.forTx(btcTx);
// Default settings
defaultSettingsConfigurator.configure(sr);
// Specific settings
sendRequestConfigurator.configure(sr);
try {
wallet.completeTx(sr);
// Disconnect input from output because we don't need the reference and it interferes serialization
for (TransactionInput transactionInput : btcTx.getInputs()) {
transactionInput.disconnect();
}
List<UTXO> selectedUTXOs = wallet.getUTXOProvider().getOpenTransactionOutputs(wallet.getWatchedAddresses()).stream().filter(utxo -> btcTx.getInputs().stream().anyMatch(input -> input.getOutpoint().getHash().equals(utxo.getHash()) && input.getOutpoint().getIndex() == utxo.getIndex())).collect(Collectors.toList());
return Optional.of(new BuildResult(btcTx, selectedUTXOs));
} catch (InsufficientMoneyException e) {
logger.warn(String.format("Not enough BTC in the wallet to complete %s", operationDescription), e);
// panicProcessor.panic("nomoney", "Not enough confirmed BTC in the federation wallet to complete " + rskTxHash + " " + btcTx);
return Optional.empty();
} catch (Wallet.CouldNotAdjustDownwards e) {
logger.warn(String.format("A user output could not be adjusted downwards to pay tx fees %s", operationDescription), e);
// panicProcessor.panic("couldnotadjustdownwards", "A user output could not be adjusted downwards to pay tx fees " + rskTxHash + " " + btcTx);
return Optional.empty();
} catch (Wallet.ExceededMaxTransactionSize e) {
logger.warn(String.format("Tx size too big %s", operationDescription), e);
// panicProcessor.panic("exceededmaxtransactionsize", "Tx size too big " + rskTxHash + " " + btcTx);
return Optional.empty();
} catch (UTXOProviderException e) {
logger.warn(String.format("UTXO provider exception sending %s", operationDescription), e);
// panicProcessor.panic("utxoprovider", "UTXO provider exception " + rskTxHash + " " + btcTx);
return Optional.empty();
}
}
use of co.rsk.bitcoinj.wallet.SendRequest in project rskj by rsksmart.
the class ReleaseTransactionBuilderTest method buildEmptyWalletTo_utxoProviderException.
@Test
public void buildEmptyWalletTo_utxoProviderException() throws InsufficientMoneyException, UTXOProviderException {
Context btcContext = new Context(NetworkParameters.fromID(NetworkParameters.ID_REGTEST));
Address to = mockAddress(123);
List<UTXO> availableUTXOs = Arrays.asList(mockUTXO("two", 2, Coin.FIFTY_COINS), mockUTXO("three", 0, Coin.CENT.times(3)));
UTXOProvider utxoProvider = mock(UTXOProvider.class);
when(wallet.getUTXOProvider()).thenReturn(utxoProvider);
when(wallet.getWatchedAddresses()).thenReturn(Arrays.asList(to));
when(utxoProvider.getOpenTransactionOutputs(any(List.class))).then((InvocationOnMock m) -> {
List<Address> addresses = m.getArgumentAt(0, List.class);
Assert.assertEquals(Arrays.asList(to), 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(to, 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(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));
tx.getOutput(0).setValue(Coin.FIFTY_COINS);
return null;
}).when(wallet).completeTx(any(SendRequest.class));
Optional<ReleaseTransactionBuilder.BuildResult> result = builder.buildEmptyWalletTo(to);
verify(wallet, times(1)).completeTx(any(SendRequest.class));
Assert.assertFalse(result.isPresent());
}
Aggregations