org.bitcoinj.core.AbstractBlockChain Java Examples
The following examples show how to use
org.bitcoinj.core.AbstractBlockChain.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WalletTest.java From green_android with GNU General Public License v3.0 | 6 votes |
@Test public void ageMattersDuringSelection() throws Exception { // Test that we prefer older coins to newer coins when building spends. This reduces required fees and improves // time to confirmation as the transaction will appear less spammy. final int ITERATIONS = 10; Transaction[] txns = new Transaction[ITERATIONS]; for (int i = 0; i < ITERATIONS; i++) { txns[i] = sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN); } // Check that we spend transactions in order of reception. for (int i = 0; i < ITERATIONS; i++) { Transaction spend = wallet.createSend(OTHER_ADDRESS, COIN); assertEquals(spend.getInputs().size(), 1); assertEquals("Failed on iteration " + i, spend.getInput(0).getOutpoint().getHash(), txns[i].getHash()); wallet.commitTx(spend); } }
Example #2
Source File: WalletTest.java From GreenBits with GNU General Public License v3.0 | 6 votes |
@Test public void keyRotationHD() throws Exception { // Test that if we rotate an HD chain, a new one is created and all arrivals on the old keys are moved. Utils.setMockClock(); wallet = new Wallet(PARAMS); ECKey key1 = wallet.freshReceiveKey(); ECKey key2 = wallet.freshReceiveKey(); sendMoneyToWallet(wallet, AbstractBlockChain.NewBlockType.BEST_CHAIN, CENT, key1.toAddress(PARAMS)); sendMoneyToWallet(wallet, AbstractBlockChain.NewBlockType.BEST_CHAIN, CENT, key2.toAddress(PARAMS)); DeterministicKey watchKey1 = wallet.getWatchingKey(); // A day later, we get compromised. Utils.rollMockClock(86400); wallet.setKeyRotationTime(Utils.currentTimeSeconds()); List<Transaction> txns = wallet.doMaintenance(null, false).get(); assertEquals(1, txns.size()); DeterministicKey watchKey2 = wallet.getWatchingKey(); assertNotEquals(watchKey1, watchKey2); }
Example #3
Source File: WalletTest.java From bcm-android with GNU General Public License v3.0 | 6 votes |
@Test public void transactions() throws Exception { // This test covers a bug in which Transaction.getValueSentFromMe was calculating incorrectly. Transaction tx = createFakeTx(UNITTEST, COIN, myAddress); // Now add another output (ie, change) that goes to some other address. TransactionOutput output = new TransactionOutput(UNITTEST, tx, valueOf(0, 5), OTHER_ADDRESS); tx.addOutput(output); // Note that tx is no longer valid: it spends more than it imports. However checking transactions balance // correctly isn't possible in SPV mode because value is a property of outputs not inputs. Without all // transactions you can't check they add up. sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, tx); // Now the other guy creates a transaction which spends that change. Transaction tx2 = new Transaction(UNITTEST); tx2.addInput(output); tx2.addOutput(new TransactionOutput(UNITTEST, tx2, valueOf(0, 5), myAddress)); // tx2 doesn't send any coins from us, even though the output is in the wallet. assertEquals(ZERO, tx2.getValueSentFromMe(wallet)); }
Example #4
Source File: WalletTest.java From bcm-android with GNU General Public License v3.0 | 6 votes |
@Test public void bounce() throws Exception { // This test covers bug 64 (False double spends). Check that if we create a spend and it's immediately sent // back to us, this isn't considered as a double spend. Coin coin1 = COIN; sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, coin1); // Send half to some other guy. Sending only half then waiting for a confirm is important to ensure the tx is // in the unspent pool, not pending or spent. Coin coinHalf = valueOf(0, 50); assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT)); assertEquals(1, wallet.getTransactions(true).size()); Transaction outbound1 = wallet.createSend(OTHER_ADDRESS, coinHalf); wallet.commitTx(outbound1); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, outbound1); assertTrue(outbound1.getWalletOutputs(wallet).size() <= 1); //the change address at most // That other guy gives us the coins right back. Transaction inbound2 = new Transaction(UNITTEST); inbound2.addOutput(new TransactionOutput(UNITTEST, inbound2, coinHalf, myAddress)); assertTrue(outbound1.getWalletOutputs(wallet).size() >= 1); inbound2.addInput(outbound1.getOutputs().get(0)); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, inbound2); assertEquals(coin1, wallet.getBalance()); }
Example #5
Source File: WalletTest.java From green_android with GNU General Public License v3.0 | 6 votes |
@Test public void testCategory2WithChange() throws Exception { // Specifically target case 2 with significant change // Generate a ton of small outputs StoredBlock block = new StoredBlock(makeSolvedTestBlock(blockStore, OTHER_ADDRESS), BigInteger.ONE, 1); int i = 0; while (i <= CENT.divide(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE.multiply(10))) { Transaction tx = createFakeTxWithChangeAddress(PARAMS, Transaction.REFERENCE_DEFAULT_MIN_TX_FEE.multiply(10), myAddress, OTHER_ADDRESS); tx.getInput(0).setSequenceNumber(i++); // Keep every transaction unique wallet.receiveFromBlock(tx, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, i); } // The selector will choose 2 with MIN_TX_FEE fee SendRequest request1 = SendRequest.to(OTHER_ADDRESS, CENT.add(SATOSHI)); request1.ensureMinRequiredFee = true; wallet.completeTx(request1); assertEquals(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE, request1.tx.getFee()); assertEquals(request1.tx.getInputs().size(), i); // We should have spent all inputs assertEquals(2, request1.tx.getOutputs().size()); // and gotten change back }
Example #6
Source File: WalletTest.java From bcm-android with GNU General Public License v3.0 | 6 votes |
@Test public void doubleSpendUnspendsOtherInputs() throws Exception { // Test another Finney attack, but this time the killed transaction was also spending some other outputs in // our wallet which were not themselves double spent. This test ensures the death of the pending transaction // frees up the other outputs and makes them spendable again. // Receive 1 coin and then 2 coins in separate transactions. sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, valueOf(2, 0)); // Create a send to a merchant of all our coins. Transaction send1 = wallet.createSend(OTHER_ADDRESS, valueOf(2, 90)); // Create a double spend of just the first one. Address BAD_GUY = LegacyAddress.fromKey(UNITTEST, new ECKey()); Transaction send2 = wallet.createSend(BAD_GUY, COIN); send2 = UNITTEST.getDefaultSerializer().makeTransaction(send2.bitcoinSerialize()); // Broadcast send1, it's now pending. wallet.commitTx(send1); assertEquals(ZERO, wallet.getBalance()); // change of 10 cents is not yet mined so not included in the balance. // Receive a block that overrides the send1 using send2. sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, send2); // send1 got rolled back and replaced with a smaller send that only used one of our received coins, thus ... assertEquals(valueOf(2, 0), wallet.getBalance()); assertTrue(wallet.isConsistent()); }
Example #7
Source File: WalletTest.java From GreenBits with GNU General Public License v3.0 | 6 votes |
@Test public void transactions() throws Exception { // This test covers a bug in which Transaction.getValueSentFromMe was calculating incorrectly. Transaction tx = createFakeTx(PARAMS, COIN, myAddress); // Now add another output (ie, change) that goes to some other address. TransactionOutput output = new TransactionOutput(PARAMS, tx, valueOf(0, 5), OTHER_ADDRESS); tx.addOutput(output); // Note that tx is no longer valid: it spends more than it imports. However checking transactions balance // correctly isn't possible in SPV mode because value is a property of outputs not inputs. Without all // transactions you can't check they add up. sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, tx); // Now the other guy creates a transaction which spends that change. Transaction tx2 = new Transaction(PARAMS); tx2.addInput(output); tx2.addOutput(new TransactionOutput(PARAMS, tx2, valueOf(0, 5), myAddress)); // tx2 doesn't send any coins from us, even though the output is in the wallet. assertEquals(ZERO, tx2.getValueSentFromMe(wallet)); }
Example #8
Source File: WalletTest.java From bcm-android with GNU General Public License v3.0 | 6 votes |
@Test public void sideChain() throws Exception { // The wallet receives a coin on the main chain, then on a side chain. Balance is equal to both added together // as we assume the side chain tx is pending and will be included shortly. Coin v1 = COIN; sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, v1); assertEquals(v1, wallet.getBalance()); assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT)); assertEquals(1, wallet.getTransactions(true).size()); Coin v2 = valueOf(0, 50); sendMoneyToWallet(AbstractBlockChain.NewBlockType.SIDE_CHAIN, v2); assertEquals(2, wallet.getTransactions(true).size()); assertEquals(v1, wallet.getBalance()); assertEquals(v1.add(v2), wallet.getBalance(Wallet.BalanceType.ESTIMATED)); }
Example #9
Source File: WalletTest.java From GreenBits with GNU General Public License v3.0 | 6 votes |
@Test public void spendToSameWallet() throws Exception { // Test that a spend to the same wallet is dealt with correctly. // It should appear in the wallet and confirm. // This is a bit of a silly thing to do in the real world as all it does is burn a fee but it is perfectly valid. Coin coin1 = COIN; Coin coinHalf = valueOf(0, 50); // Start by giving us 1 coin. sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, coin1); // Send half to ourselves. We should then have a balance available to spend of zero. assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT)); assertEquals(1, wallet.getTransactions(true).size()); Transaction outbound1 = wallet.createSend(myAddress, coinHalf); wallet.commitTx(outbound1); // We should have a zero available balance before the next block. assertEquals(ZERO, wallet.getBalance()); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, outbound1); // We should have a balance of 1 BTC after the block is received. assertEquals(coin1, wallet.getBalance()); }
Example #10
Source File: WalletTest.java From GreenBits with GNU General Public License v3.0 | 6 votes |
@Test public void feeSolverAndCoinSelectionTest_dustySendRequested() throws Exception { // Generate a few outputs to us that are far too small to spend reasonably Transaction tx1 = createFakeTx(PARAMS, SATOSHI, myAddress); Transaction tx2 = createFakeTx(PARAMS, SATOSHI, myAddress); assertNotEquals(tx1.getHash(), tx2.getHash()); Transaction tx3 = createFakeTx(PARAMS, SATOSHI.multiply(10), myAddress); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, tx1, tx2, tx3); // Not allowed to send dust. try { SendRequest request = SendRequest.to(OTHER_ADDRESS, SATOSHI); request.ensureMinRequiredFee = true; wallet.completeTx(request); fail(); } catch (Wallet.DustySendRequested e) { // Expected. } // Spend it all without fee enforcement SendRequest req = SendRequest.to(OTHER_ADDRESS, SATOSHI.multiply(12)); assertNotNull(wallet.sendCoinsOffline(req)); assertEquals(ZERO, wallet.getBalance()); }
Example #11
Source File: WalletTest.java From bcm-android with GNU General Public License v3.0 | 6 votes |
@Test public void txSpendingDeadTx() throws Exception { CoinSelector originalCoinSelector = wallet.getCoinSelector(); try { wallet.allowSpendingUnconfirmedTransactions(); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, valueOf(2, 0)); Transaction send1 = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 0))); Transaction send2 = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 20))); wallet.commitTx(send1); assertPending(send1); Transaction send1b = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(0, 50))); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, send2); assertDead(send1); assertUnspent(send2); wallet.receivePending(send1b, null); assertDead(send1); assertUnspent(send2); assertDead(send1b); } finally { wallet.setCoinSelector(originalCoinSelector); } }
Example #12
Source File: WalletTest.java From bcm-android with GNU General Public License v3.0 | 6 votes |
@Test public void testAddTransactionsDependingOn() throws Exception { CoinSelector originalCoinSelector = wallet.getCoinSelector(); try { wallet.allowSpendingUnconfirmedTransactions(); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, valueOf(2, 0)); Transaction send1 = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 0))); Transaction send2 = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 20))); wallet.commitTx(send1); Transaction send1b = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(0, 50))); wallet.commitTx(send1b); Transaction send1c = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(0, 25))); wallet.commitTx(send1c); wallet.commitTx(send2); Set<Transaction> txns = new HashSet<>(); txns.add(send1); wallet.addTransactionsDependingOn(txns, wallet.getTransactions(true)); assertEquals(3, txns.size()); assertTrue(txns.contains(send1)); assertTrue(txns.contains(send1b)); assertTrue(txns.contains(send1c)); } finally { wallet.setCoinSelector(originalCoinSelector); } }
Example #13
Source File: WalletTest.java From GreenBits with GNU General Public License v3.0 | 6 votes |
@Test public void txSpendingDeadTx() throws Exception { CoinSelector originalCoinSelector = wallet.getCoinSelector(); try { wallet.allowSpendingUnconfirmedTransactions(); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, valueOf(2, 0)); Transaction send1 = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 0))); Transaction send2 = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 20))); wallet.commitTx(send1); assertPending(send1); Transaction send1b = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(0, 50))); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, send2); assertDead(send1); assertUnspent(send2); wallet.receivePending(send1b, null); assertDead(send1); assertUnspent(send2); assertDead(send1b); } finally { wallet.setCoinSelector(originalCoinSelector); } }
Example #14
Source File: WalletTest.java From GreenBits with GNU General Public License v3.0 | 6 votes |
@Test public void pubkeyOnlyScripts() throws Exception { // Verify that we support outputs like OP_PUBKEY and the corresponding inputs. ECKey key1 = wallet.freshReceiveKey(); Coin value = valueOf(5, 0); Transaction t1 = createFakeTx(PARAMS, value, key1); if (wallet.isPendingTransactionRelevant(t1)) wallet.receivePending(t1, null); // TX should have been seen as relevant. assertEquals(value, wallet.getBalance(Wallet.BalanceType.ESTIMATED)); assertEquals(ZERO, wallet.getBalance(Wallet.BalanceType.AVAILABLE)); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, t1); // TX should have been seen as relevant, extracted and processed. assertEquals(value, wallet.getBalance(Wallet.BalanceType.AVAILABLE)); // Spend it and ensure we can spend the <key> OP_CHECKSIG output correctly. Transaction t2 = wallet.createSend(OTHER_ADDRESS, value); assertNotNull(t2); // TODO: This code is messy, improve the Script class and fixinate! assertEquals(t2.toString(), 1, t2.getInputs().get(0).getScriptSig().getChunks().size()); assertTrue(t2.getInputs().get(0).getScriptSig().getChunks().get(0).data.length > 50); }
Example #15
Source File: WalletTest.java From bcm-android with GNU General Public License v3.0 | 6 votes |
@Test public void pubkeyOnlyScripts() throws Exception { // Verify that we support outputs like OP_PUBKEY and the corresponding inputs. ECKey key1 = wallet.freshReceiveKey(); Coin value = valueOf(5, 0); Transaction t1 = createFakeTx(UNITTEST, value, key1); if (wallet.isPendingTransactionRelevant(t1)) wallet.receivePending(t1, null); // TX should have been seen as relevant. assertEquals(value, wallet.getBalance(Wallet.BalanceType.ESTIMATED)); assertEquals(ZERO, wallet.getBalance(Wallet.BalanceType.AVAILABLE)); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, t1); // TX should have been seen as relevant, extracted and processed. assertEquals(value, wallet.getBalance(Wallet.BalanceType.AVAILABLE)); // Spend it and ensure we can spend the <key> OP_CHECKSIG output correctly. Transaction t2 = wallet.createSend(OTHER_ADDRESS, value); assertNotNull(t2); // TODO: This code is messy, improve the Script class and fixinate! assertEquals(t2.toString(), 1, t2.getInputs().get(0).getScriptSig().getChunks().size()); assertTrue(t2.getInputs().get(0).getScriptSig().getChunks().get(0).data.length > 50); }
Example #16
Source File: WalletTest.java From GreenBits with GNU General Public License v3.0 | 6 votes |
@Test public void sideChain() throws Exception { // The wallet receives a coin on the main chain, then on a side chain. Balance is equal to both added together // as we assume the side chain tx is pending and will be included shortly. Coin v1 = COIN; sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, v1); assertEquals(v1, wallet.getBalance()); assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT)); assertEquals(1, wallet.getTransactions(true).size()); Coin v2 = valueOf(0, 50); sendMoneyToWallet(AbstractBlockChain.NewBlockType.SIDE_CHAIN, v2); assertEquals(2, wallet.getTransactions(true).size()); assertEquals(v1, wallet.getBalance()); assertEquals(v1.add(v2), wallet.getBalance(Wallet.BalanceType.ESTIMATED)); }
Example #17
Source File: WalletTest.java From green_android with GNU General Public License v3.0 | 6 votes |
@Test public void replayWhilstPending() throws Exception { // Check that if a pending transaction spends outputs of chain-included transactions, we mark them as spent. // See bug 345. This can happen if there is a pending transaction floating around and then you replay the // chain without emptying the memory pool (or refilling it from a peer). Coin value = COIN; Transaction tx1 = createFakeTx(PARAMS, value, myAddress); Transaction tx2 = new Transaction(PARAMS); tx2.addInput(tx1.getOutput(0)); tx2.addOutput(valueOf(0, 9), OTHER_ADDRESS); // Add a change address to ensure this tx is relevant. tx2.addOutput(CENT, wallet.currentChangeAddress()); wallet.receivePending(tx2, null); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, tx1); assertEquals(ZERO, wallet.getBalance()); assertEquals(1, wallet.getPoolSize(Pool.SPENT)); assertEquals(1, wallet.getPoolSize(Pool.PENDING)); assertEquals(0, wallet.getPoolSize(Pool.UNSPENT)); }
Example #18
Source File: WalletTest.java From bcm-android with GNU General Public License v3.0 | 6 votes |
@Test public void replayWhilstPending() throws Exception { // Check that if a pending transaction spends outputs of chain-included transactions, we mark them as spent. // See bug 345. This can happen if there is a pending transaction floating around and then you replay the // chain without emptying the memory pool (or refilling it from a peer). Coin value = COIN; Transaction tx1 = createFakeTx(UNITTEST, value, myAddress); Transaction tx2 = new Transaction(UNITTEST); tx2.addInput(tx1.getOutput(0)); tx2.addOutput(valueOf(0, 9), OTHER_ADDRESS); // Add a change address to ensure this tx is relevant. tx2.addOutput(CENT, wallet.currentChangeAddress()); wallet.receivePending(tx2, null); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, tx1); assertEquals(ZERO, wallet.getBalance()); assertEquals(1, wallet.getPoolSize(Pool.SPENT)); assertEquals(1, wallet.getPoolSize(Pool.PENDING)); assertEquals(0, wallet.getPoolSize(Pool.UNSPENT)); }
Example #19
Source File: WalletTest.java From green_android with GNU General Public License v3.0 | 6 votes |
@Test public void watchingScriptsSentFrom() throws Exception { int baseElements = wallet.getBloomFilterElementCount(); Address watchedAddress = new ECKey().toAddress(PARAMS); wallet.addWatchedAddress(watchedAddress); assertEquals(baseElements + 1, wallet.getBloomFilterElementCount()); Transaction t1 = createFakeTx(PARAMS, CENT, watchedAddress); Transaction t2 = createFakeTx(PARAMS, COIN, OTHER_ADDRESS); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, t1); assertEquals(baseElements + 2, wallet.getBloomFilterElementCount()); Transaction st2 = new Transaction(PARAMS); st2.addOutput(CENT, OTHER_ADDRESS); st2.addOutput(COIN, OTHER_ADDRESS); st2.addInput(t1.getOutput(0)); st2.addInput(t2.getOutput(0)); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, st2); assertEquals(baseElements + 2, wallet.getBloomFilterElementCount()); assertEquals(CENT, st2.getValueSentFromMe(wallet)); }
Example #20
Source File: WalletTest.java From green_android with GNU General Public License v3.0 | 6 votes |
@Test public void transactions() throws Exception { // This test covers a bug in which Transaction.getValueSentFromMe was calculating incorrectly. Transaction tx = createFakeTx(PARAMS, COIN, myAddress); // Now add another output (ie, change) that goes to some other address. TransactionOutput output = new TransactionOutput(PARAMS, tx, valueOf(0, 5), OTHER_ADDRESS); tx.addOutput(output); // Note that tx is no longer valid: it spends more than it imports. However checking transactions balance // correctly isn't possible in SPV mode because value is a property of outputs not inputs. Without all // transactions you can't check they add up. sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, tx); // Now the other guy creates a transaction which spends that change. Transaction tx2 = new Transaction(PARAMS); tx2.addInput(output); tx2.addOutput(new TransactionOutput(PARAMS, tx2, valueOf(0, 5), myAddress)); // tx2 doesn't send any coins from us, even though the output is in the wallet. assertEquals(ZERO, tx2.getValueSentFromMe(wallet)); }
Example #21
Source File: WalletTest.java From green_android with GNU General Public License v3.0 | 6 votes |
@Test public void txSpendingDeadTx() throws Exception { CoinSelector originalCoinSelector = wallet.getCoinSelector(); try { wallet.allowSpendingUnconfirmedTransactions(); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, valueOf(2, 0)); Transaction send1 = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 0))); Transaction send2 = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 20))); wallet.commitTx(send1); assertPending(send1); Transaction send1b = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(0, 50))); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, send2); assertDead(send1); assertUnspent(send2); wallet.receivePending(send1b, null); assertDead(send1); assertUnspent(send2); assertDead(send1b); } finally { wallet.setCoinSelector(originalCoinSelector); } }
Example #22
Source File: WalletTest.java From GreenBits with GNU General Public License v3.0 | 6 votes |
@Test public void totalReceivedSent() throws Exception { // Receive 4 BTC in 2 separate transactions Transaction toMe1 = createFakeTxWithoutChangeAddress(PARAMS, COIN.multiply(2), myAddress); Transaction toMe2 = createFakeTxWithoutChangeAddress(PARAMS, COIN.multiply(2), myAddress); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, toMe1, toMe2); // Check we calculate the total received correctly assertEquals(Coin.COIN.multiply(4), wallet.getTotalReceived()); // Send 3 BTC in a single transaction SendRequest req = SendRequest.to(OTHER_ADDRESS, Coin.COIN.multiply(3)); wallet.completeTx(req); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, req.tx); // Check that we still have the same totalReceived, since the above tx will have sent us change back assertEquals(Coin.COIN.multiply(4),wallet.getTotalReceived()); assertEquals(Coin.COIN.multiply(3),wallet.getTotalSent()); // TODO: test shared wallet calculation here }
Example #23
Source File: WalletTest.java From green_android with GNU General Public License v3.0 | 6 votes |
@Test public void doubleSpendForBuildingTx() throws Exception { CoinSelector originalCoinSelector = wallet.getCoinSelector(); try { wallet.allowSpendingUnconfirmedTransactions(); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, valueOf(2, 0)); Transaction send1 = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 0))); Transaction send2 = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 20))); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, send1); assertUnspent(send1); wallet.receivePending(send2, null); assertUnspent(send1); assertDead(send2); } finally { wallet.setCoinSelector(originalCoinSelector); } }
Example #24
Source File: WalletTest.java From GreenBits with GNU General Public License v3.0 | 6 votes |
@Test public void ageMattersDuringSelection() throws Exception { // Test that we prefer older coins to newer coins when building spends. This reduces required fees and improves // time to confirmation as the transaction will appear less spammy. final int ITERATIONS = 10; Transaction[] txns = new Transaction[ITERATIONS]; for (int i = 0; i < ITERATIONS; i++) { txns[i] = sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN); } // Check that we spend transactions in order of reception. for (int i = 0; i < ITERATIONS; i++) { Transaction spend = wallet.createSend(OTHER_ADDRESS, COIN); assertEquals(spend.getInputs().size(), 1); assertEquals("Failed on iteration " + i, spend.getInput(0).getOutpoint().getHash(), txns[i].getHash()); wallet.commitTx(spend); } }
Example #25
Source File: WalletTest.java From bcm-android with GNU General Public License v3.0 | 6 votes |
@Test public void childPaysForParent() throws Exception { // Receive confirmed balance to play with. Transaction toMe = createFakeTxWithoutChangeAddress(UNITTEST, COIN, myAddress); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, toMe); assertEquals(Coin.COIN, wallet.getBalance(BalanceType.ESTIMATED_SPENDABLE)); assertEquals(Coin.COIN, wallet.getBalance(BalanceType.AVAILABLE_SPENDABLE)); // Receive unconfirmed coin without fee. Transaction toMeWithoutFee = createFakeTxWithoutChangeAddress(UNITTEST, COIN, myAddress); wallet.receivePending(toMeWithoutFee, null); assertEquals(Coin.COIN.multiply(2), wallet.getBalance(BalanceType.ESTIMATED_SPENDABLE)); assertEquals(Coin.COIN, wallet.getBalance(BalanceType.AVAILABLE_SPENDABLE)); // Craft a child-pays-for-parent transaction. final Coin feeRaise = MILLICOIN; final SendRequest sendRequest = SendRequest.childPaysForParent(wallet, toMeWithoutFee, feeRaise); wallet.signTransaction(sendRequest); wallet.commitTx(sendRequest.tx); assertEquals(Transaction.Purpose.RAISE_FEE, sendRequest.tx.getPurpose()); assertEquals(Coin.COIN.multiply(2).subtract(feeRaise), wallet.getBalance(BalanceType.ESTIMATED_SPENDABLE)); assertEquals(Coin.COIN, wallet.getBalance(BalanceType.AVAILABLE_SPENDABLE)); }
Example #26
Source File: WalletTest.java From green_android with GNU General Public License v3.0 | 6 votes |
@Test public void keyRotationHD() throws Exception { // Test that if we rotate an HD chain, a new one is created and all arrivals on the old keys are moved. Utils.setMockClock(); wallet = new Wallet(PARAMS); ECKey key1 = wallet.freshReceiveKey(); ECKey key2 = wallet.freshReceiveKey(); sendMoneyToWallet(wallet, AbstractBlockChain.NewBlockType.BEST_CHAIN, CENT, key1.toAddress(PARAMS)); sendMoneyToWallet(wallet, AbstractBlockChain.NewBlockType.BEST_CHAIN, CENT, key2.toAddress(PARAMS)); DeterministicKey watchKey1 = wallet.getWatchingKey(); // A day later, we get compromised. Utils.rollMockClock(86400); wallet.setKeyRotationTime(Utils.currentTimeSeconds()); List<Transaction> txns = wallet.doMaintenance(null, false).get(); assertEquals(1, txns.size()); DeterministicKey watchKey2 = wallet.getWatchingKey(); assertNotEquals(watchKey1, watchKey2); }
Example #27
Source File: WalletTest.java From bcm-android with GNU General Public License v3.0 | 6 votes |
@Test public void keyRotationHD() throws Exception { // Test that if we rotate an HD chain, a new one is created and all arrivals on the old keys are moved. Utils.setMockClock(); wallet = new Wallet(UNITTEST); ECKey key1 = wallet.freshReceiveKey(); ECKey key2 = wallet.freshReceiveKey(); sendMoneyToWallet(wallet, AbstractBlockChain.NewBlockType.BEST_CHAIN, CENT, LegacyAddress.fromKey(UNITTEST, key1)); sendMoneyToWallet(wallet, AbstractBlockChain.NewBlockType.BEST_CHAIN, CENT, LegacyAddress.fromKey(UNITTEST, key2)); DeterministicKey watchKey1 = wallet.getWatchingKey(); // A day later, we get compromised. Utils.rollMockClock(86400); wallet.setKeyRotationTime(Utils.currentTimeSeconds()); List<Transaction> txns = wallet.doMaintenance(null, false).get(); assertEquals(1, txns.size()); DeterministicKey watchKey2 = wallet.getWatchingKey(); assertNotEquals(watchKey1, watchKey2); }
Example #28
Source File: WalletTest.java From green_android with GNU General Public License v3.0 | 5 votes |
private void receiveATransactionAmount(Wallet wallet, Address toAddress, Coin amount) { final ListenableFuture<Coin> availFuture = wallet.getBalanceFuture(amount, Wallet.BalanceType.AVAILABLE); final ListenableFuture<Coin> estimatedFuture = wallet.getBalanceFuture(amount, Wallet.BalanceType.ESTIMATED); assertFalse(availFuture.isDone()); assertFalse(estimatedFuture.isDone()); // Send some pending coins to the wallet. Transaction t1 = sendMoneyToWallet(wallet, null, amount, toAddress); Threading.waitForUserCode(); final ListenableFuture<TransactionConfidence> depthFuture = t1.getConfidence().getDepthFuture(1); assertFalse(depthFuture.isDone()); assertEquals(ZERO, wallet.getBalance()); assertEquals(amount, wallet.getBalance(Wallet.BalanceType.ESTIMATED)); assertFalse(availFuture.isDone()); // Our estimated balance has reached the requested level. assertTrue(estimatedFuture.isDone()); assertEquals(1, wallet.getPoolSize(Pool.PENDING)); assertEquals(0, wallet.getPoolSize(Pool.UNSPENT)); // Confirm the coins. sendMoneyToWallet(wallet, AbstractBlockChain.NewBlockType.BEST_CHAIN, t1); assertEquals("Incorrect confirmed tx balance", amount, wallet.getBalance()); assertEquals("Incorrect confirmed tx PENDING pool size", 0, wallet.getPoolSize(Pool.PENDING)); assertEquals("Incorrect confirmed tx UNSPENT pool size", 1, wallet.getPoolSize(Pool.UNSPENT)); assertEquals("Incorrect confirmed tx ALL pool size", 1, wallet.getTransactions(true).size()); Threading.waitForUserCode(); assertTrue(availFuture.isDone()); assertTrue(estimatedFuture.isDone()); assertTrue(depthFuture.isDone()); }
Example #29
Source File: WalletTest.java From green_android with GNU General Public License v3.0 | 5 votes |
@Test @SuppressWarnings("deprecation") // Having a test for deprecated method getFromAddress() is no evil so we suppress the warning here. public void customTransactionSpending() throws Exception { // We'll set up a wallet that receives a coin, then sends a coin of lesser value and keeps the change. Coin v1 = valueOf(3, 0); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, v1); assertEquals(v1, wallet.getBalance()); assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT)); assertEquals(1, wallet.getTransactions(true).size()); Coin v2 = valueOf(0, 50); Coin v3 = valueOf(0, 75); Coin v4 = valueOf(1, 25); Transaction t2 = new Transaction(PARAMS); t2.addOutput(v2, OTHER_ADDRESS); t2.addOutput(v3, OTHER_ADDRESS); t2.addOutput(v4, OTHER_ADDRESS); SendRequest req = SendRequest.forTx(t2); wallet.completeTx(req); // Do some basic sanity checks. assertEquals(1, t2.getInputs().size()); assertEquals(myAddress, t2.getInput(0).getScriptSig().getFromAddress(PARAMS)); assertEquals(TransactionConfidence.ConfidenceType.UNKNOWN, t2.getConfidence().getConfidenceType()); // We have NOT proven that the signature is correct! wallet.commitTx(t2); assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.PENDING)); assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.SPENT)); assertEquals(2, wallet.getTransactions(true).size()); }
Example #30
Source File: WalletTest.java From GreenBits with GNU General Public License v3.0 | 5 votes |
@Test (expected = TransactionSigner.MissingSignatureException.class) public void completeTxPartiallySignedMarriedThrowsByDefault() throws Exception { createMarriedWallet(2, 2, false); myAddress = wallet.currentAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS); sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN, myAddress); SendRequest req = SendRequest.emptyWallet(OTHER_ADDRESS); wallet.completeTx(req); }