| @@ -49,9 +49,11 @@ 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_RESULT = 0x360; | |||
| public static final int TX_RESPONSE = 0x360; | |||
| public static final int TX_OP_RESULT = 0x370; | |||
| public static final int METADATA = 0x600; | |||
| @@ -69,7 +71,7 @@ public interface DataCodes { | |||
| public static final int METADATA_CONSENSUS_SETTING = 0x631; | |||
| // public static final int METADATA_PARTICIPANT_INFO = 0x640; | |||
| public static final int METADATA_PARTICIPANT_INFO = 0x640; | |||
| public static final int METADATA_CRYPTO_SETTING = 0x642; | |||
| @@ -100,6 +102,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; | |||
| @@ -21,11 +22,13 @@ import com.jd.blockchain.consensus.mq.settings.MsgQueueNetworkSettings; | |||
| import com.jd.blockchain.consensus.mq.settings.MsgQueueNodeSettings; | |||
| import com.jd.blockchain.crypto.AddressEncoding; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| 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.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); | |||
| @@ -9,24 +9,7 @@ import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
| import com.jd.blockchain.crypto.HashDigest; | |||
| 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.tools.keygen.KeyGenCommand; | |||
| @@ -111,6 +94,7 @@ public enum ContractDeployExeUtil { | |||
| DataContractRegistry.register(ContractEventSendOperation.class); | |||
| DataContractRegistry.register(DataAccountRegisterOperation.class); | |||
| DataContractRegistry.register(UserRegisterOperation.class); | |||
| DataContractRegistry.register(ParticipantRegisterOperation.class); | |||
| } | |||
| public BlockchainService initBcsrv(String host, int port) { | |||
| @@ -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> | |||
| @@ -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(); | |||
| // | |||
| //} | |||
| @@ -2,6 +2,7 @@ package com.jd.blockchain.ledger.core; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.ledger.ParticipantNode; | |||
| import com.jd.blockchain.ledger.ParticipantNodeState; | |||
| /** | |||
| * 参与方证书数据对象; | |||
| @@ -15,6 +16,7 @@ public class ParticipantCertData implements ParticipantNode { | |||
| private String address; | |||
| private String name; | |||
| private PubKey pubKey; | |||
| private ParticipantNodeState participantNodeState; | |||
| public ParticipantCertData() { | |||
| } | |||
| @@ -24,6 +26,7 @@ public class ParticipantCertData implements ParticipantNode { | |||
| this.address = participantNode.getAddress(); | |||
| this.name = participantNode.getName(); | |||
| this.pubKey = participantNode.getPubKey(); | |||
| this.participantNodeState = participantNode.getParticipantNodeState(); | |||
| } | |||
| public ParticipantCertData(String address, String name, PubKey pubKey) { | |||
| @@ -54,4 +57,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; | |||
| } | |||
| } | |||
| @@ -3,15 +3,11 @@ package com.jd.blockchain.ledger.core.impl; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| import com.jd.blockchain.ledger.core.impl.handles.*; | |||
| import org.springframework.stereotype.Component; | |||
| import com.jd.blockchain.ledger.LedgerException; | |||
| import com.jd.blockchain.ledger.core.OperationHandle; | |||
| import com.jd.blockchain.ledger.core.impl.handles.ContractCodeDeployOperationHandle; | |||
| import com.jd.blockchain.ledger.core.impl.handles.JVMContractEventSendOperationHandle; | |||
| import com.jd.blockchain.ledger.core.impl.handles.DataAccountKVSetOperationHandle; | |||
| import com.jd.blockchain.ledger.core.impl.handles.DataAccountRegisterOperationHandle; | |||
| import com.jd.blockchain.ledger.core.impl.handles.UserRegisterOperationHandle; | |||
| @Component | |||
| public class DefaultOperationHandleRegisteration implements OperationHandleRegisteration { | |||
| @@ -29,6 +25,7 @@ public class DefaultOperationHandleRegisteration implements OperationHandleRegis | |||
| opHandles.add(new DataAccountKVSetOperationHandle()); | |||
| opHandles.add(new DataAccountRegisterOperationHandle()); | |||
| opHandles.add(new UserRegisterOperationHandle()); | |||
| opHandles.add(new ParticipantRegisterOperationHandle()); | |||
| opHandles.add(new ContractCodeDeployOperationHandle()); | |||
| opHandles.add(new JVMContractEventSendOperationHandle()); | |||
| } | |||
| @@ -0,0 +1,105 @@ | |||
| package com.jd.blockchain.ledger.core.impl.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.ledger.core.impl.OperationHandleContext; | |||
| import com.jd.blockchain.utils.Bytes; | |||
| public class ParticipantRegisterOperationHandle implements OperationHandle { | |||
| @Override | |||
| public BytesValue process(Operation op, LedgerDataSet dataset, TransactionRequestContext requestContext, | |||
| LedgerDataSet previousBlockDataset, OperationHandleContext handleContext, LedgerService ledgerService) { | |||
| ParticipantRegisterOperation participantRegOp = (ParticipantRegisterOperation) op; | |||
| System.out.println("ParticipantRegisterOperationHandle start\r\n"); | |||
| LedgerAdminAccount adminAccount = dataset.getAdminAccount(); | |||
| ParticipantInfo participantInfo = participantRegOp.getParticipantInfo(); | |||
| ConsensusProvider provider = ConsensusProviders.getProvider(adminAccount.getSetting().getConsensusProvider()); | |||
| ParticipantNode participantNode = new PartNode((int)(adminAccount.getParticipantCount()), participantInfo.getName(), participantInfo.getPubKey(), ParticipantNodeState.REGISTED); | |||
| LedgerAdminAccount.LedgerMetadataImpl metadata = (LedgerAdminAccount.LedgerMetadataImpl) adminAccount.getMetadata(); | |||
| PubKey pubKey = participantNode.getPubKey(); | |||
| BlockchainIdentityData identityData = new BlockchainIdentityData(pubKey); | |||
| //update consensus setting | |||
| Bytes newConsensusSettings = provider.getSettingsFactory().getConsensusSettingsBuilder().updateSettings(metadata.getSetting().getConsensusSetting(), participantInfo); | |||
| LedgerSetting ledgerSetting = new LedgerConfiguration(adminAccount.getSetting().getConsensusProvider(), | |||
| newConsensusSettings, metadata.getSetting().getCryptoSetting()); | |||
| metadata.setSetting(ledgerSetting); | |||
| // metadata.setViewId(metadata.getViewId() + 1); | |||
| //reg participant as user | |||
| dataset.getUserAccountSet().register(identityData.getAddress(), pubKey); | |||
| //add new participant as consensus node | |||
| adminAccount.addParticipant(participantNode); | |||
| return null; | |||
| } | |||
| @Override | |||
| public boolean support(Class<?> operationType) { | |||
| return ParticipantRegisterOperation.class.isAssignableFrom(operationType); | |||
| } | |||
| private static class PartNode implements ParticipantNode { | |||
| private int id; | |||
| private String 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).toBase58(); | |||
| this.participantNodeState = participantNodeState; | |||
| } | |||
| @Override | |||
| public int getId() { | |||
| return id; | |||
| } | |||
| @Override | |||
| public String getAddress() { | |||
| return address; | |||
| } | |||
| @Override | |||
| public String getName() { | |||
| return name; | |||
| } | |||
| @Override | |||
| public PubKey getPubKey() { | |||
| return pubKey; | |||
| } | |||
| @Override | |||
| public ParticipantNodeState getParticipantNodeState() { | |||
| return participantNodeState; | |||
| } | |||
| } | |||
| } | |||
| @@ -32,6 +32,7 @@ public class ContractInvokingTest { | |||
| DataContractRegistry.register(EndpointRequest.class); | |||
| DataContractRegistry.register(TransactionResponse.class); | |||
| DataContractRegistry.register(UserRegisterOperation.class); | |||
| DataContractRegistry.register(ParticipantRegisterOperation.class); | |||
| } | |||
| private static final String LEDGER_KEY_PREFIX = "LDG://"; | |||
| @@ -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.ContractAccountSet; | |||
| import com.jd.blockchain.ledger.core.CryptoConfig; | |||
| import com.jd.blockchain.ledger.core.DataAccountSet; | |||
| @@ -56,6 +44,7 @@ public class LedgerManagerTest { | |||
| DataContractRegistry.register(TransactionContent.class); | |||
| DataContractRegistry.register(UserRegisterOperation.class); | |||
| DataContractRegistry.register(DataAccountRegisterOperation.class); | |||
| DataContractRegistry.register(ParticipantRegisterOperation.class); | |||
| DataContractRegistry.register(BlockBody.class); | |||
| DataContractRegistry.register(CryptoProvider.class); | |||
| } | |||
| @@ -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.TransactionSet; | |||
| import com.jd.blockchain.ledger.core.impl.LedgerTransactionData; | |||
| import com.jd.blockchain.ledger.core.impl.TransactionStagedSnapshot; | |||
| @@ -51,6 +36,7 @@ public class TransactionSetTest { | |||
| DataContractRegistry.register(DataAccountKVSetOperation.class); | |||
| DataContractRegistry.register(ContractCodeDeployOperation.class); | |||
| DataContractRegistry.register(ContractEventSendOperation.class); | |||
| DataContractRegistry.register(ParticipantRegisterOperation.class); | |||
| CryptoSetting defCryptoSetting = LedgerTestUtils.createDefaultCryptoSetting(); | |||
| MemoryKVStorage testStorage = new MemoryKVStorage(); | |||
| @@ -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; | |||
| * | |||
| * @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,65 @@ | |||
| package com.jd.blockchain.ledger; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.utils.net.NetworkAddress; | |||
| /** | |||
| * 即将要注册的参与方的信息 | |||
| * @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 flag, String name, PubKey pubKey, NetworkAddress networkAddress) { | |||
| this.flag = flag; | |||
| this.name = name; | |||
| this.pubKey = pubKey; | |||
| this.networkAddress = networkAddress; | |||
| } | |||
| @Override | |||
| public String getFlag() { | |||
| return flag; | |||
| } | |||
| public void setFlag(String flag) { | |||
| this.flag = flag; | |||
| } | |||
| @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; | |||
| } | |||
| } | |||
| @@ -48,4 +48,12 @@ public interface ParticipantNode {// extends ConsensusNode, ParticipantInfo { | |||
| */ | |||
| @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,13 @@ | |||
| package com.jd.blockchain.ledger; | |||
| import com.jd.blockchain.binaryproto.DataContract; | |||
| import com.jd.blockchain.binaryproto.DataField; | |||
| import com.jd.blockchain.consts.DataCodes; | |||
| @DataContract(code= DataCodes.TX_OP_PARTICIPANT_REG) | |||
| public interface ParticipantRegisterOperation extends Operation { | |||
| @DataField(order=1, refContract = true) | |||
| ParticipantInfo getParticipantInfo(); | |||
| } | |||
| @@ -4,17 +4,7 @@ import java.util.ArrayList; | |||
| import java.util.Collection; | |||
| import java.util.List; | |||
| import com.jd.blockchain.ledger.BlockchainIdentity; | |||
| import com.jd.blockchain.ledger.BytesValue; | |||
| import com.jd.blockchain.ledger.BytesValueList; | |||
| 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.LedgerInitOperation; | |||
| import com.jd.blockchain.ledger.LedgerInitSetting; | |||
| import com.jd.blockchain.ledger.Operation; | |||
| import com.jd.blockchain.ledger.UserRegisterOperation; | |||
| import com.jd.blockchain.ledger.*; | |||
| import com.jd.blockchain.utils.Bytes; | |||
| /** | |||
| @@ -33,6 +23,8 @@ public class BlockchainOperationFactory implements ClientOperator, LedgerInitOpe | |||
| // private static final ContractEventSendOperationBuilderImpl CONTRACT_EVENT_SEND_OP_BUILDER = new ContractEventSendOperationBuilderImpl(); | |||
| private static final ParticipantRegisterOperationBuilderImpl PARTICIPANT_REG_OP_BUILDER = new ParticipantRegisterOperationBuilderImpl(); | |||
| private LedgerInitOperationBuilder ledgerInitOpBuilder = new LedgerInitOperationBuilderFilter(); | |||
| private UserRegisterOperationBuilder userRegOpBuilder = new UserRegisterOperationBuilderFilter(); | |||
| @@ -45,6 +37,8 @@ public class BlockchainOperationFactory implements ClientOperator, LedgerInitOpe | |||
| private ContractInvocationProxyBuilder contractInvoProxyBuilder = new ContractInvocationProxyBuilder(); | |||
| private ParticipantRegisterOperationBuilder participantRegOpBuilder = new ParticipantRegisterOperationBuilderFilter(); | |||
| // TODO: 暂时只支持单线程情形,未考虑多线程; | |||
| private List<Operation> operationList = new ArrayList<>(); | |||
| @@ -82,6 +76,9 @@ public class BlockchainOperationFactory implements ClientOperator, LedgerInitOpe | |||
| return contractEventSendOpBuilder; | |||
| } | |||
| @Override | |||
| public ParticipantRegisterOperationBuilder participants() {return participantRegOpBuilder;} | |||
| @Override | |||
| public <T> T contract(String address, Class<T> contractIntf) { | |||
| return contractInvoProxyBuilder.create(address, contractIntf, contractEventSendOpBuilder); | |||
| @@ -256,6 +253,15 @@ public class BlockchainOperationFactory implements ClientOperator, LedgerInitOpe | |||
| } | |||
| } | |||
| private class ParticipantRegisterOperationBuilderFilter implements ParticipantRegisterOperationBuilder { | |||
| @Override | |||
| public ParticipantRegisterOperation register(ParticipantInfo participantInfo) { | |||
| ParticipantRegisterOperation op = PARTICIPANT_REG_OP_BUILDER.register(participantInfo); | |||
| operationList.add(op); | |||
| return op; | |||
| } | |||
| } | |||
| private class ContractEventSendOperationBuilderFilter implements ContractEventSendOperationBuilder { | |||
| @Override | |||
| @@ -6,6 +6,6 @@ package com.jd.blockchain.transaction; | |||
| * @author huanghaiquan | |||
| * | |||
| */ | |||
| public interface ClientOperator extends UserOperator, DataAccountOperator, ContractOperator, EventOperator { | |||
| public interface ClientOperator extends UserOperator, DataAccountOperator, ContractOperator, EventOperator, ParticipantOperator { | |||
| } | |||
| @@ -2,6 +2,7 @@ package com.jd.blockchain.transaction; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.ledger.ParticipantNode; | |||
| import com.jd.blockchain.ledger.ParticipantNodeState; | |||
| import com.jd.blockchain.utils.net.NetworkAddress; | |||
| public class ConsensusParticipantData implements ParticipantNode { | |||
| @@ -16,6 +17,9 @@ public class ConsensusParticipantData implements ParticipantNode { | |||
| private NetworkAddress hostAddress; | |||
| private ParticipantNodeState participantNodeState; | |||
| public int getId() { | |||
| return id; | |||
| } | |||
| @@ -56,4 +60,12 @@ public class ConsensusParticipantData implements ParticipantNode { | |||
| this.address = address; | |||
| } | |||
| public ParticipantNodeState getParticipantNodeState() { | |||
| return participantNodeState; | |||
| } | |||
| public void setParticipantState(ParticipantNodeState participantNodeState) { | |||
| this.participantNodeState = participantNodeState; | |||
| } | |||
| } | |||
| @@ -0,0 +1,11 @@ | |||
| package com.jd.blockchain.transaction; | |||
| public interface ParticipantOperator { | |||
| /** | |||
| * 注册参与方操作; | |||
| * | |||
| * @return | |||
| */ | |||
| ParticipantRegisterOperationBuilder participants(); | |||
| } | |||
| @@ -0,0 +1,24 @@ | |||
| package com.jd.blockchain.transaction; | |||
| import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
| import com.jd.blockchain.ledger.ParticipantInfo; | |||
| import com.jd.blockchain.ledger.ParticipantRegisterOperation; | |||
| public class ParticipantRegisterOpTemplate implements ParticipantRegisterOperation { | |||
| static { | |||
| DataContractRegistry.register(ParticipantRegisterOperation.class); | |||
| } | |||
| private ParticipantInfo participantInfo; | |||
| public ParticipantRegisterOpTemplate(ParticipantInfo participantInfo) { | |||
| this.participantInfo = participantInfo; | |||
| } | |||
| @Override | |||
| public ParticipantInfo getParticipantInfo() { | |||
| return participantInfo; | |||
| } | |||
| } | |||
| @@ -0,0 +1,20 @@ | |||
| package com.jd.blockchain.transaction; | |||
| import com.jd.blockchain.ledger.ParticipantInfo; | |||
| import com.jd.blockchain.ledger.ParticipantRegisterOperation; | |||
| public interface ParticipantRegisterOperationBuilder { | |||
| /** | |||
| * 注册; | |||
| * | |||
| * @param | |||
| * | |||
| * @param | |||
| * | |||
| * @return | |||
| */ | |||
| ParticipantRegisterOperation register(ParticipantInfo participantInfo); | |||
| } | |||
| @@ -0,0 +1,11 @@ | |||
| package com.jd.blockchain.transaction; | |||
| import com.jd.blockchain.ledger.ParticipantInfo; | |||
| import com.jd.blockchain.ledger.ParticipantRegisterOperation; | |||
| public class ParticipantRegisterOperationBuilderImpl implements ParticipantRegisterOperationBuilder { | |||
| @Override | |||
| public ParticipantRegisterOperation register(ParticipantInfo participantNode) { | |||
| return new ParticipantRegisterOpTemplate(participantNode); | |||
| } | |||
| } | |||
| @@ -110,6 +110,9 @@ public class TxBuilder implements TransactionBuilder { | |||
| return opFactory.contractEvents(); | |||
| } | |||
| @Override | |||
| public ParticipantRegisterOperationBuilder participants() {return opFactory.participants(); } | |||
| @Override | |||
| public <T> T contract(Bytes address, Class<T> contractIntf) { | |||
| return opFactory.contract(address, contractIntf); | |||
| @@ -65,6 +65,12 @@ public class TxTemplate implements TransactionTemplate { | |||
| return txBuilder.contracts(); | |||
| } | |||
| @Override | |||
| public ParticipantRegisterOperationBuilder participants() { | |||
| stateManager.operate(); | |||
| return txBuilder.participants(); | |||
| } | |||
| @Override | |||
| public <T> T contract(Bytes address, Class<T> contractIntf) { | |||
| stateManager.operate(); | |||
| @@ -46,6 +46,7 @@ public class BinaryMessageConverter extends AbstractHttpMessageConverter<Object> | |||
| DataContractRegistry.register(ContractEventSendOperation.class); | |||
| DataContractRegistry.register(DataAccountRegisterOperation.class); | |||
| DataContractRegistry.register(UserRegisterOperation.class); | |||
| DataContractRegistry.register(ParticipantRegisterOperation.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,21 +32,6 @@ import com.jd.blockchain.consensus.service.NodeServer; | |||
| import com.jd.blockchain.consensus.service.ServerSettings; | |||
| import com.jd.blockchain.consensus.service.StateMachineReplicate; | |||
| import com.jd.blockchain.crypto.HashDigest; | |||
| import com.jd.blockchain.ledger.ContractCodeDeployOperation; | |||
| import com.jd.blockchain.ledger.ContractEventSendOperation; | |||
| import com.jd.blockchain.ledger.CryptoSetting; | |||
| import com.jd.blockchain.ledger.DataAccountKVSetOperation; | |||
| import com.jd.blockchain.ledger.DataAccountRegisterOperation; | |||
| import com.jd.blockchain.ledger.EndpointRequest; | |||
| import com.jd.blockchain.ledger.LedgerBlock; | |||
| import com.jd.blockchain.ledger.LedgerInitOperation; | |||
| import com.jd.blockchain.ledger.NodeRequest; | |||
| import com.jd.blockchain.ledger.Operation; | |||
| import com.jd.blockchain.ledger.TransactionContent; | |||
| import com.jd.blockchain.ledger.TransactionContentBody; | |||
| import com.jd.blockchain.ledger.TransactionRequest; | |||
| import com.jd.blockchain.ledger.TransactionResponse; | |||
| import com.jd.blockchain.ledger.UserRegisterOperation; | |||
| import com.jd.blockchain.ledger.core.LedgerAdminAccount; | |||
| import com.jd.blockchain.ledger.core.LedgerManage; | |||
| import com.jd.blockchain.ledger.core.LedgerRepository; | |||
| @@ -116,6 +102,7 @@ public class ManagementController implements LedgerBindingConfigAware, PeerManag | |||
| DataContractRegistry.register(ContractEventSendOperation.class); | |||
| DataContractRegistry.register(DataAccountRegisterOperation.class); | |||
| DataContractRegistry.register(UserRegisterOperation.class); | |||
| DataContractRegistry.register(ParticipantRegisterOperation.class); | |||
| DataContractRegistry.register(ActionResponse.class); | |||
| @@ -193,7 +193,7 @@ public class ClientResolveUtil { | |||
| JSONObject pubKeyObj = currConsensusParticipant.getJSONObject("pubKey"); | |||
| String pubKeyBase58 = pubKeyObj.getString("value"); | |||
| // 生成ParticipantNode对象 | |||
| ParticipantCertData participantCertData = new ParticipantCertData(id, addressBase58, name, new PubKey(Bytes.fromBase58(pubKeyBase58).toBytes())); | |||
| ParticipantCertData participantCertData = new ParticipantCertData(id, addressBase58, name, new PubKey(Bytes.fromBase58(pubKeyBase58).toBytes()), ParticipantNodeState.CONSENSUSED); | |||
| participantNodes[i] = participantCertData; | |||
| } | |||
| ledgerInitSettingData.setConsensusParticipants(participantNodes); | |||
| @@ -275,6 +275,7 @@ public class ClientResolveUtil { | |||
| private String address; | |||
| private String name; | |||
| private PubKey pubKey; | |||
| private ParticipantNodeState participantNodeState; | |||
| public ParticipantCertData() { | |||
| } | |||
| @@ -285,11 +286,12 @@ public class ClientResolveUtil { | |||
| this.pubKey = participantNode.getPubKey(); | |||
| } | |||
| public ParticipantCertData(int id, String address, String name, PubKey pubKey) { | |||
| public ParticipantCertData(int id, String address, String name, PubKey pubKey, ParticipantNodeState participantNodeState) { | |||
| this.id = id; | |||
| this.address = address; | |||
| this.name = name; | |||
| this.pubKey = pubKey; | |||
| this.participantNodeState = participantNodeState; | |||
| } | |||
| @Override | |||
| @@ -314,6 +316,12 @@ public class ClientResolveUtil { | |||
| public void setId(int id) { | |||
| this.id = id; | |||
| } | |||
| @Override | |||
| public ParticipantNodeState getParticipantNodeState() { | |||
| return participantNodeState; | |||
| } | |||
| } | |||
| public static class KvData implements KVDataEntry { | |||
| @@ -47,6 +47,7 @@ public class GatewayServiceFactory implements BlockchainServiceFactory, Closeabl | |||
| DataContractRegistry.register(ContractEventSendOperation.class); | |||
| DataContractRegistry.register(DataAccountRegisterOperation.class); | |||
| DataContractRegistry.register(UserRegisterOperation.class); | |||
| DataContractRegistry.register(ParticipantRegisterOperation.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_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); | |||
| } | |||
| @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 = KeyGenCommand.decodePrivKeyWithRawPassword(PRIV, SDKDemo_Constant.PASSWORD); | |||
| PubKey pubKey = KeyGenCommand.decodePubKey(PUB); | |||
| System.out.println("Address = "+AddressEncoding.generateAddress(pubKey)); | |||
| BlockchainKeypair user = new BlockchainKeypair(pubKey, privKey); | |||
| NetworkAddress networkAddress = new NetworkAddress("127.0.0.1", 20000); | |||
| ParticipantInfo participantInfo = new ParticipantInfoData("add","Peer4", user.getPubKey(), networkAddress); | |||
| // 注册参与方 | |||
| txTemp.participants().register(participantInfo); | |||
| // TX 准备就绪; | |||
| PreparedTransaction prepTx = txTemp.prepare(); | |||
| // 使用私钥进行签名; | |||
| prepTx.sign(keyPair); | |||
| // 提交交易; | |||
| TransactionResponse transactionResponse = prepTx.commit(); | |||
| assertTrue(transactionResponse.isSuccess()); | |||
| } | |||
| } | |||
| @@ -11,6 +11,7 @@ import java.util.concurrent.ConcurrentHashMap; | |||
| import java.util.stream.DoubleStream; | |||
| import com.jd.blockchain.crypto.*; | |||
| import com.jd.blockchain.ledger.*; | |||
| import com.jd.blockchain.ledger.core.CryptoConfig; | |||
| import org.springframework.core.io.ClassPathResource; | |||
| @@ -18,18 +19,6 @@ import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
| import com.jd.blockchain.consensus.ConsensusProvider; | |||
| import com.jd.blockchain.consensus.ConsensusProviders; | |||
| import com.jd.blockchain.consensus.ConsensusSettings; | |||
| import com.jd.blockchain.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.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.LedgerDataSet; | |||
| import com.jd.blockchain.ledger.core.LedgerEditor; | |||
| import com.jd.blockchain.ledger.core.LedgerRepository; | |||
| @@ -87,6 +76,7 @@ public class LedgerPerformanceTest { | |||
| DataContractRegistry.register(UserRegisterOperation.class); | |||
| DataContractRegistry.register(DataAccountRegisterOperation.class); | |||
| DataContractRegistry.register(DataAccountKVSetOperation.class); | |||
| DataContractRegistry.register(ParticipantRegisterOperation.class); | |||
| } | |||
| public static void test(String[] args) { | |||
| @@ -10,6 +10,7 @@ import com.jd.blockchain.crypto.*; | |||
| import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
| import com.jd.blockchain.crypto.service.sm.SMCryptoService; | |||
| import com.jd.blockchain.ledger.ParticipantNode; | |||
| import com.jd.blockchain.ledger.ParticipantNodeState; | |||
| import com.jd.blockchain.tools.keygen.KeyGenCommand; | |||
| import org.springframework.core.io.ClassPathResource; | |||
| @@ -77,7 +78,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, KeyGenCommand.decodePubKey(PUB_KEYS[i])); | |||
| participantNodes[i] = new PartNode(i, KeyGenCommand.decodePubKey(PUB_KEYS[i]), ParticipantNodeState.CONSENSUSED); | |||
| } | |||
| return participantNodes; | |||
| } | |||
| @@ -235,15 +236,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 = pubKey.toBase58(); | |||
| this.participantNodeState = participantNodeState; | |||
| } | |||
| @Override | |||
| @@ -265,6 +269,12 @@ public class Utils { | |||
| public PubKey getPubKey() { | |||
| return pubKey; | |||
| } | |||
| @Override | |||
| public ParticipantNodeState getParticipantNodeState() { | |||
| return participantNodeState; | |||
| } | |||
| } | |||
| } | |||
| @@ -166,6 +166,35 @@ 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); | |||
| ParticipantInfoData participantInfoData = new ParticipantInfoData("add", "peer4", participant.getPubKey(), new NetworkAddress("127.0.0.1", 20000)); | |||
| txTpl.participants().register(participantInfoData); | |||
| // 签名; | |||
| 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 void validKeyPair(IntegrationBase.KeyPairResponse keyPairResponse, LedgerRepository ledgerRepository, | |||
| KeyPairType keyPairType) { | |||
| TransactionResponse txResp = keyPairResponse.txResp; | |||
| @@ -17,19 +17,7 @@ import com.jd.blockchain.crypto.AsymmetricKeypair; | |||
| import com.jd.blockchain.crypto.HashDigest; | |||
| 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.tools.keygen.KeyGenCommand; | |||
| import com.jd.blockchain.utils.codec.Base58Utils; | |||
| @@ -58,6 +46,7 @@ public class SettingsInit { | |||
| DataContractRegistry.register(ContractEventSendOperation.class); | |||
| DataContractRegistry.register(DataAccountRegisterOperation.class); | |||
| DataContractRegistry.register(UserRegisterOperation.class); | |||
| DataContractRegistry.register(ParticipantRegisterOperation.class); | |||
| DataContractRegistry.register(ActionResponse.class); | |||
| @@ -13,6 +13,7 @@ import com.jd.blockchain.consts.Global; | |||
| import com.jd.blockchain.crypto.AddressEncoding; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.ledger.ParticipantNode; | |||
| import com.jd.blockchain.ledger.ParticipantNodeState; | |||
| import com.jd.blockchain.tools.keygen.KeyGenCommand; | |||
| import com.jd.blockchain.utils.PropertiesUtils; | |||
| import com.jd.blockchain.utils.codec.HexUtils; | |||
| @@ -272,6 +273,8 @@ public class LedgerInitProperties { | |||
| // private NetworkAddress consensusAddress; | |||
| private ParticipantNodeState participantNodeState; | |||
| private NetworkAddress initializerAddress; | |||
| public int getId() { | |||
| @@ -303,6 +306,14 @@ public class LedgerInitProperties { | |||
| // this.pubKeyPath = pubKeyPath; | |||
| // } | |||
| public ParticipantNodeState getParticipantNodeState() { | |||
| return participantNodeState; | |||
| } | |||
| public void setParticipantNodeState(ParticipantNodeState participantNodeState) { | |||
| this.participantNodeState = participantNodeState; | |||
| } | |||
| public NetworkAddress getInitializerAddress() { | |||
| return initializerAddress; | |||
| } | |||
| @@ -86,6 +86,7 @@ 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); | |||