# Conflicts: # source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/core/ContractInvokingTest.java # source/ledger/ledger-model/src/main/java/com/jd/blockchain/transaction/BlockchainOperationFactory.javatags/1.1.0
| @@ -51,17 +51,20 @@ public interface DataCodes { | |||
| public static final int TX_OP_CONTRACT_EVENT_SEND = 0x340; | |||
| public static final int TX_RESPONSE = 0x350; | |||
| public static final int TX_OP_PARTICIPANT_REG = 0x350; | |||
| public static final int TX_OP_PARTICIPANT_STATE_UPDATE = 0x351; | |||
| public static final int TX_OP_RESULT = 0x360; | |||
| public static final int TX_RESPONSE = 0x360; | |||
| public static final int TX_OP_RESULT = 0x370; | |||
| public static final int TX_OP_ROLE_CONFIGURE = 0x370; | |||
| public static final int TX_OP_ROLE_CONFIGURE = 0x371; | |||
| public static final int TX_OP_ROLE_CONFIGURE_ENTRY = 0x371; | |||
| public static final int TX_OP_ROLE_CONFIGURE_ENTRY = 0x372; | |||
| public static final int TX_OP_USER_ROLES_AUTHORIZE = 0x372; | |||
| public static final int TX_OP_USER_ROLES_AUTHORIZE = 0x373; | |||
| public static final int TX_OP_USER_ROLE_AUTHORIZE_ENTRY = 0x373; | |||
| public static final int TX_OP_USER_ROLE_AUTHORIZE_ENTRY = 0x374; | |||
| // enum types of permissions; | |||
| public static final int ENUM_TX_PERMISSION = 0x401; | |||
| @@ -97,9 +100,20 @@ public interface DataCodes { | |||
| // public static final int METADATA_CONSENSUS_SETTING = 0x631; | |||
| // | |||
| // public static final int METADATA_PARTICIPANT_INFO = 0x640; | |||
| // | |||
| // public static final int METADATA_CRYPTO_SETTING = 0x642; | |||
| public static final int METADATA_CRYPTO_SETTING = 0x642; | |||
| // public static final int METADATA_CONSENSUS_NODE = 0x630; | |||
| public static final int METADATA_CONSENSUS_SETTING = 0x631; | |||
| public static final int METADATA_PARTICIPANT_INFO = 0x640; | |||
| public static final int METADATA_PARTICIPANT_STATE_INFO = 0x641; | |||
| public static final int METADATA_CRYPTO_SETTING = 0x642; | |||
| public static final int METADATA_CRYPTO_SETTING_PROVIDER = 0x643; | |||
| // public static final int ACCOUNT = 0x700; | |||
| @@ -126,6 +140,8 @@ public interface DataCodes { | |||
| public static final int ENUM_TYPE_BYTES_VALUE_TYPE = 0xB23; | |||
| public static final int ENUM_TYPE_PARTICIPANT_NODE_STATE = 0xB24; | |||
| public static final int DIGITALSIGNATURE = 0xB30; | |||
| public static final int DIGITALSIGNATURE_BODY = 0xB31; | |||
| @@ -2,11 +2,18 @@ package com.jd.blockchain.consensus.bftsmart; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import java.util.HashMap; | |||
| import java.util.Map; | |||
| import java.util.Properties; | |||
| import com.jd.blockchain.consensus.ConsensusProviders; | |||
| import com.jd.blockchain.consensus.NodeSettings; | |||
| import com.jd.blockchain.ledger.ParticipantInfo; | |||
| import com.jd.blockchain.ledger.ParticipantNode; | |||
| import com.jd.blockchain.tools.keygen.KeyGenCommand; | |||
| import com.jd.blockchain.utils.Bytes; | |||
| import com.jd.blockchain.utils.PropertiesUtils; | |||
| import com.jd.blockchain.utils.Property; | |||
| import com.jd.blockchain.utils.codec.Base58Utils; | |||
| import com.jd.blockchain.utils.io.BytesUtils; | |||
| import com.jd.blockchain.utils.net.NetworkAddress; | |||
| @@ -64,6 +71,8 @@ public class BftsmartConsensusSettingsBuilder implements ConsensusSettingsBuilde | |||
| */ | |||
| public static final String CONSENSUS_SECURE_PATTERN = "system.server.%s.network.secure"; | |||
| public static final String BFTSMART_PROVIDER = "com.jd.blockchain.consensus.bftsmart.BftsmartConsensusProvider"; | |||
| private static Properties CONFIG_TEMPLATE; | |||
| @@ -164,6 +173,30 @@ public class BftsmartConsensusSettingsBuilder implements ConsensusSettingsBuilde | |||
| return config; | |||
| } | |||
| @Override | |||
| public Bytes updateSettings(Bytes oldConsensusSettings, ParticipantInfo participantInfo) { | |||
| //update consensus setting through node and system config two aspects | |||
| BftsmartConsensusSettings consensusSettings = (BftsmartConsensusSettings) ConsensusProviders.getProvider(BFTSMART_PROVIDER).getSettingsFactory().getConsensusSettingsEncoder().decode(oldConsensusSettings.toBytes()); | |||
| Property[] systemConfigs = systemConfigs(consensusSettings.getSystemConfigs()); | |||
| BftsmartNodeSettings[] nodeSettings = nodeSettings(consensusSettings.getNodes(), participantInfo); | |||
| BftsmartConsensusConfig bftsmartConsensusConfig = new BftsmartConsensusConfig(nodeSettings, systemConfigs); | |||
| // for(int i = 0 ;i < bftsmartConsensusConfig.getNodes().length; i++) { | |||
| // System.out.printf("id = %d, host = %s, port = %d\r\n", bftsmartConsensusConfig.getNodes()[i].getId(), bftsmartConsensusConfig.getNodes()[i].getNetworkAddress().getHost(), bftsmartConsensusConfig.getNodes()[i].getNetworkAddress().getPort()); | |||
| // } | |||
| // | |||
| // for(int i = 0 ;i < bftsmartConsensusConfig.getSystemConfigs().length; i++) { | |||
| // System.out.printf("property name = %s, property value = %s\r\n",bftsmartConsensusConfig.getSystemConfigs()[i].getName(), bftsmartConsensusConfig.getSystemConfigs()[i].getValue()); | |||
| // } | |||
| return new Bytes(ConsensusProviders.getProvider(BFTSMART_PROVIDER).getSettingsFactory().getConsensusSettingsEncoder().encode(bftsmartConsensusConfig)); | |||
| } | |||
| private static String keyOfNode(String pattern, int id) { | |||
| return String.format(pattern, id); | |||
| } | |||
| @@ -224,4 +257,52 @@ public class BftsmartConsensusSettingsBuilder implements ConsensusSettingsBuilde | |||
| PropertiesUtils.setValues(props, bftsmartSettings.getSystemConfigs()); | |||
| } | |||
| /** | |||
| * | |||
| * update system.servers.num property | |||
| * | |||
| */ | |||
| private Property[] systemConfigs(Property[] systemConfigs) { | |||
| Map<String, Property> propertyMap = convert2Map(systemConfigs); | |||
| int serverNum = Integer.parseInt(propertyMap.get("system.servers.num").getValue()); | |||
| propertyMap.put("system.servers.num", new Property("system.servers.num", String.valueOf(serverNum + 1))); | |||
| return convert2Array(propertyMap); | |||
| } | |||
| private Map<String, Property> convert2Map(Property[] properties) { | |||
| Map<String, Property> propertyMap = new HashMap<>(); | |||
| for (Property property : properties) { | |||
| propertyMap.put(property.getName(), property); | |||
| } | |||
| return propertyMap; | |||
| } | |||
| private Property[] convert2Array(Map<String, Property> map) { | |||
| Property[] properties = new Property[map.size()]; | |||
| int index = 0; | |||
| for (Map.Entry<String, Property> entry : map.entrySet()) { | |||
| properties[index++] = entry.getValue(); | |||
| } | |||
| return properties; | |||
| } | |||
| /** | |||
| * | |||
| * update node setting | |||
| * | |||
| */ | |||
| private BftsmartNodeSettings[] nodeSettings(NodeSettings[] nodeSettings, ParticipantInfo participantInfo) { | |||
| BftsmartNodeConfig bftsmartNodeConfig = new BftsmartNodeConfig(participantInfo.getPubKey(), nodeSettings.length, participantInfo.getNetworkAddress()); | |||
| BftsmartNodeSettings[] bftsmartNodeSettings = new BftsmartNodeSettings[nodeSettings.length + 1]; | |||
| for (int i = 0; i < nodeSettings.length; i++) { | |||
| bftsmartNodeSettings[i] = (BftsmartNodeSettings)nodeSettings[i]; | |||
| } | |||
| bftsmartNodeSettings[nodeSettings.length] = bftsmartNodeConfig; | |||
| return bftsmartNodeSettings; | |||
| } | |||
| } | |||
| @@ -1,6 +1,8 @@ | |||
| package com.jd.blockchain.consensus; | |||
| import com.jd.blockchain.ledger.ParticipantInfo; | |||
| import com.jd.blockchain.ledger.ParticipantNode; | |||
| import com.jd.blockchain.utils.Bytes; | |||
| import java.util.Properties; | |||
| @@ -16,6 +18,8 @@ public interface ConsensusSettingsBuilder { | |||
| * @return | |||
| */ | |||
| ConsensusSettings createSettings(Properties props, ParticipantNode[] participantNodes); | |||
| Bytes updateSettings(Bytes oldConsensusSettings, ParticipantInfo participantInfo); | |||
| Properties createPropertiesTemplate(); | |||
| @@ -8,6 +8,7 @@ | |||
| */ | |||
| package com.jd.blockchain.consensus.mq; | |||
| import com.jd.blockchain.consensus.ConsensusProviders; | |||
| import com.jd.blockchain.consensus.ConsensusSettings; | |||
| import com.jd.blockchain.consensus.ConsensusSettingsBuilder; | |||
| import com.jd.blockchain.consensus.NodeSettings; | |||
| @@ -22,10 +23,12 @@ import com.jd.blockchain.consensus.mq.settings.MsgQueueNodeSettings; | |||
| import com.jd.blockchain.crypto.AddressEncoding; | |||
| import com.jd.blockchain.crypto.KeyGenUtils; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.ledger.ParticipantInfo; | |||
| import com.jd.blockchain.ledger.ParticipantNode; | |||
| import com.jd.blockchain.utils.Bytes; | |||
| import com.jd.blockchain.utils.PropertiesUtils; | |||
| import com.jd.blockchain.utils.codec.Base58Utils; | |||
| import com.jd.blockchain.utils.io.BytesEncoder; | |||
| import com.jd.blockchain.utils.io.BytesUtils; | |||
| import com.jd.blockchain.utils.io.FileUtils; | |||
| import com.jd.blockchain.utils.net.NetworkAddress; | |||
| @@ -82,6 +85,8 @@ public class MsgQueueConsensusSettingsBuilder implements ConsensusSettingsBuilde | |||
| public static final String MSG_QUEUE_BLOCK_MAXDELAY = "system.msg.queue.block.maxdelay"; | |||
| public static final String MSG_QUEUE_PROVIDER = "com.jd.blockchain.consensus.mq.MsgQueueConsensusProvider"; | |||
| private static Properties CONFIG_TEMPLATE; | |||
| static { | |||
| @@ -145,6 +150,48 @@ public class MsgQueueConsensusSettingsBuilder implements ConsensusSettingsBuilde | |||
| return consensusConfig; | |||
| } | |||
| private MsgQueueNodeSettings[] nodeSettings(NodeSettings[] nodeSettings, ParticipantInfo participantInfo) { | |||
| MsgQueueNodeSettings msgQueueNodeSettings = new MsgQueueNodeConfig(); | |||
| ((MsgQueueNodeConfig) msgQueueNodeSettings).setAddress(AddressEncoding.generateAddress(participantInfo.getPubKey()).toBase58()); | |||
| ((MsgQueueNodeConfig) msgQueueNodeSettings).setPubKey(participantInfo.getPubKey()); | |||
| MsgQueueNodeSettings[] msgQueuetNodeSettings = new MsgQueueNodeSettings[nodeSettings.length + 1]; | |||
| for (int i = 0; i < nodeSettings.length; i++) { | |||
| msgQueuetNodeSettings[i] = (MsgQueueNodeSettings)nodeSettings[i]; | |||
| } | |||
| msgQueuetNodeSettings[nodeSettings.length] = msgQueueNodeSettings; | |||
| return msgQueuetNodeSettings; | |||
| } | |||
| @Override | |||
| public Bytes updateSettings(Bytes oldConsensusSettings, ParticipantInfo participantInfo) { | |||
| BytesEncoder<ConsensusSettings> consensusEncoder = ConsensusProviders.getProvider(MSG_QUEUE_PROVIDER).getSettingsFactory().getConsensusSettingsEncoder(); | |||
| MsgQueueConsensusSettings consensusSettings = (MsgQueueConsensusSettings) consensusEncoder.decode(oldConsensusSettings.toBytes()); | |||
| MsgQueueNodeSettings[] nodeSettings = nodeSettings(consensusSettings.getNodes(), participantInfo); | |||
| MsgQueueConsensusConfig msgQueueConsensusConfig = new MsgQueueConsensusConfig(); | |||
| for (int i = 0; i < nodeSettings.length; i++) { | |||
| msgQueueConsensusConfig.addNodeSettings(nodeSettings[i]); | |||
| } | |||
| msgQueueConsensusConfig.setBlockSettings(consensusSettings.getBlockSettings()); | |||
| msgQueueConsensusConfig.setNetworkSettings(consensusSettings.getNetworkSettings()); | |||
| // for(int i = 0 ;i < msgQueueConsensusConfig.getNodes().length; i++) { | |||
| // System.out.printf("node addr = %s\r\n", msgQueueConsensusConfig.getNodes()[i].getAddress()); | |||
| // } | |||
| return new Bytes(consensusEncoder.encode(msgQueueConsensusConfig)); | |||
| } | |||
| @Override | |||
| public Properties createPropertiesTemplate() { | |||
| return PropertiesUtils.cloneFrom(CONFIG_TEMPLATE); | |||
| @@ -10,24 +10,7 @@ import com.jd.blockchain.crypto.HashDigest; | |||
| import com.jd.blockchain.crypto.KeyGenUtils; | |||
| import com.jd.blockchain.crypto.PrivKey; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.ledger.BlockchainIdentity; | |||
| import com.jd.blockchain.ledger.BlockchainIdentityData; | |||
| import com.jd.blockchain.ledger.BlockchainKeyGenerator; | |||
| import com.jd.blockchain.ledger.BlockchainKeypair; | |||
| import com.jd.blockchain.ledger.ContractCodeDeployOperation; | |||
| import com.jd.blockchain.ledger.ContractEventSendOperation; | |||
| import com.jd.blockchain.ledger.DataAccountKVSetOperation; | |||
| import com.jd.blockchain.ledger.DataAccountRegisterOperation; | |||
| import com.jd.blockchain.ledger.EndpointRequest; | |||
| import com.jd.blockchain.ledger.NodeRequest; | |||
| import com.jd.blockchain.ledger.Operation; | |||
| import com.jd.blockchain.ledger.PreparedTransaction; | |||
| 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.TransactionTemplate; | |||
| import com.jd.blockchain.ledger.UserRegisterOperation; | |||
| import com.jd.blockchain.ledger.*; | |||
| import com.jd.blockchain.sdk.BlockchainService; | |||
| import com.jd.blockchain.sdk.client.GatewayServiceFactory; | |||
| import com.jd.blockchain.utils.Bytes; | |||
| @@ -111,6 +94,8 @@ public enum ContractDeployExeUtil { | |||
| DataContractRegistry.register(ContractEventSendOperation.class); | |||
| DataContractRegistry.register(DataAccountRegisterOperation.class); | |||
| DataContractRegistry.register(UserRegisterOperation.class); | |||
| DataContractRegistry.register(ParticipantRegisterOperation.class); | |||
| DataContractRegistry.register(ParticipantStateUpdateOperation.class); | |||
| } | |||
| public BlockchainService initBcsrv(String host, int port) { | |||
| @@ -5,5 +5,5 @@ PEER=$(ls $HOME/system | grep deployment-peer-) | |||
| if [ ! -n "$PEER" ]; then | |||
| echo "Peer Is Null !!!" | |||
| else | |||
| nohup java -jar -server -Xmx2g -Xms2g -Dpeer.log=$HOME $HOME/system/$PEER -home=$HOME -c $HOME/config/ledger-binding.conf -p 7080 $* >$HOME/bin/peer.out 2>&1 & | |||
| nohup java -jar -server -Xmx512m -Xms512m -Dpeer.log=$HOME $HOME/system/$PEER -home=$HOME -c $HOME/config/ledger-binding.conf -p 7080 $* >$HOME/bin/peer.out 2>&1 & | |||
| fi | |||
| @@ -40,6 +40,11 @@ | |||
| <artifactId>contract-framework</artifactId> | |||
| <version>${project.version}</version> | |||
| </dependency> | |||
| <dependency> | |||
| <groupId>com.jd.blockchain</groupId> | |||
| <artifactId>consensus-framework</artifactId> | |||
| <version>${project.version}</version> | |||
| </dependency> | |||
| <dependency> | |||
| <groupId>com.jd.blockchain</groupId> | |||
| <artifactId>contract-jvm</artifactId> | |||
| @@ -6,18 +6,11 @@ import java.util.List; | |||
| import java.util.Map; | |||
| import java.util.concurrent.ConcurrentHashMap; | |||
| import com.jd.blockchain.ledger.core.handles.*; | |||
| import org.springframework.stereotype.Component; | |||
| import com.jd.blockchain.ledger.LedgerException; | |||
| import com.jd.blockchain.ledger.Operation; | |||
| import com.jd.blockchain.ledger.core.handles.ContractCodeDeployOperationHandle; | |||
| import com.jd.blockchain.ledger.core.handles.DataAccountKVSetOperationHandle; | |||
| import com.jd.blockchain.ledger.core.handles.DataAccountRegisterOperationHandle; | |||
| import com.jd.blockchain.ledger.core.handles.JVMContractEventSendOperationHandle; | |||
| import com.jd.blockchain.ledger.core.handles.LedgerInitOperationHandle; | |||
| import com.jd.blockchain.ledger.core.handles.RolesConfigureOperationHandle; | |||
| import com.jd.blockchain.ledger.core.handles.UserAuthorizeOperationHandle; | |||
| import com.jd.blockchain.ledger.core.handles.UserRegisterOperationHandle; | |||
| import com.jd.blockchain.transaction.ContractCodeDeployOpTemplate; | |||
| import com.jd.blockchain.transaction.ContractEventSendOpTemplate; | |||
| import com.jd.blockchain.transaction.DataAccountKVSetOpTemplate; | |||
| @@ -50,6 +43,10 @@ public class DefaultOperationHandleRegisteration implements OperationHandleRegis | |||
| registerDefaultHandle(new ContractCodeDeployOperationHandle()); | |||
| registerDefaultHandle(new JVMContractEventSendOperationHandle()); | |||
| registerDefaultHandle(new ParticipantRegisterOperationHandle()); | |||
| registerDefaultHandle(new ParticipantStateUpdateOperationHandle()); | |||
| } | |||
| private static void registerDefaultHandle(OperationHandle handle) { | |||
| @@ -268,6 +268,8 @@ public class LedgerAdminDataset implements Transactional, LedgerAdminDataQuery, | |||
| * | |||
| * @return | |||
| */ | |||
| @Override | |||
| public LedgerSettings getSettings() { | |||
| return settings; | |||
| } | |||
| @@ -309,6 +311,16 @@ public class LedgerAdminDataset implements Transactional, LedgerAdminDataQuery, | |||
| participants.addConsensusParticipant(participant); | |||
| } | |||
| /** | |||
| * 更新参与方的状态参数; | |||
| * | |||
| * @param participant | |||
| */ | |||
| public void updateParticipant(ParticipantNode participant) { | |||
| participants.updateConsensusParticipant(participant); | |||
| } | |||
| @Override | |||
| public boolean isUpdated() { | |||
| return updated || participants.isUpdated() || rolePrivileges.isUpdated() || userRoles.isUpdated(); | |||
| @@ -1,24 +0,0 @@ | |||
| //package com.jd.blockchain.ledger.core; | |||
| // | |||
| //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.ledger.CryptoSetting; | |||
| //import com.jd.blockchain.utils.Bytes; | |||
| // | |||
| //@DataContract(code = DataCodes.METADATA_LEDGER_SETTING) | |||
| //public interface LedgerSetting { | |||
| // | |||
| // @DataField(order=0, primitiveType=PrimitiveType.TEXT) | |||
| // String getConsensusProvider(); | |||
| // | |||
| // @DataField(order=1, primitiveType=PrimitiveType.BYTES) | |||
| // Bytes getConsensusSetting(); | |||
| // | |||
| // @DataField(order=2, refContract=true) | |||
| // CryptoSetting getCryptoSetting(); | |||
| // | |||
| //// PrivilegeModelSetting getPrivilegesModelSetting(); | |||
| // | |||
| //} | |||
| @@ -3,6 +3,7 @@ package com.jd.blockchain.ledger.core; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.ledger.ParticipantNode; | |||
| import com.jd.blockchain.utils.Bytes; | |||
| import com.jd.blockchain.ledger.ParticipantNodeState; | |||
| /** | |||
| * 参与方证书数据对象; | |||
| @@ -16,6 +17,7 @@ public class ParticipantCertData implements ParticipantNode { | |||
| private Bytes address; | |||
| private String name; | |||
| private PubKey pubKey; | |||
| private ParticipantNodeState participantNodeState; | |||
| public ParticipantCertData() { | |||
| } | |||
| @@ -25,12 +27,14 @@ public class ParticipantCertData implements ParticipantNode { | |||
| this.address = participantNode.getAddress(); | |||
| this.name = participantNode.getName(); | |||
| this.pubKey = participantNode.getPubKey(); | |||
| this.participantNodeState = participantNode.getParticipantNodeState(); | |||
| } | |||
| public ParticipantCertData(Bytes address, String name, PubKey pubKey) { | |||
| public ParticipantCertData(Bytes address, String name, PubKey pubKey, ParticipantNodeState participantNodeState) { | |||
| this.address = address; | |||
| this.name = name; | |||
| this.pubKey = pubKey; | |||
| this.participantNodeState = participantNodeState; | |||
| } | |||
| @Override | |||
| @@ -48,6 +52,7 @@ public class ParticipantCertData implements ParticipantNode { | |||
| return pubKey; | |||
| } | |||
| @Override | |||
| public int getId() { | |||
| return id; | |||
| } | |||
| @@ -55,4 +60,14 @@ public class ParticipantCertData implements ParticipantNode { | |||
| public void setId(int id) { | |||
| this.id = id; | |||
| } | |||
| @Override | |||
| public ParticipantNodeState getParticipantNodeState() { | |||
| return participantNodeState; | |||
| } | |||
| public void setParticipantNodeState(ParticipantNodeState participantNodeState) { | |||
| this.participantNodeState = participantNodeState; | |||
| } | |||
| } | |||
| @@ -76,6 +76,25 @@ public class ParticipantDataset implements Transactional, MerkleProvable, Partic | |||
| } | |||
| } | |||
| /** | |||
| * 更新共识参与方的状态信息; <br> | |||
| * | |||
| * @param participant | |||
| */ | |||
| public void updateConsensusParticipant(ParticipantNode participant) { | |||
| Bytes key = encodeKey(participant.getAddress()); | |||
| byte[] participantBytes = BinaryProtocol.encode(participant, ParticipantNode.class); | |||
| long version = dataset.getVersion(key); | |||
| if (version < 0) { | |||
| throw new LedgerException("Participant not exist, update failed!"); | |||
| } | |||
| long nv = dataset.setValue(key, participantBytes, version); | |||
| if (nv < 0) { | |||
| throw new LedgerException("Participant update failed!"); | |||
| } | |||
| } | |||
| private Bytes encodeKey(Bytes address) { | |||
| return address; | |||
| } | |||
| @@ -0,0 +1,88 @@ | |||
| package com.jd.blockchain.ledger.core.handles; | |||
| import com.jd.blockchain.crypto.AddressEncoding; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.ledger.*; | |||
| import com.jd.blockchain.ledger.core.*; | |||
| import com.jd.blockchain.transaction.UserRegisterOpTemplate; | |||
| import com.jd.blockchain.utils.Bytes; | |||
| public class ParticipantRegisterOperationHandle extends AbstractLedgerOperationHandle<ParticipantRegisterOperation> { | |||
| public ParticipantRegisterOperationHandle() { | |||
| super(ParticipantRegisterOperation.class); | |||
| } | |||
| @Override | |||
| protected void doProcess(ParticipantRegisterOperation op, LedgerDataset newBlockDataset, | |||
| TransactionRequestExtension requestContext, LedgerDataQuery previousBlockDataset, | |||
| OperationHandleContext handleContext, LedgerService ledgerService) { | |||
| // 权限校验; | |||
| SecurityPolicy securityPolicy = SecurityContext.getContextUsersPolicy(); | |||
| securityPolicy.checkEndpointPermission(LedgerPermission.REGISTER_PARTICIPANT, MultiIDsPolicy.AT_LEAST_ONE); | |||
| ParticipantRegisterOperation participantRegOp = (ParticipantRegisterOperation) op; | |||
| LedgerAdminDataset adminAccountDataSet = newBlockDataset.getAdminDataset(); | |||
| ParticipantInfo participantInfo = new ParticipantInfoData(participantRegOp.getParticipantName(), participantRegOp.getParticipantIdentity().getPubKey(), participantRegOp.getNetworkAddress()); | |||
| ParticipantNode participantNode = new PartNode((int)(adminAccountDataSet.getParticipantCount()), participantInfo.getName(), participantInfo.getPubKey(), ParticipantNodeState.REGISTED); | |||
| //add new participant as consensus node | |||
| adminAccountDataSet.addParticipant(participantNode); | |||
| // Build UserRegisterOperation, reg participant as user | |||
| UserRegisterOperation userRegOp = new UserRegisterOpTemplate(participantRegOp.getParticipantIdentity()); | |||
| handleContext.handle(userRegOp); | |||
| } | |||
| private static class PartNode implements ParticipantNode { | |||
| private int id; | |||
| private Bytes address; | |||
| private String name; | |||
| private PubKey pubKey; | |||
| private ParticipantNodeState participantNodeState; | |||
| public PartNode(int id, String name, PubKey pubKey, ParticipantNodeState participantNodeState) { | |||
| this.id = id; | |||
| this.name = name; | |||
| this.pubKey = pubKey; | |||
| this.address = AddressEncoding.generateAddress(pubKey); | |||
| this.participantNodeState = participantNodeState; | |||
| } | |||
| @Override | |||
| public int getId() { | |||
| return id; | |||
| } | |||
| @Override | |||
| public Bytes getAddress() { | |||
| return address; | |||
| } | |||
| @Override | |||
| public String getName() { | |||
| return name; | |||
| } | |||
| @Override | |||
| public PubKey getPubKey() { | |||
| return pubKey; | |||
| } | |||
| @Override | |||
| public ParticipantNodeState getParticipantNodeState() { | |||
| return participantNodeState; | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,103 @@ | |||
| package com.jd.blockchain.ledger.core.handles; | |||
| import com.jd.blockchain.consensus.ConsensusProvider; | |||
| import com.jd.blockchain.consensus.ConsensusProviders; | |||
| import com.jd.blockchain.crypto.AddressEncoding; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.ledger.*; | |||
| import com.jd.blockchain.ledger.core.*; | |||
| import com.jd.blockchain.utils.Bytes; | |||
| public class ParticipantStateUpdateOperationHandle extends AbstractLedgerOperationHandle<ParticipantStateUpdateOperation> { | |||
| public ParticipantStateUpdateOperationHandle() { | |||
| super(ParticipantStateUpdateOperation.class); | |||
| } | |||
| @Override | |||
| protected void doProcess(ParticipantStateUpdateOperation op, LedgerDataset newBlockDataset, | |||
| TransactionRequestExtension requestContext, LedgerDataQuery previousBlockDataset, | |||
| OperationHandleContext handleContext, LedgerService ledgerService) { | |||
| // 权限校验; | |||
| SecurityPolicy securityPolicy = SecurityContext.getContextUsersPolicy(); | |||
| securityPolicy.checkEndpointPermission(LedgerPermission.REGISTER_PARTICIPANT, MultiIDsPolicy.AT_LEAST_ONE); | |||
| ParticipantStateUpdateOperation stateUpdateOperation = (ParticipantStateUpdateOperation) op; | |||
| LedgerAdminDataset adminAccountDataSet = newBlockDataset.getAdminDataset(); | |||
| ConsensusProvider provider = ConsensusProviders.getProvider(adminAccountDataSet.getSettings().getConsensusProvider()); | |||
| ParticipantNode[] participants = adminAccountDataSet.getParticipants(); | |||
| ParticipantNode participantNode = null; | |||
| for(int i = 0; i < participants.length; i++) { | |||
| if (stateUpdateOperation.getParticipantIdentity().getPubKey().equals(participants[i].getPubKey())) { | |||
| participantNode = new PartNode(participants[i].getId(), participants[i].getName(), participants[i].getPubKey(), ParticipantNodeState.CONSENSUSED); | |||
| break; | |||
| } | |||
| } | |||
| //update consensus setting | |||
| ParticipantInfo participantInfo = new ParticipantInfoData(participantNode.getName(), participantNode.getPubKey(), stateUpdateOperation.getNetworkAddress()); | |||
| Bytes newConsensusSettings = provider.getSettingsFactory().getConsensusSettingsBuilder().updateSettings(adminAccountDataSet.getSettings().getConsensusSetting(), participantInfo); | |||
| LedgerSettings ledgerSetting = new LedgerConfiguration(adminAccountDataSet.getSettings().getConsensusProvider(), | |||
| newConsensusSettings, adminAccountDataSet.getPreviousSetting().getCryptoSetting()); | |||
| adminAccountDataSet.setLedgerSetting(ledgerSetting); | |||
| adminAccountDataSet.updateParticipant(participantNode); | |||
| } | |||
| private static class PartNode implements ParticipantNode { | |||
| private int id; | |||
| private Bytes address; | |||
| private String name; | |||
| private PubKey pubKey; | |||
| private ParticipantNodeState participantNodeState; | |||
| public PartNode(int id, String name, PubKey pubKey, ParticipantNodeState participantNodeState) { | |||
| this.id = id; | |||
| this.name = name; | |||
| this.pubKey = pubKey; | |||
| this.address = AddressEncoding.generateAddress(pubKey); | |||
| this.participantNodeState = participantNodeState; | |||
| } | |||
| @Override | |||
| public int getId() { | |||
| return id; | |||
| } | |||
| @Override | |||
| public Bytes getAddress() { | |||
| return address; | |||
| } | |||
| @Override | |||
| public String getName() { | |||
| return name; | |||
| } | |||
| @Override | |||
| public PubKey getPubKey() { | |||
| return pubKey; | |||
| } | |||
| @Override | |||
| public ParticipantNodeState getParticipantNodeState() { | |||
| return participantNodeState; | |||
| } | |||
| } | |||
| } | |||
| @@ -17,33 +17,13 @@ import java.io.InputStream; | |||
| import java.util.Random; | |||
| import com.jd.blockchain.utils.io.BytesUtils; | |||
| import com.jd.blockchain.ledger.*; | |||
| import org.junit.Test; | |||
| import org.mockito.Mockito; | |||
| import com.jd.blockchain.binaryproto.BinaryProtocol; | |||
| import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
| import com.jd.blockchain.crypto.HashDigest; | |||
| import com.jd.blockchain.ledger.BlockchainKeyGenerator; | |||
| import com.jd.blockchain.ledger.BlockchainKeypair; | |||
| import com.jd.blockchain.ledger.BytesData; | |||
| import com.jd.blockchain.ledger.BytesValue; | |||
| import com.jd.blockchain.ledger.DataAccountRegisterOperation; | |||
| import com.jd.blockchain.ledger.EndpointRequest; | |||
| import com.jd.blockchain.ledger.KVDataEntry; | |||
| import com.jd.blockchain.ledger.LedgerBlock; | |||
| import com.jd.blockchain.ledger.LedgerInitSetting; | |||
| import com.jd.blockchain.ledger.LedgerPermission; | |||
| import com.jd.blockchain.ledger.LedgerTransaction; | |||
| import com.jd.blockchain.ledger.NodeRequest; | |||
| import com.jd.blockchain.ledger.OperationResult; | |||
| import com.jd.blockchain.ledger.TransactionContent; | |||
| import com.jd.blockchain.ledger.TransactionContentBody; | |||
| import com.jd.blockchain.ledger.TransactionPermission; | |||
| import com.jd.blockchain.ledger.TransactionRequest; | |||
| import com.jd.blockchain.ledger.TransactionRequestBuilder; | |||
| import com.jd.blockchain.ledger.TransactionResponse; | |||
| import com.jd.blockchain.ledger.TransactionState; | |||
| import com.jd.blockchain.ledger.UserRegisterOperation; | |||
| import com.jd.blockchain.ledger.core.DefaultOperationHandleRegisteration; | |||
| import com.jd.blockchain.ledger.core.LedgerDataQuery; | |||
| import com.jd.blockchain.ledger.core.LedgerDataset; | |||
| @@ -77,6 +57,9 @@ public class ContractInvokingTest { | |||
| DataContractRegistry.register(TransactionResponse.class); | |||
| DataContractRegistry.register(UserRegisterOperation.class); | |||
| DataContractRegistry.register(DataAccountRegisterOperation.class); | |||
| DataContractRegistry.register(ParticipantNode.class); | |||
| DataContractRegistry.register(ParticipantRegisterOperation.class); | |||
| DataContractRegistry.register(ParticipantStateUpdateOperation.class); | |||
| } | |||
| private static final String LEDGER_KEY_PREFIX = "LDG://"; | |||
| @@ -10,6 +10,7 @@ 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; | |||
| @@ -19,19 +20,6 @@ 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.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.RolePrivilegeSettings; | |||
| import com.jd.blockchain.ledger.RolePrivileges; | |||
| import com.jd.blockchain.ledger.RolesPolicy; | |||
| import com.jd.blockchain.ledger.TransactionPermission; | |||
| import com.jd.blockchain.ledger.UserRolesSettings; | |||
| import com.jd.blockchain.ledger.UserRoles; | |||
| import com.jd.blockchain.ledger.core.CryptoConfig; | |||
| import com.jd.blockchain.ledger.core.LedgerAdminDataset; | |||
| import com.jd.blockchain.ledger.core.LedgerConfiguration; | |||
| @@ -62,6 +50,7 @@ public class LedgerAdminDatasetTest { | |||
| parties[i].setHostAddress(new NetworkAddress("192.168.10." + (10 + i), 10010 + 10 * i)); | |||
| parties[i].setName("Participant[" + i + "]"); | |||
| parties[i].setPubKey(bckeys[i].getPubKey()); | |||
| parties[i].setParticipantState(ParticipantNodeState.CONSENSUSED); | |||
| } | |||
| ConsensusParticipantData[] parties1 = Arrays.copyOf(parties, 4); | |||
| initSetting.setConsensusParticipants(parties1); | |||
| @@ -6,6 +6,7 @@ import static org.junit.Assert.assertEquals; | |||
| import java.util.Arrays; | |||
| import java.util.Random; | |||
| import com.jd.blockchain.ledger.*; | |||
| import org.junit.Before; | |||
| import org.junit.Test; | |||
| @@ -17,10 +18,6 @@ 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; | |||
| import com.jd.blockchain.ledger.LedgerInitSetting; | |||
| import com.jd.blockchain.ledger.core.CryptoConfig; | |||
| import com.jd.blockchain.ledger.core.ParticipantCertData; | |||
| import com.jd.blockchain.transaction.ConsensusParticipantData; | |||
| @@ -80,6 +77,7 @@ public class LedgerInitOperationTest { | |||
| parties[i].setHostAddress(new NetworkAddress("192.168.10." + (10 + i), 10010 + 10 * i)); | |||
| parties[i].setName("Participant[" + i + "]"); | |||
| parties[i].setPubKey(keys[i].getPubKey()); | |||
| parties[i].setParticipantState(ParticipantNodeState.CONSENSUSED); | |||
| } | |||
| ConsensusParticipantData[] parties1 = Arrays.copyOf(parties, 4); | |||
| @@ -118,7 +116,7 @@ public class LedgerInitOperationTest { | |||
| for (int i = 0; i < parties.length; i++) { | |||
| keys[i] = BlockchainKeyGenerator.getInstance().generate(); | |||
| parties[i] = new ParticipantCertData(AddressEncoding.generateAddress(keys[i].getPubKey()), | |||
| "Participant[" + i + "]", keys[i].getPubKey()); | |||
| "Participant[" + i + "]", keys[i].getPubKey(), ParticipantNodeState.CONSENSUSED); | |||
| } | |||
| ParticipantCertData[] parties1 = Arrays.copyOf(parties, 4); | |||
| @@ -6,6 +6,7 @@ import static org.junit.Assert.assertEquals; | |||
| import java.util.Arrays; | |||
| import java.util.Random; | |||
| import com.jd.blockchain.ledger.ParticipantNodeState; | |||
| import org.junit.Before; | |||
| import org.junit.Test; | |||
| @@ -80,6 +81,7 @@ public class LedgerInitSettingSerializeTest { | |||
| parties[i].setHostAddress(new NetworkAddress("192.168.10." + (10 + i), 10010 + 10 * i)); | |||
| parties[i].setName("Participant[" + i + "]"); | |||
| parties[i].setPubKey(keys[i].getPubKey()); | |||
| parties[i].setParticipantState(ParticipantNodeState.CONSENSUSED); | |||
| } | |||
| ConsensusParticipantData[] parties1 = Arrays.copyOf(parties, 4); | |||
| @@ -122,7 +124,7 @@ public class LedgerInitSettingSerializeTest { | |||
| for (int i = 0; i < parties.length; i++) { | |||
| keys[i] = BlockchainKeyGenerator.getInstance().generate(); | |||
| parties[i] = new ParticipantCertData(AddressEncoding.generateAddress(keys[i].getPubKey()), | |||
| "Participant[" + i + "]", keys[i].getPubKey()); | |||
| "Participant[" + i + "]", keys[i].getPubKey(), ParticipantNodeState.CONSENSUSED); | |||
| } | |||
| ParticipantCertData[] parties1 = Arrays.copyOf(parties, 4); | |||
| @@ -6,6 +6,7 @@ import static org.junit.Assert.assertNull; | |||
| import java.util.stream.Stream; | |||
| import com.jd.blockchain.ledger.*; | |||
| import org.junit.Before; | |||
| import org.junit.Test; | |||
| @@ -19,19 +20,6 @@ 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; | |||
| import com.jd.blockchain.ledger.DataAccountRegisterOperation; | |||
| import com.jd.blockchain.ledger.DigitalSignature; | |||
| import com.jd.blockchain.ledger.LedgerBlock; | |||
| import com.jd.blockchain.ledger.LedgerInitSetting; | |||
| import com.jd.blockchain.ledger.LedgerTransaction; | |||
| import com.jd.blockchain.ledger.TransactionContent; | |||
| import com.jd.blockchain.ledger.TransactionRequest; | |||
| import com.jd.blockchain.ledger.TransactionRequestBuilder; | |||
| import com.jd.blockchain.ledger.TransactionState; | |||
| import com.jd.blockchain.ledger.UserRegisterOperation; | |||
| import com.jd.blockchain.ledger.core.ContractAccountQuery; | |||
| import com.jd.blockchain.ledger.core.CryptoConfig; | |||
| import com.jd.blockchain.ledger.core.DataAccountQuery; | |||
| @@ -57,6 +45,9 @@ public class LedgerManagerTest { | |||
| DataContractRegistry.register(TransactionContent.class); | |||
| DataContractRegistry.register(UserRegisterOperation.class); | |||
| DataContractRegistry.register(DataAccountRegisterOperation.class); | |||
| DataContractRegistry.register(ParticipantNode.class); | |||
| DataContractRegistry.register(ParticipantRegisterOperation.class); | |||
| DataContractRegistry.register(ParticipantStateUpdateOperation.class); | |||
| DataContractRegistry.register(BlockBody.class); | |||
| DataContractRegistry.register(CryptoProvider.class); | |||
| } | |||
| @@ -214,6 +205,7 @@ public class LedgerManagerTest { | |||
| parties[0].setPubKey(kp0.getPubKey()); | |||
| parties[0].setAddress(AddressEncoding.generateAddress(kp0.getPubKey())); | |||
| parties[0].setHostAddress(new NetworkAddress("127.0.0.1", 9000)); | |||
| parties[0].setParticipantState(ParticipantNodeState.CONSENSUSED); | |||
| parties[1] = new ConsensusParticipantData(); | |||
| parties[1].setId(1); | |||
| @@ -222,6 +214,7 @@ public class LedgerManagerTest { | |||
| parties[1].setPubKey(kp1.getPubKey()); | |||
| parties[1].setAddress(AddressEncoding.generateAddress(kp1.getPubKey())); | |||
| parties[1].setHostAddress(new NetworkAddress("127.0.0.1", 9010)); | |||
| parties[1].setParticipantState(ParticipantNodeState.CONSENSUSED); | |||
| parties[2] = new ConsensusParticipantData(); | |||
| parties[2].setId(2); | |||
| @@ -230,6 +223,7 @@ public class LedgerManagerTest { | |||
| parties[2].setPubKey(kp2.getPubKey()); | |||
| parties[2].setAddress(AddressEncoding.generateAddress(kp2.getPubKey())); | |||
| parties[2].setHostAddress(new NetworkAddress("127.0.0.1", 9020)); | |||
| parties[2].setParticipantState(ParticipantNodeState.CONSENSUSED); | |||
| parties[3] = new ConsensusParticipantData(); | |||
| parties[3].setId(3); | |||
| @@ -238,6 +232,7 @@ public class LedgerManagerTest { | |||
| parties[3].setPubKey(kp3.getPubKey()); | |||
| parties[3].setAddress(AddressEncoding.generateAddress(kp3.getPubKey())); | |||
| parties[3].setHostAddress(new NetworkAddress("127.0.0.1", 9030)); | |||
| parties[3].setParticipantState(ParticipantNodeState.CONSENSUSED); | |||
| initSetting.setConsensusParticipants(parties); | |||
| @@ -7,8 +7,7 @@ import static org.junit.Assert.assertTrue; | |||
| import java.util.Random; | |||
| import com.jd.blockchain.ledger.LedgerMetadata; | |||
| import com.jd.blockchain.ledger.LedgerSettings; | |||
| import com.jd.blockchain.ledger.*; | |||
| import org.junit.Before; | |||
| import org.junit.Test; | |||
| @@ -23,8 +22,6 @@ 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; | |||
| import com.jd.blockchain.ledger.core.LedgerAdminDataset; | |||
| import com.jd.blockchain.ledger.core.LedgerConfiguration; | |||
| @@ -189,7 +186,7 @@ public class LedgerMetaDataTest { | |||
| // NetworkAddress consensusAddress = new NetworkAddress("192.168.1.1", 9001, | |||
| // false); | |||
| Bytes address = AddressEncoding.generateAddress(pubKey); | |||
| ParticipantCertData participantCertData = new ParticipantCertData(address, name, pubKey); | |||
| ParticipantCertData participantCertData = new ParticipantCertData(address, name, pubKey, ParticipantNodeState.CONSENSUSED); | |||
| // encode and decode | |||
| byte[] encodeBytes = BinaryProtocol.encode(participantCertData, ParticipantNode.class); | |||
| @@ -11,13 +11,7 @@ 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.BlockchainKeyGenerator; | |||
| import com.jd.blockchain.ledger.BlockchainKeypair; | |||
| import com.jd.blockchain.ledger.CryptoSetting; | |||
| import com.jd.blockchain.ledger.LedgerInitSetting; | |||
| import com.jd.blockchain.ledger.TransactionRequest; | |||
| import com.jd.blockchain.ledger.TransactionRequestBuilder; | |||
| import com.jd.blockchain.ledger.TransactionResponse; | |||
| import com.jd.blockchain.ledger.*; | |||
| import com.jd.blockchain.ledger.core.CryptoConfig; | |||
| import com.jd.blockchain.ledger.core.TransactionStagedSnapshot; | |||
| import com.jd.blockchain.transaction.ConsensusParticipantData; | |||
| @@ -74,6 +68,7 @@ public class LedgerTestUtils { | |||
| parties[i].setPubKey(partiKeys[i].getPubKey()); | |||
| parties[i].setAddress(AddressEncoding.generateAddress(partiKeys[i].getPubKey())); | |||
| parties[i].setHostAddress(new NetworkAddress("192.168.1." + (10 + i), 9000)); | |||
| parties[i].setParticipantState(ParticipantNodeState.CONSENSUSED); | |||
| } | |||
| @@ -9,28 +9,13 @@ import static org.junit.Assert.assertTrue; | |||
| import java.util.Random; | |||
| import com.jd.blockchain.ledger.*; | |||
| import org.junit.Test; | |||
| import com.jd.blockchain.binaryproto.BinaryProtocol; | |||
| import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
| import com.jd.blockchain.crypto.HashDigest; | |||
| import com.jd.blockchain.ledger.BlockchainKeyGenerator; | |||
| import com.jd.blockchain.ledger.BlockchainKeypair; | |||
| import com.jd.blockchain.ledger.BytesDataList; | |||
| import com.jd.blockchain.ledger.BytesValueList; | |||
| 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.DataAccountKVSetOperation.KVWriteEntry; | |||
| import com.jd.blockchain.ledger.DataAccountRegisterOperation; | |||
| import com.jd.blockchain.ledger.DigitalSignature; | |||
| import com.jd.blockchain.ledger.LedgerTransaction; | |||
| import com.jd.blockchain.ledger.Operation; | |||
| import com.jd.blockchain.ledger.TransactionRequest; | |||
| import com.jd.blockchain.ledger.TransactionRequestBuilder; | |||
| import com.jd.blockchain.ledger.TransactionState; | |||
| import com.jd.blockchain.ledger.UserRegisterOperation; | |||
| import com.jd.blockchain.ledger.core.LedgerTransactionData; | |||
| import com.jd.blockchain.ledger.core.TransactionSet; | |||
| import com.jd.blockchain.ledger.core.TransactionStagedSnapshot; | |||
| @@ -51,6 +36,8 @@ public class TransactionSetTest { | |||
| DataContractRegistry.register(DataAccountKVSetOperation.class); | |||
| DataContractRegistry.register(ContractCodeDeployOperation.class); | |||
| DataContractRegistry.register(ContractEventSendOperation.class); | |||
| DataContractRegistry.register(ParticipantRegisterOperation.class); | |||
| DataContractRegistry.register(ParticipantStateUpdateOperation.class); | |||
| CryptoSetting defCryptoSetting = LedgerTestUtils.createDefaultCryptoSetting(); | |||
| MemoryKVStorage testStorage = new MemoryKVStorage(); | |||
| @@ -16,6 +16,7 @@ import com.jd.blockchain.crypto.AddressEncoding; | |||
| import com.jd.blockchain.crypto.KeyGenUtils; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.ledger.LedgerInitProperties.CryptoProperties; | |||
| import com.jd.blockchain.ledger.ParticipantNodeState; | |||
| import com.jd.blockchain.utils.Bytes; | |||
| import com.jd.blockchain.utils.PropertiesUtils; | |||
| import com.jd.blockchain.utils.StringUtils; | |||
| @@ -325,7 +326,7 @@ public class LedgerInitProperties { | |||
| .parseBoolean(PropertiesUtils.getRequiredProperty(props, initializerSecureKey)); | |||
| NetworkAddress initializerAddress = new NetworkAddress(initializerHost, initializerPort, initializerSecure); | |||
| parti.setInitializerAddress(initializerAddress); | |||
| parti.setParticipantNodeState(ParticipantNodeState.CONSENSUSED); | |||
| initProps.addConsensusParticipant(parti); | |||
| } | |||
| @@ -436,8 +437,11 @@ public class LedgerInitProperties { | |||
| // private NetworkAddress consensusAddress; | |||
| private ParticipantNodeState participantNodeState; | |||
| private NetworkAddress initializerAddress; | |||
| @Override | |||
| public int getId() { | |||
| return id; | |||
| } | |||
| @@ -451,6 +455,7 @@ public class LedgerInitProperties { | |||
| return address; | |||
| } | |||
| @Override | |||
| public String getName() { | |||
| return name; | |||
| } | |||
| @@ -467,6 +472,15 @@ public class LedgerInitProperties { | |||
| // this.pubKeyPath = pubKeyPath; | |||
| // } | |||
| @Override | |||
| public ParticipantNodeState getParticipantNodeState() { | |||
| return participantNodeState; | |||
| } | |||
| public void setParticipantNodeState(ParticipantNodeState participantNodeState) { | |||
| this.participantNodeState = participantNodeState; | |||
| } | |||
| public NetworkAddress getInitializerAddress() { | |||
| return initializerAddress; | |||
| } | |||
| @@ -475,6 +489,7 @@ public class LedgerInitProperties { | |||
| this.initializerAddress = initializerAddress; | |||
| } | |||
| @Override | |||
| public PubKey getPubKey() { | |||
| return pubKey; | |||
| } | |||
| @@ -1,35 +1,52 @@ | |||
| //package com.jd.blockchain.ledger; | |||
| // | |||
| //import com.jd.blockchain.base.data.TypeCodes; | |||
| //import com.jd.blockchain.binaryproto.DataContract; | |||
| //import com.jd.blockchain.binaryproto.DataField; | |||
| //import com.jd.blockchain.crypto.asymmetric.PubKey; | |||
| // | |||
| //import my.utils.ValueType; | |||
| // | |||
| ///** | |||
| // * 参与方信息; | |||
| // * | |||
| // * @author huanghaiquan | |||
| // * | |||
| // */ | |||
| //@DataContract(code = TypeCodes.METADATA_PARTICIPANT_INFO) | |||
| //public interface ParticipantInfo { | |||
| // | |||
| // /** | |||
| // * 参与者名称; | |||
| // * | |||
| // * @return | |||
| // */ | |||
| // @DataField(order = 1, primitiveType = ValueType.TEXT) | |||
| // String getName(); | |||
| // | |||
| // /** | |||
| // * 公钥; | |||
| // * | |||
| // * @return | |||
| // */ | |||
| // @DataField(order = 2, primitiveType = ValueType.BYTES) | |||
| // PubKey getPubKey(); | |||
| // | |||
| //} | |||
| 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.PubKey; | |||
| import com.jd.blockchain.utils.net.NetworkAddress; | |||
| /** | |||
| * 参与方信息; | |||
| * | |||
| * @author huanghaiquan | |||
| * | |||
| */ | |||
| @DataContract(code = DataCodes.METADATA_PARTICIPANT_INFO) | |||
| public interface ParticipantInfo { | |||
| /** | |||
| * regist or unregist, temporarily not available to users | |||
| * | |||
| * @return | |||
| */ | |||
| // @DataField(order = 0, primitiveType = PrimitiveType.TEXT) | |||
| // String getFlag(); | |||
| /** | |||
| * 参与者名称; | |||
| * | |||
| * @return | |||
| */ | |||
| @DataField(order = 1, primitiveType = PrimitiveType.TEXT) | |||
| String getName(); | |||
| /** | |||
| * 公钥; | |||
| * | |||
| * @return | |||
| */ | |||
| @DataField(order = 2, primitiveType = PrimitiveType.BYTES) | |||
| PubKey getPubKey(); | |||
| /** | |||
| * 共识协议的网络地址; | |||
| * | |||
| * @return | |||
| */ | |||
| @DataField(order = 3, primitiveType = PrimitiveType.BYTES) | |||
| NetworkAddress getNetworkAddress(); | |||
| } | |||
| @@ -0,0 +1,56 @@ | |||
| package com.jd.blockchain.ledger; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.utils.net.NetworkAddress; | |||
| import org.omg.CORBA.PUBLIC_MEMBER; | |||
| /** | |||
| * 即将要注册的参与方的信息 | |||
| * @author zhangshuang | |||
| * @create 2019/7/8 | |||
| * @since 1.0.0 | |||
| */ | |||
| public class ParticipantInfoData implements ParticipantInfo { | |||
| private String name; | |||
| private PubKey pubKey; | |||
| private NetworkAddress networkAddress; | |||
| // private String flag;//代表注册参与方或者删除参与方 | |||
| public ParticipantInfoData(String name, PubKey pubKey, NetworkAddress networkAddress) { | |||
| this.name = name; | |||
| this.pubKey = pubKey; | |||
| this.networkAddress = networkAddress; | |||
| } | |||
| @Override | |||
| public String getName() { | |||
| return name; | |||
| } | |||
| public void setName(String name) { | |||
| this.name = name; | |||
| } | |||
| @Override | |||
| public PubKey getPubKey() { | |||
| return pubKey; | |||
| } | |||
| public void setPubKey(PubKey pubKey) { | |||
| this.pubKey = pubKey; | |||
| } | |||
| @Override | |||
| public NetworkAddress getNetworkAddress() { | |||
| return networkAddress; | |||
| } | |||
| public void setNetworkAddress(NetworkAddress networkAddress) { | |||
| this.networkAddress = networkAddress; | |||
| } | |||
| } | |||
| @@ -49,4 +49,12 @@ public interface ParticipantNode { | |||
| */ | |||
| @DataField(order = 3, primitiveType = PrimitiveType.BYTES) | |||
| PubKey getPubKey(); | |||
| /** | |||
| * 节点的状态:已注册/已参与共识 | |||
| * | |||
| * @return | |||
| */ | |||
| @DataField(order = 4, refEnum = true) | |||
| ParticipantNodeState getParticipantNodeState(); | |||
| } | |||
| @@ -0,0 +1,43 @@ | |||
| package com.jd.blockchain.ledger; | |||
| import com.jd.blockchain.binaryproto.EnumContract; | |||
| import com.jd.blockchain.binaryproto.EnumField; | |||
| import com.jd.blockchain.binaryproto.PrimitiveType; | |||
| import com.jd.blockchain.consts.DataCodes; | |||
| /** | |||
| * 参与方节点状态 | |||
| * @author zhangshuang | |||
| * @create 2019/7/8 | |||
| * @since 1.0.0 | |||
| */ | |||
| @EnumContract(code= DataCodes.ENUM_TYPE_PARTICIPANT_NODE_STATE) | |||
| public enum ParticipantNodeState { | |||
| /** | |||
| * 已注册; | |||
| */ | |||
| REGISTED((byte) 0), | |||
| /** | |||
| * 已共识; | |||
| */ | |||
| CONSENSUSED((byte) 1); | |||
| @EnumField(type= PrimitiveType.INT8) | |||
| public final byte CODE; | |||
| private ParticipantNodeState(byte code) { | |||
| this.CODE = code; | |||
| } | |||
| public static ParticipantNodeState valueOf(byte code) { | |||
| for (ParticipantNodeState tr : values()) { | |||
| if (tr.CODE == code) { | |||
| return tr; | |||
| } | |||
| } | |||
| throw new IllegalArgumentException("Unsupported participant node state code!"); | |||
| } | |||
| } | |||
| @@ -0,0 +1,20 @@ | |||
| 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.net.NetworkAddress; | |||
| @DataContract(code= DataCodes.TX_OP_PARTICIPANT_REG) | |||
| public interface ParticipantRegisterOperation extends Operation { | |||
| @DataField(order = 0, primitiveType=PrimitiveType.TEXT) | |||
| String getParticipantName(); | |||
| @DataField(order = 1, refContract = true) | |||
| BlockchainIdentity getParticipantIdentity(); | |||
| @DataField(order = 2, primitiveType = PrimitiveType.BYTES) | |||
| NetworkAddress getNetworkAddress(); | |||
| } | |||
| @@ -0,0 +1,41 @@ | |||
| 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.PubKey; | |||
| import com.jd.blockchain.utils.net.NetworkAddress; | |||
| /** | |||
| * 参与方状态更新信息; | |||
| * | |||
| * | |||
| */ | |||
| @DataContract(code = DataCodes.METADATA_PARTICIPANT_STATE_INFO) | |||
| public interface ParticipantStateUpdateInfo { | |||
| /** | |||
| * 公钥; | |||
| * | |||
| * @return | |||
| */ | |||
| @DataField(order = 1, primitiveType = PrimitiveType.BYTES) | |||
| PubKey getPubKey(); | |||
| /** | |||
| * 共识协议的网络地址; | |||
| * | |||
| * @return | |||
| */ | |||
| @DataField(order = 2, primitiveType = PrimitiveType.BYTES) | |||
| NetworkAddress getNetworkAddress(); | |||
| /** | |||
| * 参与方状态; | |||
| * | |||
| * @return | |||
| */ | |||
| @DataField(order = 3, refEnum = true) | |||
| ParticipantNodeState getState(); | |||
| } | |||
| @@ -0,0 +1,43 @@ | |||
| package com.jd.blockchain.ledger; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.utils.net.NetworkAddress; | |||
| public class ParticipantStateUpdateInfoData implements ParticipantStateUpdateInfo { | |||
| private PubKey pubKey; | |||
| private ParticipantNodeState state; | |||
| private NetworkAddress networkAddress; | |||
| public ParticipantStateUpdateInfoData(PubKey pubKey, ParticipantNodeState state, NetworkAddress networkAddress) { | |||
| this.pubKey = pubKey; | |||
| this.state = state; | |||
| this.networkAddress = networkAddress; | |||
| } | |||
| public void setPubKey(PubKey pubKey) { | |||
| this.pubKey = pubKey; | |||
| } | |||
| @Override | |||
| public PubKey getPubKey() { | |||
| return pubKey; | |||
| } | |||
| public void setNetworkAddress(NetworkAddress networkAddress) { | |||
| this.networkAddress = networkAddress; | |||
| } | |||
| @Override | |||
| public NetworkAddress getNetworkAddress() { | |||
| return networkAddress; | |||
| } | |||
| public void setState(ParticipantNodeState state) { | |||
| this.state = state; | |||
| } | |||
| @Override | |||
| public ParticipantNodeState getState() { | |||
| return state; | |||
| } | |||
| } | |||
| @@ -0,0 +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.utils.net.NetworkAddress; | |||
| @DataContract(code= DataCodes.TX_OP_PARTICIPANT_STATE_UPDATE) | |||
| public interface ParticipantStateUpdateOperation extends Operation { | |||
| @DataField(order = 0, refContract = true) | |||
| BlockchainIdentity getParticipantIdentity(); | |||
| @DataField(order = 1, primitiveType = PrimitiveType.BYTES) | |||
| NetworkAddress getNetworkAddress(); | |||
| @DataField(order = 2, refEnum = true) | |||
| ParticipantNodeState getState(); | |||
| } | |||
| @@ -7,6 +7,11 @@ import java.util.ArrayList; | |||
| import java.util.Collection; | |||
| import java.util.List; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.ledger.*; | |||
| import com.jd.blockchain.utils.Bytes; | |||
| import com.jd.blockchain.utils.net.NetworkAddress; | |||
| /** | |||
| * @author huanghaiquan | |||
| * | |||
| @@ -27,6 +32,10 @@ public class BlockchainOperationFactory implements ClientOperator, LedgerInitOpe | |||
| private SecurityOperationBuilderFilter securityOpBuilder = new SecurityOperationBuilderFilter(); | |||
| private static final ParticipantRegisterOperationBuilderImpl PARTICIPANT_REG_OP_BUILDER = new ParticipantRegisterOperationBuilderImpl(); | |||
| private static final ParticipantStateUpdateOperationBuilderImpl PARTICIPANT_STATE_UPDATE_OP_BUILDER = new ParticipantStateUpdateOperationBuilderImpl(); | |||
| private LedgerInitOperationBuilder ledgerInitOpBuilder = new LedgerInitOperationBuilderFilter(); | |||
| private UserRegisterOperationBuilder userRegOpBuilder = new UserRegisterOperationBuilderFilter(); | |||
| @@ -39,6 +48,10 @@ public class BlockchainOperationFactory implements ClientOperator, LedgerInitOpe | |||
| private ContractInvocationProxyBuilder contractInvoProxyBuilder = new ContractInvocationProxyBuilder(); | |||
| private ParticipantRegisterOperationBuilder participantRegOpBuilder = new ParticipantRegisterOperationBuilderFilter(); | |||
| private ParticipantStateUpdateOperationBuilder participantStateModifyOpBuilder = new ParticipantStateUpdateOperationBuilderFilter(); | |||
| // TODO: 暂时只支持单线程情形,未考虑多线程; | |||
| private List<Operation> operationList = new ArrayList<>(); | |||
| @@ -81,6 +94,12 @@ public class BlockchainOperationFactory implements ClientOperator, LedgerInitOpe | |||
| return contractEventSendOpBuilder; | |||
| } | |||
| @Override | |||
| public ParticipantRegisterOperationBuilder participants() {return participantRegOpBuilder;} | |||
| @Override | |||
| public ParticipantStateUpdateOperationBuilder states() {return participantStateModifyOpBuilder;} | |||
| @Override | |||
| public <T> T contract(String address, Class<T> contractIntf) { | |||
| return contractInvoProxyBuilder.create(address, contractIntf, contractEventSendOpBuilder); | |||
| @@ -266,13 +285,30 @@ public class BlockchainOperationFactory implements ClientOperator, LedgerInitOpe | |||
| private class ContractCodeDeployOperationBuilderFilter implements ContractCodeDeployOperationBuilder { | |||
| @Override | |||
| public ContractCodeDeployOperation deploy(BlockchainIdentity id, byte[] chainCode) { | |||
| // 校验成功后发布 | |||
| ContractCodeDeployOperation op = CONTRACT_CODE_DEPLOY_OP_BUILDER.deploy(id, chainCode); | |||
| operationList.add(op); | |||
| return op; | |||
| } | |||
| } | |||
| private class ParticipantRegisterOperationBuilderFilter implements ParticipantRegisterOperationBuilder { | |||
| @Override | |||
| public ParticipantRegisterOperation register(String participantName, BlockchainIdentity participantIdentity, NetworkAddress networkAddress) { | |||
| ParticipantRegisterOperation op = PARTICIPANT_REG_OP_BUILDER.register(participantName, participantIdentity, networkAddress); | |||
| operationList.add(op); | |||
| return op; | |||
| } | |||
| } | |||
| private class ParticipantStateUpdateOperationBuilderFilter implements ParticipantStateUpdateOperationBuilder { | |||
| @Override | |||
| public ParticipantStateUpdateOperation update(BlockchainIdentity blockchainIdentity, NetworkAddress networkAddress, ParticipantNodeState participantNodeState) { | |||
| ParticipantStateUpdateOperation op = PARTICIPANT_STATE_UPDATE_OP_BUILDER.update(blockchainIdentity, networkAddress, participantNodeState); | |||
| operationList.add(op); | |||
| return op; | |||
| } | |||
| } | |||
| private class ContractEventSendOperationBuilderFilter implements ContractEventSendOperationBuilder { | |||
| @Override | |||
| @@ -7,6 +7,6 @@ package com.jd.blockchain.transaction; | |||
| * | |||
| */ | |||
| public interface ClientOperator | |||
| extends SecurityOperator, UserOperator, DataAccountOperator, ContractOperator, EventOperator { | |||
| extends SecurityOperator, UserOperator, DataAccountOperator, ContractOperator, EventOperator, ParticipantOperator { | |||
| } | |||
| @@ -4,6 +4,7 @@ import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.ledger.ParticipantNode; | |||
| import com.jd.blockchain.utils.Bytes; | |||
| import com.jd.blockchain.utils.net.NetworkAddress; | |||
| import com.jd.blockchain.ledger.ParticipantNodeState; | |||
| public class ConsensusParticipantData implements ParticipantNode { | |||
| @@ -17,6 +18,9 @@ public class ConsensusParticipantData implements ParticipantNode { | |||
| private NetworkAddress hostAddress; | |||
| private ParticipantNodeState participantNodeState; | |||
| @Override | |||
| public int getId() { | |||
| return id; | |||
| } | |||
| @@ -25,6 +29,7 @@ public class ConsensusParticipantData implements ParticipantNode { | |||
| this.id = id; | |||
| } | |||
| @Override | |||
| public String getName() { | |||
| return name; | |||
| } | |||
| @@ -33,7 +38,7 @@ public class ConsensusParticipantData implements ParticipantNode { | |||
| this.name = name; | |||
| } | |||
| public NetworkAddress getConsensusAddress() { | |||
| public NetworkAddress getHostAddress() { | |||
| return hostAddress; | |||
| } | |||
| @@ -41,6 +46,7 @@ public class ConsensusParticipantData implements ParticipantNode { | |||
| this.hostAddress = hostAddress; | |||
| } | |||
| @Override | |||
| public PubKey getPubKey() { | |||
| return pubKey; | |||
| } | |||
| @@ -49,6 +55,7 @@ public class ConsensusParticipantData implements ParticipantNode { | |||
| this.pubKey = pubKey; | |||
| } | |||
| @Override | |||
| public Bytes getAddress() { | |||
| return address; | |||
| } | |||
| @@ -57,4 +64,13 @@ public class ConsensusParticipantData implements ParticipantNode { | |||
| this.address = address; | |||
| } | |||
| @Override | |||
| public ParticipantNodeState getParticipantNodeState() { | |||
| return participantNodeState; | |||
| } | |||
| public void setParticipantState(ParticipantNodeState participantNodeState) { | |||
| this.participantNodeState = participantNodeState; | |||
| } | |||
| } | |||
| @@ -0,0 +1,18 @@ | |||
| package com.jd.blockchain.transaction; | |||
| public interface ParticipantOperator { | |||
| /** | |||
| * 注册参与方操作; | |||
| * | |||
| * @return | |||
| */ | |||
| ParticipantRegisterOperationBuilder participants(); | |||
| /** | |||
| * 参与方状态更新操作; | |||
| * | |||
| * @return | |||
| */ | |||
| ParticipantStateUpdateOperationBuilder states(); | |||
| } | |||
| @@ -0,0 +1,41 @@ | |||
| package com.jd.blockchain.transaction; | |||
| import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.ledger.BlockchainIdentity; | |||
| import com.jd.blockchain.ledger.ParticipantInfo; | |||
| import com.jd.blockchain.ledger.ParticipantRegisterOperation; | |||
| import com.jd.blockchain.utils.net.NetworkAddress; | |||
| public class ParticipantRegisterOpTemplate implements ParticipantRegisterOperation { | |||
| static { | |||
| DataContractRegistry.register(ParticipantRegisterOperation.class); | |||
| } | |||
| private String participantName; | |||
| private BlockchainIdentity participantPubKey; | |||
| private NetworkAddress networkAddress; | |||
| public ParticipantRegisterOpTemplate(String participantName, BlockchainIdentity participantPubKey, NetworkAddress networkAddress) { | |||
| this.participantName = participantName; | |||
| this.participantPubKey = participantPubKey; | |||
| this.networkAddress = networkAddress; | |||
| } | |||
| @Override | |||
| public String getParticipantName() { | |||
| return participantName; | |||
| } | |||
| @Override | |||
| public BlockchainIdentity getParticipantIdentity() { | |||
| return participantPubKey; | |||
| } | |||
| @Override | |||
| public NetworkAddress getNetworkAddress() { | |||
| return networkAddress; | |||
| } | |||
| } | |||
| @@ -0,0 +1,23 @@ | |||
| package com.jd.blockchain.transaction; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.ledger.BlockchainIdentity; | |||
| import com.jd.blockchain.ledger.ParticipantInfo; | |||
| import com.jd.blockchain.ledger.ParticipantRegisterOperation; | |||
| import com.jd.blockchain.utils.net.NetworkAddress; | |||
| public interface ParticipantRegisterOperationBuilder { | |||
| /** | |||
| * 注册; | |||
| * | |||
| * @param | |||
| * | |||
| * @param | |||
| * | |||
| * @return | |||
| */ | |||
| ParticipantRegisterOperation register(String participantName, BlockchainIdentity participantPubKey, NetworkAddress networkAddress); | |||
| } | |||
| @@ -0,0 +1,14 @@ | |||
| package com.jd.blockchain.transaction; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.ledger.BlockchainIdentity; | |||
| import com.jd.blockchain.ledger.ParticipantInfo; | |||
| import com.jd.blockchain.ledger.ParticipantRegisterOperation; | |||
| import com.jd.blockchain.utils.net.NetworkAddress; | |||
| public class ParticipantRegisterOperationBuilderImpl implements ParticipantRegisterOperationBuilder { | |||
| @Override | |||
| public ParticipantRegisterOperation register(String participantName, BlockchainIdentity participantPubKey, NetworkAddress networkAddress) { | |||
| return new ParticipantRegisterOpTemplate(participantName, participantPubKey, networkAddress); | |||
| } | |||
| } | |||
| @@ -0,0 +1,10 @@ | |||
| package com.jd.blockchain.transaction; | |||
| public interface ParticipantStateOperator { | |||
| /** | |||
| * 参与方状态更新操作; | |||
| * | |||
| * @return | |||
| */ | |||
| ParticipantStateUpdateOperationBuilder states(); | |||
| } | |||
| @@ -0,0 +1,41 @@ | |||
| package com.jd.blockchain.transaction; | |||
| import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
| import com.jd.blockchain.ledger.BlockchainIdentity; | |||
| import com.jd.blockchain.ledger.ParticipantNodeState; | |||
| import com.jd.blockchain.ledger.ParticipantStateUpdateOperation; | |||
| import com.jd.blockchain.utils.net.NetworkAddress; | |||
| public class ParticipantStateUpdateOpTemplate implements ParticipantStateUpdateOperation { | |||
| static { | |||
| DataContractRegistry.register(ParticipantStateUpdateOperation.class); | |||
| } | |||
| private BlockchainIdentity blockchainIdentity; | |||
| private NetworkAddress networkAddress; | |||
| private ParticipantNodeState participantNodeState; | |||
| public ParticipantStateUpdateOpTemplate(BlockchainIdentity blockchainIdentity, NetworkAddress networkAddress, ParticipantNodeState participantNodeState) { | |||
| this.blockchainIdentity = blockchainIdentity; | |||
| this.networkAddress = networkAddress; | |||
| this.participantNodeState = participantNodeState; | |||
| } | |||
| @Override | |||
| public BlockchainIdentity getParticipantIdentity() { | |||
| return blockchainIdentity; | |||
| } | |||
| @Override | |||
| public NetworkAddress getNetworkAddress() { | |||
| return networkAddress; | |||
| } | |||
| @Override | |||
| public ParticipantNodeState getState() { | |||
| return participantNodeState; | |||
| } | |||
| } | |||
| @@ -0,0 +1,21 @@ | |||
| package com.jd.blockchain.transaction; | |||
| import com.jd.blockchain.ledger.BlockchainIdentity; | |||
| import com.jd.blockchain.ledger.ParticipantNodeState; | |||
| import com.jd.blockchain.ledger.ParticipantStateUpdateInfo; | |||
| import com.jd.blockchain.ledger.ParticipantStateUpdateOperation; | |||
| import com.jd.blockchain.utils.net.NetworkAddress; | |||
| public interface ParticipantStateUpdateOperationBuilder { | |||
| /** | |||
| * 更新参与方状态,已注册->参与共识; | |||
| * | |||
| * @param | |||
| * | |||
| * @param | |||
| * | |||
| * @return | |||
| */ | |||
| ParticipantStateUpdateOperation update(BlockchainIdentity blockchainIdentity, NetworkAddress networkAddress, ParticipantNodeState participantNodeState); | |||
| } | |||
| @@ -0,0 +1,15 @@ | |||
| package com.jd.blockchain.transaction; | |||
| import com.jd.blockchain.ledger.BlockchainIdentity; | |||
| import com.jd.blockchain.ledger.ParticipantNodeState; | |||
| import com.jd.blockchain.ledger.ParticipantStateUpdateInfo; | |||
| import com.jd.blockchain.ledger.ParticipantStateUpdateOperation; | |||
| import com.jd.blockchain.utils.net.NetworkAddress; | |||
| public class ParticipantStateUpdateOperationBuilderImpl implements ParticipantStateUpdateOperationBuilder { | |||
| @Override | |||
| public ParticipantStateUpdateOperation update(BlockchainIdentity blockchainIdentity, NetworkAddress networkAddress, ParticipantNodeState participantNodeState) { | |||
| return new ParticipantStateUpdateOpTemplate(blockchainIdentity, networkAddress, participantNodeState); | |||
| } | |||
| } | |||
| @@ -115,6 +115,12 @@ public class TxBuilder implements TransactionBuilder { | |||
| return opFactory.contractEvents(); | |||
| } | |||
| @Override | |||
| public ParticipantRegisterOperationBuilder participants() {return opFactory.participants(); } | |||
| @Override | |||
| public ParticipantStateUpdateOperationBuilder states() {return opFactory.states(); } | |||
| @Override | |||
| public <T> T contract(Bytes address, Class<T> contractIntf) { | |||
| return opFactory.contract(address, contractIntf); | |||
| @@ -71,6 +71,18 @@ public class TxTemplate implements TransactionTemplate { | |||
| return txBuilder.contracts(); | |||
| } | |||
| @Override | |||
| public ParticipantRegisterOperationBuilder participants() { | |||
| stateManager.operate(); | |||
| return txBuilder.participants(); | |||
| } | |||
| @Override | |||
| public ParticipantStateUpdateOperationBuilder states() { | |||
| stateManager.operate(); | |||
| return txBuilder.states(); | |||
| } | |||
| @Override | |||
| public <T> T contract(Bytes address, Class<T> contractIntf) { | |||
| stateManager.operate(); | |||
| @@ -46,6 +46,8 @@ public class BinaryMessageConverter extends AbstractHttpMessageConverter<Object> | |||
| DataContractRegistry.register(ContractEventSendOperation.class); | |||
| DataContractRegistry.register(DataAccountRegisterOperation.class); | |||
| DataContractRegistry.register(UserRegisterOperation.class); | |||
| DataContractRegistry.register(ParticipantRegisterOperation.class); | |||
| DataContractRegistry.register(ParticipantStateUpdateOperation.class); | |||
| DataContractRegistry.register(ActionRequest.class); | |||
| DataContractRegistry.register(ActionResponse.class); | |||
| @@ -5,6 +5,7 @@ import java.util.List; | |||
| import java.util.Map; | |||
| import java.util.concurrent.ConcurrentHashMap; | |||
| import com.jd.blockchain.ledger.*; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| @@ -31,22 +32,7 @@ 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.LedgerAdminInfo; | |||
| 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.LedgerAdminDataQuery; | |||
| import com.jd.blockchain.ledger.core.LedgerManage; | |||
| import com.jd.blockchain.ledger.core.LedgerRepository; | |||
| @@ -117,6 +103,8 @@ public class ManagementController implements LedgerBindingConfigAware, PeerManag | |||
| DataContractRegistry.register(ContractEventSendOperation.class); | |||
| DataContractRegistry.register(DataAccountRegisterOperation.class); | |||
| DataContractRegistry.register(UserRegisterOperation.class); | |||
| DataContractRegistry.register(ParticipantRegisterOperation.class); | |||
| DataContractRegistry.register(ParticipantStateUpdateOperation.class); | |||
| DataContractRegistry.register(ActionResponse.class); | |||
| @@ -10,27 +10,13 @@ package com.jd.blockchain.sdk.converters; | |||
| import java.lang.reflect.Field; | |||
| import com.jd.blockchain.ledger.*; | |||
| import org.apache.commons.codec.binary.Base64; | |||
| import com.alibaba.fastjson.JSONArray; | |||
| import com.alibaba.fastjson.JSONObject; | |||
| import com.jd.blockchain.crypto.CryptoProvider; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.ledger.BlockchainIdentityData; | |||
| import com.jd.blockchain.ledger.BytesData; | |||
| import com.jd.blockchain.ledger.BytesValue; | |||
| import com.jd.blockchain.ledger.BytesValueEncoding; | |||
| 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.DataType; | |||
| import com.jd.blockchain.ledger.KVDataEntry; | |||
| import com.jd.blockchain.ledger.LedgerInitOperation; | |||
| import com.jd.blockchain.ledger.Operation; | |||
| import com.jd.blockchain.ledger.ParticipantNode; | |||
| import com.jd.blockchain.ledger.UserRegisterOperation; | |||
| import com.jd.blockchain.transaction.ContractCodeDeployOpTemplate; | |||
| import com.jd.blockchain.transaction.ContractEventSendOpTemplate; | |||
| import com.jd.blockchain.transaction.DataAccountKVSetOpTemplate; | |||
| @@ -214,8 +200,7 @@ public class ClientResolveUtil { | |||
| JSONObject pubKeyObj = currConsensusParticipant.getJSONObject("pubKey"); | |||
| String pubKeyBase58 = pubKeyObj.getString("value"); | |||
| // 生成ParticipantNode对象 | |||
| ParticipantCertData participantCertData = new ParticipantCertData(id, address, name, | |||
| new PubKey(Bytes.fromBase58(pubKeyBase58).toBytes())); | |||
| ParticipantCertData participantCertData = new ParticipantCertData(id, address, name, new PubKey(Bytes.fromBase58(pubKeyBase58).toBytes())); | |||
| participantNodes[i] = participantCertData; | |||
| } | |||
| ledgerInitSettingData.setConsensusParticipants(participantNodes); | |||
| @@ -298,6 +283,7 @@ public class ClientResolveUtil { | |||
| private Bytes address; | |||
| private String name; | |||
| private PubKey pubKey; | |||
| private ParticipantNodeState participantNodeState; | |||
| public ParticipantCertData() { | |||
| } | |||
| @@ -308,11 +294,12 @@ public class ClientResolveUtil { | |||
| this.pubKey = participantNode.getPubKey(); | |||
| } | |||
| public ParticipantCertData(int id, Bytes address, String name, PubKey pubKey) { | |||
| public ParticipantCertData(int id, Bytes address, String name, PubKey pubKey) { | |||
| this.id = id; | |||
| this.address = address; | |||
| this.name = name; | |||
| this.pubKey = pubKey; | |||
| this.participantNodeState = participantNodeState; | |||
| } | |||
| @Override | |||
| @@ -330,13 +317,16 @@ public class ClientResolveUtil { | |||
| return pubKey; | |||
| } | |||
| @Override | |||
| public int getId() { | |||
| return id; | |||
| } | |||
| public void setId(int id) { | |||
| this.id = id; | |||
| } | |||
| @Override | |||
| public ParticipantNodeState getParticipantNodeState() { | |||
| return participantNodeState; | |||
| } | |||
| } | |||
| public static class KvData implements KVDataEntry { | |||
| @@ -47,6 +47,8 @@ public class GatewayServiceFactory implements BlockchainServiceFactory, Closeabl | |||
| DataContractRegistry.register(ContractEventSendOperation.class); | |||
| DataContractRegistry.register(DataAccountRegisterOperation.class); | |||
| DataContractRegistry.register(UserRegisterOperation.class); | |||
| DataContractRegistry.register(ParticipantRegisterOperation.class); | |||
| DataContractRegistry.register(ParticipantStateUpdateOperation.class); | |||
| DataContractRegistry.register(ActionRequest.class); | |||
| DataContractRegistry.register(ActionResponse.class); | |||
| @@ -0,0 +1,99 @@ | |||
| package test.com.jd.blockchain.sdk.test; | |||
| import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
| import com.jd.blockchain.crypto.*; | |||
| import com.jd.blockchain.ledger.*; | |||
| import com.jd.blockchain.sdk.BlockchainService; | |||
| import com.jd.blockchain.sdk.client.GatewayServiceFactory; | |||
| import com.jd.blockchain.sdk.samples.SDKDemo_Constant; | |||
| import com.jd.blockchain.tools.keygen.KeyGenCommand; | |||
| import com.jd.blockchain.utils.net.NetworkAddress; | |||
| import org.junit.Before; | |||
| import org.junit.Test; | |||
| import static org.junit.Assert.assertTrue; | |||
| /** | |||
| * 注册参与方测试 | |||
| * @author zhangshuang | |||
| * @create 2019/7/4 | |||
| * @since 1.0.0 | |||
| */ | |||
| public class SDK_GateWay_Participant_Regist_Test_ { | |||
| private PrivKey privKey; | |||
| private PubKey pubKey; | |||
| private BlockchainKeypair CLIENT_CERT = null; | |||
| private String GATEWAY_IPADDR = null; | |||
| private int GATEWAY_PORT; | |||
| private boolean SECURE; | |||
| private BlockchainService service; | |||
| //根据密码工具产生的公私钥 | |||
| static String PUB = "3snPdw7i7PkdgqiGX7GbZuFSi1cwZn7vtjw4vifb1YoXgr9k6Kfmis"; | |||
| String PRIV = "177gjtZu8w1phqHFVNiFhA35cfimXmP6VuqrBFhfbXBWK8s4TRwro2tnpffwP1Emwr6SMN6"; | |||
| @Before | |||
| public void init() { | |||
| privKey = SDK_GateWay_KeyPair_Para.privkey1; | |||
| pubKey = SDK_GateWay_KeyPair_Para.pubKey1; | |||
| CLIENT_CERT = new BlockchainKeypair(SDK_GateWay_KeyPair_Para.pubKey0, SDK_GateWay_KeyPair_Para.privkey0); | |||
| GATEWAY_IPADDR = "127.0.0.1"; | |||
| GATEWAY_PORT = 11000; | |||
| SECURE = false; | |||
| GatewayServiceFactory serviceFactory = GatewayServiceFactory.connect(GATEWAY_IPADDR, GATEWAY_PORT, SECURE, | |||
| CLIENT_CERT); | |||
| service = serviceFactory.getBlockchainService(); | |||
| DataContractRegistry.register(TransactionContent.class); | |||
| DataContractRegistry.register(TransactionContentBody.class); | |||
| DataContractRegistry.register(TransactionRequest.class); | |||
| DataContractRegistry.register(NodeRequest.class); | |||
| DataContractRegistry.register(EndpointRequest.class); | |||
| DataContractRegistry.register(TransactionResponse.class); | |||
| DataContractRegistry.register(ParticipantRegisterOperation.class); | |||
| DataContractRegistry.register(ParticipantStateUpdateOperation.class); | |||
| } | |||
| @Test | |||
| public void registerParticipant_Test() { | |||
| HashDigest[] ledgerHashs = service.getLedgerHashs(); | |||
| // 在本地定义注册账号的 TX; | |||
| TransactionTemplate txTemp = service.newTransaction(ledgerHashs[0]); | |||
| //existed signer | |||
| AsymmetricKeypair keyPair = new BlockchainKeypair(pubKey, privKey); | |||
| PrivKey privKey = KeyGenUtils.decodePrivKeyWithRawPassword(PRIV, SDKDemo_Constant.PASSWORD); | |||
| PubKey pubKey = KeyGenUtils.decodePubKey(PUB); | |||
| System.out.println("Address = "+AddressEncoding.generateAddress(pubKey)); | |||
| BlockchainKeypair user = new BlockchainKeypair(pubKey, privKey); | |||
| NetworkAddress networkAddress = new NetworkAddress("127.0.0.1", 20000); | |||
| // 注册参与方 | |||
| txTemp.participants().register("Peer4", user.getIdentity(), networkAddress); | |||
| // TX 准备就绪; | |||
| PreparedTransaction prepTx = txTemp.prepare(); | |||
| // 使用私钥进行签名; | |||
| prepTx.sign(keyPair); | |||
| // 提交交易; | |||
| TransactionResponse transactionResponse = prepTx.commit(); | |||
| assertTrue(transactionResponse.isSuccess()); | |||
| } | |||
| } | |||
| @@ -0,0 +1,97 @@ | |||
| package test.com.jd.blockchain.sdk.test; | |||
| import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
| import com.jd.blockchain.crypto.*; | |||
| import com.jd.blockchain.ledger.*; | |||
| import com.jd.blockchain.sdk.BlockchainService; | |||
| import com.jd.blockchain.sdk.client.GatewayServiceFactory; | |||
| import com.jd.blockchain.sdk.samples.SDKDemo_Constant; | |||
| import com.jd.blockchain.tools.keygen.KeyGenCommand; | |||
| import com.jd.blockchain.utils.net.NetworkAddress; | |||
| import org.junit.Before; | |||
| import org.junit.Test; | |||
| import static org.junit.Assert.assertTrue; | |||
| /** | |||
| * 参与方状态更新测试 | |||
| * @author zhangshuang | |||
| * @create 2019/7/18 | |||
| * @since 1.0.0 | |||
| */ | |||
| public class SDK_GateWay_Participant_State_Update_Test_ { | |||
| private PrivKey privKey; | |||
| private PubKey pubKey; | |||
| private BlockchainKeypair CLIENT_CERT = null; | |||
| private String GATEWAY_IPADDR = null; | |||
| private int GATEWAY_PORT; | |||
| private boolean SECURE; | |||
| private BlockchainService service; | |||
| //根据密码工具产生的公私钥 | |||
| static String PUB = "3snPdw7i7PkdgqiGX7GbZuFSi1cwZn7vtjw4vifb1YoXgr9k6Kfmis"; | |||
| String PRIV = "177gjtZu8w1phqHFVNiFhA35cfimXmP6VuqrBFhfbXBWK8s4TRwro2tnpffwP1Emwr6SMN6"; | |||
| @Before | |||
| public void init() { | |||
| privKey = SDK_GateWay_KeyPair_Para.privkey1; | |||
| pubKey = SDK_GateWay_KeyPair_Para.pubKey1; | |||
| CLIENT_CERT = new BlockchainKeypair(SDK_GateWay_KeyPair_Para.pubKey0, SDK_GateWay_KeyPair_Para.privkey0); | |||
| GATEWAY_IPADDR = "127.0.0.1"; | |||
| GATEWAY_PORT = 11000; | |||
| SECURE = false; | |||
| GatewayServiceFactory serviceFactory = GatewayServiceFactory.connect(GATEWAY_IPADDR, GATEWAY_PORT, SECURE, | |||
| CLIENT_CERT); | |||
| service = serviceFactory.getBlockchainService(); | |||
| DataContractRegistry.register(TransactionContent.class); | |||
| DataContractRegistry.register(TransactionContentBody.class); | |||
| DataContractRegistry.register(TransactionRequest.class); | |||
| DataContractRegistry.register(NodeRequest.class); | |||
| DataContractRegistry.register(EndpointRequest.class); | |||
| DataContractRegistry.register(TransactionResponse.class); | |||
| DataContractRegistry.register(ParticipantRegisterOperation.class); | |||
| DataContractRegistry.register(ParticipantStateUpdateOperation.class); | |||
| DataContractRegistry.register(ParticipantStateUpdateOperation.class); | |||
| } | |||
| @Test | |||
| public void updateParticipantState_Test() { | |||
| HashDigest[] ledgerHashs = service.getLedgerHashs(); | |||
| // 在本地定义注册账号的 TX; | |||
| TransactionTemplate txTemp = service.newTransaction(ledgerHashs[0]); | |||
| //existed signer | |||
| AsymmetricKeypair keyPair = new BlockchainKeypair(pubKey, privKey); | |||
| PrivKey privKey = KeyGenUtils.decodePrivKeyWithRawPassword(PRIV, SDKDemo_Constant.PASSWORD); | |||
| PubKey pubKey = KeyGenUtils.decodePubKey(PUB); | |||
| System.out.println("Address = "+AddressEncoding.generateAddress(pubKey)); | |||
| BlockchainKeypair user = new BlockchainKeypair(pubKey, privKey); | |||
| NetworkAddress networkAddress = new NetworkAddress("127.0.0.1", 20000); | |||
| txTemp.states().update(user.getIdentity(),networkAddress, ParticipantNodeState.CONSENSUSED); | |||
| // TX 准备就绪; | |||
| PreparedTransaction prepTx = txTemp.prepare(); | |||
| // 使用私钥进行签名; | |||
| prepTx.sign(keyPair); | |||
| // 提交交易; | |||
| TransactionResponse transactionResponse = prepTx.commit(); | |||
| assertTrue(transactionResponse.isSuccess()); | |||
| } | |||
| } | |||
| @@ -11,6 +11,7 @@ import java.util.Set; | |||
| import java.util.concurrent.ConcurrentHashMap; | |||
| import java.util.stream.DoubleStream; | |||
| import com.jd.blockchain.ledger.*; | |||
| import org.springframework.core.io.ClassPathResource; | |||
| import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
| @@ -23,22 +24,10 @@ import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
| import com.jd.blockchain.crypto.HashDigest; | |||
| import com.jd.blockchain.crypto.KeyGenUtils; | |||
| import com.jd.blockchain.crypto.PrivKey; | |||
| import com.jd.blockchain.ledger.BlockchainIdentity; | |||
| import com.jd.blockchain.ledger.BlockchainKeyGenerator; | |||
| import com.jd.blockchain.ledger.BlockchainKeypair; | |||
| import com.jd.blockchain.ledger.BytesDataList; | |||
| import com.jd.blockchain.ledger.DataAccountKVSetOperation; | |||
| import com.jd.blockchain.ledger.DataAccountRegisterOperation; | |||
| import com.jd.blockchain.ledger.LedgerBlock; | |||
| import com.jd.blockchain.ledger.LedgerInitOperation; | |||
| import com.jd.blockchain.ledger.LedgerInitProperties; | |||
| import com.jd.blockchain.ledger.LedgerPermission; | |||
| import com.jd.blockchain.ledger.LedgerSecurityException; | |||
| import com.jd.blockchain.ledger.TransactionPermission; | |||
| import com.jd.blockchain.ledger.TransactionRequest; | |||
| import com.jd.blockchain.ledger.TransactionRequestBuilder; | |||
| import com.jd.blockchain.ledger.TransactionResponse; | |||
| import com.jd.blockchain.ledger.UserRegisterOperation; | |||
| import com.jd.blockchain.ledger.core.DefaultOperationHandleRegisteration; | |||
| import com.jd.blockchain.ledger.core.LedgerDataQuery; | |||
| import com.jd.blockchain.ledger.core.LedgerEditor; | |||
| @@ -98,6 +87,8 @@ public class LedgerPerformanceTest { | |||
| DataContractRegistry.register(UserRegisterOperation.class); | |||
| DataContractRegistry.register(DataAccountRegisterOperation.class); | |||
| DataContractRegistry.register(DataAccountKVSetOperation.class); | |||
| DataContractRegistry.register(ParticipantRegisterOperation.class); | |||
| DataContractRegistry.register(ParticipantStateUpdateOperation.class); | |||
| } | |||
| public static final LedgerSecurityManager DEFAULT_SECURITY_MANAGER = new FreedomLedgerSecurityManager(); | |||
| @@ -6,6 +6,7 @@ import java.util.Map; | |||
| import java.util.Properties; | |||
| import java.util.concurrent.ConcurrentHashMap; | |||
| import com.jd.blockchain.ledger.ParticipantNodeState; | |||
| import org.springframework.core.io.ClassPathResource; | |||
| import com.jd.blockchain.consensus.ConsensusProvider; | |||
| @@ -87,7 +88,7 @@ public class Utils { | |||
| public static ParticipantNode[] loadParticipantNodes() { | |||
| ParticipantNode[] participantNodes = new ParticipantNode[PUB_KEYS.length]; | |||
| for (int i = 0; i < PUB_KEYS.length; i++) { | |||
| participantNodes[i] = new PartNode(i, KeyGenUtils.decodePubKey(PUB_KEYS[i])); | |||
| participantNodes[i] = new PartNode(i, KeyGenUtils.decodePubKey(PUB_KEYS[i]), ParticipantNodeState.CONSENSUSED); | |||
| } | |||
| return participantNodes; | |||
| } | |||
| @@ -249,15 +250,18 @@ public class Utils { | |||
| private PubKey pubKey; | |||
| public PartNode(int id, PubKey pubKey) { | |||
| this(id, id + "", pubKey); | |||
| private ParticipantNodeState participantNodeState; | |||
| public PartNode(int id, PubKey pubKey, ParticipantNodeState participantNodeState) { | |||
| this(id, id + "", pubKey, participantNodeState); | |||
| } | |||
| public PartNode(int id, String name, PubKey pubKey) { | |||
| public PartNode(int id, String name, PubKey pubKey, ParticipantNodeState participantNodeState) { | |||
| this.id = id; | |||
| this.name = name; | |||
| this.pubKey = pubKey; | |||
| this.address = AddressEncoding.generateAddress(pubKey); | |||
| this.participantNodeState = participantNodeState; | |||
| } | |||
| @Override | |||
| @@ -279,6 +283,12 @@ public class Utils { | |||
| public PubKey getPubKey() { | |||
| return pubKey; | |||
| } | |||
| @Override | |||
| public ParticipantNodeState getParticipantNodeState() { | |||
| return participantNodeState; | |||
| } | |||
| } | |||
| } | |||
| @@ -25,6 +25,7 @@ import java.util.Random; | |||
| import java.util.concurrent.CountDownLatch; | |||
| import java.util.concurrent.atomic.AtomicLong; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.ledger.*; | |||
| import org.apache.commons.io.FileUtils; | |||
| import org.springframework.core.io.ClassPathResource; | |||
| @@ -166,6 +167,59 @@ public class IntegrationBase { | |||
| return kvResponse; | |||
| } | |||
| public static KeyPairResponse testSDK_RegisterParticipant(AsymmetricKeypair adminKey, HashDigest ledgerHash, | |||
| BlockchainService blockchainService) { | |||
| // 注册参与方,并验证最终写入; | |||
| BlockchainKeypair participant = BlockchainKeyGenerator.getInstance().generate(); | |||
| // 定义交易; | |||
| TransactionTemplate txTpl = blockchainService.newTransaction(ledgerHash); | |||
| txTpl.participants().register("peer4", new BlockchainIdentityData(participant.getPubKey()), new NetworkAddress("127.0.0.1", 20000)); | |||
| // 签名; | |||
| PreparedTransaction ptx = txTpl.prepare(); | |||
| HashDigest transactionHash = ptx.getHash(); | |||
| ptx.sign(adminKey); | |||
| // 提交并等待共识返回; | |||
| TransactionResponse txResp = ptx.commit(); | |||
| KeyPairResponse keyPairResponse = new KeyPairResponse(); | |||
| keyPairResponse.keyPair = participant; | |||
| keyPairResponse.txResp = txResp; | |||
| keyPairResponse.txHash = transactionHash; | |||
| return keyPairResponse; | |||
| } | |||
| public static KeyPairResponse testSDK_UpdateParticipantState(AsymmetricKeypair adminKey, BlockchainKeypair participantKeyPair, HashDigest ledgerHash, | |||
| BlockchainService blockchainService) { | |||
| // 定义交易; | |||
| TransactionTemplate txTpl = blockchainService.newTransaction(ledgerHash); | |||
| ParticipantInfoData participantInfoData = new ParticipantInfoData("peer4", participantKeyPair.getPubKey(), new NetworkAddress("127.0.0.1", 20000)); | |||
| txTpl.states().update(new BlockchainIdentityData(participantInfoData.getPubKey()), participantInfoData.getNetworkAddress(), ParticipantNodeState.CONSENSUSED); | |||
| // 签名; | |||
| PreparedTransaction ptx = txTpl.prepare(); | |||
| HashDigest transactionHash = ptx.getHash(); | |||
| ptx.sign(adminKey); | |||
| // 提交并等待共识返回; | |||
| TransactionResponse txResp = ptx.commit(); | |||
| KeyPairResponse keyPairResponse = new KeyPairResponse(); | |||
| keyPairResponse.keyPair = participantKeyPair; | |||
| keyPairResponse.txResp = txResp; | |||
| keyPairResponse.txHash = transactionHash; | |||
| return keyPairResponse; | |||
| } | |||
| public static void validKeyPair(IntegrationBase.KeyPairResponse keyPairResponse, LedgerRepository ledgerRepository, | |||
| KeyPairType keyPairType) { | |||
| TransactionResponse txResp = keyPairResponse.txResp; | |||
| @@ -1,5 +1,7 @@ | |||
| package test.com.jd.blockchain.intgr; | |||
| import com.jd.blockchain.consensus.ConsensusProviders; | |||
| import com.jd.blockchain.consensus.bftsmart.BftsmartConsensusSettings; | |||
| import com.jd.blockchain.crypto.AsymmetricKeypair; | |||
| import com.jd.blockchain.crypto.HashDigest; | |||
| import com.jd.blockchain.crypto.KeyGenUtils; | |||
| @@ -7,6 +9,9 @@ import com.jd.blockchain.crypto.PrivKey; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.gateway.GatewayConfigProperties; | |||
| import com.jd.blockchain.ledger.BlockchainKeypair; | |||
| import com.jd.blockchain.ledger.ParticipantNodeState; | |||
| import com.jd.blockchain.ledger.ParticipantStateUpdateInfo; | |||
| import com.jd.blockchain.ledger.ParticipantStateUpdateInfoData; | |||
| import com.jd.blockchain.ledger.core.LedgerRepository; | |||
| import com.jd.blockchain.sdk.BlockchainService; | |||
| import com.jd.blockchain.sdk.client.GatewayServiceFactory; | |||
| @@ -30,6 +35,10 @@ public class IntegrationTest4Bftsmart { | |||
| private static final boolean isRegisterDataAccount = true; | |||
| private static final boolean isRegisterParticipant = true; | |||
| private static final boolean isParticipantStateUpdate = true; | |||
| private static final boolean isWriteKv = true; | |||
| private static final String DB_TYPE_MEM = "mem"; | |||
| @@ -38,6 +47,8 @@ public class IntegrationTest4Bftsmart { | |||
| private static final String DB_TYPE_ROCKSDB = "rocksdb"; | |||
| public static final String BFTSMART_PROVIDER = "com.jd.blockchain.consensus.bftsmart.BftsmartConsensusProvider"; | |||
| @Test | |||
| public void test4Memory() { | |||
| test(LedgerInitConsensusConfig.bftsmartProvider, DB_TYPE_MEM, LedgerInitConsensusConfig.memConnectionStrings); | |||
| @@ -140,6 +151,41 @@ public class IntegrationTest4Bftsmart { | |||
| } | |||
| } | |||
| long participantCount = ledgerRepository.getAdminInfo(ledgerRepository.retrieveLatestBlock()).getParticipantCount(); | |||
| long userCount = ledgerRepository.getUserAccountSet(ledgerRepository.retrieveLatestBlock()).getTotalCount(); | |||
| System.out.printf("before add participant: participantCount = %d, userCount = %d\r\n", (int)participantCount, (int)userCount); | |||
| IntegrationBase.KeyPairResponse participantResponse; | |||
| if (isRegisterParticipant) { | |||
| participantResponse = IntegrationBase.testSDK_RegisterParticipant(adminKey, ledgerHash, blockchainService); | |||
| } | |||
| participantCount = ledgerRepository.getAdminInfo(ledgerRepository.retrieveLatestBlock()).getParticipantCount(); | |||
| userCount = ledgerRepository.getUserAccountSet(ledgerRepository.retrieveLatestBlock()).getTotalCount(); | |||
| System.out.printf("after add participant: participantCount = %d, userCount = %d\r\n", (int)participantCount, (int)userCount); | |||
| BftsmartConsensusSettings consensusSettings = (BftsmartConsensusSettings) ConsensusProviders.getProvider(BFTSMART_PROVIDER).getSettingsFactory().getConsensusSettingsEncoder().decode(ledgerRepository.getAdminInfo().getSettings().getConsensusSetting().toBytes()); | |||
| System.out.printf("update participant state before ,old consensus env node num = %d\r\n", consensusSettings.getNodes().length); | |||
| for (int i = 0; i < participantCount; i++) { | |||
| System.out.printf("part%d state = %d\r\n",i, ledgerRepository.getAdminInfo(ledgerRepository.retrieveLatestBlock()).getParticipants()[i].getParticipantNodeState().CODE); | |||
| } | |||
| if (isParticipantStateUpdate) { | |||
| IntegrationBase.testSDK_UpdateParticipantState(adminKey, new BlockchainKeypair(participantResponse.getKeyPair().getPubKey(), participantResponse.getKeyPair().getPrivKey()), ledgerHash, blockchainService); | |||
| } | |||
| BftsmartConsensusSettings consensusSettingsNew = (BftsmartConsensusSettings) ConsensusProviders.getProvider(BFTSMART_PROVIDER).getSettingsFactory().getConsensusSettingsEncoder().decode(ledgerRepository.getAdminInfo(ledgerRepository.retrieveLatestBlock()).getSettings().getConsensusSetting().toBytes()); | |||
| System.out.printf("update participant state after ,new consensus env node num = %d\r\n", consensusSettingsNew.getNodes().length); | |||
| for (int i = 0; i < participantCount; i++) { | |||
| System.out.printf("part%d state = %d\r\n",i, ledgerRepository.getAdminInfo(ledgerRepository.retrieveLatestBlock()).getParticipants()[i].getParticipantNodeState().CODE); | |||
| } | |||
| try { | |||
| System.out.println("----------------- Init Completed -----------------"); | |||
| Thread.sleep(Integer.MAX_VALUE); | |||
| @@ -1,5 +1,8 @@ | |||
| package test.com.jd.blockchain.intgr; | |||
| import com.jd.blockchain.consensus.ConsensusProviders; | |||
| import com.jd.blockchain.consensus.bftsmart.BftsmartConsensusSettings; | |||
| import com.jd.blockchain.consensus.mq.settings.MsgQueueConsensusSettings; | |||
| import com.jd.blockchain.crypto.AsymmetricKeypair; | |||
| import com.jd.blockchain.crypto.HashDigest; | |||
| import com.jd.blockchain.crypto.KeyGenUtils; | |||
| @@ -30,6 +33,10 @@ public class IntegrationTest4MQ { | |||
| private static final boolean isRegisterDataAccount = true; | |||
| private static final boolean isRegisterParticipant = true; | |||
| private static final boolean isParticipantStateUpdate = true; | |||
| private static final boolean isWriteKv = true; | |||
| private static final boolean isContract = false; | |||
| @@ -44,6 +51,8 @@ public class IntegrationTest4MQ { | |||
| private static final String DATA_RETRIEVAL_URL= "http://192.168.151.39:10001"; | |||
| public static final String MQ_PROVIDER = "com.jd.blockchain.consensus.mq.MsgQueueConsensusProvider"; | |||
| @Test | |||
| public void test4Memory() { | |||
| test(LedgerInitConsensusConfig.mqProvider, DB_TYPE_MEM, LedgerInitConsensusConfig.memConnectionStrings); | |||
| @@ -138,6 +147,43 @@ public class IntegrationTest4MQ { | |||
| integrationBase.testSDK_Contract(adminKey, ledgerHash, blockchainService,ledgerRepository); | |||
| } | |||
| long participantCount = ledgerRepository.getAdminInfo(ledgerRepository.retrieveLatestBlock()).getParticipantCount(); | |||
| long userCount = ledgerRepository.getUserAccountSet(ledgerRepository.retrieveLatestBlock()).getTotalCount(); | |||
| System.out.printf("before add participant: participantCount = %d, userCount = %d\r\n", (int)participantCount, (int)userCount); | |||
| IntegrationBase.KeyPairResponse participantResponse; | |||
| if (isRegisterParticipant) { | |||
| participantResponse = IntegrationBase.testSDK_RegisterParticipant(adminKey, ledgerHash, blockchainService); | |||
| } | |||
| participantCount = ledgerRepository.getAdminInfo(ledgerRepository.retrieveLatestBlock()).getParticipantCount(); | |||
| userCount = ledgerRepository.getUserAccountSet(ledgerRepository.retrieveLatestBlock()).getTotalCount(); | |||
| System.out.printf("after add participant: participantCount = %d, userCount = %d\r\n", (int)participantCount, (int)userCount); | |||
| MsgQueueConsensusSettings consensusSettings = (MsgQueueConsensusSettings) ConsensusProviders.getProvider(MQ_PROVIDER).getSettingsFactory().getConsensusSettingsEncoder().decode(ledgerRepository.getAdminInfo().getSettings().getConsensusSetting().toBytes()); | |||
| System.out.printf("update participant state before ,old consensus env node num = %d\r\n", consensusSettings.getNodes().length); | |||
| for (int i = 0; i < participantCount; i++) { | |||
| System.out.printf("part%d state = %d\r\n",i, ledgerRepository.getAdminInfo(ledgerRepository.retrieveLatestBlock()).getParticipants()[i].getParticipantNodeState().CODE); | |||
| } | |||
| if (isParticipantStateUpdate) { | |||
| IntegrationBase.testSDK_UpdateParticipantState(adminKey, new BlockchainKeypair(participantResponse.getKeyPair().getPubKey(), participantResponse.getKeyPair().getPrivKey()), ledgerHash, blockchainService); | |||
| } | |||
| BftsmartConsensusSettings consensusSettingsNew = (BftsmartConsensusSettings) ConsensusProviders.getProvider(MQ_PROVIDER).getSettingsFactory().getConsensusSettingsEncoder().decode(ledgerRepository.getAdminInfo(ledgerRepository.retrieveLatestBlock()).getSettings().getConsensusSetting().toBytes()); | |||
| System.out.printf("update participant state after ,new consensus env node num = %d\r\n", consensusSettingsNew.getNodes().length); | |||
| for (int i = 0; i < participantCount; i++) { | |||
| System.out.printf("part%d state = %d\r\n",i, ledgerRepository.getAdminInfo(ledgerRepository.retrieveLatestBlock()).getParticipants()[i].getParticipantNodeState().CODE); | |||
| } | |||
| IntegrationBase.testConsistencyAmongNodes(ledgers); | |||
| if(isOnline){ | |||
| @@ -6,7 +6,7 @@ system.msg.queue.block.txsize=1000 | |||
| system.msg.queue.block.maxdelay=2000 | |||
| system.servers.num=4 | |||
| system.server.0.pubkey=endPsK36koyFr1D245Sa9j83vt6pZUdFBJoJRB3xAsWM6cwhRbna | |||
| system.server.1.pubkey=endPsK36sC5JdPCDPDAXUwZtS3sxEmqEhFcC4whayAsTTh8Z6eoZ | |||
| system.server.2.pubkey=endPsK36jEG281HMHeh6oSqzqLkT95DTnCM6REDURjdb2c67uR3R | |||
| system.server.3.pubkey=endPsK36nse1dck4uF19zPvAMijCV336Y3zWdgb4rQG8QoRj5ktR | |||
| system.server.0.pubkey=3snPdw7i7PjVKiTH2VnXZu5H8QmNaSXpnk4ei533jFpuifyjS5zzH9 | |||
| system.server.1.pubkey=3snPdw7i7PajLB35tEau1kmixc6ZrjLXgxwKbkv5bHhP7nT5dhD9eX | |||
| system.server.2.pubkey=3snPdw7i7PZi6TStiyc6mzjprnNhgs2atSGNS8wPYzhbKaUWGFJt7x | |||
| system.server.3.pubkey=3snPdw7i7PifPuRX7fu3jBjsb3rJRfDe9GtbDfvFJaJ4V4hHXQfhwk | |||
| @@ -18,19 +18,7 @@ import com.jd.blockchain.crypto.HashDigest; | |||
| import com.jd.blockchain.crypto.KeyGenUtils; | |||
| import com.jd.blockchain.crypto.PrivKey; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.ledger.ContractCodeDeployOperation; | |||
| import com.jd.blockchain.ledger.ContractEventSendOperation; | |||
| 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.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.*; | |||
| import com.jd.blockchain.utils.codec.Base58Utils; | |||
| /** | |||
| @@ -58,6 +46,8 @@ public class SettingsInit { | |||
| DataContractRegistry.register(ContractEventSendOperation.class); | |||
| DataContractRegistry.register(DataAccountRegisterOperation.class); | |||
| DataContractRegistry.register(UserRegisterOperation.class); | |||
| DataContractRegistry.register(ParticipantRegisterOperation.class); | |||
| DataContractRegistry.register(ParticipantStateUpdateOperation.class); | |||
| DataContractRegistry.register(ActionResponse.class); | |||
| @@ -12,6 +12,7 @@ import java.util.Properties; | |||
| import org.mockito.Mockito; | |||
| import com.jd.blockchain.binaryproto.DataContract; | |||
| import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
| import com.jd.blockchain.consensus.ClientIdentification; | |||
| import com.jd.blockchain.consensus.ClientIdentifications; | |||
| @@ -123,8 +124,10 @@ public class MockerNodeContext implements BlockchainQueryService { | |||
| DataContractRegistry.register(ContractEventSendOperation.class); | |||
| DataContractRegistry.register(DataAccountRegisterOperation.class); | |||
| DataContractRegistry.register(UserRegisterOperation.class); | |||
| DataContractRegistry.register(ParticipantRegisterOperation.class); | |||
| DataContractRegistry.register(DataAccountKVSetOperation.class); | |||
| DataContractRegistry.register(DataAccountKVSetOperation.KVWriteEntry.class); | |||
| DataContractRegistry.register(ParticipantStateUpdateOperation.class); | |||
| DataContractRegistry.register(ActionRequest.class); | |||
| DataContractRegistry.register(ActionResponse.class); | |||