From fb90c4458d50726b6d30cf09685d9047dd56459a Mon Sep 17 00:00:00 2001
From: Andreas Schildbach
Date: Fri, 24 Jun 2022 02:26:54 +0200
Subject: [PATCH] Context: remove network parameters
This is the first step in the goal of making Context optional again,
using it only for configuration of a test environment.
---
.../org/bitcoinj/core/AbstractBlockChain.java | 16 +------
.../java/org/bitcoinj/core/BlockChain.java | 21 ++-------
.../org/bitcoinj/core/CheckpointManager.java | 4 +-
.../main/java/org/bitcoinj/core/Context.java | 26 +++--------
.../bitcoinj/core/FullPrunedBlockChain.java | 35 +++------------
.../java/org/bitcoinj/core/PeerGroup.java | 37 +++++-----------
.../java/org/bitcoinj/kits/WalletAppKit.java | 21 ---------
.../main/java/org/bitcoinj/wallet/Wallet.java | 44 ++-----------------
.../AbstractFullPrunedBlockChainTest.java | 5 +--
.../org/bitcoinj/core/BlockChainTest.java | 8 ++--
.../java/org/bitcoinj/core/BlockTest.java | 3 +-
.../org/bitcoinj/core/BloomFilterTest.java | 2 +-
.../org/bitcoinj/core/ChainSplitTest.java | 2 +-
.../org/bitcoinj/core/ParseByteCacheTest.java | 3 +-
.../bitcoinj/core/TransactionInputTest.java | 4 +-
.../bitcoinj/core/TxConfidenceTableTest.java | 2 +-
.../store/WalletProtobufSerializerTest.java | 2 +
.../org/bitcoinj/testing/TestWithWallet.java | 2 +-
.../wallet/DefaultRiskAnalysisTest.java | 3 +-
...FilteredBlockAndPartialMerkleTreeTest.java | 2 +-
.../testing/TestWithNetworkConnections.java | 2 +-
.../wallet/WalletAccountPathTest.java | 2 +-
.../org/bitcoinj/wallet/WalletLoadTest.java | 2 +
.../org/bitcoinj/tools/BuildCheckpoints.java | 2 +-
.../org/bitcoinj/wallettool/WalletTool.java | 2 +-
25 files changed, 57 insertions(+), 195 deletions(-)
diff --git a/core/src/main/java/org/bitcoinj/core/AbstractBlockChain.java b/core/src/main/java/org/bitcoinj/core/AbstractBlockChain.java
index d59639a22..fd5f98d4f 100644
--- a/core/src/main/java/org/bitcoinj/core/AbstractBlockChain.java
+++ b/core/src/main/java/org/bitcoinj/core/AbstractBlockChain.java
@@ -167,22 +167,10 @@ public abstract class AbstractBlockChain {
*/
public AbstractBlockChain(NetworkParameters params, List extends Wallet> wallets,
BlockStore blockStore) throws BlockStoreException {
- this(Context.getOrCreate(params), wallets, blockStore);
- }
-
- /**
- * Constructs a BlockChain connected to the given list of listeners (wallets) and a store.
- * @param context context for this network
- * @param wallets list of listeners (wallets)
- * @param blockStore where to store blocks
- * @throws BlockStoreException if a failure occurs while storing a block
- */
- public AbstractBlockChain(Context context, List extends Wallet> wallets,
- BlockStore blockStore) throws BlockStoreException {
this.blockStore = blockStore;
chainHead = blockStore.getChainHead();
log.info("chain head is at height {}:\n{}", chainHead.getHeight(), chainHead.getHeader());
- this.params = context.getParams();
+ this.params = params;
this.newBestBlockListeners = new CopyOnWriteArrayList<>();
this.reorganizeListeners = new CopyOnWriteArrayList<>();
@@ -191,7 +179,7 @@ public abstract class AbstractBlockChain {
for (ReorganizeListener l : wallets) addReorganizeListener(Threading.SAME_THREAD, l);
for (TransactionReceivedInBlockListener l : wallets) addTransactionReceivedListener(Threading.SAME_THREAD, l);
- this.versionTally = new VersionTally(context.getParams());
+ this.versionTally = new VersionTally(params);
this.versionTally.initialize(blockStore, chainHead);
}
diff --git a/core/src/main/java/org/bitcoinj/core/BlockChain.java b/core/src/main/java/org/bitcoinj/core/BlockChain.java
index 027365130..572c2041b 100644
--- a/core/src/main/java/org/bitcoinj/core/BlockChain.java
+++ b/core/src/main/java/org/bitcoinj/core/BlockChain.java
@@ -51,25 +51,15 @@ public class BlockChain extends AbstractBlockChain {
* {@link MemoryBlockStore} if you want to hold all headers in RAM and don't care about
* disk serialization (this is rare).
*/
- public BlockChain(Context context, Wallet wallet, BlockStore blockStore) throws BlockStoreException {
- this(context, new ArrayList(), blockStore);
- addWallet(wallet);
- }
-
- /** See {@link #BlockChain(Context, Wallet, BlockStore)}} */
public BlockChain(NetworkParameters params, Wallet wallet, BlockStore blockStore) throws BlockStoreException {
- this(Context.getOrCreate(params), wallet, blockStore);
+ this(params, new ArrayList(), blockStore);
+ addWallet(wallet);
}
/**
* Constructs a BlockChain that has no wallet at all. This is helpful when you don't actually care about sending
* and receiving coins but rather, just want to explore the network data structures.
*/
- public BlockChain(Context context, BlockStore blockStore) throws BlockStoreException {
- this(context, new ArrayList(), blockStore);
- }
-
- /** See {@link #BlockChain(Context, BlockStore)} */
public BlockChain(NetworkParameters params, BlockStore blockStore) throws BlockStoreException {
this(params, new ArrayList(), blockStore);
}
@@ -77,16 +67,11 @@ public class BlockChain extends AbstractBlockChain {
/**
* Constructs a BlockChain connected to the given list of listeners and a store.
*/
- public BlockChain(Context params, List extends Wallet> wallets, BlockStore blockStore) throws BlockStoreException {
+ public BlockChain(NetworkParameters params, List extends Wallet> wallets, BlockStore blockStore) throws BlockStoreException {
super(params, wallets, blockStore);
this.blockStore = blockStore;
}
- /** See {@link #BlockChain(Context, List, BlockStore)} */
- public BlockChain(NetworkParameters params, List extends Wallet> wallets, BlockStore blockStore) throws BlockStoreException {
- this(Context.getOrCreate(params), wallets, blockStore);
- }
-
@Override
protected StoredBlock addToBlockStore(StoredBlock storedPrev, Block blockHeader, TransactionOutputChanges txOutChanges)
throws BlockStoreException, VerificationException {
diff --git a/core/src/main/java/org/bitcoinj/core/CheckpointManager.java b/core/src/main/java/org/bitcoinj/core/CheckpointManager.java
index c38ea3b24..af680e916 100644
--- a/core/src/main/java/org/bitcoinj/core/CheckpointManager.java
+++ b/core/src/main/java/org/bitcoinj/core/CheckpointManager.java
@@ -89,8 +89,8 @@ public class CheckpointManager {
public static final BaseEncoding BASE64 = BaseEncoding.base64().omitPadding();
/** Loads the default checkpoints bundled with bitcoinj */
- public CheckpointManager(Context context) throws IOException {
- this(context.getParams(), null);
+ public CheckpointManager(NetworkParameters params) throws IOException {
+ this(params, null);
}
/** Loads the checkpoints from the given stream */
diff --git a/core/src/main/java/org/bitcoinj/core/Context.java b/core/src/main/java/org/bitcoinj/core/Context.java
index 48eacb156..1f65af76f 100644
--- a/core/src/main/java/org/bitcoinj/core/Context.java
+++ b/core/src/main/java/org/bitcoinj/core/Context.java
@@ -49,7 +49,6 @@ public class Context {
public static final int DEFAULT_EVENT_HORIZON = 100;
final private TxConfidenceTable confidenceTable;
- final private NetworkParameters params;
final private int eventHorizon;
final private boolean ensureMinRequiredFee;
final private Coin feePerKb;
@@ -57,25 +56,21 @@ public class Context {
/**
* Creates a new context object. For now, this will be done for you by the framework. Eventually you will be
* expected to do this yourself in the same manner as fetching a NetworkParameters object (at the start of your app).
- *
- * @param params The network parameters that will be associated with this context.
*/
- public Context(NetworkParameters params) {
- this(params, DEFAULT_EVENT_HORIZON, Transaction.DEFAULT_TX_FEE, true);
+ public Context() {
+ this(DEFAULT_EVENT_HORIZON, Transaction.DEFAULT_TX_FEE, true);
}
/**
* Creates a new custom context object. This is mainly meant for unit tests for now.
*
- * @param params The network parameters that will be associated with this context.
* @param eventHorizon Number of blocks after which the library will delete data and be unable to always process reorgs. See {@link #getEventHorizon()}.
* @param feePerKb The default fee per 1000 virtual bytes of transaction data to pay when completing transactions. For details, see {@link SendRequest#feePerKb}.
* @param ensureMinRequiredFee Whether to ensure the minimum required fee by default when completing transactions. For details, see {@link SendRequest#ensureMinRequiredFee}.
*/
- public Context(NetworkParameters params, int eventHorizon, Coin feePerKb, boolean ensureMinRequiredFee) {
+ public Context(int eventHorizon, Coin feePerKb, boolean ensureMinRequiredFee) {
log.info("Creating bitcoinj {} context.", VersionMessage.BITCOINJ_VERSION);
this.confidenceTable = new TxConfidenceTable();
- this.params = params;
this.eventHorizon = eventHorizon;
this.ensureMinRequiredFee = ensureMinRequiredFee;
this.feePerKb = feePerKb;
@@ -129,17 +124,15 @@ public class Context {
}
// A temporary internal shim designed to help us migrate internally in a way that doesn't wreck source compatibility.
- public static Context getOrCreate(NetworkParameters params) {
+ public static Context getOrCreate() {
Context context;
try {
context = get();
} catch (IllegalStateException e) {
log.warn("Implicitly creating context. This is a migration step and this message will eventually go away.");
- context = new Context(params);
+ context = new Context();
return context;
}
- if (context.getParams() != params)
- throw new IllegalStateException("Context does not match implicit network params: " + context.getParams() + " vs " + params);
return context;
}
@@ -163,15 +156,6 @@ public class Context {
return confidenceTable;
}
- /**
- * Returns the {@link NetworkParameters} specified when this context was (auto) created. The
- * network parameters defines various hard coded constants for a specific instance of a Bitcoin network, such as
- * main net, testnet, etc.
- */
- public NetworkParameters getParams() {
- return params;
- }
-
/**
* The event horizon is the number of blocks after which various bits of the library consider a transaction to be
* so confirmed that it's safe to delete data. Re-orgs larger than the event horizon will not be correctly
diff --git a/core/src/main/java/org/bitcoinj/core/FullPrunedBlockChain.java b/core/src/main/java/org/bitcoinj/core/FullPrunedBlockChain.java
index ba9d92a0a..cdd3d0cc3 100644
--- a/core/src/main/java/org/bitcoinj/core/FullPrunedBlockChain.java
+++ b/core/src/main/java/org/bitcoinj/core/FullPrunedBlockChain.java
@@ -66,57 +66,34 @@ public class FullPrunedBlockChain extends AbstractBlockChain {
// Whether or not to execute scriptPubKeys before accepting a transaction (i.e. check signatures).
private boolean runScripts = true;
- /**
- * Constructs a block chain connected to the given wallet and store. To obtain a {@link Wallet} you can construct
- * one from scratch, or you can deserialize a saved wallet from disk using
- * {@link Wallet#loadFromFile(File, WalletExtension...)}
- */
- public FullPrunedBlockChain(Context context, Wallet wallet, FullPrunedBlockStore blockStore) throws BlockStoreException {
- this(context, new ArrayList(), blockStore);
- addWallet(wallet);
- }
-
/**
* Constructs a block chain connected to the given wallet and store. To obtain a {@link Wallet} you can construct
* one from scratch, or you can deserialize a saved wallet from disk using
* {@link Wallet#loadFromFile(File, WalletExtension...)}
*/
public FullPrunedBlockChain(NetworkParameters params, Wallet wallet, FullPrunedBlockStore blockStore) throws BlockStoreException {
- this(Context.getOrCreate(params), wallet, blockStore);
+ this(params, new ArrayList(), blockStore);
+ addWallet(wallet);
}
/**
* Constructs a block chain connected to the given store.
*/
- public FullPrunedBlockChain(Context context, FullPrunedBlockStore blockStore) throws BlockStoreException {
- this(context, new ArrayList(), blockStore);
- }
-
- /**
- * See {@link #FullPrunedBlockChain(Context, Wallet, FullPrunedBlockStore)}
- */
public FullPrunedBlockChain(NetworkParameters params, FullPrunedBlockStore blockStore) throws BlockStoreException {
- this(Context.getOrCreate(params), blockStore);
+ this(params, new ArrayList(), blockStore);
}
/**
* Constructs a block chain connected to the given list of wallets and a store.
*/
- public FullPrunedBlockChain(Context context, List listeners, FullPrunedBlockStore blockStore) throws BlockStoreException {
- super(context, listeners, blockStore);
+ public FullPrunedBlockChain(NetworkParameters params, List listeners,
+ FullPrunedBlockStore blockStore) throws BlockStoreException {
+ super(params, listeners, blockStore);
this.blockStore = blockStore;
// Ignore upgrading for now
this.chainHead = blockStore.getVerifiedChainHead();
}
- /**
- * See {@link #FullPrunedBlockChain(Context, List, FullPrunedBlockStore)}
- */
- public FullPrunedBlockChain(NetworkParameters params, List listeners,
- FullPrunedBlockStore blockStore) throws BlockStoreException {
- this(Context.getOrCreate(params), listeners, blockStore);
- }
-
@Override
protected StoredBlock addToBlockStore(StoredBlock storedPrev, Block header, TransactionOutputChanges txOutChanges)
throws BlockStoreException, VerificationException {
diff --git a/core/src/main/java/org/bitcoinj/core/PeerGroup.java b/core/src/main/java/org/bitcoinj/core/PeerGroup.java
index b57b72bf8..342feeb39 100644
--- a/core/src/main/java/org/bitcoinj/core/PeerGroup.java
+++ b/core/src/main/java/org/bitcoinj/core/PeerGroup.java
@@ -329,45 +329,30 @@ public class PeerGroup implements TransactionBroadcaster {
/** Whether bloom filter support is enabled when using a non FullPrunedBlockchain*/
private volatile boolean vBloomFilteringEnabled = true;
- /** See {@link #PeerGroup(Context)} */
+ /**
+ * Creates a PeerGroup with the given network. No chain is provided so this node will report its chain height
+ * as zero to other peers. This constructor is useful if you just want to explore the network but aren't interested
+ * in downloading block data.
+ */
public PeerGroup(NetworkParameters params) {
this(params, null);
}
/**
- * Creates a PeerGroup with the given context. No chain is provided so this node will report its chain height
- * as zero to other peers. This constructor is useful if you just want to explore the network but aren't interested
- * in downloading block data.
- */
- public PeerGroup(Context context) {
- this(context, null);
- }
-
- /** See {@link #PeerGroup(Context, AbstractBlockChain)} */
- public PeerGroup(NetworkParameters params, @Nullable AbstractBlockChain chain) {
- this(Context.getOrCreate(params), chain, new NioClientManager());
- }
-
- /**
- * Creates a PeerGroup for the given context and chain. Blocks will be passed to the chain as they are broadcast
+ * Creates a PeerGroup for the given network and chain. Blocks will be passed to the chain as they are broadcast
* and downloaded. This is probably the constructor you want to use.
*/
- public PeerGroup(Context context, @Nullable AbstractBlockChain chain) {
- this(context, chain, new NioClientManager());
- }
-
- /** See {@link #PeerGroup(Context, AbstractBlockChain, ClientConnectionManager)} */
- public PeerGroup(NetworkParameters params, @Nullable AbstractBlockChain chain, ClientConnectionManager connectionManager) {
- this(Context.getOrCreate(params), chain, connectionManager);
+ public PeerGroup(NetworkParameters params, @Nullable AbstractBlockChain chain) {
+ this(params, chain, new NioClientManager());
}
/**
* Creates a new PeerGroup allowing you to specify the {@link ClientConnectionManager} which is used to create new
* connections and keep track of existing ones.
*/
- private PeerGroup(Context context, @Nullable AbstractBlockChain chain, ClientConnectionManager connectionManager) {
- checkNotNull(context);
- this.params = context.getParams();
+ protected PeerGroup(NetworkParameters params, @Nullable AbstractBlockChain chain, ClientConnectionManager connectionManager) {
+ checkNotNull(params);
+ this.params = params;
this.chain = chain;
fastCatchupTimeSecs = params.getGenesisBlock().getTimeSeconds();
wallets = new CopyOnWriteArrayList<>();
diff --git a/core/src/main/java/org/bitcoinj/kits/WalletAppKit.java b/core/src/main/java/org/bitcoinj/kits/WalletAppKit.java
index b413c300b..875e6ca5d 100644
--- a/core/src/main/java/org/bitcoinj/kits/WalletAppKit.java
+++ b/core/src/main/java/org/bitcoinj/kits/WalletAppKit.java
@@ -110,8 +110,6 @@ public class WalletAppKit extends AbstractIdleService {
@Nullable protected DeterministicKey restoreFromKey;
@Nullable protected PeerDiscovery discovery;
- protected volatile Context context;
-
/**
* Creates a new WalletAppKit, with a newly created {@link Context}. Files will be stored in the given directory.
* @deprecated Use {@link #WalletAppKit(BitcoinNetwork, ScriptType, KeyChainGroupStructure, File, String)}
@@ -144,29 +142,12 @@ public class WalletAppKit extends AbstractIdleService {
KeyChainGroupStructure structure, File directory, String filePrefix) {
this.network = checkNotNull(network);
this.params = NetworkParameters.of(this.network);
- this.context = new Context(params);
this.preferredOutputScriptType = checkNotNull(preferredOutputScriptType);
this.structure = checkNotNull(structure);
this.directory = checkNotNull(directory);
this.filePrefix = checkNotNull(filePrefix);
}
- /**
- * Creates a new WalletAppKit, with the given {@link Context}. Files will be stored in the given directory.
- * @deprecated Use {@link #WalletAppKit(BitcoinNetwork, ScriptType, KeyChainGroupStructure, File, String)}
- */
- @Deprecated
- public WalletAppKit(Context context, ScriptType preferredOutputScriptType,
- @Nullable KeyChainGroupStructure structure, File directory, String filePrefix) {
- this.context = context;
- this.params = checkNotNull(context.getParams());
- this.network = params.network();
- this.preferredOutputScriptType = checkNotNull(preferredOutputScriptType);
- this.structure = structure != null ? structure : KeyChainGroupStructure.BIP32;
- this.directory = checkNotNull(directory);
- this.filePrefix = checkNotNull(filePrefix);
- }
-
/** Will only connect to the given addresses. Cannot be called after startup. */
public WalletAppKit setPeerNodes(PeerAddress... addresses) {
checkState(state() == State.NEW, "Cannot call after startup");
@@ -331,7 +312,6 @@ public class WalletAppKit extends AbstractIdleService {
@Override
protected void startUp() throws Exception {
// Runs in a separate thread.
- Context.propagate(context);
if (!directory.exists()) {
if (!directory.mkdirs()) {
throw new IOException("Could not create directory " + directory.getAbsolutePath());
@@ -507,7 +487,6 @@ public class WalletAppKit extends AbstractIdleService {
protected void shutDown() throws Exception {
// Runs in a separate thread.
try {
- Context.propagate(context);
vPeerGroup.stop();
vWallet.saveToFile(vWalletFile);
vStore.close();
diff --git a/core/src/main/java/org/bitcoinj/wallet/Wallet.java b/core/src/main/java/org/bitcoinj/wallet/Wallet.java
index 489b72419..c64a667c8 100644
--- a/core/src/main/java/org/bitcoinj/wallet/Wallet.java
+++ b/core/src/main/java/org/bitcoinj/wallet/Wallet.java
@@ -233,7 +233,6 @@ public class Wallet extends BaseTaggableObject
// A list of scripts watched by this wallet.
@GuardedBy("keyChainGroupLock") private final Set