From 16668390b3c0b5513a71bfc27804623e29e54d36 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Mon, 10 Dec 2012 23:30:22 +0100 Subject: [PATCH] Support full verification in wallet tool. --- tools/pom.xml | 7 +++- .../com/google/bitcoin/tools/WalletTool.java | 39 +++++++++++++------ tools/wallet-tool | 2 +- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/tools/pom.xml b/tools/pom.xml index 6ac097ac..edcbd978 100644 --- a/tools/pom.xml +++ b/tools/pom.xml @@ -39,7 +39,7 @@ 1.6 - true + false @@ -85,5 +85,10 @@ slf4j-jdk14 1.6.4 + + com.h2database + h2 + 1.3.167 + \ No newline at end of file diff --git a/tools/src/main/java/com/google/bitcoin/tools/WalletTool.java b/tools/src/main/java/com/google/bitcoin/tools/WalletTool.java index 3240cf70..8374824d 100644 --- a/tools/src/main/java/com/google/bitcoin/tools/WalletTool.java +++ b/tools/src/main/java/com/google/bitcoin/tools/WalletTool.java @@ -20,9 +20,7 @@ import com.google.bitcoin.core.*; import com.google.bitcoin.discovery.DnsDiscovery; import com.google.bitcoin.discovery.IrcDiscovery; import com.google.bitcoin.discovery.PeerDiscovery; -import com.google.bitcoin.store.BlockStoreException; -import com.google.bitcoin.store.BoundedOverheadBlockStore; -import com.google.bitcoin.store.WalletProtobufSerializer; +import com.google.bitcoin.store.*; import com.google.bitcoin.utils.BriefLogFormatter; import joptsimple.OptionParser; import joptsimple.OptionSet; @@ -61,6 +59,7 @@ public class WalletTool { ">>> GENERAL OPTIONS\n" + " --debuglog Enables logging from the core library.\n" + " --net=PROD/TEST Which network to connect to, defaults to PROD.\n" + + " --mode=FULL/SPV Whether to do full verification of the chain or just light mode.\n" + " --wallet= Specifies what wallet file to load and save.\n" + " --chain= Specifies the name of the file that stores the block chain.\n" + " --force Overrides any safety checks on the requested action.\n" + @@ -106,18 +105,20 @@ public class WalletTool { private static OptionSpec netFlag; private static OptionSpec dateFlag; private static OptionSpec waitForFlag; + private static OptionSpec modeFlag; private static OptionSpec conditionFlag; private static NetworkParameters params; private static File walletFile; private static OptionSet options; private static java.util.logging.Logger logger; - private static BoundedOverheadBlockStore store; - private static BlockChain chain; + private static BlockStore store; + private static AbstractBlockChain chain; private static PeerGroup peers; private static Wallet wallet; private static File chainFileName; private static PeerDiscovery discovery; + private static ValidationMode mode; public static class Condition { public enum Type { @@ -200,6 +201,11 @@ public class WalletTool { TEST } + public enum ValidationMode { + FULL, + SPV + } + public static void main(String[] args) throws Exception { OptionParser parser = new OptionParser(); parser.accepts("help"); @@ -222,6 +228,10 @@ public class WalletTool { waitForFlag = parser.accepts("waitfor") .withRequiredArg() .ofType(WaitForEnum.class); + modeFlag = parser.accepts("mode") + .withRequiredArg() + .ofType(ValidationMode.class) + .defaultsTo(ValidationMode.SPV); OptionSpec chainFlag = parser.accepts("chain").withRequiredArg(); // For addkey/delkey. parser.accepts("pubkey").withRequiredArg(); @@ -255,14 +265,16 @@ public class WalletTool { discovery = new DnsDiscovery(params); break; case TEST: - params = NetworkParameters.testNet(); + params = NetworkParameters.testNet3(); chainFileName = new File("testnet.chain"); - discovery = new IrcDiscovery("#bitcoinTEST"); + discovery = new IrcDiscovery("#bitcoinTEST3"); break; default: throw new RuntimeException("Unreachable."); } + mode = modeFlag.value(options); + // Allow the user to override the name of the chain used. if (options.has(chainFlag)) { chainFileName = new File(chainFlag.value(options)); @@ -300,7 +312,7 @@ public class WalletTool { try { WalletProtobufSerializer loader = new WalletProtobufSerializer(); - if (chainFileName.exists()) { + if (chainFileName.exists() && modeFlag.value(options).equals(ValidationMode.SPV)) { // TEMPORARY MIGRATION CODE FOR 0.5 -> 0.6 TRANSITION. This will ensure the depth (but NOT workDone) // fields gets set correctly for older wallets. store = new BoundedOverheadBlockStore(params, chainFileName); @@ -502,14 +514,19 @@ public class WalletTool { System.out.println("Chain file is missing so clearing transactions from the wallet."); reset(); } - store = new BoundedOverheadBlockStore(params, chainFileName); - chain = new BlockChain(params, wallet, store); + if (mode == ValidationMode.SPV) { + store = new BoundedOverheadBlockStore(params, chainFileName); + chain = new BlockChain(params, wallet, store); + } else if (mode == ValidationMode.FULL) { + FullPrunedBlockStore s = new H2FullPrunedBlockStore(params, chainFileName.getAbsolutePath(), 5000); + store = s; + chain = new FullPrunedBlockChain(params, wallet, s); + } // This will ensure the wallet is saved when it changes. wallet.autosaveToFile(walletFile, 200, TimeUnit.MILLISECONDS, null); peers = new PeerGroup(params, chain); peers.setUserAgent("WalletTool", "1.0"); peers.addWallet(wallet); - peers.setFastCatchupTimeSecs(wallet.getEarliestKeyCreationTime()); if (options.has("peers")) { String peersFlag = (String) options.valueOf("peers"); String[] peerAddrs = peersFlag.split(","); diff --git a/tools/wallet-tool b/tools/wallet-tool index 306a7143..aa75ea59 100755 --- a/tools/wallet-tool +++ b/tools/wallet-tool @@ -12,4 +12,4 @@ if [ ! -e target/bitcoinj-tools-*.jar ] || [[ "$ALWAYS_BUILD_WALLETTOOL" != "" ] mvn package -DskipTests fi -java -jar target/bitcoinj-tools-*.jar $* +java -Xmx4g -server -jar target/bitcoinj-tools-*.jar $*