17
17
18
18
import java .io .IOException ;
19
19
import java .time .Duration ;
20
+ import java .util .Arrays ;
20
21
import java .util .List ;
21
22
import java .util .Map ;
22
23
import java .util .Optional ;
23
24
import java .util .concurrent .atomic .AtomicLong ;
24
25
import java .util .function .Consumer ;
25
26
import java .util .function .Supplier ;
27
+ import java .util .stream .Stream ;
26
28
27
29
import static org .tbk .spring .testcontainer .core .MoreTestcontainers .buildInternalContainerUrlWithoutProtocol ;
28
30
import static org .tbk .spring .testcontainer .electrumd .config .ElectrumDaemonContainerProperties .ELECTRUM_NETWORK_ENV_NAME ;
@@ -33,8 +35,6 @@ public final class SimpleElectrumDaemonContainerFactory {
33
35
@ Value
34
36
@ Builder
35
37
public static class ElectrumDaemonContainerConfig {
36
- static final String ELECTRUM_NETWORK_ENV_NAME = "ELECTRUM_NETWORK" ;
37
-
38
38
private static final Map <String , String > defaultEnvironment = ImmutableMap .<String , String >builder ()
39
39
.put (ELECTRUM_NETWORK_ENV_NAME , "regtest" )
40
40
.build ();
@@ -125,7 +125,7 @@ private String dockerContainerName() {
125
125
.replace ("/" , "-" );
126
126
}
127
127
128
- private void copyWalletToContainerIfNecessary (ElectrumDaemonContainerConfig config , ElectrumDaemonContainer <?> electrumDaemonContainer ) {
128
+ private void copyWalletToContainerIfNecessary (ElectrumDaemonContainerConfig config , ElectrumDaemonContainer <?> container ) {
129
129
Optional <MountableFile > mountableWalletOrEmpty = config .getDefaultWallet ()
130
130
.map (MountableFile ::forClasspathResource );
131
131
@@ -150,23 +150,23 @@ private void copyWalletToContainerIfNecessary(ElectrumDaemonContainerConfig conf
150
150
log .debug ("copy file to container: {} -> {}" , filesystemPath , containerWalletFilePath );
151
151
}
152
152
153
- electrumDaemonContainer .withCopyFileToContainer (mountableWallet , containerWalletFilePath );
153
+ container .withCopyFileToContainer (mountableWallet , containerWalletFilePath );
154
154
}
155
155
}
156
156
157
- private void restartDaemonWithCustomizedSettings (ElectrumDaemonContainerConfig config , ElectrumDaemonContainer <?> electrumDaemonContainer , Supplier <Optional <String >> serverUrlSupplier ) {
158
- daemonStop (electrumDaemonContainer );
157
+ private void restartDaemonWithCustomizedSettings (ElectrumDaemonContainerConfig config , ElectrumDaemonContainer <?> container , Supplier <Optional <String >> serverUrlSupplier ) {
158
+ daemonStop (container );
159
159
160
- setupDefaultConfigValuesHack (electrumDaemonContainer );
160
+ setupDefaultConfigValuesHack (container );
161
161
162
162
serverUrlSupplier .get ().ifPresent (serverUrl -> {
163
- setupConnectingToServerHack (electrumDaemonContainer , serverUrl );
163
+ setupConnectingToServerHack (container , serverUrl );
164
164
});
165
165
166
- daemonStart (electrumDaemonContainer );
166
+ daemonStart (container );
167
167
168
168
// let the daemon some time to startup; 5000ms seems to be enough
169
- loadWalletIfNecessary (config , electrumDaemonContainer , Duration .ofMillis (5_000 ));
169
+ loadWalletIfNecessary (config , container , Duration .ofMillis (5_000 ));
170
170
}
171
171
172
172
/**
@@ -176,16 +176,16 @@ private void restartDaemonWithCustomizedSettings(ElectrumDaemonContainerConfig c
176
176
* the proper settings.
177
177
* TODO: try to make the docker setup more customizable e.g. like lnzap/docker-lnd does.
178
178
*/
179
- private void setupConnectingToServerHack (ElectrumDaemonContainer <?> electrumDaemonContainer , String serverUrl ) {
180
- updateElectrumConfigHack (electrumDaemonContainer , "server" , serverUrl );
181
- updateElectrumConfigHack (electrumDaemonContainer , "oneserver" , "true" );
179
+ private void setupConnectingToServerHack (ElectrumDaemonContainer <?> container , String serverUrl ) {
180
+ updateElectrumConfigHack (container , "server" , serverUrl );
181
+ updateElectrumConfigHack (container , "oneserver" , "true" );
182
182
}
183
183
184
- private void setupDefaultConfigValuesHack (ElectrumDaemonContainer <?> electrumDaemonContainer ) {
185
- updateElectrumConfigHack (electrumDaemonContainer , "auto_connect" , "true" );
186
- updateElectrumConfigHack (electrumDaemonContainer , "log_to_file" , "true" );
187
- updateElectrumConfigHack (electrumDaemonContainer , "check_updates" , "false" );
188
- updateElectrumConfigHack (electrumDaemonContainer , "dont_show_testnet_warning" , "true" );
184
+ private void setupDefaultConfigValuesHack (ElectrumDaemonContainer <?> container ) {
185
+ updateElectrumConfigHack (container , "auto_connect" , "true" );
186
+ updateElectrumConfigHack (container , "log_to_file" , "true" );
187
+ updateElectrumConfigHack (container , "check_updates" , "false" );
188
+ updateElectrumConfigHack (container , "dont_show_testnet_warning" , "true" );
189
189
}
190
190
191
191
private Optional <String > networkFlag (ElectrumDaemonContainer <?> container ) {
@@ -196,55 +196,40 @@ private Optional<String> networkFlag(ElectrumDaemonContainer<?> container) {
196
196
}
197
197
198
198
private void daemonStart (ElectrumDaemonContainer <?> electrumDaemonContainer ) {
199
- daemonExec (electrumDaemonContainer , "daemon" );
199
+ daemonExec (electrumDaemonContainer , "daemon" , "-d" );
200
200
}
201
201
202
202
private void daemonStop (ElectrumDaemonContainer <?> electrumDaemonContainer ) {
203
203
daemonExec (electrumDaemonContainer , "stop" );
204
204
}
205
205
206
- private void loadWalletIfNecessary (ElectrumDaemonContainerConfig config , ElectrumDaemonContainer <?> electrumDaemonContainer , Duration timeout ) {
206
+ private void loadWalletIfNecessary (ElectrumDaemonContainerConfig config , ElectrumDaemonContainer <?> container , Duration timeout ) {
207
207
if (config .getDefaultWallet ().isPresent ()) {
208
208
try {
209
209
Thread .sleep (timeout .toMillis ());
210
210
211
- daemonExec (electrumDaemonContainer , "load_wallet" );
211
+ daemonExec (container , "load_wallet" );
212
212
} catch (InterruptedException e ) {
213
213
throw new RuntimeException ("Error while adapting electrum-daemon: restart with auto-loading wallet failed" , e );
214
214
}
215
215
}
216
216
}
217
217
218
- private Container .ExecResult daemonExec (ElectrumDaemonContainer <?> electrumDaemonContainer , String command ) {
219
- try {
220
- Optional <String > networkFlag = networkFlag (electrumDaemonContainer );
221
-
222
- if (networkFlag .isEmpty ()) {
223
- return electrumDaemonContainer .execInContainer ("electrum" , command );
224
- } else {
225
- if ("daemon" .equals (command )) {
226
- return electrumDaemonContainer .execInContainer ("electrum" , networkFlag .get (), command , "-d" );
227
- } else {
228
- return electrumDaemonContainer .execInContainer ("electrum" , networkFlag .get (), command );
229
- }
230
- }
231
- } catch (InterruptedException | IOException e ) {
232
- String errorMessage = String .format ("Error while executing `electrum daemon %s`" , command );
233
- throw new RuntimeException (errorMessage , e );
234
- }
218
+ private Container .ExecResult updateElectrumConfigHack (ElectrumDaemonContainer <?> container , String key , String value ) {
219
+ return daemonExec (container , "--offline" , "setconfig" , key , value );
235
220
}
236
221
237
- private void updateElectrumConfigHack (ElectrumDaemonContainer <?> electrumDaemonContainer , String key , String value ) {
238
- try {
239
- Optional <String > networkFlag = networkFlag (electrumDaemonContainer );
222
+ private Container .ExecResult daemonExec (ElectrumDaemonContainer <?> container , String ... commands ) {
223
+ Optional <String > networkFlag = networkFlag (container );
240
224
241
- if (networkFlag .isEmpty ()) {
242
- electrumDaemonContainer .execInContainer ("electrum" , "--offline" , "setconfig" , key , value );
243
- } else {
244
- electrumDaemonContainer .execInContainer ("electrum" , "--offline" , networkFlag .get (), "setconfig" , key , value );
245
- }
225
+ String [] command = Stream .concat (Stream .of ("electrum" , networkFlag .orElse ("" )), Stream .of (commands ))
226
+ .filter (it -> !it .isEmpty ())
227
+ .toArray (String []::new );
228
+
229
+ try {
230
+ return container .execInContainer (command );
246
231
} catch (InterruptedException | IOException e ) {
247
- String errorMessage = String .format ("Error while executing `electrum setconfig %s %s `" , key , value );
232
+ String errorMessage = String .format ("Error while executing `%s `" , String . join ( " " , command ) );
248
233
throw new RuntimeException (errorMessage , e );
249
234
}
250
235
}
0 commit comments