tools: Add id() and networkParameters() methods to Network (was NetworkEnum)

* Rename from `NetworkEnum` to `Network`
* Use new methods to simplify BuildCheckpoints and WalletTool
* Remove unused imports
This commit is contained in:
Sean Gilligan 2021-12-22 13:08:19 -08:00 committed by Andreas Schildbach
parent bd6e1c6a09
commit 3c3279fa04
4 changed files with 64 additions and 48 deletions

View file

@ -17,17 +17,12 @@
package org.bitcoinj.tools;
import org.bitcoinj.core.listeners.NewBestBlockListener;
import org.bitcoinj.core.*;
import org.bitcoinj.net.discovery.DnsDiscovery;
import org.bitcoinj.params.MainNetParams;
import org.bitcoinj.params.RegTestParams;
import org.bitcoinj.params.TestNet3Params;
import org.bitcoinj.store.BlockStore;
import org.bitcoinj.store.MemoryBlockStore;
import org.bitcoinj.utils.BriefLogFormatter;
import org.bitcoinj.utils.Threading;
import com.google.common.io.Resources;
import picocli.CommandLine;
import java.io.DataOutputStream;
@ -58,7 +53,7 @@ import static java.util.concurrent.TimeUnit.SECONDS;
@CommandLine.Command(name = "build-checkpoints", usageHelpAutoWidth = true, sortOptions = false, description = "Create checkpoint files to use with CheckpointManager.")
public class BuildCheckpoints implements Callable<Integer> {
@CommandLine.Option(names = "--net", description = "Which network to connect to. Valid values: ${COMPLETION-CANDIDATES}. Default: ${DEFAULT-VALUE}")
private NetworkEnum net = NetworkEnum.MAIN;
private Network net = Network.MAIN;
@CommandLine.Option(names = "--peer", description = "IP address/domain name for connection instead of localhost.")
private String peer = null;
@CommandLine.Option(names = "--days", description = "How many days to keep as a safety margin. Checkpointing will be done up to this many days ago.")
@ -77,18 +72,16 @@ public class BuildCheckpoints implements Callable<Integer> {
@Override
public Integer call() throws Exception {
final String suffix;
params = net.networkParameters();
switch (net) {
case MAIN:
case PROD:
params = MainNetParams.get();
suffix = "";
break;
case TEST:
params = TestNet3Params.get();
suffix = "-testnet";
break;
case REGTEST:
params = RegTestParams.get();
suffix = "-regtest";
break;
default:

View file

@ -0,0 +1,53 @@
/*
* Copyright by the original author or authors.
*
* 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 org.bitcoinj.tools;
import org.bitcoinj.core.NetworkParameters;
/**
* A convenient {@code enum} representation of a network.
*/
public enum Network {
MAIN(NetworkParameters.ID_MAINNET),
PROD(NetworkParameters.ID_MAINNET), // alias for MAIN
TEST(NetworkParameters.ID_TESTNET),
REGTEST(NetworkParameters.ID_REGTEST);
private final String id;
Network(String networkId) {
id = networkId;
}
/**
* Get the network id string as specified in {@link NetworkParameters}
*
* @return The network id string
*/
public String id() {
return id;
}
/**
* Get the associated {@link NetworkParameters}
*
* @return The network parameters
*/
public NetworkParameters networkParameters() {
return NetworkParameters.fromID(id);
}
}

View file

@ -1,24 +0,0 @@
/*
* Copyright 2014 Mike Hearn
*
* 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 org.bitcoinj.tools;
public enum NetworkEnum {
MAIN,
PROD, // alias for MAIN
TEST,
REGTEST
}

View file

@ -20,9 +20,7 @@ package org.bitcoinj.tools;
import org.bitcoinj.core.Sha256Hash;
import org.bitcoinj.core.TransactionOutput;
import org.bitcoinj.crypto.*;
import org.bitcoinj.params.MainNetParams;
import org.bitcoinj.params.RegTestParams;
import org.bitcoinj.params.TestNet3Params;
import org.bitcoinj.protocols.payments.PaymentProtocol;
import org.bitcoinj.protocols.payments.PaymentProtocolException;
import org.bitcoinj.protocols.payments.PaymentSession;
@ -51,14 +49,12 @@ import org.bitcoinj.core.AbstractBlockChain;
import org.bitcoinj.core.Address;
import org.bitcoinj.core.AddressFormatException;
import org.bitcoinj.core.Base58;
import org.bitcoinj.core.Block;
import org.bitcoinj.core.BlockChain;
import org.bitcoinj.core.CheckpointManager;
import org.bitcoinj.core.Coin;
import org.bitcoinj.core.Context;
import org.bitcoinj.core.DumpedPrivateKey;
import org.bitcoinj.core.ECKey;
import org.bitcoinj.core.FilteredBlock;
import org.bitcoinj.core.FullPrunedBlockChain;
import org.bitcoinj.core.InsufficientMoneyException;
import org.bitcoinj.core.LegacyAddress;
@ -71,7 +67,6 @@ import org.bitcoinj.core.StoredBlock;
import org.bitcoinj.core.Transaction;
import org.bitcoinj.core.Utils;
import org.bitcoinj.core.VerificationException;
import org.bitcoinj.core.listeners.BlocksDownloadedEventListener;
import org.bitcoinj.core.listeners.DownloadProgressTracker;
import org.bitcoinj.wallet.MarriedKeyChain;
import org.bitcoinj.wallet.Protos;
@ -175,7 +170,7 @@ public class WalletTool implements Callable<Integer> {
" If you omit both options, the creation time is being cleared (set to 0).%n")
private String actionStr;
@CommandLine.Option(names = "--net", description = "Which network to connect to. Valid values: ${COMPLETION-CANDIDATES}. Default: ${DEFAULT-VALUE}")
private NetworkEnum net = NetworkEnum.MAIN;
private Network net = Network.MAIN;
@CommandLine.Option(names = "--debuglog", description = "Enables logging from the core library.")
private boolean debugLog = false;
@CommandLine.Option(names = "--force", description = "Overrides any safety checks on the requested action.")
@ -371,26 +366,25 @@ public class WalletTool implements Callable<Integer> {
java.util.logging.Logger logger = LogManager.getLogManager().getLogger("");
logger.setLevel(Level.SEVERE);
}
params = net.networkParameters();
String fileName;
switch (net) {
case MAIN:
case PROD:
params = MainNetParams.get();
if (chainFile == null)
chainFile = new File("mainnet.chain");
fileName = "mainnet.chain";
break;
case TEST:
params = TestNet3Params.get();
if (chainFile == null)
chainFile = new File("testnet.chain");
fileName = "testnet.chain";
break;
case REGTEST:
params = RegTestParams.get();
if (chainFile == null)
chainFile = new File("regtest.chain");
fileName = "regtest.chain";
break;
default:
throw new RuntimeException("Unreachable.");
}
if (chainFile == null) {
chainFile = new File(fileName);
}
Context.propagate(new Context(params));
if (conditionStr != null) {