diff --git a/source/base/src/main/java/com/jd/blockchain/consts/DataCodes.java b/source/base/src/main/java/com/jd/blockchain/consts/DataCodes.java index e4e17a14..6b47a505 100644 --- a/source/base/src/main/java/com/jd/blockchain/consts/DataCodes.java +++ b/source/base/src/main/java/com/jd/blockchain/consts/DataCodes.java @@ -68,6 +68,8 @@ public interface DataCodes { // public static final int METADATA_PARTICIPANT_INFO = 0x640; public static final int METADATA_CRYPTO_SETTING = 0x642; + + public static final int METADATA_CRYPTO_SETTING_PROVIDER = 0x643; // public static final int ACCOUNT = 0x700; diff --git a/source/base/src/main/java/com/jd/blockchain/provider/ProviderManager.java b/source/base/src/main/java/com/jd/blockchain/provider/ProviderManager.java index 447ba3db..98584de4 100644 --- a/source/base/src/main/java/com/jd/blockchain/provider/ProviderManager.java +++ b/source/base/src/main/java/com/jd/blockchain/provider/ProviderManager.java @@ -4,6 +4,7 @@ import java.security.AccessControlContext; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; @@ -48,32 +49,45 @@ public final class ProviderManager { * @return */ public S getService(Class serviceClazz, String providerName) { - NamedProviders providers = getServiceProvider(serviceClazz); + NamedProviders providers = getNamedProviders(serviceClazz); return providers.getService(providerName); } + + public Provider getProvider(Class serviceClazz, String providerName) { + @SuppressWarnings("unchecked") + NamedProviders providers = (NamedProviders) serviceProviders.get(serviceClazz); + if (providers == null) { + return null; + } + return providers.getProvider(providerName); + } public Collection> getAllProviders(Class serviceClazz) { - NamedProviders providers = getServiceProvider(serviceClazz); + @SuppressWarnings("unchecked") + NamedProviders providers = (NamedProviders) serviceProviders.get(serviceClazz); + if (providers == null) { + return Collections.emptyList(); + } return providers.getProviders(); } public S installProvider(Class serviceClazz, String providerFullName) { - NamedProviders providers = getServiceProvider(serviceClazz); + NamedProviders providers = getNamedProviders(serviceClazz); return providers.install(providerFullName); } public S installProvider(Class service, String providerFullName, ClassLoader classLoader) { - NamedProviders providers = getServiceProvider(service); + NamedProviders providers = getNamedProviders(service); return providers.install(providerFullName, classLoader); } public void installAllProviders(Class serviceClazz, ClassLoader classLoader) { - NamedProviders providers = getServiceProvider(serviceClazz); + NamedProviders providers = getNamedProviders(serviceClazz); providers.installAll(classLoader); } @SuppressWarnings("unchecked") - private NamedProviders getServiceProvider(Class serviceClazz) { + private NamedProviders getNamedProviders(Class serviceClazz) { NamedProviders providers = (NamedProviders) serviceProviders.get(serviceClazz); if (providers == null) { synchronized (mutex) { @@ -189,6 +203,11 @@ public final class ProviderManager { public Collection> getProviders() { return namedProviders.values(); } + + + public Provider getProvider(String providerFullName) { + return namedProviders.get(providerFullName); + } public S getService(String name) { String fullName = shortNames.get(name); diff --git a/source/contract/contract-maven-plugin/src/main/java/com/jd/blockchain/ContractDeployMojo.java b/source/contract/contract-maven-plugin/src/main/java/com/jd/blockchain/ContractDeployMojo.java index 80be911f..3eac13bb 100644 --- a/source/contract/contract-maven-plugin/src/main/java/com/jd/blockchain/ContractDeployMojo.java +++ b/source/contract/contract-maven-plugin/src/main/java/com/jd/blockchain/ContractDeployMojo.java @@ -5,6 +5,7 @@ import com.jd.blockchain.crypto.PrivKey; import com.jd.blockchain.crypto.PubKey; import com.jd.blockchain.ledger.BlockchainKeypair; import com.jd.blockchain.tools.keygen.KeyGenCommand; +import com.jd.blockchain.utils.StringUtils; import com.jd.blockchain.utils.codec.Base58Utils; import com.jd.blockchain.utils.io.FileUtils; import org.apache.maven.plugin.AbstractMojo; diff --git a/source/contract/contract-maven-plugin/src/main/java/com/jd/blockchain/StringUtils.java b/source/contract/contract-maven-plugin/src/main/java/com/jd/blockchain/StringUtils.java deleted file mode 100644 index f8fe1867..00000000 --- a/source/contract/contract-maven-plugin/src/main/java/com/jd/blockchain/StringUtils.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.jd.blockchain; -/** - * @Author zhaogw - * @Date 2018/11/26 20:46 - */ -public abstract class StringUtils { - public static boolean isEmpty(Object str) { - return str == null || "".equals(str); - } -} \ No newline at end of file diff --git a/source/contract/contract-maven-plugin/src/main/resources/sys-contract.properties b/source/contract/contract-maven-plugin/src/main/resources/sys-contract.properties index 6dd66e5e..a7179906 100644 --- a/source/contract/contract-maven-plugin/src/main/resources/sys-contract.properties +++ b/source/contract/contract-maven-plugin/src/main/resources/sys-contract.properties @@ -1,9 +1,9 @@ -#项目源文件存放的位置; +#PROJECT_BASE_DIR PROJECT_BASE_DIR=E:\\gitCode\\block\\prototype\\ -#合同使用的类库存放的位置,可能不在项目中,故采用全新的地址; +#LEDGER_BASE_CLASS_PATH LEDGER_BASE_CLASS_PATH=E:\\gitCode\\block\\prototype\\libs\\ -#为了测试,临时添加的变量; +#deploy and execute the contract; cParam=com.jd.blockchain.contract.AssetContract3 sParam=E:\\gitCode\\block\\prototype\\source\\sdk\\contract-sample\\src\\main\\java\\ eParam=utf-8 diff --git a/source/contract/contract-tools/pom.xml b/source/contract/contract-tools/pom.xml deleted file mode 100644 index da60f463..00000000 --- a/source/contract/contract-tools/pom.xml +++ /dev/null @@ -1,80 +0,0 @@ - - 4.0.0 - - com.jd.blockchain - contract - 0.9.0-SNAPSHOT - - contract-tools - - - - com.jd.blockchain - contract-compiler - ${project.version} - - - com.jd.blockchain - contract-jar - ${project.version} - - - commons-io - commons-io - ${commons-io.version} - - - junit - junit - test - - - org.mockito - mockito-core - test - - - org.slf4j - slf4j-log4j12 - - - - - - - org.apache.maven.plugins - maven-surefire-plugin - 2.5 - - true - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/Crypto.java b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/Crypto.java index 86dbf55b..2bca057a 100644 --- a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/Crypto.java +++ b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/Crypto.java @@ -106,6 +106,44 @@ public final class Crypto { private Crypto() { } + public static CryptoProvider[] getProviders() { + Collection> providers = pm.getAllProviders(CryptoService.class); + CryptoProvider[] infos = new CryptoProvider[providers.size()]; + + int i = 0; + for (Provider pd : providers) { + CryptoProviderInfo info = getProviderInfo(pd); + infos[i] = info; + } + + return infos; + } + + private static CryptoProviderInfo getProviderInfo(Provider pd) { + Collection functions = pd.getService().getFunctions(); + CryptoAlgorithm[] algorithms = new CryptoAlgorithm[functions.size()]; + int i = 0; + for (CryptoFunction function : functions) { + algorithms[i] = function.getAlgorithm(); + i++; + } + return new CryptoProviderInfo(pd.getFullName(), algorithms); + } + + /** + * 杩斿洖鎸囧畾鍚嶇О鐨勫瘑鐮佹湇鍔℃彁渚涜咃紱濡傛灉涓嶅瓨鍦紝鍒欒繑鍥 null 锛 + * + * @param providerFullName + * @return + */ + public static CryptoProvider getProvider(String providerFullName) { + Provider pd = pm.getProvider(CryptoService.class, providerFullName); + if (pd == null) { + throw new CryptoException("Crypto service provider named [" + providerFullName + "] does not exist!"); + } + return getProviderInfo(pd); + } + public static Collection getAllAlgorithms() { return algorithms.values(); } diff --git a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoAlgorithm.java b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoAlgorithm.java index 4f505ba9..b501626f 100644 --- a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoAlgorithm.java +++ b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoAlgorithm.java @@ -9,7 +9,7 @@ import com.jd.blockchain.binaryproto.PrimitiveType; import com.jd.blockchain.consts.DataCodes; import com.jd.blockchain.utils.io.BytesUtils; -//@DataContract(code = DataCodes.CRYPTO_ALGORITHM) +@DataContract(code = DataCodes.CRYPTO_ALGORITHM) public interface CryptoAlgorithm { /** @@ -63,7 +63,7 @@ public interface CryptoAlgorithm { * {@link #EXT_ALGORITHM}) 5 绉嶏級; 鎺ヤ笅鏉4浣嶆爣璇嗗瘑閽ョ被鍨嬶紙鍖呮嫭锛歿@link #SYMMETRIC_KEY}, * {@link #ASYMMETRIC_KEY}锛夛紱 鏈鍚8浣嶆槸绠楁硶鍞竴ID锛 */ -// @DataField(primitiveType = PrimitiveType.INT16, order = 0) + @DataField(order = 0, primitiveType = PrimitiveType.INT16) short code(); /** @@ -75,7 +75,13 @@ public interface CryptoAlgorithm { * * @return */ + @DataField(order = 1, primitiveType = PrimitiveType.TEXT) String name(); + + + public static String getString(CryptoAlgorithm algorithm) { + return String.format("%s[%s]", algorithm.name(), (algorithm.code() & 0xFFFF)); + } /** * diff --git a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoAlgorithmDefinition.java b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoAlgorithmDefinition.java index 64b6a3e1..4e9f94b0 100644 --- a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoAlgorithmDefinition.java +++ b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoAlgorithmDefinition.java @@ -23,16 +23,14 @@ public final class CryptoAlgorithmDefinition implements CryptoAlgorithm { @Override public String toString() { - return name + "[" + (code & 0xFFFF) + "]"; + return CryptoAlgorithm.getString(this); } /** * 澹版槑涓椤瑰搱甯岀畻娉曪紱 * - * @param name - * 绠楁硶鍚嶇О锛 - * @param uid - * 绠楁硶ID锛涢渶瑕佸湪鍚岀被绠楁硶涓繚鎸佸敮涓鎬э紱 + * @param name 绠楁硶鍚嶇О锛 + * @param uid 绠楁硶ID锛涢渶瑕佸湪鍚岀被绠楁硶涓繚鎸佸敮涓鎬э紱 * @return */ public static CryptoAlgorithm defineHash(String name, byte uid) { @@ -43,10 +41,8 @@ public final class CryptoAlgorithmDefinition implements CryptoAlgorithm { /** * 澹版槑涓椤归潪瀵圭О瀵嗙爜绠楁硶锛 * - * @param name - * 绠楁硶鍚嶇О锛 - * @param uid - * 绠楁硶ID锛涢渶瑕佸湪鍚岀被绠楁硶涓繚鎸佸敮涓鎬э紱 + * @param name 绠楁硶鍚嶇О锛 + * @param uid 绠楁硶ID锛涢渶瑕佸湪鍚岀被绠楁硶涓繚鎸佸敮涓鎬э紱 * @return */ public static CryptoAlgorithm defineSignature(String name, boolean encryptable, byte uid) { @@ -62,10 +58,8 @@ public final class CryptoAlgorithmDefinition implements CryptoAlgorithm { /** * 澹版槑涓椤归潪瀵圭О鍔犲瘑绠楁硶锛 * - * @param name - * 绠楁硶鍚嶇О锛 - * @param uid - * 绠楁硶ID锛涢渶瑕佸湪鍚岀被绠楁硶涓繚鎸佸敮涓鎬э紱 + * @param name 绠楁硶鍚嶇О锛 + * @param uid 绠楁硶ID锛涢渶瑕佸湪鍚岀被绠楁硶涓繚鎸佸敮涓鎬э紱 * @return */ public static CryptoAlgorithm defineAsymmetricEncryption(String name, byte uid) { @@ -76,10 +70,8 @@ public final class CryptoAlgorithmDefinition implements CryptoAlgorithm { /** * 澹版槑涓椤瑰绉板瘑鐮佺畻娉曪紱 * - * @param name - * 绠楁硶鍚嶇О锛 - * @param uid - * 绠楁硶ID锛涢渶瑕佸湪鍚岀被绠楁硶涓繚鎸佸敮涓鎬э紱 + * @param name 绠楁硶鍚嶇О锛 + * @param uid 绠楁硶ID锛涢渶瑕佸湪鍚岀被绠楁硶涓繚鎸佸敮涓鎬э紱 * @return */ public static CryptoAlgorithm defineSymmetricEncryption(String name, byte uid) { @@ -90,10 +82,8 @@ public final class CryptoAlgorithmDefinition implements CryptoAlgorithm { /** * 澹版槑涓椤归殢鏈烘暟绠楁硶锛 * - * @param name - * 绠楁硶鍚嶇О锛 - * @param uid - * 绠楁硶ID锛涢渶瑕佸湪鍚岀被绠楁硶涓繚鎸佸敮涓鎬э紱 + * @param name 绠楁硶鍚嶇О锛 + * @param uid 绠楁硶ID锛涢渶瑕佸湪鍚岀被绠楁硶涓繚鎸佸敮涓鎬э紱 * @return */ public static CryptoAlgorithm defineRandom(String name, byte uid) { @@ -104,10 +94,8 @@ public final class CryptoAlgorithmDefinition implements CryptoAlgorithm { /** * 澹版槑涓椤规墿灞曠殑瀵嗙爜绠楁硶锛 * - * @param name - * 绠楁硶鍚嶇О锛 - * @param uid - * 绠楁硶ID锛涢渶瑕佸湪鍚岀被绠楁硶涓繚鎸佸敮涓鎬э紱 + * @param name 绠楁硶鍚嶇О锛 + * @param uid 绠楁硶ID锛涢渶瑕佸湪鍚岀被绠楁硶涓繚鎸佸敮涓鎬э紱 * @return */ public static CryptoAlgorithm definExt(String name, byte uid) { diff --git a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoProvider.java b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoProvider.java new file mode 100644 index 00000000..57b06831 --- /dev/null +++ b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoProvider.java @@ -0,0 +1,17 @@ +package com.jd.blockchain.crypto; + +import com.jd.blockchain.binaryproto.DataContract; +import com.jd.blockchain.binaryproto.DataField; +import com.jd.blockchain.binaryproto.PrimitiveType; +import com.jd.blockchain.consts.DataCodes; + +@DataContract(code = DataCodes.METADATA_CRYPTO_SETTING_PROVIDER) +public interface CryptoProvider { + + @DataField(order = 0, primitiveType = PrimitiveType.TEXT) + String getName(); + + @DataField(order = 1, list = true, refContract = true) + CryptoAlgorithm[] getAlgorithms(); + +} diff --git a/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoProviderInfo.java b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoProviderInfo.java new file mode 100644 index 00000000..b0b17231 --- /dev/null +++ b/source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoProviderInfo.java @@ -0,0 +1,24 @@ +package com.jd.blockchain.crypto; + +class CryptoProviderInfo implements CryptoProvider { + + private String name; + + private CryptoAlgorithm[] algorithms; + + public CryptoProviderInfo(String name, CryptoAlgorithm[] algorithms) { + this.name = name; + this.algorithms = algorithms; + } + + @Override + public String getName() { + return name; + } + + @Override + public CryptoAlgorithm[] getAlgorithms() { + return algorithms.clone(); + } + +} diff --git a/source/deployment/deployment-peer/src/main/resources/config/init/ledger-init.conf b/source/deployment/deployment-peer/src/main/resources/config/init/ledger-init.conf deleted file mode 100644 index e6af54f4..00000000 --- a/source/deployment/deployment-peer/src/main/resources/config/init/ledger-init.conf +++ /dev/null @@ -1,67 +0,0 @@ -#璐︽湰鐨勭瀛愶紱涓娈16杩涘埗瀛楃锛屾渶闀垮彲浠ュ寘鍚64涓瓧绗︼紱鍙互鐢ㄥ瓧绗︹-鈥濆垎闅旓紝浠ヤ究鏇村鏄撹鍙栵紱 -#涓嶅悓鐨勮处鏈紝绉嶅瓙涓嶈兘鐩稿悓 -ledger.seed=932dfe23-fe23232f-283f32fa-dd32aa76-8322ca2f-56236cda-7136b322-cb323ffe - -#璐︽湰鐨勬弿杩板悕绉帮紱姝ゅ睘鎬т笉鍙備笌鍏辫瘑锛屼粎浠呭湪褰撳墠鍙備笌鏂圭殑鏈湴鑺傜偣鐢ㄤ簬鎻忚堪鐢ㄩ旓紱 -#ledger.name= - -#鍙備笌鏂圭殑涓暟锛屽悗缁互cons_parti.id鍒嗗埆鏍囪瘑姣忎竴涓弬涓庢柟鐨勯厤缃紱 -cons_parti.count=4 - - -#绗0涓弬涓庢柟鐨勫悕绉帮紙涓嶅悓鍙備笌鏂瑰悕绉颁笉鑳界浉鍚岋級 -cons_parti.0.name= -#绗0涓弬涓庢柟鐨勫叕閽ユ枃浠惰矾寰 -cons_parti.0.pubkey-path= -#绗0涓弬涓庢柟鐨勫叕閽ュ唴瀹癸紙鐢眐eygen宸ュ叿鐢熸垚锛夛紝姝ゅ弬鏁颁紭鍏堜簬 pubkey-path 鍙傛暟 -cons_parti.0.pubkey= -#绗0涓弬涓庢柟鐨勮处鏈垵濮嬫湇鍔$殑涓绘満 -cons_parti.0.initializer.host=127.0.0.1 -#绗0涓弬涓庢柟鐨勮处鏈垵濮嬫湇鍔$殑绔彛 -cons_parti.0.initializer.port=17000 -#绗0涓弬涓庢柟鐨勮处鏈垵濮嬫湇鍔℃槸鍚﹀紑鍚畨鍏ㄨ繛鎺 -cons_parti.0.initializer.secure=false - - -#绗1涓弬涓庢柟鐨勫悕绉 -cons_parti.1.name= -#绗1涓弬涓庢柟鐨勫叕閽ユ枃浠惰矾寰 -cons_parti.1.pubkey-path= -#绗1涓弬涓庢柟鐨勫叕閽ュ唴瀹癸紙鐢眐eygen宸ュ叿鐢熸垚锛夛紝姝ゅ弬鏁颁紭鍏堜簬 pubkey-path 鍙傛暟 -cons_parti.1.pubkey= -#绗1涓弬涓庢柟鐨勮处鏈垵濮嬫湇鍔$殑涓绘満 -cons_parti.1.initializer.host=127.0.0.1 -#绗1涓弬涓庢柟鐨勮处鏈垵濮嬫湇鍔$殑绔彛 -cons_parti.1.initializer.port=17010 -#绗1涓弬涓庢柟鐨勮处鏈垵濮嬫湇鍔℃槸鍚﹀紑鍚畨鍏ㄨ繛鎺 -cons_parti.1.initializer.secure=false - - -#绗2涓弬涓庢柟鐨勫悕绉 -cons_parti.2.name= -#绗2涓弬涓庢柟鐨勫叕閽ユ枃浠惰矾寰 -cons_parti.2.pubkey-path= -#绗2涓弬涓庢柟鐨勫叕閽ュ唴瀹癸紙鐢眐eygen宸ュ叿鐢熸垚锛夛紝姝ゅ弬鏁颁紭鍏堜簬 pubkey-path 鍙傛暟 -cons_parti.2.pubkey= -#绗2涓弬涓庢柟鐨勮处鏈垵濮嬫湇鍔$殑涓绘満 -cons_parti.2.initializer.host=127.0.0.1 -#绗2涓弬涓庢柟鐨勮处鏈垵濮嬫湇鍔$殑绔彛 -cons_parti.2.initializer.port=17020 -#绗2涓弬涓庢柟鐨勮处鏈垵濮嬫湇鍔℃槸鍚﹀紑鍚畨鍏ㄨ繛鎺 -cons_parti.2.initializer.secure=false - - -#绗3涓弬涓庢柟鐨勫悕绉 -cons_parti.3.name= -#绗3涓弬涓庢柟鐨勫叕閽ユ枃浠惰矾寰 -cons_parti.3.pubkey-path= -#绗3涓弬涓庢柟鐨勫叕閽ュ唴瀹癸紙鐢眐eygen宸ュ叿鐢熸垚锛夛紝姝ゅ弬鏁颁紭鍏堜簬 pubkey-path 鍙傛暟 -cons_parti.3.pubkey= -#绗3涓弬涓庢柟鐨勮处鏈垵濮嬫湇鍔$殑涓绘満 -cons_parti.3.initializer.host=127.0.0.1 -#绗3涓弬涓庢柟鐨勮处鏈垵濮嬫湇鍔$殑绔彛 -cons_parti.3.initializer.port=17030 -#绗3涓弬涓庢柟鐨勮处鏈垵濮嬫湇鍔℃槸鍚﹀紑鍚畨鍏ㄨ繛鎺 -cons_parti.3.initializer.secure=false - - diff --git a/source/deployment/deployment-peer/src/main/resources/config/init/ledger.init b/source/deployment/deployment-peer/src/main/resources/config/init/ledger.init new file mode 100644 index 00000000..9df38d79 --- /dev/null +++ b/source/deployment/deployment-peer/src/main/resources/config/init/ledger.init @@ -0,0 +1,97 @@ + +#璐︽湰鐨勭瀛愶紱涓娈16杩涘埗瀛楃锛屾渶闀垮彲浠ュ寘鍚64涓瓧绗︼紱鍙互鐢ㄥ瓧绗︹-鈥濆垎闅旓紝浠ヤ究鏇村鏄撹鍙栵紱 +ledger.seed=932dfe23-fe23232f-283f32fa-dd32aa76-8322ca2f-56236cda-7136b322-cb323ffe + +#璐︽湰鐨勬弿杩板悕绉帮紱姝ゅ睘鎬т笉鍙備笌鍏辫瘑锛屼粎浠呭湪褰撳墠鍙備笌鏂圭殑鏈湴鑺傜偣鐢ㄤ簬鎻忚堪鐢ㄩ旓紱 +ledger.name= + + +#鍏辫瘑鏈嶅姟鎻愪緵鑰咃紱蹇呴』锛 +consensus.service-provider=com.jd.blockchain.consensus.bftsmart.BftsmartConsensusProvider + +#鍏辫瘑鏈嶅姟鐨勫弬鏁伴厤缃紱蹇呴』锛 +consensus.conf=classpath:bftsmart.config + +#瀵嗙爜鏈嶅姟鎻愪緵鑰呭垪琛紝浠ヨ嫳鏂囬楃偣鈥,鈥濆垎闅旓紱蹇呴』锛 +crypto.service-providers=com.jd.blockchain.crypto.service.classic.ClassicCryptoService, \ +com.jd.blockchain.crypto.service.sm.SMCryptoService + + +#鍙備笌鏂圭殑涓暟锛屽悗缁互 cons_parti.id 鍒嗗埆鏍囪瘑姣忎竴涓弬涓庢柟鐨勯厤缃紱 +cons_parti.count=4 + +#绗0涓弬涓庢柟鐨勫悕绉帮紱 +cons_parti.0.name=jd.com +#绗0涓弬涓庢柟鐨勫叕閽ユ枃浠惰矾寰勶紱 +cons_parti.0.pubkey-path=keys/jd-com.pub +#绗0涓弬涓庢柟鐨勫叕閽ュ唴瀹癸紙鐢眐eygen宸ュ叿鐢熸垚锛夛紱姝ゅ弬鏁颁紭鍏堜簬 pubkey-path 鍙傛暟锛 +cons_parti.0.pubkey=3snPdw7i7PjVKiTH2VnXZu5H8QmNaSXpnk4ei533jFpuifyjS5zzH9 +#绗0涓弬涓庢柟鐨勫叡璇嗘湇鍔$殑涓绘満鍦板潃锛 +cons_parti.0.consensus.host=127.0.0.1 +#绗0涓弬涓庢柟鐨勫叡璇嗘湇鍔$殑绔彛锛 +cons_parti.0.consensus.port=8900 +#绗0涓弬涓庢柟鐨勫叡璇嗘湇鍔℃槸鍚﹀紑鍚畨鍏ㄨ繛鎺ワ紱 +cons_parti.0.consensus.secure=true +#绗0涓弬涓庢柟鐨勮处鏈垵濮嬫湇鍔$殑涓绘満锛 +cons_parti.0.initializer.host=127.0.0.1 +#绗0涓弬涓庢柟鐨勮处鏈垵濮嬫湇鍔$殑绔彛锛 +cons_parti.0.initializer.port=8800 +#绗0涓弬涓庢柟鐨勮处鏈垵濮嬫湇鍔℃槸鍚﹀紑鍚畨鍏ㄨ繛鎺ワ紱 +cons_parti.0.initializer.secure=true + +#绗1涓弬涓庢柟鐨勫悕绉帮紱 +cons_parti.1.name=at.com +#绗1涓弬涓庢柟鐨勫叕閽ユ枃浠惰矾寰勶紱 +cons_parti.1.pubkey-path=keys/at-com.pub +#绗1涓弬涓庢柟鐨勫叕閽ュ唴瀹癸紙鐢眐eygen宸ュ叿鐢熸垚锛夛紱姝ゅ弬鏁颁紭鍏堜簬 pubkey-path 鍙傛暟锛 +cons_parti.1.pubkey=3snPdw7i7PajLB35tEau1kmixc6ZrjLXgxwKbkv5bHhP7nT5dhD9eX +#绗1涓弬涓庢柟鐨勫叡璇嗘湇鍔$殑涓绘満鍦板潃锛 +cons_parti.1.consensus.host=127.0.0.1 +#绗1涓弬涓庢柟鐨勫叡璇嗘湇鍔$殑绔彛锛 +cons_parti.1.consensus.port=8910 +#绗1涓弬涓庢柟鐨勫叡璇嗘湇鍔℃槸鍚﹀紑鍚畨鍏ㄨ繛鎺ワ紱 +cons_parti.1.consensus.secure=false +#绗1涓弬涓庢柟鐨勮处鏈垵濮嬫湇鍔$殑涓绘満锛 +cons_parti.1.initializer.host=127.0.0.1 +#绗1涓弬涓庢柟鐨勮处鏈垵濮嬫湇鍔$殑绔彛锛 +cons_parti.1.initializer.port=8810 +#绗1涓弬涓庢柟鐨勮处鏈垵濮嬫湇鍔℃槸鍚﹀紑鍚畨鍏ㄨ繛鎺ワ紱 +cons_parti.1.initializer.secure=false + +#绗2涓弬涓庢柟鐨勫悕绉帮紱 +cons_parti.2.name=bt.com +#绗2涓弬涓庢柟鐨勫叕閽ユ枃浠惰矾寰勶紱 +cons_parti.2.pubkey-path=classpath:keys/parti2.pub +#绗2涓弬涓庢柟鐨勫叕閽ュ唴瀹癸紙鐢眐eygen宸ュ叿鐢熸垚锛夛紱姝ゅ弬鏁颁紭鍏堜簬 pubkey-path 鍙傛暟锛 +cons_parti.2.pubkey= +#绗2涓弬涓庢柟鐨勫叡璇嗘湇鍔$殑涓绘満鍦板潃锛 +cons_parti.2.consensus.host=127.0.0.1 +#绗2涓弬涓庢柟鐨勫叡璇嗘湇鍔$殑绔彛锛 +cons_parti.2.consensus.port=8920 +#绗2涓弬涓庢柟鐨勫叡璇嗘湇鍔℃槸鍚﹀紑鍚畨鍏ㄨ繛鎺ワ紱 +cons_parti.2.consensus.secure=false +#绗2涓弬涓庢柟鐨勮处鏈垵濮嬫湇鍔$殑涓绘満锛 +cons_parti.2.initializer.host=127.0.0.1 +#绗2涓弬涓庢柟鐨勮处鏈垵濮嬫湇鍔$殑绔彛锛 +cons_parti.2.initializer.port=8820 +#绗2涓弬涓庢柟鐨勮处鏈垵濮嬫湇鍔℃槸鍚﹀紑鍚畨鍏ㄨ繛鎺ワ紱 +cons_parti.2.initializer.secure=true + +#绗3涓弬涓庢柟鐨勫悕绉帮紱 +cons_parti.3.name=xt.com +#绗3涓弬涓庢柟鐨勫叕閽ユ枃浠惰矾寰勶紱 +cons_parti.3.pubkey-path=keys/xt-com.pub +#绗3涓弬涓庢柟鐨勫叕閽ュ唴瀹癸紙鐢眐eygen宸ュ叿鐢熸垚锛夛紱姝ゅ弬鏁颁紭鍏堜簬 pubkey-path 鍙傛暟锛 +cons_parti.3.pubkey=3snPdw7i7PifPuRX7fu3jBjsb3rJRfDe9GtbDfvFJaJ4V4hHXQfhwk +#绗3涓弬涓庢柟鐨勫叡璇嗘湇鍔$殑涓绘満鍦板潃锛 +cons_parti.3.consensus.host=127.0.0.1 +#绗3涓弬涓庢柟鐨勫叡璇嗘湇鍔$殑绔彛锛 +cons_parti.3.consensus.port=8930 +#绗3涓弬涓庢柟鐨勫叡璇嗘湇鍔℃槸鍚﹀紑鍚畨鍏ㄨ繛鎺ワ紱 +cons_parti.3.consensus.secure=false +#绗3涓弬涓庢柟鐨勮处鏈垵濮嬫湇鍔$殑涓绘満锛 +cons_parti.3.initializer.host=127.0.0.1 +#绗3涓弬涓庢柟鐨勮处鏈垵濮嬫湇鍔$殑绔彛锛 +cons_parti.3.initializer.port=8830 +#绗3涓弬涓庢柟鐨勮处鏈垵濮嬫湇鍔℃槸鍚﹀紑鍚畨鍏ㄨ繛鎺ワ紱 +cons_parti.3.initializer.secure=false diff --git a/source/deployment/deployment-peer/src/main/resources/config/init/local.conf b/source/deployment/deployment-peer/src/main/resources/config/init/local.conf index b182823e..27652184 100644 --- a/source/deployment/deployment-peer/src/main/resources/config/init/local.conf +++ b/source/deployment/deployment-peer/src/main/resources/config/init/local.conf @@ -22,11 +22,3 @@ ledger.db.uri= #璐︽湰鏁版嵁搴撶殑杩炴帴鍙d护 ledger.db.pwd= -#鍏辫瘑閰嶇疆鏂囦欢璺緞 -#鎺ㄨ崘浣跨敤缁濆璺緞锛岀浉瀵硅矾寰勪互褰撳墠鏂囦欢(local.conf锛夋墍鍦ㄧ洰褰曚负鍩哄噯 -consensus.conf=bftsmart.config - -#鍏辫瘑Providers閰嶇疆 -#BftSmart鍏辫瘑Provider锛歝om.jd.blockchain.consensus.bftsmart.BftsmartConsensusProvider -#绠鍗曟秷鎭叡璇哖rovider锛歝om.jd.blockchain.consensus.mq.MsgQueueConsensusProvider -consensus.service-provider=com.jd.blockchain.consensus.bftsmart.BftsmartConsensusProvider diff --git a/source/gateway/src/main/java/com/jd/blockchain/gateway/service/GatewayQueryServiceHandler.java b/source/gateway/src/main/java/com/jd/blockchain/gateway/service/GatewayQueryServiceHandler.java index 71e34a7c..1a6c0267 100644 --- a/source/gateway/src/main/java/com/jd/blockchain/gateway/service/GatewayQueryServiceHandler.java +++ b/source/gateway/src/main/java/com/jd/blockchain/gateway/service/GatewayQueryServiceHandler.java @@ -81,7 +81,7 @@ public class GatewayQueryServiceHandler implements GatewayQueryService { ledgerInitSettings.setSeed(initSeed(ledgerMetadata.getSeed())); // 璁剧疆鍏辫瘑鍗忚 - ledgerInitSettings.setConsensusProtocol(consensusProtocol(ledgerMetadata.getSetting().getConsensusProvider())); + ledgerInitSettings.setConsensusProtocol(ledgerMetadata.getSetting().getConsensusProvider()); return ledgerInitSettings; } @@ -110,24 +110,6 @@ public class GatewayQueryServiceHandler implements GatewayQueryService { return seed.toString(); } - /** - * 鐢熸垚鍏辫瘑鍗忚 - * - * @param consensusProvider - * 鍏辫瘑鍗忚鎻愭彁渚涜 - * @return - */ - private int consensusProtocol(String consensusProvider) { - - if (consensusProvider.equals(BftsmartConsensusProvider.NAME)) { - return LedgerInitSettings.CONSENSUS_PROTOCOL.BFTSMART.code(); - } else if (consensusProvider.equals(MsgQueueConsensusProvider.NAME)) { - return LedgerInitSettings.CONSENSUS_PROTOCOL.MSGQUEUE.code(); - } - - return LedgerInitSettings.CONSENSUS_PROTOCOL.UNKNOWN.code(); - } - /** * 鍒濆鍖栧叡璇嗛厤缃 * diff --git a/source/gateway/src/main/java/com/jd/blockchain/gateway/web/BlockBrowserController.java b/source/gateway/src/main/java/com/jd/blockchain/gateway/web/BlockBrowserController.java index 381cc50f..66f94b97 100644 --- a/source/gateway/src/main/java/com/jd/blockchain/gateway/web/BlockBrowserController.java +++ b/source/gateway/src/main/java/com/jd/blockchain/gateway/web/BlockBrowserController.java @@ -239,6 +239,13 @@ public class BlockBrowserController implements BlockchainExtendQueryService { return peerService.getQueryService().getDataEntries(ledgerHash, address, keys); } + @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, path = "ledgers/{ledgerHash}/accounts/{address}/entries-version") + public KVDataEntry[] getDataEntries(@PathVariable("ledgerHash") HashDigest ledgerHash, + @PathVariable("address") String address, + @RequestBody KVInfoVO kvInfoVO) { + return peerService.getQueryService().getDataEntries(ledgerHash, address, kvInfoVO); + } + @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, path = "ledgers/{ledgerHash}/accounts/address/{address}/entries") @Override public KVDataEntry[] getDataEntries(@PathVariable("ledgerHash") HashDigest ledgerHash, diff --git a/source/gateway/src/main/java/com/jd/blockchain/gateway/web/GatewayWebServerConfigurer.java b/source/gateway/src/main/java/com/jd/blockchain/gateway/web/GatewayWebServerConfigurer.java index b93a8008..4481088f 100644 --- a/source/gateway/src/main/java/com/jd/blockchain/gateway/web/GatewayWebServerConfigurer.java +++ b/source/gateway/src/main/java/com/jd/blockchain/gateway/web/GatewayWebServerConfigurer.java @@ -2,6 +2,13 @@ package com.jd.blockchain.gateway.web; import java.util.List; +import com.jd.blockchain.binaryproto.DataContractRegistry; +import com.jd.blockchain.consensus.ClientIdentification; +import com.jd.blockchain.consensus.ClientIdentifications; +import com.jd.blockchain.consensus.action.ActionRequest; +import com.jd.blockchain.consensus.action.ActionResponse; +import com.jd.blockchain.consensus.bftsmart.BftsmartNodeSettings; +import com.jd.blockchain.ledger.*; import com.jd.blockchain.web.serializes.ByteArrayObjectUtil; import org.springframework.context.annotation.Configuration; import org.springframework.format.FormatterRegistry; @@ -27,6 +34,7 @@ public class GatewayWebServerConfigurer implements WebMvcConfigurer { static { JSONSerializeUtils.disableCircularReferenceDetect(); JSONSerializeUtils.configStringSerializer(ByteArray.class); + DataContractRegistry.register(BftsmartNodeSettings.class); } diff --git a/source/gateway/src/main/resources/gateway.conf b/source/gateway/src/main/resources/gateway.conf index c8ceaf4e..c0caa7c9 100644 --- a/source/gateway/src/main/resources/gateway.conf +++ b/source/gateway/src/main/resources/gateway.conf @@ -21,10 +21,10 @@ peer.providers=com.jd.blockchain.consensus.bftsmart.BftsmartConsensusProvider data.retrieval.url=http://127.0.0.1:10001 #榛樿鍏挜鐨勫唴瀹癸紙Base58缂栫爜鏁版嵁锛夛紱 -keys.default.pubkey=3snPdw7i7PapsDoW185c3kfK6p8s6SwiJAdEUzgnfeuUox12nxgzXu +keys.default.pubkey=3snPdw7i7PjVKiTH2VnXZu5H8QmNaSXpnk4ei533jFpuifyjS5zzH9 #榛樿绉侀挜鐨勮矾寰勶紱鍦 pk-path 鍜 pk 涔嬮棿蹇呴』璁剧疆鍏朵竴锛 keys.default.privkey-path= #榛樿绉侀挜鐨勫唴瀹癸紙鍔犲瘑鐨凚ase58缂栫爜鏁版嵁锛夛紱鍦 pk-path 鍜 pk 涔嬮棿蹇呴』璁剧疆鍏朵竴锛 -keys.default.privkey=177gjyoEUhdD1NkQSxBVvfSyovMd1ha5H46zsb9kyErLNBuQkLRAf2ea6CNjStjCFJQN8S1 +keys.default.privkey=177gjzHTznYdPgWqZrH43W3yp37onm74wYXT4v9FukpCHBrhRysBBZh7Pzdo5AMRyQGJD7x #榛樿绉侀挜鐨勮В鐮佸瘑鐮侊紱 keys.default.privkey-password=DYu3G8aGTMBW1WrTw76zxQJQU4DHLw9MLyy7peG4LKkY \ No newline at end of file diff --git a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/CryptoConfig.java b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/CryptoConfig.java index 6244d018..2b905d6b 100644 --- a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/CryptoConfig.java +++ b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/CryptoConfig.java @@ -1,22 +1,37 @@ package com.jd.blockchain.ledger.core; +import java.util.HashMap; + import com.jd.blockchain.crypto.CryptoAlgorithm; +import com.jd.blockchain.crypto.CryptoProvider; import com.jd.blockchain.ledger.CryptoSetting; public class CryptoConfig implements CryptoSetting { + private CryptoProvider[] cryptoProviders; + private short hashAlgorithm; private boolean autoVerifyHash; + HashMap providers; + HashMap nameAlgorithms; + HashMap codeAlgorithms; + public CryptoConfig() { } public CryptoConfig(CryptoSetting setting) { - this.hashAlgorithm = setting.getHashAlgorithm(); + setSupportedProviders(setting.getSupportedProviders()); + setHashAlgorithm(setting.getHashAlgorithm()); this.autoVerifyHash = setting.getAutoVerifyHash(); } + @Override + public CryptoProvider[] getSupportedProviders() { + return cryptoProviders == null ? null : cryptoProviders.clone(); + } + @Override public short getHashAlgorithm() { return hashAlgorithm; @@ -27,11 +42,47 @@ public class CryptoConfig implements CryptoSetting { return autoVerifyHash; } + public void setSupportedProviders(CryptoProvider[] supportedProviders) { + HashMap providers = new HashMap(); + HashMap nameAlgorithms = new HashMap(); + HashMap codeAlgorithms = new HashMap(); + if (supportedProviders != null) { + // 妫鏌ユ槸鍚﹀瓨鍦ㄩ噸澶嶇殑鎻愪緵鑰呬互鍙婄畻娉曪紱 + for (CryptoProvider cryptoProvider : supportedProviders) { + if (providers.containsKey(cryptoProvider.getName())) { + throw new LedgerException("Duplicate crypto providers [" + cryptoProvider.getName() + "]!"); + } + CryptoAlgorithm[] algorithms = cryptoProvider.getAlgorithms(); + for (CryptoAlgorithm alg : algorithms) { + if (nameAlgorithms.containsKey(alg.name())) { + throw new LedgerException("Duplicate crypto algorithms [" + alg.toString() + "] from provider " + + cryptoProvider.getName() + "!"); + } + if (codeAlgorithms.containsKey(alg.code())) { + throw new LedgerException("Duplicate crypto algorithms [" + alg.toString() + "] from provider" + + cryptoProvider.getName() + "!"); + } + nameAlgorithms.put(alg.name(), alg); + codeAlgorithms.put(alg.code(), alg); + } + providers.put(cryptoProvider.getName(), cryptoProvider); + } + } + this.providers = providers; + this.nameAlgorithms = nameAlgorithms; + this.codeAlgorithms = codeAlgorithms; + + this.cryptoProviders = supportedProviders; + } + public void setHashAlgorithm(CryptoAlgorithm hashAlgorithm) { - this.hashAlgorithm = hashAlgorithm.code(); + setHashAlgorithm(hashAlgorithm.code()); } public void setHashAlgorithm(short hashAlgorithm) { + if (codeAlgorithms == null || !codeAlgorithms.containsKey(hashAlgorithm)) { + throw new LedgerException("The specified algorithm[" + hashAlgorithm + "] has no provider!"); + } this.hashAlgorithm = hashAlgorithm; } diff --git a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/DataAccount.java b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/DataAccount.java index aa51901a..0655cb24 100644 --- a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/DataAccount.java +++ b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/DataAccount.java @@ -157,6 +157,9 @@ public class DataAccount implements AccountHeader, MerkleProvable { * @return return total count; */ public long getDataEntriesTotalCount() { + if(baseAccount == null){ + return 0; + } return baseAccount.dataset.getDataCount(); } diff --git a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/impl/LedgerManager.java b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/impl/LedgerManager.java index 93842206..4525ed9d 100644 --- a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/impl/LedgerManager.java +++ b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/impl/LedgerManager.java @@ -3,10 +3,15 @@ package com.jd.blockchain.ledger.core.impl; import java.util.HashMap; import java.util.Map; +import com.jd.blockchain.crypto.Crypto; +import com.jd.blockchain.crypto.CryptoAlgorithm; +import com.jd.blockchain.crypto.CryptoProvider; import com.jd.blockchain.crypto.HashDigest; +import com.jd.blockchain.ledger.CryptoSetting; import com.jd.blockchain.ledger.LedgerInitSetting; import com.jd.blockchain.ledger.core.LedgerConsts; import com.jd.blockchain.ledger.core.LedgerEditor; +import com.jd.blockchain.ledger.core.LedgerException; import com.jd.blockchain.ledger.core.LedgerManage; import com.jd.blockchain.ledger.core.LedgerRepository; import com.jd.blockchain.storage.service.ExPolicyKVStorage; @@ -57,26 +62,83 @@ public class LedgerManager implements LedgerManage { @Override public LedgerRepository register(HashDigest ledgerHash, KVStorageService storageService) { + // 鍔犺浇璐︽湰鏁版嵁搴擄紱 VersioningKVStorage ledgerVersioningStorage = storageService.getVersioningKVStorage(); ExPolicyKVStorage ledgerExPolicyStorage = storageService.getExPolicyKVStorage(); LedgerRepository ledgerRepo = new LedgerRepositoryImpl(ledgerHash, LEDGER_PREFIX, ledgerExPolicyStorage, ledgerVersioningStorage); - LedgerRepositoryContext ledgerCtx = new LedgerRepositoryContext(); - ledgerCtx.ledgerRepo = ledgerRepo; - ledgerCtx.storageService = storageService; + // 鏍¢獙 crypto service provider 锛 + CryptoSetting cryptoSetting = ledgerRepo.getAdminAccount().getSetting().getCryptoSetting(); + checkCryptoSetting(cryptoSetting, ledgerHash); + + // 鍒涘缓璐︽湰涓婁笅鏂囷紱 + LedgerRepositoryContext ledgerCtx = new LedgerRepositoryContext(ledgerRepo, storageService); ledgers.put(ledgerHash, ledgerCtx); return ledgerRepo; } + /** + * 妫鏌ヨ处鏈殑瀵嗙爜鍙傛暟璁剧疆涓庢湰鍦拌妭鐐圭殑杩愯鏃剁幆澧冩槸鍚﹀尮閰嶏紱 + * + * @param cryptoSetting + * @param ledgerHash + */ + private void checkCryptoSetting(CryptoSetting cryptoSetting, HashDigest ledgerHash) { + CryptoProvider[] cryptoProviders = cryptoSetting.getSupportedProviders(); + if (cryptoProviders == null || cryptoProviders.length == 0) { + throw new LedgerException("No supported crypto service providers has been setted in the ledger[" + + ledgerHash.toBase58() + "]!"); + } + for (CryptoProvider cp : cryptoProviders) { + CryptoProvider regCp = Crypto.getProvider(cp.getName()); + checkCryptoProviderConsistency(regCp, cp); + } + } + + /** + * 妫鏌ュ瘑鐮佹湇鍔℃彁渚涜呯殑淇℃伅鏄惁鍖归厤锛 + * + * @param registeredProvider + * @param settingProvider + */ + private void checkCryptoProviderConsistency(CryptoProvider registeredProvider, CryptoProvider settingProvider) { + if (registeredProvider == null) { + throw new LedgerException("Crypto service provider[" + settingProvider.getName() + + "] has not registered in the runtime environment of current peer!"); + } + + CryptoAlgorithm[] runtimeAlgothms = registeredProvider.getAlgorithms(); + CryptoAlgorithm[] settingAlgothms = settingProvider.getAlgorithms(); + if (runtimeAlgothms.length != settingAlgothms.length) { + throw new LedgerException("Crypto service provider[" + settingProvider.getName() + + "] has not registered in runtime of current peer!"); + } + HashMap runtimeAlgothmMap = new HashMap(); + for (CryptoAlgorithm alg : runtimeAlgothms) { + runtimeAlgothmMap.put(alg.code(), alg); + } + for (CryptoAlgorithm alg : settingAlgothms) { + CryptoAlgorithm regAlg = runtimeAlgothmMap.get(alg.code()); + if (regAlg == null) { + throw new LedgerException( + String.format("Crypto algorithm[%s] is not defined by provider[%s] in runtime of current peer!", + alg.toString(), registeredProvider.getName())); + } + if (!regAlg.name().equals(alg.name())) { + throw new LedgerException(String.format( + "Crypto algorithm[%s] do not match the same code algorithm[%s] defined by provider[%s] in runtime of current peer!", + CryptoAlgorithm.getString(alg), CryptoAlgorithm.getString(regAlg), + registeredProvider.getName())); + } + } + } + @Override public void unregister(HashDigest ledgerHash) { - LedgerRepositoryContext ledgerCtx = ledgers.get(ledgerHash); + LedgerRepositoryContext ledgerCtx = ledgers.remove(ledgerHash); if (ledgerCtx != null) { ledgerCtx.ledgerRepo.close(); - ledgers.remove(ledgerHash); - ledgerCtx.ledgerRepo = null; - ledgerCtx.storageService = null; } } @@ -88,18 +150,6 @@ public class LedgerManager implements LedgerManage { */ @Override public LedgerEditor newLedger(LedgerInitSetting initSetting, KVStorageService storageService) { - // GenesisLedgerStorageProxy genesisStorageProxy = new - // GenesisLedgerStorageProxy(); - // BufferedKVStorage bufferedStorage = new - // BufferedKVStorage(genesisStorageProxy, genesisStorageProxy, false); - - // LedgerEditor genesisBlockEditor = - // LedgerTransactionalEditor.createEditor(initSetting, - // bufferedStorage, bufferedStorage); - - // return new LedgerInitializer(genesisBlockEditor, bufferedStorage, - // genesisStorageProxy, storageService, this); - LedgerEditor genesisBlockEditor = LedgerTransactionalEditor.createEditor(initSetting, LEDGER_PREFIX, storageService.getExPolicyKVStorage(), storageService.getVersioningKVStorage()); return genesisBlockEditor; @@ -110,10 +160,16 @@ public class LedgerManager implements LedgerManage { return LEDGER_PREFIX + base58LedgerHash + LedgerConsts.KEY_SEPERATOR; } + private static class LedgerRepositoryContext { - private LedgerRepository ledgerRepo; + public final LedgerRepository ledgerRepo; - private KVStorageService storageService; + public final KVStorageService storageService; + + public LedgerRepositoryContext(LedgerRepository ledgerRepo, KVStorageService storageService) { + this.ledgerRepo = ledgerRepo; + this.storageService = storageService; + } } } diff --git a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/impl/LedgerQueryService.java b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/impl/LedgerQueryService.java index 086b18ff..fba47095 100644 --- a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/impl/LedgerQueryService.java +++ b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/impl/LedgerQueryService.java @@ -2,6 +2,7 @@ package com.jd.blockchain.ledger.core.impl; import com.jd.blockchain.binaryproto.BinaryProtocol; import com.jd.blockchain.binaryproto.PrimitiveType; +import com.jd.blockchain.contract.ContractException; import com.jd.blockchain.crypto.HashDigest; import com.jd.blockchain.ledger.*; import com.jd.blockchain.ledger.core.ContractAccountSet; @@ -15,6 +16,10 @@ import com.jd.blockchain.ledger.core.UserAccountSet; import com.jd.blockchain.transaction.BlockchainQueryService; import com.jd.blockchain.utils.Bytes; import com.jd.blockchain.utils.QueryUtil; +import com.jd.blockchain.utils.StringUtils; + +import java.util.ArrayList; +import java.util.List; public class LedgerQueryService implements BlockchainQueryService { @@ -263,6 +268,9 @@ public class LedgerQueryService implements BlockchainQueryService { long ver; for (int i = 0; i < entries.length; i++) { ver = dataAccount.getDataVersion(Bytes.fromString(keys[i])); + + dataAccount.getBytes(Bytes.fromString(keys[i]),1); + if (ver < 0) { entries[i] = new KVDataObject(keys[i], -1, PrimitiveType.NIL, null); }else { @@ -275,6 +283,60 @@ public class LedgerQueryService implements BlockchainQueryService { return entries; } + public KVDataEntry[] getDataEntries(HashDigest ledgerHash, String address, KVInfoVO kvInfoVO) { + //parse kvInfoVO; + List keyList = new ArrayList<>(); + List versionList = new ArrayList<>(); + if(kvInfoVO != null){ + for(KVDataVO kvDataVO : kvInfoVO.getData()){ + for(Long version : kvDataVO.getVersion()){ + keyList.add(kvDataVO.getKey()); + versionList.add(version); + } + } + } + String[] keys = keyList.toArray(new String[keyList.size()]); + Long[] versions = versionList.toArray(new Long[versionList.size()]); + + if (keys == null || keys.length == 0) { + return null; + } + if (versions == null || versions.length == 0) { + return null; + } + if(keys.length != versions.length){ + throw new ContractException("keys.length!=versions.length!"); + } + + LedgerRepository ledger = ledgerService.getLedger(ledgerHash); + LedgerBlock block = ledger.getLatestBlock(); + DataAccountSet dataAccountSet = ledger.getDataAccountSet(block); + DataAccount dataAccount = dataAccountSet.getDataAccount(Bytes.fromBase58(address)); + + KVDataEntry[] entries = new KVDataEntry[keys.length]; + long ver = -1; + for (int i = 0; i < entries.length; i++) { +// ver = dataAccount.getDataVersion(Bytes.fromString(keys[i])); +// dataAccount.getBytes(Bytes.fromString(keys[i]),1); + ver = versions[i]; + if (ver < 0) { + entries[i] = new KVDataObject(keys[i], -1, PrimitiveType.NIL, null); + }else { + if(dataAccount.getDataEntriesTotalCount()==0 || + dataAccount.getBytes(Bytes.fromString(keys[i]), ver) == null){ + //is the address is not exist; the result is null; + entries[i] = new KVDataObject(keys[i], -1, PrimitiveType.NIL, null); + } else { + byte[] value = dataAccount.getBytes(Bytes.fromString(keys[i]), ver); + BytesValue decodeData = BinaryProtocol.decode(value); + entries[i] = new KVDataObject(keys[i], ver, PrimitiveType.valueOf(decodeData.getType().CODE), decodeData.getValue().toBytes()); + } + } + } + + return entries; + } + @Override public KVDataEntry[] getDataEntries(HashDigest ledgerHash, String address, int fromIndex, int count) { diff --git a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/impl/handles/ContractLedgerContext.java b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/impl/handles/ContractLedgerContext.java index 2d51c185..43595152 100644 --- a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/impl/handles/ContractLedgerContext.java +++ b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/impl/handles/ContractLedgerContext.java @@ -156,6 +156,11 @@ public class ContractLedgerContext implements LedgerContext { return innerQueryService.getDataEntries(ledgerHash, address, keys); } + @Override + public KVDataEntry[] getDataEntries(HashDigest ledgerHash, String address, KVInfoVO kvInfoVO) { + return innerQueryService.getDataEntries(ledgerHash, address, kvInfoVO); + } + @Override public KVDataEntry[] getDataEntries(HashDigest ledgerHash, String address, int fromIndex, int count) { return innerQueryService.getDataEntries(ledgerHash, address, fromIndex, count); diff --git a/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/AccountSetTest.java b/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/AccountSetTest.java index 1334bdf5..5b69b118 100644 --- a/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/AccountSetTest.java +++ b/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/AccountSetTest.java @@ -6,8 +6,12 @@ import static org.junit.Assert.assertTrue; import org.junit.Test; +import com.jd.blockchain.crypto.Crypto; +import com.jd.blockchain.crypto.CryptoProvider; import com.jd.blockchain.crypto.HashDigest; import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; +import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; +import com.jd.blockchain.crypto.service.sm.SMCryptoService; import com.jd.blockchain.ledger.BlockchainKeyGenerator; import com.jd.blockchain.ledger.BlockchainKeypair; import com.jd.blockchain.ledger.core.AccountSet; @@ -17,6 +21,11 @@ import com.jd.blockchain.ledger.core.impl.OpeningAccessPolicy; import com.jd.blockchain.storage.service.utils.MemoryKVStorage; public class AccountSetTest { + + + private static final String[] SUPPORTED_PROVIDERS = { ClassicCryptoService.class.getName(), + SMCryptoService.class.getName() }; + @Test public void test() { @@ -24,7 +33,12 @@ public class AccountSetTest { MemoryKVStorage storage = new MemoryKVStorage(); + CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; + for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { + supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); + } CryptoConfig cryptoConf = new CryptoConfig(); + cryptoConf.setSupportedProviders(supportedProviders); cryptoConf.setAutoVerifyHash(true); cryptoConf.setHashAlgorithm(ClassicAlgorithm.SHA256); diff --git a/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/BaseAccountTest.java b/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/BaseAccountTest.java index 5d11b337..23b3445a 100644 --- a/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/BaseAccountTest.java +++ b/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/BaseAccountTest.java @@ -5,7 +5,11 @@ import static org.junit.Assert.assertFalse; import org.junit.Test; +import com.jd.blockchain.crypto.Crypto; +import com.jd.blockchain.crypto.CryptoProvider; import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; +import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; +import com.jd.blockchain.crypto.service.sm.SMCryptoService; import com.jd.blockchain.ledger.BlockchainKeyGenerator; import com.jd.blockchain.ledger.BlockchainKeypair; import com.jd.blockchain.ledger.core.BaseAccount; @@ -22,12 +26,21 @@ import com.jd.blockchain.utils.io.BytesUtils; */ public class BaseAccountTest { + public static final String[] SUPPORTED_PROVIDERS = { ClassicCryptoService.class.getName(), + SMCryptoService.class.getName() }; + @Test public void basicTest() { String keyPrefix = ""; MemoryKVStorage testStorage = new MemoryKVStorage(); + CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; + for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { + supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); + } + CryptoConfig cryptoConf = new CryptoConfig(); + cryptoConf.setSupportedProviders(supportedProviders); cryptoConf.setAutoVerifyHash(true); cryptoConf.setHashAlgorithm(ClassicAlgorithm.SHA256); diff --git a/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerAdminAccountTest.java b/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerAdminAccountTest.java index 823b3397..1293a9d8 100644 --- a/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerAdminAccountTest.java +++ b/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerAdminAccountTest.java @@ -13,8 +13,12 @@ import com.jd.blockchain.ledger.LedgerMetadata; import org.junit.Test; import com.jd.blockchain.crypto.AddressEncoding; +import com.jd.blockchain.crypto.Crypto; +import com.jd.blockchain.crypto.CryptoProvider; import com.jd.blockchain.crypto.HashDigest; import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; +import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; +import com.jd.blockchain.crypto.service.sm.SMCryptoService; import com.jd.blockchain.ledger.BlockchainKeyGenerator; import com.jd.blockchain.ledger.BlockchainKeypair; import com.jd.blockchain.ledger.ParticipantNode; @@ -30,6 +34,9 @@ import com.jd.blockchain.utils.net.NetworkAddress; public class LedgerAdminAccountTest { + private static final String[] SUPPORTED_PROVIDERS = { ClassicCryptoService.class.getName(), + SMCryptoService.class.getName() }; + private Random rand = new Random(); @Test @@ -55,7 +62,13 @@ public class LedgerAdminAccountTest { initSetting.setConsensusSettings(new Bytes(csSysSettingBytes)); initSetting.setConsensusProvider("consensus-provider"); + CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; + for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { + supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); + } + CryptoConfig cryptoSetting = new CryptoConfig(); + cryptoSetting.setSupportedProviders(supportedProviders); cryptoSetting.setAutoVerifyHash(true); cryptoSetting.setHashAlgorithm(ClassicAlgorithm.SHA256); initSetting.setCryptoSetting(cryptoSetting); diff --git a/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerEditerTest.java b/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerEditerTest.java index 621aa6fc..3ae78c32 100644 --- a/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerEditerTest.java +++ b/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerEditerTest.java @@ -11,8 +11,11 @@ import com.jd.blockchain.binaryproto.DataContractRegistry; import com.jd.blockchain.crypto.AddressEncoding; import com.jd.blockchain.crypto.AsymmetricKeypair; import com.jd.blockchain.crypto.Crypto; +import com.jd.blockchain.crypto.CryptoProvider; import com.jd.blockchain.crypto.SignatureFunction; import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; +import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; +import com.jd.blockchain.crypto.service.sm.SMCryptoService; import com.jd.blockchain.ledger.BlockchainKeypair; import com.jd.blockchain.ledger.LedgerBlock; import com.jd.blockchain.ledger.LedgerInitSetting; @@ -34,6 +37,10 @@ import com.jd.blockchain.utils.io.BytesUtils; import com.jd.blockchain.utils.net.NetworkAddress; public class LedgerEditerTest { + + private static final String[] SUPPORTED_PROVIDERS = { ClassicCryptoService.class.getName(), + SMCryptoService.class.getName() }; + static { DataContractRegistry.register(com.jd.blockchain.ledger.TransactionContent.class); @@ -109,8 +116,14 @@ public class LedgerEditerTest { private LedgerInitSetting createLedgerInitSetting() { SignatureFunction signFunc = Crypto.getSignatureFunction("ED25519"); + + CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; + for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { + supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); + } CryptoConfig defCryptoSetting = new CryptoConfig(); + defCryptoSetting.setSupportedProviders(supportedProviders); defCryptoSetting.setAutoVerifyHash(true); defCryptoSetting.setHashAlgorithm(ClassicAlgorithm.SHA256); diff --git a/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerInitOperationTest.java b/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerInitOperationTest.java index e9ae7693..9cee71df 100644 --- a/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerInitOperationTest.java +++ b/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerInitOperationTest.java @@ -12,7 +12,11 @@ import org.junit.Test; import com.jd.blockchain.binaryproto.BinaryProtocol; import com.jd.blockchain.binaryproto.DataContractRegistry; import com.jd.blockchain.crypto.AddressEncoding; +import com.jd.blockchain.crypto.Crypto; +import com.jd.blockchain.crypto.CryptoProvider; import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; +import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; +import com.jd.blockchain.crypto.service.sm.SMCryptoService; import com.jd.blockchain.ledger.BlockchainKeyGenerator; import com.jd.blockchain.ledger.BlockchainKeypair; import com.jd.blockchain.ledger.LedgerInitOperation; @@ -27,6 +31,9 @@ import com.jd.blockchain.utils.net.NetworkAddress; public class LedgerInitOperationTest { + private static final String[] SUPPORTED_PROVIDERS = { ClassicCryptoService.class.getName(), + SMCryptoService.class.getName() }; + byte[] seed = null; byte[] csSysSettingBytes = null; LedgerInitSettingData ledgerInitSettingData = new LedgerInitSettingData(); @@ -44,7 +51,12 @@ public class LedgerInitOperationTest { csSysSettingBytes = new byte[64]; rand.nextBytes(csSysSettingBytes); + CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; + for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { + supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); + } CryptoConfig cryptoConfig = new CryptoConfig(); + cryptoConfig.setSupportedProviders(supportedProviders); cryptoConfig.setAutoVerifyHash(true); cryptoConfig.setHashAlgorithm(ClassicAlgorithm.SHA256); diff --git a/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerInitSettingTest.java b/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerInitSettingSerializeTest.java similarity index 89% rename from source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerInitSettingTest.java rename to source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerInitSettingSerializeTest.java index 7f9a4e7c..f867ae9b 100644 --- a/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerInitSettingTest.java +++ b/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerInitSettingSerializeTest.java @@ -12,7 +12,11 @@ import org.junit.Test; import com.jd.blockchain.binaryproto.BinaryProtocol; import com.jd.blockchain.binaryproto.DataContractRegistry; import com.jd.blockchain.crypto.AddressEncoding; +import com.jd.blockchain.crypto.Crypto; +import com.jd.blockchain.crypto.CryptoProvider; import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; +import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; +import com.jd.blockchain.crypto.service.sm.SMCryptoService; import com.jd.blockchain.ledger.BlockchainKeyGenerator; import com.jd.blockchain.ledger.BlockchainKeypair; import com.jd.blockchain.ledger.LedgerInitSetting; @@ -24,12 +28,15 @@ 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(); LedgerInitOpTemplate template = new LedgerInitOpTemplate(); + private static final String[] SUPPORTED_PROVIDERS = { ClassicCryptoService.class.getName(), + SMCryptoService.class.getName() }; + @Before public void initCfg() { @@ -41,7 +48,13 @@ public class LedgerInitSettingTest { csSysSettingBytes = new byte[64]; rand.nextBytes(csSysSettingBytes); + CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; + for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { + supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); + } + CryptoConfig cryptoConfig = new CryptoConfig(); + cryptoConfig.setSupportedProviders(supportedProviders); cryptoConfig.setAutoVerifyHash(true); cryptoConfig.setHashAlgorithm(ClassicAlgorithm.SHA256); @@ -71,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); diff --git a/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerManagerTest.java b/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerManagerTest.java index 9974d5d8..cdbece82 100644 --- a/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerManagerTest.java +++ b/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerManagerTest.java @@ -13,9 +13,12 @@ import com.jd.blockchain.binaryproto.DataContractRegistry; import com.jd.blockchain.crypto.AddressEncoding; 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.SignatureFunction; import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; +import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; +import com.jd.blockchain.crypto.service.sm.SMCryptoService; import com.jd.blockchain.ledger.BlockBody; import com.jd.blockchain.ledger.BlockchainKeyGenerator; import com.jd.blockchain.ledger.BlockchainKeypair; @@ -54,8 +57,12 @@ public class LedgerManagerTest { DataContractRegistry.register(UserRegisterOperation.class); DataContractRegistry.register(DataAccountRegisterOperation.class); DataContractRegistry.register(BlockBody.class); + DataContractRegistry.register(CryptoProvider.class); } + public static final String[] SUPPORTED_PROVIDERS = { ClassicCryptoService.class.getName(), + SMCryptoService.class.getName() }; + private SignatureFunction signatureFunction; @Before @@ -170,7 +177,16 @@ public class LedgerManagerTest { } private LedgerInitSetting createLedgerInitSetting() { + + CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; + for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { + supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); + } + CryptoConfig defCryptoSetting = new CryptoConfig(); + + defCryptoSetting.setSupportedProviders(supportedProviders); + defCryptoSetting.setAutoVerifyHash(true); defCryptoSetting.setHashAlgorithm(ClassicAlgorithm.SHA256); diff --git a/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerMetaDataTest.java b/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerMetaDataTest.java index f744780d..401c5de2 100644 --- a/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerMetaDataTest.java +++ b/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerMetaDataTest.java @@ -15,9 +15,13 @@ import org.junit.Test; import com.jd.blockchain.binaryproto.BinaryProtocol; import com.jd.blockchain.binaryproto.DataContractRegistry; import com.jd.blockchain.crypto.AddressEncoding; +import com.jd.blockchain.crypto.Crypto; +import com.jd.blockchain.crypto.CryptoProvider; import com.jd.blockchain.crypto.HashDigest; import com.jd.blockchain.crypto.PubKey; import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; +import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; +import com.jd.blockchain.crypto.service.sm.SMCryptoService; import com.jd.blockchain.ledger.CryptoSetting; import com.jd.blockchain.ledger.ParticipantNode; import com.jd.blockchain.ledger.core.CryptoConfig; @@ -30,6 +34,10 @@ import com.jd.blockchain.utils.Bytes; * Created by zhangshuang3 on 2018/8/31. */ public class LedgerMetaDataTest { + + private static final String[] SUPPORTED_PROVIDERS = { ClassicCryptoService.class.getName(), + SMCryptoService.class.getName() }; + byte[] seed = null; String consensusProvider = "test-provider"; byte[] consensusSettingBytes = null; @@ -56,7 +64,13 @@ public class LedgerMetaDataTest { // ConsensusConfig consensusConfig = new ConsensusConfig(); // consensusConfig.setValue(settingValue);ClassicCryptoService.ED25519_ALGORITHM + CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; + for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { + supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); + } + CryptoConfig cryptoConfig = new CryptoConfig(); + cryptoConfig.setSupportedProviders(supportedProviders); cryptoConfig.setAutoVerifyHash(true); cryptoConfig.setHashAlgorithm(ClassicAlgorithm.SHA256); @@ -93,7 +107,13 @@ public class LedgerMetaDataTest { // ConsensusConfig consensusConfig = new ConsensusConfig(); // consensusConfig.setValue(settingValue); + CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; + for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { + supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); + } + CryptoConfig cryptoConfig = new CryptoConfig(); + cryptoConfig.setSupportedProviders(supportedProviders); cryptoConfig.setAutoVerifyHash(true); cryptoConfig.setHashAlgorithm(ClassicAlgorithm.SHA256); @@ -133,7 +153,14 @@ public class LedgerMetaDataTest { @Test public void testSerialize_CryptoSetting() { // LedgerCodes.METADATA_LEDGER_SETTING_CRYPTO + + CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; + for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { + supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); + } + CryptoConfig cryptoConfig = new CryptoConfig(); + cryptoConfig.setSupportedProviders(supportedProviders); cryptoConfig.setAutoVerifyHash(true); cryptoConfig.setHashAlgorithm(ClassicAlgorithm.SHA256); byte[] encodeBytes = BinaryProtocol.encode(cryptoConfig, CryptoSetting.class); diff --git a/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerTestUtils.java b/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerTestUtils.java index d6f6ae43..9bfdcc6a 100644 --- a/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerTestUtils.java +++ b/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/LedgerTestUtils.java @@ -4,10 +4,13 @@ import java.util.Random; 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.PubKey; import com.jd.blockchain.crypto.SignatureFunction; import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; +import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; +import com.jd.blockchain.crypto.service.sm.SMCryptoService; import com.jd.blockchain.ledger.BlockchainIdentityData; import com.jd.blockchain.ledger.CryptoSetting; import com.jd.blockchain.ledger.PreparedTransaction; @@ -22,6 +25,9 @@ public class LedgerTestUtils { // private static ThreadLocalRandom rand = ThreadLocalRandom.current(); + private static final String[] SUPPORTED_PROVIDERS = { ClassicCryptoService.class.getName(), + SMCryptoService.class.getName() }; + private static Random rand = new Random(); public static TransactionRequest createTxRequest(HashDigest ledgerHash) { @@ -76,7 +82,14 @@ public class LedgerTestUtils { } public static CryptoSetting createDefaultCryptoSetting() { + + CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; + for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { + supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); + } + CryptoConfig cryptoSetting = new CryptoConfig(); + cryptoSetting.setSupportedProviders(supportedProviders); cryptoSetting.setAutoVerifyHash(true); cryptoSetting.setHashAlgorithm(ClassicAlgorithm.SHA256); return cryptoSetting; diff --git a/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/MerkleDataSetTest.java b/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/MerkleDataSetTest.java index 2e966c50..fbb5eb09 100644 --- a/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/MerkleDataSetTest.java +++ b/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/MerkleDataSetTest.java @@ -14,8 +14,12 @@ import java.util.Set; import org.junit.Test; +import com.jd.blockchain.crypto.Crypto; +import com.jd.blockchain.crypto.CryptoProvider; import com.jd.blockchain.crypto.HashDigest; import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; +import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; +import com.jd.blockchain.crypto.service.sm.SMCryptoService; import com.jd.blockchain.ledger.core.CryptoConfig; import com.jd.blockchain.ledger.core.MerkleDataSet; import com.jd.blockchain.ledger.core.MerkleProof; @@ -26,13 +30,23 @@ import com.jd.blockchain.utils.io.BytesUtils; public class MerkleDataSetTest { + private static final String[] SUPPORTED_PROVIDERS = { ClassicCryptoService.class.getName(), + SMCryptoService.class.getName() }; + /** * 娴嬭瘯瀛樺偍鐨勫闀匡紱 */ @Test public void testStorageIncreasement() { + + CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; + for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { + supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); + } + String keyPrefix = ""; CryptoConfig cryptoConfig = new CryptoConfig(); + cryptoConfig.setSupportedProviders(supportedProviders); cryptoConfig.setHashAlgorithm(ClassicAlgorithm.SHA256); cryptoConfig.setAutoVerifyHash(true); @@ -116,7 +130,13 @@ public class MerkleDataSetTest { String keyPrefix = ""; Random rand = new Random(); + CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; + for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { + supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); + } + CryptoConfig cryptoConfig = new CryptoConfig(); + cryptoConfig.setSupportedProviders(supportedProviders); cryptoConfig.setHashAlgorithm(ClassicAlgorithm.SHA256); cryptoConfig.setAutoVerifyHash(true); @@ -281,7 +301,13 @@ public class MerkleDataSetTest { String keyPrefix = ""; Random rand = new Random(); + CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; + for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { + supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); + } + CryptoConfig cryptoConfig = new CryptoConfig(); + cryptoConfig.setSupportedProviders(supportedProviders); cryptoConfig.setHashAlgorithm(ClassicAlgorithm.SHA256); cryptoConfig.setAutoVerifyHash(true); diff --git a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/ContractEventSendOperation.java b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/ContractEventSendOperation.java index a9bd77e8..b5ee3b5a 100644 --- a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/ContractEventSendOperation.java +++ b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/ContractEventSendOperation.java @@ -1,26 +1,48 @@ -package com.jd.blockchain.ledger; - -import com.jd.blockchain.binaryproto.DataContract; -import com.jd.blockchain.binaryproto.DataField; -import com.jd.blockchain.binaryproto.PrimitiveType; -import com.jd.blockchain.consts.DataCodes; -import com.jd.blockchain.utils.Bytes; - -/** - * @author huanghaiquan - * - */ -@DataContract(code= DataCodes.TX_OP_CONTRACT_EVENT_SEND) -public interface ContractEventSendOperation extends Operation { - - @DataField(order=2, primitiveType=PrimitiveType.BYTES) - Bytes getContractAddress(); - - @DataField(order=3, primitiveType=PrimitiveType.TEXT) - String getEvent(); - - - @DataField(order=4, primitiveType=PrimitiveType.BYTES) - byte[] getArgs(); - -} +package com.jd.blockchain.ledger; + +import com.jd.blockchain.binaryproto.DataContract; +import com.jd.blockchain.binaryproto.DataField; +import com.jd.blockchain.binaryproto.PrimitiveType; +import com.jd.blockchain.consts.DataCodes; +import com.jd.blockchain.utils.Bytes; + +/** + * @author huanghaiquan + * + */ +@DataContract(code = DataCodes.TX_OP_CONTRACT_EVENT_SEND) +public interface ContractEventSendOperation extends Operation { + + /** + * 鍝嶅簲浜嬩欢鐨勫悎绾﹀湴鍧锛 + * + * @return + */ + @DataField(order = 2, primitiveType = PrimitiveType.BYTES) + Bytes getContractAddress(); + + /** + * 浜嬩欢鍚嶏紱 + * + * @return + */ + @DataField(order = 3, primitiveType = PrimitiveType.TEXT) + String getEvent(); + + /** + * 浜嬩欢鍙傛暟锛 + * + * @return + */ + @DataField(order = 4, primitiveType = PrimitiveType.BYTES) + byte[] getArgs(); + + /** + * 鏃堕棿鎴筹紱 + * + * @return + */ + @DataField(order = 5, primitiveType = PrimitiveType.INT64) + long getTs(); + +} diff --git a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/CryptoProviderInfo.java b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/CryptoProviderInfo.java deleted file mode 100644 index 01527459..00000000 --- a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/CryptoProviderInfo.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.jd.blockchain.ledger; - -import com.jd.blockchain.crypto.CryptoAlgorithm; - -public interface CryptoProviderInfo { - - String getName(); - - CryptoAlgorithm[] getAlgorithms(); - - -} diff --git a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/CryptoSetting.java b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/CryptoSetting.java index cb41c645..9f24322c 100644 --- a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/CryptoSetting.java +++ b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/CryptoSetting.java @@ -4,6 +4,7 @@ import com.jd.blockchain.binaryproto.DataContract; import com.jd.blockchain.binaryproto.DataField; import com.jd.blockchain.binaryproto.PrimitiveType; import com.jd.blockchain.consts.DataCodes; +import com.jd.blockchain.crypto.CryptoProvider; /** * 榛樺厠灏旀爲绠楁硶鐩稿叧鐨勯厤缃紱 @@ -14,6 +15,16 @@ import com.jd.blockchain.consts.DataCodes; @DataContract(code = DataCodes.METADATA_CRYPTO_SETTING) public interface CryptoSetting { + /** + * 绯荤粺鏀寔鐨勫瘑鐮佹湇鍔℃彁渚涜咃紱 + * + * @return + */ + @DataField(order = 0, refContract = true, list = true) + public CryptoProvider[] getSupportedProviders(); + + + /** * 绯荤粺涓娇鐢ㄧ殑 Hash 绠楁硶锛
* @@ -27,7 +38,7 @@ public interface CryptoSetting { public short getHashAlgorithm(); /** - * 褰撴湁瀹屾暣鎬ц瘉鏄庣殑鏁版嵁琚粠鎸佷箙鍖栦粙璐ㄤ腑鍔犺浇鏃讹紝鏄惁瀵瑰叾杩涜瀹屾暣鎬ф牎楠岋紙閲嶆柊璁$畻 hash 姣斿鏄惁涓鑷达級锛
+ * 褰撴湁鍔犺浇闄勫甫鍝堝笇鎽樿鐨勬暟鎹椂锛屾槸鍚﹂噸鏂拌绠楀搱甯屾憳瑕佽繘琛屽畬鏁存ф牎楠岋紱
* * 濡傛灉涓 true 锛屽垯鑷姩杩涜鏍¢獙锛屽鏋滄牎楠屽け璐ワ紝浼氬紩鍙戝紓甯革紱
* @@ -37,5 +48,6 @@ public interface CryptoSetting { */ @DataField(order = 2, primitiveType = PrimitiveType.BOOLEAN) public boolean getAutoVerifyHash(); + } diff --git a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/KVDataVO.java b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/KVDataVO.java new file mode 100644 index 00000000..4fc214d0 --- /dev/null +++ b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/KVDataVO.java @@ -0,0 +1,26 @@ +package com.jd.blockchain.ledger; + +/** + * @author zhaogw + * date 2019/5/14 14:17 + */ +public class KVDataVO { + private String key; + private long[] version; + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public long[] getVersion() { + return version; + } + + public void setVersion(long[] version) { + this.version = version; + } +} diff --git a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/KVInfoVO.java b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/KVInfoVO.java new file mode 100644 index 00000000..5f49e3de --- /dev/null +++ b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/KVInfoVO.java @@ -0,0 +1,18 @@ +package com.jd.blockchain.ledger; + +/** + * for BlockBrowserController.java, param is json ,then match it; + * @author zhaogw + * date 2019/5/14 14:19 + */ +public class KVInfoVO { + private KVDataVO[] data; + + public KVDataVO[] getData() { + return data; + } + + public void setData(KVDataVO[] data) { + this.data = data; + } +} diff --git a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/ParticipantNode.java b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/ParticipantNode.java index c8974d1c..dd2c62fa 100644 --- a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/ParticipantNode.java +++ b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/ParticipantNode.java @@ -22,6 +22,7 @@ public interface ParticipantNode {// extends ConsensusNode, ParticipantInfo { * * @return */ + @DataField(order = 0, primitiveType = PrimitiveType.INT32) int getId(); /** diff --git a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/TransactionContent.java b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/TransactionContent.java index 5203835a..96cdf58b 100644 --- a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/TransactionContent.java +++ b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/TransactionContent.java @@ -1,21 +1,21 @@ -package com.jd.blockchain.ledger; - -import com.jd.blockchain.binaryproto.DataContract; -import com.jd.blockchain.binaryproto.DataField; -import com.jd.blockchain.binaryproto.PrimitiveType; -import com.jd.blockchain.consts.DataCodes; -import com.jd.blockchain.crypto.HashDigest; - -/** - * 浜ゆ槗鍐呭锛 - * - * @author huanghaiquan - * - */ -@DataContract(code= DataCodes.TX_CONTENT) -public interface TransactionContent extends TransactionContentBody, HashObject { - @Override - @DataField(order=1, primitiveType = PrimitiveType.BYTES) - HashDigest getHash(); - -} +package com.jd.blockchain.ledger; + +import com.jd.blockchain.binaryproto.DataContract; +import com.jd.blockchain.binaryproto.DataField; +import com.jd.blockchain.binaryproto.PrimitiveType; +import com.jd.blockchain.consts.DataCodes; +import com.jd.blockchain.crypto.HashDigest; + +/** + * 浜ゆ槗鍐呭锛 + * + * @author huanghaiquan + * + */ +@DataContract(code= DataCodes.TX_CONTENT) +public interface TransactionContent extends TransactionContentBody, HashObject { + @Override + @DataField(order=1, primitiveType = PrimitiveType.BYTES) + HashDigest getHash(); + +} diff --git a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/TransactionContentBody.java b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/TransactionContentBody.java index 14ce61ac..e1819224 100644 --- a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/TransactionContentBody.java +++ b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/TransactionContentBody.java @@ -1,36 +1,36 @@ -package com.jd.blockchain.ledger; - -import com.jd.blockchain.binaryproto.DataContract; -import com.jd.blockchain.binaryproto.DataField; -import com.jd.blockchain.binaryproto.PrimitiveType; -import com.jd.blockchain.consts.DataCodes; -import com.jd.blockchain.crypto.HashDigest; - -/** - * 浜ゆ槗鍐呭锛 - * - * @author huanghaiquan - * - */ -@DataContract(code = DataCodes.TX_CONTENT_BODY) -public interface TransactionContentBody { - - /** - * 鎵ц浜ゆ槗鐨勮处鏈湴鍧锛 - * - * 娉細闄や簡璐︽湰鐨勫垱涓栦氦鏄撲箣澶栵紝浠讳綍浜ゆ槗鐨勮处鏈湴鍧閮戒笉鍏佽涓 null; - * - * @return - */ - @DataField(order = 1, primitiveType = PrimitiveType.BYTES) - HashDigest getLedgerHash(); - - /** - * 鎿嶄綔鍒楄〃锛 - * - * @return - */ - @DataField(order = 2, list = true, refContract = true, genericContract = true) - Operation[] getOperations(); - -} +package com.jd.blockchain.ledger; + +import com.jd.blockchain.binaryproto.DataContract; +import com.jd.blockchain.binaryproto.DataField; +import com.jd.blockchain.binaryproto.PrimitiveType; +import com.jd.blockchain.consts.DataCodes; +import com.jd.blockchain.crypto.HashDigest; + +/** + * 浜ゆ槗鍐呭锛 + * + * @author huanghaiquan + * + */ +@DataContract(code = DataCodes.TX_CONTENT_BODY) +public interface TransactionContentBody { + + /** + * 鎵ц浜ゆ槗鐨勮处鏈湴鍧锛 + * + * 娉細闄や簡璐︽湰鐨勫垱涓栦氦鏄撲箣澶栵紝浠讳綍浜ゆ槗鐨勮处鏈湴鍧閮戒笉鍏佽涓 null; + * + * @return + */ + @DataField(order = 1, primitiveType = PrimitiveType.BYTES) + HashDigest getLedgerHash(); + + /** + * 鎿嶄綔鍒楄〃锛 + * + * @return + */ + @DataField(order = 2, list = true, refContract = true, genericContract = true) + Operation[] getOperations(); + +} diff --git a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/transaction/BlockchainQueryService.java b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/transaction/BlockchainQueryService.java index 45301f13..1b46f1aa 100644 --- a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/transaction/BlockchainQueryService.java +++ b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/transaction/BlockchainQueryService.java @@ -261,6 +261,8 @@ public interface BlockchainQueryService { */ KVDataEntry[] getDataEntries(HashDigest ledgerHash, String address, String... keys); + KVDataEntry[] getDataEntries(HashDigest ledgerHash, String address, KVInfoVO kvInfoVO); + /** * 杩斿洖鎸囧畾鏁版嵁璐︽埛涓璌V鏁版嵁鐨勬绘暟锛
* diff --git a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/transaction/ContractEventSendOpTemplate.java b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/transaction/ContractEventSendOpTemplate.java index 29777b96..aa3a7b6a 100644 --- a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/transaction/ContractEventSendOpTemplate.java +++ b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/transaction/ContractEventSendOpTemplate.java @@ -1,40 +1,50 @@ -package com.jd.blockchain.transaction; - -import com.jd.blockchain.binaryproto.DataContractRegistry; -import com.jd.blockchain.ledger.ContractEventSendOperation; -import com.jd.blockchain.utils.Bytes; - -public class ContractEventSendOpTemplate implements ContractEventSendOperation { - static { - DataContractRegistry.register(ContractEventSendOperation.class); - } - - private Bytes contractAddress; - private byte[] args; - private String event; - - public ContractEventSendOpTemplate() { - } - - public ContractEventSendOpTemplate(Bytes contractAddress, String event, byte[] args) { - this.contractAddress = contractAddress; - this.event = event; - this.args = args; - } - - @Override - public Bytes getContractAddress() { - return contractAddress; - } - - @Override - public String getEvent() { - return event; - } - - @Override - public byte[] getArgs() { - return args; - } - -} +package com.jd.blockchain.transaction; + +import com.jd.blockchain.binaryproto.DataContractRegistry; +import com.jd.blockchain.ledger.ContractEventSendOperation; +import com.jd.blockchain.utils.Bytes; + +public class ContractEventSendOpTemplate implements ContractEventSendOperation { + static { + DataContractRegistry.register(ContractEventSendOperation.class); + } + + private Bytes contractAddress; + private byte[] args; + private String event; + + private long ts; + + public ContractEventSendOpTemplate() { + } + + public ContractEventSendOpTemplate(Bytes contractAddress, String event, byte[] args) { + this.contractAddress = contractAddress; + this.event = event; + this.args = args; + } + + @Override + public Bytes getContractAddress() { + return contractAddress; + } + + @Override + public String getEvent() { + return event; + } + + @Override + public byte[] getArgs() { + return args; + } + + @Override + public long getTs() { + return ts; + } + + public void setTs(long ts) { + this.ts = ts; + } +} diff --git a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/transaction/ContractEventSendOperationBuilderImpl.java b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/transaction/ContractEventSendOperationBuilderImpl.java index 7c54e4e8..b60153fc 100644 --- a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/transaction/ContractEventSendOperationBuilderImpl.java +++ b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/transaction/ContractEventSendOperationBuilderImpl.java @@ -4,18 +4,19 @@ import com.jd.blockchain.ledger.ContractEventSendOperation; import com.jd.blockchain.utils.Bytes; @Deprecated -class ContractEventSendOperationBuilderImpl implements ContractEventSendOperationBuilder{ - +class ContractEventSendOperationBuilderImpl implements ContractEventSendOperationBuilder { @Override public ContractEventSendOperation send(String address, String event, byte[] args) { ContractEventSendOpTemplate op = new ContractEventSendOpTemplate(Bytes.fromBase58(address), event, args); + op.setTs(System.currentTimeMillis()); return op; } - + @Override public ContractEventSendOperation send(Bytes address, String event, byte[] args) { ContractEventSendOpTemplate op = new ContractEventSendOpTemplate(address, event, args); + op.setTs(System.currentTimeMillis()); return op; } diff --git a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/transaction/ContractInvocationProxy.java b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/transaction/ContractInvocationProxy.java index c9b97e4d..00495bdb 100644 --- a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/transaction/ContractInvocationProxy.java +++ b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/transaction/ContractInvocationProxy.java @@ -1,22 +1,53 @@ package com.jd.blockchain.transaction; +import com.jd.blockchain.utils.Bytes; +import com.jd.blockchain.utils.serialize.binary.BinarySerializeUtils; + import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; -public class ContractInvocationProxy implements InvocationHandler { - - - private String contractMessage; - +public class ContractInvocationProxy implements InvocationHandler { + + // private String contractMessage; + + private Bytes contractAddress; + + private ContractType contractType; private ContractEventSendOperationBuilder sendOpBuilder; - - + + public ContractInvocationProxy(Bytes contractAddress, ContractType contractType, + ContractEventSendOperationBuilder sendOpBuilder) { + this.contractAddress = contractAddress; + this.contractType = contractType; + this.sendOpBuilder = sendOpBuilder; + } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { - // TODO Auto-generated method stub + + if(contractType == null){ + return "contractType == null, no invoke really."; + } + + String event = contractType.getEvent(method); + if (event == null) { + // 閫傞厤 Object 瀵硅薄鐨勬柟娉曪紱 + // toString 鏂规硶锛 + return String.format("[%s]-%s", contractAddress, contractType.toString()); + + // hashCode 鏂规硶锛 + } + // 鍚堢害鏂规硶锛 + byte[] argBytes = serializeArgs(args); + sendOpBuilder.send(contractAddress, event, argBytes); + + // TODO: 鏆傛椂鏈冭檻鏈夎繑鍥炲肩殑鎯呭喌锛 return null; } + private byte[] serializeArgs(Object[] args) { + // TODO 鏍规嵁鏂规硶鍙傛暟鐨勫畾涔夊簭鍒楀寲鍙傛暟锛 + return BinarySerializeUtils.serialize(args); + } } diff --git a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/transaction/DataAccountKVSetOpTemplate.java b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/transaction/DataAccountKVSetOpTemplate.java index 5a1b48ab..a9873c4c 100644 --- a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/transaction/DataAccountKVSetOpTemplate.java +++ b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/transaction/DataAccountKVSetOpTemplate.java @@ -49,7 +49,7 @@ public class DataAccountKVSetOpTemplate implements DataAccountKVSetOperation { public void set(String key, BytesValue value, long expVersion) { if (kvset.containsKey(key)) { - throw new IllegalArgumentException("Cann't set the same key repeatly!"); + throw new IllegalArgumentException("Cann't set the same key repeatedly!"); } KVData kvdata = new KVData(key, value, expVersion); kvset.put(key, kvdata); @@ -57,7 +57,7 @@ public class DataAccountKVSetOpTemplate implements DataAccountKVSetOperation { public void set(KVData kvData) { if (kvset.containsKey(kvData.getKey())) { - throw new IllegalArgumentException("Cann't set the same key repeatly!"); + throw new IllegalArgumentException("Cann't set the same key repeatedly!"); } kvset.put(kvData.getKey(), kvData); } diff --git a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/transaction/TxBuilder.java b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/transaction/TxBuilder.java index 1f315482..19d0020a 100644 --- a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/transaction/TxBuilder.java +++ b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/transaction/TxBuilder.java @@ -1,93 +1,93 @@ -package com.jd.blockchain.transaction; - -import com.jd.blockchain.binaryproto.BinaryProtocol; -import com.jd.blockchain.binaryproto.DataContractRegistry; -import com.jd.blockchain.crypto.Crypto; -import com.jd.blockchain.crypto.HashDigest; -import com.jd.blockchain.ledger.TransactionBuilder; -import com.jd.blockchain.ledger.TransactionContent; -import com.jd.blockchain.ledger.TransactionContentBody; -import com.jd.blockchain.ledger.TransactionRequestBuilder; -import com.jd.blockchain.utils.Bytes; - -public class TxBuilder implements TransactionBuilder { - - static { - DataContractRegistry.register(TransactionContentBody.class); - } - - private BlockchainOperationFactory opFactory = new BlockchainOperationFactory(); - - private static final String DEFAULT_HASH_ALGORITHM = "SHA256"; - - private HashDigest ledgerHash; - - public TxBuilder(HashDigest ledgerHash) { - this.ledgerHash = ledgerHash; - } - - @Override - public HashDigest getLedgerHash() { - return ledgerHash; - } - - @Override - public TransactionRequestBuilder prepareRequest() { - TransactionContent txContent = prepareContent(); - return new TxRequestBuilder(txContent); - } - - @Override - public TransactionContent prepareContent() { - TxContentBlob txContent = new TxContentBlob(ledgerHash); - txContent.addOperations(opFactory.getOperations()); - - byte[] contentBodyBytes = BinaryProtocol.encode(txContent, TransactionContentBody.class); - HashDigest contentHash = Crypto.getHashFunction(DEFAULT_HASH_ALGORITHM).hash(contentBodyBytes); - txContent.setHash(contentHash); - - return txContent; - } - - @Override - public LedgerInitOperationBuilder ledgers() { - return opFactory.ledgers(); - } - - @Override - public UserRegisterOperationBuilder users() { - return opFactory.users(); - } - - @Override - public DataAccountRegisterOperationBuilder dataAccounts() { - return opFactory.dataAccounts(); - } - - @Override - public DataAccountKVSetOperationBuilder dataAccount(String accountAddress) { - return opFactory.dataAccount(accountAddress); - } - - @Override - public DataAccountKVSetOperationBuilder dataAccount(Bytes accountAddress) { - return opFactory.dataAccount(accountAddress); - } - - @Override - public ContractCodeDeployOperationBuilder contracts() { - return opFactory.contracts(); - } - - @Override - public ContractEventSendOperationBuilder contractEvents() { - return opFactory.contractEvents(); - } - - @Override - public T contract(String address, Class contractIntf) { - // TODO Auto-generated method stub - throw new IllegalStateException("Not implemented."); - } - -} +package com.jd.blockchain.transaction; + +import com.jd.blockchain.binaryproto.BinaryProtocol; +import com.jd.blockchain.binaryproto.DataContractRegistry; +import com.jd.blockchain.crypto.Crypto; +import com.jd.blockchain.crypto.HashDigest; +import com.jd.blockchain.ledger.TransactionBuilder; +import com.jd.blockchain.ledger.TransactionContent; +import com.jd.blockchain.ledger.TransactionContentBody; +import com.jd.blockchain.ledger.TransactionRequestBuilder; +import com.jd.blockchain.utils.Bytes; + +public class TxBuilder implements TransactionBuilder { + + static { + DataContractRegistry.register(TransactionContentBody.class); + } + + private BlockchainOperationFactory opFactory = new BlockchainOperationFactory(); + + private static final String DEFAULT_HASH_ALGORITHM = "SHA256"; + + private HashDigest ledgerHash; + + public TxBuilder(HashDigest ledgerHash) { + this.ledgerHash = ledgerHash; + } + + @Override + public HashDigest getLedgerHash() { + return ledgerHash; + } + + @Override + public TransactionRequestBuilder prepareRequest() { + TransactionContent txContent = prepareContent(); + return new TxRequestBuilder(txContent); + } + + @Override + public TransactionContent prepareContent() { + TxContentBlob txContent = new TxContentBlob(ledgerHash); + txContent.addOperations(opFactory.getOperations()); + + byte[] contentBodyBytes = BinaryProtocol.encode(txContent, TransactionContentBody.class); + HashDigest contentHash = Crypto.getHashFunction(DEFAULT_HASH_ALGORITHM).hash(contentBodyBytes); + txContent.setHash(contentHash); + + return txContent; + } + + @Override + public LedgerInitOperationBuilder ledgers() { + return opFactory.ledgers(); + } + + @Override + public UserRegisterOperationBuilder users() { + return opFactory.users(); + } + + @Override + public DataAccountRegisterOperationBuilder dataAccounts() { + return opFactory.dataAccounts(); + } + + @Override + public DataAccountKVSetOperationBuilder dataAccount(String accountAddress) { + return opFactory.dataAccount(accountAddress); + } + + @Override + public DataAccountKVSetOperationBuilder dataAccount(Bytes accountAddress) { + return opFactory.dataAccount(accountAddress); + } + + @Override + public ContractCodeDeployOperationBuilder contracts() { + return opFactory.contracts(); + } + + @Override + public ContractEventSendOperationBuilder contractEvents() { + return opFactory.contractEvents(); + } + + @Override + public T contract(String address, Class contractIntf) { + // TODO Auto-generated method stub + throw new IllegalStateException("Not implemented."); + } + +} diff --git a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/transaction/TxContentBlob.java b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/transaction/TxContentBlob.java index f1cb2993..ccbe3a98 100644 --- a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/transaction/TxContentBlob.java +++ b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/transaction/TxContentBlob.java @@ -1,91 +1,91 @@ -package com.jd.blockchain.transaction; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -import com.jd.blockchain.crypto.HashDigest; -import com.jd.blockchain.ledger.Operation; -import com.jd.blockchain.ledger.TransactionContent; -import com.jd.blockchain.utils.io.NumberMask; - -/** - * 浜ゆ槗鍐呭鐨勬暟鎹潡锛 - *

- * - * 鍖呭惈鍘熷浜ゆ槗璇锋眰鐨勬暟鎹潡锛 - * - * @author huanghaiquan - * - */ -public class TxContentBlob implements TransactionContent { - - /** - * 鎿嶄綔鏁伴噺鐨勬渶澶у硷紱 - */ - public static final int MAX_OP_COUNT = NumberMask.SHORT.MAX_BOUNDARY_SIZE; - - private List operationList = new ArrayList(); - - private HashDigest hash; - - private HashDigest ledgerHash; - - public TxContentBlob(HashDigest ledgerHash) { - this.ledgerHash = ledgerHash; - } - - /** - * 浜ゆ槗鍐呭鐨勫搱甯屽硷紱 - */ - @Override - public HashDigest getHash() { - return this.hash; - } - - /** - * 鏇存柊浜ゆ槗鍐呭鐨勫搱甯屽硷紱 - *

- * 娉細褰撳墠瀵硅薄鍙厖褰撳煎璞★紝涓嶆牎楠屾寚瀹氬搱甯屽肩殑瀹屾暣鎬э紝璋冪敤鑰呭簲璇ュ湪澶栭儴瀹炴柦瀹屾暣鎬ф牎楠岋紱 - * - * @param hash - */ - public void setHash(HashDigest hash) { - this.hash = hash; - } - - /** - * 浜ゆ槗璇锋眰閾剧殑hash - * - * @return - */ - @Override - public HashDigest getLedgerHash() { - return ledgerHash; - } - - public void setLedgerHash(HashDigest ledgerHash) { - this.ledgerHash = ledgerHash; - } - - @Override - public Operation[] getOperations() { - return operationList.toArray(new Operation[operationList.size()]); - } - - public void setOperations(Object[] operations) { - // in array's case ,cast will failed! - for (Object operation : operations) { - Operation op = (Operation) operation; - addOperation(op); - } - } - - public void addOperation(Operation operation) { - operationList.add(operation); - } - - public void addOperations(Collection operations) { - operationList.addAll(operations); - } -} +package com.jd.blockchain.transaction; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import com.jd.blockchain.crypto.HashDigest; +import com.jd.blockchain.ledger.Operation; +import com.jd.blockchain.ledger.TransactionContent; +import com.jd.blockchain.utils.io.NumberMask; + +/** + * 浜ゆ槗鍐呭鐨勬暟鎹潡锛 + *

+ * + * 鍖呭惈鍘熷浜ゆ槗璇锋眰鐨勬暟鎹潡锛 + * + * @author huanghaiquan + * + */ +public class TxContentBlob implements TransactionContent { + + /** + * 鎿嶄綔鏁伴噺鐨勬渶澶у硷紱 + */ + public static final int MAX_OP_COUNT = NumberMask.SHORT.MAX_BOUNDARY_SIZE; + + private List operationList = new ArrayList(); + + private HashDigest hash; + + private HashDigest ledgerHash; + + public TxContentBlob(HashDigest ledgerHash) { + this.ledgerHash = ledgerHash; + } + + /** + * 浜ゆ槗鍐呭鐨勫搱甯屽硷紱 + */ + @Override + public HashDigest getHash() { + return this.hash; + } + + /** + * 鏇存柊浜ゆ槗鍐呭鐨勫搱甯屽硷紱 + *

+ * 娉細褰撳墠瀵硅薄鍙厖褰撳煎璞★紝涓嶆牎楠屾寚瀹氬搱甯屽肩殑瀹屾暣鎬э紝璋冪敤鑰呭簲璇ュ湪澶栭儴瀹炴柦瀹屾暣鎬ф牎楠岋紱 + * + * @param hash + */ + public void setHash(HashDigest hash) { + this.hash = hash; + } + + /** + * 浜ゆ槗璇锋眰閾剧殑hash + * + * @return + */ + @Override + public HashDigest getLedgerHash() { + return ledgerHash; + } + + public void setLedgerHash(HashDigest ledgerHash) { + this.ledgerHash = ledgerHash; + } + + @Override + public Operation[] getOperations() { + return operationList.toArray(new Operation[operationList.size()]); + } + + public void setOperations(Object[] operations) { + // in array's case ,cast will failed! + for (Object operation : operations) { + Operation op = (Operation) operation; + addOperation(op); + } + } + + public void addOperation(Operation operation) { + operationList.add(operation); + } + + public void addOperations(Collection operations) { + operationList.addAll(operations); + } +} diff --git a/source/peer/pom.xml b/source/peer/pom.xml index 51746e7d..01311d5c 100644 --- a/source/peer/pom.xml +++ b/source/peer/pom.xml @@ -20,11 +20,6 @@ consensus-framework ${project.version} - - com.jd.blockchain - state-transfer - ${project.version} - com.jd.blockchain ledger-rpc diff --git a/source/peer/src/main/java/com/jd/blockchain/peer/LedgerBindingConfigAware.java b/source/peer/src/main/java/com/jd/blockchain/peer/LedgerBindingConfigAware.java index c0bb263b..e8b244c6 100644 --- a/source/peer/src/main/java/com/jd/blockchain/peer/LedgerBindingConfigAware.java +++ b/source/peer/src/main/java/com/jd/blockchain/peer/LedgerBindingConfigAware.java @@ -4,8 +4,6 @@ import com.jd.blockchain.consensus.service.NodeServer; import com.jd.blockchain.crypto.HashDigest; import com.jd.blockchain.tools.initializer.LedgerBindingConfig; -import java.util.List; - public interface LedgerBindingConfigAware { void setConfig(LedgerBindingConfig config); diff --git a/source/peer/src/main/java/com/jd/blockchain/peer/web/LedgerQueryController.java b/source/peer/src/main/java/com/jd/blockchain/peer/web/LedgerQueryController.java index 6db9674c..222d7bf6 100644 --- a/source/peer/src/main/java/com/jd/blockchain/peer/web/LedgerQueryController.java +++ b/source/peer/src/main/java/com/jd/blockchain/peer/web/LedgerQueryController.java @@ -1,21 +1,30 @@ package com.jd.blockchain.peer.web; +import com.jd.blockchain.contract.ContractException; import com.jd.blockchain.ledger.*; -import com.jd.blockchain.ledger.core.*; +import com.jd.blockchain.utils.StringUtils; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import com.jd.blockchain.binaryproto.BinaryProtocol; import com.jd.blockchain.binaryproto.PrimitiveType; import com.jd.blockchain.crypto.HashDigest; +import com.jd.blockchain.ledger.core.ContractAccountSet; +import com.jd.blockchain.ledger.core.DataAccount; +import com.jd.blockchain.ledger.core.DataAccountSet; +import com.jd.blockchain.ledger.core.LedgerAdministration; +import com.jd.blockchain.ledger.core.LedgerRepository; +import com.jd.blockchain.ledger.core.LedgerService; +import com.jd.blockchain.ledger.core.ParticipantCertData; +import com.jd.blockchain.ledger.core.TransactionSet; +import com.jd.blockchain.ledger.core.UserAccountSet; import com.jd.blockchain.transaction.BlockchainQueryService; import com.jd.blockchain.utils.Bytes; import com.jd.blockchain.utils.QueryUtil; +import java.util.ArrayList; +import java.util.List; + @RestController @RequestMapping(path = "/") public class LedgerQueryController implements BlockchainQueryService { @@ -342,6 +351,63 @@ public class LedgerQueryController implements BlockchainQueryService { return entries; } + @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, path = "ledgers/{ledgerHash}/accounts/{address}/entries-version") + @Override + public KVDataEntry[] getDataEntries(@PathVariable(name = "ledgerHash") HashDigest ledgerHash, + @PathVariable(name = "address") String address, + @RequestBody KVInfoVO kvInfoVO) { + //parse kvInfoVO; + List keyList = new ArrayList<>(); + List versionList = new ArrayList<>(); + if(kvInfoVO != null){ + for(KVDataVO kvDataVO : kvInfoVO.getData()){ + for(Long version : kvDataVO.getVersion()){ + keyList.add(kvDataVO.getKey()); + versionList.add(version); + } + } + } + String[] keys = keyList.toArray(new String[keyList.size()]); + Long[] versions = versionList.toArray(new Long[versionList.size()]); + + if (keys == null || keys.length == 0) { + return null; + } + if (versions == null || versions.length == 0) { + return null; + } + if(keys.length != versions.length){ + throw new ContractException("keys.length!=versions.length!"); + } + + LedgerRepository ledger = ledgerService.getLedger(ledgerHash); + LedgerBlock block = ledger.getLatestBlock(); + DataAccountSet dataAccountSet = ledger.getDataAccountSet(block); + DataAccount dataAccount = dataAccountSet.getDataAccount(Bytes.fromBase58(address)); + + KVDataEntry[] entries = new KVDataEntry[keys.length]; + long ver = -1; + for (int i = 0; i < entries.length; i++) { +// ver = dataAccount.getDataVersion(Bytes.fromString(keys[i])); + ver = versions[i]; + if (ver < 0) { + entries[i] = new KVDataObject(keys[i], -1, PrimitiveType.NIL, null); + }else { + if(dataAccount.getDataEntriesTotalCount()==0 || + dataAccount.getBytes(Bytes.fromString(keys[i]), ver) == null){ + //is the address is not exist; the result is null; + entries[i] = new KVDataObject(keys[i], -1, PrimitiveType.NIL, null); + } else { + byte[] value = dataAccount.getBytes(Bytes.fromString(keys[i]), ver); + BytesValue decodeData = BinaryProtocol.decode(value); + entries[i] = new KVDataObject(keys[i], ver, PrimitiveType.valueOf(decodeData.getType().CODE), decodeData.getValue().toBytes()); + } + } + } + + return entries; + } + @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, path = "ledgers/{ledgerHash}/accounts/address/{address}/entries") @Override public KVDataEntry[] getDataEntries(@PathVariable(name = "ledgerHash") HashDigest ledgerHash, diff --git a/source/peer/src/main/java/com/jd/blockchain/peer/web/ManagementController.java b/source/peer/src/main/java/com/jd/blockchain/peer/web/ManagementController.java index 0e8c863e..ac95d361 100644 --- a/source/peer/src/main/java/com/jd/blockchain/peer/web/ManagementController.java +++ b/source/peer/src/main/java/com/jd/blockchain/peer/web/ManagementController.java @@ -5,10 +5,6 @@ import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import javax.annotation.PostConstruct; -import javax.annotation.PreDestroy; - -import com.jd.blockchain.ledger.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -35,6 +31,21 @@ import com.jd.blockchain.consensus.service.NodeServer; import com.jd.blockchain.consensus.service.ServerSettings; import com.jd.blockchain.consensus.service.StateMachineReplicate; import com.jd.blockchain.crypto.HashDigest; +import com.jd.blockchain.ledger.ContractCodeDeployOperation; +import com.jd.blockchain.ledger.ContractEventSendOperation; +import com.jd.blockchain.ledger.CryptoSetting; +import com.jd.blockchain.ledger.DataAccountKVSetOperation; +import com.jd.blockchain.ledger.DataAccountRegisterOperation; +import com.jd.blockchain.ledger.EndpointRequest; +import com.jd.blockchain.ledger.LedgerBlock; +import com.jd.blockchain.ledger.LedgerInitOperation; +import com.jd.blockchain.ledger.NodeRequest; +import com.jd.blockchain.ledger.Operation; +import com.jd.blockchain.ledger.TransactionContent; +import com.jd.blockchain.ledger.TransactionContentBody; +import com.jd.blockchain.ledger.TransactionRequest; +import com.jd.blockchain.ledger.TransactionResponse; +import com.jd.blockchain.ledger.UserRegisterOperation; import com.jd.blockchain.ledger.core.LedgerAdminAccount; import com.jd.blockchain.ledger.core.LedgerManage; import com.jd.blockchain.ledger.core.LedgerRepository; @@ -68,35 +79,17 @@ public class ManagementController implements LedgerBindingConfigAware, PeerManag public static final int MIN_GATEWAY_ID = 10000; - // @Autowired - // private PeerSettings peerSetting; - -// @Autowired -// private ConsensusTransactionService consensusService; - - // private ConsensusPeer consensusReplica; - @Autowired private LedgerManage ledgerManager; @Autowired private DbConnectionFactory connFactory; - // private Map ledgerConns = new - // ConcurrentHashMap<>(); - private Map ledgerTxConverters = new ConcurrentHashMap<>(); private Map ledgerPeers = new ConcurrentHashMap<>(); private Map ledgerCryptoSettings = new ConcurrentHashMap<>(); - // private Map nodeRealms = new - // ConcurrentHashMap<>(); - - // private Map ledgerRealms = new - // ConcurrentHashMap<>(); - // private Map ledgerRealmsNoConflict = new - // ConcurrentHashMap<>(); private LedgerBindingConfig config; @@ -106,9 +99,6 @@ public class ManagementController implements LedgerBindingConfigAware, PeerManag @Autowired private StateMachineReplicate consensusStateManager; - // private static int step = 0; - // private static int temp = 0; - static { DataContractRegistry.register(LedgerInitOperation.class); DataContractRegistry.register(LedgerBlock.class); @@ -134,24 +124,6 @@ public class ManagementController implements LedgerBindingConfigAware, PeerManag } - @PostConstruct - private void init() { - - } - - @PreDestroy - private void destroy() { -// DbConnection[] conns = ledgerConns.values().toArray(new DbConnection[ledgerConns.size()]); -// ledgerConns.clear(); -// for (DbConnection conn : conns) { -// try { -// conn.close(); -// } catch (Exception e) { -// // Ignore; -// } -// } - } - /** * 鎺ュ叆璁よ瘉锛 * @@ -234,42 +206,8 @@ public class ManagementController implements LedgerBindingConfigAware, PeerManag HashDigest[] ledgerHashs = config.getLedgerHashs(); for (HashDigest ledgerHash : ledgerHashs) { setConfig(config,ledgerHash); -// LedgerBindingConfig.BindingConfig bindingConfig = config.getLedger(ledgerHash); -// DbConnection dbConnNew = connFactory.connect(bindingConfig.getDbConnection().getUri(), -// bindingConfig.getDbConnection().getPassword()); -// LedgerRepository ledgerRepository = ledgerManager.register(ledgerHash, dbConnNew.getStorageService()); -// -// // load provider; -// LedgerAdminAccount ledgerAdminAccount = ledgerRepository.getAdminAccount(); -// String consensusProvider = ledgerAdminAccount.getSetting().getConsensusProvider(); -// ConsensusProvider provider = ConsensusProviders.getProvider(consensusProvider); -// // find current node; -// Bytes csSettingBytes = ledgerAdminAccount.getSetting().getConsensusSetting(); -// ConsensusSettings csSettings = provider.getSettingsFactory().getConsensusSettingsEncoder() -// .decode(csSettingBytes.toBytes()); -// NodeSettings currentNode = null; -// for (NodeSettings nodeSettings : csSettings.getNodes()) { -// if (nodeSettings.getAddress().equals(bindingConfig.getParticipant().getAddress())) { -// currentNode = nodeSettings; -// } -// } -// if (currentNode == null) { -// throw new IllegalArgumentException( -// "Current node is not found from the consensus settings of ledger[" + ledgerHash.toBase58() -// + "]!"); -// } -// ServerSettings serverSettings = provider.getServerFactory().buildServerSettings(ledgerHash.toBase58(), csSettings, currentNode.getAddress()); -// -// NodeServer server = provider.getServerFactory().setupServer(serverSettings, consensusMessageHandler, -// consensusStateManager); -// ledgerPeers.put(ledgerHash, server); -// ledgerCryptoSettings.put(ledgerHash, ledgerAdminAccount.getSetting().getCryptoSetting()); - } - // remove duplicate consensus realm,and establish consensus peer and consensus - // realm corresponding relationship - // initBindingConfig(config); this.config = config; } catch (Exception e) { @@ -314,46 +252,6 @@ public class ManagementController implements LedgerBindingConfigAware, PeerManag return server; } - // private void initBindingConfig(LedgerBindingConfig config) { - // boolean intersection = false; - // // to remove intersection consensus realm - // for (HashDigest hashDigest : ledgerRealms.keySet()) { - // ConsensusRealm consensusRealm1i = ledgerRealms.get(hashDigest); - // for (ConsensusRealm consensusRealm1j : ledgerRealms.values()) { - // // avoid compare with myself - // if (consensusRealm1i.equals(consensusRealm1j)) { - // continue; - // } - // if (consensusRealm1i.hasIntersection(consensusRealm1j)) { - // intersection = true; - // break; - // } - // } - // // prompt consensus realm conflict info - // if (intersection == true) { - // ConsoleUtils.info("\r\nconsensus realm intersection with other consensus - // realm\r\n"); - // continue; - // } - // if (intersection == false) { - // // add consensus realm without conflict to ledgerRealmsNoConflict - // ledgerRealmsNoConflict.put(hashDigest, consensusRealm1i); - // - // // String consensusSystemFile = - // config.getLedger(hashDigest).getCsConfigFile(); - // int currentId = config.getLedger(hashDigest).getParticipant().getId(); - // // init consensusSystemConfig; - // ConsensusProperties csProps = - // ConsensusProperties.resolve(consensusRealm1i.getSetting()); - // ConsensusPeer consensusPeer = new ConsensusPeer(consensusRealm1i, currentId, - // consensusService, - // csProps.getProperties()); - // ledgerPeers.put(hashDigest, consensusPeer); - // } - // } // END OF FOR:get ledgerRealmsNoConflict and ledgerPeers - // - // } - @Override public ConsensusRealm[] getRealms() { throw new IllegalStateException("Not implemented!"); @@ -364,45 +262,6 @@ public class ManagementController implements LedgerBindingConfigAware, PeerManag for (NodeServer peer : ledgerPeers.values()) { runRealm(peer); } - // try { - // - // // for (ConsensusPeer peer : ledgerPeers.values()) { - // for (Map.Entry entry : ledgerPeers.entrySet()) { - // HashDigest ledgerHash = entry.getKey(); - // ConsensusPeer peer = entry.getValue(); - // // TODO: 澶氱嚎绋嬪惎鍔紱 - // ConsensusNode[] nodes = peer.getConsensusRealm().getNodes(); - // StringBuilder consensusInfo = new StringBuilder(); - // for (ConsensusNode node : nodes) { - // consensusInfo.append( - // String.format("[%s]-%s; ", node.getAddress(), - // node.getConsensusAddress().toString())); - // } - // LOGGER.debug(String.format("-------- start consensus peer[Id=%s] --Nodes=%s - // -------------", - // peer.getCurrentId(), consensusInfo.toString())); - // peer.start(); - // // 璁剧疆娑堟伅闃熷垪 - // MsgQueueMessageDispatcher messageDispatcher = ledgerTxConverters.get(ledgerHash); - // - // if (messageDispatcher == null) { - // LedgerBindingConfig.BindingConfig bindingConfig = - // this.config.getLedger(ledgerHash); - // MQConnectionConfig mqConnection = bindingConfig.getMqConnection(); - // if (mqConnection != null && mqConnection.getServer() != null) { - // MessageQueueConfig mqConfig = new - // MessageQueueConfig(mqConnection.getServer(), - // mqConnection.getTopic()); - // messageDispatcher = MessageDispatcherFactory.newInstance(mqConfig, peer); - // Executors.newSingleThreadExecutor().execute(messageDispatcher); // 鍚姩鐩戝惉 - // } - // } - // } - // } catch (Exception e) { - // LOGGER.error("Error occurred on starting all consensus realms! --" + - // e.getMessage(), e); - // throw new IllegalStateException(e.getMessage(), e); - // } } @Override diff --git a/source/peer/src/main/java/com/jd/blockchain/peer/web/PeerTimeTasks.java b/source/peer/src/main/java/com/jd/blockchain/peer/web/PeerTimeTasks.java index 8e3d220f..698fb584 100644 --- a/source/peer/src/main/java/com/jd/blockchain/peer/web/PeerTimeTasks.java +++ b/source/peer/src/main/java/com/jd/blockchain/peer/web/PeerTimeTasks.java @@ -15,11 +15,11 @@ import com.jd.blockchain.peer.ConsensusManage; import com.jd.blockchain.peer.LedgerBindingConfigAware; import com.jd.blockchain.peer.PeerServerBooter; import com.jd.blockchain.tools.initializer.LedgerBindingConfig; -import com.jd.blockchain.utils.ArgumentSet; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.CommandLineRunner; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.core.io.ClassPathResource; @@ -38,9 +38,11 @@ import java.util.*; * @since 1.0.0 */ @Component -//@EnableScheduling +@EnableScheduling public class PeerTimeTasks implements ApplicationContextAware { + private static Logger LOGGER = LoggerFactory.getLogger(PeerTimeTasks.class); + private ApplicationContext applicationContext; @Autowired @@ -51,7 +53,9 @@ public class PeerTimeTasks implements ApplicationContextAware { //姣1鍒嗛挓鎵ц涓娆 @Scheduled(cron = "0 */5 * * * * ") public void updateLedger(){ - System.out.println ("Update Ledger Tasks Start " + new Date()); + + LOGGER.debug("Time Task Update Ledger Tasks Start {}", new Date()); + try { LedgerBindingConfig ledgerBindingConfig = loadLedgerBindingConfig(); @@ -78,7 +82,8 @@ public class PeerTimeTasks implements ApplicationContextAware { Map bindingConfigAwares = applicationContext.getBeansOfType(LedgerBindingConfigAware.class); List nodeServers = new ArrayList<>(); for (HashDigest ledgerHash : newAddHashs) { - System.out.printf("newLedger[%s] \r\n", ledgerHash.toBase58()); + + LOGGER.info("New Ledger [{}] Need To Be Init !!!", ledgerHash.toBase58()); for (LedgerBindingConfigAware aware : bindingConfigAwares.values()) { nodeServers.add(aware.setConfig(ledgerBindingConfig, ledgerHash)); } @@ -89,10 +94,10 @@ public class PeerTimeTasks implements ApplicationContextAware { consensusManage.runRealm(nodeServer); } } else { - System.out.println("All Ledgers is newest!!!"); + LOGGER.debug("All Ledgers is newest!!!"); } } catch (Exception e) { - e.printStackTrace(); + LOGGER.error(e.getMessage()); } } @@ -104,7 +109,8 @@ public class PeerTimeTasks implements ApplicationContextAware { private LedgerBindingConfig loadLedgerBindingConfig() throws Exception { LedgerBindingConfig ledgerBindingConfig; ledgerBindConfigFile = PeerServerBooter.ledgerBindConfigFile; - System.out.printf("load ledgerBindConfigFile = %s \r\n", ledgerBindConfigFile); + LOGGER.debug("Load LedgerBindConfigFile path = {}", + ledgerBindConfigFile == null ? "Default" : ledgerBindConfigFile); if (ledgerBindConfigFile == null) { ClassPathResource configResource = new ClassPathResource("ledger-binding.conf"); InputStream in = configResource.getInputStream(); diff --git a/source/sdk/sdk-base/src/main/java/com/jd/blockchain/sdk/LedgerInitSettings.java b/source/sdk/sdk-base/src/main/java/com/jd/blockchain/sdk/LedgerInitSettings.java index fa3c2cd0..d4f287ab 100644 --- a/source/sdk/sdk-base/src/main/java/com/jd/blockchain/sdk/LedgerInitSettings.java +++ b/source/sdk/sdk-base/src/main/java/com/jd/blockchain/sdk/LedgerInitSettings.java @@ -34,7 +34,7 @@ public class LedgerInitSettings { /** * 鍏辫瘑鍗忚 */ - private int consensusProtocol; + private String consensusProtocol; /** * 鍏辫瘑閰嶇疆 @@ -70,11 +70,11 @@ public class LedgerInitSettings { this.cryptoSetting = cryptoSetting; } - public int getConsensusProtocol() { + public String getConsensusProtocol() { return consensusProtocol; } - public void setConsensusProtocol(int consensusProtocol) { + public void setConsensusProtocol(String consensusProtocol) { this.consensusProtocol = consensusProtocol; } @@ -93,21 +93,4 @@ public class LedgerInitSettings { public void setParticipantNodes(ParticipantNode[] participantNodes) { this.participantNodes = participantNodes; } - - public enum CONSENSUS_PROTOCOL { - UNKNOWN(0), - BFTSMART(1), - MSGQUEUE(2), - ; - - private int code; - - CONSENSUS_PROTOCOL(int code) { - this.code = code; - } - - public int code() { - return code; - } - } } diff --git a/source/sdk/sdk-base/src/main/java/com/jd/blockchain/sdk/proxy/BlockchainServiceProxy.java b/source/sdk/sdk-base/src/main/java/com/jd/blockchain/sdk/proxy/BlockchainServiceProxy.java index 4dbe7bc9..e18f1df7 100644 --- a/source/sdk/sdk-base/src/main/java/com/jd/blockchain/sdk/proxy/BlockchainServiceProxy.java +++ b/source/sdk/sdk-base/src/main/java/com/jd/blockchain/sdk/proxy/BlockchainServiceProxy.java @@ -147,6 +147,11 @@ public abstract class BlockchainServiceProxy implements BlockchainService { return getQueryService(ledgerHash).getDataEntries(ledgerHash, address, keys); } + @Override + public KVDataEntry[] getDataEntries(HashDigest ledgerHash, String address, KVInfoVO kvInfoVO) { + return getQueryService(ledgerHash).getDataEntries(ledgerHash, address, kvInfoVO); + } + @Override public KVDataEntry[] getDataEntries(HashDigest ledgerHash, String address, int fromIndex, int count) { return getQueryService(ledgerHash).getDataEntries(ledgerHash, address, fromIndex, count); diff --git a/source/sdk/sdk-base/src/main/java/com/jd/blockchain/sdk/proxy/HttpBlockchainQueryService.java b/source/sdk/sdk-base/src/main/java/com/jd/blockchain/sdk/proxy/HttpBlockchainQueryService.java index bfdc0baf..bd063a5d 100644 --- a/source/sdk/sdk-base/src/main/java/com/jd/blockchain/sdk/proxy/HttpBlockchainQueryService.java +++ b/source/sdk/sdk-base/src/main/java/com/jd/blockchain/sdk/proxy/HttpBlockchainQueryService.java @@ -496,7 +496,11 @@ public interface HttpBlockchainQueryService extends BlockchainExtendQueryService @PathParam(name="address") String address, @RequestParam(name="keys", array = true) String... keys); - @HttpAction(method = HttpMethod.POST, path = "ledgers/{ledgerHash}/accounts/address/{address}/entries") + @HttpAction(method=HttpMethod.POST, path="ledgers/{ledgerHash}/accounts/{address}/entries-version") + @Override + KVDataEntry[] getDataEntries(@PathParam(name="ledgerHash", converter=HashDigestToStringConverter.class) HashDigest ledgerHash, + @PathParam(name="address") String address, + @RequestBody KVInfoVO kvInfoVO); /** * 杩斿洖鏁版嵁璐︽埛涓寚瀹氬簭鍙风殑鏈鏂板硷紱 @@ -513,6 +517,7 @@ public interface HttpBlockchainQueryService extends BlockchainExtendQueryService * 濡傛灉鍙傛暟鍊间负 -1锛屽垯杩斿洖鍏ㄩ儴鐨勮褰曪紱
* @return */ + @HttpAction(method = HttpMethod.POST, path = "ledgers/{ledgerHash}/accounts/address/{address}/entries") @Override KVDataEntry[] getDataEntries(@PathParam(name = "ledgerHash") HashDigest ledgerHash, @PathParam(name = "address") String address, diff --git a/source/test/pom.xml b/source/test/pom.xml index ac3b0562..4df214ad 100644 --- a/source/test/pom.xml +++ b/source/test/pom.xml @@ -15,7 +15,6 @@ test-consensus-node test-ledger-core test-integration - test-stp-community diff --git a/source/test/test-integration/src/main/java/test/com/jd/blockchain/intgr/PresetAnswerPrompter.java b/source/test/test-integration/src/main/java/test/com/jd/blockchain/intgr/PresetAnswerPrompter.java index 152ee356..d6ccbc6a 100644 --- a/source/test/test-integration/src/main/java/test/com/jd/blockchain/intgr/PresetAnswerPrompter.java +++ b/source/test/test-integration/src/main/java/test/com/jd/blockchain/intgr/PresetAnswerPrompter.java @@ -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; } - - } diff --git a/source/test/test-integration/src/main/java/test/com/jd/blockchain/intgr/consensus/ConsensusTest.java b/source/test/test-integration/src/main/java/test/com/jd/blockchain/intgr/consensus/ConsensusTest.java index bfb0a392..d7f66545 100644 --- a/source/test/test-integration/src/main/java/test/com/jd/blockchain/intgr/consensus/ConsensusTest.java +++ b/source/test/test-integration/src/main/java/test/com/jd/blockchain/intgr/consensus/ConsensusTest.java @@ -215,26 +215,26 @@ public class ConsensusTest { DBConnectionConfig testDb0 = new DBConnectionConfig(); testDb0.setConnectionUri("memory://local/0"); LedgerBindingConfig bindingConfig0 = new LedgerBindingConfig(); - AsyncCallback callback0 = nodeCtx0.startInitCommand(privkey0, encodedPassword, initSetting, csProps,csProvider, - testDb0, consolePrompter, bindingConfig0, quitLatch); + AsyncCallback 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 callback1 = nodeCtx1.startInitCommand(privkey1, encodedPassword, initSetting, csProps,csProvider, - testDb1, consolePrompter, bindingConfig1, quitLatch); + AsyncCallback 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 callback2 = nodeCtx2.startInitCommand(privkey2, encodedPassword, initSetting, csProps,csProvider, - testDb2, consolePrompter, bindingConfig2, quitLatch); + AsyncCallback 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 callback3 = nodeCtx3.startInitCommand(privkey3, encodedPassword, initSetting, csProps,csProvider, - testDb3, consolePrompter, bindingConfig3, quitLatch); + AsyncCallback 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 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 invoker = new ThreadInvoker() { @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; diff --git a/source/test/test-integration/src/main/java/test/com/jd/blockchain/intgr/perf/GlobalPerformanceTest.java b/source/test/test-integration/src/main/java/test/com/jd/blockchain/intgr/perf/GlobalPerformanceTest.java index ee1ecf4e..a35584c0 100644 --- a/source/test/test-integration/src/main/java/test/com/jd/blockchain/intgr/perf/GlobalPerformanceTest.java +++ b/source/test/test-integration/src/main/java/test/com/jd/blockchain/intgr/perf/GlobalPerformanceTest.java @@ -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; diff --git a/source/test/test-integration/src/main/java/test/com/jd/blockchain/intgr/perf/LedgerInitializeTest.java b/source/test/test-integration/src/main/java/test/com/jd/blockchain/intgr/perf/LedgerInitializeTest.java index 9a5cbd36..dbd8b21b 100644 --- a/source/test/test-integration/src/main/java/test/com/jd/blockchain/intgr/perf/LedgerInitializeTest.java +++ b/source/test/test-integration/src/main/java/test/com/jd/blockchain/intgr/perf/LedgerInitializeTest.java @@ -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 callback0 = node0.startInit(0, privkey0, initSetting, csProps, csProvider, testDb0, - consolePrompter); + AsyncCallback 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 callback1 = node1.startInit(1, privkey1, initSetting, csProps, csProvider, testDb1, - consolePrompter); + AsyncCallback 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 callback2 = node2.startInit(2, privkey2, initSetting, csProps, csProvider, testDb2, - consolePrompter); + AsyncCallback 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 callback3 = node3.startInit(3, privkey3, initSetting, csProps, csProvider, testDb03, - consolePrompter); + AsyncCallback 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 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 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 invoker = new ThreadInvoker() { @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 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 invoker = new ThreadInvoker() { @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); } }; diff --git a/source/test/test-integration/src/main/java/test/com/jd/blockchain/intgr/perf/LedgerInitializeWebTest.java b/source/test/test-integration/src/main/java/test/com/jd/blockchain/intgr/perf/LedgerInitializeWebTest.java index 2a72c515..fe0d8bbc 100644 --- a/source/test/test-integration/src/main/java/test/com/jd/blockchain/intgr/perf/LedgerInitializeWebTest.java +++ b/source/test/test-integration/src/main/java/test/com/jd/blockchain/intgr/perf/LedgerInitializeWebTest.java @@ -264,23 +264,23 @@ public class LedgerInitializeWebTest { DBConnectionConfig testDb0 = new DBConnectionConfig(); testDb0.setConnectionUri("memory://local/0"); - AsyncCallback callback0 = node0.startInit(privkey0, initSetting, csProps, csProvider, testDb0, - consolePrompter, quitLatch); + AsyncCallback callback0 = node0.startInit(privkey0, initSetting, testDb0, consolePrompter, + quitLatch); DBConnectionConfig testDb1 = new DBConnectionConfig(); testDb1.setConnectionUri("memory://local/1"); - AsyncCallback callback1 = node1.startInit(privkey1, initSetting, csProps, csProvider, testDb1, - consolePrompter, quitLatch); + AsyncCallback callback1 = node1.startInit(privkey1, initSetting, testDb1, consolePrompter, + quitLatch); DBConnectionConfig testDb2 = new DBConnectionConfig(); testDb2.setConnectionUri("memory://local/2"); - AsyncCallback callback2 = node2.startInit(privkey2, initSetting, csProps, csProvider, testDb2, - consolePrompter, quitLatch); + AsyncCallback callback2 = node2.startInit(privkey2, initSetting, testDb2, consolePrompter, + quitLatch); DBConnectionConfig testDb03 = new DBConnectionConfig(); testDb03.setConnectionUri("memory://local/3"); - AsyncCallback callback3 = node3.startInit(privkey3, initSetting, csProps, csProvider, testDb03, - consolePrompter, quitLatch); + AsyncCallback 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 startInit(PrivKey privKey, LedgerInitProperties setting, - ConsensusSettings csProps, ConsensusProvider csProvider, DBConnectionConfig dbConnConfig, - Prompter prompter, CountDownLatch quitLatch) { + DBConnectionConfig dbConnConfig, Prompter prompter, CountDownLatch quitLatch) { ThreadInvoker invoker = new ThreadInvoker() { @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; diff --git a/source/test/test-integration/src/main/java/test/com/jd/blockchain/intgr/perf/Utils.java b/source/test/test-integration/src/main/java/test/com/jd/blockchain/intgr/perf/Utils.java index 7bd15006..33d494a3 100644 --- a/source/test/test-integration/src/main/java/test/com/jd/blockchain/intgr/perf/Utils.java +++ b/source/test/test-integration/src/main/java/test/com/jd/blockchain/intgr/perf/Utils.java @@ -106,16 +106,14 @@ public class Utils { } public AsyncCallback 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 invoker = new ThreadInvoker() { @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 invoker = new ThreadInvoker() { @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); } }; diff --git a/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/IntegrationBaseTest.java b/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/IntegrationBaseTest.java index da0e82b8..0703ef98 100644 --- a/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/IntegrationBaseTest.java +++ b/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/IntegrationBaseTest.java @@ -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 callback0 = nodeCtx0.startInitCommand(privkey0, encodedPassword, initSetting, csProps, - csProvider, testDb0, consolePrompter, bindingConfig0, quitLatch); + AsyncCallback callback0 = nodeCtx0.startInitCommand(privkey0, encodedPassword, initSetting, testDb0, + consolePrompter, bindingConfig0, quitLatch); DBConnectionConfig testDb1 = new DBConnectionConfig(); testDb1.setConnectionUri(LedgerInitConsensusConfig.memConnectionStrings[1]); LedgerBindingConfig bindingConfig1 = new LedgerBindingConfig(); - AsyncCallback callback1 = nodeCtx1.startInitCommand(privkey1, encodedPassword, initSetting, csProps, - csProvider, testDb1, consolePrompter, bindingConfig1, quitLatch); + AsyncCallback callback1 = nodeCtx1.startInitCommand(privkey1, encodedPassword, initSetting, testDb1, + consolePrompter, bindingConfig1, quitLatch); DBConnectionConfig testDb2 = new DBConnectionConfig(); testDb2.setConnectionUri(LedgerInitConsensusConfig.memConnectionStrings[2]); LedgerBindingConfig bindingConfig2 = new LedgerBindingConfig(); - AsyncCallback callback2 = nodeCtx2.startInitCommand(privkey2, encodedPassword, initSetting, csProps, - csProvider, testDb2, consolePrompter, bindingConfig2, quitLatch); + AsyncCallback callback2 = nodeCtx2.startInitCommand(privkey2, encodedPassword, initSetting, testDb2, + consolePrompter, bindingConfig2, quitLatch); DBConnectionConfig testDb3 = new DBConnectionConfig(); testDb3.setConnectionUri(LedgerInitConsensusConfig.memConnectionStrings[3]); LedgerBindingConfig bindingConfig3 = new LedgerBindingConfig(); - AsyncCallback callback3 = nodeCtx3.startInitCommand(privkey3, encodedPassword, initSetting, csProps, - csProvider, testDb3, consolePrompter, bindingConfig3, quitLatch); + AsyncCallback callback3 = nodeCtx3.startInitCommand(privkey3, encodedPassword, initSetting, testDb3, + consolePrompter, bindingConfig3, quitLatch); HashDigest ledgerHash0 = callback0.waitReturn(); HashDigest ledgerHash1 = callback1.waitReturn(); diff --git a/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/IntegrationTest2.java b/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/IntegrationTest2.java index 4e8d317b..a6f2f30a 100644 --- a/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/IntegrationTest2.java +++ b/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/IntegrationTest2.java @@ -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 peerStarting0 = peer0.start(); AsyncCallback peerStarting1 = peer1.start(); @@ -192,26 +192,26 @@ public class IntegrationTest2 { DBConnectionConfig testDb0 = new DBConnectionConfig(); testDb0.setConnectionUri(dbConns[0]); LedgerBindingConfig bindingConfig0 = new LedgerBindingConfig(); - AsyncCallback callback0 = nodeCtx0.startInitCommand(privkey0, encodedPassword, initSetting, csProps, csProvider, - testDb0, consolePrompter, bindingConfig0, quitLatch); + AsyncCallback callback0 = nodeCtx0.startInitCommand(privkey0, encodedPassword, initSetting, testDb0, + consolePrompter, bindingConfig0, quitLatch); DBConnectionConfig testDb1 = new DBConnectionConfig(); testDb1.setConnectionUri(dbConns[1]); LedgerBindingConfig bindingConfig1 = new LedgerBindingConfig(); - AsyncCallback callback1 = nodeCtx1.startInitCommand(privkey1, encodedPassword, initSetting, csProps,csProvider, - testDb1, consolePrompter, bindingConfig1, quitLatch); + AsyncCallback callback1 = nodeCtx1.startInitCommand(privkey1, encodedPassword, initSetting, testDb1, + consolePrompter, bindingConfig1, quitLatch); DBConnectionConfig testDb2 = new DBConnectionConfig(); testDb2.setConnectionUri(dbConns[2]); LedgerBindingConfig bindingConfig2 = new LedgerBindingConfig(); - AsyncCallback callback2 = nodeCtx2.startInitCommand(privkey2, encodedPassword, initSetting, csProps,csProvider, - testDb2, consolePrompter, bindingConfig2, quitLatch); + AsyncCallback callback2 = nodeCtx2.startInitCommand(privkey2, encodedPassword, initSetting, testDb2, + consolePrompter, bindingConfig2, quitLatch); DBConnectionConfig testDb3 = new DBConnectionConfig(); testDb3.setConnectionUri(dbConns[3]); LedgerBindingConfig bindingConfig3 = new LedgerBindingConfig(); - AsyncCallback callback3 = nodeCtx3.startInitCommand(privkey3, encodedPassword, initSetting, csProps,csProvider, - testDb3, consolePrompter, bindingConfig3, quitLatch); + AsyncCallback 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()); } - /** * 鏍规嵁鍚堢害鏋勫缓瀛楄妭鏁扮粍; * diff --git a/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/IntegrationTestDataAccount.java b/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/IntegrationTestDataAccount.java index bc16860c..5b1b5457 100644 --- a/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/IntegrationTestDataAccount.java +++ b/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/IntegrationTestDataAccount.java @@ -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 callback0 = nodeCtx0.startInitCommand(privkey0, encodedPassword, initSetting, csProps, - csProvider, testDb0, consolePrompter, bindingConfig0, quitLatch); + AsyncCallback callback0 = nodeCtx0.startInitCommand(privkey0, encodedPassword, initSetting, testDb0, + consolePrompter, bindingConfig0, quitLatch); DBConnectionConfig testDb1 = new DBConnectionConfig(); testDb1.setConnectionUri(dbConns[1]); LedgerBindingConfig bindingConfig1 = new LedgerBindingConfig(); - AsyncCallback callback1 = nodeCtx1.startInitCommand(privkey1, encodedPassword, initSetting, csProps, - csProvider, testDb1, consolePrompter, bindingConfig1, quitLatch); + AsyncCallback callback1 = nodeCtx1.startInitCommand(privkey1, encodedPassword, initSetting, testDb1, + consolePrompter, bindingConfig1, quitLatch); DBConnectionConfig testDb2 = new DBConnectionConfig(); testDb2.setConnectionUri(dbConns[2]); LedgerBindingConfig bindingConfig2 = new LedgerBindingConfig(); - AsyncCallback callback2 = nodeCtx2.startInitCommand(privkey2, encodedPassword, initSetting, csProps, - csProvider, testDb2, consolePrompter, bindingConfig2, quitLatch); + AsyncCallback callback2 = nodeCtx2.startInitCommand(privkey2, encodedPassword, initSetting, testDb2, + consolePrompter, bindingConfig2, quitLatch); DBConnectionConfig testDb3 = new DBConnectionConfig(); testDb3.setConnectionUri(dbConns[3]); LedgerBindingConfig bindingConfig3 = new LedgerBindingConfig(); - AsyncCallback callback3 = nodeCtx3.startInitCommand(privkey3, encodedPassword, initSetting, csProps, - csProvider, testDb3, consolePrompter, bindingConfig3, quitLatch); + AsyncCallback callback3 = nodeCtx3.startInitCommand(privkey3, encodedPassword, initSetting, testDb3, + consolePrompter, bindingConfig3, quitLatch); HashDigest ledgerHash0 = callback0.waitReturn(); HashDigest ledgerHash1 = callback1.waitReturn(); diff --git a/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/initializer/LedgerInitSettingTest.java b/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/initializer/LedgerInitPropertiesTest.java similarity index 67% rename from source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/initializer/LedgerInitSettingTest.java rename to source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/initializer/LedgerInitPropertiesTest.java index f0100d9f..3f926073 100644 --- a/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/initializer/LedgerInitSettingTest.java +++ b/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/initializer/LedgerInitPropertiesTest.java @@ -16,39 +16,44 @@ import com.jd.blockchain.tools.keygen.KeyGenCommand; import com.jd.blockchain.utils.codec.HexUtils; import test.com.jd.blockchain.intgr.IntegrationBase; -public class LedgerInitSettingTest { +public class LedgerInitPropertiesTest { @Test public void test() throws IOException { ClassPathResource ledgerInitSettingResource = new ClassPathResource("ledger.init"); InputStream in = ledgerInitSettingResource.getInputStream(); try { - LedgerInitProperties setting = LedgerInitProperties.resolve(in); - assertEquals(4, setting.getConsensusParticipantCount()); - String expectedLedgerSeed = "932dfe23-fe23232f-283f32fa-dd32aa76-8322ca2f-56236cda-7136b322-cb323ffe".replace("-", ""); - String actualLedgerSeed = HexUtils.encode(setting.getLedgerSeed()); + LedgerInitProperties initProps = LedgerInitProperties.resolve(in); + assertEquals(4, initProps.getConsensusParticipantCount()); + String expectedLedgerSeed = "932dfe23-fe23232f-283f32fa-dd32aa76-8322ca2f-56236cda-7136b322-cb323ffe" + .replace("-", ""); + String actualLedgerSeed = HexUtils.encode(initProps.getLedgerSeed()); assertEquals(expectedLedgerSeed, actualLedgerSeed); - - ConsensusParticipantConfig part0 = setting.getConsensusParticipant(0); + + assertEquals("com.jd.blockchain.consensus.bftsmart.BftsmartConsensusProvider", + initProps.getConsensusProvider()); + + String[] cryptoProviders = initProps.getCryptoProviders(); + assertEquals(2, cryptoProviders.length); + assertEquals("com.jd.blockchain.crypto.service.classic.ClassicCryptoService", cryptoProviders[0]); + assertEquals("com.jd.blockchain.crypto.service.sm.SMCryptoService", cryptoProviders[1]); + + ConsensusParticipantConfig part0 = initProps.getConsensusParticipant(0); assertEquals("jd.com", part0.getName()); - assertEquals("keys/jd-com.pub", part0.getPubKeyPath()); PubKey pubKey0 = KeyGenCommand.decodePubKey("3snPdw7i7PjVKiTH2VnXZu5H8QmNaSXpnk4ei533jFpuifyjS5zzH9"); assertEquals(pubKey0, part0.getPubKey()); -// assertEquals("127.0.0.1", part0.getConsensusAddress().getHost()); -// assertEquals(8900, part0.getConsensusAddress().getPort()); -// assertEquals(true, part0.getConsensusAddress().isSecure()); assertEquals("127.0.0.1", part0.getInitializerAddress().getHost()); assertEquals(8800, part0.getInitializerAddress().getPort()); assertEquals(true, part0.getInitializerAddress().isSecure()); - - ConsensusParticipantConfig part1 = setting.getConsensusParticipant(1); + + ConsensusParticipantConfig part1 = initProps.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()); - + + ConsensusParticipantConfig part2 = initProps.getConsensusParticipant(2); + assertEquals("7VeRAr3dSbi1xatq11ZcF7sEPkaMmtZhV9shonGJWk9T4pLe", part2.getPubKey().toBase58()); + } finally { in.close(); } diff --git a/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/initializer/LedgerInitializeTest.java b/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/initializer/LedgerInitializeTest.java index e96d3c18..09ddf134 100644 --- a/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/initializer/LedgerInitializeTest.java +++ b/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/initializer/LedgerInitializeTest.java @@ -9,22 +9,22 @@ 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.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; import com.jd.blockchain.crypto.SignatureDigest; +import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; +import com.jd.blockchain.crypto.service.sm.SMCryptoService; import com.jd.blockchain.ledger.LedgerBlock; import com.jd.blockchain.ledger.LedgerInitOperation; import com.jd.blockchain.ledger.UserRegisterOperation; @@ -35,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; @@ -60,6 +61,9 @@ public class LedgerInitializeTest { DataContractRegistry.register(UserRegisterOperation.class); } + private static final String[] SUPPORTED_PROVIDERS = { ClassicCryptoService.class.getName(), + SMCryptoService.class.getName() }; + public static final String PASSWORD = IntegrationBase.PASSWORD; public static final String[] PUB_KEYS = IntegrationBase.PUB_KEYS; @@ -85,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); @@ -100,26 +104,22 @@ public class LedgerInitializeTest { PrivKey privkey0 = KeyGenCommand.decodePrivKeyWithRawPassword(PRIV_KEYS[0], PASSWORD); DBConnectionConfig testDb0 = new DBConnectionConfig(); testDb0.setConnectionUri(dbConnections[0]); - AsyncCallback callback0 = node0.startInit(0, privkey0, initSetting, csProps, csProvider, testDb0, - consolePrompter); + AsyncCallback 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 callback1 = node1.startInit(1, privkey1, initSetting, csProps, csProvider, testDb1, - consolePrompter); + AsyncCallback 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 callback2 = node2.startInit(2, privkey2, initSetting, csProps, csProvider, testDb2, - consolePrompter); + AsyncCallback 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 callback3 = node3.startInit(3, privkey3, initSetting, csProps, csProvider, testDb03, - consolePrompter); + AsyncCallback callback3 = node3.startInit(3, privkey3, initSetting, testDb03, consolePrompter); HashDigest ledgerHash0 = callback0.waitReturn(); HashDigest ledgerHash1 = callback1.waitReturn(); @@ -234,16 +234,14 @@ public class LedgerInitializeTest { } public AsyncCallback 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 invoker = new ThreadInvoker() { @Override protected HashDigest invoke() throws Exception { - return initProcess.initialize(currentId, privKey, setting, csProps, csProvider, dbConnConfig, - prompter); + return initProcess.initialize(currentId, privKey, setting, dbConnConfig, prompter); } }; @@ -251,10 +249,14 @@ public class LedgerInitializeTest { } public AsyncCallback 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++) { + supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); + } CryptoConfig cryptoSetting = new CryptoConfig(); + cryptoSetting.setSupportedProviders(supportedProviders); cryptoSetting.setAutoVerifyHash(autoVerifyHash); cryptoSetting.setHashAlgorithm(Crypto.getAlgorithm("SHA256")); @@ -263,8 +265,7 @@ public class LedgerInitializeTest { ThreadInvoker invoker = new ThreadInvoker() { @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); } }; diff --git a/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/initializer/LedgerInitializeWeb4Nodes.java b/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/initializer/LedgerInitializeWeb4Nodes.java index ec80dd18..ef18078f 100644 --- a/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/initializer/LedgerInitializeWeb4Nodes.java +++ b/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/initializer/LedgerInitializeWeb4Nodes.java @@ -94,23 +94,23 @@ public class LedgerInitializeWeb4Nodes { DBConnectionConfig testDb0 = new DBConnectionConfig(); testDb0.setConnectionUri(dbConns[0]); - AsyncCallback callback0 = node0.startInit(privkey0, initSetting, csProps, csProvider, testDb0, - consolePrompter, quitLatch); + AsyncCallback callback0 = node0.startInit(privkey0, initSetting, testDb0, consolePrompter, + quitLatch); DBConnectionConfig testDb1 = new DBConnectionConfig(); testDb1.setConnectionUri(dbConns[1]); - AsyncCallback callback1 = node1.startInit(privkey1, initSetting, csProps, csProvider, testDb1, - consolePrompter, quitLatch); + AsyncCallback callback1 = node1.startInit(privkey1, initSetting, testDb1, consolePrompter, + quitLatch); DBConnectionConfig testDb2 = new DBConnectionConfig(); testDb2.setConnectionUri(dbConns[2]); - AsyncCallback callback2 = node2.startInit(privkey2, initSetting, csProps, csProvider, testDb2, - consolePrompter, quitLatch); + AsyncCallback callback2 = node2.startInit(privkey2, initSetting, testDb2, consolePrompter, + quitLatch); DBConnectionConfig testDb03 = new DBConnectionConfig(); testDb03.setConnectionUri(dbConns[3]); - AsyncCallback callback3 = node3.startInit(privkey3, initSetting, csProps, csProvider, testDb03, - consolePrompter, quitLatch); + AsyncCallback 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 startInit(PrivKey privKey, LedgerInitProperties setting, - ConsensusSettings csProps, ConsensusProvider csProvider, DBConnectionConfig dbConnConfig, - Prompter prompter, CountDownLatch quitLatch) { + DBConnectionConfig dbConnConfig, Prompter prompter, CountDownLatch quitLatch) { ThreadInvoker invoker = new ThreadInvoker() { @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()); diff --git a/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/initializer/LedgerInitializeWeb4SingleStepsTest.java b/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/initializer/LedgerInitializeWeb4SingleStepsTest.java index bdc80ae4..3426b296 100644 --- a/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/initializer/LedgerInitializeWeb4SingleStepsTest.java +++ b/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/initializer/LedgerInitializeWeb4SingleStepsTest.java @@ -340,8 +340,7 @@ public class LedgerInitializeWeb4SingleStepsTest { } public AsyncCallback startInit(PrivKey privKey, LedgerInitProperties setting, - ConsensusSettings csProps, ConsensusProvider csProvider, DBConnectionConfig dbConnConfig, - Prompter prompter, CountDownLatch quitLatch) { + DBConnectionConfig dbConnConfig, Prompter prompter, CountDownLatch quitLatch) { ThreadInvoker invoker = new ThreadInvoker() { @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 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; diff --git a/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/ledger/LedgerBlockGeneratingTest.java b/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/ledger/LedgerBlockGeneratingTest.java index c350f232..a97a26c4 100644 --- a/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/ledger/LedgerBlockGeneratingTest.java +++ b/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/ledger/LedgerBlockGeneratingTest.java @@ -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 callback0 = node0.startInit(0, privkey0, initSetting, csProps, csProvider, testDb0, - consolePrompter, !optimized); + AsyncCallback 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 callback1 = node1.startInit(1, privkey1, initSetting, csProps, csProvider, testDb1, - consolePrompter, !optimized); + AsyncCallback 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 callback2 = node2.startInit(2, privkey2, initSetting, csProps, csProvider, testDb2, - consolePrompter, !optimized); + AsyncCallback 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 callback3 = node3.startInit(3, privkey3, initSetting, csProps, csProvider, testDb03, - consolePrompter, !optimized); + AsyncCallback callback3 = node3.startInit(3, privkey3, initSetting, testDb03, consolePrompter, + !optimized); HashDigest ledgerHash0 = callback0.waitReturn(); HashDigest ledgerHash1 = callback1.waitReturn(); diff --git a/source/test/test-integration/src/test/resources/keys/parti2.pub b/source/test/test-integration/src/test/resources/keys/parti2.pub new file mode 100644 index 00000000..dde44b8e --- /dev/null +++ b/source/test/test-integration/src/test/resources/keys/parti2.pub @@ -0,0 +1 @@ +3snPdw7i7PZi6TStiyc6mzjprnNhgs2atSGNS8wPYzhbKaUWGFJt7x \ No newline at end of file diff --git a/source/test/test-integration/src/test/resources/ledger.init b/source/test/test-integration/src/test/resources/ledger.init index d25b002d..9df38d79 100644 --- a/source/test/test-integration/src/test/resources/ledger.init +++ b/source/test/test-integration/src/test/resources/ledger.init @@ -5,6 +5,18 @@ 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 + +#瀵嗙爜鏈嶅姟鎻愪緵鑰呭垪琛紝浠ヨ嫳鏂囬楃偣鈥,鈥濆垎闅旓紱蹇呴』锛 +crypto.service-providers=com.jd.blockchain.crypto.service.classic.ClassicCryptoService, \ +com.jd.blockchain.crypto.service.sm.SMCryptoService + + #鍙備笌鏂圭殑涓暟锛屽悗缁互 cons_parti.id 鍒嗗埆鏍囪瘑姣忎竴涓弬涓庢柟鐨勯厤缃紱 cons_parti.count=4 @@ -49,7 +61,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涓弬涓庢柟鐨勫叕閽ュ唴瀹癸紙鐢眐eygen宸ュ叿鐢熸垚锛夛紱姝ゅ弬鏁颁紭鍏堜簬 pubkey-path 鍙傛暟锛 cons_parti.2.pubkey= #绗2涓弬涓庢柟鐨勫叡璇嗘湇鍔$殑涓绘満鍦板潃锛 diff --git a/source/test/test-integration/src/test/resources/ledger_init_test.init b/source/test/test-integration/src/test/resources/ledger_init_test.init index 9c9959c2..518d88c1 100644 --- a/source/test/test-integration/src/test/resources/ledger_init_test.init +++ b/source/test/test-integration/src/test/resources/ledger_init_test.init @@ -5,6 +5,16 @@ 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 + +#瀵嗙爜鏈嶅姟鎻愪緵鑰呭垪琛紝浠ヨ嫳鏂囬楃偣鈥,鈥濆垎闅旓紱蹇呴』锛 +crypto.service-providers=com.jd.blockchain.crypto.service.classic.ClassicCryptoService, \ +com.jd.blockchain.crypto.service.sm.SMCryptoService + #鍙備笌鏂圭殑涓暟锛屽悗缁互 cons_parti.id 鍒嗗埆鏍囪瘑姣忎竴涓弬涓庢柟鐨勯厤缃紱 cons_parti.count=4 diff --git a/source/test/test-integration/src/test/resources/ledger_init_test_web2.init b/source/test/test-integration/src/test/resources/ledger_init_test_web2.init index 849f223e..73626947 100644 --- a/source/test/test-integration/src/test/resources/ledger_init_test_web2.init +++ b/source/test/test-integration/src/test/resources/ledger_init_test_web2.init @@ -5,6 +5,17 @@ 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 + +#瀵嗙爜鏈嶅姟鎻愪緵鑰呭垪琛紝浠ヨ嫳鏂囬楃偣鈥,鈥濆垎闅旓紱蹇呴』锛 +crypto.service-providers=com.jd.blockchain.crypto.service.classic.ClassicCryptoService, \ +com.jd.blockchain.crypto.service.sm.SMCryptoService + + #鍙備笌鏂圭殑涓暟锛屽悗缁互 cons_parti.id 鍒嗗埆鏍囪瘑姣忎竴涓弬涓庢柟鐨勯厤缃紱 cons_parti.count=4 diff --git a/source/test/test-stp-community/pom.xml b/source/test/test-stp-community/pom.xml deleted file mode 100644 index 12d031b1..00000000 --- a/source/test/test-stp-community/pom.xml +++ /dev/null @@ -1,36 +0,0 @@ - - - - - test - com.jd.blockchain - 0.9.0-SNAPSHOT - - 4.0.0 - - test-stp-community - - test-stp-community - - - UTF-8 - 1.8 - 1.8 - - - - - - com.jd.blockchain - stp-communication - ${project.version} - - - - junit - junit - test - - - diff --git a/source/test/test-stp-community/src/main/java/com/jd/blockchain/stp/commucation/MyMessageExecutor.java b/source/test/test-stp-community/src/main/java/com/jd/blockchain/stp/commucation/MyMessageExecutor.java deleted file mode 100644 index 26656a8b..00000000 --- a/source/test/test-stp-community/src/main/java/com/jd/blockchain/stp/commucation/MyMessageExecutor.java +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Copyright: Copyright 2016-2020 JD.COM All Right Reserved - * FileName: com.jd.blockchain.stp.commucation.MyMessageExecutor - * Author: shaozhuguang - * Department: Y浜嬩笟閮 - * Date: 2019/4/17 涓嬪崍3:38 - * Description: - */ -package com.jd.blockchain.stp.commucation; - -import com.jd.blockchain.stp.communication.MessageExecutor; -import com.jd.blockchain.stp.communication.RemoteSession; - -import java.nio.charset.Charset; - -/** - * - * @author shaozhuguang - * @create 2019/4/17 - * @since 1.0.0 - */ - -public class MyMessageExecutor implements MessageExecutor { - - @Override - public byte[] receive(String key, byte[] data, RemoteSession session) { - String receiveMsg = new String(data, Charset.defaultCharset()); - System.out.printf("receive client {%s} request {%s} \r\n", session.remoteNode().toString(), receiveMsg); - String msg = session.localId() + " -> received !!!"; - return msg.getBytes(Charset.defaultCharset()); - } - - @Override - public REPLY replyType() { - return REPLY.AUTO; - } -} \ No newline at end of file diff --git a/source/test/test-stp-community/src/main/java/com/jd/blockchain/stp/commucation/StpReceiversBoot.java b/source/test/test-stp-community/src/main/java/com/jd/blockchain/stp/commucation/StpReceiversBoot.java deleted file mode 100644 index e725d77b..00000000 --- a/source/test/test-stp-community/src/main/java/com/jd/blockchain/stp/commucation/StpReceiversBoot.java +++ /dev/null @@ -1,75 +0,0 @@ -/** - * Copyright: Copyright 2016-2020 JD.COM All Right Reserved - * FileName: com.jd.blockchain.StpReceiversBoot - * Author: shaozhuguang - * Department: Y浜嬩笟閮 - * Date: 2019/4/18 涓嬪崍3:44 - * Description: - */ -package com.jd.blockchain.stp.commucation; - -import com.jd.blockchain.stp.communication.MessageExecutor; -import com.jd.blockchain.stp.communication.manager.RemoteSessionManager; -import com.jd.blockchain.stp.communication.node.LocalNode; - -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -/** - * - * @author shaozhuguang - * @create 2019/4/18 - * @since 1.0.0 - */ - -public class StpReceiversBoot { - - private int[] listenPorts; - - private final String remoteHost = "127.0.0.1"; - - private ExecutorService threadPool; - - public StpReceiversBoot(int... ports) { - listenPorts = ports; - threadPool = Executors.newFixedThreadPool(ports.length + 2); - } - - public RemoteSessionManager[] start(MessageExecutor messageExecutor) { - - final int totalSessionSize = listenPorts.length; - - CountDownLatch countDownLatch = new CountDownLatch(totalSessionSize); - - RemoteSessionManager[] sessionManagers = new RemoteSessionManager[totalSessionSize]; - for (int i = 0; i < totalSessionSize; i++) { - final int port = listenPorts[i], index = i; - threadPool.execute(() -> { - // 鍒涘缓鏈湴鑺傜偣 - final LocalNode localNode = new LocalNode(remoteHost, port, messageExecutor); - try { - // 鍚姩褰撳墠鑺傜偣 - RemoteSessionManager sessionManager = new RemoteSessionManager(localNode); - sessionManagers[index] = sessionManager; - System.out.printf("Current Node {%s} start success !!! \r\n", localNode.toString()); - } catch (Exception e) { - e.printStackTrace(); - } finally { - countDownLatch.countDown(); - } - }); - } - - // 绛夊緟鎵鏈夎妭鐐瑰惎鍔ㄥ畬鎴 - try { - countDownLatch.await(); - } catch (Exception e) { - e.printStackTrace(); - } - - return sessionManagers; - } - - -} \ No newline at end of file diff --git a/source/test/test-stp-community/src/test/java/com/jd/blockchain/StpReceiversBootTest.java b/source/test/test-stp-community/src/test/java/com/jd/blockchain/StpReceiversBootTest.java deleted file mode 100644 index 1cd6e985..00000000 --- a/source/test/test-stp-community/src/test/java/com/jd/blockchain/StpReceiversBootTest.java +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright: Copyright 2016-2020 JD.COM All Right Reserved - * FileName: com.jd.blockchain.StpReceiversBootTest - * Author: shaozhuguang - * Department: Y浜嬩笟閮 - * Date: 2019/4/18 涓嬪崍3:53 - * Description: - */ -package com.jd.blockchain; - -import com.jd.blockchain.stp.commucation.MyMessageExecutor; -import com.jd.blockchain.stp.commucation.StpReceiversBoot; -import com.jd.blockchain.stp.communication.manager.RemoteSessionManager; -import org.junit.Test; - -/** - * - * @author shaozhuguang - * @create 2019/4/18 - * @since 1.0.0 - */ - -public class StpReceiversBootTest { - - public static final int[] localPorts = new int[]{9900, 9901}; - - @Test - public void test() { - StpReceiversBoot stpReceiversBoot = new StpReceiversBoot(9900, 9901); - RemoteSessionManager[] sessionManagers = stpReceiversBoot.start(new MyMessageExecutor()); - - try { - Thread.sleep(10000); - - // 鍏抽棴鎵鏈夌殑鐩戝惉鍣 - for (RemoteSessionManager sessionManager : sessionManagers) { - sessionManager.connectionManager().closeReceiver(); - } - } catch (Exception e) { - e.printStackTrace(); - } - } -} \ No newline at end of file diff --git a/source/test/test-stp-community/src/test/java/com/jd/blockchain/StpSenderTest.java b/source/test/test-stp-community/src/test/java/com/jd/blockchain/StpSenderTest.java deleted file mode 100644 index 29741ae0..00000000 --- a/source/test/test-stp-community/src/test/java/com/jd/blockchain/StpSenderTest.java +++ /dev/null @@ -1,96 +0,0 @@ -/** - * Copyright: Copyright 2016-2020 JD.COM All Right Reserved - * FileName: com.jd.blockchain.StpSenderTest - * Author: shaozhuguang - * Department: Y浜嬩笟閮 - * Date: 2019/4/18 涓嬪崍3:56 - * Description: - */ -package com.jd.blockchain; - -import com.jd.blockchain.stp.commucation.MyMessageExecutor; -import com.jd.blockchain.stp.commucation.StpReceiversBoot; -import com.jd.blockchain.stp.communication.RemoteSession; -import com.jd.blockchain.stp.communication.callback.CallBackBarrier; -import com.jd.blockchain.stp.communication.callback.CallBackDataListener; -import com.jd.blockchain.stp.communication.manager.RemoteSessionManager; -import com.jd.blockchain.stp.communication.message.LoadMessage; -import com.jd.blockchain.stp.communication.node.RemoteNode; -import org.junit.Test; - -import java.util.Iterator; -import java.util.LinkedList; - -/** - * - * @author shaozhuguang - * @create 2019/4/18 - * @since 1.0.0 - */ - -public class StpSenderTest { - - // 鏈湴鐨勭鍙 - private static final int localPort = 9800; - - // 杩炴帴鐨勮繙绔鍙i泦鍚 - private static final int[] remotePorts = StpReceiversBootTest.localPorts; - - // 鏈湴鑺傜偣淇℃伅 - private static final String localHost = "127.0.0.1"; - - @Test - public void test() { - // 棣栧厛鍚姩鏈湴鑺傜偣 - StpReceiversBoot stpReceiversBoot = new StpReceiversBoot(localPort); - RemoteSessionManager[] sessionManagers = stpReceiversBoot.start(new MyMessageExecutor()); - - // 鏈湴鑺傜偣鍚姩瀹屾垚鍚 - if (sessionManagers != null && sessionManagers.length > 0) { - RemoteSessionManager localSessionManager = sessionManagers[0]; - - // 杩炴帴杩滅鐨勪袱涓妭鐐 - RemoteNode[] remoteNodes = new RemoteNode[]{ - new RemoteNode(localHost, remotePorts[0]), - new RemoteNode(localHost, remotePorts[1]) - }; - - RemoteSession[] remoteSessions = localSessionManager.newSessions(remoteNodes); - - // 鐢熸垚璇锋眰瀵硅薄 - LoadMessage loadMessage = new StpTest.StpLoadMessage(localHost + ":" + localPort); - - // 寮傛鍙戦佸鐞嗚繃绋 - CallBackBarrier callBackBarrier = CallBackBarrier.newCallBackBarrier(remoteSessions.length, 10000); - - // 鍙戦佽姹傝嚦remotes - LinkedList responses = new LinkedList<>(); - for (RemoteSession remoteSession : remoteSessions) { - CallBackDataListener response = remoteSession.asyncRequest(loadMessage, callBackBarrier); - responses.addLast(response); - } - - // 瓒呮椂鍒ゆ柇 - try { - if (callBackBarrier.tryCall()) { - - // 璇存槑缁撴灉宸茬粡鍏ㄩ儴杩斿洖 - // 鎵撳嵃鍑烘墍鏈夌殑缁撴灉 - // 閫氳繃杩唬鍣ㄩ亶鍘嗛摼琛 - Iterator iterator = responses.iterator(); - while (iterator.hasNext()) { - CallBackDataListener response = iterator.next(); - // 鍒ゆ柇鏄惁宸插畬鎴愶紝瀵逛簬娌℃湁瀹屾垚鐨勭洿鎺ユ斁寮冿紙鍥犱负宸茬粡瓒呮椂锛 - if (response.isDone()) { - System.out.printf("Receive Response {%s} {%s} \r\n", - response.remoteNode().toString(), new String(response.getCallBackData())); - } - } - } - Thread.sleep(Integer.MAX_VALUE); - } catch (Exception e) { - e.printStackTrace(); - } - } - } -} \ No newline at end of file diff --git a/source/test/test-stp-community/src/test/java/com/jd/blockchain/StpTest.java b/source/test/test-stp-community/src/test/java/com/jd/blockchain/StpTest.java deleted file mode 100644 index 2e66c8e1..00000000 --- a/source/test/test-stp-community/src/test/java/com/jd/blockchain/StpTest.java +++ /dev/null @@ -1,206 +0,0 @@ -/** - * Copyright: Copyright 2016-2020 JD.COM All Right Reserved - * FileName: com.jd.blockchain.StpTest - * Author: shaozhuguang - * Department: Y浜嬩笟閮 - * Date: 2019/4/11 涓嬪崍3:31 - * Description: - */ -package com.jd.blockchain; - -import com.jd.blockchain.stp.commucation.MyMessageExecutor; -import com.jd.blockchain.stp.communication.RemoteSession; -import com.jd.blockchain.stp.communication.callback.CallBackBarrier; -import com.jd.blockchain.stp.communication.callback.CallBackDataListener; -import com.jd.blockchain.stp.communication.manager.RemoteSessionManager; -import com.jd.blockchain.stp.communication.message.LoadMessage; -import com.jd.blockchain.stp.communication.node.LocalNode; -import com.jd.blockchain.stp.communication.node.RemoteNode; -import org.junit.Before; -import org.junit.Test; - -import java.nio.charset.Charset; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -import static org.junit.Assert.assertNull; - -/** - * - * @author shaozhuguang - * @create 2019/4/11 - * @since 1.0.0 - */ - -public class StpTest { - - private int maxWaitTime = 2000; - - private final String remoteHost = "127.0.0.1"; - - private final int localPort = 9001; - - private final int[] listenPorts = new int[]{9001, 9002, 9003, 9004}; - - private final RemoteSessionManager[] sessionManagers = new RemoteSessionManager[listenPorts.length]; - - private final ExecutorService threadPool = Executors.newFixedThreadPool(6); - - private RemoteSession[] remoteSessions; - - @Before - public void init() { - - System.out.println("---------- listenStart -----------"); - listenStart(); - System.out.println("---------- listenComplete -----------"); - System.out.println("---------- ConnectionStart ----------"); - connectOneOther(); - System.out.println("---------- ConnectionComplete ----------"); - } - - private void listenStart() { - CountDownLatch countDownLatch = new CountDownLatch(listenPorts.length); - - for (int i = 0; i < listenPorts.length; i++) { - final int port = listenPorts[i], index = i; - threadPool.execute(() -> { - // 鍒涘缓鏈湴鑺傜偣 - final LocalNode localNode = new LocalNode(remoteHost, port, new MyMessageExecutor()); - try { - // 鍚姩褰撳墠鑺傜偣 - RemoteSessionManager sessionManager = new RemoteSessionManager(localNode); - sessionManagers[index] = sessionManager; - System.out.printf("Current Node {%s} start success !!! \r\n", localNode.toString()); - } catch (Exception e) { - e.printStackTrace(); - } finally { - countDownLatch.countDown(); - } - }); - } - - // 绛夊緟鎵鏈夎妭鐐瑰惎鍔ㄥ畬鎴 - try { - countDownLatch.await(); - } catch (Exception e) { - e.printStackTrace(); - } - } - - private void connectAllOthers() { - // 鎵鏈夎妭鐐瑰畬鎴愪箣鍚庯紝闇瑕佸惎鍔 - // 鍚姩涓涓妭鐐 - RemoteSessionManager starter = sessionManagers[0]; - - // 褰撳墠鑺傜偣闇瑕佽繛鎺ュ埌鍏朵粬3涓妭鐐 - RemoteNode[] remoteNodes = new RemoteNode[listenPorts.length - 1]; - int index = 0; - for (int port : listenPorts) { - if (port != localPort) { - remoteNodes[index++] = new RemoteNode(remoteHost, port); - } - } - - remoteSessions = starter.newSessions(remoteNodes); - } - - private void connectOneOther() { - // 鎵鏈夎妭鐐瑰畬鎴愪箣鍚庯紝闇瑕佸惎鍔 - // 鍚姩涓涓妭鐐 - RemoteSessionManager starter = sessionManagers[0]; - - // 褰撳墠鑺傜偣闇瑕佽繛鎺ュ埌鍏朵粬3涓妭鐐 - RemoteNode[] remoteNodes = new RemoteNode[1]; - int index = 0; - for (int port : listenPorts) { - if (port != localPort && index < 1) { - remoteNodes[index++] = new RemoteNode(remoteHost, port); - } - } - - remoteSessions = starter.newSessions(remoteNodes); - } - - private void connectOneErrorNode() { - // 鎵鏈夎妭鐐瑰畬鎴愪箣鍚庯紝闇瑕佸惎鍔 - // 鍚姩涓涓妭鐐 - RemoteSessionManager starter = sessionManagers[0]; - - // 褰撳墠鑺傜偣闇瑕佽繛鎺ュ埌鍏朵粬3涓妭鐐 - RemoteNode[] remoteNodes = new RemoteNode[1]; - - remoteNodes[0] = new RemoteNode(remoteHost, 10001); - - remoteSessions = starter.newSessions(remoteNodes); - - assertNull(remoteSessions); - } - - - @Test - public void test() { - - try { - Thread.sleep(3000); - - } catch (Exception e) { - e.printStackTrace(); - } - - // 鐢熸垚璇锋眰瀵硅薄 - LoadMessage loadMessage = new StpLoadMessage(remoteHost + ":" + localPort); - - // 寮傛鍙戦佸鐞嗚繃绋 - CallBackBarrier callBackBarrier = CallBackBarrier.newCallBackBarrier(remoteSessions.length, 10000); - - // 鍙戦佽姹傝嚦remotes - LinkedList responses = new LinkedList<>(); - for (RemoteSession remoteSession : remoteSessions) { - CallBackDataListener response = remoteSession.asyncRequest(loadMessage, callBackBarrier); - responses.addLast(response); - } - - // 瓒呮椂鍒ゆ柇 - try { - if (callBackBarrier.tryCall()) { - - // 璇存槑缁撴灉宸茬粡鍏ㄩ儴杩斿洖 - // 鎵撳嵃鍑烘墍鏈夌殑缁撴灉 - // 閫氳繃杩唬鍣ㄩ亶鍘嗛摼琛 - Iterator iterator = responses.iterator(); - while (iterator.hasNext()) { - CallBackDataListener response = iterator.next(); - // 鍒ゆ柇鏄惁宸插畬鎴愶紝瀵逛簬娌℃湁瀹屾垚鐨勭洿鎺ユ斁寮冿紙鍥犱负宸茬粡瓒呮椂锛 - if (response.isDone()) { - System.out.printf("Receive Response {%s} {%s} \r\n", - response.remoteNode().toString(), new String(response.getCallBackData())); - } - } - } - Thread.sleep(Integer.MAX_VALUE); - } catch (Exception e) { - e.printStackTrace(); - } - - - } - - public static class StpLoadMessage implements LoadMessage { - - private String localInfo; - - public StpLoadMessage(String localInfo) { - this.localInfo = localInfo; - } - - @Override - public byte[] toBytes() { - String msg = localInfo + " -> Send !!!"; - return msg.getBytes(Charset.defaultCharset()); - } - } -} \ No newline at end of file diff --git a/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/LedgerInitCommand.java b/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/LedgerInitCommand.java index f010164d..4947975a 100644 --- a/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/LedgerInitCommand.java +++ b/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/LedgerInitCommand.java @@ -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()); } } diff --git a/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/LedgerInitProcess.java b/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/LedgerInitProcess.java index 1e19232d..ee1b1692 100644 --- a/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/LedgerInitProcess.java +++ b/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/LedgerInitProcess.java @@ -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); } diff --git a/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/LedgerInitProperties.java b/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/LedgerInitProperties.java index 805315f9..2ee028b2 100644 --- a/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/LedgerInitProperties.java +++ b/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/LedgerInitProperties.java @@ -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"; - // // 鍏辫瘑鍙備笌鏂圭殑鍏辫瘑鏈嶅姟鐨勭鍙o紱 - // 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"; // 鍏辫瘑鍙備笌鏂圭殑璐︽湰鍒濆鏈嶅姟鐨勭鍙o紱 @@ -45,70 +40,37 @@ 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"; + + // 瀵嗙爜鏈嶅姟鎻愪緵鑰呭垪琛紝浠ヨ嫳鏂囬楃偣鈥,鈥濆垎闅旓紱蹇呴』锛 + public static final String CRYPTO_SERVICE_PROVIDERS = "crypto.service-providers"; + + public static final String CRYPTO_SERVICE_PROVIDERS_SPLITTER = ","; + private byte[] ledgerSeed; private List consensusParticipants = new ArrayList<>(); + private String consensusProvider; + + private Properties consensusConfig; + + private String[] cryptoProviders; + public byte[] getLedgerSeed() { - return ledgerSeed; + return ledgerSeed.clone(); } - 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() { @@ -118,21 +80,24 @@ public class LedgerInitProperties { public List getConsensusParticipants() { return consensusParticipants; } - - public ConsensusParticipantConfig[] getConsensusParticipantArray() { - return consensusParticipants.toArray(new ConsensusParticipantConfig[consensusParticipants.size()]); + + public String[] getCryptoProviders() { + return cryptoProviders.clone(); + } + + public void setCryptoProviders(String[] cryptoProviders) { + this.cryptoProviders = cryptoProviders; } /** * 杩斿洖鍙備笌鑰咃紱 * - * @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 +128,31 @@ 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); + + // 瑙f瀽鍏辫瘑鐩稿叧鐨勫睘鎬э紱 + 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)); + // 瑙f瀽瀵嗙爜鎻愪緵鑰呭垪琛紱 + String cryptoProviderNames = PropertiesUtils.getProperty(props, CRYPTO_SERVICE_PROVIDERS, true); + String[] cryptoProviders = cryptoProviderNames.split(CRYPTO_SERVICE_PROVIDERS_SPLITTER); + for (int i = 0; i < cryptoProviders.length; i++) { + cryptoProviders[i] = cryptoProviders[i].trim(); + } + initProps.cryptoProviders = cryptoProviders; + + // 瑙f瀽鍙備笌鏂硅妭鐐瑰垪琛紱 + int partCount = getInt(PropertiesUtils.getRequiredProperty(props, PART_COUNT)); if (partCount < 0) { throw new IllegalArgumentException(String.format("Property[%s] is negative!", PART_COUNT)); } @@ -180,62 +165,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 +212,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 +234,7 @@ public class LedgerInitProperties { public void setId(int id) { this.id = id; } - + @Override public String getAddress() { return address; @@ -285,13 +248,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; diff --git a/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/LocalConfig.java b/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/LocalConfig.java index 8696263c..c3ad268e 100644 --- a/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/LocalConfig.java +++ b/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/LocalConfig.java @@ -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 { // 璐︽湰鏁版嵁搴撶殑杩炴帴鍙d护锛 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); - - } } diff --git a/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/Prompter.java b/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/Prompter.java index 663a2c46..b1f589dd 100644 --- a/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/Prompter.java +++ b/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/Prompter.java @@ -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); diff --git a/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/web/LedgerInitializeWebController.java b/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/web/LedgerInitializeWebController.java index 42e1d5d6..4c9af288 100644 --- a/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/web/LedgerInitializeWebController.java +++ b/source/tools/tools-initializer/src/main/java/com/jd/blockchain/tools/initializer/web/LedgerInitializeWebController.java @@ -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,14 +19,17 @@ 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.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; import com.jd.blockchain.crypto.SignatureDigest; import com.jd.blockchain.crypto.SignatureFunction; +import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; +import com.jd.blockchain.crypto.service.sm.SMCryptoService; import com.jd.blockchain.ledger.BlockchainIdentity; import com.jd.blockchain.ledger.BlockchainIdentityData; import com.jd.blockchain.ledger.CryptoSetting; @@ -53,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; @@ -76,8 +80,11 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI 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; @@ -122,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; @@ -148,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..."); @@ -230,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; } @@ -299,7 +309,12 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI } public CryptoSetting createDefaultCryptoSetting() { + CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; + for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { + supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); + } CryptoConfig defCryptoSetting = new CryptoConfig(); + defCryptoSetting.setSupportedProviders(supportedProviders); defCryptoSetting.setAutoVerifyHash(true); defCryptoSetting.setHashAlgorithm(Crypto.getAlgorithm("SHA256")); @@ -318,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 partiList = ledgerProps.getConsensusParticipants(); + ConsensusParticipantConfig[] parties = partiList.toArray(new ConsensusParticipantConfig[partiList.size()]); ConsensusParticipantConfig[] orderedParties = sortAndVerify(parties); initSetting.setConsensusParticipants(orderedParties); @@ -447,7 +464,7 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI List 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)); } } @@ -474,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()); @@ -525,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 { @@ -595,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; } @@ -636,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; } @@ -666,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); @@ -684,7 +702,7 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI /** * 鏍¢獙骞惰褰曟寚瀹氱殑鍙備笌鏂瑰仛鍑虹殑鍐冲畾锛 *

- * 娉細瀵 {@link DecisionResultHandle#setResult(LedgerInitDecision)} + * 娉細瀵 {@link DecisionResultHandle#setValue(LedgerInitDecision)} * 鏂规硶鐨勮皟鐢ㄤ笉鏄嚎绋嬪畨鍏ㄧ殑锛屼絾鐢变簬鏄湪婊¤冻鏈夋晥鎬ф牎楠屼箣鍚庢墠璋冪敤锛屽叿鏈夊箓绛夋э紝鎵浠ヤ笉蹇呭璇ユ柟娉曠殑澶氬璋冪敤杩涜鍚屾锛 * * @param targetDecision @@ -695,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]", @@ -708,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; } @@ -723,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; } /** diff --git a/source/tools/tools-initializer/src/main/resources/local.conf b/source/tools/tools-initializer/src/main/resources/local.conf index 9df0303c..dc7c7347 100644 --- a/source/tools/tools-initializer/src/main/resources/local.conf +++ b/source/tools/tools-initializer/src/main/resources/local.conf @@ -16,10 +16,3 @@ ledger.db.uri=redis://127.0.0.1/0 #璐︽湰鏁版嵁搴撶殑杩炴帴鍙d护锛 ledger.db.pwd= - -#鍏辫瘑鍗忚鐨勫弬鏁伴厤缃紱蹇呴』鍙傛暟锛 -consensus.conf=mq.config - -#鍏辫瘑鍗忚鐨勫疄鐜扮被鍨嬶紱蹇呴』鍙傛暟锛 -consensus.service-provider=com.jd.blockchain.consensus.mq.MsgQueueConsensusProvider - diff --git a/source/utils/utils-common/src/main/java/com/jd/blockchain/utils/ArrayUtils.java b/source/utils/utils-common/src/main/java/com/jd/blockchain/utils/ArrayUtils.java index bb04964e..c787a014 100644 --- a/source/utils/utils-common/src/main/java/com/jd/blockchain/utils/ArrayUtils.java +++ b/source/utils/utils-common/src/main/java/com/jd/blockchain/utils/ArrayUtils.java @@ -1,14 +1,7 @@ package com.jd.blockchain.utils; import java.lang.reflect.Array; -import java.util.Collections; -import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; -import java.util.SortedSet; -import java.util.TreeSet; +import java.util.*; /** * @author haiq @@ -83,5 +76,4 @@ public abstract class ArrayUtils { } return new ReadonlyArrayListWrapper(array, fromIndex, toIndex); } - } diff --git a/source/utils/utils-common/src/main/java/com/jd/blockchain/utils/PropertiesUtils.java b/source/utils/utils-common/src/main/java/com/jd/blockchain/utils/PropertiesUtils.java index ec6f5da9..994272b0 100644 --- a/source/utils/utils-common/src/main/java/com/jd/blockchain/utils/PropertiesUtils.java +++ b/source/utils/utils-common/src/main/java/com/jd/blockchain/utils/PropertiesUtils.java @@ -37,9 +37,10 @@ public abstract class PropertiesUtils { /** * 鍒涘缓閰嶇疆瀵硅薄鐨勫疄渚嬶紝骞朵笖浠庢寚瀹氱殑灞炴ц〃涓垵濮嬪寲瀵瑰簲鐨勫疄渚嬪瓧娈碉紱 + * * @param configClass configClass - * @param properties properties - * @param T + * @param properties properties + * @param T * @return T */ @SuppressWarnings("unchecked") @@ -56,13 +57,10 @@ public abstract class PropertiesUtils { /** * 鍒涘缓閰嶇疆瀵硅薄鐨勫疄渚嬶紝骞朵笖浠庢寚瀹氱殑灞炴ц〃涓垵濮嬪寲瀵瑰簲鐨勫疄渚嬪瓧娈碉紱 * - * @param configClass - * 閰嶇疆瀵硅薄鐨勭被鍨嬶紱 - * @param properties - * 灞炴ц〃锛 - * @param propsPrefix - * 鍦ㄥ睘鎬ц〃涓笌閰嶇疆瀵硅薄鐩稿叧鐨勫睘鎬х殑key鐨勫墠缂锛 - * @param T + * @param configClass 閰嶇疆瀵硅薄鐨勭被鍨嬶紱 + * @param properties 灞炴ц〃锛 + * @param propsPrefix 鍦ㄥ睘鎬ц〃涓笌閰嶇疆瀵硅薄鐩稿叧鐨勫睘鎬х殑key鐨勫墠缂锛 + * @param T * @return T */ public static T createInstance(Class 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 class + * @param configClass 閰嶇疆瀵硅薄鐨勭被鍨嬶紱 + * @param configFilePathPattern properties閰嶇疆鏂囦欢鐨勮矾寰勶紱鍙互鎸囧畾 spring 璧勬簮璺緞琛ㄨ揪寮忥紱 + * @param charset 瀛楃闆嗭紱 + * @param 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); } + /** + * 杩斿洖鎸囧畾鐨勫睘鎬э紱
+ * + * @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()); diff --git a/source/utils/utils-common/src/main/java/com/jd/blockchain/utils/StringUtils.java b/source/utils/utils-common/src/main/java/com/jd/blockchain/utils/StringUtils.java new file mode 100644 index 00000000..5ae1a856 --- /dev/null +++ b/source/utils/utils-common/src/main/java/com/jd/blockchain/utils/StringUtils.java @@ -0,0 +1,24 @@ +package com.jd.blockchain.utils; + +import java.util.regex.Pattern; + +/** + * @Author zhaogw + * date 2018/11/26 20:46 + */ +public class StringUtils { + public static boolean isEmpty(Object str) { + return str == null || "".equals(str); + } + + /* + * 鍒ゆ柇鏄惁涓烘暣鏁 + * @param str 浼犲叆鐨勫瓧绗︿覆 + * @return 鏄暣鏁拌繑鍥瀟rue,鍚﹀垯杩斿洖false + */ + + public static boolean isNumber(String str) { + Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$"); + return pattern.matcher(str).matches(); + } +} \ No newline at end of file diff --git a/source/utils/utils-common/src/main/java/com/jd/blockchain/utils/concurrent/InvocationResult.java b/source/utils/utils-common/src/main/java/com/jd/blockchain/utils/concurrent/InvocationResult.java index 84533ba7..27a7e68f 100644 --- a/source/utils/utils-common/src/main/java/com/jd/blockchain/utils/concurrent/InvocationResult.java +++ b/source/utils/utils-common/src/main/java/com/jd/blockchain/utils/concurrent/InvocationResult.java @@ -2,16 +2,16 @@ package com.jd.blockchain.utils.concurrent; public class InvocationResult { - 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() { diff --git a/source/utils/utils-common/src/main/java/com/jd/blockchain/utils/io/FileUtils.java b/source/utils/utils-common/src/main/java/com/jd/blockchain/utils/io/FileUtils.java index e5ba84f9..77cfca01 100644 --- a/source/utils/utils-common/src/main/java/com/jd/blockchain/utils/io/FileUtils.java +++ b/source/utils/utils-common/src/main/java/com/jd/blockchain/utils/io/FileUtils.java @@ -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 { /** * 浠ラ粯璁ゅ瓧绗﹂泦锛圲TF-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 { /** * 浠ラ粯璁ゅ瓧绗﹂泦锛圲TF-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 锛屽垯鍚炲櫖鍒犻櫎杩囩▼涓殑寮傚父锛屾剰鍛崇潃鏂规硶鍗充究姝e父杩斿洖鏃朵篃鏈夊彲鑳藉垹闄や笉瀹屽叏锛 + * @param silent 鏄惁闈欓粯鍒犻櫎锛涘鏋滀负 true 锛屽垯鍚炲櫖鍒犻櫎杩囩▼涓殑寮傚父锛屾剰鍛崇潃鏂规硶鍗充究姝e父杩斿洖鏃朵篃鏈夊彲鑳藉垹闄や笉瀹屽叏锛 */ public static void deleteFile(File file, boolean silent) { if (file.isFile()) {