From 8f76a1d37be25821cc567833a24906d6aae77205 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Tue, 11 Nov 2014 10:29:25 +0100 Subject: [PATCH] Introduce explicit wallet and useragent params in WalletFacade Previously, WalletFacade relied on "appName" to derive all information related to the location of the bitcoinj wallet, the prefix of that wallet, and the useragent name that will be used for bitcoin version messages. Now explicit parameters have been exposed for each of these, making for a clearer and more configurable arrangement. The values associated with each parameter still default to the value of "appName" (usually "Bitsquare", "Bitsquare-Alice", or similar), however the assignment of these defaults is now done in BitsquareEnvironment#defaultProperties PropertySource. This approach allows for overriding any or all of these parameters in any of the property sources that have higher precedence than the default set, (e.g. in system environment variables, the bitsquare.properties file, etc). As a result of these changes, WalletFacade now has no awareness whatsover of the Bitsquare "application", which is as it should be. This change removes a conceptual tangle, and what would have become a code-level tangle had we tried to replace the use of @Named("appName") with a reference to BitsquareEnvironment#APP_NAME_KEY. This begins a series of such changes, in which references to "appName" will be eliminated in favor of similar explicit parameters. --- .../bitsquare/app/BitsquareEnvironment.java | 5 ++ .../java/io/bitsquare/btc/BitcoinModule.java | 16 ++++-- .../java/io/bitsquare/btc/WalletFacade.java | 49 ++++++++++++------- 3 files changed, 50 insertions(+), 20 deletions(-) diff --git a/src/main/java/io/bitsquare/app/BitsquareEnvironment.java b/src/main/java/io/bitsquare/app/BitsquareEnvironment.java index a3e2cd55b1..aef285ecb5 100644 --- a/src/main/java/io/bitsquare/app/BitsquareEnvironment.java +++ b/src/main/java/io/bitsquare/app/BitsquareEnvironment.java @@ -18,6 +18,7 @@ package io.bitsquare.app; import io.bitsquare.BitsquareException; +import io.bitsquare.btc.WalletFacade; import com.google.common.base.Preconditions; @@ -69,6 +70,10 @@ public class BitsquareEnvironment extends StandardEnvironment { private PropertySource defaultProperties(String appName) { return new PropertiesPropertySource(BITSQUARE_DEFAULT_PROPERTY_SOURCE_NAME, new Properties() {{ setProperty(APP_NAME_KEY, appName); + setProperty(WalletFacade.DIR_KEY, AppDirectory.dir(appName).toString()); + setProperty(WalletFacade.PREFIX_KEY, appName); + setProperty(WalletFacade.USERAGENT_NAME_KEY, appName); + setProperty(WalletFacade.USERAGENT_VERSION_KEY, "0.1"); }}); } diff --git a/src/main/java/io/bitsquare/btc/BitcoinModule.java b/src/main/java/io/bitsquare/btc/BitcoinModule.java index 1d77a557ff..7e133d97fc 100644 --- a/src/main/java/io/bitsquare/btc/BitcoinModule.java +++ b/src/main/java/io/bitsquare/btc/BitcoinModule.java @@ -26,8 +26,13 @@ import org.bitcoinj.params.TestNet3Params; import com.google.inject.Injector; +import java.io.File; + import org.springframework.core.env.Environment; +import static com.google.inject.name.Names.named; +import static io.bitsquare.btc.WalletFacade.*; + public class BitcoinModule extends BitsquareModule { public static final String BITCOIN_NETWORK_KEY = "bitcoin.network"; @@ -39,10 +44,15 @@ public class BitcoinModule extends BitsquareModule { @Override protected void configure() { - bind(WalletFacade.class).asEagerSingleton(); - bind(FeePolicy.class).asEagerSingleton(); - bind(BlockChainFacade.class).asEagerSingleton(); + File walletDir = new File(env.getRequiredProperty(WalletFacade.DIR_KEY)); + bind(File.class).annotatedWith(named(WalletFacade.DIR_KEY)).toInstance(walletDir); + bindConstant().annotatedWith(named(WalletFacade.PREFIX_KEY)).to(env.getRequiredProperty(WalletFacade.PREFIX_KEY)); + bindConstant().annotatedWith(named(USERAGENT_NAME_KEY)).to(env.getRequiredProperty(USERAGENT_NAME_KEY)); + bindConstant().annotatedWith(named(USERAGENT_VERSION_KEY)).to(env.getRequiredProperty(USERAGENT_VERSION_KEY)); bind(NetworkParameters.class).toInstance(network()); + bind(FeePolicy.class).asEagerSingleton(); + bind(WalletFacade.class).asEagerSingleton(); + bind(BlockChainFacade.class).asEagerSingleton(); } @Override diff --git a/src/main/java/io/bitsquare/btc/WalletFacade.java b/src/main/java/io/bitsquare/btc/WalletFacade.java index 91e5ead604..5445060a40 100644 --- a/src/main/java/io/bitsquare/btc/WalletFacade.java +++ b/src/main/java/io/bitsquare/btc/WalletFacade.java @@ -57,6 +57,7 @@ import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.Service; +import java.io.File; import java.io.Serializable; import java.math.BigInteger; @@ -84,8 +85,6 @@ import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import lighthouse.files.AppDirectory; - import static org.bitcoinj.script.ScriptOpCodes.OP_RETURN; /** @@ -94,37 +93,53 @@ import static org.bitcoinj.script.ScriptOpCodes.OP_RETURN; */ public class WalletFacade { private static final Logger log = LoggerFactory.getLogger(WalletFacade.class); + private static final String LOCK_NAME = "lock"; + + public static final String DIR_KEY = "wallet.dir"; + public static final String PREFIX_KEY = "wallet.prefix"; + public static final String USERAGENT_NAME_KEY = "bitcoin.useragent.name"; + public static final String USERAGENT_VERSION_KEY = "bitcoin.useragent.version"; - private final ReentrantLock lock = Threading.lock("lock"); - private final NetworkParameters params; - private WalletAppKit walletAppKit; - private final FeePolicy feePolicy; - private final CryptoFacade cryptoFacade; - private final Persistence persistence; - private final String appName; private final List addressConfidenceListeners = new CopyOnWriteArrayList<>(); private final List txConfidenceListeners = new CopyOnWriteArrayList<>(); private final List balanceListeners = new CopyOnWriteArrayList<>(); + private final ReentrantLock lock = Threading.lock(LOCK_NAME); + + private final NetworkParameters params; + private final FeePolicy feePolicy; + private final CryptoFacade cryptoFacade; + private final Persistence persistence; + private final File walletDir; + private final String walletPrefix; + private final String userAgentName; + private final String userAgentVersion; + + private WalletAppKit walletAppKit; private Wallet wallet; private WalletEventListener walletEventListener; private AddressEntry registrationAddressEntry; private AddressEntry arbitratorDepositAddressEntry; - - @GuardedBy("lock") - private List addressEntryList = new ArrayList<>(); + private @GuardedBy(LOCK_NAME) List addressEntryList = new ArrayList<>(); /////////////////////////////////////////////////////////////////////////////////////////// // Constructor /////////////////////////////////////////////////////////////////////////////////////////// @Inject - public WalletFacade(NetworkParameters params, FeePolicy feePolicy, CryptoFacade cryptoFacade, - Persistence persistence, @Named("appName") String appName) { + public WalletFacade(NetworkParameters params, FeePolicy feePolicy, + CryptoFacade cryptoFacade, Persistence persistence, + @Named(DIR_KEY) File walletDir, + @Named(PREFIX_KEY) String walletPrefix, + @Named(USERAGENT_NAME_KEY) String userAgentName, + @Named(USERAGENT_VERSION_KEY) String userAgentVersion) { this.params = params; this.feePolicy = feePolicy; this.cryptoFacade = cryptoFacade; this.persistence = persistence; - this.appName = appName; + this.walletDir = walletDir; + this.walletPrefix = walletPrefix; + this.userAgentName = userAgentName; + this.userAgentVersion = userAgentVersion; } @@ -141,7 +156,7 @@ public class WalletFacade { Threading.USER_THREAD = executor; // If seed is non-null it means we are restoring from backup. - walletAppKit = new WalletAppKit(params, AppDirectory.dir(appName).toFile(), appName) { + walletAppKit = new WalletAppKit(params, walletDir, walletPrefix) { @Override protected void onSetupCompleted() { // Don't make the user wait for confirmations for now, as the intention is they're sending it @@ -195,7 +210,7 @@ public class WalletFacade { walletAppKit.setDownloadListener(downloadListener) .setBlockingStartup(false) - .setUserAgent(appName, "0.1"); + .setUserAgent(userAgentName, userAgentVersion); /* // TODO restore from DeterministicSeed