initialize bwt only on connection, default to core multi-wallet use

This commit is contained in:
Craig Raw 2021-01-20 13:03:47 +02:00
parent a9a3eef157
commit aacecc8517
10 changed files with 58 additions and 29 deletions

View file

@ -69,7 +69,7 @@ dependencies {
exclude group: 'org.openjfx', module: 'javafx-web'
exclude group: 'org.openjfx', module: 'javafx-media'
}
implementation('dev.bwt:bwt-jni:0.1.6')
implementation('dev.bwt:bwt-jni:0.1.7')
testImplementation('junit:junit:4.12')
}

2
drongo

@ -1 +1 @@
Subproject commit 3e91bdb46cd235247550b2b579285a05609c0837
Subproject commit 93d494fcde0d6979b2ded2a9d5ebd5d54d58b8ea

View file

@ -30,7 +30,7 @@
<key>CFBundleVersion</key>
<string>100</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright (C) 2020</string>
<string>Copyright (C) 2021</string>
<key>NSHighResolutionCapable</key>
<string>true</string>
<key>NSCameraUsageDescription</key>

View file

@ -66,6 +66,9 @@ public class MainApp extends Application {
if(Config.get().getServerType() == null && Config.get().getCoreServer() == null && Config.get().getElectrumServer() != null) {
Config.get().setServerType(ServerType.ELECTRUM_SERVER);
} else if(Config.get().getServerType() == ServerType.BITCOIN_CORE && Config.get().getCoreWallet() == null) {
Config.get().setCoreMultiWallet(Boolean.TRUE);
Config.get().setCoreWallet("");
}
AppController appController = AppServices.newAppWindow(stage);

View file

@ -44,6 +44,7 @@ public class Config {
private CoreAuthType coreAuthType;
private File coreDataDir;
private String coreAuth;
private Boolean coreMultiWallet;
private String coreWallet;
private String electrumServer;
private File electrumServerCert;
@ -298,12 +299,21 @@ public class Config {
flush();
}
public Boolean getCoreMultiWallet() {
return coreMultiWallet;
}
public void setCoreMultiWallet(Boolean coreMultiWallet) {
this.coreMultiWallet = coreMultiWallet;
flush();
}
public String getCoreWallet() {
return coreWallet;
}
public void setCoreWallet(String coreWallet) {
this.coreWallet = (coreWallet == null || coreWallet.isEmpty() ? null : coreWallet);
this.coreWallet = coreWallet;
flush();
}

View file

@ -25,23 +25,29 @@ import java.util.stream.Collectors;
public class Bwt {
private static final Logger log = LoggerFactory.getLogger(Bwt.class);
public static final String DEFAULT_CORE_WALLET = "sparrow";
private static final int IMPORT_BATCH_SIZE = 350;
private static boolean initialized;
private Long shutdownPtr;
private boolean terminating;
private boolean ready;
static {
try {
org.controlsfx.tools.Platform platform = org.controlsfx.tools.Platform.getCurrent();
if(platform == org.controlsfx.tools.Platform.OSX) {
NativeUtils.loadLibraryFromJar("/native/osx/x64/libbwt_jni.dylib");
} else if(platform == org.controlsfx.tools.Platform.WINDOWS) {
NativeUtils.loadLibraryFromJar("/native/windows/x64/bwt_jni.dll");
} else {
NativeUtils.loadLibraryFromJar("/native/linux/x64/libbwt_jni.so");
public synchronized static void initialize() {
if(!initialized) {
try {
org.controlsfx.tools.Platform platform = org.controlsfx.tools.Platform.getCurrent();
if(platform == org.controlsfx.tools.Platform.OSX) {
NativeUtils.loadLibraryFromJar("/native/osx/x64/libbwt_jni.dylib");
} else if(platform == org.controlsfx.tools.Platform.WINDOWS) {
NativeUtils.loadLibraryFromJar("/native/windows/x64/bwt_jni.dll");
} else {
NativeUtils.loadLibraryFromJar("/native/linux/x64/libbwt_jni.so");
}
initialized = true;
} catch(IOException e) {
log.error("Error loading bwt library", e);
}
} catch(IOException e) {
log.error("Error loading bwt library", e);
}
}
@ -111,9 +117,10 @@ public class Bwt {
} else {
bwtConfig.bitcoindAuth = config.getCoreAuth();
}
if(config.getCoreWallet() != null && !config.getCoreWallet().isEmpty()) {
if(config.getCoreMultiWallet() != Boolean.FALSE) {
bwtConfig.bitcoindWallet = config.getCoreWallet();
}
bwtConfig.createWalletIfMissing = true;
Gson gson = new Gson();
String jsonConfig = gson.toJson(bwtConfig);
@ -178,6 +185,9 @@ public class Bwt {
@SerializedName("bitcoind_wallet")
public String bitcoindWallet;
@SerializedName("create_wallet_if_missing")
public Boolean createWalletIfMissing;
@SerializedName("descriptors")
public List<String> descriptors;

View file

@ -779,6 +779,7 @@ public class ElectrumServer {
private final Bwt bwt = new Bwt();
private final ReentrantLock bwtStartLock = new ReentrantLock();
private final Condition bwtStartCondition = bwtStartLock.newCondition();
private Throwable bwtStartException;
private final StringProperty statusProperty = new SimpleStringProperty();
public ConnectionService() {
@ -796,10 +797,14 @@ public class ElectrumServer {
ElectrumServer electrumServer = new ElectrumServer();
if(Config.get().getServerType() == ServerType.BITCOIN_CORE) {
Bwt.initialize();
if(!bwt.isRunning()) {
Bwt.ConnectionService bwtConnectionService = bwt.getConnectionService(subscribe ? AppServices.get().getOpenWallets().keySet() : null);
bwtStartException = null;
bwtConnectionService.setOnFailed(workerStateEvent -> {
log.error("Failed to start BWT", workerStateEvent.getSource().getException());
bwtStartException = workerStateEvent.getSource().getException();
try {
bwtStartLock.lock();
bwtStartCondition.signal();
@ -813,8 +818,12 @@ public class ElectrumServer {
bwtStartLock.lock();
bwtStartCondition.await();
if(!bwt.isRunning()) {
throw new ServerException("Check if Bitcoin Core is running, and the authentication details are correct.");
if(!bwt.isReady()) {
if(bwtStartException != null && bwtStartException.getMessage().contains("Wallet file not specified")) {
throw new ServerException("Bitcoin Core requires Multi-Wallet to be enabled in the Server Preferences");
} else {
throw new ServerException("Check if Bitcoin Core is running, and the authentication details are correct.");
}
}
} catch(InterruptedException e) {
Thread.currentThread().interrupt();

View file

@ -205,10 +205,8 @@ public class ServerPreferencesController extends PreferencesDetailController {
});
coreMultiWallet.selectedProperty().addListener((observable, oldValue, newValue) -> {
coreWallet.setText(" ");
coreWallet.setText("");
config.setCoreMultiWallet(newValue);
coreWallet.setDisable(!newValue);
coreWallet.setPromptText(newValue ? "" : "Default");
});
electrumUseSsl.selectedProperty().addListener((observable, oldValue, newValue) -> {
@ -307,12 +305,15 @@ public class ServerPreferencesController extends PreferencesDetailController {
}
}
coreMultiWallet.setSelected(true);
coreMultiWallet.setSelected(config.getCoreWallet() != null);
if(config.getCoreWallet() != null) {
coreWallet.setPromptText("Default");
if(config.getCoreWallet() == null) {
coreWallet.setText(Bwt.DEFAULT_CORE_WALLET);
} else {
coreWallet.setText(config.getCoreWallet());
}
coreMultiWallet.setSelected(config.getCoreMultiWallet() != Boolean.FALSE);
String electrumServer = config.getElectrumServer();
if(electrumServer != null) {
Protocol protocol = Protocol.getProtocol(electrumServer);
@ -452,10 +453,6 @@ public class ServerPreferencesController extends PreferencesDetailController {
(Control c, String newValue) -> ValidationResult.fromErrorIf( c, "Core pass required", coreAuthToggleGroup.getSelectedToggle().getUserData() == CoreAuthType.USERPASS && newValue.isEmpty())
));
validationSupport.registerValidator(coreWallet, Validator.combine(
(Control c, String newValue) -> ValidationResult.fromErrorIf( c, "Core wallet required", coreMultiWallet.isSelected() && newValue.isEmpty())
));
validationSupport.registerValidator(electrumHost, Validator.combine(
(Control c, String newValue) -> ValidationResult.fromErrorIf( c, "Invalid Electrum host", getHost(newValue) == null)
));

View file

@ -89,7 +89,7 @@
<PasswordField fx:id="corePass"/>
</Field>
<Field text="Multi-Wallet:">
<UnlabeledToggleSwitch fx:id="coreMultiWallet"/> <HelpLabel helpText="Enable this if using multiple Bitcoin Core wallets" />
<UnlabeledToggleSwitch fx:id="coreMultiWallet"/> <HelpLabel helpText="Creates a new Bitcoin Core wallet with the following name (recommended to avoid conflicts)" />
</Field>
<Field text="Wallet Name:" styleClass="label-button">
<TextField fx:id="coreWallet"/>