| @@ -28,7 +28,7 @@ import com.jd.blockchain.transaction.LedgerInitSettingData; | |||
| import com.jd.blockchain.utils.Bytes; | |||
| import com.jd.blockchain.utils.net.NetworkAddress; | |||
| public class LedgerInitSettingTest { | |||
| public class LedgerInitSettingSerializeTest { | |||
| byte[] seed = null; | |||
| byte[] csSysSettingBytes = null; | |||
| LedgerInitSettingData ledgerInitSettingData = new LedgerInitSettingData(); | |||
| @@ -84,7 +84,7 @@ public class LedgerInitSettingTest { | |||
| ConsensusParticipantData[] parties1 = Arrays.copyOf(parties, 4); | |||
| ledgerInitSettingData.setConsensusParticipants(parties1); | |||
| byte[] encode = BinaryProtocol.encode(ledgerInitSettingData, LedgerInitSetting.class); | |||
| LedgerInitSetting decode = BinaryProtocol.decode(encode); | |||
| @@ -7,13 +7,13 @@ import com.jd.blockchain.tools.initializer.ConsolePrompter; | |||
| public class PresetAnswerPrompter extends ConsolePrompter { | |||
| private Properties answers = new Properties(); | |||
| private String defaultAnswer; | |||
| public PresetAnswerPrompter(String defaultAnswer) { | |||
| this.defaultAnswer = defaultAnswer; | |||
| } | |||
| public void setAnswer(String tag, String answer) { | |||
| answers.setProperty(tag, answer); | |||
| } | |||
| @@ -21,7 +21,7 @@ public class PresetAnswerPrompter extends ConsolePrompter { | |||
| public void setDefaultAnswer(String defaultAnswer) { | |||
| this.defaultAnswer = defaultAnswer; | |||
| } | |||
| @Override | |||
| public String confirm(String tag, String format, Object... args) { | |||
| System.out.print(String.format(format, args)); | |||
| @@ -29,7 +29,5 @@ public class PresetAnswerPrompter extends ConsolePrompter { | |||
| System.out.println(String.format("\r\n [Mocked answer:%s]", answer)); | |||
| return answer; | |||
| } | |||
| } | |||
| @@ -215,26 +215,26 @@ public class ConsensusTest { | |||
| DBConnectionConfig testDb0 = new DBConnectionConfig(); | |||
| testDb0.setConnectionUri("memory://local/0"); | |||
| LedgerBindingConfig bindingConfig0 = new LedgerBindingConfig(); | |||
| AsyncCallback<HashDigest> callback0 = nodeCtx0.startInitCommand(privkey0, encodedPassword, initSetting, csProps,csProvider, | |||
| testDb0, consolePrompter, bindingConfig0, quitLatch); | |||
| AsyncCallback<HashDigest> callback0 = nodeCtx0.startInitCommand(privkey0, encodedPassword, initSetting, csProps, | |||
| csProvider, testDb0, consolePrompter, bindingConfig0, quitLatch); | |||
| DBConnectionConfig testDb1 = new DBConnectionConfig(); | |||
| testDb1.setConnectionUri("memory://local/1"); | |||
| LedgerBindingConfig bindingConfig1 = new LedgerBindingConfig(); | |||
| AsyncCallback<HashDigest> callback1 = nodeCtx1.startInitCommand(privkey1, encodedPassword, initSetting, csProps,csProvider, | |||
| testDb1, consolePrompter, bindingConfig1, quitLatch); | |||
| AsyncCallback<HashDigest> callback1 = nodeCtx1.startInitCommand(privkey1, encodedPassword, initSetting, csProps, | |||
| csProvider, testDb1, consolePrompter, bindingConfig1, quitLatch); | |||
| DBConnectionConfig testDb2 = new DBConnectionConfig(); | |||
| testDb2.setConnectionUri("memory://local/2"); | |||
| LedgerBindingConfig bindingConfig2 = new LedgerBindingConfig(); | |||
| AsyncCallback<HashDigest> callback2 = nodeCtx2.startInitCommand(privkey2, encodedPassword, initSetting, csProps,csProvider, | |||
| testDb2, consolePrompter, bindingConfig2, quitLatch); | |||
| AsyncCallback<HashDigest> callback2 = nodeCtx2.startInitCommand(privkey2, encodedPassword, initSetting, csProps, | |||
| csProvider, testDb2, consolePrompter, bindingConfig2, quitLatch); | |||
| DBConnectionConfig testDb3 = new DBConnectionConfig(); | |||
| testDb3.setConnectionUri("memory://local/3"); | |||
| LedgerBindingConfig bindingConfig3 = new LedgerBindingConfig(); | |||
| AsyncCallback<HashDigest> callback3 = nodeCtx3.startInitCommand(privkey3, encodedPassword, initSetting, csProps,csProvider, | |||
| testDb3, consolePrompter, bindingConfig3, quitLatch); | |||
| AsyncCallback<HashDigest> callback3 = nodeCtx3.startInitCommand(privkey3, encodedPassword, initSetting, csProps, | |||
| csProvider, testDb3, consolePrompter, bindingConfig3, quitLatch); | |||
| HashDigest ledgerHash0 = callback0.waitReturn(); | |||
| HashDigest ledgerHash1 = callback1.waitReturn(); | |||
| @@ -354,8 +354,8 @@ public class ConsensusTest { | |||
| } | |||
| public AsyncCallback<HashDigest> startInit(PrivKey privKey, LedgerInitProperties setting, | |||
| ConsensusSettings csProps, ConsensusProvider csProvider, DBConnectionConfig dbConnConfig, Prompter prompter, | |||
| CountDownLatch quitLatch) { | |||
| ConsensusSettings csProps, ConsensusProvider csProvider, DBConnectionConfig dbConnConfig, | |||
| Prompter prompter, CountDownLatch quitLatch) { | |||
| ThreadInvoker<HashDigest> invoker = new ThreadInvoker<HashDigest>() { | |||
| @Override | |||
| @@ -364,7 +364,7 @@ public class ConsensusTest { | |||
| // NodeWebContext.this.initProcess = ctx.getBean(LedgerInitProcess.class); | |||
| NodeInitContext.this.dbConnConfig = dbConnConfig; | |||
| HashDigest ledgerHash = NodeInitContext.this.initProcess.initialize(id, privKey, setting, csProps, csProvider, | |||
| HashDigest ledgerHash = NodeInitContext.this.initProcess.initialize(id, privKey, setting, | |||
| dbConnConfig, prompter); | |||
| quitLatch.countDown(); | |||
| @@ -386,8 +386,7 @@ public class ConsensusTest { | |||
| @Override | |||
| protected HashDigest invoke() throws Exception { | |||
| LedgerInitCommand initCmd = new LedgerInitCommand(); | |||
| HashDigest ledgerHash = initCmd.startInit(id, privKey, base58Pwd, ledgerSetting, csProps, | |||
| csProvider, dbConnConfig, prompter, conf, db); | |||
| HashDigest ledgerHash = initCmd.startInit(id, privKey, base58Pwd, ledgerSetting, dbConnConfig, prompter, conf, db); | |||
| NodeInitContext.this.ledgerManager = initCmd.getLedgerManager(); | |||
| quitLatch.countDown(); | |||
| return ledgerHash; | |||
| @@ -187,7 +187,7 @@ public class GlobalPerformanceTest { | |||
| Properties props = Utils.loadConsensusSetting(); | |||
| ConsensusProvider csProvider = getConsensusProvider(); | |||
| ConsensusSettings csProps = csProvider.getSettingsFactory().getConsensusSettingsBuilder().createSettings(props); | |||
| // 启动服务器; | |||
| NetworkAddress initAddr0 = initSetting.getConsensusParticipant(0).getInitializerAddress(); | |||
| NodeInitContext nodeCtx0 = new NodeInitContext(0, initAddr0); | |||
| @@ -365,8 +365,8 @@ public class GlobalPerformanceTest { | |||
| // NodeWebContext.this.initProcess = ctx.getBean(LedgerInitProcess.class); | |||
| NodeInitContext.this.dbConnConfig = dbConnConfig; | |||
| HashDigest ledgerHash = NodeInitContext.this.initProcess.initialize(id, privKey, setting, csProps, | |||
| csProvider, dbConnConfig, prompter); | |||
| HashDigest ledgerHash = NodeInitContext.this.initProcess.initialize(id, privKey, setting, | |||
| dbConnConfig, prompter); | |||
| quitLatch.countDown(); | |||
| return ledgerHash; | |||
| @@ -387,8 +387,8 @@ public class GlobalPerformanceTest { | |||
| @Override | |||
| protected HashDigest invoke() throws Exception { | |||
| LedgerInitCommand initCmd = new LedgerInitCommand(); | |||
| HashDigest ledgerHash = initCmd.startInit(id, privKey, base58Pwd, ledgerSetting, csProps, | |||
| csProvider, dbConnConfig, prompter, conf, db); | |||
| HashDigest ledgerHash = initCmd.startInit(id, privKey, base58Pwd, ledgerSetting, dbConnConfig, | |||
| prompter, conf, db); | |||
| NodeInitContext.this.ledgerManager = initCmd.getLedgerManager(); | |||
| quitLatch.countDown(); | |||
| return ledgerHash; | |||
| @@ -68,9 +68,6 @@ public class LedgerInitializeTest { | |||
| public void testInitWith4Nodes() { | |||
| Prompter consolePrompter = new PresetAnswerPrompter("N"); // new ConsolePrompter(); | |||
| LedgerInitProperties initSetting = loadInitSetting(); | |||
| Properties props = loadConsensusSetting(); | |||
| ConsensusProvider csProvider = getConsensusProvider(); | |||
| ConsensusSettings csProps = csProvider.getSettingsFactory().getConsensusSettingsBuilder().createSettings(props); | |||
| NodeContext node0 = new NodeContext(initSetting.getConsensusParticipant(0).getInitializerAddress(), | |||
| serviceRegisterMap); | |||
| @@ -81,31 +78,28 @@ public class LedgerInitializeTest { | |||
| NodeContext node3 = new NodeContext(initSetting.getConsensusParticipant(3).getInitializerAddress(), | |||
| serviceRegisterMap); | |||
| String[] memoryConnString = new String[]{"memory://local/0", "memory://local/1", "memory://local/2", "memory://local/3"}; | |||
| String[] memoryConnString = new String[] { "memory://local/0", "memory://local/1", "memory://local/2", | |||
| "memory://local/3" }; | |||
| PrivKey privkey0 = KeyGenCommand.decodePrivKeyWithRawPassword(PRIV_KEYS[0], PASSWORD); | |||
| DBConnectionConfig testDb0 = new DBConnectionConfig(); | |||
| testDb0.setConnectionUri(memoryConnString[0]); | |||
| AsyncCallback<HashDigest> callback0 = node0.startInit(0, privkey0, initSetting, csProps, csProvider, testDb0, | |||
| consolePrompter); | |||
| AsyncCallback<HashDigest> callback0 = node0.startInit(0, privkey0, initSetting, testDb0, consolePrompter); | |||
| PrivKey privkey1 = KeyGenCommand.decodePrivKeyWithRawPassword(PRIV_KEYS[1], PASSWORD); | |||
| DBConnectionConfig testDb1 = new DBConnectionConfig(); | |||
| testDb1.setConnectionUri(memoryConnString[1]); | |||
| AsyncCallback<HashDigest> callback1 = node1.startInit(1, privkey1, initSetting, csProps, csProvider, testDb1, | |||
| consolePrompter); | |||
| AsyncCallback<HashDigest> callback1 = node1.startInit(1, privkey1, initSetting, testDb1, consolePrompter); | |||
| PrivKey privkey2 = KeyGenCommand.decodePrivKeyWithRawPassword(PRIV_KEYS[2], PASSWORD); | |||
| DBConnectionConfig testDb2 = new DBConnectionConfig(); | |||
| testDb2.setConnectionUri(memoryConnString[2]); | |||
| AsyncCallback<HashDigest> callback2 = node2.startInit(2, privkey2, initSetting, csProps, csProvider, testDb2, | |||
| consolePrompter); | |||
| AsyncCallback<HashDigest> callback2 = node2.startInit(2, privkey2, initSetting, testDb2, consolePrompter); | |||
| PrivKey privkey3 = KeyGenCommand.decodePrivKeyWithRawPassword(PRIV_KEYS[3], PASSWORD); | |||
| DBConnectionConfig testDb03 = new DBConnectionConfig(); | |||
| testDb03.setConnectionUri(memoryConnString[3]); | |||
| AsyncCallback<HashDigest> callback3 = node3.startInit(3, privkey3, initSetting, csProps, csProvider, testDb03, | |||
| consolePrompter); | |||
| AsyncCallback<HashDigest> callback3 = node3.startInit(3, privkey3, initSetting, testDb03, consolePrompter); | |||
| HashDigest ledgerHash0 = callback0.waitReturn(); | |||
| HashDigest ledgerHash1 = callback1.waitReturn(); | |||
| @@ -188,23 +182,21 @@ public class LedgerInitializeTest { | |||
| public NodeContext(NetworkAddress address, Map<NetworkAddress, LedgerInitConsensusService> serviceRegisterMap) { | |||
| this.initCsServiceFactory = new MultiThreadInterInvokerFactory(serviceRegisterMap); | |||
| LedgerInitializeWebController initController = new LedgerInitializeWebController(ledgerManager, memoryDBConnFactory, | |||
| initCsServiceFactory); | |||
| LedgerInitializeWebController initController = new LedgerInitializeWebController(ledgerManager, | |||
| memoryDBConnFactory, initCsServiceFactory); | |||
| serviceRegisterMap.put(address, initController); | |||
| this.initProcess = initController; | |||
| } | |||
| public AsyncCallback<HashDigest> startInit(int currentId, PrivKey privKey, LedgerInitProperties setting, | |||
| ConsensusSettings csProps, ConsensusProvider csProvider, DBConnectionConfig dbConnConfig, | |||
| Prompter prompter) { | |||
| DBConnectionConfig dbConnConfig, Prompter prompter) { | |||
| partiKey = new AsymmetricKeypair(setting.getConsensusParticipant(0).getPubKey(), privKey); | |||
| ThreadInvoker<HashDigest> invoker = new ThreadInvoker<HashDigest>() { | |||
| @Override | |||
| protected HashDigest invoke() throws Exception { | |||
| return initProcess.initialize(currentId, privKey, setting, csProps, csProvider, dbConnConfig, | |||
| prompter); | |||
| return initProcess.initialize(currentId, privKey, setting, dbConnConfig, prompter); | |||
| } | |||
| }; | |||
| @@ -212,8 +204,7 @@ public class LedgerInitializeTest { | |||
| } | |||
| public AsyncCallback<HashDigest> startInit(int currentId, PrivKey privKey, LedgerInitProperties setting, | |||
| ConsensusSettings csProps, ConsensusProvider csProvider, DBConnectionConfig dbConnConfig, | |||
| Prompter prompter, boolean autoVerifyHash) { | |||
| DBConnectionConfig dbConnConfig, Prompter prompter, boolean autoVerifyHash) { | |||
| CryptoConfig cryptoSetting = new CryptoConfig(); | |||
| cryptoSetting.setAutoVerifyHash(autoVerifyHash); | |||
| @@ -224,8 +215,7 @@ public class LedgerInitializeTest { | |||
| ThreadInvoker<HashDigest> invoker = new ThreadInvoker<HashDigest>() { | |||
| @Override | |||
| protected HashDigest invoke() throws Exception { | |||
| return initProcess.initialize(currentId, privKey, setting, csProps, csProvider, dbConnConfig, | |||
| prompter, cryptoSetting); | |||
| return initProcess.initialize(currentId, privKey, setting, dbConnConfig, prompter, cryptoSetting); | |||
| } | |||
| }; | |||
| @@ -264,23 +264,23 @@ public class LedgerInitializeWebTest { | |||
| DBConnectionConfig testDb0 = new DBConnectionConfig(); | |||
| testDb0.setConnectionUri("memory://local/0"); | |||
| AsyncCallback<HashDigest> callback0 = node0.startInit(privkey0, initSetting, csProps, csProvider, testDb0, | |||
| consolePrompter, quitLatch); | |||
| AsyncCallback<HashDigest> callback0 = node0.startInit(privkey0, initSetting, testDb0, consolePrompter, | |||
| quitLatch); | |||
| DBConnectionConfig testDb1 = new DBConnectionConfig(); | |||
| testDb1.setConnectionUri("memory://local/1"); | |||
| AsyncCallback<HashDigest> callback1 = node1.startInit(privkey1, initSetting, csProps, csProvider, testDb1, | |||
| consolePrompter, quitLatch); | |||
| AsyncCallback<HashDigest> callback1 = node1.startInit(privkey1, initSetting, testDb1, consolePrompter, | |||
| quitLatch); | |||
| DBConnectionConfig testDb2 = new DBConnectionConfig(); | |||
| testDb2.setConnectionUri("memory://local/2"); | |||
| AsyncCallback<HashDigest> callback2 = node2.startInit(privkey2, initSetting, csProps, csProvider, testDb2, | |||
| consolePrompter, quitLatch); | |||
| AsyncCallback<HashDigest> callback2 = node2.startInit(privkey2, initSetting, testDb2, consolePrompter, | |||
| quitLatch); | |||
| DBConnectionConfig testDb03 = new DBConnectionConfig(); | |||
| testDb03.setConnectionUri("memory://local/3"); | |||
| AsyncCallback<HashDigest> callback3 = node3.startInit(privkey3, initSetting, csProps, csProvider, testDb03, | |||
| consolePrompter, quitLatch); | |||
| AsyncCallback<HashDigest> callback3 = node3.startInit(privkey3, initSetting, testDb03, consolePrompter, | |||
| quitLatch); | |||
| HashDigest ledgerHash0 = callback0.waitReturn(); | |||
| HashDigest ledgerHash1 = callback1.waitReturn(); | |||
| @@ -402,8 +402,7 @@ public class LedgerInitializeWebTest { | |||
| } | |||
| public AsyncCallback<HashDigest> startInit(PrivKey privKey, LedgerInitProperties setting, | |||
| ConsensusSettings csProps, ConsensusProvider csProvider, DBConnectionConfig dbConnConfig, | |||
| Prompter prompter, CountDownLatch quitLatch) { | |||
| DBConnectionConfig dbConnConfig, Prompter prompter, CountDownLatch quitLatch) { | |||
| ThreadInvoker<HashDigest> invoker = new ThreadInvoker<HashDigest>() { | |||
| @Override | |||
| @@ -412,8 +411,8 @@ public class LedgerInitializeWebTest { | |||
| // NodeWebContext.this.initProcess = ctx.getBean(LedgerInitProcess.class); | |||
| NodeWebContext.this.dbConnConfig = dbConnConfig; | |||
| HashDigest ledgerHash = NodeWebContext.this.initProcess.initialize(id, privKey, setting, csProps, | |||
| csProvider, dbConnConfig, prompter); | |||
| HashDigest ledgerHash = NodeWebContext.this.initProcess.initialize(id, privKey, setting, | |||
| dbConnConfig, prompter); | |||
| quitLatch.countDown(); | |||
| return ledgerHash; | |||
| @@ -443,8 +442,8 @@ public class LedgerInitializeWebTest { | |||
| @Override | |||
| protected HashDigest invoke() throws Exception { | |||
| LedgerInitCommand initCmd = new LedgerInitCommand(); | |||
| HashDigest ledgerHash = initCmd.startInit(id, privKey, base58Pwd, ledgerSetting, csProps, | |||
| csProvider, dbConnConfig, prompter, conf, db); | |||
| HashDigest ledgerHash = initCmd.startInit(id, privKey, base58Pwd, ledgerSetting, dbConnConfig, | |||
| prompter, conf, db); | |||
| NodeWebContext.this.ledgerManager = initCmd.getLedgerManager(); | |||
| quitLatch.countDown(); | |||
| return ledgerHash; | |||
| @@ -106,16 +106,14 @@ public class Utils { | |||
| } | |||
| public AsyncCallback<HashDigest> startInit(int currentId, PrivKey privKey, LedgerInitProperties setting, | |||
| ConsensusSettings csProps, ConsensusProvider consensusProvider, DBConnectionConfig dbConnConfig, | |||
| Prompter prompter) { | |||
| DBConnectionConfig dbConnConfig, Prompter prompter) { | |||
| partiKey = new AsymmetricKeypair(setting.getConsensusParticipant(0).getPubKey(), privKey); | |||
| ThreadInvoker<HashDigest> invoker = new ThreadInvoker<HashDigest>() { | |||
| @Override | |||
| protected HashDigest invoke() throws Exception { | |||
| return initProcess.initialize(currentId, privKey, setting, csProps, consensusProvider, dbConnConfig, | |||
| prompter); | |||
| return initProcess.initialize(currentId, privKey, setting, dbConnConfig, prompter); | |||
| } | |||
| }; | |||
| @@ -150,8 +148,7 @@ public class Utils { | |||
| ThreadInvoker<HashDigest> invoker = new ThreadInvoker<HashDigest>() { | |||
| @Override | |||
| protected HashDigest invoke() throws Exception { | |||
| return initProcess.initialize(currentId, privKey, setting, csProps, consensusProvider, dbConnConfig, | |||
| prompter, cryptoSetting); | |||
| return initProcess.initialize(currentId, privKey, setting, dbConnConfig, prompter, cryptoSetting); | |||
| } | |||
| }; | |||
| @@ -1,7 +1,16 @@ | |||
| package test.com.jd.blockchain.intgr; | |||
| import static org.junit.Assert.assertEquals; | |||
| import static org.junit.Assert.assertNotNull; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import java.util.Properties; | |||
| import java.util.concurrent.CountDownLatch; | |||
| import org.springframework.core.io.ClassPathResource; | |||
| import com.jd.blockchain.consensus.ConsensusProvider; | |||
| import com.jd.blockchain.consensus.ConsensusProviders; | |||
| import com.jd.blockchain.consensus.ConsensusSettings; | |||
| import com.jd.blockchain.crypto.AsymmetricKeypair; | |||
| import com.jd.blockchain.crypto.HashDigest; | |||
| @@ -9,27 +18,21 @@ import com.jd.blockchain.crypto.PrivKey; | |||
| import com.jd.blockchain.gateway.GatewayConfigProperties.KeyPairConfig; | |||
| import com.jd.blockchain.ledger.LedgerBlock; | |||
| import com.jd.blockchain.ledger.core.LedgerRepository; | |||
| import com.jd.blockchain.tools.initializer.*; | |||
| import com.jd.blockchain.tools.initializer.DBConnectionConfig; | |||
| import com.jd.blockchain.tools.initializer.LedgerBindingConfig; | |||
| import com.jd.blockchain.tools.initializer.LedgerInitProperties; | |||
| import com.jd.blockchain.tools.initializer.Prompter; | |||
| import com.jd.blockchain.tools.keygen.KeyGenCommand; | |||
| import com.jd.blockchain.utils.concurrent.ThreadInvoker.AsyncCallback; | |||
| import com.jd.blockchain.utils.net.NetworkAddress; | |||
| import org.springframework.core.io.ClassPathResource; | |||
| import test.com.jd.blockchain.intgr.IntegratedContext.Node; | |||
| import test.com.jd.blockchain.intgr.initializer.LedgerInitializeWeb4SingleStepsTest; | |||
| import test.com.jd.blockchain.intgr.initializer.LedgerInitializeWeb4SingleStepsTest.NodeWebContext; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import java.util.Properties; | |||
| import java.util.concurrent.CountDownLatch; | |||
| import static org.junit.Assert.assertEquals; | |||
| import static org.junit.Assert.assertNotNull; | |||
| public class IntegrationBaseTest { | |||
| LedgerInitConsensusConfig.ConsensusConfig bftsmartConfig = LedgerInitConsensusConfig.bftsmartConfig; | |||
| LedgerInitConsensusConfig.ConsensusConfig bftsmartConfig = LedgerInitConsensusConfig.bftsmartConfig; | |||
| public IntegratedContext context = initLedgers(bftsmartConfig.getConfigPath(), bftsmartConfig.getProvider()); | |||
| public GatewayTestRunner gateway0; | |||
| @@ -150,26 +153,26 @@ public class IntegrationBaseTest { | |||
| DBConnectionConfig testDb0 = new DBConnectionConfig(); | |||
| testDb0.setConnectionUri(LedgerInitConsensusConfig.memConnectionStrings[0]); | |||
| LedgerBindingConfig bindingConfig0 = new LedgerBindingConfig(); | |||
| AsyncCallback<HashDigest> callback0 = nodeCtx0.startInitCommand(privkey0, encodedPassword, initSetting, csProps, | |||
| csProvider, testDb0, consolePrompter, bindingConfig0, quitLatch); | |||
| AsyncCallback<HashDigest> callback0 = nodeCtx0.startInitCommand(privkey0, encodedPassword, initSetting, testDb0, | |||
| consolePrompter, bindingConfig0, quitLatch); | |||
| DBConnectionConfig testDb1 = new DBConnectionConfig(); | |||
| testDb1.setConnectionUri(LedgerInitConsensusConfig.memConnectionStrings[1]); | |||
| LedgerBindingConfig bindingConfig1 = new LedgerBindingConfig(); | |||
| AsyncCallback<HashDigest> callback1 = nodeCtx1.startInitCommand(privkey1, encodedPassword, initSetting, csProps, | |||
| csProvider, testDb1, consolePrompter, bindingConfig1, quitLatch); | |||
| AsyncCallback<HashDigest> callback1 = nodeCtx1.startInitCommand(privkey1, encodedPassword, initSetting, testDb1, | |||
| consolePrompter, bindingConfig1, quitLatch); | |||
| DBConnectionConfig testDb2 = new DBConnectionConfig(); | |||
| testDb2.setConnectionUri(LedgerInitConsensusConfig.memConnectionStrings[2]); | |||
| LedgerBindingConfig bindingConfig2 = new LedgerBindingConfig(); | |||
| AsyncCallback<HashDigest> callback2 = nodeCtx2.startInitCommand(privkey2, encodedPassword, initSetting, csProps, | |||
| csProvider, testDb2, consolePrompter, bindingConfig2, quitLatch); | |||
| AsyncCallback<HashDigest> callback2 = nodeCtx2.startInitCommand(privkey2, encodedPassword, initSetting, testDb2, | |||
| consolePrompter, bindingConfig2, quitLatch); | |||
| DBConnectionConfig testDb3 = new DBConnectionConfig(); | |||
| testDb3.setConnectionUri(LedgerInitConsensusConfig.memConnectionStrings[3]); | |||
| LedgerBindingConfig bindingConfig3 = new LedgerBindingConfig(); | |||
| AsyncCallback<HashDigest> callback3 = nodeCtx3.startInitCommand(privkey3, encodedPassword, initSetting, csProps, | |||
| csProvider, testDb3, consolePrompter, bindingConfig3, quitLatch); | |||
| AsyncCallback<HashDigest> callback3 = nodeCtx3.startInitCommand(privkey3, encodedPassword, initSetting, testDb3, | |||
| consolePrompter, bindingConfig3, quitLatch); | |||
| HashDigest ledgerHash0 = callback0.waitReturn(); | |||
| HashDigest ledgerHash1 = callback1.waitReturn(); | |||
| @@ -57,7 +57,7 @@ public class IntegrationTest2 { | |||
| public void test() { | |||
| // init ledgers of all nodes ; | |||
| IntegratedContext context = initLedgers(LedgerInitConsensusConfig.mqConfig, | |||
| LedgerInitConsensusConfig.memConnectionStrings); | |||
| LedgerInitConsensusConfig.memConnectionStrings); | |||
| Node node0 = context.getNode(0); | |||
| Node node1 = context.getNode(1); | |||
| Node node2 = context.getNode(2); | |||
| @@ -70,10 +70,10 @@ public class IntegrationTest2 { | |||
| PeerTestRunner peer1 = new PeerTestRunner(peerSrvAddr1, node1.getBindingConfig(), node1.getStorageDB()); | |||
| NetworkAddress peerSrvAddr2 = new NetworkAddress("127.0.0.1", 13220); | |||
| PeerTestRunner peer2 = new PeerTestRunner(peerSrvAddr2,node2.getBindingConfig(), node2.getStorageDB()); | |||
| PeerTestRunner peer2 = new PeerTestRunner(peerSrvAddr2, node2.getBindingConfig(), node2.getStorageDB()); | |||
| NetworkAddress peerSrvAddr3 = new NetworkAddress("127.0.0.1", 13230); | |||
| PeerTestRunner peer3 = new PeerTestRunner(peerSrvAddr3,node3.getBindingConfig(), node3.getStorageDB()); | |||
| PeerTestRunner peer3 = new PeerTestRunner(peerSrvAddr3, node3.getBindingConfig(), node3.getStorageDB()); | |||
| AsyncCallback<Object> peerStarting0 = peer0.start(); | |||
| AsyncCallback<Object> peerStarting1 = peer1.start(); | |||
| @@ -192,26 +192,26 @@ public class IntegrationTest2 { | |||
| DBConnectionConfig testDb0 = new DBConnectionConfig(); | |||
| testDb0.setConnectionUri(dbConns[0]); | |||
| LedgerBindingConfig bindingConfig0 = new LedgerBindingConfig(); | |||
| AsyncCallback<HashDigest> callback0 = nodeCtx0.startInitCommand(privkey0, encodedPassword, initSetting, csProps, csProvider, | |||
| testDb0, consolePrompter, bindingConfig0, quitLatch); | |||
| AsyncCallback<HashDigest> callback0 = nodeCtx0.startInitCommand(privkey0, encodedPassword, initSetting, testDb0, | |||
| consolePrompter, bindingConfig0, quitLatch); | |||
| DBConnectionConfig testDb1 = new DBConnectionConfig(); | |||
| testDb1.setConnectionUri(dbConns[1]); | |||
| LedgerBindingConfig bindingConfig1 = new LedgerBindingConfig(); | |||
| AsyncCallback<HashDigest> callback1 = nodeCtx1.startInitCommand(privkey1, encodedPassword, initSetting, csProps,csProvider, | |||
| testDb1, consolePrompter, bindingConfig1, quitLatch); | |||
| AsyncCallback<HashDigest> callback1 = nodeCtx1.startInitCommand(privkey1, encodedPassword, initSetting, testDb1, | |||
| consolePrompter, bindingConfig1, quitLatch); | |||
| DBConnectionConfig testDb2 = new DBConnectionConfig(); | |||
| testDb2.setConnectionUri(dbConns[2]); | |||
| LedgerBindingConfig bindingConfig2 = new LedgerBindingConfig(); | |||
| AsyncCallback<HashDigest> callback2 = nodeCtx2.startInitCommand(privkey2, encodedPassword, initSetting, csProps,csProvider, | |||
| testDb2, consolePrompter, bindingConfig2, quitLatch); | |||
| AsyncCallback<HashDigest> callback2 = nodeCtx2.startInitCommand(privkey2, encodedPassword, initSetting, testDb2, | |||
| consolePrompter, bindingConfig2, quitLatch); | |||
| DBConnectionConfig testDb3 = new DBConnectionConfig(); | |||
| testDb3.setConnectionUri(dbConns[3]); | |||
| LedgerBindingConfig bindingConfig3 = new LedgerBindingConfig(); | |||
| AsyncCallback<HashDigest> callback3 = nodeCtx3.startInitCommand(privkey3, encodedPassword, initSetting, csProps,csProvider, | |||
| testDb3, consolePrompter, bindingConfig3, quitLatch); | |||
| AsyncCallback<HashDigest> callback3 = nodeCtx3.startInitCommand(privkey3, encodedPassword, initSetting, testDb3, | |||
| consolePrompter, bindingConfig3, quitLatch); | |||
| HashDigest ledgerHash0 = callback0.waitReturn(); | |||
| HashDigest ledgerHash1 = callback1.waitReturn(); | |||
| @@ -286,7 +286,7 @@ public class IntegrationTest2 { | |||
| } | |||
| private void testSDK_Contract(AsymmetricKeypair adminKey, HashDigest ledgerHash, | |||
| BlockchainService blockchainService, IntegratedContext context) { | |||
| BlockchainService blockchainService, IntegratedContext context) { | |||
| BlockchainKeypair userKey = BlockchainKeyGenerator.getInstance().generate(); | |||
| // 定义交易; | |||
| @@ -304,19 +304,18 @@ public class IntegrationTest2 { | |||
| assertTrue(txResp.isSuccess()); | |||
| // execute the contract; | |||
| testContractExe(adminKey, ledgerHash, userKey, blockchainService, context); | |||
| testContractExe(adminKey, ledgerHash, userKey, blockchainService, context); | |||
| } | |||
| private void testContractExe(AsymmetricKeypair adminKey, HashDigest ledgerHash, BlockchainKeypair userKey, | |||
| BlockchainService blockchainService, IntegratedContext context) { | |||
| BlockchainService blockchainService, IntegratedContext context) { | |||
| LedgerInfo ledgerInfo = blockchainService.getLedger(ledgerHash); | |||
| LedgerBlock previousBlock = blockchainService.getBlock(ledgerHash, ledgerInfo.getLatestBlockHeight()-1); | |||
| LedgerBlock previousBlock = blockchainService.getBlock(ledgerHash, ledgerInfo.getLatestBlockHeight() - 1); | |||
| // 定义交易; | |||
| TransactionTemplate txTpl = blockchainService.newTransaction(ledgerHash); | |||
| txTpl.contractEvents().send(contractDeployKey.getAddress(), eventName, | |||
| ("888##999##abc").getBytes()); | |||
| txTpl.contractEvents().send(contractDeployKey.getAddress(), eventName, ("888##999##abc").getBytes()); | |||
| // 签名; | |||
| PreparedTransaction ptx = txTpl.prepare(); | |||
| @@ -327,7 +326,6 @@ public class IntegrationTest2 { | |||
| assertTrue(txResp.isSuccess()); | |||
| } | |||
| /** | |||
| * 根据合约构建字节数组; | |||
| * | |||
| @@ -8,13 +8,11 @@ import java.io.InputStream; | |||
| import java.util.Properties; | |||
| import java.util.concurrent.CountDownLatch; | |||
| import com.jd.blockchain.storage.service.DbConnection; | |||
| import org.junit.Test; | |||
| import org.springframework.core.io.ClassPathResource; | |||
| import com.alibaba.fastjson.JSON; | |||
| import com.jd.blockchain.consensus.ConsensusProvider; | |||
| import com.jd.blockchain.consensus.ConsensusProviders; | |||
| import com.jd.blockchain.consensus.ConsensusSettings; | |||
| import com.jd.blockchain.crypto.AsymmetricKeypair; | |||
| import com.jd.blockchain.crypto.HashDigest; | |||
| @@ -34,6 +32,7 @@ import com.jd.blockchain.ledger.core.LedgerRepository; | |||
| import com.jd.blockchain.ledger.core.impl.LedgerManager; | |||
| import com.jd.blockchain.sdk.BlockchainService; | |||
| import com.jd.blockchain.sdk.client.GatewayServiceFactory; | |||
| import com.jd.blockchain.storage.service.DbConnection; | |||
| import com.jd.blockchain.tools.initializer.DBConnectionConfig; | |||
| import com.jd.blockchain.tools.initializer.LedgerBindingConfig; | |||
| import com.jd.blockchain.tools.initializer.LedgerInitProperties; | |||
| @@ -50,7 +49,7 @@ import test.com.jd.blockchain.intgr.initializer.LedgerInitializeWeb4SingleStepsT | |||
| public class IntegrationTestDataAccount { | |||
| LedgerInitConsensusConfig.ConsensusConfig config = LedgerInitConsensusConfig.mqConfig; | |||
| LedgerInitConsensusConfig.ConsensusConfig config = LedgerInitConsensusConfig.mqConfig; | |||
| public IntegratedContext context = initLedgers(config, LedgerInitConsensusConfig.memConnectionStrings); | |||
| public GatewayTestRunner gateway0; | |||
| @@ -173,8 +172,8 @@ public class IntegrationTestDataAccount { | |||
| } | |||
| // 通过调用SDK->GATEWAY,测试一个区块包含多个交易时的写入情况,并验证写入结果; | |||
| private void testAddKvOpToDataAccount(GatewayTestRunner gateway, AsymmetricKeypair adminKey, IntegratedContext context, | |||
| Bytes dataAddr) { | |||
| private void testAddKvOpToDataAccount(GatewayTestRunner gateway, AsymmetricKeypair adminKey, | |||
| IntegratedContext context, Bytes dataAddr) { | |||
| GatewayServiceFactory gwsrvFact = GatewayServiceFactory.connect(gateway.getServiceAddress()); | |||
| BlockchainService blockchainService = gwsrvFact.getBlockchainService(); | |||
| @@ -182,7 +181,8 @@ public class IntegrationTestDataAccount { | |||
| LedgerManager ledgerManager = context.getNode(0).getLedgerManager(); | |||
| DbConnection memoryBasedDb = context.getNode(0).getStorageDB().connect(LedgerInitConsensusConfig.memConnectionStrings[0]); | |||
| DbConnection memoryBasedDb = context.getNode(0).getStorageDB() | |||
| .connect(LedgerInitConsensusConfig.memConnectionStrings[0]); | |||
| LedgerRepository ledgerRepository = ledgerManager.register(ledgerHashs[0], memoryBasedDb.getStorageService()); | |||
| @@ -288,26 +288,26 @@ public class IntegrationTestDataAccount { | |||
| DBConnectionConfig testDb0 = new DBConnectionConfig(); | |||
| testDb0.setConnectionUri(dbConns[0]); | |||
| LedgerBindingConfig bindingConfig0 = new LedgerBindingConfig(); | |||
| AsyncCallback<HashDigest> callback0 = nodeCtx0.startInitCommand(privkey0, encodedPassword, initSetting, csProps, | |||
| csProvider, testDb0, consolePrompter, bindingConfig0, quitLatch); | |||
| AsyncCallback<HashDigest> callback0 = nodeCtx0.startInitCommand(privkey0, encodedPassword, initSetting, testDb0, | |||
| consolePrompter, bindingConfig0, quitLatch); | |||
| DBConnectionConfig testDb1 = new DBConnectionConfig(); | |||
| testDb1.setConnectionUri(dbConns[1]); | |||
| LedgerBindingConfig bindingConfig1 = new LedgerBindingConfig(); | |||
| AsyncCallback<HashDigest> callback1 = nodeCtx1.startInitCommand(privkey1, encodedPassword, initSetting, csProps, | |||
| csProvider, testDb1, consolePrompter, bindingConfig1, quitLatch); | |||
| AsyncCallback<HashDigest> callback1 = nodeCtx1.startInitCommand(privkey1, encodedPassword, initSetting, testDb1, | |||
| consolePrompter, bindingConfig1, quitLatch); | |||
| DBConnectionConfig testDb2 = new DBConnectionConfig(); | |||
| testDb2.setConnectionUri(dbConns[2]); | |||
| LedgerBindingConfig bindingConfig2 = new LedgerBindingConfig(); | |||
| AsyncCallback<HashDigest> callback2 = nodeCtx2.startInitCommand(privkey2, encodedPassword, initSetting, csProps, | |||
| csProvider, testDb2, consolePrompter, bindingConfig2, quitLatch); | |||
| AsyncCallback<HashDigest> callback2 = nodeCtx2.startInitCommand(privkey2, encodedPassword, initSetting, testDb2, | |||
| consolePrompter, bindingConfig2, quitLatch); | |||
| DBConnectionConfig testDb3 = new DBConnectionConfig(); | |||
| testDb3.setConnectionUri(dbConns[3]); | |||
| LedgerBindingConfig bindingConfig3 = new LedgerBindingConfig(); | |||
| AsyncCallback<HashDigest> callback3 = nodeCtx3.startInitCommand(privkey3, encodedPassword, initSetting, csProps, | |||
| csProvider, testDb3, consolePrompter, bindingConfig3, quitLatch); | |||
| AsyncCallback<HashDigest> callback3 = nodeCtx3.startInitCommand(privkey3, encodedPassword, initSetting, testDb3, | |||
| consolePrompter, bindingConfig3, quitLatch); | |||
| HashDigest ledgerHash0 = callback0.waitReturn(); | |||
| HashDigest ledgerHash1 = callback1.waitReturn(); | |||
| @@ -25,13 +25,14 @@ public class LedgerInitSettingTest { | |||
| try { | |||
| LedgerInitProperties setting = LedgerInitProperties.resolve(in); | |||
| assertEquals(4, setting.getConsensusParticipantCount()); | |||
| String expectedLedgerSeed = "932dfe23-fe23232f-283f32fa-dd32aa76-8322ca2f-56236cda-7136b322-cb323ffe".replace("-", ""); | |||
| String expectedLedgerSeed = "932dfe23-fe23232f-283f32fa-dd32aa76-8322ca2f-56236cda-7136b322-cb323ffe" | |||
| .replace("-", ""); | |||
| String actualLedgerSeed = HexUtils.encode(setting.getLedgerSeed()); | |||
| assertEquals(expectedLedgerSeed, actualLedgerSeed); | |||
| ConsensusParticipantConfig part0 = setting.getConsensusParticipant(0); | |||
| assertEquals("jd.com", part0.getName()); | |||
| assertEquals("keys/jd-com.pub", part0.getPubKeyPath()); | |||
| // assertEquals("keys/jd-com.pub", part0.getPubKeyPath()); | |||
| PubKey pubKey0 = KeyGenCommand.decodePubKey("3snPdw7i7PjVKiTH2VnXZu5H8QmNaSXpnk4ei533jFpuifyjS5zzH9"); | |||
| assertEquals(pubKey0, part0.getPubKey()); | |||
| // assertEquals("127.0.0.1", part0.getConsensusAddress().getHost()); | |||
| @@ -40,15 +41,15 @@ public class LedgerInitSettingTest { | |||
| assertEquals("127.0.0.1", part0.getInitializerAddress().getHost()); | |||
| assertEquals(8800, part0.getInitializerAddress().getPort()); | |||
| assertEquals(true, part0.getInitializerAddress().isSecure()); | |||
| ConsensusParticipantConfig part1 = setting.getConsensusParticipant(1); | |||
| assertEquals(false, part1.getInitializerAddress().isSecure()); | |||
| PubKey pubKey1 = KeyGenCommand.decodePubKey("3snPdw7i7PajLB35tEau1kmixc6ZrjLXgxwKbkv5bHhP7nT5dhD9eX"); | |||
| assertEquals(pubKey1, part1.getPubKey()); | |||
| ConsensusParticipantConfig part2 = setting.getConsensusParticipant(2); | |||
| assertEquals(null, part2.getPubKey()); | |||
| assertEquals("7VeRAr3dSbi1xatq11ZcF7sEPkaMmtZhV9shonGJWk9T4pLe", part2.getPubKey().toBase58()); | |||
| } finally { | |||
| in.close(); | |||
| } | |||
| @@ -9,19 +9,16 @@ import java.util.Map; | |||
| import java.util.Properties; | |||
| import java.util.concurrent.ConcurrentHashMap; | |||
| import com.jd.blockchain.storage.service.utils.MemoryDBConnFactory; | |||
| import org.junit.Test; | |||
| import org.springframework.core.io.ClassPathResource; | |||
| import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
| import com.jd.blockchain.consensus.ConsensusProvider; | |||
| import com.jd.blockchain.consensus.ConsensusProviders; | |||
| import com.jd.blockchain.consensus.ConsensusSettings; | |||
| import com.jd.blockchain.crypto.AddressEncoding; | |||
| import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
| import com.jd.blockchain.crypto.CryptoProvider; | |||
| import com.jd.blockchain.crypto.AsymmetricKeypair; | |||
| import com.jd.blockchain.crypto.Crypto; | |||
| import com.jd.blockchain.crypto.CryptoProvider; | |||
| import com.jd.blockchain.crypto.HashDigest; | |||
| import com.jd.blockchain.crypto.PrivKey; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| @@ -38,6 +35,7 @@ import com.jd.blockchain.ledger.core.LedgerRepository; | |||
| import com.jd.blockchain.ledger.core.UserAccount; | |||
| import com.jd.blockchain.ledger.core.UserAccountSet; | |||
| import com.jd.blockchain.ledger.core.impl.LedgerManager; | |||
| import com.jd.blockchain.storage.service.utils.MemoryDBConnFactory; | |||
| import com.jd.blockchain.tools.initializer.DBConnectionConfig; | |||
| import com.jd.blockchain.tools.initializer.LedgerInitProcess; | |||
| import com.jd.blockchain.tools.initializer.LedgerInitProperties; | |||
| @@ -91,8 +89,8 @@ public class LedgerInitializeTest { | |||
| Prompter consolePrompter = new PresetAnswerPrompter("N"); // new ConsolePrompter(); | |||
| LedgerInitProperties initSetting = loadInitSetting(); | |||
| Properties props = loadConsensusSetting(config.getConfigPath()); | |||
| ConsensusProvider csProvider = LedgerInitConsensusConfig.getConsensusProvider(config.getProvider()); | |||
| ConsensusSettings csProps = csProvider.getSettingsFactory().getConsensusSettingsBuilder().createSettings(props); | |||
| // ConsensusProvider csProvider = LedgerInitConsensusConfig.getConsensusProvider(config.getProvider()); | |||
| // ConsensusSettings csProps = csProvider.getSettingsFactory().getConsensusSettingsBuilder().createSettings(props); | |||
| NodeContext node0 = new NodeContext(initSetting.getConsensusParticipant(0).getInitializerAddress(), | |||
| serviceRegisterMap); | |||
| @@ -106,26 +104,22 @@ public class LedgerInitializeTest { | |||
| PrivKey privkey0 = KeyGenCommand.decodePrivKeyWithRawPassword(PRIV_KEYS[0], PASSWORD); | |||
| DBConnectionConfig testDb0 = new DBConnectionConfig(); | |||
| testDb0.setConnectionUri(dbConnections[0]); | |||
| AsyncCallback<HashDigest> callback0 = node0.startInit(0, privkey0, initSetting, csProps, csProvider, testDb0, | |||
| consolePrompter); | |||
| AsyncCallback<HashDigest> callback0 = node0.startInit(0, privkey0, initSetting, testDb0, consolePrompter); | |||
| PrivKey privkey1 = KeyGenCommand.decodePrivKeyWithRawPassword(PRIV_KEYS[1], PASSWORD); | |||
| DBConnectionConfig testDb1 = new DBConnectionConfig(); | |||
| testDb1.setConnectionUri(dbConnections[1]); | |||
| AsyncCallback<HashDigest> callback1 = node1.startInit(1, privkey1, initSetting, csProps, csProvider, testDb1, | |||
| consolePrompter); | |||
| AsyncCallback<HashDigest> callback1 = node1.startInit(1, privkey1, initSetting, testDb1, consolePrompter); | |||
| PrivKey privkey2 = KeyGenCommand.decodePrivKeyWithRawPassword(PRIV_KEYS[2], PASSWORD); | |||
| DBConnectionConfig testDb2 = new DBConnectionConfig(); | |||
| testDb2.setConnectionUri(dbConnections[2]); | |||
| AsyncCallback<HashDigest> callback2 = node2.startInit(2, privkey2, initSetting, csProps, csProvider, testDb2, | |||
| consolePrompter); | |||
| AsyncCallback<HashDigest> callback2 = node2.startInit(2, privkey2, initSetting, testDb2, consolePrompter); | |||
| PrivKey privkey3 = KeyGenCommand.decodePrivKeyWithRawPassword(PRIV_KEYS[3], PASSWORD); | |||
| DBConnectionConfig testDb03 = new DBConnectionConfig(); | |||
| testDb03.setConnectionUri(dbConnections[3]); | |||
| AsyncCallback<HashDigest> callback3 = node3.startInit(3, privkey3, initSetting, csProps, csProvider, testDb03, | |||
| consolePrompter); | |||
| AsyncCallback<HashDigest> callback3 = node3.startInit(3, privkey3, initSetting, testDb03, consolePrompter); | |||
| HashDigest ledgerHash0 = callback0.waitReturn(); | |||
| HashDigest ledgerHash1 = callback1.waitReturn(); | |||
| @@ -240,16 +234,14 @@ public class LedgerInitializeTest { | |||
| } | |||
| public AsyncCallback<HashDigest> startInit(int currentId, PrivKey privKey, LedgerInitProperties setting, | |||
| ConsensusSettings csProps, ConsensusProvider csProvider, DBConnectionConfig dbConnConfig, | |||
| Prompter prompter) { | |||
| DBConnectionConfig dbConnConfig, Prompter prompter) { | |||
| partiKey = new AsymmetricKeypair(setting.getConsensusParticipant(0).getPubKey(), privKey); | |||
| ThreadInvoker<HashDigest> invoker = new ThreadInvoker<HashDigest>() { | |||
| @Override | |||
| protected HashDigest invoke() throws Exception { | |||
| return initProcess.initialize(currentId, privKey, setting, csProps, csProvider, dbConnConfig, | |||
| prompter); | |||
| return initProcess.initialize(currentId, privKey, setting, dbConnConfig, prompter); | |||
| } | |||
| }; | |||
| @@ -257,8 +249,7 @@ public class LedgerInitializeTest { | |||
| } | |||
| public AsyncCallback<HashDigest> startInit(int currentId, PrivKey privKey, LedgerInitProperties setting, | |||
| ConsensusSettings csProps, ConsensusProvider csProvider, DBConnectionConfig dbConnConfig, | |||
| Prompter prompter, boolean autoVerifyHash) { | |||
| DBConnectionConfig dbConnConfig, Prompter prompter, boolean autoVerifyHash) { | |||
| CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; | |||
| for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { | |||
| @@ -274,8 +265,7 @@ public class LedgerInitializeTest { | |||
| ThreadInvoker<HashDigest> invoker = new ThreadInvoker<HashDigest>() { | |||
| @Override | |||
| protected HashDigest invoke() throws Exception { | |||
| return initProcess.initialize(currentId, privKey, setting, csProps, csProvider, dbConnConfig, | |||
| prompter, cryptoSetting); | |||
| return initProcess.initialize(currentId, privKey, setting, dbConnConfig, prompter, cryptoSetting); | |||
| } | |||
| }; | |||
| @@ -94,23 +94,23 @@ public class LedgerInitializeWeb4Nodes { | |||
| DBConnectionConfig testDb0 = new DBConnectionConfig(); | |||
| testDb0.setConnectionUri(dbConns[0]); | |||
| AsyncCallback<HashDigest> callback0 = node0.startInit(privkey0, initSetting, csProps, csProvider, testDb0, | |||
| consolePrompter, quitLatch); | |||
| AsyncCallback<HashDigest> callback0 = node0.startInit(privkey0, initSetting, testDb0, consolePrompter, | |||
| quitLatch); | |||
| DBConnectionConfig testDb1 = new DBConnectionConfig(); | |||
| testDb1.setConnectionUri(dbConns[1]); | |||
| AsyncCallback<HashDigest> callback1 = node1.startInit(privkey1, initSetting, csProps, csProvider, testDb1, | |||
| consolePrompter, quitLatch); | |||
| AsyncCallback<HashDigest> callback1 = node1.startInit(privkey1, initSetting, testDb1, consolePrompter, | |||
| quitLatch); | |||
| DBConnectionConfig testDb2 = new DBConnectionConfig(); | |||
| testDb2.setConnectionUri(dbConns[2]); | |||
| AsyncCallback<HashDigest> callback2 = node2.startInit(privkey2, initSetting, csProps, csProvider, testDb2, | |||
| consolePrompter, quitLatch); | |||
| AsyncCallback<HashDigest> callback2 = node2.startInit(privkey2, initSetting, testDb2, consolePrompter, | |||
| quitLatch); | |||
| DBConnectionConfig testDb03 = new DBConnectionConfig(); | |||
| testDb03.setConnectionUri(dbConns[3]); | |||
| AsyncCallback<HashDigest> callback3 = node3.startInit(privkey3, initSetting, csProps, csProvider, testDb03, | |||
| consolePrompter, quitLatch); | |||
| AsyncCallback<HashDigest> callback3 = node3.startInit(privkey3, initSetting, testDb03, consolePrompter, | |||
| quitLatch); | |||
| HashDigest ledgerHash0 = callback0.waitReturn(); | |||
| HashDigest ledgerHash1 = callback1.waitReturn(); | |||
| @@ -230,8 +230,7 @@ public class LedgerInitializeWeb4Nodes { | |||
| } | |||
| public AsyncCallback<HashDigest> startInit(PrivKey privKey, LedgerInitProperties setting, | |||
| ConsensusSettings csProps, ConsensusProvider csProvider, DBConnectionConfig dbConnConfig, | |||
| Prompter prompter, CountDownLatch quitLatch) { | |||
| DBConnectionConfig dbConnConfig, Prompter prompter, CountDownLatch quitLatch) { | |||
| ThreadInvoker<HashDigest> invoker = new ThreadInvoker<HashDigest>() { | |||
| @Override | |||
| @@ -239,8 +238,8 @@ public class LedgerInitializeWeb4Nodes { | |||
| doStartServer(); | |||
| NodeWebContext.this.dbConnConfig = dbConnConfig; | |||
| HashDigest ledgerHash = NodeWebContext.this.initProcess.initialize(id, privKey, setting, csProps, | |||
| csProvider, dbConnConfig, prompter); | |||
| HashDigest ledgerHash = NodeWebContext.this.initProcess.initialize(id, privKey, setting, | |||
| dbConnConfig, prompter); | |||
| System.out.printf("ledgerHash = %s \r\n", ledgerHash.toBase58()); | |||
| @@ -252,7 +251,6 @@ public class LedgerInitializeWeb4Nodes { | |||
| return invoker.start(); | |||
| } | |||
| public void doStartServer() { | |||
| String argServerAddress = String.format("--server.address=%s", serverAddress.getHost()); | |||
| String argServerPort = String.format("--server.port=%s", serverAddress.getPort()); | |||
| @@ -340,8 +340,7 @@ public class LedgerInitializeWeb4SingleStepsTest { | |||
| } | |||
| public AsyncCallback<HashDigest> startInit(PrivKey privKey, LedgerInitProperties setting, | |||
| ConsensusSettings csProps, ConsensusProvider csProvider, DBConnectionConfig dbConnConfig, | |||
| Prompter prompter, CountDownLatch quitLatch) { | |||
| DBConnectionConfig dbConnConfig, Prompter prompter, CountDownLatch quitLatch) { | |||
| ThreadInvoker<HashDigest> invoker = new ThreadInvoker<HashDigest>() { | |||
| @Override | |||
| @@ -350,8 +349,8 @@ public class LedgerInitializeWeb4SingleStepsTest { | |||
| // NodeWebContext.this.initProcess = ctx.getBean(LedgerInitProcess.class); | |||
| NodeWebContext.this.dbConnConfig = dbConnConfig; | |||
| HashDigest ledgerHash = NodeWebContext.this.initProcess.initialize(id, privKey, setting, csProps, | |||
| csProvider, dbConnConfig, prompter); | |||
| HashDigest ledgerHash = NodeWebContext.this.initProcess.initialize(id, privKey, setting, | |||
| dbConnConfig, prompter); | |||
| quitLatch.countDown(); | |||
| return ledgerHash; | |||
| @@ -362,9 +361,8 @@ public class LedgerInitializeWeb4SingleStepsTest { | |||
| } | |||
| public AsyncCallback<HashDigest> startInitCommand(PrivKey privKey, String base58Pwd, | |||
| LedgerInitProperties ledgerSetting, ConsensusSettings csProps, ConsensusProvider csProvider, | |||
| DBConnectionConfig dbConnConfig, Prompter prompter, LedgerBindingConfig conf, | |||
| CountDownLatch quitLatch) { | |||
| LedgerInitProperties ledgerSetting, DBConnectionConfig dbConnConfig, Prompter prompter, | |||
| LedgerBindingConfig conf, CountDownLatch quitLatch) { | |||
| this.db = new CompositeConnectionFactory(); | |||
| this.dbConnConfig = dbConnConfig; | |||
| @@ -372,8 +370,8 @@ public class LedgerInitializeWeb4SingleStepsTest { | |||
| @Override | |||
| protected HashDigest invoke() throws Exception { | |||
| LedgerInitCommand initCmd = new LedgerInitCommand(); | |||
| HashDigest ledgerHash = initCmd.startInit(id, privKey, base58Pwd, ledgerSetting, csProps, | |||
| csProvider, dbConnConfig, prompter, conf, db); | |||
| HashDigest ledgerHash = initCmd.startInit(id, privKey, base58Pwd, ledgerSetting, dbConnConfig, | |||
| prompter, conf, db); | |||
| NodeWebContext.this.ledgerManager = initCmd.getLedgerManager(); | |||
| quitLatch.countDown(); | |||
| return ledgerHash; | |||
| @@ -141,41 +141,36 @@ public class LedgerBlockGeneratingTest { | |||
| NodeContext node3 = new NodeContext(initSetting.getConsensusParticipant(3).getInitializerAddress(), | |||
| serviceRegisterMap); | |||
| String[] memConns = new String[]{ | |||
| "memory://local/0", | |||
| "memory://local/1", | |||
| "memory://local/2", | |||
| "memory://local/3" | |||
| }; | |||
| String[] memConns = new String[] { "memory://local/0", "memory://local/1", "memory://local/2", | |||
| "memory://local/3" }; | |||
| PrivKey privkey0 = KeyGenCommand.decodePrivKeyWithRawPassword(LedgerInitializeTest.PRIV_KEYS[0], | |||
| LedgerInitializeTest.PASSWORD); | |||
| DBConnectionConfig testDb0 = new DBConnectionConfig(); | |||
| testDb0.setConnectionUri(memConns[0]); | |||
| AsyncCallback<HashDigest> callback0 = node0.startInit(0, privkey0, initSetting, csProps, csProvider, testDb0, | |||
| consolePrompter, !optimized); | |||
| AsyncCallback<HashDigest> callback0 = node0.startInit(0, privkey0, initSetting, testDb0, consolePrompter, | |||
| !optimized); | |||
| PrivKey privkey1 = KeyGenCommand.decodePrivKeyWithRawPassword(LedgerInitializeTest.PRIV_KEYS[1], | |||
| LedgerInitializeTest.PASSWORD); | |||
| DBConnectionConfig testDb1 = new DBConnectionConfig(); | |||
| testDb1.setConnectionUri(memConns[1]); | |||
| AsyncCallback<HashDigest> callback1 = node1.startInit(1, privkey1, initSetting, csProps, csProvider, testDb1, | |||
| consolePrompter, !optimized); | |||
| AsyncCallback<HashDigest> callback1 = node1.startInit(1, privkey1, initSetting, testDb1, consolePrompter, | |||
| !optimized); | |||
| PrivKey privkey2 = KeyGenCommand.decodePrivKeyWithRawPassword(LedgerInitializeTest.PRIV_KEYS[2], | |||
| LedgerInitializeTest.PASSWORD); | |||
| DBConnectionConfig testDb2 = new DBConnectionConfig(); | |||
| testDb2.setConnectionUri(memConns[2]); | |||
| AsyncCallback<HashDigest> callback2 = node2.startInit(2, privkey2, initSetting, csProps, csProvider, testDb2, | |||
| consolePrompter, !optimized); | |||
| AsyncCallback<HashDigest> callback2 = node2.startInit(2, privkey2, initSetting, testDb2, consolePrompter, | |||
| !optimized); | |||
| PrivKey privkey3 = KeyGenCommand.decodePrivKeyWithRawPassword(LedgerInitializeTest.PRIV_KEYS[3], | |||
| LedgerInitializeTest.PASSWORD); | |||
| DBConnectionConfig testDb03 = new DBConnectionConfig(); | |||
| testDb03.setConnectionUri(memConns[3]); | |||
| AsyncCallback<HashDigest> callback3 = node3.startInit(3, privkey3, initSetting, csProps, csProvider, testDb03, | |||
| consolePrompter, !optimized); | |||
| AsyncCallback<HashDigest> callback3 = node3.startInit(3, privkey3, initSetting, testDb03, consolePrompter, | |||
| !optimized); | |||
| HashDigest ledgerHash0 = callback0.waitReturn(); | |||
| HashDigest ledgerHash1 = callback1.waitReturn(); | |||
| @@ -0,0 +1 @@ | |||
| 3snPdw7i7PZi6TStiyc6mzjprnNhgs2atSGNS8wPYzhbKaUWGFJt7x | |||
| @@ -5,6 +5,13 @@ ledger.seed=932dfe23-fe23232f-283f32fa-dd32aa76-8322ca2f-56236cda-7136b322-cb323 | |||
| #账本的描述名称;此属性不参与共识,仅仅在当前参与方的本地节点用于描述用途; | |||
| ledger.name= | |||
| #共识服务提供者;必须; | |||
| consensus.service-provider=com.jd.blockchain.consensus.bftsmart.BftsmartConsensusProvider | |||
| #共识服务的参数配置;必须; | |||
| consensus.conf=classpath:bftsmart.config | |||
| #参与方的个数,后续以 cons_parti.id 分别标识每一个参与方的配置; | |||
| cons_parti.count=4 | |||
| @@ -49,7 +56,7 @@ cons_parti.1.initializer.secure=false | |||
| #第2个参与方的名称; | |||
| cons_parti.2.name=bt.com | |||
| #第2个参与方的公钥文件路径; | |||
| cons_parti.2.pubkey-path=keys/bt-com.pub | |||
| cons_parti.2.pubkey-path=classpath:keys/parti2.pub | |||
| #第2个参与方的公钥内容(由keygen工具生成);此参数优先于 pubkey-path 参数; | |||
| cons_parti.2.pubkey= | |||
| #第2个参与方的共识服务的主机地址; | |||
| @@ -5,6 +5,12 @@ ledger.seed=932dfe23-fe23232f-283f32fa-dd32aa76-8322ca2f-56236cda-7136b322-cb323 | |||
| #账本的描述名称;此属性不参与共识,仅仅在当前参与方的本地节点用于描述用途; | |||
| ledger.name= | |||
| #共识服务提供者;必须; | |||
| consensus.service-provider=com.jd.blockchain.consensus.bftsmart.BftsmartConsensusProvider | |||
| #共识服务的参数配置;必须; | |||
| consensus.conf=classpath:bftsmart.config | |||
| #参与方的个数,后续以 cons_parti.id 分别标识每一个参与方的配置; | |||
| cons_parti.count=4 | |||
| @@ -5,6 +5,12 @@ ledger.seed=932dfe23-fe23232f-283f32fa-dd32aa76-8322ca2f-56236cda-7136b322-cb323 | |||
| #账本的描述名称;此属性不参与共识,仅仅在当前参与方的本地节点用于描述用途; | |||
| #ledger.name= | |||
| #共识服务提供者;必须; | |||
| consensus.service-provider=com.jd.blockchain.consensus.bftsmart.BftsmartConsensusProvider | |||
| #共识服务的参数配置;必须; | |||
| consensus.conf=classpath:bftsmart.config | |||
| #参与方的个数,后续以 cons_parti.id 分别标识每一个参与方的配置; | |||
| cons_parti.count=4 | |||
| @@ -1,7 +1,6 @@ | |||
| package com.jd.blockchain.tools.initializer; | |||
| import java.io.File; | |||
| import java.util.Properties; | |||
| import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; | |||
| import org.springframework.boot.SpringApplication; | |||
| @@ -11,9 +10,6 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties | |||
| import org.springframework.context.ApplicationContextInitializer; | |||
| import org.springframework.context.ConfigurableApplicationContext; | |||
| import com.jd.blockchain.consensus.ConsensusProvider; | |||
| import com.jd.blockchain.consensus.ConsensusProviders; | |||
| import com.jd.blockchain.consensus.ConsensusSettings; | |||
| import com.jd.blockchain.crypto.AddressEncoding; | |||
| import com.jd.blockchain.crypto.HashDigest; | |||
| import com.jd.blockchain.crypto.PrivKey; | |||
| @@ -23,7 +19,6 @@ import com.jd.blockchain.tools.initializer.LedgerBindingConfig.BindingConfig; | |||
| import com.jd.blockchain.tools.initializer.LedgerInitProperties.ConsensusParticipantConfig; | |||
| import com.jd.blockchain.tools.keygen.KeyGenCommand; | |||
| import com.jd.blockchain.utils.ArgumentSet; | |||
| import com.jd.blockchain.utils.ConsoleUtils; | |||
| import com.jd.blockchain.utils.ArgumentSet.ArgEntry; | |||
| import com.jd.blockchain.utils.ArgumentSet.Setting; | |||
| import com.jd.blockchain.utils.io.FileUtils; | |||
| @@ -51,72 +46,68 @@ public class LedgerInitCommand { | |||
| // 是否输出调试信息; | |||
| private static final String DEBUG_OPT = "-debug"; | |||
| private static final Prompter DEFAULT_PROMPTER = new ConsolePrompter(); | |||
| /** | |||
| * 入口; | |||
| * | |||
| * @param args | |||
| */ | |||
| public static void main(String[] args) { | |||
| Prompter prompter = DEFAULT_PROMPTER; | |||
| Setting argSetting = ArgumentSet.setting().prefix(LOCAL_ARG, INI_ARG).option(DEBUG_OPT); | |||
| ArgumentSet argset = ArgumentSet.resolve(args, argSetting); | |||
| try { | |||
| ArgEntry localArg = argset.getArg(LOCAL_ARG); | |||
| if (localArg == null) { | |||
| ConsoleUtils.info("Miss local config file which can be specified with arg [%s]!!!", LOCAL_ARG); | |||
| prompter.info("Miss local config file which can be specified with arg [%s]!!!", LOCAL_ARG); | |||
| } | |||
| LocalConfig localConf = LocalConfig.resolve(localArg.getValue()); | |||
| ArgEntry iniArg = argset.getArg(INI_ARG); | |||
| if (iniArg == null) { | |||
| ConsoleUtils.info("Miss ledger initializing config file which can be specified with arg [%s]!!!", | |||
| INI_ARG); | |||
| prompter.info("Miss ledger initializing config file which can be specified with arg [%s]!!!", INI_ARG); | |||
| return; | |||
| } | |||
| // // load ledger init setting; | |||
| LedgerInitProperties ledgerInitSetting = LedgerInitProperties.resolve(iniArg.getValue()); | |||
| // load ledger init setting; | |||
| LedgerInitProperties ledgerInitProperties = LedgerInitProperties.resolve(iniArg.getValue()); | |||
| String localNodePubKeyString = localConf.getLocal().getPubKeyString(); | |||
| PubKey localNodePubKey = KeyGenCommand.decodePubKey(localNodePubKeyString); | |||
| // 地址根据公钥生成 | |||
| String localNodeAddress = AddressEncoding.generateAddress(localNodePubKey).toBase58(); | |||
| // load all pub keys; | |||
| // 加载全部公钥; | |||
| int currId = -1; | |||
| for (int i = 0; i < ledgerInitSetting.getConsensusParticipantCount(); i++) { | |||
| ConsensusParticipantConfig pconf = ledgerInitSetting.getConsensusParticipant(i); | |||
| String currPartAddress = pconf.getAddress(); | |||
| if (currPartAddress == null) { | |||
| if (pconf.getPubKeyPath() != null) { | |||
| PubKey pubKey = KeyGenCommand.readPubKey(pconf.getPubKeyPath()); | |||
| pconf.setPubKey(pubKey); | |||
| currPartAddress = pconf.getAddress(); | |||
| } | |||
| } | |||
| if (localNodeAddress.equals(currPartAddress)) { | |||
| for (int i = 0; i < ledgerInitProperties.getConsensusParticipantCount(); i++) { | |||
| ConsensusParticipantConfig partiConf = ledgerInitProperties.getConsensusParticipant(i); | |||
| // String partiAddress = partiConf.getAddress(); | |||
| // if (partiAddress == null) { | |||
| // if (partiConf.getPubKeyPath() != null) { | |||
| // PubKey pubKey = KeyGenCommand.readPubKey(partiConf.getPubKeyPath()); | |||
| // partiConf.setPubKey(pubKey); | |||
| // partiAddress = partiConf.getAddress(); | |||
| // } | |||
| // } | |||
| if (localNodeAddress.equals(partiConf.getAddress())) { | |||
| currId = i; | |||
| } | |||
| } | |||
| if (currId == -1) { | |||
| throw new IllegalStateException("The current node specified in local.conf is not found in ledger.init!"); | |||
| throw new IllegalStateException( | |||
| "The current node specified in local.conf is not found in ledger.init!"); | |||
| } | |||
| // 加载当前节点的私钥; | |||
| String base58Pwd = localConf.getLocal().getPassword(); | |||
| if (base58Pwd == null) { | |||
| base58Pwd = KeyGenCommand.readPasswordString(); | |||
| } | |||
| PrivKey privKey = KeyGenCommand.decodePrivKey(localConf.getLocal().getPrivKeyString(), base58Pwd); | |||
| // Load consensus properties; | |||
| Properties props = FileUtils.readProperties(localConf.getConsensusConfig()); | |||
| ConsensusProvider csProvider = ConsensusProviders.getProvider(localConf.getConsensusProvider()); | |||
| ConsensusSettings csSettings = csProvider.getSettingsFactory().getConsensusSettingsBuilder() | |||
| .createSettings(props); | |||
| // ConsensusProperties csProps = new ConsensusProperties(props); | |||
| // Output ledger binding config of peer; | |||
| if (!FileUtils.existDirectory(localConf.getBindingOutDir())) { | |||
| FileUtils.makeDirectory(localConf.getBindingOutDir()); | |||
| @@ -131,27 +122,26 @@ public class LedgerInitCommand { | |||
| // 启动初始化; | |||
| LedgerInitCommand initCommand = new LedgerInitCommand(); | |||
| HashDigest newLedgerHash = initCommand.startInit(currId, privKey, base58Pwd, ledgerInitSetting, csSettings, csProvider, | |||
| localConf.getStoragedDb(), new ConsolePrompter(), conf); | |||
| HashDigest newLedgerHash = initCommand.startInit(currId, privKey, base58Pwd, ledgerInitProperties, | |||
| localConf.getStoragedDb(), prompter, conf); | |||
| if (newLedgerHash != null) { | |||
| // success; | |||
| // so save ledger binding config to file system; | |||
| conf.store(ledgerBindingFile); | |||
| ConsoleUtils.info("\r\n------ Update Ledger binding configuration success! ------[%s]", | |||
| prompter.info("\r\n------ Update Ledger binding configuration success! ------[%s]", | |||
| ledgerBindingFile.getAbsolutePath()); | |||
| } | |||
| // ConsoleUtils.confirm("\r\n\r\n Press any key to quit. :>"); | |||
| } catch (Exception e) { | |||
| ConsoleUtils.error("\r\nError!! -- %s\r\n", e.getMessage()); | |||
| prompter.error("\r\nError!! -- %s\r\n", e.getMessage()); | |||
| if (argset.hasOption(DEBUG_OPT)) { | |||
| e.printStackTrace(); | |||
| } | |||
| ConsoleUtils.error("\r\n Ledger init process has been broken by error!"); | |||
| prompter.error("\r\n Ledger init process has been broken by error!"); | |||
| } | |||
| prompter.confirm(InitializingStep.LEDGER_INIT_COMPLETED.toString(), "\r\n\r\n Press any key to quit. :>"); | |||
| } | |||
| @@ -164,20 +154,19 @@ public class LedgerInitCommand { | |||
| public LedgerInitCommand() { | |||
| } | |||
| public HashDigest startInit(int currId, PrivKey privKey, String base58Pwd, LedgerInitProperties ledgerSetting, | |||
| ConsensusSettings csSettings, ConsensusProvider csProvider, DBConnectionConfig dbConnConfig, | |||
| Prompter prompter, LedgerBindingConfig conf, Object... extBeans) { | |||
| if (currId < 0 || currId >= ledgerSetting.getConsensusParticipantCount()) { | |||
| ConsoleUtils.info( | |||
| public HashDigest startInit(int currId, PrivKey privKey, String base58Pwd, | |||
| LedgerInitProperties ledgerInitProperties, DBConnectionConfig dbConnConfig, Prompter prompter, | |||
| LedgerBindingConfig conf, Object... extBeans) { | |||
| if (currId < 0 || currId >= ledgerInitProperties.getConsensusParticipantCount()) { | |||
| prompter.info( | |||
| "Your participant id is illegal which is less than 1 or great than the total participants count[%s]!!!", | |||
| ledgerSetting.getConsensusParticipantCount()); | |||
| ledgerInitProperties.getConsensusParticipantCount()); | |||
| return null; | |||
| } | |||
| // generate binding config; | |||
| BindingConfig bindingConf = new BindingConfig(); | |||
| // bindingConf.setCsConfigFile(localConf.getConsensusConfig()); | |||
| bindingConf.getParticipant().setAddress(ledgerSetting.getConsensusParticipant(currId).getAddress()); | |||
| bindingConf.getParticipant().setAddress(ledgerInitProperties.getConsensusParticipant(currId).getAddress()); | |||
| String encodedPrivKey = KeyGenCommand.encodePrivKey(privKey, base58Pwd); | |||
| bindingConf.getParticipant().setPk(encodedPrivKey); | |||
| bindingConf.getParticipant().setPassword(base58Pwd); | |||
| @@ -185,16 +174,13 @@ public class LedgerInitCommand { | |||
| bindingConf.getDbConnection().setConnectionUri(dbConnConfig.getUri()); | |||
| bindingConf.getDbConnection().setPassword(dbConnConfig.getPassword()); | |||
| // bindingConf.getMqConnection().setServer(mqConnConfig.getServer()); | |||
| // bindingConf.getMqConnection().setTopic(mqConnConfig.getTopic()); | |||
| // confirm continue; | |||
| prompter.info("\r\n\r\n This is participant [%s], the ledger initialization is ready to start!\r\n", currId); | |||
| // ConsoleUtils.confirm("Press any key to continue... "); | |||
| // prompter.confirm("Press any key to continue... "); | |||
| // ConsoleUtils.confirm("Press any key to continue... "); | |||
| // prompter.confirm("Press any key to continue... "); | |||
| // start web listener; | |||
| NetworkAddress serverAddress = ledgerSetting.getConsensusParticipant(currId).getInitializerAddress(); | |||
| // start the web controller of Ledger Initializer; | |||
| NetworkAddress serverAddress = ledgerInitProperties.getConsensusParticipant(currId).getInitializerAddress(); | |||
| String argServerAddress = String.format("--server.address=%s", serverAddress.getHost()); | |||
| String argServerPort = String.format("--server.port=%s", serverAddress.getPort()); | |||
| String[] innerArgs = { argServerAddress, argServerPort }; | |||
| @@ -211,21 +197,21 @@ public class LedgerInitCommand { | |||
| ConfigurableApplicationContext ctx = app.run(innerArgs); | |||
| this.ledgerManager = ctx.getBean(LedgerManager.class); | |||
| prompter.info("\r\n------ Web listener[%s:%s] was started. ------\r\n", serverAddress.getHost(), | |||
| serverAddress.getPort()); | |||
| prompter.info("\r\n------ Web controller of Ledger Initializer[%s:%s] was started. ------\r\n", | |||
| serverAddress.getHost(), serverAddress.getPort()); | |||
| try { | |||
| LedgerInitProcess initProc = ctx.getBean(LedgerInitProcess.class); | |||
| HashDigest ledgerHash = initProc.initialize(currId, privKey, ledgerSetting, csSettings, csProvider, | |||
| HashDigest ledgerHash = initProc.initialize(currId, privKey, ledgerInitProperties, | |||
| bindingConf.getDbConnection(), prompter); | |||
| if (ledgerHash == null) { | |||
| // ledger init fail; | |||
| ConsoleUtils.error("\r\n------ Ledger initialize fail! ------\r\n"); | |||
| prompter.error("\r\n------ Ledger initialize fail! ------\r\n"); | |||
| return null; | |||
| } else { | |||
| ConsoleUtils.info("\r\n------ Ledger initialize success! ------"); | |||
| ConsoleUtils.info("New Ledger Hash is :[%s]", ledgerHash.toBase58()); | |||
| prompter.info("\r\n------ Ledger initialize success! ------"); | |||
| prompter.info("New Ledger Hash is :[%s]", ledgerHash.toBase58()); | |||
| if (conf == null) { | |||
| conf = new LedgerBindingConfig(); | |||
| @@ -237,7 +223,7 @@ public class LedgerInitCommand { | |||
| } | |||
| } finally { | |||
| ctx.close(); | |||
| ConsoleUtils.info("\r\n------ Web listener[%s:%s] was closed. ------\r\n", serverAddress.getHost(), | |||
| prompter.info("\r\n------ Web listener[%s:%s] was closed. ------\r\n", serverAddress.getHost(), | |||
| serverAddress.getPort()); | |||
| } | |||
| } | |||
| @@ -17,44 +17,26 @@ public interface LedgerInitProcess { | |||
| /** | |||
| * Init a new ledger; | |||
| * | |||
| * @param currentId | |||
| * @param privKey | |||
| * @param ledgerInitProps | |||
| * 账本初始化配置属性; | |||
| * @param consensusSettings | |||
| * 共识配置属性; | |||
| * @param consensusServiceProvider | |||
| * @param prompter | |||
| * @return 返回新账本的 hash;如果未初始化成功,则返回 null; | |||
| */ | |||
| /** | |||
| * Init a new ledger; | |||
| * @param currentId Id of current participant; | |||
| * @param privKey Private key of current participant; | |||
| * @param ledgerInitProps The settings about this initialization; | |||
| * @param consensusSettings The consensus settings | |||
| * @param consensusProvider | |||
| * @param dbConnConfig | |||
| * @param prompter | |||
| * @param currentId Id of current participant. | |||
| * @param privKey Private key of current participant. | |||
| * @param ledgerInitProps The settings about this initialization. | |||
| * @param dbConnConfig The configuration of DB Connection. | |||
| * @param prompter Prompter for interaction. | |||
| * @return | |||
| */ | |||
| HashDigest initialize(int currentId, PrivKey privKey, LedgerInitProperties ledgerInitProps, | |||
| ConsensusSettings consensusSettings, ConsensusProvider consensusProvider, | |||
| DBConnectionConfig dbConnConfig, Prompter prompter); | |||
| /** | |||
| * @param currentId | |||
| * @param privKey | |||
| * @param ledgerInitProps | |||
| * @param consensusSettings | |||
| * @param consensusProvider | |||
| * @param dbConnConfig | |||
| * @param prompter | |||
| * @param cryptoSetting | |||
| * @return | |||
| */ | |||
| HashDigest initialize(int currentId, PrivKey privKey, LedgerInitProperties ledgerInitProps, | |||
| ConsensusSettings consensusSettings, ConsensusProvider consensusProvider, | |||
| DBConnectionConfig dbConnConfig, Prompter prompter, CryptoSetting cryptoSetting); | |||
| } | |||
| @@ -1,23 +1,23 @@ | |||
| package com.jd.blockchain.tools.initializer; | |||
| import java.io.ByteArrayOutputStream; | |||
| import java.io.File; | |||
| import java.io.FileNotFoundException; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| import java.util.Properties; | |||
| import org.springframework.util.ResourceUtils; | |||
| import com.jd.blockchain.crypto.AddressEncoding; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.ledger.ParticipantNode; | |||
| import com.jd.blockchain.tools.keygen.KeyGenCommand; | |||
| import com.jd.blockchain.utils.PropertiesUtils; | |||
| import com.jd.blockchain.utils.codec.HexUtils; | |||
| import com.jd.blockchain.utils.io.FileUtils; | |||
| import com.jd.blockchain.utils.net.NetworkAddress; | |||
| import org.springframework.core.io.ClassPathResource; | |||
| public class LedgerInitProperties { | |||
| // 账本种子; | |||
| @@ -32,12 +32,7 @@ public class LedgerInitProperties { | |||
| public static final String PART_PUBKEY_PATH = "pubkey-path"; | |||
| // 参与方的公钥文件路径; | |||
| public static final String PART_PUBKEY = "pubkey"; | |||
| // //共识参与方的共识服务的主机地址; | |||
| // public static final String PART_CONSENSUS_HOST = "consensus.host"; | |||
| // // 共识参与方的共识服务的端口; | |||
| // public static final String PART_CONSENSUS_PORT = "consensus.port"; | |||
| // // 共识参与方的共识服务是否开启安全连接; | |||
| // public static final String PART_CONSENSUS_SECURE = "consensus.secure"; | |||
| // 共识参与方的账本初始服务的主机; | |||
| public static final String PART_INITIALIZER_HOST = "initializer.host"; | |||
| // 共识参与方的账本初始服务的端口; | |||
| @@ -45,70 +40,30 @@ public class LedgerInitProperties { | |||
| // 共识参与方的账本初始服务是否开启安全连接; | |||
| public static final String PART_INITIALIZER_SECURE = "initializer.secure"; | |||
| // 共识服务的参数配置;必须; | |||
| public static final String CONSENSUS_CONFIG = "consensus.conf"; | |||
| // 共识服务提供者;必须; | |||
| public static final String CONSENSUS_SERVICE_PROVIDER = "consensus.service-provider"; | |||
| private byte[] ledgerSeed; | |||
| private List<ConsensusParticipantConfig> consensusParticipants = new ArrayList<>(); | |||
| private String consensusProvider; | |||
| private Properties consensusConfig; | |||
| public byte[] getLedgerSeed() { | |||
| return ledgerSeed; | |||
| } | |||
| public static byte[] getHostSettingValue() throws Exception { | |||
| ClassPathResource hostConfigResource = new ClassPathResource("hosts.config"); | |||
| InputStream fis = hostConfigResource.getInputStream(); | |||
| ByteArrayOutputStream bos = null; | |||
| byte[] buffer = null; | |||
| try { | |||
| bos = new ByteArrayOutputStream(1000); | |||
| byte[] b = new byte[1000]; | |||
| int n; | |||
| while ((n = fis.read(b)) != -1) { | |||
| bos.write(b, 0, n); | |||
| } | |||
| // host file to bytes | |||
| buffer = bos.toByteArray(); | |||
| } catch (FileNotFoundException e) { | |||
| throw new Exception(e.getMessage(), e); | |||
| } catch (IOException e) { | |||
| throw new Exception(e.getMessage(), e); | |||
| } finally { | |||
| if (fis != null) { | |||
| fis.close(); | |||
| } | |||
| if (bos != null) { | |||
| bos.close(); | |||
| } | |||
| } | |||
| return buffer; | |||
| public Properties getConsensusConfig() { | |||
| return consensusConfig; | |||
| } | |||
| public static byte[] getSystemSettingValue() throws Exception { | |||
| ClassPathResource systemConfigResource = new ClassPathResource("bftsmart.config"); | |||
| InputStream fis = systemConfigResource.getInputStream(); | |||
| ByteArrayOutputStream bos = null; | |||
| byte[] buffer = null; | |||
| try { | |||
| bos = new ByteArrayOutputStream(1000); | |||
| byte[] b = new byte[1000]; | |||
| int n; | |||
| while ((n = fis.read(b)) != -1) { | |||
| bos.write(b, 0, n); | |||
| } | |||
| // file to bytes | |||
| buffer = bos.toByteArray(); | |||
| } catch (FileNotFoundException e) { | |||
| throw new Exception(e.getMessage(), e); | |||
| } catch (IOException e) { | |||
| throw new Exception(e.getMessage(), e); | |||
| } finally { | |||
| if (fis != null) { | |||
| fis.close(); | |||
| } | |||
| if (bos != null) { | |||
| bos.close(); | |||
| } | |||
| } | |||
| return buffer; | |||
| public String getConsensusProvider() { | |||
| return consensusProvider; | |||
| } | |||
| public int getConsensusParticipantCount() { | |||
| @@ -119,20 +74,15 @@ public class LedgerInitProperties { | |||
| return consensusParticipants; | |||
| } | |||
| public ConsensusParticipantConfig[] getConsensusParticipantArray() { | |||
| return consensusParticipants.toArray(new ConsensusParticipantConfig[consensusParticipants.size()]); | |||
| } | |||
| /** | |||
| * 返回参与者; | |||
| * | |||
| * @param address | |||
| * 从 1 开始; 小于等于 {@link #getConsensusParticipantCount()}; | |||
| * @param address 从 1 开始; 小于等于 {@link #getConsensusParticipantCount()}; | |||
| * @return | |||
| */ | |||
| public ConsensusParticipantConfig getConsensusParticipant(int id) { | |||
| for (ConsensusParticipantConfig p : consensusParticipants) { | |||
| if (p.getId()== id) { | |||
| if (p.getId() == id) { | |||
| return p; | |||
| } | |||
| } | |||
| @@ -163,11 +113,23 @@ public class LedgerInitProperties { | |||
| } | |||
| private static LedgerInitProperties resolve(Properties props) { | |||
| String hexLedgerSeed = getProperty(props, LEDGER_SEED).replace("-", ""); | |||
| String hexLedgerSeed = PropertiesUtils.getRequiredProperty(props, LEDGER_SEED).replace("-", ""); | |||
| byte[] ledgerSeed = HexUtils.decode(hexLedgerSeed); | |||
| LedgerInitProperties setting = new LedgerInitProperties(ledgerSeed); | |||
| LedgerInitProperties initProps = new LedgerInitProperties(ledgerSeed); | |||
| // 解析共识相关的属性; | |||
| initProps.consensusProvider = PropertiesUtils.getRequiredProperty(props, CONSENSUS_SERVICE_PROVIDER); | |||
| String consensusConfigFilePath = PropertiesUtils.getRequiredProperty(props, CONSENSUS_CONFIG); | |||
| try { | |||
| File consensusConfigFile = ResourceUtils.getFile(consensusConfigFilePath); | |||
| initProps.consensusConfig = FileUtils.readProperties(consensusConfigFile); | |||
| } catch (FileNotFoundException e) { | |||
| throw new IllegalArgumentException( | |||
| String.format("Consensus config file[%s] doesn't exist! ", consensusConfigFilePath), e); | |||
| } | |||
| int partCount = getInt(getProperty(props, PART_COUNT)); | |||
| // 解析参与方节点列表; | |||
| int partCount = getInt(PropertiesUtils.getRequiredProperty(props, PART_COUNT)); | |||
| if (partCount < 0) { | |||
| throw new IllegalArgumentException(String.format("Property[%s] is negative!", PART_COUNT)); | |||
| } | |||
| @@ -180,62 +142,40 @@ public class LedgerInitProperties { | |||
| parti.setId(i); | |||
| String nameKey = getKeyOfCsParti(i, PART_NAME); | |||
| parti.setName(getProperty(props, nameKey)); | |||
| parti.setName(PropertiesUtils.getRequiredProperty(props, nameKey)); | |||
| String pubkeyPathKey = getKeyOfCsParti(i, PART_PUBKEY_PATH); | |||
| parti.setPubKeyPath(getProperty(props, pubkeyPathKey)); | |||
| String pubkeyPath = PropertiesUtils.getProperty(props, pubkeyPathKey, false); | |||
| String pubkeyKey = getKeyOfCsParti(i, PART_PUBKEY); | |||
| String base58PubKey = getProperty(props, pubkeyKey); | |||
| String base58PubKey = PropertiesUtils.getProperty(props, pubkeyKey, false); | |||
| if (base58PubKey != null) { | |||
| PubKey pubKey = KeyGenCommand.decodePubKey(base58PubKey); | |||
| parti.setPubKey(pubKey); | |||
| } else if (pubkeyPath != null) { | |||
| PubKey pubKey = KeyGenCommand.readPubKey(pubkeyPath); | |||
| parti.setPubKey(pubKey); | |||
| } else { | |||
| throw new IllegalArgumentException( | |||
| String.format("Property[%s] and property[%s] are all empty!", pubkeyKey, pubkeyPathKey)); | |||
| } | |||
| // String consensusHostKey = getKeyOfCsParti(i, PART_CONSENSUS_HOST); | |||
| // String consensusHost = getProperty(props, consensusHostKey); | |||
| // | |||
| // String consensusPortKey = getKeyOfCsParti(i, PART_CONSENSUS_PORT); | |||
| // int consensusPort = getInt(getProperty(props, consensusPortKey)); | |||
| // | |||
| // String consensusSecureKey = getKeyOfCsParti(i, PART_CONSENSUS_SECURE); | |||
| // boolean consensusSecure = Boolean.parseBoolean(getProperty(props, | |||
| // consensusSecureKey)); | |||
| // NetworkAddress consensusAddress = new NetworkAddress(consensusHost, | |||
| // consensusPort, consensusSecure); | |||
| // parti.setConsensusAddress(consensusAddress); | |||
| String initializerHostKey = getKeyOfCsParti(i, PART_INITIALIZER_HOST); | |||
| String initializerHost = getProperty(props, initializerHostKey); | |||
| String initializerHost = PropertiesUtils.getRequiredProperty(props, initializerHostKey); | |||
| String initializerPortKey = getKeyOfCsParti(i, PART_INITIALIZER_PORT); | |||
| int initializerPort = getInt(getProperty(props, initializerPortKey)); | |||
| int initializerPort = getInt(PropertiesUtils.getRequiredProperty(props, initializerPortKey)); | |||
| String initializerSecureKey = getKeyOfCsParti(i, PART_INITIALIZER_SECURE); | |||
| boolean initializerSecure = Boolean.parseBoolean(getProperty(props, initializerSecureKey)); | |||
| boolean initializerSecure = Boolean | |||
| .parseBoolean(PropertiesUtils.getRequiredProperty(props, initializerSecureKey)); | |||
| NetworkAddress initializerAddress = new NetworkAddress(initializerHost, initializerPort, initializerSecure); | |||
| parti.setInitializerAddress(initializerAddress); | |||
| setting.addConsensusParticipant(parti); | |||
| initProps.addConsensusParticipant(parti); | |||
| } | |||
| return setting; | |||
| } | |||
| private static String getProperty(Properties props, String key) { | |||
| return getProperty(props, key, true); | |||
| } | |||
| private static String getProperty(Properties props, String key, boolean required) { | |||
| String value = props.getProperty(key); | |||
| if (value == null) { | |||
| if (required) { | |||
| throw new IllegalArgumentException("Miss property[" + key + "]!"); | |||
| } | |||
| return null; | |||
| } | |||
| value = value.trim(); | |||
| return value.length() == 0 ? null : value; | |||
| return initProps; | |||
| } | |||
| private static int getInt(String strInt) { | |||
| @@ -249,14 +189,14 @@ public class LedgerInitProperties { | |||
| * | |||
| */ | |||
| public static class ConsensusParticipantConfig implements ParticipantNode { | |||
| private int id; | |||
| private String address; | |||
| private String name; | |||
| private String pubKeyPath; | |||
| // private String pubKeyPath; | |||
| private PubKey pubKey; | |||
| @@ -271,7 +211,7 @@ public class LedgerInitProperties { | |||
| public void setId(int id) { | |||
| this.id = id; | |||
| } | |||
| @Override | |||
| public String getAddress() { | |||
| return address; | |||
| @@ -285,13 +225,13 @@ public class LedgerInitProperties { | |||
| this.name = name; | |||
| } | |||
| public String getPubKeyPath() { | |||
| return pubKeyPath; | |||
| } | |||
| public void setPubKeyPath(String pubKeyPath) { | |||
| this.pubKeyPath = pubKeyPath; | |||
| } | |||
| // public String getPubKeyPath() { | |||
| // return pubKeyPath; | |||
| // } | |||
| // | |||
| // public void setPubKeyPath(String pubKeyPath) { | |||
| // this.pubKeyPath = pubKeyPath; | |||
| // } | |||
| public NetworkAddress getInitializerAddress() { | |||
| return initializerAddress; | |||
| @@ -2,8 +2,6 @@ package com.jd.blockchain.tools.initializer; | |||
| import java.io.File; | |||
| import java.io.InputStream; | |||
| import java.net.URI; | |||
| import java.nio.file.Path; | |||
| import java.util.Properties; | |||
| import com.jd.blockchain.utils.PathUtils; | |||
| @@ -30,17 +28,11 @@ public class LocalConfig { | |||
| // 账本数据库的连接口令; | |||
| public static final String LEDGER_DB_PWD = "ledger.db.pwd"; | |||
| // 账本消息队列的地址 | |||
| // public static final String LEDGER_MQ_SERVER = "ledger.mq.server"; | |||
| // | |||
| // // 账本消息队列的主题 | |||
| // public static final String LEDGER_MQ_TOPIC = "ledger.mq.topic"; | |||
| // 共识系统的参数配置;必须参数; | |||
| public static final String CONSENSUS_CONF = "consensus.conf"; | |||
| // 共识系统的参数配置;必须参数; | |||
| public static final String CONSENSUS_SERVICE_PROVIDER = "consensus.service-provider"; | |||
| // // 共识系统的参数配置;必须参数; | |||
| // public static final String CONSENSUS_CONF = "consensus.conf"; | |||
| // | |||
| // // 共识系统的参数配置;必须参数; | |||
| // public static final String CONSENSUS_SERVICE_PROVIDER = "consensus.service-provider"; | |||
| private LocalParticipantConfig local = new LocalParticipantConfig(); | |||
| @@ -48,10 +40,9 @@ public class LocalConfig { | |||
| private DBConnectionConfig storagedDb = new DBConnectionConfig(); | |||
| // private MQConnectionConfig handleMq = new MQConnectionConfig(); | |||
| private String consensusConfig; | |||
| private String consensusProvider; | |||
| // private String consensusConfig; | |||
| // private String consensusProvider; | |||
| public LocalParticipantConfig getLocal() { | |||
| return local; | |||
| @@ -77,22 +68,6 @@ public class LocalConfig { | |||
| this.storagedDb = storagedDb; | |||
| } | |||
| // public MQConnectionConfig getHandleMq() { | |||
| // return handleMq; | |||
| // } | |||
| // | |||
| // public void setHandleMq(MQConnectionConfig handleMq) { | |||
| // this.handleMq = handleMq; | |||
| // } | |||
| public String getConsensusConfig() { | |||
| return consensusConfig; | |||
| } | |||
| public void setConsensusConfig(String consensusConfig) { | |||
| this.consensusConfig = consensusConfig; | |||
| } | |||
| public static LocalConfig resolve(String initSettingFile) { | |||
| Properties props = FileUtils.readProperties(initSettingFile, "UTF-8"); | |||
| return resolve(props, initSettingFile); | |||
| @@ -118,17 +93,12 @@ public class LocalConfig { | |||
| if (initSettingFile == null) { | |||
| conf.bindingOutDir = PropertiesUtils.getRequiredProperty(props, LEDGER_BINDING_OUT); | |||
| conf.consensusConfig = PropertiesUtils.getRequiredProperty(props, CONSENSUS_CONF); | |||
| } else { | |||
| String bindingOutDir = PropertiesUtils.getRequiredProperty(props, LEDGER_BINDING_OUT); | |||
| String consensusConfig = PropertiesUtils.getRequiredProperty(props, CONSENSUS_CONF); | |||
| String initSettingDir = PathUtils.concatPaths(initSettingFile, "../"); | |||
| conf.bindingOutDir = absolutePath(initSettingDir, bindingOutDir); | |||
| conf.consensusConfig = absolutePath(initSettingDir, consensusConfig); | |||
| } | |||
| conf.consensusProvider = PropertiesUtils.getRequiredProperty(props, CONSENSUS_SERVICE_PROVIDER); | |||
| return conf; | |||
| } | |||
| @@ -141,18 +111,6 @@ public class LocalConfig { | |||
| return absolutePath; | |||
| } | |||
| private static int getInt(String strInt) { | |||
| return Integer.parseInt(strInt.trim()); | |||
| } | |||
| public String getConsensusProvider() { | |||
| return consensusProvider; | |||
| } | |||
| public void setConsensusProvider(String consensusProvider) { | |||
| this.consensusProvider = consensusProvider; | |||
| } | |||
| /** | |||
| * 当前参与方的本地配置信息; | |||
| * | |||
| @@ -192,12 +150,4 @@ public class LocalConfig { | |||
| } | |||
| public static void main(String[] args) { | |||
| String currPath = "/Users/zhanglin33/Ddisk/20mintest/0/config/init/local.conf"; | |||
| // String settingPath = "../"; | |||
| String settingPath = "bftsmart.config"; | |||
| String path = absolutePath(PathUtils.concatPaths(currPath, "../"), settingPath); | |||
| System.out.println(path); | |||
| } | |||
| } | |||
| @@ -2,6 +2,28 @@ package com.jd.blockchain.tools.initializer; | |||
| public interface Prompter { | |||
| //定义一些常用的问答常量; | |||
| /** | |||
| * 询问是或否; | |||
| */ | |||
| public static final String QUESTION_YESNO = "YES/NO"; | |||
| /** | |||
| * 提示按任意键继续; | |||
| */ | |||
| public static final String PROMPT_ANYKEY_TO_CONTINUE = "ANYKEY"; | |||
| /** | |||
| * | |||
| */ | |||
| public static final String ANSWER_YES = "Y"; | |||
| public static final String ANSWER_NO = "N"; | |||
| void info(String format, Object... args); | |||
| void error(String format, Object... args); | |||
| @@ -5,6 +5,7 @@ import java.util.ArrayList; | |||
| import java.util.Arrays; | |||
| import java.util.Comparator; | |||
| import java.util.List; | |||
| import java.util.Properties; | |||
| import java.util.Random; | |||
| import java.util.concurrent.CountDownLatch; | |||
| import java.util.concurrent.TimeUnit; | |||
| @@ -18,10 +19,10 @@ import org.springframework.web.bind.annotation.RestController; | |||
| import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
| import com.jd.blockchain.consensus.ConsensusProvider; | |||
| import com.jd.blockchain.consensus.ConsensusProviders; | |||
| import com.jd.blockchain.consensus.ConsensusSettings; | |||
| import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
| import com.jd.blockchain.crypto.CryptoProvider; | |||
| import com.jd.blockchain.crypto.Crypto; | |||
| import com.jd.blockchain.crypto.CryptoProvider; | |||
| import com.jd.blockchain.crypto.HashDigest; | |||
| import com.jd.blockchain.crypto.PrivKey; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| @@ -56,11 +57,11 @@ import com.jd.blockchain.tools.initializer.LedgerInitException; | |||
| import com.jd.blockchain.tools.initializer.LedgerInitProcess; | |||
| import com.jd.blockchain.tools.initializer.LedgerInitProperties; | |||
| import com.jd.blockchain.tools.initializer.LedgerInitProperties.ConsensusParticipantConfig; | |||
| import com.jd.blockchain.tools.initializer.Prompter; | |||
| import com.jd.blockchain.transaction.DigitalSignatureBlob; | |||
| import com.jd.blockchain.transaction.LedgerInitSettingData; | |||
| import com.jd.blockchain.transaction.TxBuilder; | |||
| import com.jd.blockchain.transaction.TxRequestBuilder; | |||
| import com.jd.blockchain.tools.initializer.Prompter; | |||
| import com.jd.blockchain.utils.Bytes; | |||
| import com.jd.blockchain.utils.concurrent.InvocationResult; | |||
| import com.jd.blockchain.utils.io.BytesUtils; | |||
| @@ -78,13 +79,12 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI | |||
| static { | |||
| DataContractRegistry.register(TransactionRequest.class); | |||
| } | |||
| private static final String[] SUPPORTED_PROVIDERS = { ClassicCryptoService.class.getName(), | |||
| SMCryptoService.class.getName() }; | |||
| private static final String DEFAULT_SIGN_ALGORITHM = "ED25519"; | |||
| private final SignatureFunction SIGN_FUNC; | |||
| private volatile LedgerInitPermission localPermission; | |||
| @@ -129,7 +129,7 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI | |||
| public LedgerInitializeWebController(LedgerManage ledgerManager, DbConnectionFactory dbConnFactory, | |||
| InitConsensusServiceFactory initCsServiceFactory) { | |||
| this.SIGN_FUNC = Crypto.getSignatureFunction(DEFAULT_SIGN_ALGORITHM); | |||
| this.ledgerManager = ledgerManager; | |||
| this.dbConnFactory = dbConnFactory; | |||
| this.initCsServiceFactory = initCsServiceFactory; | |||
| @@ -155,36 +155,39 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI | |||
| this.prompter = prompter; | |||
| } | |||
| public ConsensusProvider getConsensusProvider() { | |||
| return consensusProvider; | |||
| } | |||
| // private ConsensusProvider getConsensusProvider() { | |||
| // return consensusProvider; | |||
| // } | |||
| public void setConsensusProvider(ConsensusProvider consensusProvider) { | |||
| private void setConsensusProvider(ConsensusProvider consensusProvider) { | |||
| this.consensusProvider = consensusProvider; | |||
| } | |||
| @Override | |||
| public HashDigest initialize(int currentId, PrivKey privKey, LedgerInitProperties ledgerInitProps, | |||
| ConsensusSettings csSettings, ConsensusProvider csProvider, DBConnectionConfig dbConnConfig, | |||
| Prompter prompter) { | |||
| return initialize(currentId, privKey, ledgerInitProps, csSettings, csProvider, dbConnConfig, prompter, | |||
| createDefaultCryptoSetting()); | |||
| DBConnectionConfig dbConnConfig, Prompter prompter) { | |||
| return initialize(currentId, privKey, ledgerInitProps, dbConnConfig, prompter, createDefaultCryptoSetting()); | |||
| } | |||
| @Override | |||
| public HashDigest initialize(int currentId, PrivKey privKey, LedgerInitProperties ledgerInitProps, | |||
| ConsensusSettings consensusProps, ConsensusProvider csProvider, DBConnectionConfig dbConnConfig, | |||
| Prompter prompter, CryptoSetting cryptoSetting) { | |||
| DBConnectionConfig dbConnConfig, Prompter prompter, CryptoSetting cryptoSetting) { | |||
| if (this.ledgerInitSetting != null) { | |||
| throw new IllegalStateException("ledger init process has already started."); | |||
| } | |||
| setPrompter(prompter); | |||
| Properties csProps = ledgerInitProps.getConsensusConfig(); | |||
| ConsensusProvider csProvider = ConsensusProviders.getProvider(ledgerInitProps.getConsensusProvider()); | |||
| ConsensusSettings csSettings = csProvider.getSettingsFactory().getConsensusSettingsBuilder() | |||
| .createSettings(csProps); | |||
| setConsensusProvider(csProvider); | |||
| prompter.info("Init settings and sign permision..."); | |||
| prepareLocalPermission(currentId, privKey, ledgerInitProps, consensusProps, cryptoSetting); | |||
| prepareLocalPermission(currentId, privKey, ledgerInitProps, csSettings, cryptoSetting); | |||
| prompter.confirm(InitializingStep.PERMISSION_READY.toString(), | |||
| "Ledger init permission has already prepared! Any key to continue..."); | |||
| @@ -237,7 +240,7 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI | |||
| this.decisions[i] = new DecisionResultHandle(i); | |||
| } | |||
| // 预置当前参与方的“决定”到列表,避免向自己发起请求; | |||
| this.decisions[currentId].setResult(localDecision); | |||
| this.decisions[currentId].setValue(localDecision); | |||
| return localDecision; | |||
| } | |||
| @@ -330,7 +333,9 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI | |||
| LedgerInitSettingData initSetting = new LedgerInitSettingData(); | |||
| initSetting.setLedgerSeed(ledgerProps.getLedgerSeed()); | |||
| initSetting.setCryptoSetting(cryptoSetting); | |||
| ConsensusParticipantConfig[] parties = ledgerProps.getConsensusParticipantArray(); | |||
| List<ConsensusParticipantConfig> partiList = ledgerProps.getConsensusParticipants(); | |||
| ConsensusParticipantConfig[] parties = partiList.toArray(new ConsensusParticipantConfig[partiList.size()]); | |||
| ConsensusParticipantConfig[] orderedParties = sortAndVerify(parties); | |||
| initSetting.setConsensusParticipants(orderedParties); | |||
| @@ -459,7 +464,7 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI | |||
| List<String> waitingIds = new ArrayList<>(); | |||
| for (int i = 0; i < results.length; i++) { | |||
| if (results[i] != null) { | |||
| if (results[i].getResult() == null) { | |||
| if (results[i].getValue() == null) { | |||
| waitingIds.add("" + (i + 1)); | |||
| } | |||
| } | |||
| @@ -486,7 +491,7 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI | |||
| continue; | |||
| } | |||
| PubKey pubKey = participants[i].getPubKey(); | |||
| LedgerInitPermission permission = (LedgerInitPermission) results[i].getResult(); | |||
| LedgerInitPermission permission = (LedgerInitPermission) results[i].getValue(); | |||
| if (permission.getParticipantId() != participants[i].getId()) { | |||
| prompter.error("\r\nThe id of received permission isn't equal to it's participant ! --[Id=%s][name=%s]", | |||
| participants[i].getAddress(), participants[i].getName()); | |||
| @@ -537,7 +542,7 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI | |||
| public void run() { | |||
| try { | |||
| LedgerInitPermission permission = initConsensus.requestPermission(currentId, reqAuthSign); | |||
| result.setResult(permission); | |||
| result.setValue(permission); | |||
| } catch (Exception e) { | |||
| result.setError(e); | |||
| } finally { | |||
| @@ -607,7 +612,7 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI | |||
| while (!allDecided) { | |||
| allDecided = true; | |||
| for (int i = 0; i < randDecHdls.length; i++) { | |||
| if (randDecHdls[i].getResult() != null) { | |||
| if (randDecHdls[i].getValue() != null) { | |||
| // 忽略当前参与方自己(在初始化“决定”时已经置为非空),以及已经收到主动提交“决定”的参与方; | |||
| continue; | |||
| } | |||
| @@ -648,10 +653,11 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI | |||
| try { | |||
| targetDecision = initConsensus.synchronizeDecision(localDecision); | |||
| } catch (Exception e1) { | |||
| prompter.info("Error occurred on synchronizing decision . --%s", e1.getMessage()); | |||
| prompter.info("Error occurred on synchronizing decision . --[%s] %s", e1.getClass().getName(), | |||
| e1.getMessage()); | |||
| } | |||
| if (targetDecision == null) { | |||
| if (resultHandle.getResult() != null) { | |||
| if (resultHandle.getValue() != null) { | |||
| // 已经验证过; | |||
| return true; | |||
| } | |||
| @@ -678,7 +684,7 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI | |||
| } | |||
| } while (targetDecision == null); | |||
| if (targetDecision.getParticipantId() != targetId && resultHandle.getResult() == null) { | |||
| if (targetDecision.getParticipantId() != targetId && resultHandle.getValue() == null) { | |||
| prompter.error( | |||
| "The received id of participant isn't equal to id of request target participant! --[Id=%s]", | |||
| targetId); | |||
| @@ -696,7 +702,7 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI | |||
| /** | |||
| * 校验并记录指定的参与方做出的决定; | |||
| * <p> | |||
| * 注:对 {@link DecisionResultHandle#setResult(LedgerInitDecision)} | |||
| * 注:对 {@link DecisionResultHandle#setValue(LedgerInitDecision)} | |||
| * 方法的调用不是线程安全的,但由于是在满足有效性校验之后才调用,具有幂等性,所以不必对该方法的多处调用进行同步; | |||
| * | |||
| * @param targetDecision | |||
| @@ -707,7 +713,7 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI | |||
| private synchronized boolean validateAndRecordDecision(LedgerInitDecision targetDecision, | |||
| DecisionResultHandle resultHandle) { | |||
| if ((!localDecision.getLedgerHash().equals(targetDecision.getLedgerHash())) | |||
| && resultHandle.getResult() == null) { | |||
| && resultHandle.getValue() == null) { | |||
| // 如果结果已经被 | |||
| prompter.error( | |||
| "The received ledger hash of participant isn't equal to ledger hash of current participant! --[Id=%s]", | |||
| @@ -720,13 +726,13 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI | |||
| .getPubKey(); | |||
| byte[] deciBytes = getDecisionBytes(targetDecision.getParticipantId(), targetDecision.getLedgerHash()); | |||
| if ((!SIGN_FUNC.verify(targetDecision.getSignature(), targetPubKey, deciBytes)) | |||
| && resultHandle.getResult() == null) { | |||
| && resultHandle.getValue() == null) { | |||
| prompter.error("The signature of received decision is invalid! --[Id=%s]", | |||
| targetDecision.getParticipantId()); | |||
| return false; | |||
| } | |||
| resultHandle.setResult(targetDecision); | |||
| resultHandle.setValue(targetDecision); | |||
| return true; | |||
| } | |||
| @@ -735,20 +741,38 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI | |||
| public LedgerInitDecision synchronizeDecision(@RequestBody LedgerInitDecision initDecision) { | |||
| int remoteId = initDecision.getParticipantId(); | |||
| if (remoteId == currentId) { | |||
| prompter.error("Reject decision because of self-synchronization! --[Id=%s]", remoteId); | |||
| throw new LedgerInitException( | |||
| String.format("Reject decision because of self-synchronization! --[Id=%s]", remoteId)); | |||
| } | |||
| if (this.genesisBlock == null) { | |||
| // 当前参与者尚未准备就绪,返回 null; | |||
| prompter.info("Not ready for genesis block! --[RemoteId=%s][CurrentId=%s]", remoteId, currentId); | |||
| return null; | |||
| } | |||
| DecisionResultHandle resultHandle = this.decisions[remoteId]; | |||
| if (!validateAndRecordDecision(initDecision, resultHandle)) { | |||
| // 签名无效; | |||
| prompter.info("Received request of synchronizing decision! --[RemoteId=%s][CurrentId=%s]", remoteId, currentId); | |||
| try { | |||
| DecisionResultHandle resultHandle = this.decisions[remoteId]; | |||
| if (!validateAndRecordDecision(initDecision, resultHandle)) { | |||
| // 签名无效; | |||
| prompter.error("Reject decision because of invalid signature! --[RemoteId=%s][CurrentId=%s]", remoteId, | |||
| currentId); | |||
| throw new LedgerInitException( | |||
| String.format("Reject decision because of invalid signature! --[RemoteId=%s][CurrentId=%s]", | |||
| remoteId, currentId)); | |||
| } | |||
| return localDecision; | |||
| } catch (Exception e) { | |||
| prompter.error(e, | |||
| "Error occurred while receiving the request of synchronizing decision! --[RemoteId=%s][CurrentId=%s] %s", | |||
| remoteId, currentId, e.getMessage()); | |||
| throw new LedgerInitException( | |||
| String.format("Reject decision because of invalid signature! --[Id=%s]", remoteId)); | |||
| "Error occurred while receiving the request of synchronizing decision! --" + e.getMessage(), e); | |||
| } | |||
| return localDecision; | |||
| } | |||
| /** | |||
| @@ -37,9 +37,10 @@ public abstract class PropertiesUtils { | |||
| /** | |||
| * 创建配置对象的实例,并且从指定的属性表中初始化对应的实例字段; | |||
| * | |||
| * @param configClass configClass | |||
| * @param properties properties | |||
| * @param <T> T | |||
| * @param properties properties | |||
| * @param <T> T | |||
| * @return T | |||
| */ | |||
| @SuppressWarnings("unchecked") | |||
| @@ -56,13 +57,10 @@ public abstract class PropertiesUtils { | |||
| /** | |||
| * 创建配置对象的实例,并且从指定的属性表中初始化对应的实例字段; | |||
| * | |||
| * @param configClass | |||
| * 配置对象的类型; | |||
| * @param properties | |||
| * 属性表; | |||
| * @param propsPrefix | |||
| * 在属性表中与配置对象相关的属性的key的前缀; | |||
| * @param <T> T | |||
| * @param configClass 配置对象的类型; | |||
| * @param properties 属性表; | |||
| * @param propsPrefix 在属性表中与配置对象相关的属性的key的前缀; | |||
| * @param <T> T | |||
| * @return T | |||
| */ | |||
| public static <T> T createInstance(Class<T> configClass, Properties properties, String propsPrefix) { | |||
| @@ -77,12 +75,9 @@ public abstract class PropertiesUtils { | |||
| /** | |||
| * 设置配置值; | |||
| * | |||
| * @param obj | |||
| * 配置对象;配置值将设置到此对象匹配的属性; | |||
| * @param configValues | |||
| * 配置值; | |||
| * @param propPrefix | |||
| * 自动加入的属性前缀; | |||
| * @param obj 配置对象;配置值将设置到此对象匹配的属性; | |||
| * @param configValues 配置值; | |||
| * @param propPrefix 自动加入的属性前缀; | |||
| */ | |||
| public static void setValues(Object obj, Properties configValues, String propPrefix) { | |||
| Properties values = new Properties(); | |||
| @@ -93,10 +88,8 @@ public abstract class PropertiesUtils { | |||
| /** | |||
| * 设置配置值; | |||
| * | |||
| * @param obj | |||
| * 配置对象;配置值将设置到此对象匹配的属性; | |||
| * @param configValues | |||
| * 配置值; | |||
| * @param obj 配置对象;配置值将设置到此对象匹配的属性; | |||
| * @param configValues 配置值; | |||
| */ | |||
| public static void setValues(Object obj, Properties configValues) { | |||
| BeanWrapper confBean = new BeanWrapperImpl(obj); | |||
| @@ -109,13 +102,10 @@ public abstract class PropertiesUtils { | |||
| /** | |||
| * 从指定的路径加载配置; | |||
| * | |||
| * @param configClass | |||
| * 配置对象的类型; | |||
| * @param configFilePathPattern | |||
| * properties配置文件的路径;可以指定 spring 资源路径表达式; | |||
| * @param charset | |||
| * 字符集; | |||
| * @param <T> class | |||
| * @param configClass 配置对象的类型; | |||
| * @param configFilePathPattern properties配置文件的路径;可以指定 spring 资源路径表达式; | |||
| * @param charset 字符集; | |||
| * @param <T> class | |||
| * @return T | |||
| * @throws IOException exception | |||
| */ | |||
| @@ -127,12 +117,9 @@ public abstract class PropertiesUtils { | |||
| /** | |||
| * 从指定的路径加载配置; | |||
| * | |||
| * @param obj | |||
| * 配置对象;配置文件的值将设置到此对象匹配的属性; | |||
| * @param configFilePathPattern | |||
| * properties配置文件的路径;可以指定 spring 资源路径表达式; | |||
| * @param charset | |||
| * 字符集; | |||
| * @param obj 配置对象;配置文件的值将设置到此对象匹配的属性; | |||
| * @param configFilePathPattern properties配置文件的路径;可以指定 spring 资源路径表达式; | |||
| * @param charset 字符集; | |||
| * @throws IOException exception | |||
| */ | |||
| public static void load(Object obj, String configFilePathPattern, String charset) throws IOException { | |||
| @@ -180,10 +167,8 @@ public abstract class PropertiesUtils { | |||
| /** | |||
| * 合并两个 properties ; | |||
| * | |||
| * @param props | |||
| * 要将其它值合并进来的属性集合;操作将对其产生修改; | |||
| * @param from | |||
| * 属性值将要合并进入其它属性集合;操作不对其产生修改; | |||
| * @param props 要将其它值合并进来的属性集合;操作将对其产生修改; | |||
| * @param from 属性值将要合并进入其它属性集合;操作不对其产生修改; | |||
| */ | |||
| public static void mergeFrom(Properties props, Properties from) { | |||
| mergeFrom(props, from, null); | |||
| @@ -192,12 +177,9 @@ public abstract class PropertiesUtils { | |||
| /** | |||
| * 合并两个 properties ; | |||
| * | |||
| * @param props | |||
| * 要将其它值合并进来的属性集合;操作将对其产生修改; | |||
| * @param from | |||
| * 属性值将要合并进入其它属性集合;操作不对其产生修改; | |||
| * @param propertyNamePrefix | |||
| * 属性名称前缀; | |||
| * @param props 要将其它值合并进来的属性集合;操作将对其产生修改; | |||
| * @param from 属性值将要合并进入其它属性集合;操作不对其产生修改; | |||
| * @param propertyNamePrefix 属性名称前缀; | |||
| */ | |||
| public static void mergeFrom(Properties props, Properties from, String propertyNamePrefix) { | |||
| if (propertyNamePrefix == null || propertyNamePrefix.length() == 0) { | |||
| @@ -214,12 +196,9 @@ public abstract class PropertiesUtils { | |||
| /** | |||
| * 获取指定 properties 中以指定的前缀开头的子集; | |||
| * | |||
| * @param props | |||
| * 要抽取的属性集合; | |||
| * @param propertyNamePrefix | |||
| * 属性名称前缀; | |||
| * @param trimPrefix | |||
| * 是否在复制的新的属性集合去掉指定的前缀; | |||
| * @param props 要抽取的属性集合; | |||
| * @param propertyNamePrefix 属性名称前缀; | |||
| * @param trimPrefix 是否在复制的新的属性集合去掉指定的前缀; | |||
| * @return properties | |||
| */ | |||
| public static Properties subset(Properties props, String propertyNamePrefix, boolean trimPrefix) { | |||
| @@ -278,13 +257,22 @@ public abstract class PropertiesUtils { | |||
| * 如果不存在,或者返回值为空(null 或 空白字符),则抛出 {@link IllegalArgumentException} 异常; | |||
| * | |||
| * @param props props | |||
| * @param key key | |||
| * @param key key | |||
| * @return String | |||
| */ | |||
| public static String getRequiredProperty(Properties props, String key) { | |||
| return getProperty(props, key, true); | |||
| } | |||
| /** | |||
| * 返回指定的属性; <br> | |||
| * | |||
| * @param props 属性表; | |||
| * @param key 要查找的 key; | |||
| * @param required 值为 false 时,如果不存在则返回 null;值为 true 时,如果不存在,或者返回值为空(null 或 | |||
| * 空白字符),则抛出 {@link IllegalArgumentException} 异常; | |||
| * @return | |||
| */ | |||
| public static String getProperty(Properties props, String key, boolean required) { | |||
| String value = props.getProperty(key); | |||
| if (value == null) { | |||
| @@ -319,7 +307,7 @@ public abstract class PropertiesUtils { | |||
| setValues(props, propValues); | |||
| return props; | |||
| } | |||
| public static Properties setValues(Properties props, Property[] propValues) { | |||
| for (Property p : propValues) { | |||
| props.setProperty(p.getName(), p.getValue()); | |||
| @@ -2,16 +2,16 @@ package com.jd.blockchain.utils.concurrent; | |||
| public class InvocationResult<T> { | |||
| private volatile T result; | |||
| private volatile T value; | |||
| private volatile Exception error; | |||
| public T getResult() { | |||
| return result; | |||
| public T getValue() { | |||
| return value; | |||
| } | |||
| public void setResult(T result) { | |||
| this.result = result; | |||
| public void setValue(T value) { | |||
| this.value = value; | |||
| } | |||
| public Exception getError() { | |||
| @@ -1,9 +1,23 @@ | |||
| package com.jd.blockchain.utils.io; | |||
| import java.io.*; | |||
| import java.io.BufferedOutputStream; | |||
| import java.io.BufferedReader; | |||
| import java.io.BufferedWriter; | |||
| import java.io.File; | |||
| import java.io.FileInputStream; | |||
| import java.io.FileNotFoundException; | |||
| import java.io.FileOutputStream; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import java.io.InputStreamReader; | |||
| import java.io.OutputStream; | |||
| import java.io.OutputStreamWriter; | |||
| import java.io.Reader; | |||
| import java.util.ArrayList; | |||
| import java.util.Properties; | |||
| import org.springframework.util.ResourceUtils; | |||
| /** | |||
| * @author haiq | |||
| * | |||
| @@ -41,7 +55,7 @@ public class FileUtils { | |||
| throw new IllegalStateException(e.getMessage(), e); | |||
| } | |||
| } | |||
| /** | |||
| * 返回父目录的路径; | |||
| * | |||
| @@ -67,9 +81,8 @@ public class FileUtils { | |||
| /** | |||
| * 读取指定文件的首行; | |||
| * | |||
| * @param file file | |||
| * @param charset | |||
| * 字符集; | |||
| * @param file file | |||
| * @param charset 字符集; | |||
| * @return 返回首行非空行;返回结果不会自动截取两头的空字符串; | |||
| * @throws IOException exception | |||
| */ | |||
| @@ -90,9 +103,8 @@ public class FileUtils { | |||
| /** | |||
| * 返回指定文件的所有行; | |||
| * | |||
| * @param file file | |||
| * @param charset | |||
| * 字符集; | |||
| * @param file file | |||
| * @param charset 字符集; | |||
| * @return 返回首行非空行;返回结果不会自动截取两头的空字符串; | |||
| */ | |||
| public static String[] readLines(File file, String charset) { | |||
| @@ -155,10 +167,8 @@ public class FileUtils { | |||
| /** | |||
| * 以默认字符集(UTF-8)将指定的文本保存到指定的文件中; | |||
| * | |||
| * @param file | |||
| * 要保存的文件; | |||
| * @param text | |||
| * 文本内容; | |||
| * @param file 要保存的文件; | |||
| * @param text 文本内容; | |||
| */ | |||
| public static void writeText(String text, File file) { | |||
| writeText(text, file, DEFAULT_CHARSET); | |||
| @@ -166,12 +176,10 @@ public class FileUtils { | |||
| /** | |||
| * 将指定的文本保存到指定的文件中; | |||
| * @param text | |||
| * 文本内容; | |||
| * @param file | |||
| * 要保存的文件; | |||
| * @param charset | |||
| * 字符集; | |||
| * | |||
| * @param text 文本内容; | |||
| * @param file 要保存的文件; | |||
| * @param charset 字符集; | |||
| */ | |||
| public static void writeText(String text, File file, String charset) { | |||
| try (FileOutputStream out = new FileOutputStream(file, false)) { | |||
| @@ -181,7 +189,7 @@ public class FileUtils { | |||
| throw new RuntimeIOException(e.getMessage(), e); | |||
| } | |||
| } | |||
| public static void writeBytes(byte[] content, File file) { | |||
| try (FileOutputStream out = new FileOutputStream(file, false)) { | |||
| out.write(content); | |||
| @@ -190,7 +198,7 @@ public class FileUtils { | |||
| throw new RuntimeIOException(e.getMessage(), e); | |||
| } | |||
| } | |||
| public static void appendBytes(byte[] content, File file) { | |||
| try (FileOutputStream out = new FileOutputStream(file, true)) { | |||
| out.write(content); | |||
| @@ -224,16 +232,23 @@ public class FileUtils { | |||
| /** | |||
| * 以默认字符集(UTF-8)从文件读取文本; | |||
| * | |||
| * @param file file | |||
| * @return String | |||
| */ | |||
| public static String readText(String file) { | |||
| return readText(new File(file), DEFAULT_CHARSET); | |||
| public static String readText(String filePath) { | |||
| try { | |||
| File file = ResourceUtils.getFile(filePath); | |||
| return readText(file, DEFAULT_CHARSET); | |||
| } catch (FileNotFoundException e) { | |||
| throw new RuntimeIOException(e.getMessage(), e); | |||
| } | |||
| } | |||
| /** | |||
| * 从文件读取文本; | |||
| * @param file file | |||
| * | |||
| * @param file file | |||
| * @param charset charset | |||
| * @return String | |||
| */ | |||
| @@ -254,7 +269,7 @@ public class FileUtils { | |||
| /** | |||
| * 从文件读取文本; | |||
| * | |||
| * @param file file | |||
| * @param file file | |||
| * @param charset charset | |||
| * @return String | |||
| */ | |||
| @@ -278,7 +293,7 @@ public class FileUtils { | |||
| /** | |||
| * 从流读取文本; | |||
| * | |||
| * @param in in | |||
| * @param in in | |||
| * @param charset charset | |||
| * @return String | |||
| * @throws IOException exception | |||
| @@ -310,7 +325,7 @@ public class FileUtils { | |||
| throw new RuntimeIOException(e.getMessage(), e); | |||
| } | |||
| } | |||
| public static byte[] readBytes(File file) { | |||
| try { | |||
| FileInputStream in = new FileInputStream(file); | |||
| @@ -324,8 +339,12 @@ public class FileUtils { | |||
| } | |||
| } | |||
| public static Properties readProperties(String systemConfig) { | |||
| return readProperties(systemConfig, DEFAULT_CHARSET); | |||
| public static Properties readProperties(String file) { | |||
| return readProperties(file, DEFAULT_CHARSET); | |||
| } | |||
| public static Properties readProperties(File file) { | |||
| return readProperties(file, DEFAULT_CHARSET); | |||
| } | |||
| public static Properties readProperties(String file, String charset) { | |||
| @@ -417,11 +436,11 @@ public class FileUtils { | |||
| throw new IllegalStateException(e.getMessage(), e); | |||
| } | |||
| } | |||
| public static void deleteFile(String dir) { | |||
| deleteFile(dir, false); | |||
| } | |||
| public static void deleteFile(File file) { | |||
| deleteFile(file, false); | |||
| } | |||
| @@ -435,8 +454,7 @@ public class FileUtils { | |||
| * 删除文件; | |||
| * | |||
| * @param file | |||
| * @param silent | |||
| * 是否静默删除;如果为 true ,则吞噬删除过程中的异常,意味着方法即便正常返回时也有可能删除不完全; | |||
| * @param silent 是否静默删除;如果为 true ,则吞噬删除过程中的异常,意味着方法即便正常返回时也有可能删除不完全; | |||
| */ | |||
| public static void deleteFile(File file, boolean silent) { | |||
| if (file.isFile()) { | |||