-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
SystemProperties.java
967 lines (825 loc) · 33.9 KB
/
SystemProperties.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
/*
* Copyright (c) [2016] [ <ether.camp> ]
* This file is part of the ethereumJ library.
*
* The ethereumJ library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ethereumJ library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>.
*/
package org.ethereum.config;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigObject;
import com.typesafe.config.ConfigRenderOptions;
import org.apache.commons.lang3.tuple.Pair;
import org.ethereum.config.blockchain.OlympicConfig;
import org.ethereum.config.net.*;
import org.ethereum.core.Genesis;
import org.ethereum.core.genesis.GenesisConfig;
import org.ethereum.core.genesis.GenesisJson;
import org.ethereum.core.genesis.GenesisLoader;
import org.ethereum.crypto.ECKey;
import org.ethereum.net.p2p.P2pHandler;
import org.ethereum.net.rlpx.MessageCodec;
import org.ethereum.net.rlpx.Node;
import org.ethereum.util.BuildInfo;
import org.ethereum.util.ByteUtil;
import org.ethereum.util.Utils;
import org.ethereum.validator.BlockCustomHashRule;
import org.ethereum.validator.BlockHeaderValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongycastle.util.encoders.Hex;
import java.io.*;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;
import java.util.*;
import static org.ethereum.crypto.HashUtil.sha3;
import static org.ethereum.util.ByteUtil.toHexString;
/**
* Utility class to retrieve property values from the ethereumj.conf files
*
* The properties are taken from different sources and merged in the following order
* (the config option from the next source overrides option from previous):
* - resource ethereumj.conf : normally used as a reference config with default values
* and shouldn't be changed
* - system property : each config entry might be altered via -D VM option
* - [user dir]/config/ethereumj.conf
* - config specified with the -Dethereumj.conf.file=[file.conf] VM option
* - CLI options
*
* @author Roman Mandeleil
* @since 22.05.2014
*/
public class SystemProperties {
private static Logger logger = LoggerFactory.getLogger("general");
public final static String PROPERTY_DB_DIR = "database.dir";
public final static String PROPERTY_LISTEN_PORT = "peer.listen.port";
public final static String PROPERTY_PEER_ACTIVE = "peer.active";
public final static String PROPERTY_DB_RESET = "database.reset";
public final static String PROPERTY_PEER_DISCOVERY_ENABLED = "peer.discovery.enabled";
/* Testing */
private final static Boolean DEFAULT_VMTEST_LOAD_LOCAL = false;
private final static String DEFAULT_BLOCKS_LOADER = "";
private static SystemProperties CONFIG;
private static boolean useOnlySpringConfig = false;
private String generatedNodePrivateKey;
/**
* Returns the static config instance. If the config is passed
* as a Spring bean by the application this instance shouldn't
* be used
* This method is mainly used for testing purposes
* (Autowired fields are initialized with this static instance
* but when running within Spring context they replaced with the
* bean config instance)
*/
public static SystemProperties getDefault() {
return useOnlySpringConfig ? null : getSpringDefault();
}
static SystemProperties getSpringDefault() {
if (CONFIG == null) {
CONFIG = new SystemProperties();
}
return CONFIG;
}
public static void resetToDefault() {
CONFIG = null;
}
/**
* Used mostly for testing purposes to ensure the application
* refers only to the config passed as a Spring bean.
* If this property is set to true {@link #getDefault()} returns null
*/
public static void setUseOnlySpringConfig(boolean useOnlySpringConfig) {
SystemProperties.useOnlySpringConfig = useOnlySpringConfig;
}
static boolean isUseOnlySpringConfig() {
return useOnlySpringConfig;
}
/**
* Marks config accessor methods which need to be called (for value validation)
* upon config creation or modification
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
private @interface ValidateMe {};
private Config config;
// mutable options for tests
private String databaseDir = null;
private Boolean databaseReset = null;
private String projectVersion = null;
private String projectVersionModifier = null;
protected Integer databaseVersion = null;
private String genesisInfo = null;
private String bindIp = null;
private String externalIp = null;
private Boolean syncEnabled = null;
private Boolean discoveryEnabled = null;
private GenesisJson genesisJson;
private BlockchainNetConfig blockchainConfig;
private Genesis genesis;
private Boolean vmTrace;
private Boolean recordInternalTransactionsData;
private final ClassLoader classLoader;
private GenerateNodeIdStrategy generateNodeIdStrategy = null;
public SystemProperties() {
this(ConfigFactory.empty());
}
public SystemProperties(File configFile) {
this(ConfigFactory.parseFile(configFile));
}
public SystemProperties(String configResource) {
this(ConfigFactory.parseResources(configResource));
}
public SystemProperties(Config apiConfig) {
this(apiConfig, SystemProperties.class.getClassLoader());
}
public SystemProperties(Config apiConfig, ClassLoader classLoader) {
try {
this.classLoader = classLoader;
Config javaSystemProperties = ConfigFactory.load("no-such-resource-only-system-props");
Config referenceConfig = ConfigFactory.parseResources("ethereumj.conf");
logger.info("Config (" + (referenceConfig.entrySet().size() > 0 ? " yes " : " no ") + "): default properties from resource 'ethereumj.conf'");
String res = System.getProperty("ethereumj.conf.res");
Config cmdLineConfigRes = res != null ? ConfigFactory.parseResources(res) : ConfigFactory.empty();
logger.info("Config (" + (cmdLineConfigRes.entrySet().size() > 0 ? " yes " : " no ") + "): user properties from -Dethereumj.conf.res resource '" + res + "'");
Config userConfig = ConfigFactory.parseResources("user.conf");
logger.info("Config (" + (userConfig.entrySet().size() > 0 ? " yes " : " no ") + "): user properties from resource 'user.conf'");
File userDirFile = new File(System.getProperty("user.dir"), "/config/ethereumj.conf");
Config userDirConfig = ConfigFactory.parseFile(userDirFile);
logger.info("Config (" + (userDirConfig.entrySet().size() > 0 ? " yes " : " no ") + "): user properties from file '" + userDirFile + "'");
Config testConfig = ConfigFactory.parseResources("test-ethereumj.conf");
logger.info("Config (" + (testConfig.entrySet().size() > 0 ? " yes " : " no ") + "): test properties from resource 'test-ethereumj.conf'");
Config testUserConfig = ConfigFactory.parseResources("test-user.conf");
logger.info("Config (" + (testUserConfig.entrySet().size() > 0 ? " yes " : " no ") + "): test properties from resource 'test-user.conf'");
String file = System.getProperty("ethereumj.conf.file");
Config cmdLineConfigFile = file != null ? ConfigFactory.parseFile(new File(file)) : ConfigFactory.empty();
logger.info("Config (" + (cmdLineConfigFile.entrySet().size() > 0 ? " yes " : " no ") + "): user properties from -Dethereumj.conf.file file '" + file + "'");
logger.info("Config (" + (apiConfig.entrySet().size() > 0 ? " yes " : " no ") + "): config passed via constructor");
config = apiConfig
.withFallback(cmdLineConfigFile)
.withFallback(testUserConfig)
.withFallback(testConfig)
.withFallback(userDirConfig)
.withFallback(userConfig)
.withFallback(cmdLineConfigRes)
.withFallback(referenceConfig);
logger.debug("Config trace: " + config.root().render(ConfigRenderOptions.defaults().
setComments(false).setJson(false)));
config = javaSystemProperties.withFallback(config)
.resolve(); // substitute variables in config if any
validateConfig();
// There could be several files with the same name from other packages,
// "version.properties" is a very common name
List<InputStream> iStreams = loadResources("version.properties", this.getClass().getClassLoader());
for (InputStream is : iStreams) {
Properties props = new Properties();
props.load(is);
if (props.getProperty("versionNumber") == null || props.getProperty("databaseVersion") == null) {
continue;
}
this.projectVersion = props.getProperty("versionNumber");
this.projectVersion = this.projectVersion.replaceAll("'", "");
if (this.projectVersion == null) this.projectVersion = "-.-.-";
this.projectVersionModifier = "master".equals(BuildInfo.buildBranch) ? "RELEASE" : "SNAPSHOT";
this.databaseVersion = Integer.valueOf(props.getProperty("databaseVersion"));
this.generateNodeIdStrategy = new GetNodeIdFromPropsFile(databaseDir())
.withFallback(new GenerateNodeIdRandomly(databaseDir()));
break;
}
} catch (Exception e) {
logger.error("Can't read config.", e);
throw new RuntimeException(e);
}
}
/**
* Loads resources using given ClassLoader assuming, there could be several resources
* with the same name
*/
public static List<InputStream> loadResources(
final String name, final ClassLoader classLoader) throws IOException {
final List<InputStream> list = new ArrayList<InputStream>();
final Enumeration<URL> systemResources =
(classLoader == null ? ClassLoader.getSystemClassLoader() : classLoader)
.getResources(name);
while (systemResources.hasMoreElements()) {
list.add(systemResources.nextElement().openStream());
}
return list;
}
public Config getConfig() {
return config;
}
/**
* Puts a new config atop of existing stack making the options
* in the supplied config overriding existing options
* Once put this config can't be removed
*
* @param overrideOptions - atop config
*/
public void overrideParams(Config overrideOptions) {
config = overrideOptions.withFallback(config);
validateConfig();
}
/**
* Puts a new config atop of existing stack making the options
* in the supplied config overriding existing options
* Once put this config can't be removed
*
* @param keyValuePairs [name] [value] [name] [value] ...
*/
public void overrideParams(String ... keyValuePairs) {
if (keyValuePairs.length % 2 != 0) throw new RuntimeException("Odd argument number");
Map<String, String> map = new HashMap<>();
for (int i = 0; i < keyValuePairs.length; i += 2) {
map.put(keyValuePairs[i], keyValuePairs[i + 1]);
}
overrideParams(map);
}
/**
* Puts a new config atop of existing stack making the options
* in the supplied config overriding existing options
* Once put this config can't be removed
*
* @param cliOptions - command line options to take presidency
*/
public void overrideParams(Map<String, ?> cliOptions) {
Config cliConf = ConfigFactory.parseMap(cliOptions);
overrideParams(cliConf);
}
private void validateConfig() {
for (Method method : getClass().getMethods()) {
try {
if (method.isAnnotationPresent(ValidateMe.class)) {
method.invoke(this);
}
} catch (Exception e) {
throw new RuntimeException("Error validating config method: " + method, e);
}
}
}
public <T> T getProperty(String propName, T defaultValue) {
if (!config.hasPath(propName)) return defaultValue;
String string = config.getString(propName);
if (string.trim().isEmpty()) return defaultValue;
return (T) config.getAnyRef(propName);
}
public BlockchainNetConfig getBlockchainConfig() {
if (blockchainConfig == null) {
GenesisJson genesisJson = getGenesisJson();
if (genesisJson.getConfig() != null && genesisJson.getConfig().isCustomConfig()) {
blockchainConfig = new JsonNetConfig(genesisJson.getConfig());
} else {
if (config.hasPath("blockchain.config.name") && config.hasPath("blockchain.config.class")) {
throw new RuntimeException("Only one of two options should be defined: 'blockchain.config.name' and 'blockchain.config.class'");
}
if (config.hasPath("blockchain.config.name")) {
switch (config.getString("blockchain.config.name")) {
case "main":
blockchainConfig = new MainNetConfig();
break;
case "olympic":
blockchainConfig = new OlympicConfig();
break;
case "morden":
blockchainConfig = new MordenNetConfig();
break;
case "ropsten":
blockchainConfig = new RopstenNetConfig();
break;
case "testnet":
blockchainConfig = new TestNetConfig();
break;
default:
throw new RuntimeException("Unknown value for 'blockchain.config.name': '" + config.getString("blockchain.config.name") + "'");
}
} else {
String className = config.getString("blockchain.config.class");
try {
Class<? extends BlockchainNetConfig> aClass = (Class<? extends BlockchainNetConfig>) classLoader.loadClass(className);
blockchainConfig = aClass.newInstance();
} catch (ClassNotFoundException e) {
throw new RuntimeException("The class specified via blockchain.config.class '" + className + "' not found", e);
} catch (ClassCastException e) {
throw new RuntimeException("The class specified via blockchain.config.class '" + className + "' is not instance of org.ethereum.config.BlockchainForkConfig", e);
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException("The class specified via blockchain.config.class '" + className + "' couldn't be instantiated (check for default constructor and its accessibility)", e);
}
}
}
if (genesisJson.getConfig() != null && genesisJson.getConfig().headerValidators != null) {
for (GenesisConfig.HashValidator validator : genesisJson.getConfig().headerValidators) {
BlockHeaderValidator headerValidator = new BlockHeaderValidator(new BlockCustomHashRule(ByteUtil.hexStringToBytes(validator.hash)));
blockchainConfig.getConfigForBlock(validator.number).headerValidators().add(
Pair.of(validator.number, headerValidator));
}
}
}
return blockchainConfig;
}
public void setBlockchainConfig(BlockchainNetConfig config) {
blockchainConfig = config;
}
@ValidateMe
public boolean peerDiscovery() {
return discoveryEnabled == null ? config.getBoolean("peer.discovery.enabled") : discoveryEnabled;
}
public void setDiscoveryEnabled(Boolean discoveryEnabled) {
this.discoveryEnabled = discoveryEnabled;
}
@ValidateMe
public boolean peerDiscoveryPersist() {
return config.getBoolean("peer.discovery.persist");
}
@ValidateMe
public int peerDiscoveryWorkers() {
return config.getInt("peer.discovery.workers");
}
@ValidateMe
public int peerDiscoveryTouchPeriod() {
return config.getInt("peer.discovery.touchPeriod");
}
@ValidateMe
public int peerDiscoveryTouchMaxNodes() {
return config.getInt("peer.discovery.touchMaxNodes");
}
@ValidateMe
public int peerConnectionTimeout() {
return config.getInt("peer.connection.timeout") * 1000;
}
@ValidateMe
public int defaultP2PVersion() {
return config.hasPath("peer.p2p.version") ? config.getInt("peer.p2p.version") : P2pHandler.VERSION;
}
@ValidateMe
public int rlpxMaxFrameSize() {
return config.hasPath("peer.p2p.framing.maxSize") ? config.getInt("peer.p2p.framing.maxSize") : MessageCodec.NO_FRAMING;
}
@ValidateMe
public int transactionApproveTimeout() {
return config.getInt("transaction.approve.timeout") * 1000;
}
@ValidateMe
public List<String> peerDiscoveryIPList() {
return config.getStringList("peer.discovery.ip.list");
}
@ValidateMe
public boolean databaseReset() {
return databaseReset == null ? config.getBoolean("database.reset") : databaseReset;
}
public void setDatabaseReset(Boolean reset) {
databaseReset = reset;
}
@ValidateMe
public long databaseResetBlock() {
return config.getLong("database.resetBlock");
}
@ValidateMe
public boolean databaseFromBackup() {
return config.getBoolean("database.fromBackup");
}
@ValidateMe
public int databasePruneDepth() {
return config.getBoolean("database.prune.enabled") ? config.getInt("database.prune.maxDepth") : -1;
}
@ValidateMe
public List<Node> peerActive() {
if (!config.hasPath("peer.active")) {
return Collections.EMPTY_LIST;
}
List<Node> ret = new ArrayList<>();
List<? extends ConfigObject> list = config.getObjectList("peer.active");
for (ConfigObject configObject : list) {
Node n;
if (configObject.get("url") != null) {
String url = configObject.toConfig().getString("url");
n = new Node(url.startsWith("enode://") ? url : "enode://" + url);
} else if (configObject.get("ip") != null) {
String ip = configObject.toConfig().getString("ip");
int port = configObject.toConfig().getInt("port");
byte[] nodeId;
if (configObject.toConfig().hasPath("nodeId")) {
nodeId = Hex.decode(configObject.toConfig().getString("nodeId").trim());
if (nodeId.length != 64) {
throw new RuntimeException("Invalid config nodeId '" + nodeId + "' at " + configObject);
}
} else {
if (configObject.toConfig().hasPath("nodeName")) {
String nodeName = configObject.toConfig().getString("nodeName").trim();
// FIXME should be keccak-512 here ?
nodeId = ECKey.fromPrivate(sha3(nodeName.getBytes())).getNodeId();
} else {
throw new RuntimeException("Either nodeId or nodeName should be specified: " + configObject);
}
}
n = new Node(nodeId, ip, port);
} else {
throw new RuntimeException("Unexpected element within 'peer.active' config list: " + configObject);
}
ret.add(n);
}
return ret;
}
@ValidateMe
public NodeFilter peerTrusted() {
List<? extends ConfigObject> list = config.getObjectList("peer.trusted");
NodeFilter ret = new NodeFilter();
for (ConfigObject configObject : list) {
byte[] nodeId = null;
String ipMask = null;
if (configObject.get("nodeId") != null) {
nodeId = Hex.decode(configObject.toConfig().getString("nodeId").trim());
}
if (configObject.get("ip") != null) {
ipMask = configObject.toConfig().getString("ip").trim();
}
ret.add(nodeId, ipMask);
}
return ret;
}
@ValidateMe
public Integer blockQueueSize() {
return config.getInt("cache.blockQueueSize") * 1024 * 1024;
}
@ValidateMe
public Integer headerQueueSize() {
return config.getInt("cache.headerQueueSize") * 1024 * 1024;
}
@ValidateMe
public Integer peerChannelReadTimeout() {
return config.getInt("peer.channel.read.timeout");
}
@ValidateMe
public Integer traceStartBlock() {
return config.getInt("trace.startblock");
}
@ValidateMe
public boolean recordBlocks() {
return config.getBoolean("record.blocks");
}
@ValidateMe
public boolean dumpFull() {
return config.getBoolean("dump.full");
}
@ValidateMe
public String dumpDir() {
return config.getString("dump.dir");
}
@ValidateMe
public String dumpStyle() {
return config.getString("dump.style");
}
@ValidateMe
public int dumpBlock() {
return config.getInt("dump.block");
}
@ValidateMe
public String databaseDir() {
return databaseDir == null ? config.getString("database.dir") : databaseDir;
}
public String ethashDir() {
return config.hasPath("ethash.dir") ? config.getString("ethash.dir") : databaseDir();
}
public void setDataBaseDir(String dataBaseDir) {
this.databaseDir = dataBaseDir;
}
@ValidateMe
public boolean dumpCleanOnRestart() {
return config.getBoolean("dump.clean.on.restart");
}
@ValidateMe
public boolean playVM() {
return config.getBoolean("play.vm");
}
@ValidateMe
public boolean blockChainOnly() {
return config.getBoolean("blockchain.only");
}
@ValidateMe
public int syncPeerCount() {
return config.getInt("sync.peer.count");
}
public Integer syncVersion() {
if (!config.hasPath("sync.version")) {
return null;
}
return config.getInt("sync.version");
}
@ValidateMe
public boolean exitOnBlockConflict() {
return config.getBoolean("sync.exitOnBlockConflict");
}
@ValidateMe
public String projectVersion() {
return projectVersion;
}
@ValidateMe
public Integer databaseVersion() {
return databaseVersion;
}
@ValidateMe
public String projectVersionModifier() {
return projectVersionModifier;
}
@ValidateMe
public String helloPhrase() {
return config.getString("hello.phrase");
}
@ValidateMe
public String rootHashStart() {
return config.hasPath("root.hash.start") ? config.getString("root.hash.start") : null;
}
@ValidateMe
public List<String> peerCapabilities() {
return config.getStringList("peer.capabilities");
}
@ValidateMe
public boolean vmTrace() {
return vmTrace == null ? (vmTrace = config.getBoolean("vm.structured.trace")) : vmTrace;
}
@ValidateMe
public boolean vmTraceCompressed() {
return config.getBoolean("vm.structured.compressed");
}
@ValidateMe
public int vmTraceInitStorageLimit() {
return config.getInt("vm.structured.initStorageLimit");
}
@ValidateMe
public int cacheFlushBlocks() {
return config.getInt("cache.flush.blocks");
}
@ValidateMe
public String vmTraceDir() {
return config.getString("vm.structured.dir");
}
public String customSolcPath() {
return config.hasPath("solc.path") ? config.getString("solc.path"): null;
}
public String privateKey() {
if (config.hasPath("peer.privateKey")) {
String key = config.getString("peer.privateKey");
if (key.length() != 64 || !Utils.isHexEncoded(key)) {
throw new RuntimeException("The peer.privateKey needs to be Hex encoded and 32 byte length");
}
return key;
} else {
return getGeneratedNodePrivateKey();
}
}
private String getGeneratedNodePrivateKey() {
if (generatedNodePrivateKey == null) {
generatedNodePrivateKey = generateNodeIdStrategy.getNodePrivateKey();
}
return generatedNodePrivateKey;
}
public ECKey getMyKey() {
return ECKey.fromPrivate(Hex.decode(privateKey()));
}
/**
* Home NodeID calculated from 'peer.privateKey' property
*/
public byte[] nodeId() {
return getMyKey().getNodeId();
}
@ValidateMe
public int networkId() {
return config.getInt("peer.networkId");
}
@ValidateMe
public int maxActivePeers() {
return config.getInt("peer.maxActivePeers");
}
@ValidateMe
public boolean eip8() {
return config.getBoolean("peer.p2p.eip8");
}
@ValidateMe
public int listenPort() {
return config.getInt("peer.listen.port");
}
/**
* This can be a blocking call with long timeout (thus no ValidateMe)
*/
public String bindIp() {
if (!config.hasPath("peer.discovery.bind.ip") || config.getString("peer.discovery.bind.ip").trim().isEmpty()) {
if (bindIp == null) {
logger.info("Bind address wasn't set, Punching to identify it...");
try (Socket s = new Socket("www.google.com", 80)) {
bindIp = s.getLocalAddress().getHostAddress();
logger.info("UDP local bound to: {}", bindIp);
} catch (IOException e) {
logger.warn("Can't get bind IP. Fall back to 0.0.0.0: " + e);
bindIp = "0.0.0.0";
}
}
return bindIp;
} else {
return config.getString("peer.discovery.bind.ip").trim();
}
}
/**
* This can be a blocking call with long timeout (thus no ValidateMe)
*/
public String externalIp() {
if (!config.hasPath("peer.discovery.external.ip") || config.getString("peer.discovery.external.ip").trim().isEmpty()) {
if (externalIp == null) {
logger.info("External IP wasn't set, using checkip.amazonaws.com to identify it...");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(
new URL("http://checkip.amazonaws.com").openStream()));
externalIp = in.readLine();
if (externalIp == null || externalIp.trim().isEmpty()) {
throw new IOException("Invalid address: '" + externalIp + "'");
}
try {
InetAddress.getByName(externalIp);
} catch (Exception e) {
throw new IOException("Invalid address: '" + externalIp + "'");
}
logger.info("External address identified: {}", externalIp);
} catch (IOException e) {
externalIp = bindIp();
logger.warn("Can't get external IP. Fall back to peer.bind.ip: " + externalIp + " :" + e);
}
}
return externalIp;
} else {
return config.getString("peer.discovery.external.ip").trim();
}
}
@ValidateMe
public String getKeyValueDataSource() {
return config.getString("keyvalue.datasource");
}
@ValidateMe
public boolean isSyncEnabled() {
return this.syncEnabled == null ? config.getBoolean("sync.enabled") : syncEnabled;
}
public void setSyncEnabled(Boolean syncEnabled) {
this.syncEnabled = syncEnabled;
}
@ValidateMe
public boolean isFastSyncEnabled() {
return isSyncEnabled() && config.getBoolean("sync.fast.enabled");
}
@ValidateMe
public byte[] getFastSyncPivotBlockHash() {
if (!config.hasPath("sync.fast.pivotBlockHash")) return null;
byte[] ret = Hex.decode(config.getString("sync.fast.pivotBlockHash"));
if (ret.length != 32) throw new RuntimeException("Invalid block hash length: " + toHexString(ret));
return ret;
}
@ValidateMe
public boolean fastSyncBackupState() {
return config.getBoolean("sync.fast.backupState");
}
@ValidateMe
public boolean fastSyncSkipHistory() {
return config.getBoolean("sync.fast.skipHistory");
}
@ValidateMe
public int makeDoneByTimeout() {
return config.getInt("sync.makeDoneByTimeout");
}
@ValidateMe
public boolean isPublicHomeNode() { return config.getBoolean("peer.discovery.public.home.node");}
@ValidateMe
public String genesisInfo() {
return genesisInfo == null ? config.getString("genesis") : genesisInfo;
}
@ValidateMe
public int txOutdatedThreshold() {
return config.getInt("transaction.outdated.threshold");
}
public void setGenesisInfo(String genesisInfo){
this.genesisInfo = genesisInfo;
}
@ValidateMe
public boolean minerStart() {
return config.getBoolean("mine.start");
}
@ValidateMe
public byte[] getMinerCoinbase() {
String sc = config.getString("mine.coinbase");
byte[] c = ByteUtil.hexStringToBytes(sc);
if (c.length != 20) throw new RuntimeException("mine.coinbase has invalid value: '" + sc + "'");
return c;
}
@ValidateMe
public byte[] getMineExtraData() {
byte[] bytes;
if (config.hasPath("mine.extraDataHex")) {
bytes = Hex.decode(config.getString("mine.extraDataHex"));
} else {
bytes = config.getString("mine.extraData").getBytes();
}
if (bytes.length > 32) throw new RuntimeException("mine.extraData exceed 32 bytes length: " + bytes.length);
return bytes;
}
@ValidateMe
public BigInteger getMineMinGasPrice() {
return new BigInteger(config.getString("mine.minGasPrice"));
}
@ValidateMe
public long getMineMinBlockTimeoutMsec() {
return config.getLong("mine.minBlockTimeoutMsec");
}
@ValidateMe
public int getMineCpuThreads() {
return config.getInt("mine.cpuMineThreads");
}
@ValidateMe
public boolean isMineFullDataset() {
return config.getBoolean("mine.fullDataSet");
}
@ValidateMe
public String getCryptoProviderName() {
return config.getString("crypto.providerName");
}
@ValidateMe
public boolean recordInternalTransactionsData() {
if (recordInternalTransactionsData == null) {
recordInternalTransactionsData = config.getBoolean("record.internal.transactions.data");
}
return recordInternalTransactionsData;
}
public void setRecordInternalTransactionsData(Boolean recordInternalTransactionsData) {
this.recordInternalTransactionsData = recordInternalTransactionsData;
}
@ValidateMe
public String getHash256AlgName() {
return config.getString("crypto.hash.alg256");
}
@ValidateMe
public String getHash512AlgName() {
return config.getString("crypto.hash.alg512");
}
@ValidateMe
public String getEthashMode() {
return config.getString("sync.ethash");
}
private GenesisJson getGenesisJson() {
if (genesisJson == null) {
genesisJson = GenesisLoader.loadGenesisJson(this, classLoader);
}
return genesisJson;
}
public Genesis getGenesis() {
if (genesis == null) {
genesis = GenesisLoader.parseGenesis(getBlockchainConfig(), getGenesisJson());
}
return genesis;
}
/**
* Method used in StandaloneBlockchain.
*/
public Genesis useGenesis(InputStream inputStream) {
genesisJson = GenesisLoader.loadGenesisJson(inputStream);
genesis = GenesisLoader.parseGenesis(getBlockchainConfig(), getGenesisJson());
return genesis;
}
public String dump() {
return config.root().render(ConfigRenderOptions.defaults().setComments(false));
}
/*
*
* Testing
*
*/
public boolean vmTestLoadLocal() {
return config.hasPath("GitHubTests.VMTest.loadLocal") ?
config.getBoolean("GitHubTests.VMTest.loadLocal") : DEFAULT_VMTEST_LOAD_LOCAL;
}
public String blocksLoader() {
return config.hasPath("blocks.loader") ?
config.getString("blocks.loader") : DEFAULT_BLOCKS_LOADER;
}
public String githubTestsPath() {
return config.hasPath("GitHubTests.testPath") ?
config.getString("GitHubTests.testPath") : "";
}
public boolean githubTestsLoadLocal() {
return config.hasPath("GitHubTests.testPath") &&
!config.getString("GitHubTests.testPath").isEmpty();
}
void setGenerateNodeIdStrategy(GenerateNodeIdStrategy generateNodeIdStrategy) {
this.generateNodeIdStrategy = generateNodeIdStrategy;
}
}