From 536cfdf44305e4a7e26d100029169accb65a0869 Mon Sep 17 00:00:00 2001 From: zhaoguangwei Date: Thu, 12 Sep 2019 18:18:34 +0800 Subject: [PATCH 1/8] should use: partiConf.getAddress().toBase58() --- .../com/jd/blockchain/tools/initializer/LedgerInitCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 c39da297..0073a346 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 @@ -103,7 +103,7 @@ public class LedgerInitCommand { // partiAddress = partiConf.getAddress(); // } // } - if (localNodeAddress.equals(partiConf.getAddress())) { + if (localNodeAddress.equals(partiConf.getAddress().toBase58())) { currId = i; } } From e2126851fae49e1a637561e5b7cf4f5f823de886 Mon Sep 17 00:00:00 2001 From: huanghaiquan Date: Mon, 16 Sep 2019 10:30:36 +0800 Subject: [PATCH 2/8] Refactor LedgerAdminInfo as a binary serializable Object; --- .../com/jd/blockchain/consts/DataCodes.java | 2 +- .../ledger/core/EmptyLedgerDataset.java | 4 ++-- .../ledger/core/LedgerAdminDataQuery.java | 4 ++-- .../ledger/core/LedgerAdminDataset.java | 6 +++--- .../ledger/core/LedgerInitializer.java | 11 ++++++++++ .../blockchain/ledger/core/LedgerQuery.java | 5 +++++ .../ledger/core/LedgerRepositoryImpl.java | 21 ++++++++++++++----- .../ledger/core/LedgerAdminDatasetTest.java | 18 ++++++++++++++-- .../jd/blockchain/ledger/LedgerAdminInfo.java | 18 ++++++++++------ .../ledger/LedgerAdminSettings.java | 8 +++++++ .../test/ledger/RolesAuthorizationTest.java | 4 ++-- 11 files changed, 78 insertions(+), 23 deletions(-) create mode 100644 source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/LedgerAdminSettings.java 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 607c051d..279daea0 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 @@ -22,7 +22,7 @@ public interface DataCodes { public static final int DATA_SNAPSHOT = 0x130; -// public static final int LEDGER_ADMIN_DATA = 0x131; + public static final int LEDGER_ADMIN_INFO = 0x131; public static final int TX = 0x200; diff --git a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/EmptyLedgerDataset.java b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/EmptyLedgerDataset.java index 64e08117..3bd87b8d 100644 --- a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/EmptyLedgerDataset.java +++ b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/EmptyLedgerDataset.java @@ -2,7 +2,7 @@ package com.jd.blockchain.ledger.core; import com.jd.blockchain.crypto.HashDigest; import com.jd.blockchain.ledger.AccountHeader; -import com.jd.blockchain.ledger.LedgerAdminInfo; +import com.jd.blockchain.ledger.LedgerAdminSettings; import com.jd.blockchain.ledger.MerkleProof; import com.jd.blockchain.ledger.ParticipantDataQuery; import com.jd.blockchain.ledger.ParticipantNode; @@ -45,7 +45,7 @@ public class EmptyLedgerDataset implements LedgerDataQuery { @Override - public LedgerAdminInfo getAdminInfo() { + public LedgerAdminSettings getAdminInfo() { return null; } diff --git a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerAdminDataQuery.java b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerAdminDataQuery.java index 05c62138..623a47b1 100644 --- a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerAdminDataQuery.java +++ b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerAdminDataQuery.java @@ -1,11 +1,11 @@ package com.jd.blockchain.ledger.core; -import com.jd.blockchain.ledger.LedgerAdminInfo; +import com.jd.blockchain.ledger.LedgerAdminSettings; import com.jd.blockchain.ledger.ParticipantDataQuery; public interface LedgerAdminDataQuery { - LedgerAdminInfo getAdminInfo(); + LedgerAdminSettings getAdminInfo(); ParticipantDataQuery getParticipantDataset(); diff --git a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerAdminDataset.java b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerAdminDataset.java index 9c794f82..f7cf951c 100644 --- a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerAdminDataset.java +++ b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerAdminDataset.java @@ -8,7 +8,7 @@ import com.jd.blockchain.binaryproto.DataContractRegistry; import com.jd.blockchain.crypto.Crypto; import com.jd.blockchain.crypto.HashDigest; import com.jd.blockchain.crypto.HashFunction; -import com.jd.blockchain.ledger.LedgerAdminInfo; +import com.jd.blockchain.ledger.LedgerAdminSettings; import com.jd.blockchain.ledger.LedgerException; import com.jd.blockchain.ledger.LedgerInitSetting; import com.jd.blockchain.ledger.LedgerMetadata; @@ -23,7 +23,7 @@ import com.jd.blockchain.storage.service.VersioningKVStorage; import com.jd.blockchain.utils.Bytes; import com.jd.blockchain.utils.Transactional; -public class LedgerAdminDataset implements Transactional, LedgerAdminDataQuery, LedgerAdminInfo { +public class LedgerAdminDataset implements Transactional, LedgerAdminDataQuery, LedgerAdminSettings { static { DataContractRegistry.register(LedgerMetadata.class); @@ -110,7 +110,7 @@ public class LedgerAdminDataset implements Transactional, LedgerAdminDataQuery, } @Override - public LedgerAdminInfo getAdminInfo() { + public LedgerAdminSettings getAdminInfo() { return this; } diff --git a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerInitializer.java b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerInitializer.java index ddd5a68d..06fce128 100644 --- a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerInitializer.java +++ b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerInitializer.java @@ -8,6 +8,7 @@ import com.jd.blockchain.ledger.BlockchainIdentityData; import com.jd.blockchain.ledger.BlockchainKeypair; import com.jd.blockchain.ledger.DigitalSignature; import com.jd.blockchain.ledger.LedgerAdminInfo; +import com.jd.blockchain.ledger.LedgerAdminSettings; import com.jd.blockchain.ledger.LedgerBlock; import com.jd.blockchain.ledger.LedgerInitException; import com.jd.blockchain.ledger.LedgerInitOperation; @@ -254,6 +255,16 @@ public class LedgerInitializer { public LedgerAdminInfo getAdminInfo(LedgerBlock block) { return null; } + + @Override + public LedgerAdminSettings getAdminSettings() { + return null; + } + + @Override + public LedgerAdminSettings getAdminSettings(LedgerBlock block) { + return null; + } @Override public LedgerBlock getBlock(HashDigest hash) { diff --git a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerQuery.java b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerQuery.java index 0bed1953..748cb212 100644 --- a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerQuery.java +++ b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerQuery.java @@ -2,6 +2,7 @@ package com.jd.blockchain.ledger.core; import com.jd.blockchain.crypto.HashDigest; import com.jd.blockchain.ledger.LedgerAdminInfo; +import com.jd.blockchain.ledger.LedgerAdminSettings; import com.jd.blockchain.ledger.LedgerBlock; public interface LedgerQuery { @@ -53,6 +54,10 @@ public interface LedgerQuery { LedgerAdminInfo getAdminInfo(); LedgerAdminInfo getAdminInfo(LedgerBlock block); + + LedgerAdminSettings getAdminSettings(); + + LedgerAdminSettings getAdminSettings(LedgerBlock block); LedgerBlock getBlock(HashDigest hash); diff --git a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerRepositoryImpl.java b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerRepositoryImpl.java index 55c9d6d6..caba630f 100644 --- a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerRepositoryImpl.java +++ b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerRepositoryImpl.java @@ -7,6 +7,7 @@ import com.jd.blockchain.crypto.HashFunction; import com.jd.blockchain.ledger.BlockBody; import com.jd.blockchain.ledger.CryptoSetting; import com.jd.blockchain.ledger.LedgerAdminInfo; +import com.jd.blockchain.ledger.LedgerAdminSettings; import com.jd.blockchain.ledger.LedgerBlock; import com.jd.blockchain.ledger.LedgerDataSnapshot; import com.jd.blockchain.ledger.LedgerInitSetting; @@ -244,7 +245,7 @@ class LedgerRepositoryImpl implements LedgerRepository { @Override public LedgerAdminInfo getAdminInfo() { - return getAdminInfo(getLatestBlock()); + return getAdminSettings(getLatestBlock()); } private LedgerBlock deserialize(byte[] blockBytes) { @@ -265,7 +266,17 @@ class LedgerRepositoryImpl implements LedgerRepository { } @Override - public LedgerAdminDataset getAdminInfo(LedgerBlock block) { + public LedgerAdminInfo getAdminInfo(LedgerBlock block) { + return getAdminSettings(block); + } + + @Override + public LedgerAdminSettings getAdminSettings() { + return getAdminSettings(getLatestBlock()); + } + + @Override + public LedgerAdminSettings getAdminSettings(LedgerBlock block) { long height = getLatestBlockHeight(); if (height == block.getHeight()) { return latestState.getAdminDataset(); @@ -284,7 +295,7 @@ class LedgerRepositoryImpl implements LedgerRepository { if (height == block.getHeight()) { return latestState.getUserAccountSet(); } - LedgerAdminDataset adminAccount = getAdminInfo(block); + LedgerAdminSettings adminAccount = getAdminSettings(block); return createUserAccountSet(block, adminAccount.getSettings().getCryptoSetting()); } @@ -300,7 +311,7 @@ class LedgerRepositoryImpl implements LedgerRepository { return latestState.getDataAccountSet(); } - LedgerAdminDataset adminAccount = getAdminInfo(block); + LedgerAdminSettings adminAccount = getAdminSettings(block); return createDataAccountSet(block, adminAccount.getSettings().getCryptoSetting()); } @@ -316,7 +327,7 @@ class LedgerRepositoryImpl implements LedgerRepository { return latestState.getContractAccountSet(); } - LedgerAdminDataset adminAccount = getAdminInfo(block); + LedgerAdminSettings adminAccount = getAdminSettings(block); return createContractAccountSet(block, adminAccount.getSettings().getCryptoSetting()); } diff --git a/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/core/LedgerAdminDatasetTest.java b/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/core/LedgerAdminDatasetTest.java index 08c3ec89..684d720a 100644 --- a/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/core/LedgerAdminDatasetTest.java +++ b/source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/core/LedgerAdminDatasetTest.java @@ -10,7 +10,6 @@ import static org.junit.Assert.assertTrue; import java.util.Arrays; import java.util.Random; -import com.jd.blockchain.ledger.*; import org.junit.Test; import com.jd.blockchain.crypto.AddressEncoding; @@ -20,6 +19,21 @@ 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.LedgerAdminInfo; +import com.jd.blockchain.ledger.LedgerAdminSettings; +import com.jd.blockchain.ledger.LedgerMetadata_V2; +import com.jd.blockchain.ledger.LedgerPermission; +import com.jd.blockchain.ledger.LedgerSettings; +import com.jd.blockchain.ledger.ParticipantNode; +import com.jd.blockchain.ledger.ParticipantNodeState; +import com.jd.blockchain.ledger.RolePrivilegeSettings; +import com.jd.blockchain.ledger.RolePrivileges; +import com.jd.blockchain.ledger.RolesPolicy; +import com.jd.blockchain.ledger.TransactionPermission; +import com.jd.blockchain.ledger.UserRoles; +import com.jd.blockchain.ledger.UserRolesSettings; import com.jd.blockchain.ledger.core.CryptoConfig; import com.jd.blockchain.ledger.core.LedgerAdminDataset; import com.jd.blockchain.ledger.core.LedgerConfiguration; @@ -213,7 +227,7 @@ public class LedgerAdminDatasetTest { actualLedgerSettings.getCryptoSetting().getHashAlgorithm()); } - private void verifyRealoadingRoleAuthorizations(LedgerAdminInfo actualAccount, + private void verifyRealoadingRoleAuthorizations(LedgerAdminSettings actualAccount, RolePrivilegeSettings expRolePrivilegeSettings, UserRolesSettings expUserRoleSettings) { // 验证基本信息; RolePrivilegeSettings actualRolePrivileges = actualAccount.getRolePrivileges(); diff --git a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/LedgerAdminInfo.java b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/LedgerAdminInfo.java index b666535e..62a2aa2f 100644 --- a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/LedgerAdminInfo.java +++ b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/LedgerAdminInfo.java @@ -1,17 +1,23 @@ 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; + +@DataContract(code=DataCodes.LEDGER_ADMIN_INFO) public interface LedgerAdminInfo { + @DataField(order=1, refContract = true) LedgerMetadata_V2 getMetadata(); + @DataField(order=2, refContract = true) LedgerSettings getSettings(); - long getParticipantCount(); - + @DataField(order=3, refContract = true, list = true) ParticipantNode[] getParticipants(); - - UserRolesSettings getUserRoles(); - - RolePrivilegeSettings getRolePrivileges(); + + @DataField(order=4, primitiveType = PrimitiveType.INT64) + long getParticipantCount(); } \ No newline at end of file diff --git a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/LedgerAdminSettings.java b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/LedgerAdminSettings.java new file mode 100644 index 00000000..1c8cc9ff --- /dev/null +++ b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/LedgerAdminSettings.java @@ -0,0 +1,8 @@ +package com.jd.blockchain.ledger; + +public interface LedgerAdminSettings extends LedgerAdminInfo { + + UserRolesSettings getUserRoles(); + + RolePrivilegeSettings getRolePrivileges(); +} diff --git a/source/test/test-ledger/src/test/java/test/com/jd/blockchain/test/ledger/RolesAuthorizationTest.java b/source/test/test-ledger/src/test/java/test/com/jd/blockchain/test/ledger/RolesAuthorizationTest.java index 2cd06f96..0e015086 100644 --- a/source/test/test-ledger/src/test/java/test/com/jd/blockchain/test/ledger/RolesAuthorizationTest.java +++ b/source/test/test-ledger/src/test/java/test/com/jd/blockchain/test/ledger/RolesAuthorizationTest.java @@ -192,7 +192,7 @@ public class RolesAuthorizationTest { if (roles == null) { roles = new String[0]; } - UserRoles userRoles = ledger.getAdminInfo().getUserRoles().getUserRoles(address); + UserRoles userRoles = ledger.getAdminSettings().getUserRoles().getUserRoles(address); assertNotNull(userRoles); assertEquals(policy, userRoles.getPolicy()); @@ -206,7 +206,7 @@ public class RolesAuthorizationTest { private void assertRolePermissions(LedgerQuery ledger, String roleName, LedgerPermission[] ledgerPermissions, TransactionPermission[] txPermissions) { - RolePrivilegeSettings roles = ledger.getAdminInfo().getRolePrivileges(); + RolePrivilegeSettings roles = ledger.getAdminSettings().getRolePrivileges(); assertTrue(roles.contains(roleName)); RolePrivileges privileges = roles.getRolePrivilege(roleName); assertEquals(ledgerPermissions.length, privileges.getLedgerPrivilege().getPermissionCount()); From ab0978146e25f6d33ac7a3a53f81f307b31a2502 Mon Sep 17 00:00:00 2001 From: huanghaiquan Date: Mon, 16 Sep 2019 15:05:19 +0800 Subject: [PATCH 3/8] Updated default init configuration template; --- .../main/resources/config/init/ledger.init | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) 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 index 14a95134..e84f24f5 100644 --- a/source/deployment/deployment-peer/src/main/resources/config/init/ledger.init +++ b/source/deployment/deployment-peer/src/main/resources/config/init/ledger.init @@ -7,6 +7,48 @@ ledger.name= #声明的账本创建时间;格式为 “yyyy-MM-dd HH:mm:ss.SSSZ”,表示”年-月-日 时:分:秒:毫秒时区“;例如:“2019-08-01 14:26:58.069+0800”,其中,+0800 表示时区是东8区 created-time=2019-08-01 14:26:58.069+0800 + +#----------------------------------------------- +# 初始的角色名称列表;可选项; +# 角色名称不区分大小写,最长不超过20个字符;多个角色名称之间用半角的逗点“,”分隔; +# 系统会预置一个默认角色“DEFAULT”,所有未指定角色的用户都以赋予该角色的权限;若初始化时未配置默认角色的权限,则为默认角色分配所有权限; +# +# 注:如果声明了角色,但未声明角色对应的权限清单,这会忽略该角色的初始化; +# +#security.roles=DEFAULT, ADMIN, MANAGER, GUEST + +# 赋予角色的账本权限清单;可选项; +# 可选的权限如下; +# AUTHORIZE_ROLES, SET_CONSENSUS, SET_CRYPTO, REGISTER_PARTICIPANT, +# REGISTER_USER, REGISTER_DATA_ACCOUNT, REGISTER_CONTRACT, UPGRADE_CONTRACT, +# SET_USER_ATTRIBUTES, WRITE_DATA_ACCOUNT, +# APPROVE_TX, CONSENSUS_TX +# 多项权限之间用逗点“,”分隔; +# +#security.role.DEFAULT.ledger-privileges=REGISTER_USER, REGISTER_DATA_ACCOUNT + +# 赋予角色的交易权限清单;可选项; +# 可选的权限如下; +# DIRECT_OPERATION, CONTRACT_OPERATION +# 多项权限之间用逗点“,”分隔; +# +#security.role.DEFAULT.tx-privileges=DIRECT_OPERATION, CONTRACT_OPERATION + +# 其它角色的配置示例; +# 系统管理员角色:只能操作全局性的参数配置和用户注册,只能执行直接操作指令; +#security.role.ADMIN.ledger-privileges=CONFIGURE_ROLES, AUTHORIZE_USER_ROLES, SET_CONSENSUS, SET_CRYPTO, REGISTER_PARTICIPANT, REGISTER_USER +#security.role.ADMIN.tx-privileges=DIRECT_OPERATION + +# 业务主管角色:只能够执行账本数据相关的操作,包括注册用户、注册数据账户、注册合约、升级合约、写入数据等;能够执行直接操作指令和调用合约; +#security.role.MANAGER.ledger-privileges=CONFIGURE_ROLES, AUTHORIZE_USER_ROLES, REGISTER_USER, REGISTER_DATA_ACCOUNT, REGISTER_CONTRACT, UPGRADE_CONTRACT, SET_USER_ATTRIBUTES, WRITE_DATA_ACCOUNT, +#security.role.MANAGER.tx-privileges=DIRECT_OPERATION, CONTRACT_OPERATION + +# 访客角色:不具备任何的账本权限,只有数据读取的操作;也只能够通过调用合约来读取数据; +#security.role.GUEST.ledger-privileges= +#security.role.GUEST.tx-privileges=CONTRACT_OPERATION + + +#----------------------------------------------- #共识服务提供者;必须; consensus.service-provider=com.jd.blockchain.consensus.bftsmart.BftsmartConsensusProvider @@ -17,16 +59,36 @@ consensus.conf=bftsmart.config crypto.service-providers=com.jd.blockchain.crypto.service.classic.ClassicCryptoService, \ com.jd.blockchain.crypto.service.sm.SMCryptoService +#从存储中加载账本数据时,是否校验哈希;可选; +crypto.verify-hash=true + +#哈希算法; +crypto.hash-algorithm=SHA256 + #参与方的个数,后续以 cons_parti.id 分别标识每一个参与方的配置; cons_parti.count=4 +#--------------------- #第0个参与方的名称; cons_parti.0.name= #第0个参与方的公钥文件路径; cons_parti.0.pubkey-path= #第0个参与方的公钥内容(由keygen工具生成);此参数优先于 pubkey-path 参数; cons_parti.0.pubkey= + +#第0个参与方的角色清单;可选项; +#cons_parti.0.roles=ADMIN, MANAGER +#第0个参与方的角色权限策略,可选值有:UNION(并集),INTERSECT(交集);可选项; +#cons_parti.0.roles-policy=UNION + +#第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个参与方的账本初始服务的端口; @@ -34,12 +96,27 @@ cons_parti.0.initializer.port=8800 #第0个参与方的账本初始服务是否开启安全连接; cons_parti.0.initializer.secure=false + +#--------------------- #第1个参与方的名称; cons_parti.1.name= #第1个参与方的公钥文件路径; cons_parti.1.pubkey-path= #第1个参与方的公钥内容(由keygen工具生成);此参数优先于 pubkey-path 参数; cons_parti.1.pubkey= + +#第1个参与方的角色清单;可选项; +#cons_parti.1.roles=MANAGER +#第1个参与方的角色权限策略,可选值有:UNION(并集),INTERSECT(交集);可选项; +#cons_parti.1.roles-policy=UNION + +#第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个参与方的账本初始服务的端口; @@ -47,12 +124,26 @@ cons_parti.1.initializer.port=8810 #第1个参与方的账本初始服务是否开启安全连接; cons_parti.1.initializer.secure=false +#--------------------- #第2个参与方的名称; cons_parti.2.name= #第2个参与方的公钥文件路径; cons_parti.2.pubkey-path= #第2个参与方的公钥内容(由keygen工具生成);此参数优先于 pubkey-path 参数; cons_parti.2.pubkey= + +#第2个参与方的角色清单;可选项; +#cons_parti.2.roles=MANAGER +#第2个参与方的角色权限策略,可选值有:UNION(并集),INTERSECT(交集);可选项; +#cons_parti.2.roles-policy=UNION + +#第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个参与方的账本初始服务的端口; @@ -60,12 +151,26 @@ cons_parti.2.initializer.port=8820 #第2个参与方的账本初始服务是否开启安全连接; cons_parti.2.initializer.secure=false +#--------------------- #第3个参与方的名称; cons_parti.3.name= #第3个参与方的公钥文件路径; cons_parti.3.pubkey-path= #第3个参与方的公钥内容(由keygen工具生成);此参数优先于 pubkey-path 参数; cons_parti.3.pubkey= + +#第3个参与方的角色清单;可选项; +#cons_parti.3.roles=GUEST +#第3个参与方的角色权限策略,可选值有:UNION(并集),INTERSECT(交集);可选项; +#cons_parti.3.roles-policy=INTERSECT + +#第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个参与方的账本初始服务的端口; From 539f9e9a0709527136ef9f7fc23c1c5b50f2fa36 Mon Sep 17 00:00:00 2001 From: huanghaiquan Date: Mon, 16 Sep 2019 15:29:42 +0800 Subject: [PATCH 4/8] Fixed bugs that no explicit exception when data contract encoder is not found; --- .../java/com/jd/blockchain/binaryproto/BinaryProtocol.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/source/binary-proto/src/main/java/com/jd/blockchain/binaryproto/BinaryProtocol.java b/source/binary-proto/src/main/java/com/jd/blockchain/binaryproto/BinaryProtocol.java index ddf8e012..c06c6039 100644 --- a/source/binary-proto/src/main/java/com/jd/blockchain/binaryproto/BinaryProtocol.java +++ b/source/binary-proto/src/main/java/com/jd/blockchain/binaryproto/BinaryProtocol.java @@ -42,6 +42,10 @@ public class BinaryProtocol { long version = HeaderEncoder.resolveVersion(bytes); DataContractEncoder encoder = DataContractContext.ENCODER_LOOKUP.lookup(code, version); + if (encoder == null) { + throw new DataContractException( + String.format("No data contract was registered with code[%s] and version[%s]!", code, version)); + } return encoder.decode(bytes.getInputStream()); } From 03eb3c70ed3e0b1ec6233681c8d30d691363efd7 Mon Sep 17 00:00:00 2001 From: shaozhuguang Date: Mon, 16 Sep 2019 17:09:54 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E8=B4=A6=E6=9C=AC=E5=9F=BA=E6=9C=AC=E4=BF=A1=E6=81=AF=E6=A0=88?= =?UTF-8?q?=E6=BA=A2=E5=87=BA=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../contract/contract-maven-plugin/ReadME.MD | 2 +- .../gateway/service/GatewayQueryService.java | 4 +- .../service/GatewayQueryServiceHandler.java | 56 +++--- .../gateway/web/BlockBrowserController.java | 13 +- .../ledger/core/LedgerAdminInfoData.java | 161 ++++++++++++++++++ .../ledger/core/LedgerRepositoryImpl.java | 15 +- ...tSettings.java => LedgerBaseSettings.java} | 2 +- .../sdk/proxy/HttpBlockchainQueryService.java | 14 +- 8 files changed, 227 insertions(+), 40 deletions(-) create mode 100644 source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerAdminInfoData.java rename source/sdk/sdk-base/src/main/java/com/jd/blockchain/sdk/{LedgerInitSettings.java => LedgerBaseSettings.java} (98%) diff --git a/source/contract/contract-maven-plugin/ReadME.MD b/source/contract/contract-maven-plugin/ReadME.MD index fce27392..1cc457b6 100644 --- a/source/contract/contract-maven-plugin/ReadME.MD +++ b/source/contract/contract-maven-plugin/ReadME.MD @@ -35,7 +35,7 @@ + 3)executions->execution->phase:建议使用package及其后续阶段(若不了解phase含义,请自行查阅相关信息); + 4)executions->execution->goals->goal:必须使用compile; + 5)mainClass:必填,该类为需要发布的合约执行类(注意此处是类,不是接口),必须正确配置; - + 6)finalName:必填,最终在编译正常的情况下,会产生{finalName}-jdchain-contract.jar文件,只有该文件是可以发布到JDChain的合约包; + + 6)finalName:必填,最终在编译正常的情况下,会产生{finalName}-JDChain-Contract.jar文件,只有该文件是可以发布到JDChain的合约包; ### 2、执行命令 diff --git a/source/gateway/src/main/java/com/jd/blockchain/gateway/service/GatewayQueryService.java b/source/gateway/src/main/java/com/jd/blockchain/gateway/service/GatewayQueryService.java index d88f49aa..8a856953 100644 --- a/source/gateway/src/main/java/com/jd/blockchain/gateway/service/GatewayQueryService.java +++ b/source/gateway/src/main/java/com/jd/blockchain/gateway/service/GatewayQueryService.java @@ -3,7 +3,7 @@ package com.jd.blockchain.gateway.service; import com.jd.blockchain.crypto.HashDigest; import com.jd.blockchain.ledger.ParticipantNode; import com.jd.blockchain.sdk.ContractSettings; -import com.jd.blockchain.sdk.LedgerInitSettings; +import com.jd.blockchain.sdk.LedgerBaseSettings; /** * queryService only for gateway; @@ -34,7 +34,7 @@ public interface GatewayQueryService { * 账本Hash * @return */ - LedgerInitSettings getLedgerInitSettings(HashDigest ledgerHash); + LedgerBaseSettings getLedgerBaseSettings(HashDigest ledgerHash); /** * 获取账本指定合约信息 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 451beec4..aeedd64c 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 @@ -10,7 +10,7 @@ import com.jd.blockchain.ledger.LedgerAdminInfo; import com.jd.blockchain.ledger.LedgerMetadata; import com.jd.blockchain.ledger.ParticipantNode; import com.jd.blockchain.sdk.ContractSettings; -import com.jd.blockchain.sdk.LedgerInitSettings; +import com.jd.blockchain.sdk.LedgerBaseSettings; import com.jd.blockchain.utils.QueryUtil; import com.jd.blockchain.utils.codec.HexUtils; import com.jd.blockchain.utils.decompiler.utils.DecompilerUtils; @@ -31,30 +31,26 @@ public class GatewayQueryServiceHandler implements GatewayQueryService { @Override public HashDigest[] getLedgersHash(int fromIndex, int count) { - HashDigest ledgersHash[] = peerService.getQueryService().getLedgerHashs(); - int indexAndCount[] = QueryUtil.calFromIndexAndCount(fromIndex,count,ledgersHash.length); - HashDigest ledgersHashNew[] = Arrays.copyOfRange(ledgersHash,indexAndCount[0],indexAndCount[0]+indexAndCount[1]); - return ledgersHashNew; + HashDigest[] ledgersHashs = peerService.getQueryService().getLedgerHashs(); + int[] indexAndCount = QueryUtil.calFromIndexAndCount(fromIndex, count, ledgersHashs.length); + return Arrays.copyOfRange(ledgersHashs, indexAndCount[0], indexAndCount[0] + indexAndCount[1]); } @Override public ParticipantNode[] getConsensusParticipants(HashDigest ledgerHash, int fromIndex, int count) { - ParticipantNode participantNode[] = peerService.getQueryService().getConsensusParticipants(ledgerHash); - int indexAndCount[] = QueryUtil.calFromIndexAndCount(fromIndex,count,participantNode.length); - ParticipantNode participantNodesNew[] = Arrays.copyOfRange(participantNode,indexAndCount[0],indexAndCount[0]+indexAndCount[1]); - return participantNodesNew; + ParticipantNode[] participantNodes = peerService.getQueryService().getConsensusParticipants(ledgerHash); + int[] indexAndCount = QueryUtil.calFromIndexAndCount(fromIndex, count, participantNodes.length); + ParticipantNode[] participantNodesNews = Arrays.copyOfRange(participantNodes, indexAndCount[0], + indexAndCount[0] + indexAndCount[1]); + return participantNodesNews; } @Override - public LedgerInitSettings getLedgerInitSettings(HashDigest ledgerHash) { - - ParticipantNode[] participantNodes = peerService.getQueryService().getConsensusParticipants(ledgerHash); - - LedgerMetadata ledgerMetadata = peerService.getQueryService().getLedgerMetadata(ledgerHash); + public LedgerBaseSettings getLedgerBaseSettings(HashDigest ledgerHash) { LedgerAdminInfo ledgerAdminInfo = peerService.getQueryService().getLedgerAdminInfo(ledgerHash); - return initLedgerInitSettings(participantNodes, ledgerMetadata, ledgerAdminInfo); + return initLedgerBaseSettings(ledgerAdminInfo); } @Override @@ -73,36 +69,38 @@ public class GatewayQueryServiceHandler implements GatewayQueryService { } /** - * 初始化账本配置 + * 初始化账本的基本配置 + * + * @param ledgerAdminInfo + * 账本信息 * - * @param participantNodes - * 参与方列表 - * @param ledgerMetadata - * 账本元数据 * @return */ - private LedgerInitSettings initLedgerInitSettings(ParticipantNode[] participantNodes, LedgerMetadata ledgerMetadata, LedgerAdminInfo ledgerAdminInfo) { - LedgerInitSettings ledgerInitSettings = new LedgerInitSettings(); + private LedgerBaseSettings initLedgerBaseSettings(LedgerAdminInfo ledgerAdminInfo) { + + LedgerMetadata ledgerMetadata = ledgerAdminInfo.getMetadata(); + + LedgerBaseSettings ledgerBaseSettings = new LedgerBaseSettings(); // 设置参与方 - ledgerInitSettings.setParticipantNodes(participantNodes); + ledgerBaseSettings.setParticipantNodes(ledgerAdminInfo.getParticipants()); // 设置共识设置 - ledgerInitSettings.setConsensusSettings(initConsensusSettings(ledgerAdminInfo)); + ledgerBaseSettings.setConsensusSettings(initConsensusSettings(ledgerAdminInfo)); // 设置参与方根Hash - ledgerInitSettings.setParticipantsHash(ledgerMetadata.getParticipantsHash()); + ledgerBaseSettings.setParticipantsHash(ledgerMetadata.getParticipantsHash()); // 设置算法配置 - ledgerInitSettings.setCryptoSetting(ledgerAdminInfo.getSettings().getCryptoSetting()); + ledgerBaseSettings.setCryptoSetting(ledgerAdminInfo.getSettings().getCryptoSetting()); // 设置种子 - ledgerInitSettings.setSeed(initSeed(ledgerMetadata.getSeed())); + ledgerBaseSettings.setSeed(initSeed(ledgerMetadata.getSeed())); // 设置共识协议 - ledgerInitSettings.setConsensusProtocol(ledgerAdminInfo.getSettings().getConsensusProvider()); + ledgerBaseSettings.setConsensusProtocol(ledgerAdminInfo.getSettings().getConsensusProvider()); - return ledgerInitSettings; + return ledgerBaseSettings; } /** 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 7de12a75..a6ef2f9b 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 @@ -5,6 +5,8 @@ import java.util.List; import javax.servlet.http.HttpServletRequest; +import com.jd.blockchain.gateway.service.GatewayQueryService; +import com.jd.blockchain.sdk.LedgerBaseSettings; import com.jd.blockchain.utils.decompiler.utils.DecompilerUtils; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -47,6 +49,9 @@ public class BlockBrowserController implements BlockchainExtendQueryService { @Autowired private PeerService peerService; + @Autowired + private GatewayQueryService gatewayQueryService; + @Autowired private DataRetrievalService dataRetrievalService; @@ -86,10 +91,10 @@ public class BlockBrowserController implements BlockchainExtendQueryService { return peerService.getQueryService().getLedgerMetadata(ledgerHash); } -// @RequestMapping(method = RequestMethod.GET, path = "ledgers/{ledgerHash}/settings") -// public LedgerInitSettings getLedgerInitSettings(@PathVariable(name = "ledgerHash") HashDigest ledgerHash) { -// return gatewayQueryService.getLedgerInitSettings(ledgerHash); -// } + @RequestMapping(method = RequestMethod.GET, path = "ledgers/{ledgerHash}/settings") + public LedgerBaseSettings getLedgerInitSettings(@PathVariable(name = "ledgerHash") HashDigest ledgerHash) { + return gatewayQueryService.getLedgerBaseSettings(ledgerHash); + } @RequestMapping(method = RequestMethod.GET, path = "ledgers/{ledgerHash}/blocks") public LedgerBlock[] getBlocks(@PathVariable(name = "ledgerHash") HashDigest ledgerHash) { diff --git a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerAdminInfoData.java b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerAdminInfoData.java new file mode 100644 index 00000000..6c2660fa --- /dev/null +++ b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerAdminInfoData.java @@ -0,0 +1,161 @@ +package com.jd.blockchain.ledger.core; + +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.crypto.HashFunction; +import com.jd.blockchain.ledger.LedgerMetadata; +import com.jd.blockchain.ledger.*; +import com.jd.blockchain.storage.service.ExPolicyKVStorage; +import com.jd.blockchain.storage.service.VersioningKVStorage; +import com.jd.blockchain.utils.Bytes; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static com.jd.blockchain.ledger.core.LedgerAdminDataset.*; + +/** + * @author shaozhuguang + * @date 2019-09-16 + * + * LedgerAdminInfo的独立实现类,主要用于页面展示,区分 {@link LedgerAdminDataset} + */ +public class LedgerAdminInfoData implements LedgerAdminInfo { + + static { + DataContractRegistry.register(LedgerMetadata.class); + DataContractRegistry.register(LedgerMetadata_V2.class); + } + + private static Logger LOGGER = LoggerFactory.getLogger(LedgerAdminInfoData.class); + + private final Bytes metaPrefix; + + private final Bytes settingPrefix; + + private LedgerMetadata_V2 origMetadata; + + private LedgerAdminDataset.LedgerMetadataInfo metadata; + + /** + * 原来的账本设置; + * + *
+ * 对 LedgerMetadata 修改的新配置不能立即生效,需要达成共识后,在下一次区块计算中才生效; + */ + private LedgerSettings previousSettings; + + /** + * 账本的参与节点; + */ + private ParticipantDataset participants; + + /** + * 账本参数配置; + */ + private LedgerSettings settings; + + private ExPolicyKVStorage storage; + + public LedgerAdminInfoData(HashDigest adminAccountHash, String keyPrefix, ExPolicyKVStorage kvStorage, + VersioningKVStorage versioningKVStorage, boolean readonly) { + + this.metaPrefix = Bytes.fromString(keyPrefix + LEDGER_META_PREFIX); + this.settingPrefix = Bytes.fromString(keyPrefix + LEDGER_SETTING_PREFIX); + this.storage = kvStorage; + this.origMetadata = loadAndVerifyMetadata(adminAccountHash); + this.metadata = new LedgerMetadataInfo(origMetadata); + this.settings = loadAndVerifySettings(metadata.getSettingsHash()); + // 复制记录一份配置作为上一个区块的原始配置,该实例仅供读取,不做修改,也不会回写到存储; + this.previousSettings = new LedgerConfiguration(settings); + + String partiPrefix = keyPrefix + LEDGER_PARTICIPANT_PREFIX; + this.participants = new ParticipantDataset(metadata.getParticipantsHash(), previousSettings.getCryptoSetting(), + partiPrefix, kvStorage, versioningKVStorage, readonly); + } + + private LedgerMetadata_V2 loadAndVerifyMetadata(HashDigest adminAccountHash) { + Bytes key = encodeMetadataKey(adminAccountHash); + byte[] bytes = storage.get(key); + HashFunction hashFunc = Crypto.getHashFunction(adminAccountHash.getAlgorithm()); + if (!hashFunc.verify(adminAccountHash, bytes)) { + String errorMsg = "Verification of the hash for ledger metadata failed! --[HASH=" + key + "]"; + LOGGER.error(errorMsg); + throw new LedgerException(errorMsg); + } + return deserializeMetadata(bytes); + } + + + private LedgerSettings loadAndVerifySettings(HashDigest settingsHash) { + if (settingsHash == null) { + return null; + } + Bytes key = encodeSettingsKey(settingsHash); + byte[] bytes = storage.get(key); + HashFunction hashFunc = Crypto.getHashFunction(settingsHash.getAlgorithm()); + if (!hashFunc.verify(settingsHash, bytes)) { + String errorMsg = "Verification of the hash for ledger setting failed! --[HASH=" + key + "]"; + LOGGER.error(errorMsg); + throw new LedgerException(errorMsg); + } + return deserializeSettings(bytes); + } + + private Bytes encodeMetadataKey(HashDigest metadataHash) { + return metaPrefix.concat(metadataHash); + } + + private LedgerMetadata_V2 deserializeMetadata(byte[] bytes) { + return BinaryProtocol.decode(bytes); + } + + private LedgerSettings deserializeSettings(byte[] bytes) { + return BinaryProtocol.decode(bytes); + } + + private Bytes encodeSettingsKey(HashDigest settingsHash) { + return settingPrefix.concat(settingsHash); + } + + /** + * 返回元数据配置信息 + * + * @return + */ + @Override + public LedgerMetadata_V2 getMetadata() { + return metadata; + } + + /** + * 返回当前设置的账本配置; + * + * @return + */ + @Override + public LedgerSettings getSettings() { + return settings; + } + + /** + * 返回当前参与方的数量 + * + * @return + */ + @Override + public long getParticipantCount() { + return participants.getParticipantCount(); + } + + /** + * 返回当前参与方列表 + * + * @return + */ + @Override + public ParticipantNode[] getParticipants() { + return participants.getParticipants(); + } +} diff --git a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerRepositoryImpl.java b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerRepositoryImpl.java index caba630f..90890e4e 100644 --- a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerRepositoryImpl.java +++ b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerRepositoryImpl.java @@ -245,7 +245,7 @@ class LedgerRepositoryImpl implements LedgerRepository { @Override public LedgerAdminInfo getAdminInfo() { - return getAdminSettings(getLatestBlock()); + return createAdminData(getLatestBlock()); } private LedgerBlock deserialize(byte[] blockBytes) { @@ -267,7 +267,7 @@ class LedgerRepositoryImpl implements LedgerRepository { @Override public LedgerAdminInfo getAdminInfo(LedgerBlock block) { - return getAdminSettings(block); + return createAdminData(block); } @Override @@ -285,6 +285,17 @@ class LedgerRepositoryImpl implements LedgerRepository { return createAdminDataset(block); } + /** + * 生成LedgerAdminInfoData对象 + * 该对象主要用于页面展示 + * + * @param block + * @return + */ + private LedgerAdminInfoData createAdminData(LedgerBlock block) { + return new LedgerAdminInfoData(block.getAdminAccountHash(), keyPrefix, exPolicyStorage, versioningStorage, true); + } + private LedgerAdminDataset createAdminDataset(LedgerBlock block) { return new LedgerAdminDataset(block.getAdminAccountHash(), keyPrefix, exPolicyStorage, versioningStorage, true); } 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/LedgerBaseSettings.java similarity index 98% rename from source/sdk/sdk-base/src/main/java/com/jd/blockchain/sdk/LedgerInitSettings.java rename to source/sdk/sdk-base/src/main/java/com/jd/blockchain/sdk/LedgerBaseSettings.java index d4f287ab..7cc58594 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/LedgerBaseSettings.java @@ -14,7 +14,7 @@ import com.jd.blockchain.ledger.ParticipantNode; * @since 1.0.0 * */ -public class LedgerInitSettings { +public class LedgerBaseSettings { /** * 账本初始化种子 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 838ad829..ccd5ceec 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 @@ -8,6 +8,8 @@ import com.jd.blockchain.transaction.BlockchainQueryService; import com.jd.blockchain.utils.http.*; import com.jd.blockchain.utils.web.client.WebResponseConverterFactory; import com.jd.blockchain.sdk.converters.HashDigestToStringConverter; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; /** * 作为内部使用的适配接口,用于声明 HTTP 协议的服务请求; @@ -194,6 +196,17 @@ public interface HttpBlockchainQueryService extends BlockchainExtendQueryService @Override long getAdditionalContractCount(@PathParam(name="ledgerHash", converter=HashDigestToStringConverter.class) HashDigest ledgerHash); + + /** + * 获取账本信息; + * + * @param ledgerHash + * @return 账本对象;如果不存在,则返回 null; + */ + @HttpAction(method=HttpMethod.GET, path="ledgers/{ledgerHash}/admininfo") + @Override + LedgerAdminInfo getLedgerAdminInfo(@PathParam(name="ledgerHash", converter=HashDigestToStringConverter.class) HashDigest ledgerHash); + /** * 返回指定账本的参与列表 * @@ -204,7 +217,6 @@ public interface HttpBlockchainQueryService extends BlockchainExtendQueryService @Override ParticipantNode[] getConsensusParticipants(@PathParam(name="ledgerHash", converter=HashDigestToStringConverter.class) HashDigest ledgerHash); - /** * 返回指定账本的元数据 * From f1cb118dc5b953e0212b6e2c3a395ec87aed6d34 Mon Sep 17 00:00:00 2001 From: shaozhuguang Date: Mon, 16 Sep 2019 17:52:11 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9LedgerAdminInfoData?= =?UTF-8?q?=E5=8C=85=E8=A3=85=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ledger/core/LedgerAdminInfoData.java | 116 ++---------------- .../ledger/core/LedgerRepositoryImpl.java | 8 +- 2 files changed, 16 insertions(+), 108 deletions(-) diff --git a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerAdminInfoData.java b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerAdminInfoData.java index 6c2660fa..06837128 100644 --- a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerAdminInfoData.java +++ b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerAdminInfoData.java @@ -1,19 +1,6 @@ package com.jd.blockchain.ledger.core; -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.crypto.HashFunction; -import com.jd.blockchain.ledger.LedgerMetadata; import com.jd.blockchain.ledger.*; -import com.jd.blockchain.storage.service.ExPolicyKVStorage; -import com.jd.blockchain.storage.service.VersioningKVStorage; -import com.jd.blockchain.utils.Bytes; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import static com.jd.blockchain.ledger.core.LedgerAdminDataset.*; /** * @author shaozhuguang @@ -23,100 +10,15 @@ import static com.jd.blockchain.ledger.core.LedgerAdminDataset.*; */ public class LedgerAdminInfoData implements LedgerAdminInfo { - static { - DataContractRegistry.register(LedgerMetadata.class); - DataContractRegistry.register(LedgerMetadata_V2.class); - } - - private static Logger LOGGER = LoggerFactory.getLogger(LedgerAdminInfoData.class); - - private final Bytes metaPrefix; - - private final Bytes settingPrefix; - - private LedgerMetadata_V2 origMetadata; - - private LedgerAdminDataset.LedgerMetadataInfo metadata; - /** - * 原来的账本设置; + * 包装类型 + * 将LedgerAdminInfo重新封装,用于页面显示 * - *
- * 对 LedgerMetadata 修改的新配置不能立即生效,需要达成共识后,在下一次区块计算中才生效; */ - private LedgerSettings previousSettings; - - /** - * 账本的参与节点; - */ - private ParticipantDataset participants; - - /** - * 账本参数配置; - */ - private LedgerSettings settings; - - private ExPolicyKVStorage storage; - - public LedgerAdminInfoData(HashDigest adminAccountHash, String keyPrefix, ExPolicyKVStorage kvStorage, - VersioningKVStorage versioningKVStorage, boolean readonly) { - - this.metaPrefix = Bytes.fromString(keyPrefix + LEDGER_META_PREFIX); - this.settingPrefix = Bytes.fromString(keyPrefix + LEDGER_SETTING_PREFIX); - this.storage = kvStorage; - this.origMetadata = loadAndVerifyMetadata(adminAccountHash); - this.metadata = new LedgerMetadataInfo(origMetadata); - this.settings = loadAndVerifySettings(metadata.getSettingsHash()); - // 复制记录一份配置作为上一个区块的原始配置,该实例仅供读取,不做修改,也不会回写到存储; - this.previousSettings = new LedgerConfiguration(settings); - - String partiPrefix = keyPrefix + LEDGER_PARTICIPANT_PREFIX; - this.participants = new ParticipantDataset(metadata.getParticipantsHash(), previousSettings.getCryptoSetting(), - partiPrefix, kvStorage, versioningKVStorage, readonly); - } - - private LedgerMetadata_V2 loadAndVerifyMetadata(HashDigest adminAccountHash) { - Bytes key = encodeMetadataKey(adminAccountHash); - byte[] bytes = storage.get(key); - HashFunction hashFunc = Crypto.getHashFunction(adminAccountHash.getAlgorithm()); - if (!hashFunc.verify(adminAccountHash, bytes)) { - String errorMsg = "Verification of the hash for ledger metadata failed! --[HASH=" + key + "]"; - LOGGER.error(errorMsg); - throw new LedgerException(errorMsg); - } - return deserializeMetadata(bytes); - } - - - private LedgerSettings loadAndVerifySettings(HashDigest settingsHash) { - if (settingsHash == null) { - return null; - } - Bytes key = encodeSettingsKey(settingsHash); - byte[] bytes = storage.get(key); - HashFunction hashFunc = Crypto.getHashFunction(settingsHash.getAlgorithm()); - if (!hashFunc.verify(settingsHash, bytes)) { - String errorMsg = "Verification of the hash for ledger setting failed! --[HASH=" + key + "]"; - LOGGER.error(errorMsg); - throw new LedgerException(errorMsg); - } - return deserializeSettings(bytes); - } - - private Bytes encodeMetadataKey(HashDigest metadataHash) { - return metaPrefix.concat(metadataHash); - } - - private LedgerMetadata_V2 deserializeMetadata(byte[] bytes) { - return BinaryProtocol.decode(bytes); - } - - private LedgerSettings deserializeSettings(byte[] bytes) { - return BinaryProtocol.decode(bytes); - } + private LedgerAdminInfo ledgerAdminInfo; - private Bytes encodeSettingsKey(HashDigest settingsHash) { - return settingPrefix.concat(settingsHash); + public LedgerAdminInfoData(LedgerAdminInfo ledgerAdminInfo) { + this.ledgerAdminInfo = ledgerAdminInfo; } /** @@ -126,7 +28,7 @@ public class LedgerAdminInfoData implements LedgerAdminInfo { */ @Override public LedgerMetadata_V2 getMetadata() { - return metadata; + return ledgerAdminInfo.getMetadata(); } /** @@ -136,7 +38,7 @@ public class LedgerAdminInfoData implements LedgerAdminInfo { */ @Override public LedgerSettings getSettings() { - return settings; + return ledgerAdminInfo.getSettings(); } /** @@ -146,7 +48,7 @@ public class LedgerAdminInfoData implements LedgerAdminInfo { */ @Override public long getParticipantCount() { - return participants.getParticipantCount(); + return ledgerAdminInfo.getParticipantCount(); } /** @@ -156,6 +58,6 @@ public class LedgerAdminInfoData implements LedgerAdminInfo { */ @Override public ParticipantNode[] getParticipants() { - return participants.getParticipants(); + return ledgerAdminInfo.getParticipants(); } } diff --git a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerRepositoryImpl.java b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerRepositoryImpl.java index 90890e4e..5a35488a 100644 --- a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerRepositoryImpl.java +++ b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerRepositoryImpl.java @@ -293,9 +293,15 @@ class LedgerRepositoryImpl implements LedgerRepository { * @return */ private LedgerAdminInfoData createAdminData(LedgerBlock block) { - return new LedgerAdminInfoData(block.getAdminAccountHash(), keyPrefix, exPolicyStorage, versioningStorage, true); + return new LedgerAdminInfoData(createAdminDataset(block)); } + /** + * 生成LedgerAdminDataset对象 + * + * @param block + * @return + */ private LedgerAdminDataset createAdminDataset(LedgerBlock block) { return new LedgerAdminDataset(block.getAdminAccountHash(), keyPrefix, exPolicyStorage, versioningStorage, true); } From c849affc4f7ca866f17992e58b0297de30caf481 Mon Sep 17 00:00:00 2001 From: shaozhuguang Date: Mon, 16 Sep 2019 18:26:55 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9Gateway=E5=AF=B9=E5=BA=94?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E8=B7=AF=E5=BE=84=E5=8F=8A?= =?UTF-8?q?LedgerSettings=E6=B3=A8=E5=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gateway/boot/GatewayBooter.java | 1 - .../gateway/GatewayServerBooter.java | 33 +++++++++++++++++-- .../peer/web/ManagementController.java | 3 +- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/source/deployment/deployment-gateway/src/main/java/com/jd/blockchain/gateway/boot/GatewayBooter.java b/source/deployment/deployment-gateway/src/main/java/com/jd/blockchain/gateway/boot/GatewayBooter.java index f6c637ad..4c9d5537 100644 --- a/source/deployment/deployment-gateway/src/main/java/com/jd/blockchain/gateway/boot/GatewayBooter.java +++ b/source/deployment/deployment-gateway/src/main/java/com/jd/blockchain/gateway/boot/GatewayBooter.java @@ -18,7 +18,6 @@ public class GatewayBooter { writePID(); GatewayServerBooter.main(args); } catch (Exception e) { - e.printStackTrace(); System.err.println("Error!!! --[" + e.getClass().getName() + "] " + e.getMessage()); } } diff --git a/source/gateway/src/main/java/com/jd/blockchain/gateway/GatewayServerBooter.java b/source/gateway/src/main/java/com/jd/blockchain/gateway/GatewayServerBooter.java index 76c39b61..bdc8e881 100644 --- a/source/gateway/src/main/java/com/jd/blockchain/gateway/GatewayServerBooter.java +++ b/source/gateway/src/main/java/com/jd/blockchain/gateway/GatewayServerBooter.java @@ -2,6 +2,7 @@ package com.jd.blockchain.gateway; import java.io.File; import java.io.InputStream; +import java.net.URL; import java.util.ArrayList; import java.util.List; @@ -23,10 +24,14 @@ import com.jd.blockchain.utils.ConsoleUtils; public class GatewayServerBooter { + private static final String DEFAULT_GATEWAY_PROPS = "application-gw.properties"; + // 当前参与方在初始化配置中的参与方列表的编号; private static final String HOST_ARG = "-c"; + //sp;针对spring.config.location这个参数进行包装; private static final String SPRING_CF_LOCATION = BaseConstant.SPRING_CF_LOCATION; + // 是否输出调试信息; private static final String DEBUG_OPT = "-debug"; @@ -57,11 +62,20 @@ public class GatewayServerBooter { }else { //if no the config file, then should tip as follows. but it's not a good feeling, so we create it by inputStream; ConsoleUtils.info("no param:-sp, format: -sp /x/xx.properties, use the default application-gw.properties "); - ClassPathResource configResource = new ClassPathResource("application-gw.properties"); + ClassPathResource configResource = new ClassPathResource(DEFAULT_GATEWAY_PROPS); InputStream in = configResource.getInputStream(); - File targetFile = new File(System.getProperty("user.dir")+File.separator+"conf"+File.separator+"application-gw.properties"); + + // 将文件写入至config目录下 + String configPath = bootPath() + "config" + File.separator + DEFAULT_GATEWAY_PROPS; + File targetFile = new File(configPath); + + // 先将原来文件删除再Copy + if (targetFile.exists()) { + FileUtils.forceDelete(targetFile); + } + FileUtils.copyInputStreamToFile(in, targetFile); - springConfigLocation = "file:"+targetFile.getAbsolutePath(); + springConfigLocation = "file:" + targetFile.getAbsolutePath(); } // 启动服务器; @@ -147,4 +161,17 @@ public class GatewayServerBooter { return appCtx; } + private static String bootPath() throws Exception { + URL url = GatewayServerBooter.class.getProtectionDomain().getCodeSource().getLocation(); + String currPath = java.net.URLDecoder.decode(url.getPath(), "UTF-8"); + // 处理打包至SpringBoot问题 + if (currPath.contains("!/")) { + currPath = currPath.substring(5, currPath.indexOf("!/")); + } + if (currPath.endsWith(".jar")) { + currPath = currPath.substring(0, currPath.lastIndexOf("/") + 1); + } + System.out.printf("Current Project Boot Path = %s \r\n", currPath); + return new File(currPath).getParent() + File.separator; + } } \ No newline at end of file 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 cf4b8bac..9e771a33 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 @@ -111,7 +111,8 @@ public class ManagementController implements LedgerBindingConfigAware, PeerManag DataContractRegistry.register(BftsmartConsensusSettings.class); DataContractRegistry.register(BftsmartNodeSettings.class); -// DataContractRegistry.register(LedgerAdminDataQuery.class); + DataContractRegistry.register(LedgerAdminInfo.class); + DataContractRegistry.register(LedgerSettings.class); } From ae87c7a35f93efec77b42b679b8fef677c6fc2e3 Mon Sep 17 00:00:00 2001 From: shaozhuguang Date: Tue, 17 Sep 2019 10:38:42 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9LedgerAdminInfoData?= =?UTF-8?q?=E6=9E=84=E9=80=A0=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ledger/core/LedgerAdminInfoData.java | 45 +++++++++++++++---- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerAdminInfoData.java b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerAdminInfoData.java index 06837128..a0a74190 100644 --- a/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerAdminInfoData.java +++ b/source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/LedgerAdminInfoData.java @@ -11,14 +11,43 @@ import com.jd.blockchain.ledger.*; public class LedgerAdminInfoData implements LedgerAdminInfo { /** - * 包装类型 - * 将LedgerAdminInfo重新封装,用于页面显示 + * 元数据 + */ + private LedgerMetadata_V2 metadata; + + /** + * 账本配置 + * + */ + private LedgerSettings ledgerSettings; + + /** + * 参与方数量 + * + */ + private long participantCount; + + /** + * 参与方 * */ - private LedgerAdminInfo ledgerAdminInfo; + private ParticipantNode[] participantNodes; + /** + * 包装构造方法 + * + * @param ledgerAdminInfo + */ public LedgerAdminInfoData(LedgerAdminInfo ledgerAdminInfo) { - this.ledgerAdminInfo = ledgerAdminInfo; + this(ledgerAdminInfo.getMetadata(), ledgerAdminInfo.getSettings(), + ledgerAdminInfo.getParticipantCount(), ledgerAdminInfo.getParticipants()); + } + + public LedgerAdminInfoData(LedgerMetadata_V2 metadata, LedgerSettings ledgerSettings, long participantCount, ParticipantNode[] participantNodes) { + this.metadata = metadata; + this.ledgerSettings = ledgerSettings; + this.participantCount = participantCount; + this.participantNodes = participantNodes; } /** @@ -28,7 +57,7 @@ public class LedgerAdminInfoData implements LedgerAdminInfo { */ @Override public LedgerMetadata_V2 getMetadata() { - return ledgerAdminInfo.getMetadata(); + return this.metadata; } /** @@ -38,7 +67,7 @@ public class LedgerAdminInfoData implements LedgerAdminInfo { */ @Override public LedgerSettings getSettings() { - return ledgerAdminInfo.getSettings(); + return this.ledgerSettings; } /** @@ -48,7 +77,7 @@ public class LedgerAdminInfoData implements LedgerAdminInfo { */ @Override public long getParticipantCount() { - return ledgerAdminInfo.getParticipantCount(); + return this.participantCount; } /** @@ -58,6 +87,6 @@ public class LedgerAdminInfoData implements LedgerAdminInfo { */ @Override public ParticipantNode[] getParticipants() { - return ledgerAdminInfo.getParticipants(); + return this.participantNodes; } }