Search in sources :

Example 6 with SendRequest

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());
}
Also used : Script(co.rsk.bitcoinj.script.Script) SendRequest(co.rsk.bitcoinj.wallet.SendRequest) InvocationOnMock(org.mockito.invocation.InvocationOnMock) List(java.util.List) Test(org.junit.Test)

Example 7 with SendRequest

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));
}
Also used : SendRequest(co.rsk.bitcoinj.wallet.SendRequest) InvocationOnMock(org.mockito.invocation.InvocationOnMock)

Example 8 with SendRequest

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);
        }
    }
}
Also used : java.util(java.util) Hex(org.spongycastle.util.encoders.Hex) LoggerFactory(org.slf4j.LoggerFactory) RskAddress(co.rsk.core.RskAddress) Keccak256(co.rsk.crypto.Keccak256) Block(org.ethereum.core.Block) TransactionSignature(co.rsk.bitcoinj.crypto.TransactionSignature) Pair(org.apache.commons.lang3.tuple.Pair) BridgeConstants(co.rsk.config.BridgeConstants) co.rsk.bitcoinj.core(co.rsk.bitcoinj.core) PrecompiledContracts(org.ethereum.vm.PrecompiledContracts) BigInteger(java.math.BigInteger) Nullable(javax.annotation.Nullable) Wallet(co.rsk.bitcoinj.wallet.Wallet) BtcBlockStore(co.rsk.bitcoinj.store.BtcBlockStore) PanicProcessor(co.rsk.panic.PanicProcessor) ScriptChunk(co.rsk.bitcoinj.script.ScriptChunk) Logger(org.slf4j.Logger) IOException(java.io.IOException) Instant(java.time.Instant) Repository(org.ethereum.core.Repository) Collectors(java.util.stream.Collectors) SendRequest(co.rsk.bitcoinj.wallet.SendRequest) Program(org.ethereum.vm.program.Program) ScriptBuilder(co.rsk.bitcoinj.script.ScriptBuilder) Script(co.rsk.bitcoinj.script.Script) BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException) VisibleForTesting(com.google.common.annotations.VisibleForTesting) RskSystemProperties(co.rsk.config.RskSystemProperties) BridgeEventLogger(co.rsk.peg.utils.BridgeEventLogger) Transaction(org.ethereum.core.Transaction) InputStream(java.io.InputStream) SendRequest(co.rsk.bitcoinj.wallet.SendRequest) Wallet(co.rsk.bitcoinj.wallet.Wallet)

Aggregations

SendRequest (co.rsk.bitcoinj.wallet.SendRequest)8 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 Script (co.rsk.bitcoinj.script.Script)5 List (java.util.List)4 Test (org.junit.Test)4 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)2 Wallet (co.rsk.bitcoinj.wallet.Wallet)2 java.util (java.util)2 Collectors (java.util.stream.Collectors)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 TransactionSignature (co.rsk.bitcoinj.crypto.TransactionSignature)1 ScriptBuilder (co.rsk.bitcoinj.script.ScriptBuilder)1 ScriptChunk (co.rsk.bitcoinj.script.ScriptChunk)1 BlockStoreException (co.rsk.bitcoinj.store.BlockStoreException)1 BtcBlockStore (co.rsk.bitcoinj.store.BtcBlockStore)1 BridgeConstants (co.rsk.config.BridgeConstants)1 RskSystemProperties (co.rsk.config.RskSystemProperties)1 RskAddress (co.rsk.core.RskAddress)1 Keccak256 (co.rsk.crypto.Keccak256)1