mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2024-11-20 10:12:19 +01:00
Optimize chain download further by skipping merkle root verification unless there are transactions relevant to a wallet in the block. Refactor some code out of WalletTest into a new static TestUtils class.
This commit is contained in:
parent
85caefbd4d
commit
48535f9a9c
@ -348,13 +348,13 @@ public class Block extends Message {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks the block data to ensure it follows the rules laid out in the network parameters. Specifically, throws
|
* Checks the block data to ensure it follows the rules laid out in the network parameters. Specifically, throws
|
||||||
* an exception if the proof of work is invalid, if the timestamp is too far from what it should be, or if the
|
* an exception if the proof of work is invalid, or if the timestamp is too far from what it should be. This is
|
||||||
* transactions don't hash to the value in the merkle root field. This is <b>not</b> everything that is required
|
* <b>not</b> everything that is required for a block to be valid, only what is checkable independent of the
|
||||||
* for a block to be valid, only what is checkable independent of the chain.
|
* chain and without a transaction index.
|
||||||
*
|
*
|
||||||
* @throws VerificationException
|
* @throws VerificationException
|
||||||
*/
|
*/
|
||||||
public void verify() throws VerificationException {
|
public void verifyHeader() throws VerificationException {
|
||||||
// Prove that this block is OK. It might seem that we can just ignore most of these checks given that the
|
// Prove that this block is OK. It might seem that we can just ignore most of these checks given that the
|
||||||
// network is also verifying the blocks, but we cannot as it'd open us to a variety of obscure attacks.
|
// network is also verifying the blocks, but we cannot as it'd open us to a variety of obscure attacks.
|
||||||
//
|
//
|
||||||
@ -362,15 +362,28 @@ public class Block extends Message {
|
|||||||
// enough, it's probably been done by the network.
|
// enough, it's probably been done by the network.
|
||||||
checkProofOfWork(true);
|
checkProofOfWork(true);
|
||||||
checkTimestamp();
|
checkTimestamp();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks the block contents
|
||||||
|
* @throws VerificationException
|
||||||
|
*/
|
||||||
|
public void verifyTransactions() throws VerificationException {
|
||||||
// Now we need to check that the body of the block actually matches the headers. The network won't generate
|
// Now we need to check that the body of the block actually matches the headers. The network won't generate
|
||||||
// an invalid block, but if we didn't validate this then an untrusted man-in-the-middle could obtain the next
|
// an invalid block, but if we didn't validate this then an untrusted man-in-the-middle could obtain the next
|
||||||
// valid block from the network and simply replace the transactions in it with their own fictional
|
// valid block from the network and simply replace the transactions in it with their own fictional
|
||||||
// transactions that reference spent or non-existant inputs.
|
// transactions that reference spent or non-existant inputs.
|
||||||
if (transactions != null) {
|
assert transactions.size() > 0;
|
||||||
assert transactions.size() > 0;
|
checkTransactions();
|
||||||
checkTransactions();
|
checkMerkleRoot();
|
||||||
checkMerkleRoot();
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
|
* Verifies both the header and that the transactions hash to the merkle root.
|
||||||
|
*/
|
||||||
|
public void verify() throws VerificationException {
|
||||||
|
verifyHeader();
|
||||||
|
verifyTransactions();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -490,7 +503,7 @@ public class Block extends Message {
|
|||||||
b.setTime(time);
|
b.setTime(time);
|
||||||
b.solve();
|
b.solve();
|
||||||
try {
|
try {
|
||||||
b.verify();
|
b.verifyHeader();
|
||||||
} catch (VerificationException e) {
|
} catch (VerificationException e) {
|
||||||
throw new RuntimeException(e); // Cannot happen.
|
throw new RuntimeException(e); // Cannot happen.
|
||||||
}
|
}
|
||||||
|
@ -149,12 +149,27 @@ public class BlockChain {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prove the block is internally valid: hash is lower than target, merkle root is correct and so on.
|
// Does this block contain any transactions we might care about? Check this up front before verifying the
|
||||||
|
// blocks validity so we can skip the merkle root verification if the contents aren't interesting. This saves
|
||||||
|
// a lot of time for big blocks.
|
||||||
|
boolean contentsImportant = false;
|
||||||
|
HashMap<Wallet, List<Transaction>> walletToTxMap = new HashMap<Wallet, List<Transaction>>();;
|
||||||
|
if (block.transactions != null) {
|
||||||
|
scanTransactions(block, walletToTxMap);
|
||||||
|
contentsImportant = walletToTxMap.size() > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prove the block is internally valid: hash is lower than target, etc. This only checks the block contents
|
||||||
|
// if there is a tx sending or receiving coins using an address in one of our wallets. And those transactions
|
||||||
|
// are only lightly verified: presence in a valid connecting block is taken as proof of validity. See the
|
||||||
|
// article here for more details: http://code.google.com/p/bitcoinj/wiki/SecurityModel
|
||||||
try {
|
try {
|
||||||
block.verify();
|
block.verifyHeader();
|
||||||
|
if (contentsImportant)
|
||||||
|
block.verifyTransactions();
|
||||||
} catch (VerificationException e) {
|
} catch (VerificationException e) {
|
||||||
log.error("Failed to verify block:", e);
|
log.error("Failed to verify block: ", e);
|
||||||
log.error(block.toString());
|
log.error(block.getHashAsString());
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -176,9 +191,7 @@ public class BlockChain {
|
|||||||
StoredBlock newStoredBlock = storedPrev.build(block);
|
StoredBlock newStoredBlock = storedPrev.build(block);
|
||||||
checkDifficultyTransitions(storedPrev, newStoredBlock);
|
checkDifficultyTransitions(storedPrev, newStoredBlock);
|
||||||
blockStore.put(newStoredBlock);
|
blockStore.put(newStoredBlock);
|
||||||
// block.transactions may be null here if we received only a header and not a full block. This does not
|
connectBlock(newStoredBlock, storedPrev, walletToTxMap);
|
||||||
// happen currently but might in future if getheaders is implemented.
|
|
||||||
connectBlock(newStoredBlock, storedPrev, block.transactions);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tryConnecting)
|
if (tryConnecting)
|
||||||
@ -188,7 +201,8 @@ public class BlockChain {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void connectBlock(StoredBlock newStoredBlock, StoredBlock storedPrev, List<Transaction> newTransactions)
|
private void connectBlock(StoredBlock newStoredBlock, StoredBlock storedPrev,
|
||||||
|
HashMap<Wallet, List<Transaction>> newTransactions)
|
||||||
throws BlockStoreException, VerificationException {
|
throws BlockStoreException, VerificationException {
|
||||||
if (storedPrev.equals(chainHead)) {
|
if (storedPrev.equals(chainHead)) {
|
||||||
// This block connects to the best known block, it is a normal continuation of the system.
|
// This block connects to the best known block, it is a normal continuation of the system.
|
||||||
@ -296,14 +310,16 @@ public class BlockChain {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void sendTransactionsToWallet(StoredBlock block, NewBlockType blockType,
|
private void sendTransactionsToWallet(StoredBlock block, NewBlockType blockType,
|
||||||
List<Transaction> newTransactions) throws VerificationException {
|
HashMap<Wallet, List<Transaction>> newTransactions) throws VerificationException {
|
||||||
// Scan the transactions to find out if any mention addresses we own.
|
for (Wallet wallet : newTransactions.keySet()) {
|
||||||
for (Transaction tx : newTransactions) {
|
|
||||||
try {
|
try {
|
||||||
scanTransaction(block, tx, blockType);
|
List<Transaction> txns = newTransactions.get(wallet);
|
||||||
|
for (Transaction tx : txns) {
|
||||||
|
wallet.receive(tx, block, blockType);
|
||||||
|
}
|
||||||
} catch (ScriptException e) {
|
} catch (ScriptException e) {
|
||||||
// We don't want scripts we don't understand to break the block chain,
|
// We don't want scripts we don't understand to break the block chain so just note that this tx was
|
||||||
// so just note that this tx was not scanned here and continue.
|
// not scanned here and continue.
|
||||||
log.warn("Failed to parse a script: " + e.toString());
|
log.warn("Failed to parse a script: " + e.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -409,33 +425,50 @@ public class BlockChain {
|
|||||||
receivedDifficulty.toString(16) + " vs " + newDifficulty.toString(16));
|
receivedDifficulty.toString(16) + " vs " + newDifficulty.toString(16));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void scanTransaction(StoredBlock block, Transaction tx, NewBlockType blockType)
|
/**
|
||||||
throws ScriptException, VerificationException {
|
* For the transactions in the given block, update the txToWalletMap such that each wallet maps to a list of
|
||||||
for (Wallet wallet : wallets) {
|
* transactions for which it is relevant.
|
||||||
boolean shouldReceive = false;
|
*/
|
||||||
for (TransactionOutput output : tx.outputs) {
|
private void scanTransactions(Block block, HashMap<Wallet, List<Transaction>> walletToTxMap)
|
||||||
// TODO: Handle more types of outputs, not just regular to address outputs.
|
throws VerificationException {
|
||||||
if (output.getScriptPubKey().isSentToIP()) return;
|
for (Transaction tx : block.transactions) {
|
||||||
// This is not thread safe as a key could be removed between the call to isMine and receive.
|
try {
|
||||||
if (output.isMine(wallet)) {
|
for (Wallet wallet : wallets) {
|
||||||
shouldReceive = true;
|
boolean shouldReceive = false;
|
||||||
}
|
for (TransactionOutput output : tx.outputs) {
|
||||||
}
|
// TODO: Handle more types of outputs, not just regular to address outputs.
|
||||||
|
if (output.getScriptPubKey().isSentToIP()) return;
|
||||||
// Coinbase transactions don't have anything useful in their inputs (as they create coins out of thin air).
|
// This is not thread safe as a key could be removed between the call to isMine and receive.
|
||||||
if (!tx.isCoinBase()) {
|
if (output.isMine(wallet)) {
|
||||||
for (TransactionInput i : tx.inputs) {
|
shouldReceive = true;
|
||||||
byte[] pubkey = i.getScriptSig().getPubKey();
|
}
|
||||||
// This is not thread safe as a key could be removed between the call to isPubKeyMine and receive.
|
|
||||||
if (wallet.isPubKeyMine(pubkey)) {
|
|
||||||
shouldReceive = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Coinbase transactions don't have anything useful in their inputs (as they create coins out of thin air).
|
||||||
|
if (!shouldReceive && !tx.isCoinBase()) {
|
||||||
|
for (TransactionInput i : tx.inputs) {
|
||||||
|
byte[] pubkey = i.getScriptSig().getPubKey();
|
||||||
|
// This is not thread safe as a key could be removed between the call to isPubKeyMine and receive.
|
||||||
|
if (wallet.isPubKeyMine(pubkey)) {
|
||||||
|
shouldReceive = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!shouldReceive) continue;
|
||||||
|
List<Transaction> txList = walletToTxMap.get(wallet);
|
||||||
|
if (txList == null) {
|
||||||
|
txList = new LinkedList<Transaction>();
|
||||||
|
walletToTxMap.put(wallet, txList);
|
||||||
|
}
|
||||||
|
txList.add(tx);
|
||||||
}
|
}
|
||||||
|
} catch (ScriptException e) {
|
||||||
|
// We don't want scripts we don't understand to break the block chain so just note that this tx was
|
||||||
|
// not scanned here and continue.
|
||||||
|
log.warn("Failed to parse a script: " + e.toString());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (shouldReceive)
|
|
||||||
wallet.receive(tx, block, blockType);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -115,7 +115,7 @@ public class DiskBlockStore implements BlockStore {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Don't try to verify the genesis block to avoid upsetting the unit tests.
|
// Don't try to verify the genesis block to avoid upsetting the unit tests.
|
||||||
b.verify();
|
b.verifyHeader();
|
||||||
// Calculate its height and total chain work.
|
// Calculate its height and total chain work.
|
||||||
s = prev.build(b);
|
s = prev.build(b);
|
||||||
}
|
}
|
||||||
|
@ -16,16 +16,18 @@
|
|||||||
|
|
||||||
package com.google.bitcoin.core;
|
package com.google.bitcoin.core;
|
||||||
|
|
||||||
import com.google.bitcoin.bouncycastle.util.encoders.Hex;
|
import com.google.bitcoin.store.BlockStore;
|
||||||
import com.google.bitcoin.store.MemoryBlockStore;
|
import com.google.bitcoin.store.MemoryBlockStore;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
|
|
||||||
|
import static com.google.bitcoin.core.TestUtils.createFakeBlock;
|
||||||
|
import static com.google.bitcoin.core.TestUtils.createFakeTx;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
// NOTE: Handling of chain splits/reorgs are in ChainSplitTests.
|
// Handling of chain splits/reorgs are in ChainSplitTests.
|
||||||
|
|
||||||
public class BlockChainTest {
|
public class BlockChainTest {
|
||||||
private static final NetworkParameters testNet = NetworkParameters.testNet();
|
private static final NetworkParameters testNet = NetworkParameters.testNet();
|
||||||
@ -33,21 +35,27 @@ public class BlockChainTest {
|
|||||||
|
|
||||||
private Wallet wallet;
|
private Wallet wallet;
|
||||||
private BlockChain chain;
|
private BlockChain chain;
|
||||||
|
private BlockStore blockStore;
|
||||||
private Address coinbaseTo;
|
private Address coinbaseTo;
|
||||||
private NetworkParameters unitTestParams;
|
private NetworkParameters unitTestParams;
|
||||||
private Address someOtherGuy;
|
|
||||||
|
private void resetBlockStore() {
|
||||||
|
blockStore = new MemoryBlockStore(unitTestParams);
|
||||||
|
}
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
|
|
||||||
testNetChain = new BlockChain(testNet, new Wallet(testNet), new MemoryBlockStore(testNet));
|
testNetChain = new BlockChain(testNet, new Wallet(testNet), new MemoryBlockStore(testNet));
|
||||||
|
|
||||||
unitTestParams = NetworkParameters.unitTests();
|
unitTestParams = NetworkParameters.unitTests();
|
||||||
wallet = new Wallet(unitTestParams);
|
wallet = new Wallet(unitTestParams);
|
||||||
wallet.addKey(new ECKey());
|
wallet.addKey(new ECKey());
|
||||||
chain = new BlockChain(unitTestParams, wallet, new MemoryBlockStore(unitTestParams));
|
|
||||||
|
resetBlockStore();
|
||||||
|
chain = new BlockChain(unitTestParams, wallet, blockStore);
|
||||||
|
|
||||||
coinbaseTo = wallet.keychain.get(0).toAddress(unitTestParams);
|
coinbaseTo = wallet.keychain.get(0).toAddress(unitTestParams);
|
||||||
someOtherGuy = new ECKey().toAddress(unitTestParams);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -68,10 +76,39 @@ public class BlockChainTest {
|
|||||||
} catch (VerificationException e) {
|
} catch (VerificationException e) {
|
||||||
b2.setNonce(n);
|
b2.setNonce(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now it works because we reset the nonce.
|
// Now it works because we reset the nonce.
|
||||||
assertTrue(testNetChain.add(b2));
|
assertTrue(testNetChain.add(b2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void merkleRoots() throws Exception {
|
||||||
|
// Test that merkle root verification takes place when a relevant transaction is present and doesn't when
|
||||||
|
// there isn't any such tx present (as an optimization).
|
||||||
|
Transaction tx1 = createFakeTx(unitTestParams,
|
||||||
|
Utils.toNanoCoins(1, 0),
|
||||||
|
wallet.keychain.get(0).toAddress(unitTestParams));
|
||||||
|
Block b1 = createFakeBlock(unitTestParams, blockStore, tx1).block;
|
||||||
|
chain.add(b1);
|
||||||
|
resetBlockStore();
|
||||||
|
Sha256Hash hash = b1.getMerkleRoot();
|
||||||
|
b1.setMerkleRoot(Sha256Hash.ZERO_HASH);
|
||||||
|
try {
|
||||||
|
chain.add(b1);
|
||||||
|
fail();
|
||||||
|
} catch (VerificationException e) {
|
||||||
|
// Expected.
|
||||||
|
b1.setMerkleRoot(hash);
|
||||||
|
}
|
||||||
|
// Now add a second block with no relevant transactions and then break it.
|
||||||
|
Transaction tx2 = createFakeTx(unitTestParams, Utils.toNanoCoins(1, 0),
|
||||||
|
new ECKey().toAddress(unitTestParams));
|
||||||
|
Block b2 = createFakeBlock(unitTestParams, blockStore, tx2).block;
|
||||||
|
hash = b2.getMerkleRoot();
|
||||||
|
b2.setMerkleRoot(Sha256Hash.ZERO_HASH);
|
||||||
|
chain.add(b2); // Broken block is accepted because its contents don't matter to us.
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUnconnectedBlocks() throws Exception {
|
public void testUnconnectedBlocks() throws Exception {
|
||||||
Block b1 = unitTestParams.genesisBlock.createNextBlock(coinbaseTo);
|
Block b1 = unitTestParams.genesisBlock.createNextBlock(coinbaseTo);
|
||||||
@ -163,7 +200,7 @@ public class BlockChainTest {
|
|||||||
b2.setTime(1296734343L);
|
b2.setTime(1296734343L);
|
||||||
b2.setPrevBlockHash(new Sha256Hash("000000033cc282bc1fa9dcae7a533263fd7fe66490f550d80076433340831604"));
|
b2.setPrevBlockHash(new Sha256Hash("000000033cc282bc1fa9dcae7a533263fd7fe66490f550d80076433340831604"));
|
||||||
assertEquals("000000037b21cac5d30fc6fda2581cf7b2612908aed2abbcc429c45b0557a15f", b2.getHashAsString());
|
assertEquals("000000037b21cac5d30fc6fda2581cf7b2612908aed2abbcc429c45b0557a15f", b2.getHashAsString());
|
||||||
b2.verify();
|
b2.verifyHeader();
|
||||||
return b2;
|
return b2;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,7 +211,7 @@ public class BlockChainTest {
|
|||||||
b1.setTime(1296734340);
|
b1.setTime(1296734340);
|
||||||
b1.setPrevBlockHash(new Sha256Hash("00000007199508e34a9ff81e6ec0c477a4cccff2a4767a8eee39c11db367b008"));
|
b1.setPrevBlockHash(new Sha256Hash("00000007199508e34a9ff81e6ec0c477a4cccff2a4767a8eee39c11db367b008"));
|
||||||
assertEquals("000000033cc282bc1fa9dcae7a533263fd7fe66490f550d80076433340831604", b1.getHashAsString());
|
assertEquals("000000033cc282bc1fa9dcae7a533263fd7fe66490f550d80076433340831604", b1.getHashAsString());
|
||||||
b1.verify();
|
b1.verifyHeader();
|
||||||
return b1;
|
return b1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
64
tests/com/google/bitcoin/core/TestUtils.java
Normal file
64
tests/com/google/bitcoin/core/TestUtils.java
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2011 Google Inc.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.google.bitcoin.core;
|
||||||
|
|
||||||
|
import com.google.bitcoin.store.BlockStore;
|
||||||
|
import com.google.bitcoin.store.BlockStoreException;
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
|
|
||||||
|
public class TestUtils {
|
||||||
|
public static Transaction createFakeTx(NetworkParameters params, BigInteger nanocoins, Address to) {
|
||||||
|
Transaction t = new Transaction(params);
|
||||||
|
TransactionOutput o1 = new TransactionOutput(params, t, nanocoins, to);
|
||||||
|
t.addOutput(o1);
|
||||||
|
// Make a previous tx simply to send us sufficient coins. This prev tx is not really valid but it doesn't
|
||||||
|
// matter for our purposes.
|
||||||
|
Transaction prevTx = new Transaction(params);
|
||||||
|
TransactionOutput prevOut = new TransactionOutput(params, prevTx, nanocoins, to);
|
||||||
|
prevTx.addOutput(prevOut);
|
||||||
|
// Connect it.
|
||||||
|
t.addInput(prevOut);
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class BlockPair {
|
||||||
|
StoredBlock storedBlock;
|
||||||
|
Block block;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Emulates receiving a valid block that builds on top of the chain.
|
||||||
|
public static BlockPair createFakeBlock(NetworkParameters params, BlockStore blockStore,
|
||||||
|
Transaction... transactions) {
|
||||||
|
try {
|
||||||
|
Block b = blockStore.getChainHead().getHeader().createNextBlock(new ECKey().toAddress(params));
|
||||||
|
for (Transaction tx : transactions)
|
||||||
|
b.addTransaction(tx);
|
||||||
|
b.solve();
|
||||||
|
BlockPair pair = new BlockPair();
|
||||||
|
pair.block = b;
|
||||||
|
pair.storedBlock = blockStore.getChainHead().build(b);
|
||||||
|
blockStore.put(pair.storedBlock);
|
||||||
|
blockStore.setChainHead(pair.storedBlock);
|
||||||
|
return pair;
|
||||||
|
} catch (VerificationException e) {
|
||||||
|
throw new RuntimeException(e); // Cannot happen.
|
||||||
|
} catch (BlockStoreException e) {
|
||||||
|
throw new RuntimeException(e); // Cannot happen.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -24,6 +24,8 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
|
|
||||||
|
import static com.google.bitcoin.core.TestUtils.createFakeBlock;
|
||||||
|
import static com.google.bitcoin.core.TestUtils.createFakeTx;
|
||||||
import static com.google.bitcoin.core.Utils.*;
|
import static com.google.bitcoin.core.Utils.*;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
@ -46,50 +48,11 @@ public class WalletTest {
|
|||||||
blockStore = new MemoryBlockStore(params);
|
blockStore = new MemoryBlockStore(params);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Transaction createFakeTx(BigInteger nanocoins, Address to) {
|
|
||||||
Transaction t = new Transaction(params);
|
|
||||||
TransactionOutput o1 = new TransactionOutput(params, t, nanocoins, to);
|
|
||||||
t.addOutput(o1);
|
|
||||||
// Make a previous tx simply to send us sufficient coins. This prev tx is not really valid but it doesn't
|
|
||||||
// matter for our purposes.
|
|
||||||
Transaction prevTx = new Transaction(params);
|
|
||||||
TransactionOutput prevOut = new TransactionOutput(params, prevTx, nanocoins, to);
|
|
||||||
prevTx.addOutput(prevOut);
|
|
||||||
// Connect it.
|
|
||||||
t.addInput(prevOut);
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
|
|
||||||
class BlockPair {
|
|
||||||
StoredBlock storedBlock;
|
|
||||||
Block block;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Emulates receiving a valid block that builds on top of the chain.
|
|
||||||
private BlockPair createFakeBlock(Transaction... transactions) {
|
|
||||||
try {
|
|
||||||
Block b = blockStore.getChainHead().getHeader().createNextBlock(new ECKey().toAddress(params));
|
|
||||||
for (Transaction tx : transactions)
|
|
||||||
b.addTransaction(tx);
|
|
||||||
b.solve();
|
|
||||||
BlockPair pair = new BlockPair();
|
|
||||||
pair.block = b;
|
|
||||||
pair.storedBlock = blockStore.getChainHead().build(b);
|
|
||||||
blockStore.put(pair.storedBlock);
|
|
||||||
blockStore.setChainHead(pair.storedBlock);
|
|
||||||
return pair;
|
|
||||||
} catch (VerificationException e) {
|
|
||||||
throw new RuntimeException(e); // Cannot happen.
|
|
||||||
} catch (BlockStoreException e) {
|
|
||||||
throw new RuntimeException(e); // Cannot happen.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void basicSpending() throws Exception {
|
public void basicSpending() throws Exception {
|
||||||
// We'll set up a wallet that receives a coin, then sends a coin of lesser value and keeps the change.
|
// We'll set up a wallet that receives a coin, then sends a coin of lesser value and keeps the change.
|
||||||
BigInteger v1 = Utils.toNanoCoins(1, 0);
|
BigInteger v1 = Utils.toNanoCoins(1, 0);
|
||||||
Transaction t1 = createFakeTx(v1, myAddress);
|
Transaction t1 = createFakeTx(params, v1, myAddress);
|
||||||
|
|
||||||
wallet.receive(t1, null, BlockChain.NewBlockType.BEST_CHAIN);
|
wallet.receive(t1, null, BlockChain.NewBlockType.BEST_CHAIN);
|
||||||
assertEquals(v1, wallet.getBalance());
|
assertEquals(v1, wallet.getBalance());
|
||||||
@ -109,13 +72,13 @@ public class WalletTest {
|
|||||||
public void sideChain() throws Exception {
|
public void sideChain() throws Exception {
|
||||||
// The wallet receives a coin on the main chain, then on a side chain. Only main chain counts towards balance.
|
// The wallet receives a coin on the main chain, then on a side chain. Only main chain counts towards balance.
|
||||||
BigInteger v1 = Utils.toNanoCoins(1, 0);
|
BigInteger v1 = Utils.toNanoCoins(1, 0);
|
||||||
Transaction t1 = createFakeTx(v1, myAddress);
|
Transaction t1 = createFakeTx(params, v1, myAddress);
|
||||||
|
|
||||||
wallet.receive(t1, null, BlockChain.NewBlockType.BEST_CHAIN);
|
wallet.receive(t1, null, BlockChain.NewBlockType.BEST_CHAIN);
|
||||||
assertEquals(v1, wallet.getBalance());
|
assertEquals(v1, wallet.getBalance());
|
||||||
|
|
||||||
BigInteger v2 = toNanoCoins(0, 50);
|
BigInteger v2 = toNanoCoins(0, 50);
|
||||||
Transaction t2 = createFakeTx(v2, myAddress);
|
Transaction t2 = createFakeTx(params, v2, myAddress);
|
||||||
wallet.receive(t2, null, BlockChain.NewBlockType.SIDE_CHAIN);
|
wallet.receive(t2, null, BlockChain.NewBlockType.SIDE_CHAIN);
|
||||||
|
|
||||||
assertEquals(v1, wallet.getBalance());
|
assertEquals(v1, wallet.getBalance());
|
||||||
@ -123,7 +86,7 @@ public class WalletTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void listeners() throws Exception {
|
public void listeners() throws Exception {
|
||||||
final Transaction fakeTx = createFakeTx(Utils.toNanoCoins(1, 0), myAddress);
|
final Transaction fakeTx = createFakeTx(params, Utils.toNanoCoins(1, 0), myAddress);
|
||||||
final boolean[] didRun = new boolean[1];
|
final boolean[] didRun = new boolean[1];
|
||||||
WalletEventListener listener = new WalletEventListener() {
|
WalletEventListener listener = new WalletEventListener() {
|
||||||
public void onCoinsReceived(Wallet w, Transaction tx, BigInteger prevBalance, BigInteger newBalance) {
|
public void onCoinsReceived(Wallet w, Transaction tx, BigInteger prevBalance, BigInteger newBalance) {
|
||||||
@ -144,10 +107,10 @@ public class WalletTest {
|
|||||||
// Receive 5 coins then half a coin.
|
// Receive 5 coins then half a coin.
|
||||||
BigInteger v1 = toNanoCoins(5, 0);
|
BigInteger v1 = toNanoCoins(5, 0);
|
||||||
BigInteger v2 = toNanoCoins(0, 50);
|
BigInteger v2 = toNanoCoins(0, 50);
|
||||||
Transaction t1 = createFakeTx(v1, myAddress);
|
Transaction t1 = createFakeTx(params, v1, myAddress);
|
||||||
Transaction t2 = createFakeTx(v2, myAddress);
|
Transaction t2 = createFakeTx(params, v2, myAddress);
|
||||||
StoredBlock b1 = createFakeBlock(t1).storedBlock;
|
StoredBlock b1 = createFakeBlock(params, blockStore, t1).storedBlock;
|
||||||
StoredBlock b2 = createFakeBlock(t2).storedBlock;
|
StoredBlock b2 = createFakeBlock(params, blockStore, t2).storedBlock;
|
||||||
BigInteger expected = toNanoCoins(5, 50);
|
BigInteger expected = toNanoCoins(5, 50);
|
||||||
wallet.receive(t1, b1, BlockChain.NewBlockType.BEST_CHAIN);
|
wallet.receive(t1, b1, BlockChain.NewBlockType.BEST_CHAIN);
|
||||||
wallet.receive(t2, b2, BlockChain.NewBlockType.BEST_CHAIN);
|
wallet.receive(t2, b2, BlockChain.NewBlockType.BEST_CHAIN);
|
||||||
@ -165,7 +128,7 @@ public class WalletTest {
|
|||||||
wallet.getBalance(Wallet.BalanceType.ESTIMATED)));
|
wallet.getBalance(Wallet.BalanceType.ESTIMATED)));
|
||||||
|
|
||||||
// Now confirm the transaction by including it into a block.
|
// Now confirm the transaction by including it into a block.
|
||||||
StoredBlock b3 = createFakeBlock(spend).storedBlock;
|
StoredBlock b3 = createFakeBlock(params, blockStore, spend).storedBlock;
|
||||||
wallet.receive(spend, b3, BlockChain.NewBlockType.BEST_CHAIN);
|
wallet.receive(spend, b3, BlockChain.NewBlockType.BEST_CHAIN);
|
||||||
|
|
||||||
// Change is confirmed. We started with 5.50 so we should have 4.50 left.
|
// Change is confirmed. We started with 5.50 so we should have 4.50 left.
|
||||||
@ -180,22 +143,22 @@ public class WalletTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void blockChainCatchup() throws Exception {
|
public void blockChainCatchup() throws Exception {
|
||||||
Transaction tx1 = createFakeTx(Utils.toNanoCoins(1, 0), myAddress);
|
Transaction tx1 = createFakeTx(params, Utils.toNanoCoins(1, 0), myAddress);
|
||||||
StoredBlock b1 = createFakeBlock(tx1).storedBlock;
|
StoredBlock b1 = createFakeBlock(params, blockStore, tx1).storedBlock;
|
||||||
wallet.receive(tx1, b1, BlockChain.NewBlockType.BEST_CHAIN);
|
wallet.receive(tx1, b1, BlockChain.NewBlockType.BEST_CHAIN);
|
||||||
// Send 0.10 to somebody else.
|
// Send 0.10 to somebody else.
|
||||||
Transaction send1 = wallet.createSend(new ECKey().toAddress(params), toNanoCoins(0, 10), myAddress);
|
Transaction send1 = wallet.createSend(new ECKey().toAddress(params), toNanoCoins(0, 10), myAddress);
|
||||||
// Pretend it makes it into the block chain, our wallet state is cleared but we still have the keys, and we
|
// Pretend it makes it into the block chain, our wallet state is cleared but we still have the keys, and we
|
||||||
// want to get back to our previous state. We can do this by just not confirming the transaction as
|
// want to get back to our previous state. We can do this by just not confirming the transaction as
|
||||||
// createSend is stateless.
|
// createSend is stateless.
|
||||||
StoredBlock b2 = createFakeBlock(send1).storedBlock;
|
StoredBlock b2 = createFakeBlock(params, blockStore, send1).storedBlock;
|
||||||
wallet.receive(send1, b2, BlockChain.NewBlockType.BEST_CHAIN);
|
wallet.receive(send1, b2, BlockChain.NewBlockType.BEST_CHAIN);
|
||||||
assertEquals(bitcoinValueToFriendlyString(wallet.getBalance()), "0.90");
|
assertEquals(bitcoinValueToFriendlyString(wallet.getBalance()), "0.90");
|
||||||
// And we do it again after the catchup.
|
// And we do it again after the catchup.
|
||||||
Transaction send2 = wallet.createSend(new ECKey().toAddress(params), toNanoCoins(0, 10), myAddress);
|
Transaction send2 = wallet.createSend(new ECKey().toAddress(params), toNanoCoins(0, 10), myAddress);
|
||||||
// What we'd really like to do is prove the official client would accept it .... no such luck unfortunately.
|
// What we'd really like to do is prove the official client would accept it .... no such luck unfortunately.
|
||||||
wallet.confirmSend(send2);
|
wallet.confirmSend(send2);
|
||||||
StoredBlock b3 = createFakeBlock(send2).storedBlock;
|
StoredBlock b3 = createFakeBlock(params, blockStore, send2).storedBlock;
|
||||||
wallet.receive(send2, b3, BlockChain.NewBlockType.BEST_CHAIN);
|
wallet.receive(send2, b3, BlockChain.NewBlockType.BEST_CHAIN);
|
||||||
assertEquals(bitcoinValueToFriendlyString(wallet.getBalance()), "0.80");
|
assertEquals(bitcoinValueToFriendlyString(wallet.getBalance()), "0.80");
|
||||||
}
|
}
|
||||||
@ -203,7 +166,7 @@ public class WalletTest {
|
|||||||
@Test
|
@Test
|
||||||
public void balances() throws Exception {
|
public void balances() throws Exception {
|
||||||
BigInteger nanos = Utils.toNanoCoins(1, 0);
|
BigInteger nanos = Utils.toNanoCoins(1, 0);
|
||||||
Transaction tx1 = createFakeTx(nanos, myAddress);
|
Transaction tx1 = createFakeTx(params, nanos, myAddress);
|
||||||
wallet.receive(tx1, null, BlockChain.NewBlockType.BEST_CHAIN);
|
wallet.receive(tx1, null, BlockChain.NewBlockType.BEST_CHAIN);
|
||||||
assertEquals(nanos, tx1.getValueSentToMe(wallet, true));
|
assertEquals(nanos, tx1.getValueSentToMe(wallet, true));
|
||||||
// Send 0.10 to somebody else.
|
// Send 0.10 to somebody else.
|
||||||
@ -216,7 +179,7 @@ public class WalletTest {
|
|||||||
@Test
|
@Test
|
||||||
public void transactions() throws Exception {
|
public void transactions() throws Exception {
|
||||||
// This test covers a bug in which Transaction.getValueSentFromMe was calculating incorrectly.
|
// This test covers a bug in which Transaction.getValueSentFromMe was calculating incorrectly.
|
||||||
Transaction tx = createFakeTx(Utils.toNanoCoins(1, 0), myAddress);
|
Transaction tx = createFakeTx(params, Utils.toNanoCoins(1, 0), myAddress);
|
||||||
// Now add another output (ie, change) that goes to some other address.
|
// Now add another output (ie, change) that goes to some other address.
|
||||||
Address someOtherGuy = new ECKey().toAddress(params);
|
Address someOtherGuy = new ECKey().toAddress(params);
|
||||||
TransactionOutput output = new TransactionOutput(params, tx, Utils.toNanoCoins(0, 5), someOtherGuy);
|
TransactionOutput output = new TransactionOutput(params, tx, Utils.toNanoCoins(0, 5), someOtherGuy);
|
||||||
@ -255,7 +218,7 @@ public class WalletTest {
|
|||||||
|
|
||||||
// Receive 1 BTC.
|
// Receive 1 BTC.
|
||||||
BigInteger nanos = Utils.toNanoCoins(1, 0);
|
BigInteger nanos = Utils.toNanoCoins(1, 0);
|
||||||
Transaction t1 = createFakeTx(nanos, myAddress);
|
Transaction t1 = createFakeTx(params, nanos, myAddress);
|
||||||
wallet.receive(t1, null, BlockChain.NewBlockType.BEST_CHAIN);
|
wallet.receive(t1, null, BlockChain.NewBlockType.BEST_CHAIN);
|
||||||
// Create a send to a merchant.
|
// Create a send to a merchant.
|
||||||
Transaction send1 = wallet.createSend(new ECKey().toAddress(params), toNanoCoins(0, 50));
|
Transaction send1 = wallet.createSend(new ECKey().toAddress(params), toNanoCoins(0, 50));
|
||||||
|
Loading…
Reference in New Issue
Block a user