| @@ -1,28 +1,22 @@ | |||
| package com.jd.blockchain.consensus.bftsmart.client; | |||
| import bftsmart.reconfiguration.util.TOMConfiguration; | |||
| import com.jd.blockchain.consensus.ClientIdentification; | |||
| import java.util.concurrent.atomic.AtomicInteger; | |||
| import java.util.regex.Matcher; | |||
| import java.util.regex.Pattern; | |||
| import com.jd.blockchain.consensus.ClientIncomingSettings; | |||
| import com.jd.blockchain.consensus.ConsensusManageService; | |||
| import com.jd.blockchain.consensus.ConsensusSecurityException; | |||
| import com.jd.blockchain.consensus.bftsmart.BftsmartClientIncomingSettings; | |||
| import com.jd.blockchain.consensus.bftsmart.BftsmartTopology; | |||
| import com.jd.blockchain.consensus.bftsmart.service.BftsmartConsensusManageService; | |||
| import com.jd.blockchain.consensus.client.ClientFactory; | |||
| import com.jd.blockchain.consensus.client.ClientSettings; | |||
| import com.jd.blockchain.consensus.client.ConsensusClient; | |||
| import com.jd.blockchain.crypto.CryptoUtils; | |||
| import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
| import com.jd.blockchain.crypto.PrivKey; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.crypto.asymmetric.*; | |||
| import com.jd.blockchain.utils.http.agent.HttpServiceAgent; | |||
| import com.jd.blockchain.utils.http.agent.ServiceEndpoint; | |||
| import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; | |||
| import com.jd.blockchain.crypto.asymmetric.SignatureDigest; | |||
| import com.jd.blockchain.crypto.asymmetric.SignatureFunction; | |||
| import com.jd.blockchain.utils.net.NetworkAddress; | |||
| import com.jd.blockchain.utils.serialize.binary.BinarySerializeUtils; | |||
| import java.util.concurrent.atomic.AtomicInteger; | |||
| import java.util.regex.Matcher; | |||
| import java.util.regex.Pattern; | |||
| public class BftsmartConsensusClientFactory implements ClientFactory { | |||
| @@ -43,7 +37,7 @@ public class BftsmartConsensusClientFactory implements ClientFactory { | |||
| PubKey pubKey = clientKeyPair.getPubKey(); | |||
| PrivKey privKey = clientKeyPair.getPrivKey(); | |||
| SignatureFunction signatureFunction = CryptoUtils.sign(pubKey.getAlgorithm()); | |||
| SignatureFunction signatureFunction =CryptoServiceProviders.getSignatureFunction(pubKey.getAlgorithm()); | |||
| SignatureDigest signatureDigest = signatureFunction.sign(privKey, pubKey.toBytes()); | |||
| BftsmartClientIdentification bftsmartClientIdentification = new BftsmartClientIdentification(); | |||
| @@ -1,59 +1,62 @@ | |||
| package com.jd.blockchain.consensus.bftsmart.service; | |||
| import java.util.concurrent.locks.Lock; | |||
| import java.util.concurrent.locks.ReentrantLock; | |||
| import com.jd.blockchain.consensus.ClientIdentification; | |||
| import com.jd.blockchain.consensus.ConsensusManageService; | |||
| import com.jd.blockchain.consensus.ConsensusSecurityException; | |||
| import com.jd.blockchain.consensus.bftsmart.BftsmartClientIncomingConfig; | |||
| import com.jd.blockchain.consensus.bftsmart.BftsmartClientIncomingSettings; | |||
| import com.jd.blockchain.crypto.CryptoUtils; | |||
| import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
| import com.jd.blockchain.crypto.asymmetric.SignatureFunction; | |||
| import com.jd.blockchain.utils.serialize.binary.BinarySerializeUtils; | |||
| import java.util.concurrent.locks.Lock; | |||
| import java.util.concurrent.locks.ReentrantLock; | |||
| public class BftsmartConsensusManageService implements ConsensusManageService { | |||
| public static final int CLIENT_RANGE = 100 * 1000; | |||
| public static final int CLIENT_RANGE = 100 * 1000; | |||
| private BftsmartNodeServer nodeServer; | |||
| private int clientId; | |||
| private static final Lock authLock = new ReentrantLock(); | |||
| private BftsmartNodeServer nodeServer; | |||
| private int clientId; | |||
| private static final Lock authLock = new ReentrantLock(); | |||
| public BftsmartConsensusManageService(BftsmartNodeServer nodeServer) { | |||
| this.nodeServer = nodeServer; | |||
| //Assume that each peer node corresponds to up to 100 gateways | |||
| clientId = nodeServer.getServerId() * CLIENT_RANGE; | |||
| } | |||
| public BftsmartConsensusManageService(BftsmartNodeServer nodeServer) { | |||
| this.nodeServer = nodeServer; | |||
| // Assume that each peer node corresponds to up to 100 gateways | |||
| clientId = nodeServer.getServerId() * CLIENT_RANGE; | |||
| } | |||
| @Override | |||
| public BftsmartClientIncomingSettings authClientIncoming(ClientIdentification authId) { | |||
| if (verify(authId)) { | |||
| BftsmartClientIncomingSettings clientIncomingSettings = new BftsmartClientIncomingConfig(); | |||
| @Override | |||
| public BftsmartClientIncomingSettings authClientIncoming(ClientIdentification authId) { | |||
| if (verify(authId)) { | |||
| BftsmartClientIncomingSettings clientIncomingSettings = new BftsmartClientIncomingConfig(); | |||
| ((BftsmartClientIncomingConfig) clientIncomingSettings).setTopology(BinarySerializeUtils.serialize(nodeServer.getTopology())); | |||
| ((BftsmartClientIncomingConfig) clientIncomingSettings).setTomConfig(BinarySerializeUtils.serialize(nodeServer.getTomConfig())); | |||
| ((BftsmartClientIncomingConfig) clientIncomingSettings).setConsensusSettings(nodeServer.getConsensusSetting()); | |||
| ((BftsmartClientIncomingConfig) clientIncomingSettings).setPubKey(authId.getPubKey()); | |||
| //compute gateway id | |||
| try { | |||
| authLock.lock(); | |||
| ((BftsmartClientIncomingConfig) clientIncomingSettings).setClientId(clientId++); | |||
| } finally { | |||
| authLock.unlock(); | |||
| } | |||
| ((BftsmartClientIncomingConfig) clientIncomingSettings) | |||
| .setTopology(BinarySerializeUtils.serialize(nodeServer.getTopology())); | |||
| ((BftsmartClientIncomingConfig) clientIncomingSettings) | |||
| .setTomConfig(BinarySerializeUtils.serialize(nodeServer.getTomConfig())); | |||
| ((BftsmartClientIncomingConfig) clientIncomingSettings) | |||
| .setConsensusSettings(nodeServer.getConsensusSetting()); | |||
| ((BftsmartClientIncomingConfig) clientIncomingSettings).setPubKey(authId.getPubKey()); | |||
| // compute gateway id | |||
| try { | |||
| authLock.lock(); | |||
| ((BftsmartClientIncomingConfig) clientIncomingSettings).setClientId(clientId++); | |||
| } finally { | |||
| authLock.unlock(); | |||
| } | |||
| return clientIncomingSettings; | |||
| return clientIncomingSettings; | |||
| } | |||
| } | |||
| return null; | |||
| } | |||
| return null; | |||
| } | |||
| public boolean verify(ClientIdentification authId) { | |||
| public boolean verify(ClientIdentification authId) { | |||
| SignatureFunction signatureFunction = CryptoUtils.sign(authId.getPubKey().getAlgorithm()); | |||
| SignatureFunction signatureFunction = CryptoServiceProviders | |||
| .getSignatureFunction(authId.getPubKey().getAlgorithm()); | |||
| return signatureFunction.verify(authId.getSignature(), authId.getPubKey(), authId.getIdentityInfo()); | |||
| } | |||
| return signatureFunction.verify(authId.getSignature(), authId.getPubKey(), authId.getIdentityInfo()); | |||
| } | |||
| } | |||
| @@ -1,27 +1,18 @@ | |||
| package com.jd.blockchain.consensus.bftsmart.service; | |||
| import java.io.ByteArrayInputStream; | |||
| import java.io.ByteArrayOutputStream; | |||
| import java.util.*; | |||
| import java.util.concurrent.ConcurrentHashMap; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| import java.util.Properties; | |||
| import java.util.concurrent.CopyOnWriteArrayList; | |||
| import java.util.concurrent.ExecutorService; | |||
| import java.util.concurrent.Executors; | |||
| import java.util.concurrent.ScheduledExecutorService; | |||
| import java.util.concurrent.ScheduledThreadPoolExecutor; | |||
| import java.util.concurrent.TimeUnit; | |||
| import java.util.concurrent.atomic.AtomicInteger; | |||
| import java.util.concurrent.atomic.AtomicLong; | |||
| import bftsmart.tom.*; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import com.jd.blockchain.binaryproto.BinaryEncodingUtils; | |||
| import com.jd.blockchain.consensus.ConsensusManageService; | |||
| import com.jd.blockchain.consensus.NodeSettings; | |||
| import com.jd.blockchain.consensus.bftsmart.BftsmartCommitBlockSettings; | |||
| import com.jd.blockchain.consensus.bftsmart.BftsmartConsensusProvider; | |||
| import com.jd.blockchain.consensus.bftsmart.BftsmartConsensusSettings; | |||
| import com.jd.blockchain.consensus.bftsmart.BftsmartNodeSettings; | |||
| @@ -31,28 +22,17 @@ import com.jd.blockchain.consensus.service.NodeServer; | |||
| import com.jd.blockchain.consensus.service.ServerSettings; | |||
| import com.jd.blockchain.consensus.service.StateHandle; | |||
| import com.jd.blockchain.consensus.service.StateMachineReplicate; | |||
| import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
| import com.jd.blockchain.crypto.CryptoUtils; | |||
| import com.jd.blockchain.crypto.hash.HashDigest; | |||
| import com.jd.blockchain.ledger.BlockchainKeyGenerator; | |||
| import com.jd.blockchain.ledger.BlockchainKeyPair; | |||
| import com.jd.blockchain.ledger.TransactionContent; | |||
| import com.jd.blockchain.ledger.TransactionRequest; | |||
| import com.jd.blockchain.ledger.TransactionRespHandle; | |||
| import com.jd.blockchain.ledger.TransactionResponse; | |||
| import com.jd.blockchain.ledger.TransactionState; | |||
| import com.jd.blockchain.ledger.data.TxContentBlob; | |||
| import com.jd.blockchain.ledger.data.TxRequestMessage; | |||
| import com.jd.blockchain.utils.PropertiesUtils; | |||
| import com.jd.blockchain.utils.codec.Base58Utils; | |||
| import com.jd.blockchain.utils.concurrent.AsyncFuture; | |||
| import com.jd.blockchain.utils.concurrent.CompletableAsyncFuture; | |||
| import com.jd.blockchain.utils.io.BytesUtils; | |||
| import com.jd.blockchain.utils.serialize.binary.BinarySerializeUtils; | |||
| import bftsmart.reconfiguration.util.HostsConfig; | |||
| import bftsmart.reconfiguration.util.TOMConfiguration; | |||
| import bftsmart.reconfiguration.views.MemoryBasedViewStorage; | |||
| import bftsmart.tom.MessageContext; | |||
| import bftsmart.tom.ReplyContext; | |||
| import bftsmart.tom.ReplyContextMessage; | |||
| import bftsmart.tom.ServiceReplica; | |||
| import bftsmart.tom.core.messages.TOMMessage; | |||
| import bftsmart.tom.server.defaultservices.DefaultRecoverable; | |||
| @@ -10,23 +10,18 @@ package com.jd.blockchain.consensus.mq.client; | |||
| import com.jd.blockchain.consensus.ClientIncomingSettings; | |||
| import com.jd.blockchain.consensus.ConsensusManageService; | |||
| import com.jd.blockchain.consensus.ConsensusSettings; | |||
| import com.jd.blockchain.consensus.client.ClientFactory; | |||
| import com.jd.blockchain.consensus.client.ClientSettings; | |||
| import com.jd.blockchain.consensus.mq.config.MsgQueueClientConfig; | |||
| import com.jd.blockchain.consensus.mq.config.MsgQueueClientIncomingConfig; | |||
| import com.jd.blockchain.consensus.mq.config.MsgQueueConsensusConfig; | |||
| import com.jd.blockchain.consensus.mq.settings.MsgQueueClientIncomingSettings; | |||
| import com.jd.blockchain.consensus.mq.settings.MsgQueueClientSettings; | |||
| import com.jd.blockchain.consensus.mq.settings.MsgQueueConsensusSettings; | |||
| import com.jd.blockchain.crypto.CryptoUtils; | |||
| import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; | |||
| import com.jd.blockchain.crypto.asymmetric.SignatureDigest; | |||
| import com.jd.blockchain.crypto.asymmetric.SignatureFunction; | |||
| import java.lang.reflect.Proxy; | |||
| /** | |||
| * | |||
| * @author shaozhuguang | |||
| @@ -41,7 +36,7 @@ public class MsgQueueClientFactory implements ClientFactory { | |||
| PubKey pubKey = clientKeyPair.getPubKey(); | |||
| byte[] address = pubKey.toBytes(); // 使用公钥地址作为认证信息 | |||
| SignatureFunction signatureFunction = CryptoUtils.sign(pubKey.getAlgorithm()); | |||
| SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(pubKey.getAlgorithm()); | |||
| SignatureDigest signatureDigest = signatureFunction.sign(clientKeyPair.getPrivKey(), address); | |||
| MsgQueueClientIdentification mqci = new MsgQueueClientIdentification() | |||
| @@ -10,12 +10,8 @@ package com.jd.blockchain.consensus.mq.client; | |||
| import com.jd.blockchain.consensus.ClientIdentification; | |||
| import com.jd.blockchain.consensus.mq.MsgQueueConsensusProvider; | |||
| import com.jd.blockchain.crypto.CryptoUtils; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.crypto.asymmetric.SignatureDigest; | |||
| import com.jd.blockchain.crypto.asymmetric.SignatureFunction; | |||
| import java.util.Arrays; | |||
| /** | |||
| * | |||
| @@ -8,21 +8,18 @@ | |||
| */ | |||
| package com.jd.blockchain.consensus.mq.server; | |||
| import java.util.Arrays; | |||
| import com.jd.blockchain.consensus.ClientIdentification; | |||
| import com.jd.blockchain.consensus.ConsensusManageService; | |||
| import com.jd.blockchain.consensus.ConsensusSecurityException; | |||
| import com.jd.blockchain.consensus.mq.client.MsgQueueClientIdentification; | |||
| import com.jd.blockchain.consensus.mq.config.MsgQueueClientIncomingConfig; | |||
| import com.jd.blockchain.consensus.mq.config.MsgQueueConsensusConfig; | |||
| import com.jd.blockchain.consensus.mq.settings.MsgQueueClientIncomingSettings; | |||
| import com.jd.blockchain.consensus.mq.settings.MsgQueueConsensusSettings; | |||
| import com.jd.blockchain.crypto.CryptoUtils; | |||
| import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.crypto.asymmetric.SignatureFunction; | |||
| import java.lang.reflect.Proxy; | |||
| import java.util.Arrays; | |||
| /** | |||
| * | |||
| * @author shaozhuguang | |||
| @@ -65,7 +62,7 @@ public class MsgQueueConsensusManageService implements ConsensusManageService { | |||
| byte[] identityInfo = authId.getIdentityInfo(); | |||
| byte[] address = pubKey.toBytes(); // 使用公钥地址作为认证信息 | |||
| if (Arrays.equals(address, identityInfo)) { | |||
| SignatureFunction signatureFunction = CryptoUtils.sign(pubKey.getAlgorithm()); | |||
| SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(pubKey.getAlgorithm()); | |||
| isLegal = signatureFunction.verify(authId.getSignature(), pubKey, identityInfo); | |||
| } | |||
| return isLegal; | |||
| @@ -125,16 +125,21 @@ public final class CryptoServiceProviders { | |||
| return algorithms.get(code); | |||
| } | |||
| /** | |||
| * Return the CryptoAlogrithm object of the specified name ,or null if none; | |||
| * | |||
| * @param name | |||
| * @return | |||
| */ | |||
| public static CryptoAlgorithm getAlgorithm(String name) { | |||
| Short code = names.get(name.toUpperCase()); | |||
| return code == null ? null : algorithms.get(code); | |||
| } | |||
| public static RandomFunction getRandomFunction(String algorithmName) { | |||
| CryptoAlgorithm algorithm = getAlgorithm(algorithmName); | |||
| if (algorithm == null) { | |||
| throw new CryptoException( | |||
| "Algorithm " + algorithmName + " has no service provider!"); | |||
| throw new CryptoException("Algorithm " + algorithmName + " has no service provider!"); | |||
| } | |||
| return getRandomFunction(algorithm); | |||
| } | |||
| @@ -152,12 +157,11 @@ public final class CryptoServiceProviders { | |||
| return (RandomFunction) func; | |||
| } | |||
| public static HashFunction getHashFunction(String algorithmName) { | |||
| CryptoAlgorithm algorithm = getAlgorithm(algorithmName); | |||
| if (algorithm == null) { | |||
| throw new CryptoException( | |||
| "Algorithm " + algorithmName + " has no service provider!"); | |||
| throw new CryptoException("Algorithm " + algorithmName + " has no service provider!"); | |||
| } | |||
| return getHashFunction(algorithm); | |||
| } | |||
| @@ -175,12 +179,11 @@ public final class CryptoServiceProviders { | |||
| return (HashFunction) func; | |||
| } | |||
| public static AsymmetricEncryptionFunction getAsymmetricEncryptionFunction(String algorithmName) { | |||
| CryptoAlgorithm algorithm = getAlgorithm(algorithmName); | |||
| if (algorithm == null) { | |||
| throw new CryptoException( | |||
| "Algorithm " + algorithmName + " has no service provider!"); | |||
| throw new CryptoException("Algorithm " + algorithmName + " has no service provider!"); | |||
| } | |||
| return getAsymmetricEncryptionFunction(algorithm); | |||
| } | |||
| @@ -198,13 +201,11 @@ public final class CryptoServiceProviders { | |||
| return (AsymmetricEncryptionFunction) func; | |||
| } | |||
| public static SignatureFunction getSignatureFunction(String algorithmName) { | |||
| CryptoAlgorithm algorithm = getAlgorithm(algorithmName); | |||
| if (algorithm == null) { | |||
| throw new CryptoException( | |||
| "Algorithm " + algorithmName + " has no service provider!"); | |||
| throw new CryptoException("Algorithm " + algorithmName + " has no service provider!"); | |||
| } | |||
| return getSignatureFunction(algorithm); | |||
| } | |||
| @@ -222,13 +223,11 @@ public final class CryptoServiceProviders { | |||
| return (SignatureFunction) func; | |||
| } | |||
| public static SymmetricEncryptionFunction getSymmetricEncryptionFunction(String algorithmName) { | |||
| CryptoAlgorithm algorithm = getAlgorithm(algorithmName); | |||
| if (algorithm == null) { | |||
| throw new CryptoException( | |||
| "Algorithm " + algorithmName + " has no service provider!"); | |||
| throw new CryptoException("Algorithm " + algorithmName + " has no service provider!"); | |||
| } | |||
| return getSymmetricEncryptionFunction(algorithm); | |||
| } | |||
| @@ -246,13 +245,11 @@ public final class CryptoServiceProviders { | |||
| return (SymmetricEncryptionFunction) func; | |||
| } | |||
| public static CryptoFunction getCryptoFunction(String algorithmName) { | |||
| CryptoAlgorithm algorithm = getAlgorithm(algorithmName); | |||
| if (algorithm == null) { | |||
| throw new CryptoException( | |||
| "Algorithm " + algorithmName + " has no service provider!"); | |||
| throw new CryptoException("Algorithm " + algorithmName + " has no service provider!"); | |||
| } | |||
| return getCryptoFunction(algorithm); | |||
| } | |||
| @@ -1,6 +1,5 @@ | |||
| package com.jd.blockchain.gateway.web; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.web.bind.annotation.RequestBody; | |||
| import org.springframework.web.bind.annotation.RequestMapping; | |||
| @@ -9,7 +8,7 @@ import org.springframework.web.bind.annotation.ResponseBody; | |||
| import org.springframework.web.bind.annotation.RestController; | |||
| import com.jd.blockchain.binaryproto.BinaryEncodingUtils; | |||
| import com.jd.blockchain.crypto.CryptoUtils; | |||
| import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
| import com.jd.blockchain.crypto.asymmetric.SignatureFunction; | |||
| import com.jd.blockchain.crypto.hash.HashDigest; | |||
| import com.jd.blockchain.gateway.PeerService; | |||
| @@ -56,7 +55,8 @@ public class TxProcessingController implements TransactionService { | |||
| // 验证签名; | |||
| byte[] content = BinaryEncodingUtils.encode(txRequest.getTransactionContent(), TransactionContent.class); | |||
| for (DigitalSignature sign : partiSigns) { | |||
| SignatureFunction signFunc = CryptoUtils.sign(sign.getPubKey().getAlgorithm()); | |||
| SignatureFunction signFunc = CryptoServiceProviders | |||
| .getSignatureFunction(sign.getPubKey().getAlgorithm()); | |||
| if (!signFunc.verify(sign.getDigest(), sign.getPubKey(), content)) { | |||
| throw new BusinessException("The validation of participant signatures fail!"); | |||
| } | |||
| @@ -67,4 +67,3 @@ public class TxProcessingController implements TransactionService { | |||
| return peerService.getTransactionService().process(txRequest); | |||
| } | |||
| } | |||
| @@ -3,13 +3,11 @@ package test.com.jd.blockchain.gateway.data; | |||
| import static org.junit.Assert.assertArrayEquals; | |||
| import static org.junit.Assert.assertEquals; | |||
| import java.util.Random; | |||
| import org.junit.Test; | |||
| import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
| import com.jd.blockchain.crypto.CryptoUtils; | |||
| import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
| import com.jd.blockchain.crypto.hash.HashDigest; | |||
| import com.jd.blockchain.crypto.hash.HashFunction; | |||
| import com.jd.blockchain.utils.serialize.json.JSONSerializeUtils; | |||
| public class HashDigestJSONSerializeTest { | |||
| @@ -43,11 +41,11 @@ public class HashDigestJSONSerializeTest { | |||
| JSONSerializeUtils.configSerialization(HashDigest.class, HashDigestSerializer.INSTANCE, | |||
| HashDigestDeserializer.INSTANCE); | |||
| HashDigest hash = new HashDigest(CryptoAlgorithm.SHA256, "jd-test".getBytes()); | |||
| HashFunction hashFunc = CryptoServiceProviders.getHashFunction("SHA256"); | |||
| HashDigest hash = hashFunc.hash("jd-test".getBytes()); | |||
| String hashJson = JSONSerializeUtils.serializeToJSON(hash, true); | |||
| HashDigest hashDigest = JSONSerializeUtils.deserializeFromJSON(hashJson, HashDigest.class); | |||
| HashDigest hashDigest = JSONSerializeUtils.deserializeFromJSON(hashJson, HashDigest.class); | |||
| assertArrayEquals(hash.getRawDigest(), hashDigest.getRawDigest()); | |||
| assertEquals(hash.getAlgorithm(), hashDigest.getAlgorithm()); | |||
| @@ -58,7 +56,7 @@ public class HashDigestJSONSerializeTest { | |||
| String json = JSONSerializeUtils.serializeToJSON(data, true); | |||
| TestData desData = JSONSerializeUtils.deserializeFromJSON(json, TestData.class); | |||
| TestData desData = JSONSerializeUtils.deserializeFromJSON(json, TestData.class); | |||
| assertEquals(data.getHash(), desData.getHash()); | |||
| assertEquals(data.getId(), desData.getId()); | |||
| } | |||
| @@ -5,15 +5,16 @@ import org.slf4j.LoggerFactory; | |||
| import com.jd.blockchain.binaryproto.BinaryEncodingUtils; | |||
| import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
| import com.jd.blockchain.crypto.CryptoUtils; | |||
| import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
| import com.jd.blockchain.crypto.hash.HashDigest; | |||
| import com.jd.blockchain.crypto.hash.HashFunction; | |||
| import com.jd.blockchain.ledger.LedgerInitSetting; | |||
| import com.jd.blockchain.ledger.ParticipantNode; | |||
| import com.jd.blockchain.storage.service.ExPolicyKVStorage; | |||
| import com.jd.blockchain.storage.service.ExPolicyKVStorage.ExPolicy; | |||
| import com.jd.blockchain.storage.service.VersioningKVStorage; | |||
| import com.jd.blockchain.utils.Bytes; | |||
| import com.jd.blockchain.utils.Transactional; | |||
| import com.jd.blockchain.storage.service.VersioningKVStorage; | |||
| public class LedgerAdminAccount implements Transactional, LedgerAdministration { | |||
| @@ -94,9 +95,10 @@ public class LedgerAdminAccount implements Transactional, LedgerAdministration { | |||
| // 检查参与者列表是否已经按照 id 升序排列,并且 id 不冲突; | |||
| // 注:参与者的 id 要求从 0 开始编号,顺序依次递增,不允许跳空; | |||
| for (int i = 0; i < parties.length; i++) { | |||
| // if (parties[i].getAddress() != i) { | |||
| // throw new LedgerException("The id of participant isn't match the order of the participant list!"); | |||
| // } | |||
| // if (parties[i].getAddress() != i) { | |||
| // throw new LedgerException("The id of participant isn't match the order of the | |||
| // participant list!"); | |||
| // } | |||
| } | |||
| // 初始化元数据; | |||
| @@ -155,7 +157,8 @@ public class LedgerAdminAccount implements Transactional, LedgerAdministration { | |||
| // String key = encodeMetadataKey(base58Hash); | |||
| Bytes key = encodeMetadataKey(adminAccountHash); | |||
| byte[] bytes = settingsStorage.get(key); | |||
| if (!CryptoUtils.hashCrypto().verify(adminAccountHash, bytes)) { | |||
| HashFunction hashFunc = CryptoServiceProviders.getHashFunction(adminAccountHash.getAlgorithm()); | |||
| if (!hashFunc.verify(adminAccountHash, bytes)) { | |||
| LOGGER.error("The hash verification of ledger settings fail! --[HASH=" + key + "]"); | |||
| throw new LedgerException("The hash verification of ledger settings fail!"); | |||
| } | |||
| @@ -216,18 +219,19 @@ public class LedgerAdminAccount implements Transactional, LedgerAdministration { | |||
| return participants.getParticipantCount(); | |||
| } | |||
| // /* | |||
| // * (non-Javadoc) | |||
| // * | |||
| // * @see | |||
| // * com.jd.blockchain.ledger.core.LedgerAdministration#getParticipant(java.lang. | |||
| // * String) | |||
| // */ | |||
| // @Override | |||
| // public ParticipantNode getParticipant(int id) { | |||
| // return participants.getParticipant(id); | |||
| // } | |||
| // /* | |||
| // * (non-Javadoc) | |||
| // * | |||
| // * @see | |||
| // * | |||
| // com.jd.blockchain.ledger.core.LedgerAdministration#getParticipant(java.lang. | |||
| // * String) | |||
| // */ | |||
| // @Override | |||
| // public ParticipantNode getParticipant(int id) { | |||
| // return participants.getParticipant(id); | |||
| // } | |||
| @Override | |||
| public ParticipantNode[] getParticipants() { | |||
| return participants.getParticipants(); | |||
| @@ -258,8 +262,9 @@ public class LedgerAdminAccount implements Transactional, LedgerAdministration { | |||
| // 基于之前的密码配置来计算元数据的哈希; | |||
| byte[] metadataBytes = serializeMetadata(metadata); | |||
| HashDigest metadataHash = CryptoUtils.hashCrypto() | |||
| .getFunction(previousSetting.getCryptoSetting().getHashAlgorithm()).hash(metadataBytes); | |||
| HashFunction hashFunc = CryptoServiceProviders | |||
| .getHashFunction(previousSetting.getCryptoSetting().getHashAlgorithm()); | |||
| HashDigest metadataHash = hashFunc.hash(metadataBytes); | |||
| if (adminAccountHash == null || !adminAccountHash.equals(metadataHash)) { | |||
| // update modify; | |||
| // String base58MetadataHash = metadataHash.toBase58(); | |||
| @@ -1,8 +1,10 @@ | |||
| package com.jd.blockchain.ledger.core.impl; | |||
| import com.jd.blockchain.binaryproto.BinaryEncodingUtils; | |||
| import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
| import com.jd.blockchain.crypto.CryptoUtils; | |||
| import com.jd.blockchain.crypto.hash.HashDigest; | |||
| import com.jd.blockchain.crypto.hash.HashFunction; | |||
| import com.jd.blockchain.ledger.BlockBody; | |||
| import com.jd.blockchain.ledger.CryptoSetting; | |||
| import com.jd.blockchain.ledger.LedgerBlock; | |||
| @@ -230,7 +232,8 @@ public class LedgerRepositoryImpl implements LedgerRepository { | |||
| } else { | |||
| blockBodyBytes = BinaryEncodingUtils.encode(block, BlockBody.class); | |||
| } | |||
| boolean pass = CryptoUtils.hashCrypto().verify(blockHash, blockBodyBytes); | |||
| HashFunction hashFunc = CryptoServiceProviders.getHashFunction(blockHash.getAlgorithm()); | |||
| boolean pass = hashFunc.verify(blockHash, blockBodyBytes); | |||
| if (!pass) { | |||
| throw new LedgerException("Block hash verification fail!"); | |||
| } | |||
| @@ -3,7 +3,7 @@ package com.jd.blockchain.ledger.core.impl; | |||
| import java.util.Stack; | |||
| import com.jd.blockchain.binaryproto.BinaryEncodingUtils; | |||
| import com.jd.blockchain.crypto.CryptoUtils; | |||
| import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
| import com.jd.blockchain.crypto.hash.HashDigest; | |||
| import com.jd.blockchain.ledger.BlockBody; | |||
| import com.jd.blockchain.ledger.CryptoSetting; | |||
| @@ -189,7 +189,7 @@ public class LedgerTransactionalEditor implements LedgerEditor { | |||
| // compute block hash; | |||
| byte[] blockBodyBytes = BinaryEncodingUtils.encode(newlyBlock, BlockBody.class); | |||
| HashDigest blockHash = CryptoUtils.hashCrypto().getFunction(cryptoSetting.getHashAlgorithm()) | |||
| HashDigest blockHash = CryptoServiceProviders.getHashFunction(cryptoSetting.getHashAlgorithm()) | |||
| .hash(blockBodyBytes); | |||
| newlyBlock.setHash(blockHash); | |||
| if (newlyBlock.getLedgerHash() == null) { | |||
| @@ -9,9 +9,7 @@ import org.junit.Test; | |||
| import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
| import com.jd.blockchain.crypto.AddressEncoding; | |||
| import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
| import com.jd.blockchain.crypto.CryptoUtils; | |||
| import com.jd.blockchain.crypto.asymmetric.AsymmetricCryptography; | |||
| import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
| import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; | |||
| import com.jd.blockchain.crypto.asymmetric.SignatureFunction; | |||
| import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
| @@ -44,9 +42,7 @@ public class LedgerEditerTest { | |||
| } | |||
| String ledgerKeyPrefix = "LDG://"; | |||
| AsymmetricCryptography asymmetricCryptography = CryptoUtils.asymmCrypto(); | |||
| SignatureFunction signatureFunction = asymmetricCryptography | |||
| .getSignatureFunction(ClassicCryptoService.ED25519_ALGORITHM); | |||
| SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction("ED25519"); | |||
| // 存储; | |||
| MemoryKVStorage storage = new MemoryKVStorage(); | |||
| @@ -111,6 +107,8 @@ public class LedgerEditerTest { | |||
| } | |||
| private LedgerInitSetting createLedgerInitSetting() { | |||
| SignatureFunction signFunc = CryptoServiceProviders.getSignatureFunction("ED25519"); | |||
| CryptoConfig defCryptoSetting = new CryptoConfig(); | |||
| defCryptoSetting.setAutoVerifyHash(true); | |||
| defCryptoSetting.setHashAlgorithm(ClassicCryptoService.SHA256_ALGORITHM); | |||
| @@ -123,7 +121,7 @@ public class LedgerEditerTest { | |||
| parties[0] = new ConsensusParticipantData(); | |||
| parties[0].setId(0); | |||
| parties[0].setName("John"); | |||
| CryptoKeyPair kp0 = CryptoUtils.sign(ClassicCryptoService.ED25519_ALGORITHM).generateKeyPair(); | |||
| CryptoKeyPair kp0 = signFunc.generateKeyPair(); | |||
| parties[0].setPubKey(kp0.getPubKey()); | |||
| parties[0].setAddress(AddressEncoding.generateAddress(kp0.getPubKey()).toBase58()); | |||
| parties[0].setHostAddress(new NetworkAddress("192.168.1.6", 9000)); | |||
| @@ -131,7 +129,7 @@ public class LedgerEditerTest { | |||
| parties[1] = new ConsensusParticipantData(); | |||
| parties[1].setId(1); | |||
| parties[1].setName("John"); | |||
| CryptoKeyPair kp1 = CryptoUtils.sign(ClassicCryptoService.ED25519_ALGORITHM).generateKeyPair(); | |||
| CryptoKeyPair kp1 = signFunc.generateKeyPair(); | |||
| parties[1].setPubKey(kp1.getPubKey()); | |||
| parties[1].setAddress(AddressEncoding.generateAddress(kp1.getPubKey()).toBase58()); | |||
| parties[1].setHostAddress(new NetworkAddress("192.168.1.7", 9000)); | |||
| @@ -4,19 +4,14 @@ import static org.junit.Assert.assertEquals; | |||
| import static org.junit.Assert.assertNotNull; | |||
| import static org.junit.Assert.assertNull; | |||
| import java.util.Arrays; | |||
| import java.util.Collections; | |||
| import java.util.Comparator; | |||
| import java.util.stream.Collectors; | |||
| import java.util.stream.Stream; | |||
| import org.bouncycastle.util.io.Streams; | |||
| import org.junit.Before; | |||
| import org.junit.Test; | |||
| import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
| import com.jd.blockchain.crypto.AddressEncoding; | |||
| import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
| import com.jd.blockchain.crypto.CryptoUtils; | |||
| import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
| import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; | |||
| import com.jd.blockchain.crypto.asymmetric.SignatureFunction; | |||
| import com.jd.blockchain.crypto.hash.HashDigest; | |||
| @@ -53,7 +48,7 @@ import com.jd.blockchain.utils.io.BytesUtils; | |||
| import com.jd.blockchain.utils.net.NetworkAddress; | |||
| public class LedgerManagerTest { | |||
| static { | |||
| DataContractRegistry.register(TransactionContent.class); | |||
| DataContractRegistry.register(UserRegisterOperation.class); | |||
| @@ -61,7 +56,12 @@ public class LedgerManagerTest { | |||
| DataContractRegistry.register(BlockBody.class); | |||
| } | |||
| private static SignatureFunction signatureFunction = CryptoUtils.sign(ClassicCryptoService.ED25519_ALGORITHM); | |||
| private SignatureFunction signatureFunction; | |||
| @Before | |||
| public void intialize() { | |||
| signatureFunction = CryptoServiceProviders.getSignatureFunction("ED25519"); | |||
| } | |||
| @Test | |||
| public void testLedgerInit() { | |||
| @@ -81,11 +81,12 @@ public class LedgerManagerTest { | |||
| // 记录交易,注册用户; | |||
| LedgerTransactionContext txCtx = ldgEdt.newTransaction(genesisTxReq); | |||
| LedgerDataSet ldgDS = txCtx.getDataSet(); | |||
| BlockchainKeyPair userKP = BlockchainKeyGenerator.getInstance().generate();; | |||
| BlockchainKeyPair userKP = BlockchainKeyGenerator.getInstance().generate(); | |||
| ; | |||
| UserAccount userAccount = ldgDS.getUserAccountSet().register(userKP.getAddress(), userKP.getPubKey()); | |||
| userAccount.setProperty("Name", "孙悟空", -1); | |||
| userAccount.setProperty("Age", "10000", -1); | |||
| System.out.println("UserAddress=" + userAccount.getAddress()); | |||
| // 提交交易结果; | |||
| @@ -105,63 +106,62 @@ public class LedgerManagerTest { | |||
| // 提交数据,写入存储; | |||
| ldgEdt.commit(); | |||
| //重新加载并校验结果; | |||
| // 重新加载并校验结果; | |||
| LedgerManager reloadLedgerManager = new LedgerManager(); | |||
| LedgerRepository reloadLedgerRepo = reloadLedgerManager.register(ledgerHash, storage); | |||
| HashDigest genesisHash= reloadLedgerRepo.getBlockHash(0); | |||
| HashDigest genesisHash = reloadLedgerRepo.getBlockHash(0); | |||
| assertEquals(ledgerHash, genesisHash); | |||
| LedgerBlock latestBlock = reloadLedgerRepo.getLatestBlock(); | |||
| assertEquals(0, latestBlock.getHeight()); | |||
| assertEquals(ledgerHash, latestBlock.getHash()); | |||
| assertEquals(ledgerHash, latestBlock.getLedgerHash()); | |||
| LedgerEditor editor1 = reloadLedgerRepo.createNextBlock(); | |||
| TxBuilder txBuilder = new TxBuilder(ledgerHash); | |||
| BlockchainKeyPair dataKey = BlockchainKeyGenerator.getInstance().generate(); | |||
| txBuilder.dataAccounts().register(dataKey.getIdentity()); | |||
| TransactionRequestBuilder txReqBuilder = txBuilder.prepareRequest(); | |||
| DigitalSignature dgtsign = txReqBuilder.signAsEndpoint(userKP); | |||
| TransactionRequest txRequest = txReqBuilder.buildRequest(); | |||
| LedgerTransactionContext txCtx1 = editor1.newTransaction(txRequest); | |||
| txCtx1.getDataSet().getDataAccountSet().register(dataKey.getAddress(), dataKey.getPubKey(), null); | |||
| txCtx1.commit(TransactionState.SUCCESS); | |||
| LedgerBlock block1 = editor1.prepare(); | |||
| editor1.commit(); | |||
| assertEquals(1, block1.getHeight()); | |||
| assertNotNull(block1.getHash()); | |||
| assertEquals(genesisHash, block1.getPreviousHash()); | |||
| assertEquals(ledgerHash, block1.getLedgerHash()); | |||
| latestBlock = reloadLedgerRepo.getLatestBlock(); | |||
| assertEquals(1, latestBlock.getHeight()); | |||
| assertEquals(block1.getHash(), latestBlock.getHash()); | |||
| showStorageKeys(storage); | |||
| reloadLedgerManager = new LedgerManager(); | |||
| reloadLedgerRepo = reloadLedgerManager.register(ledgerHash, storage); | |||
| latestBlock = reloadLedgerRepo.getLatestBlock(); | |||
| assertEquals(1, latestBlock.getHeight()); | |||
| assertEquals(block1.getHash(), latestBlock.getHash()); | |||
| DataAccountSet dataAccountSet = reloadLedgerRepo.getDataAccountSet(latestBlock); | |||
| UserAccountSet userAccountSet = reloadLedgerRepo.getUserAccountSet(latestBlock); | |||
| ContractAccountSet contractAccountSet = reloadLedgerRepo.getContractAccountSet(latestBlock); | |||
| } | |||
| private void showStorageKeys(MemoryKVStorage storage) { | |||
| // 输出写入的 kv; | |||
| System.out.println("------------------- Storage Keys -------------------"); | |||
| Object[] keys = Stream.of(storage.getStorageKeySet().toArray(new Bytes[0])).map(p -> p.toString()).sorted((o1, o2) -> o1.compareTo(o2)).toArray(); | |||
| Object[] keys = Stream.of(storage.getStorageKeySet().toArray(new Bytes[0])).map(p -> p.toString()) | |||
| .sorted((o1, o2) -> o1.compareTo(o2)).toArray(); | |||
| int i = 0; | |||
| for (Object k : keys) { | |||
| i++; | |||
| @@ -169,7 +169,6 @@ public class LedgerManagerTest { | |||
| } | |||
| } | |||
| private LedgerInitSetting createLedgerInitSetting() { | |||
| CryptoConfig defCryptoSetting = new CryptoConfig(); | |||
| defCryptoSetting.setAutoVerifyHash(true); | |||
| @@ -183,7 +182,7 @@ public class LedgerManagerTest { | |||
| parties[0] = new ConsensusParticipantData(); | |||
| parties[0].setId(0); | |||
| parties[0].setName("John"); | |||
| CryptoKeyPair kp0 = CryptoUtils.sign(ClassicCryptoService.ED25519_ALGORITHM).generateKeyPair(); | |||
| CryptoKeyPair kp0 = signatureFunction.generateKeyPair(); | |||
| parties[0].setPubKey(kp0.getPubKey()); | |||
| parties[0].setAddress(AddressEncoding.generateAddress(kp0.getPubKey()).toBase58()); | |||
| parties[0].setHostAddress(new NetworkAddress("127.0.0.1", 9000)); | |||
| @@ -191,7 +190,7 @@ public class LedgerManagerTest { | |||
| parties[1] = new ConsensusParticipantData(); | |||
| parties[1].setId(1); | |||
| parties[1].setName("Mary"); | |||
| CryptoKeyPair kp1 = CryptoUtils.sign(ClassicCryptoService.ED25519_ALGORITHM).generateKeyPair(); | |||
| CryptoKeyPair kp1 = signatureFunction.generateKeyPair(); | |||
| parties[1].setPubKey(kp1.getPubKey()); | |||
| parties[1].setAddress(AddressEncoding.generateAddress(kp1.getPubKey()).toBase58()); | |||
| parties[1].setHostAddress(new NetworkAddress("127.0.0.1", 9010)); | |||
| @@ -199,7 +198,7 @@ public class LedgerManagerTest { | |||
| parties[2] = new ConsensusParticipantData(); | |||
| parties[2].setId(2); | |||
| parties[2].setName("Jerry"); | |||
| CryptoKeyPair kp2 = CryptoUtils.sign(ClassicCryptoService.ED25519_ALGORITHM).generateKeyPair(); | |||
| CryptoKeyPair kp2 = signatureFunction.generateKeyPair(); | |||
| parties[2].setPubKey(kp2.getPubKey()); | |||
| parties[2].setAddress(AddressEncoding.generateAddress(kp2.getPubKey()).toBase58()); | |||
| parties[2].setHostAddress(new NetworkAddress("127.0.0.1", 9020)); | |||
| @@ -207,7 +206,7 @@ public class LedgerManagerTest { | |||
| parties[3] = new ConsensusParticipantData(); | |||
| parties[3].setId(3); | |||
| parties[3].setName("Tom"); | |||
| CryptoKeyPair kp3 = CryptoUtils.sign(ClassicCryptoService.ED25519_ALGORITHM).generateKeyPair(); | |||
| CryptoKeyPair kp3 = signatureFunction.generateKeyPair(); | |||
| parties[3].setPubKey(kp3.getPubKey()); | |||
| parties[3].setAddress(AddressEncoding.generateAddress(kp3.getPubKey()).toBase58()); | |||
| parties[3].setHostAddress(new NetworkAddress("127.0.0.1", 9030)); | |||
| @@ -216,6 +215,6 @@ public class LedgerManagerTest { | |||
| return initSetting; | |||
| } | |||
| // | |||
| // | |||
| } | |||
| @@ -2,14 +2,17 @@ package test.com.jd.blockchain.ledger; | |||
| import java.util.Random; | |||
| import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
| import com.jd.blockchain.crypto.CryptoUtils; | |||
| import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; | |||
| import com.jd.blockchain.crypto.asymmetric.SignatureFunction; | |||
| import com.jd.blockchain.crypto.hash.HashDigest; | |||
| import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
| import com.jd.blockchain.ledger.*; | |||
| import com.jd.blockchain.ledger.BlockchainIdentityData; | |||
| import com.jd.blockchain.ledger.CryptoSetting; | |||
| import com.jd.blockchain.ledger.PreparedTransaction; | |||
| import com.jd.blockchain.ledger.TransactionRequest; | |||
| import com.jd.blockchain.ledger.TransactionResponse; | |||
| import com.jd.blockchain.ledger.core.CryptoConfig; | |||
| import com.jd.blockchain.ledger.core.impl.TransactionStagedSnapshot; | |||
| import com.jd.blockchain.ledger.data.TransactionService; | |||
| @@ -20,12 +23,12 @@ public class LedgerTestUtils { | |||
| // private static ThreadLocalRandom rand = ThreadLocalRandom.current(); | |||
| private static Random rand = new Random(); | |||
| public static TransactionRequest createTxRequest(HashDigest ledgerHash) { | |||
| return createTxRequest(ledgerHash, CryptoUtils.sign(ClassicCryptoService.ED25519_ALGORITHM)); | |||
| SignatureFunction signFunc = CryptoServiceProviders.getSignatureFunction("ED25519"); | |||
| return createTxRequest(ledgerHash, signFunc); | |||
| } | |||
| public static TransactionRequest createTxRequest(HashDigest ledgerHash, SignatureFunction signatureFunction) { | |||
| TxHandle txHandle = new TxHandle(); | |||
| @@ -65,21 +68,19 @@ public class LedgerTestUtils { | |||
| txDataSnapshot.setUserAccountSetHash(generateRandomHash()); | |||
| return txDataSnapshot; | |||
| } | |||
| public static HashDigest generateRandomHash() { | |||
| byte[] data = new byte[64]; | |||
| rand.nextBytes(data); | |||
| return CryptoUtils.hash(ClassicCryptoService.SHA256_ALGORITHM).hash(data); | |||
| return CryptoServiceProviders.getHashFunction("SHA256").hash(data); | |||
| } | |||
| public static CryptoSetting createDefaultCryptoSetting() { | |||
| CryptoConfig cryptoSetting = new CryptoConfig(); | |||
| cryptoSetting.setAutoVerifyHash(true); | |||
| cryptoSetting.setHashAlgorithm(ClassicCryptoService.SHA256_ALGORITHM); | |||
| return cryptoSetting; | |||
| } | |||
| private static class TxHandle implements TransactionService { | |||
| @@ -17,8 +17,7 @@ import org.junit.Test; | |||
| import com.jd.blockchain.binaryproto.BinaryEncodingUtils; | |||
| import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
| import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
| import com.jd.blockchain.crypto.CryptoUtils; | |||
| import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.crypto.asymmetric.SignatureDigest; | |||
| import com.jd.blockchain.crypto.hash.HashDigest; | |||
| @@ -53,209 +52,217 @@ import com.jd.blockchain.utils.io.ByteArray; | |||
| public class LedgerTransactionDataTest { | |||
| private LedgerTransactionData data; | |||
| @Before | |||
| public void initLedgerTransactionImpl() throws Exception { | |||
| DataContractRegistry.register(LedgerTransaction.class); | |||
| DataContractRegistry.register(Transaction.class); | |||
| DataContractRegistry.register(LedgerDataSnapshot.class); | |||
| DataContractRegistry.register(NodeRequest.class); | |||
| DataContractRegistry.register(EndpointRequest.class); | |||
| DataContractRegistry.register(HashObject.class); | |||
| DataContractRegistry.register(DataAccountKVSetOperation.class); | |||
| TransactionRequest txRequestMessage = initTxRequestMessage(); | |||
| long blockHeight = 9986L; | |||
| data = new LedgerTransactionData(blockHeight, txRequestMessage, TransactionState.SUCCESS, initTransactionStagedSnapshot()); | |||
| HashDigest hash = new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "zhangsan".getBytes()); | |||
| HashDigest adminAccountHash = new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "lisi".getBytes()); | |||
| HashDigest userAccountSetHash = new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "wangwu".getBytes()); | |||
| HashDigest dataAccountSetHash = new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "zhaoliu".getBytes()); | |||
| HashDigest contractAccountSetHash = new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "sunqi".getBytes()); | |||
| data.setHash(hash); | |||
| // data.setBlockHeight(blockHeight); | |||
| data.setAdminAccountHash(adminAccountHash); | |||
| data.setUserAccountSetHash(userAccountSetHash); | |||
| data.setDataAccountSetHash(dataAccountSetHash); | |||
| data.setContractAccountSetHash(contractAccountSetHash); | |||
| } | |||
| @Test | |||
| public void testSerialize_LedgerTransaction() throws Exception { | |||
| byte[] serialBytes = BinaryEncodingUtils.encode(data, LedgerTransaction.class); | |||
| LedgerTransaction resolvedData = BinaryEncodingUtils.decode(serialBytes); | |||
| System.out.println("------Assert start ------"); | |||
| assertEquals(resolvedData.getAdminAccountHash(), data.getAdminAccountHash()); | |||
| assertEquals(resolvedData.getContractAccountSetHash(), data.getContractAccountSetHash()); | |||
| assertEquals(resolvedData.getDataAccountSetHash(), data.getDataAccountSetHash()); | |||
| assertEquals(resolvedData.getUserAccountSetHash(), data.getUserAccountSetHash()); | |||
| assertEquals(resolvedData.getExecutionState(), data.getExecutionState()); | |||
| assertEquals(resolvedData.getHash(), data.getHash()); | |||
| assertEquals(resolvedData.getBlockHeight(), data.getBlockHeight()); | |||
| // EndpointSignatures 验证 | |||
| DigitalSignature[] dataEndpointSignatures = data.getEndpointSignatures(); | |||
| DigitalSignature[] resolvedEndpointSignatures = resolvedData.getEndpointSignatures(); | |||
| for (int i = 0; i < dataEndpointSignatures.length; i++) { | |||
| assertEquals(dataEndpointSignatures[i].getPubKey(), resolvedEndpointSignatures[i].getPubKey()); | |||
| assertEquals(dataEndpointSignatures[i].getDigest(), resolvedEndpointSignatures[i].getDigest()); | |||
| } | |||
| // NodeSignatures 验证 | |||
| DigitalSignature[] dataNodeSignatures = data.getNodeSignatures(); | |||
| DigitalSignature[] resolvedNodeSignatures = resolvedData.getNodeSignatures(); | |||
| for (int i = 0; i < dataNodeSignatures.length; i++) { | |||
| assertEquals(dataNodeSignatures[i].getPubKey(), resolvedNodeSignatures[i].getPubKey()); | |||
| assertEquals(dataNodeSignatures[i].getDigest(), resolvedNodeSignatures[i].getDigest()); | |||
| } | |||
| assertEqual(data.getTransactionContent(), resolvedData.getTransactionContent()); | |||
| System.out.println("------Assert OK ------"); | |||
| } | |||
| @Test | |||
| public void testSerialize_Transaction() throws Exception { | |||
| byte[] serialBytes = BinaryEncodingUtils.encode(data, Transaction.class); | |||
| Transaction resolvedData = BinaryEncodingUtils.decode(serialBytes); | |||
| System.out.println("------Assert start ------"); | |||
| assertEquals(resolvedData.getExecutionState(), data.getExecutionState()); | |||
| assertEquals(resolvedData.getHash(), data.getHash()); | |||
| assertEquals(resolvedData.getBlockHeight(), data.getBlockHeight()); | |||
| // EndpointSignatures 验证 | |||
| DigitalSignature[] dataEndpointSignatures = data.getEndpointSignatures(); | |||
| DigitalSignature[] resolvedEndpointSignatures = resolvedData.getEndpointSignatures(); | |||
| for (int i = 0; i < dataEndpointSignatures.length; i++) { | |||
| assertEquals(dataEndpointSignatures[i].getPubKey(), resolvedEndpointSignatures[i].getPubKey()); | |||
| assertEquals(dataEndpointSignatures[i].getDigest(), resolvedEndpointSignatures[i].getDigest()); | |||
| } | |||
| // NodeSignatures 验证 | |||
| DigitalSignature[] dataNodeSignatures = data.getNodeSignatures(); | |||
| DigitalSignature[] resolvedNodeSignatures = resolvedData.getNodeSignatures(); | |||
| for (int i = 0; i < dataNodeSignatures.length; i++) { | |||
| assertEquals(dataNodeSignatures[i].getPubKey(), resolvedNodeSignatures[i].getPubKey()); | |||
| assertEquals(dataNodeSignatures[i].getDigest(), resolvedNodeSignatures[i].getDigest()); | |||
| } | |||
| assertEqual(data.getTransactionContent(), resolvedData.getTransactionContent()); | |||
| System.out.println("------Assert OK ------"); | |||
| } | |||
| @Test | |||
| public void testSerialize_LedgerDataSnapshot() throws Exception { | |||
| byte[] serialBytes = BinaryEncodingUtils.encode(data, LedgerDataSnapshot.class); | |||
| LedgerDataSnapshot resolvedData = BinaryEncodingUtils.decode(serialBytes); | |||
| System.out.println("------Assert start ------"); | |||
| assertEquals(resolvedData.getAdminAccountHash(), data.getAdminAccountHash()); | |||
| assertEquals(resolvedData.getContractAccountSetHash(), data.getContractAccountSetHash()); | |||
| assertEquals(resolvedData.getDataAccountSetHash(), data.getDataAccountSetHash()); | |||
| assertEquals(resolvedData.getUserAccountSetHash(), data.getUserAccountSetHash()); | |||
| System.out.println("------Assert OK ------"); | |||
| } | |||
| @Test | |||
| public void testSerialize_NodeRequest() throws Exception { | |||
| byte[] serialBytes = BinaryEncodingUtils.encode(data, NodeRequest.class); | |||
| NodeRequest resolvedData = BinaryEncodingUtils.decode(serialBytes); | |||
| System.out.println("------Assert start ------"); | |||
| // EndpointSignatures 验证 | |||
| DigitalSignature[] dataEndpointSignatures = data.getEndpointSignatures(); | |||
| DigitalSignature[] resolvedEndpointSignatures = resolvedData.getEndpointSignatures(); | |||
| for (int i = 0; i < dataEndpointSignatures.length; i++) { | |||
| assertEquals(dataEndpointSignatures[i].getPubKey(), resolvedEndpointSignatures[i].getPubKey()); | |||
| assertEquals(dataEndpointSignatures[i].getDigest(), resolvedEndpointSignatures[i].getDigest()); | |||
| } | |||
| // NodeSignatures 验证 | |||
| DigitalSignature[] dataNodeSignatures = data.getNodeSignatures(); | |||
| DigitalSignature[] resolvedNodeSignatures = resolvedData.getNodeSignatures(); | |||
| for (int i = 0; i < dataNodeSignatures.length; i++) { | |||
| assertEquals(dataNodeSignatures[i].getPubKey(), resolvedNodeSignatures[i].getPubKey()); | |||
| assertEquals(dataNodeSignatures[i].getDigest(), resolvedNodeSignatures[i].getDigest()); | |||
| } | |||
| assertEqual(data.getTransactionContent(), resolvedData.getTransactionContent()); | |||
| System.out.println("------Assert OK ------"); | |||
| } | |||
| @Test | |||
| public void testSerialize_EndpointRequest() throws Exception { | |||
| byte[] serialBytes = BinaryEncodingUtils.encode(data, EndpointRequest.class); | |||
| EndpointRequest resolvedData = BinaryEncodingUtils.decode(serialBytes); | |||
| System.out.println("------Assert start ------"); | |||
| // EndpointSignatures 验证 | |||
| DigitalSignature[] dataEndpointSignatures = data.getEndpointSignatures(); | |||
| DigitalSignature[] resolvedEndpointSignatures = resolvedData.getEndpointSignatures(); | |||
| for (int i = 0; i < dataEndpointSignatures.length; i++) { | |||
| assertEquals(dataEndpointSignatures[i].getPubKey(), resolvedEndpointSignatures[i].getPubKey()); | |||
| assertEquals(dataEndpointSignatures[i].getDigest(), resolvedEndpointSignatures[i].getDigest()); | |||
| } | |||
| assertEqual(data.getTransactionContent(), resolvedData.getTransactionContent()); | |||
| System.out.println("------Assert OK ------"); | |||
| } | |||
| private void assertEqual(TransactionContent dataTxContent, TransactionContent resolvedTxContent) { | |||
| assertEquals(dataTxContent.getHash(), resolvedTxContent.getHash()); | |||
| assertEquals(dataTxContent.getLedgerHash(), resolvedTxContent.getLedgerHash()); | |||
| // assertEquals(dataTxContent.getSequenceNumber(), resolvedTxContent.getSequenceNumber()); | |||
| // assertEquals(dataTxContent.getSubjectAccount(), resolvedTxContent.getSubjectAccount()); | |||
| } | |||
| private TransactionStagedSnapshot initTransactionStagedSnapshot() { | |||
| TransactionStagedSnapshot transactionStagedSnapshot = new TransactionStagedSnapshot(); | |||
| transactionStagedSnapshot.setAdminAccountHash(new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "zhangsan".getBytes())); | |||
| transactionStagedSnapshot.setContractAccountSetHash(new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "lisi".getBytes())); | |||
| transactionStagedSnapshot.setDataAccountSetHash(new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "wangwu".getBytes())); | |||
| transactionStagedSnapshot.setUserAccountSetHash(new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "zhaoliu".getBytes())); | |||
| return transactionStagedSnapshot; | |||
| } | |||
| private TxRequestMessage initTxRequestMessage() throws Exception { | |||
| TxRequestMessage txRequestMessage = new TxRequestMessage(initTransactionContent()); | |||
| SignatureDigest digest1 = new SignatureDigest(ClassicCryptoService.ED25519_ALGORITHM, "zhangsan".getBytes()); | |||
| SignatureDigest digest2 = new SignatureDigest(ClassicCryptoService.ED25519_ALGORITHM, "lisi".getBytes()); | |||
| DigitalSignatureBlob endPoint1 = new DigitalSignatureBlob(new PubKey(ClassicCryptoService.ED25519_ALGORITHM, "jd1.com".getBytes()) | |||
| , digest1); | |||
| DigitalSignatureBlob endPoint2 = new DigitalSignatureBlob(new PubKey(ClassicCryptoService.ED25519_ALGORITHM, "jd2.com".getBytes()) | |||
| , digest2); | |||
| txRequestMessage.addEndpointSignatures(endPoint1); | |||
| txRequestMessage.addEndpointSignatures(endPoint2); | |||
| SignatureDigest digest3 = new SignatureDigest(ClassicCryptoService.ED25519_ALGORITHM, "wangwu".getBytes()); | |||
| SignatureDigest digest4 = new SignatureDigest(ClassicCryptoService.ED25519_ALGORITHM, "zhaoliu".getBytes()); | |||
| DigitalSignatureBlob node1 = new DigitalSignatureBlob(new PubKey(ClassicCryptoService.ED25519_ALGORITHM, "jd3.com".getBytes()) | |||
| , digest3); | |||
| DigitalSignatureBlob node2 = new DigitalSignatureBlob(new PubKey(ClassicCryptoService.ED25519_ALGORITHM, "jd4.com".getBytes()) | |||
| , digest4); | |||
| txRequestMessage.addNodeSignatures(node1); | |||
| txRequestMessage.addNodeSignatures(node2); | |||
| return txRequestMessage; | |||
| } | |||
| private TransactionContent initTransactionContent() throws Exception{ | |||
| TxContentBlob contentBlob = null; | |||
| BlockchainKeyPair id = BlockchainKeyGenerator.getInstance().generate(ClassicCryptoService.ED25519_ALGORITHM); | |||
| HashDigest ledgerHash = CryptoUtils.hash(ClassicCryptoService.SHA256_ALGORITHM).hash(UUID.randomUUID().toString().getBytes("UTF-8")); | |||
| BlockchainOperationFactory opFactory = new BlockchainOperationFactory(); | |||
| contentBlob = new TxContentBlob(ledgerHash); | |||
| contentBlob.setHash(new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "jd.com".getBytes())); | |||
| // contentBlob.setSubjectAccount(id.getAddress()); | |||
| // contentBlob.setSequenceNumber(1); | |||
| DataAccountKVSetOperation kvsetOP = opFactory.dataAccount(id.getAddress()).set("Name", ByteArray.fromString("AAA", "UTF-8"), -1).getOperation(); | |||
| contentBlob.addOperation(kvsetOP); | |||
| return contentBlob; | |||
| } | |||
| private LedgerTransactionData data; | |||
| @Before | |||
| public void initLedgerTransactionImpl() throws Exception { | |||
| DataContractRegistry.register(LedgerTransaction.class); | |||
| DataContractRegistry.register(Transaction.class); | |||
| DataContractRegistry.register(LedgerDataSnapshot.class); | |||
| DataContractRegistry.register(NodeRequest.class); | |||
| DataContractRegistry.register(EndpointRequest.class); | |||
| DataContractRegistry.register(HashObject.class); | |||
| DataContractRegistry.register(DataAccountKVSetOperation.class); | |||
| TransactionRequest txRequestMessage = initTxRequestMessage(); | |||
| long blockHeight = 9986L; | |||
| data = new LedgerTransactionData(blockHeight, txRequestMessage, TransactionState.SUCCESS, | |||
| initTransactionStagedSnapshot()); | |||
| HashDigest hash = new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "zhangsan".getBytes()); | |||
| HashDigest adminAccountHash = new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "lisi".getBytes()); | |||
| HashDigest userAccountSetHash = new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "wangwu".getBytes()); | |||
| HashDigest dataAccountSetHash = new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "zhaoliu".getBytes()); | |||
| HashDigest contractAccountSetHash = new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "sunqi".getBytes()); | |||
| data.setHash(hash); | |||
| // data.setBlockHeight(blockHeight); | |||
| data.setAdminAccountHash(adminAccountHash); | |||
| data.setUserAccountSetHash(userAccountSetHash); | |||
| data.setDataAccountSetHash(dataAccountSetHash); | |||
| data.setContractAccountSetHash(contractAccountSetHash); | |||
| } | |||
| @Test | |||
| public void testSerialize_LedgerTransaction() throws Exception { | |||
| byte[] serialBytes = BinaryEncodingUtils.encode(data, LedgerTransaction.class); | |||
| LedgerTransaction resolvedData = BinaryEncodingUtils.decode(serialBytes); | |||
| System.out.println("------Assert start ------"); | |||
| assertEquals(resolvedData.getAdminAccountHash(), data.getAdminAccountHash()); | |||
| assertEquals(resolvedData.getContractAccountSetHash(), data.getContractAccountSetHash()); | |||
| assertEquals(resolvedData.getDataAccountSetHash(), data.getDataAccountSetHash()); | |||
| assertEquals(resolvedData.getUserAccountSetHash(), data.getUserAccountSetHash()); | |||
| assertEquals(resolvedData.getExecutionState(), data.getExecutionState()); | |||
| assertEquals(resolvedData.getHash(), data.getHash()); | |||
| assertEquals(resolvedData.getBlockHeight(), data.getBlockHeight()); | |||
| // EndpointSignatures 验证 | |||
| DigitalSignature[] dataEndpointSignatures = data.getEndpointSignatures(); | |||
| DigitalSignature[] resolvedEndpointSignatures = resolvedData.getEndpointSignatures(); | |||
| for (int i = 0; i < dataEndpointSignatures.length; i++) { | |||
| assertEquals(dataEndpointSignatures[i].getPubKey(), resolvedEndpointSignatures[i].getPubKey()); | |||
| assertEquals(dataEndpointSignatures[i].getDigest(), resolvedEndpointSignatures[i].getDigest()); | |||
| } | |||
| // NodeSignatures 验证 | |||
| DigitalSignature[] dataNodeSignatures = data.getNodeSignatures(); | |||
| DigitalSignature[] resolvedNodeSignatures = resolvedData.getNodeSignatures(); | |||
| for (int i = 0; i < dataNodeSignatures.length; i++) { | |||
| assertEquals(dataNodeSignatures[i].getPubKey(), resolvedNodeSignatures[i].getPubKey()); | |||
| assertEquals(dataNodeSignatures[i].getDigest(), resolvedNodeSignatures[i].getDigest()); | |||
| } | |||
| assertEqual(data.getTransactionContent(), resolvedData.getTransactionContent()); | |||
| System.out.println("------Assert OK ------"); | |||
| } | |||
| @Test | |||
| public void testSerialize_Transaction() throws Exception { | |||
| byte[] serialBytes = BinaryEncodingUtils.encode(data, Transaction.class); | |||
| Transaction resolvedData = BinaryEncodingUtils.decode(serialBytes); | |||
| System.out.println("------Assert start ------"); | |||
| assertEquals(resolvedData.getExecutionState(), data.getExecutionState()); | |||
| assertEquals(resolvedData.getHash(), data.getHash()); | |||
| assertEquals(resolvedData.getBlockHeight(), data.getBlockHeight()); | |||
| // EndpointSignatures 验证 | |||
| DigitalSignature[] dataEndpointSignatures = data.getEndpointSignatures(); | |||
| DigitalSignature[] resolvedEndpointSignatures = resolvedData.getEndpointSignatures(); | |||
| for (int i = 0; i < dataEndpointSignatures.length; i++) { | |||
| assertEquals(dataEndpointSignatures[i].getPubKey(), resolvedEndpointSignatures[i].getPubKey()); | |||
| assertEquals(dataEndpointSignatures[i].getDigest(), resolvedEndpointSignatures[i].getDigest()); | |||
| } | |||
| // NodeSignatures 验证 | |||
| DigitalSignature[] dataNodeSignatures = data.getNodeSignatures(); | |||
| DigitalSignature[] resolvedNodeSignatures = resolvedData.getNodeSignatures(); | |||
| for (int i = 0; i < dataNodeSignatures.length; i++) { | |||
| assertEquals(dataNodeSignatures[i].getPubKey(), resolvedNodeSignatures[i].getPubKey()); | |||
| assertEquals(dataNodeSignatures[i].getDigest(), resolvedNodeSignatures[i].getDigest()); | |||
| } | |||
| assertEqual(data.getTransactionContent(), resolvedData.getTransactionContent()); | |||
| System.out.println("------Assert OK ------"); | |||
| } | |||
| @Test | |||
| public void testSerialize_LedgerDataSnapshot() throws Exception { | |||
| byte[] serialBytes = BinaryEncodingUtils.encode(data, LedgerDataSnapshot.class); | |||
| LedgerDataSnapshot resolvedData = BinaryEncodingUtils.decode(serialBytes); | |||
| System.out.println("------Assert start ------"); | |||
| assertEquals(resolvedData.getAdminAccountHash(), data.getAdminAccountHash()); | |||
| assertEquals(resolvedData.getContractAccountSetHash(), data.getContractAccountSetHash()); | |||
| assertEquals(resolvedData.getDataAccountSetHash(), data.getDataAccountSetHash()); | |||
| assertEquals(resolvedData.getUserAccountSetHash(), data.getUserAccountSetHash()); | |||
| System.out.println("------Assert OK ------"); | |||
| } | |||
| @Test | |||
| public void testSerialize_NodeRequest() throws Exception { | |||
| byte[] serialBytes = BinaryEncodingUtils.encode(data, NodeRequest.class); | |||
| NodeRequest resolvedData = BinaryEncodingUtils.decode(serialBytes); | |||
| System.out.println("------Assert start ------"); | |||
| // EndpointSignatures 验证 | |||
| DigitalSignature[] dataEndpointSignatures = data.getEndpointSignatures(); | |||
| DigitalSignature[] resolvedEndpointSignatures = resolvedData.getEndpointSignatures(); | |||
| for (int i = 0; i < dataEndpointSignatures.length; i++) { | |||
| assertEquals(dataEndpointSignatures[i].getPubKey(), resolvedEndpointSignatures[i].getPubKey()); | |||
| assertEquals(dataEndpointSignatures[i].getDigest(), resolvedEndpointSignatures[i].getDigest()); | |||
| } | |||
| // NodeSignatures 验证 | |||
| DigitalSignature[] dataNodeSignatures = data.getNodeSignatures(); | |||
| DigitalSignature[] resolvedNodeSignatures = resolvedData.getNodeSignatures(); | |||
| for (int i = 0; i < dataNodeSignatures.length; i++) { | |||
| assertEquals(dataNodeSignatures[i].getPubKey(), resolvedNodeSignatures[i].getPubKey()); | |||
| assertEquals(dataNodeSignatures[i].getDigest(), resolvedNodeSignatures[i].getDigest()); | |||
| } | |||
| assertEqual(data.getTransactionContent(), resolvedData.getTransactionContent()); | |||
| System.out.println("------Assert OK ------"); | |||
| } | |||
| @Test | |||
| public void testSerialize_EndpointRequest() throws Exception { | |||
| byte[] serialBytes = BinaryEncodingUtils.encode(data, EndpointRequest.class); | |||
| EndpointRequest resolvedData = BinaryEncodingUtils.decode(serialBytes); | |||
| System.out.println("------Assert start ------"); | |||
| // EndpointSignatures 验证 | |||
| DigitalSignature[] dataEndpointSignatures = data.getEndpointSignatures(); | |||
| DigitalSignature[] resolvedEndpointSignatures = resolvedData.getEndpointSignatures(); | |||
| for (int i = 0; i < dataEndpointSignatures.length; i++) { | |||
| assertEquals(dataEndpointSignatures[i].getPubKey(), resolvedEndpointSignatures[i].getPubKey()); | |||
| assertEquals(dataEndpointSignatures[i].getDigest(), resolvedEndpointSignatures[i].getDigest()); | |||
| } | |||
| assertEqual(data.getTransactionContent(), resolvedData.getTransactionContent()); | |||
| System.out.println("------Assert OK ------"); | |||
| } | |||
| private void assertEqual(TransactionContent dataTxContent, TransactionContent resolvedTxContent) { | |||
| assertEquals(dataTxContent.getHash(), resolvedTxContent.getHash()); | |||
| assertEquals(dataTxContent.getLedgerHash(), resolvedTxContent.getLedgerHash()); | |||
| // assertEquals(dataTxContent.getSequenceNumber(), | |||
| // resolvedTxContent.getSequenceNumber()); | |||
| // assertEquals(dataTxContent.getSubjectAccount(), | |||
| // resolvedTxContent.getSubjectAccount()); | |||
| } | |||
| private TransactionStagedSnapshot initTransactionStagedSnapshot() { | |||
| TransactionStagedSnapshot transactionStagedSnapshot = new TransactionStagedSnapshot(); | |||
| transactionStagedSnapshot | |||
| .setAdminAccountHash(new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "zhangsan".getBytes())); | |||
| transactionStagedSnapshot | |||
| .setContractAccountSetHash(new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "lisi".getBytes())); | |||
| transactionStagedSnapshot | |||
| .setDataAccountSetHash(new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "wangwu".getBytes())); | |||
| transactionStagedSnapshot | |||
| .setUserAccountSetHash(new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "zhaoliu".getBytes())); | |||
| return transactionStagedSnapshot; | |||
| } | |||
| private TxRequestMessage initTxRequestMessage() throws Exception { | |||
| TxRequestMessage txRequestMessage = new TxRequestMessage(initTransactionContent()); | |||
| SignatureDigest digest1 = new SignatureDigest(ClassicCryptoService.ED25519_ALGORITHM, "zhangsan".getBytes()); | |||
| SignatureDigest digest2 = new SignatureDigest(ClassicCryptoService.ED25519_ALGORITHM, "lisi".getBytes()); | |||
| DigitalSignatureBlob endPoint1 = new DigitalSignatureBlob( | |||
| new PubKey(ClassicCryptoService.ED25519_ALGORITHM, "jd1.com".getBytes()), digest1); | |||
| DigitalSignatureBlob endPoint2 = new DigitalSignatureBlob( | |||
| new PubKey(ClassicCryptoService.ED25519_ALGORITHM, "jd2.com".getBytes()), digest2); | |||
| txRequestMessage.addEndpointSignatures(endPoint1); | |||
| txRequestMessage.addEndpointSignatures(endPoint2); | |||
| SignatureDigest digest3 = new SignatureDigest(ClassicCryptoService.ED25519_ALGORITHM, "wangwu".getBytes()); | |||
| SignatureDigest digest4 = new SignatureDigest(ClassicCryptoService.ED25519_ALGORITHM, "zhaoliu".getBytes()); | |||
| DigitalSignatureBlob node1 = new DigitalSignatureBlob( | |||
| new PubKey(ClassicCryptoService.ED25519_ALGORITHM, "jd3.com".getBytes()), digest3); | |||
| DigitalSignatureBlob node2 = new DigitalSignatureBlob( | |||
| new PubKey(ClassicCryptoService.ED25519_ALGORITHM, "jd4.com".getBytes()), digest4); | |||
| txRequestMessage.addNodeSignatures(node1); | |||
| txRequestMessage.addNodeSignatures(node2); | |||
| return txRequestMessage; | |||
| } | |||
| private TransactionContent initTransactionContent() throws Exception { | |||
| TxContentBlob contentBlob = null; | |||
| BlockchainKeyPair id = BlockchainKeyGenerator.getInstance().generate(ClassicCryptoService.ED25519_ALGORITHM); | |||
| HashDigest ledgerHash = CryptoServiceProviders.getHashFunction("SHA256") | |||
| .hash(UUID.randomUUID().toString().getBytes("UTF-8")); | |||
| BlockchainOperationFactory opFactory = new BlockchainOperationFactory(); | |||
| contentBlob = new TxContentBlob(ledgerHash); | |||
| contentBlob.setHash(new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "jd.com".getBytes())); | |||
| // contentBlob.setSubjectAccount(id.getAddress()); | |||
| // contentBlob.setSequenceNumber(1); | |||
| DataAccountKVSetOperation kvsetOP = opFactory.dataAccount(id.getAddress()) | |||
| .set("Name", ByteArray.fromString("AAA", "UTF-8"), -1).getOperation(); | |||
| contentBlob.addOperation(kvsetOP); | |||
| return contentBlob; | |||
| } | |||
| } | |||
| @@ -49,14 +49,14 @@ | |||
| <groupId>io.nats</groupId> | |||
| <artifactId>jnats</artifactId> | |||
| </dependency> | |||
| <dependency> | |||
| <groupId>com.jd.blockchain</groupId> | |||
| <artifactId>crypto-impl</artifactId> | |||
| <artifactId>crypto-classic</artifactId> | |||
| <version>${project.version}</version> | |||
| <scope>test</scope> | |||
| </dependency> | |||
| </dependencies> | |||
| <build> | |||
| <plugins> | |||
| @@ -8,14 +8,13 @@ import java.util.Properties; | |||
| import java.util.Random; | |||
| import java.util.concurrent.CountDownLatch; | |||
| import com.jd.blockchain.storage.service.KVStorageService; | |||
| import org.springframework.core.io.ClassPathResource; | |||
| import com.jd.blockchain.consensus.ConsensusProvider; | |||
| import com.jd.blockchain.consensus.ConsensusProviders; | |||
| import com.jd.blockchain.consensus.ConsensusSettings; | |||
| import com.jd.blockchain.crypto.AddressEncoding; | |||
| import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
| import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
| import com.jd.blockchain.crypto.PrivKey; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; | |||
| @@ -39,6 +38,7 @@ import com.jd.blockchain.ledger.core.LedgerRepository; | |||
| import com.jd.blockchain.ledger.core.impl.LedgerManager; | |||
| import com.jd.blockchain.sdk.BlockchainService; | |||
| import com.jd.blockchain.sdk.client.GatewayServiceFactory; | |||
| import com.jd.blockchain.storage.service.KVStorageService; | |||
| import com.jd.blockchain.tools.initializer.DBConnectionConfig; | |||
| import com.jd.blockchain.tools.initializer.LedgerBindingConfig; | |||
| import com.jd.blockchain.tools.initializer.LedgerInitProperties; | |||
| @@ -59,7 +59,6 @@ public class IntegrationTest { | |||
| private String contractZipName = "AssetContract1.contract"; | |||
| private String eventName = "issue-asset"; | |||
| HashDigest txContentHash; | |||
| String pubKeyVal = "jd.com" + System.currentTimeMillis();; | |||
| // String userPubKeyVal = "this is user's pubKey"; | |||
| // 保存资产总数的键; | |||
| private static final String KEY_TOTAL = "TOTAL"; | |||
| @@ -597,7 +596,7 @@ public class IntegrationTest { | |||
| txTpl.contractEvents().send(contractDeployKey.getAddress(), eventName, | |||
| ("888##abc##" + contractDataKey.getAddress() + "##" + previousBlock.getHash().toBase58() + "##" | |||
| + userKey.getAddress() + "##" + contractDeployKey.getAddress() + "##" + txContentHash.toBase58() | |||
| + "##" + pubKeyVal).getBytes()); | |||
| + "##SOME-VALUE").getBytes()); | |||
| // 签名; | |||
| PreparedTransaction ptx = txTpl.prepare(); | |||
| @@ -617,7 +616,8 @@ public class IntegrationTest { | |||
| // 验证合约中的赋值,外部可以获得; | |||
| DataAccountSet dataAccountSet = ledgerOfNode0.getDataAccountSet(backgroundLedgerBlock); | |||
| PubKey pubKey = new PubKey(CryptoAlgorithm.ED25519, pubKeyVal.getBytes()); | |||
| CryptoKeyPair key = CryptoServiceProviders.getSignatureFunction("ED25519").generateKeyPair(); | |||
| PubKey pubKey = key.getPubKey(); | |||
| Bytes dataAddress = AddressEncoding.generateAddress(pubKey); | |||
| // 验证userAccount,从合约内部赋值,然后外部验证;由于目前不允许输入重复的key,所以在内部合约中构建的key,不便于在外展示,屏蔽之; | |||
| @@ -6,14 +6,13 @@ import java.util.Map; | |||
| import java.util.Properties; | |||
| import java.util.concurrent.ConcurrentHashMap; | |||
| import com.jd.blockchain.storage.service.utils.MemoryDBConnFactory; | |||
| import org.springframework.core.io.ClassPathResource; | |||
| import com.jd.blockchain.consensus.ConsensusProvider; | |||
| import com.jd.blockchain.consensus.ConsensusProviders; | |||
| import com.jd.blockchain.consensus.ConsensusSettings; | |||
| import com.jd.blockchain.crypto.AddressEncoding; | |||
| import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
| import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
| import com.jd.blockchain.crypto.PrivKey; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; | |||
| @@ -27,6 +26,7 @@ import com.jd.blockchain.ledger.core.LedgerRepository; | |||
| import com.jd.blockchain.ledger.core.UserAccount; | |||
| import com.jd.blockchain.ledger.core.UserAccountSet; | |||
| import com.jd.blockchain.ledger.core.impl.LedgerManager; | |||
| import com.jd.blockchain.storage.service.utils.MemoryDBConnFactory; | |||
| //import com.jd.blockchain.storage.service.utils.MemoryBasedDb; | |||
| import com.jd.blockchain.tools.initializer.DBConnectionConfig; | |||
| import com.jd.blockchain.tools.initializer.LedgerInitProcess; | |||
| @@ -217,7 +217,7 @@ public class LedgerInitializeTest { | |||
| CryptoConfig cryptoSetting = new CryptoConfig(); | |||
| cryptoSetting.setAutoVerifyHash(autoVerifyHash); | |||
| cryptoSetting.setHashAlgorithm(CryptoAlgorithm.SHA256); | |||
| cryptoSetting.setHashAlgorithm(CryptoServiceProviders.getAlgorithm("SHA256")); | |||
| partiKey = new CryptoKeyPair(setting.getConsensusParticipant(0).getPubKey(), privKey); | |||
| @@ -5,7 +5,6 @@ import java.io.InputStream; | |||
| import java.util.Properties; | |||
| import java.util.concurrent.CountDownLatch; | |||
| import com.jd.blockchain.storage.service.impl.composite.CompositeConnectionFactory; | |||
| import org.springframework.boot.SpringApplication; | |||
| import org.springframework.context.ConfigurableApplicationContext; | |||
| import org.springframework.core.io.ClassPathResource; | |||
| @@ -15,11 +14,11 @@ import com.jd.blockchain.consensus.ConsensusProvider; | |||
| import com.jd.blockchain.consensus.ConsensusProviders; | |||
| import com.jd.blockchain.consensus.ConsensusSettings; | |||
| import com.jd.blockchain.crypto.AddressEncoding; | |||
| import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
| import com.jd.blockchain.crypto.CryptoUtils; | |||
| import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
| import com.jd.blockchain.crypto.PrivKey; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.crypto.asymmetric.SignatureDigest; | |||
| import com.jd.blockchain.crypto.asymmetric.SignatureFunction; | |||
| import com.jd.blockchain.crypto.hash.HashDigest; | |||
| import com.jd.blockchain.ledger.LedgerBlock; | |||
| import com.jd.blockchain.ledger.LedgerInitOperation; | |||
| @@ -33,6 +32,7 @@ import com.jd.blockchain.ledger.core.UserAccount; | |||
| import com.jd.blockchain.ledger.core.UserAccountSet; | |||
| import com.jd.blockchain.ledger.core.impl.LedgerManager; | |||
| import com.jd.blockchain.storage.service.DbConnection; | |||
| import com.jd.blockchain.storage.service.impl.composite.CompositeConnectionFactory; | |||
| //import com.jd.blockchain.storage.service.utils.MemoryBasedDb; | |||
| import com.jd.blockchain.tools.initializer.DBConnectionConfig; | |||
| import com.jd.blockchain.tools.initializer.LedgerBindingConfig; | |||
| @@ -73,7 +73,7 @@ public class LedgerInitializeWebTest { | |||
| LedgerInitProperties initSetting = loadInitSetting_1(); | |||
| // 加载共识配置; | |||
| Properties props = loadConsensusSetting(); | |||
| // ConsensusProperties csProps = new ConsensusProperties(props); | |||
| // ConsensusProperties csProps = new ConsensusProperties(props); | |||
| ConsensusProvider csProvider = getConsensusProvider(); | |||
| ConsensusSettings csProps = csProvider.getSettingsFactory().getConsensusSettingsBuilder().createSettings(props); | |||
| @@ -223,10 +223,11 @@ public class LedgerInitializeWebTest { | |||
| public SignatureDigest signPermissionRequest(int requesterId, PrivKey privKey, LedgerInitProperties initSetting) { | |||
| byte[] reqAuthBytes = BytesUtils.concat(BytesUtils.toBytes(requesterId), initSetting.getLedgerSeed()); | |||
| SignatureDigest reqAuthSign = CryptoUtils.sign(CryptoAlgorithm.ED25519).sign(privKey, reqAuthBytes); | |||
| SignatureFunction signFunc = CryptoServiceProviders.getSignatureFunction("ED25519"); | |||
| SignatureDigest reqAuthSign = signFunc.sign(privKey, reqAuthBytes); | |||
| return reqAuthSign; | |||
| } | |||
| private static ConsensusProvider getConsensusProvider() { | |||
| return ConsensusProviders.getProvider("com.jd.blockchain.consensus.bftsmart.BftsmartConsensusProvider"); | |||
| } | |||
| @@ -237,7 +238,7 @@ public class LedgerInitializeWebTest { | |||
| Prompter consolePrompter = new PresetAnswerPrompter("N"); // new ConsolePrompter(); | |||
| LedgerInitProperties initSetting = loadInitSetting_2(); | |||
| Properties props = loadConsensusSetting(); | |||
| // ConsensusProperties csProps = new ConsensusProperties(props); | |||
| // ConsensusProperties csProps = new ConsensusProperties(props); | |||
| ConsensusProvider csProvider = getConsensusProvider(); | |||
| ConsensusSettings csProps = csProvider.getSettingsFactory().getConsensusSettingsBuilder().createSettings(props); | |||
| @@ -263,23 +264,23 @@ public class LedgerInitializeWebTest { | |||
| DBConnectionConfig testDb0 = new DBConnectionConfig(); | |||
| testDb0.setConnectionUri("memory://local/0"); | |||
| AsyncCallback<HashDigest> callback0 = node0.startInit(privkey0, initSetting, csProps, csProvider, testDb0, consolePrompter, | |||
| quitLatch); | |||
| AsyncCallback<HashDigest> callback0 = node0.startInit(privkey0, initSetting, csProps, csProvider, testDb0, | |||
| consolePrompter, quitLatch); | |||
| DBConnectionConfig testDb1 = new DBConnectionConfig(); | |||
| testDb1.setConnectionUri("memory://local/1"); | |||
| AsyncCallback<HashDigest> callback1 = node1.startInit(privkey1, initSetting, csProps, csProvider, testDb1, consolePrompter, | |||
| quitLatch); | |||
| AsyncCallback<HashDigest> callback1 = node1.startInit(privkey1, initSetting, csProps, csProvider, testDb1, | |||
| consolePrompter, quitLatch); | |||
| DBConnectionConfig testDb2 = new DBConnectionConfig(); | |||
| testDb2.setConnectionUri("memory://local/2"); | |||
| AsyncCallback<HashDigest> callback2 = node2.startInit(privkey2, initSetting, csProps,csProvider, testDb2, consolePrompter, | |||
| quitLatch); | |||
| AsyncCallback<HashDigest> callback2 = node2.startInit(privkey2, initSetting, csProps, csProvider, testDb2, | |||
| consolePrompter, quitLatch); | |||
| DBConnectionConfig testDb03 = new DBConnectionConfig(); | |||
| testDb03.setConnectionUri("memory://local/3"); | |||
| AsyncCallback<HashDigest> callback3 = node3.startInit(privkey3, initSetting, csProps, csProvider, testDb03, consolePrompter, | |||
| quitLatch); | |||
| AsyncCallback<HashDigest> callback3 = node3.startInit(privkey3, initSetting, csProps, csProvider, testDb03, | |||
| consolePrompter, quitLatch); | |||
| HashDigest ledgerHash0 = callback0.waitReturn(); | |||
| HashDigest ledgerHash1 = callback1.waitReturn(); | |||
| @@ -347,7 +348,7 @@ public class LedgerInitializeWebTest { | |||
| private DBConnectionConfig dbConnConfig; | |||
| // private MQConnectionConfig mqConnConfig; | |||
| // private MQConnectionConfig mqConnConfig; | |||
| private volatile ConfigurableApplicationContext ctx; | |||
| @@ -1,14 +1,37 @@ | |||
| package test.com.jd.blockchain.intgr.perf; | |||
| import java.io.File; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| import java.util.Map; | |||
| import java.util.Properties; | |||
| import java.util.concurrent.ConcurrentHashMap; | |||
| import java.util.stream.DoubleStream; | |||
| import org.springframework.core.io.ClassPathResource; | |||
| import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
| import com.jd.blockchain.consensus.ConsensusProvider; | |||
| import com.jd.blockchain.consensus.ConsensusProviders; | |||
| import com.jd.blockchain.consensus.ConsensusSettings; | |||
| import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
| import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
| import com.jd.blockchain.crypto.PrivKey; | |||
| import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; | |||
| import com.jd.blockchain.crypto.hash.HashDigest; | |||
| import com.jd.blockchain.ledger.*; | |||
| import com.jd.blockchain.ledger.BlockchainIdentity; | |||
| import com.jd.blockchain.ledger.BlockchainKeyGenerator; | |||
| import com.jd.blockchain.ledger.BlockchainKeyPair; | |||
| 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; | |||
| @@ -34,20 +57,10 @@ import com.jd.blockchain.utils.concurrent.ThreadInvoker.AsyncCallback; | |||
| import com.jd.blockchain.utils.io.BytesUtils; | |||
| import com.jd.blockchain.utils.io.FileUtils; | |||
| import com.jd.blockchain.utils.net.NetworkAddress; | |||
| import org.springframework.core.io.ClassPathResource; | |||
| import test.com.jd.blockchain.intgr.PresetAnswerPrompter; | |||
| import test.com.jd.blockchain.intgr.perf.Utils.NodeContext; | |||
| import java.io.File; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| import java.util.Map; | |||
| import java.util.Properties; | |||
| import java.util.concurrent.ConcurrentHashMap; | |||
| import java.util.stream.DoubleStream; | |||
| //import com.jd.blockchain.storage.service.utils.MemoryBasedDb; | |||
| /** | |||
| @@ -97,8 +110,8 @@ public class LedgerPerformanceTest { | |||
| dbType = DBType.ROCKSDB; | |||
| } | |||
| CryptoAlgorithm hashAlg = ArgumentSet.hasOption(args, "-160") ? CryptoAlgorithm.RIPEMD160 | |||
| : CryptoAlgorithm.SHA256; | |||
| CryptoAlgorithm hashAlg = ArgumentSet.hasOption(args, "-160") ? CryptoServiceProviders.getAlgorithm("RIPEMD260") | |||
| : CryptoServiceProviders.getAlgorithm("SHA256"); | |||
| System.out.println( | |||
| String.format("----- LedgerPerformanceTest [HashAlgorithm=%s][DBType=%s] ----", hashAlg, dbType)); | |||
| // 初始化,并获取其中一个节点的账本,单独进行性能测试; | |||
| @@ -11,6 +11,7 @@ import org.springframework.core.io.ClassPathResource; | |||
| import com.jd.blockchain.consensus.ConsensusProvider; | |||
| import com.jd.blockchain.consensus.ConsensusSettings; | |||
| import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
| import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
| import com.jd.blockchain.crypto.PrivKey; | |||
| import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; | |||
| import com.jd.blockchain.crypto.asymmetric.SignatureDigest; | |||
| @@ -124,8 +125,9 @@ public class Utils { | |||
| public AsyncCallback<HashDigest> startInit(int currentId, PrivKey privKey, LedgerInitProperties setting, | |||
| ConsensusSettings csProps, ConsensusProvider consensusProvider, DBConnectionConfig dbConnConfig, | |||
| Prompter prompter, boolean autoVerifyHash) { | |||
| CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("SHA256"); | |||
| return startInit(currentId, privKey, setting, csProps, consensusProvider, dbConnConfig, prompter, | |||
| autoVerifyHash, CryptoAlgorithm.SHA256); | |||
| autoVerifyHash, algorithm); | |||
| } | |||
| public AsyncCallback<HashDigest> startInit(int currentId, PrivKey privKey, LedgerInitProperties setting, | |||
| @@ -15,7 +15,7 @@ import org.junit.Test; | |||
| import org.springframework.core.io.ClassPathResource; | |||
| import com.jd.blockchain.crypto.AddressEncoding; | |||
| import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
| import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
| import com.jd.blockchain.crypto.PrivKey; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; | |||
| @@ -52,7 +52,6 @@ import test.com.jd.blockchain.intgr.initializer.LedgerInitializeWeb4SingleStepsT | |||
| public class IntegrationTestAll4Redis { | |||
| public static final String PASSWORD = "abc"; | |||
| public static final String[] PUB_KEYS = { "endPsK36imXrY66pru6ttZ8dZ3TynWekmdqoM1K7ZRRoRBBiYVzM", | |||
| @@ -76,8 +75,8 @@ public class IntegrationTestAll4Redis { | |||
| private String contractZipName = "AssetContract1.contract"; | |||
| private String eventName = "issue-asset"; | |||
| HashDigest txContentHash; | |||
| String pubKeyVal = "jd.com"+System.currentTimeMillis(); | |||
| // String userPubKeyVal = "this is user's pubKey"; | |||
| String pubKeyVal = "jd.com" + System.currentTimeMillis(); | |||
| // String userPubKeyVal = "this is user's pubKey"; | |||
| // 保存资产总数的键; | |||
| private static final String KEY_TOTAL = "TOTAL"; | |||
| // 第二个参数; | |||
| @@ -130,8 +129,10 @@ public class IntegrationTestAll4Redis { | |||
| gwStarting0.waitReturn(); | |||
| // 执行测试用例之前,校验每个节点的一致性; | |||
| LedgerRepository[] ledgers = buildLedgers(new LedgerBindingConfig[]{bindingConfig0, bindingConfig1, bindingConfig2, bindingConfig3}, | |||
| new DbConnectionFactory[]{dbConnectionFactory0,dbConnectionFactory1,dbConnectionFactory2,dbConnectionFactory3}); | |||
| LedgerRepository[] ledgers = buildLedgers( | |||
| new LedgerBindingConfig[] { bindingConfig0, bindingConfig1, bindingConfig2, bindingConfig3 }, | |||
| new DbConnectionFactory[] { dbConnectionFactory0, dbConnectionFactory1, dbConnectionFactory2, | |||
| dbConnectionFactory3 }); | |||
| testConsistencyAmongNodes(ledgers); | |||
| PrivKey privkey0 = KeyGenCommand.decodePrivKeyWithRawPassword(PRIV_KEYS[0], PASSWORD); | |||
| @@ -144,34 +145,35 @@ public class IntegrationTestAll4Redis { | |||
| PubKey pubKey2 = KeyGenCommand.decodePubKey(PUB_KEYS[2]); | |||
| PubKey pubKey3 = KeyGenCommand.decodePubKey(PUB_KEYS[3]); | |||
| CryptoKeyPair adminKey = new CryptoKeyPair(pubKey0,privkey0); | |||
| CryptoKeyPair adminKey = new CryptoKeyPair(pubKey0, privkey0); | |||
| testWriteBatchTransactions(gateway0, adminKey, ledgers[0]); | |||
| testSDK(gateway0, adminKey, ledgers[0]); | |||
| // 执行测试用例之后,校验每个节点的一致性; | |||
| testConsistencyAmongNodes(ledgers); | |||
| } | |||
| private LedgerBindingConfig loadBindingConfig(int id){ | |||
| private LedgerBindingConfig loadBindingConfig(int id) { | |||
| ClassPathResource res = new ClassPathResource("ledger-binding-redis-" + id + ".conf"); | |||
| try(InputStream in = res.getInputStream()){ | |||
| try (InputStream in = res.getInputStream()) { | |||
| return LedgerBindingConfig.resolve(in); | |||
| } catch (IOException e) { | |||
| throw new IllegalStateException(e.getMessage(), e); | |||
| } | |||
| } | |||
| private LedgerRepository[] buildLedgers(LedgerBindingConfig[] bindingConfigs, DbConnectionFactory[] dbConnectionFactories){ | |||
| int[] ids = {0, 1, 2, 3}; | |||
| private LedgerRepository[] buildLedgers(LedgerBindingConfig[] bindingConfigs, | |||
| DbConnectionFactory[] dbConnectionFactories) { | |||
| int[] ids = { 0, 1, 2, 3 }; | |||
| LedgerRepository[] ledgers = new LedgerRepository[ids.length]; | |||
| LedgerManager[] ledgerManagers = new LedgerManager[ids.length]; | |||
| for (int i = 0; i < ids.length; i++) { | |||
| ledgerManagers[i] = new LedgerManager(); | |||
| HashDigest ledgerHash = bindingConfigs[0].getLedgerHashs()[0]; | |||
| DbConnection conn = dbConnectionFactories[i].connect(bindingConfigs[i].getLedger(ledgerHash).getDbConnection().getUri(), | |||
| DbConnection conn = dbConnectionFactories[i].connect( | |||
| bindingConfigs[i].getLedger(ledgerHash).getDbConnection().getUri(), | |||
| bindingConfigs[i].getLedger(ledgerHash).getDbConnection().getPassword()); | |||
| ledgers[i] = ledgerManagers[i].register(ledgerHash, conn.getStorageService()); | |||
| } | |||
| @@ -199,8 +201,9 @@ public class IntegrationTestAll4Redis { | |||
| } | |||
| } | |||
| //测试一个区块包含多个交易的写入情况,并验证写入结果; | |||
| private void testWriteBatchTransactions(GatewayTestRunner gateway, CryptoKeyPair adminKey,LedgerRepository ledgerRepository) { | |||
| // 测试一个区块包含多个交易的写入情况,并验证写入结果; | |||
| private void testWriteBatchTransactions(GatewayTestRunner gateway, CryptoKeyPair adminKey, | |||
| LedgerRepository ledgerRepository) { | |||
| // 连接网关; | |||
| GatewayServiceFactory gwsrvFact = GatewayServiceFactory.connect(gateway.getServiceAddress()); | |||
| BlockchainService blockchainService = gwsrvFact.getBlockchainService(); | |||
| @@ -209,18 +212,16 @@ public class IntegrationTestAll4Redis { | |||
| TransactionTemplate txTpl = blockchainService.newTransaction(ledgerHashs[0]); | |||
| //regist user account | |||
| // regist user account | |||
| txTpl.users().register(userKey.getIdentity()); | |||
| //regist data account | |||
| // regist data account | |||
| txTpl.dataAccounts().register(dataKey.getIdentity()); | |||
| //add kv ops for data account | |||
| // add kv ops for data account | |||
| DataAccountKVSetOperation dataKvsetOP = txTpl.dataAccount(dataKey.getAddress()) | |||
| .set("A", "Value_A_0".getBytes(), -1) | |||
| .set("B", "Value_B_0".getBytes(), -1) | |||
| .set("C", "Value_C_0".getBytes(), -1) | |||
| .set("D", "Value_D_0".getBytes(), -1).getOperation(); | |||
| .set("A", "Value_A_0".getBytes(), -1).set("B", "Value_B_0".getBytes(), -1) | |||
| .set("C", "Value_C_0".getBytes(), -1).set("D", "Value_D_0".getBytes(), -1).getOperation(); | |||
| // 签名; | |||
| PreparedTransaction ptx = txTpl.prepare(); | |||
| @@ -232,27 +233,39 @@ public class IntegrationTestAll4Redis { | |||
| assertTrue(txResp.isSuccess()); | |||
| assertEquals(ledgerRepository.retrieveLatestBlockHeight(), txResp.getBlockHeight()); | |||
| assertArrayEquals("Value_A_0".getBytes(), ledgerRepository.getDataAccountSet(ledgerRepository.retrieveLatestBlock()).getDataAccount(dataKey.getAddress()).getBytes("A")); | |||
| assertArrayEquals("Value_B_0".getBytes(), ledgerRepository.getDataAccountSet(ledgerRepository.retrieveLatestBlock()).getDataAccount(dataKey.getAddress()).getBytes("B")); | |||
| assertArrayEquals("Value_C_0".getBytes(), ledgerRepository.getDataAccountSet(ledgerRepository.retrieveLatestBlock()).getDataAccount(dataKey.getAddress()).getBytes("C")); | |||
| assertArrayEquals("Value_D_0".getBytes(), ledgerRepository.getDataAccountSet(ledgerRepository.retrieveLatestBlock()).getDataAccount(dataKey.getAddress()).getBytes("D")); | |||
| assertEquals(0, ledgerRepository.getDataAccountSet(ledgerRepository.retrieveLatestBlock()).getDataAccount(dataKey.getAddress()).getDataVersion("A")); | |||
| assertEquals(0, ledgerRepository.getDataAccountSet(ledgerRepository.retrieveLatestBlock()).getDataAccount(dataKey.getAddress()).getDataVersion("B")); | |||
| assertEquals(0, ledgerRepository.getDataAccountSet(ledgerRepository.retrieveLatestBlock()).getDataAccount(dataKey.getAddress()).getDataVersion("C")); | |||
| assertEquals(0, ledgerRepository.getDataAccountSet(ledgerRepository.retrieveLatestBlock()).getDataAccount(dataKey.getAddress()).getDataVersion("D")); | |||
| assertArrayEquals("Value_A_0".getBytes(), | |||
| ledgerRepository.getDataAccountSet(ledgerRepository.retrieveLatestBlock()) | |||
| .getDataAccount(dataKey.getAddress()).getBytes("A")); | |||
| assertArrayEquals("Value_B_0".getBytes(), | |||
| ledgerRepository.getDataAccountSet(ledgerRepository.retrieveLatestBlock()) | |||
| .getDataAccount(dataKey.getAddress()).getBytes("B")); | |||
| assertArrayEquals("Value_C_0".getBytes(), | |||
| ledgerRepository.getDataAccountSet(ledgerRepository.retrieveLatestBlock()) | |||
| .getDataAccount(dataKey.getAddress()).getBytes("C")); | |||
| assertArrayEquals("Value_D_0".getBytes(), | |||
| ledgerRepository.getDataAccountSet(ledgerRepository.retrieveLatestBlock()) | |||
| .getDataAccount(dataKey.getAddress()).getBytes("D")); | |||
| assertEquals(0, ledgerRepository.getDataAccountSet(ledgerRepository.retrieveLatestBlock()) | |||
| .getDataAccount(dataKey.getAddress()).getDataVersion("A")); | |||
| assertEquals(0, ledgerRepository.getDataAccountSet(ledgerRepository.retrieveLatestBlock()) | |||
| .getDataAccount(dataKey.getAddress()).getDataVersion("B")); | |||
| assertEquals(0, ledgerRepository.getDataAccountSet(ledgerRepository.retrieveLatestBlock()) | |||
| .getDataAccount(dataKey.getAddress()).getDataVersion("C")); | |||
| assertEquals(0, ledgerRepository.getDataAccountSet(ledgerRepository.retrieveLatestBlock()) | |||
| .getDataAccount(dataKey.getAddress()).getDataVersion("D")); | |||
| return; | |||
| } | |||
| private void testSDK(GatewayTestRunner gateway, CryptoKeyPair adminKey,LedgerRepository ledgerRepository) { | |||
| private void testSDK(GatewayTestRunner gateway, CryptoKeyPair adminKey, LedgerRepository ledgerRepository) { | |||
| // 连接网关; | |||
| GatewayServiceFactory gwsrvFact = GatewayServiceFactory.connect(gateway.getServiceAddress()); | |||
| BlockchainService bcsrv = gwsrvFact.getBlockchainService(); | |||
| HashDigest[] ledgerHashs = bcsrv.getLedgerHashs(); | |||
| BlockchainKeyPair newUserAcount = testSDK_RegisterUser(adminKey, ledgerHashs[0], bcsrv, ledgerRepository); | |||
| BlockchainKeyPair newDataAccount = testSDK_RegisterDataAccount(adminKey, ledgerHashs[0], bcsrv, ledgerRepository); | |||
| BlockchainKeyPair newDataAccount = testSDK_RegisterDataAccount(adminKey, ledgerHashs[0], bcsrv, | |||
| ledgerRepository); | |||
| testSDK_InsertData(adminKey, ledgerHashs[0], bcsrv, newDataAccount.getAddress(), ledgerRepository); | |||
| LedgerBlock latestBlock = testSDK_Contract(adminKey, ledgerHashs[0], bcsrv, ledgerRepository); | |||
| @@ -291,7 +304,8 @@ public class IntegrationTestAll4Redis { | |||
| assertEquals(txResp.getContentHash(), prepTx.getHash()); | |||
| assertEquals(txResp.getBlockHash(), ledgerRepository.getLatestBlockHash()); | |||
| KVDataEntry[] kvDataEntries = blockchainService.getDataEntries(ledgerHash, dataAccountAddress.toString(), dataKey); | |||
| KVDataEntry[] kvDataEntries = blockchainService.getDataEntries(ledgerHash, dataAccountAddress.toString(), | |||
| dataKey); | |||
| for (KVDataEntry kvDataEntry : kvDataEntries) { | |||
| assertEquals(dataKey, kvDataEntry.getKey()); | |||
| String valHexText = (String) kvDataEntry.getValue(); | |||
| @@ -302,7 +316,7 @@ public class IntegrationTestAll4Redis { | |||
| } | |||
| private BlockchainKeyPair testSDK_RegisterDataAccount(CryptoKeyPair adminKey, HashDigest ledgerHash, | |||
| BlockchainService blockchainService, LedgerRepository ledgerRepository) { | |||
| BlockchainService blockchainService, LedgerRepository ledgerRepository) { | |||
| // 注册数据账户,并验证最终写入; | |||
| BlockchainKeyPair dataAccount = BlockchainKeyGenerator.getInstance().generate(); | |||
| @@ -337,7 +351,7 @@ public class IntegrationTestAll4Redis { | |||
| } | |||
| private BlockchainKeyPair testSDK_RegisterUser(CryptoKeyPair adminKey, HashDigest ledgerHash, | |||
| BlockchainService blockchainService, LedgerRepository ledgerRepository) { | |||
| BlockchainService blockchainService, LedgerRepository ledgerRepository) { | |||
| // 注册用户,并验证最终写入; | |||
| BlockchainKeyPair user = BlockchainKeyGenerator.getInstance().generate(); | |||
| @@ -366,7 +380,6 @@ public class IntegrationTestAll4Redis { | |||
| return user; | |||
| } | |||
| public static LedgerInitProperties loadInitSetting_integration() { | |||
| ClassPathResource ledgerInitSettingResource = new ClassPathResource("ledger_init_test_integration.init"); | |||
| try (InputStream in = ledgerInitSettingResource.getInputStream()) { | |||
| @@ -378,12 +391,13 @@ public class IntegrationTestAll4Redis { | |||
| } | |||
| private LedgerBlock testSDK_Contract(CryptoKeyPair adminKey, HashDigest ledgerHash, | |||
| BlockchainService blockchainService,LedgerRepository ledgerRepository) { | |||
| System.out.println("adminKey="+AddressEncoding.generateAddress(adminKey.getPubKey())); | |||
| BlockchainService blockchainService, LedgerRepository ledgerRepository) { | |||
| System.out.println("adminKey=" + AddressEncoding.generateAddress(adminKey.getPubKey())); | |||
| BlockchainKeyPair userKey = BlockchainKeyGenerator.getInstance().generate(); | |||
| System.out.println("userKey="+userKey.getAddress()); | |||
| System.out.println("userKey=" + userKey.getAddress()); | |||
| // valid the basic data in contract; | |||
| // prepareContractData(adminKey, ledgerHash, blockchainService,ledgerRepository); | |||
| // prepareContractData(adminKey, ledgerHash, | |||
| // blockchainService,ledgerRepository); | |||
| TransactionTemplate txTpl = blockchainService.newTransaction(ledgerHash); | |||
| txTpl.users().register(userKey.getIdentity()); | |||
| @@ -391,12 +405,12 @@ public class IntegrationTestAll4Redis { | |||
| // 定义交易; | |||
| // 注册数据账户,并验证最终写入; | |||
| txTpl.dataAccounts().register(contractDataKey.getIdentity()); | |||
| // dataAccountSet.getDataAccount(dataAddress) | |||
| DataAccount dataAccount = ledgerRepository.getDataAccountSet(ledgerRepository.getLatestBlock()).getDataAccount(contractDataKey.getAddress()); | |||
| // dataAccountSet.getDataAccount(dataAddress) | |||
| DataAccount dataAccount = ledgerRepository.getDataAccountSet(ledgerRepository.getLatestBlock()) | |||
| .getDataAccount(contractDataKey.getAddress()); | |||
| DataAccountKVSetOperation kvsetOP = txTpl.dataAccount(contractDataKey.getAddress()) | |||
| .set("A", "Value_A_0".getBytes(), -1) | |||
| .set("B", "Value_B_0".getBytes(), -1) | |||
| .set("A", "Value_A_0".getBytes(), -1).set("B", "Value_B_0".getBytes(), -1) | |||
| .set(KEY_TOTAL, "total value,dataAccount".getBytes(), -1) | |||
| .set(KEY_ABC, "abc value,dataAccount".getBytes(), -1) | |||
| // 所有的模拟数据都在这个dataAccount中填充; | |||
| @@ -417,19 +431,19 @@ public class IntegrationTestAll4Redis { | |||
| txResp.getContentHash(); | |||
| LedgerBlock block = ledgerRepository.getBlock(txResp.getBlockHeight()); | |||
| byte[] contractCodeInDb = ledgerRepository.getContractAccountSet(block).getContract(contractDeployKey.getAddress()) | |||
| .getChainCode(); | |||
| byte[] contractCodeInDb = ledgerRepository.getContractAccountSet(block) | |||
| .getContract(contractDeployKey.getAddress()).getChainCode(); | |||
| assertArrayEquals(contractCode, contractCodeInDb); | |||
| txContentHash = ptx.getHash(); | |||
| // execute the contract; | |||
| testContractExe(adminKey, ledgerHash, userKey, blockchainService, ledgerRepository); | |||
| testContractExe(adminKey, ledgerHash, userKey, blockchainService, ledgerRepository); | |||
| return block; | |||
| } | |||
| private void testContractExe(CryptoKeyPair adminKey, HashDigest ledgerHash, BlockchainKeyPair userKey, | |||
| BlockchainService blockchainService,LedgerRepository ledgerRepository) { | |||
| BlockchainService blockchainService, LedgerRepository ledgerRepository) { | |||
| LedgerInfo ledgerInfo = blockchainService.getLedger(ledgerHash); | |||
| LedgerBlock previousBlock = blockchainService.getBlock(ledgerHash, ledgerInfo.getLatestBlockHeight() - 1); | |||
| @@ -438,8 +452,8 @@ public class IntegrationTestAll4Redis { | |||
| txTpl.contractEvents().send(contractDeployKey.getAddress(), eventName, | |||
| ("888##abc##" + contractDataKey.getAddress() + "##" + previousBlock.getHash().toBase58() + "##" | |||
| + userKey.getAddress() + "##" + contractDeployKey.getAddress() + "##" | |||
| + txContentHash.toBase58()+"##"+pubKeyVal).getBytes()); | |||
| + userKey.getAddress() + "##" + contractDeployKey.getAddress() + "##" + txContentHash.toBase58() | |||
| + "##" + pubKeyVal).getBytes()); | |||
| // 签名; | |||
| PreparedTransaction ptx = txTpl.prepare(); | |||
| @@ -460,25 +474,27 @@ public class IntegrationTestAll4Redis { | |||
| // 验证合约中的赋值,外部可以获得; | |||
| DataAccountSet dataAccountSet = ledgerRepository.getDataAccountSet(backgroundLedgerBlock); | |||
| PubKey pubKey = new PubKey(CryptoAlgorithm.ED25519, pubKeyVal.getBytes()); | |||
| CryptoKeyPair key = CryptoServiceProviders.getSignatureFunction("ED25519").generateKeyPair(); | |||
| PubKey pubKey = key.getPubKey(); | |||
| Bytes dataAddress = AddressEncoding.generateAddress(pubKey); | |||
| assertEquals(dataAddress, dataAccountSet.getDataAccount(dataAddress).getAddress()); | |||
| assertEquals("hello", new String(dataAccountSet.getDataAccount(dataAddress).getBytes(KEY_TOTAL, -1))); | |||
| // 验证userAccount,从合约内部赋值,然后外部验证;内部定义动态key,外部不便于得到,临时屏蔽; | |||
| // UserAccountSet userAccountSet = ledgerRepository.getUserAccountSet(backgroundLedgerBlock); | |||
| // PubKey userPubKey = new PubKey(CryptoAlgorithm.ED25519, userPubKeyVal.getBytes()); | |||
| // String userAddress = AddressEncoding.generateAddress(userPubKey); | |||
| // assertEquals(userAddress, userAccountSet.getUser(userAddress).getAddress()); | |||
| // UserAccountSet userAccountSet = | |||
| // ledgerRepository.getUserAccountSet(backgroundLedgerBlock); | |||
| // PubKey userPubKey = new PubKey(CryptoAlgorithm.ED25519, | |||
| // userPubKeyVal.getBytes()); | |||
| // String userAddress = AddressEncoding.generateAddress(userPubKey); | |||
| // assertEquals(userAddress, userAccountSet.getUser(userAddress).getAddress()); | |||
| } | |||
| private void prepareContractData(CryptoKeyPair adminKey, HashDigest ledgerHash, | |||
| BlockchainService blockchainService, LedgerRepository ledgerRepository) { | |||
| private void prepareContractData(CryptoKeyPair adminKey, HashDigest ledgerHash, BlockchainService blockchainService, | |||
| LedgerRepository ledgerRepository) { | |||
| // 定义交易; | |||
| TransactionTemplate txTpl = blockchainService.newTransaction(ledgerHash); | |||
| // 签名; | |||
| PreparedTransaction ptx = txTpl.prepare(); | |||
| ptx.sign(adminKey); | |||
| @@ -8,15 +8,16 @@ | |||
| */ | |||
| package test.com.jd.blockchain.intgr.batch.bftsmart; | |||
| import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
| import com.jd.blockchain.crypto.CryptoUtils; | |||
| import static com.jd.blockchain.tools.keygen.KeyGenCommand.encodePrivKey; | |||
| import static com.jd.blockchain.tools.keygen.KeyGenCommand.encodePubKey; | |||
| import org.junit.Test; | |||
| import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
| import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; | |||
| import com.jd.blockchain.crypto.asymmetric.SignatureFunction; | |||
| import com.jd.blockchain.utils.codec.Base58Utils; | |||
| import com.jd.blockchain.utils.security.ShaUtils; | |||
| import org.junit.Test; | |||
| import static com.jd.blockchain.tools.keygen.KeyGenCommand.encodePrivKey; | |||
| import static com.jd.blockchain.tools.keygen.KeyGenCommand.encodePubKey; | |||
| /** | |||
| * | |||
| @@ -35,8 +36,9 @@ public class BftsmartTestBase { | |||
| @Test | |||
| public void newUsers() { | |||
| SignatureFunction signFunc = CryptoServiceProviders.getSignatureFunction("ED25519"); | |||
| for (int i = 0; i < userSize; i++) { | |||
| CryptoKeyPair kp = CryptoUtils.sign(CryptoAlgorithm.ED25519).generateKeyPair(); | |||
| CryptoKeyPair kp = signFunc.generateKeyPair(); | |||
| String base58PubKey = encodePubKey(kp.getPubKey()); | |||
| @@ -19,6 +19,7 @@ import com.jd.blockchain.consensus.ConsensusProviders; | |||
| import com.jd.blockchain.consensus.ConsensusSettings; | |||
| import com.jd.blockchain.crypto.AddressEncoding; | |||
| import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
| import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
| import com.jd.blockchain.crypto.PrivKey; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; | |||
| @@ -255,7 +256,7 @@ public class LedgerInitializeTest { | |||
| CryptoConfig cryptoSetting = new CryptoConfig(); | |||
| cryptoSetting.setAutoVerifyHash(autoVerifyHash); | |||
| cryptoSetting.setHashAlgorithm(CryptoAlgorithm.SHA256); | |||
| cryptoSetting.setHashAlgorithm(CryptoServiceProviders.getAlgorithm("SHA256")); | |||
| partiKey = new CryptoKeyPair(setting.getConsensusParticipant(0).getPubKey(), privKey); | |||
| @@ -10,24 +10,17 @@ import java.io.InputStream; | |||
| import java.util.Properties; | |||
| import java.util.concurrent.CountDownLatch; | |||
| import com.jd.blockchain.storage.service.impl.composite.CompositeConnectionFactory; | |||
| import org.junit.Test; | |||
| import org.springframework.boot.SpringApplication; | |||
| import org.springframework.context.ConfigurableApplicationContext; | |||
| import org.springframework.core.io.ClassPathResource; | |||
| import com.jd.blockchain.binaryproto.BinaryEncodingUtils; | |||
| import com.jd.blockchain.consensus.ConsensusProvider; | |||
| import com.jd.blockchain.consensus.ConsensusProviders; | |||
| import com.jd.blockchain.consensus.ConsensusSettings; | |||
| import com.jd.blockchain.crypto.AddressEncoding; | |||
| import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
| import com.jd.blockchain.crypto.CryptoUtils; | |||
| import com.jd.blockchain.crypto.PrivKey; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.crypto.asymmetric.SignatureDigest; | |||
| import com.jd.blockchain.crypto.hash.HashDigest; | |||
| import com.jd.blockchain.ledger.LedgerBlock; | |||
| import com.jd.blockchain.ledger.LedgerInitOperation; | |||
| import com.jd.blockchain.ledger.Operation; | |||
| import com.jd.blockchain.ledger.TransactionContent; | |||
| @@ -35,11 +28,10 @@ import com.jd.blockchain.ledger.UserRegisterOperation; | |||
| import com.jd.blockchain.ledger.core.LedgerInitDecision; | |||
| import com.jd.blockchain.ledger.core.LedgerInitPermission; | |||
| import com.jd.blockchain.ledger.core.LedgerRepository; | |||
| import com.jd.blockchain.ledger.core.UserAccount; | |||
| import com.jd.blockchain.ledger.core.UserAccountSet; | |||
| import com.jd.blockchain.ledger.core.impl.LedgerManager; | |||
| import com.jd.blockchain.ledger.data.TxRequestBuilder; | |||
| import com.jd.blockchain.storage.service.DbConnection; | |||
| import com.jd.blockchain.storage.service.impl.composite.CompositeConnectionFactory; | |||
| import com.jd.blockchain.tools.initializer.DBConnectionConfig; | |||
| import com.jd.blockchain.tools.initializer.LedgerBindingConfig; | |||
| import com.jd.blockchain.tools.initializer.LedgerInitCommand; | |||
| @@ -50,7 +42,6 @@ import com.jd.blockchain.tools.initializer.web.HttpInitConsensServiceFactory; | |||
| import com.jd.blockchain.tools.initializer.web.LedgerInitConsensusService; | |||
| import com.jd.blockchain.tools.initializer.web.LedgerInitializeWebController; | |||
| import com.jd.blockchain.tools.keygen.KeyGenCommand; | |||
| import com.jd.blockchain.utils.Bytes; | |||
| import com.jd.blockchain.utils.concurrent.ThreadInvoker; | |||
| import com.jd.blockchain.utils.concurrent.ThreadInvoker.AsyncCallback; | |||
| import com.jd.blockchain.utils.io.BytesUtils; | |||
| @@ -4,6 +4,7 @@ import java.io.IOException; | |||
| import java.util.Random; | |||
| import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
| import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
| import com.jd.blockchain.crypto.hash.HashDigest; | |||
| import com.jd.blockchain.ledger.core.CryptoConfig; | |||
| import com.jd.blockchain.ledger.core.MerkleDataSet; | |||
| @@ -131,7 +132,7 @@ public class MerkleDatasetPerformanceTester { | |||
| Random rand = new Random(); | |||
| CryptoConfig cryptoConfig = new CryptoConfig(); | |||
| cryptoConfig.setHashAlgorithm(CryptoAlgorithm.SHA256); | |||
| cryptoConfig.setHashAlgorithm(CryptoServiceProviders.getAlgorithm("SHA256")); | |||
| cryptoConfig.setAutoVerifyHash(true); | |||
| // generate base data sample; | |||
| @@ -20,18 +20,19 @@ import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
| import com.jd.blockchain.consensus.ConsensusProvider; | |||
| import com.jd.blockchain.consensus.ConsensusSettings; | |||
| import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
| import com.jd.blockchain.crypto.CryptoUtils; | |||
| import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
| import com.jd.blockchain.crypto.PrivKey; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.crypto.asymmetric.SignatureDigest; | |||
| import com.jd.blockchain.crypto.asymmetric.SignatureFunction; | |||
| import com.jd.blockchain.crypto.hash.HashDigest; | |||
| import com.jd.blockchain.ledger.BlockchainIdentity; | |||
| import com.jd.blockchain.ledger.BlockchainIdentityData; | |||
| import com.jd.blockchain.ledger.ParticipantNode; | |||
| import com.jd.blockchain.ledger.CryptoSetting; | |||
| import com.jd.blockchain.ledger.LedgerBlock; | |||
| import com.jd.blockchain.ledger.LedgerInitSetting; | |||
| import com.jd.blockchain.ledger.Operation; | |||
| import com.jd.blockchain.ledger.ParticipantNode; | |||
| import com.jd.blockchain.ledger.TransactionBuilder; | |||
| import com.jd.blockchain.ledger.TransactionContent; | |||
| import com.jd.blockchain.ledger.TransactionRequest; | |||
| @@ -56,11 +57,11 @@ import com.jd.blockchain.tools.initializer.LedgerInitException; | |||
| import com.jd.blockchain.tools.initializer.LedgerInitProcess; | |||
| import com.jd.blockchain.tools.initializer.LedgerInitProperties; | |||
| import com.jd.blockchain.tools.initializer.LedgerInitProperties.ConsensusParticipantConfig; | |||
| import com.jd.blockchain.tools.initializer.Prompter; | |||
| import com.jd.blockchain.utils.Bytes; | |||
| import com.jd.blockchain.utils.concurrent.InvocationResult; | |||
| import com.jd.blockchain.utils.io.BytesUtils; | |||
| import com.jd.blockchain.utils.net.NetworkAddress; | |||
| import com.jd.blockchain.tools.initializer.Prompter; | |||
| /** | |||
| * 账本初始化控制器; | |||
| @@ -75,7 +76,9 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI | |||
| DataContractRegistry.register(TransactionRequest.class); | |||
| } | |||
| private static final CryptoAlgorithm SIGN_ALG = CryptoAlgorithm.ED25519; | |||
| private static final String DEFAULT_SIGN_ALGORITHM = "ED25519"; | |||
| private final SignatureFunction SIGN_FUNC; | |||
| private volatile LedgerInitPermission localPermission; | |||
| @@ -113,10 +116,13 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI | |||
| private InitConsensusServiceFactory initCsServiceFactory; | |||
| public LedgerInitializeWebController() { | |||
| this.SIGN_FUNC = CryptoServiceProviders.getSignatureFunction(DEFAULT_SIGN_ALGORITHM); | |||
| } | |||
| public LedgerInitializeWebController(LedgerManage ledgerManager, DbConnectionFactory dbConnFactory, | |||
| InitConsensusServiceFactory initCsServiceFactory) { | |||
| this.SIGN_FUNC = CryptoServiceProviders.getSignatureFunction(DEFAULT_SIGN_ALGORITHM); | |||
| this.ledgerManager = ledgerManager; | |||
| this.dbConnFactory = dbConnFactory; | |||
| this.initCsServiceFactory = initCsServiceFactory; | |||
| @@ -295,7 +301,7 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI | |||
| public CryptoSetting createDefaultCryptoSetting() { | |||
| CryptoConfig defCryptoSetting = new CryptoConfig(); | |||
| defCryptoSetting.setAutoVerifyHash(true); | |||
| defCryptoSetting.setHashAlgorithm(CryptoAlgorithm.SHA256); | |||
| defCryptoSetting.setHashAlgorithm(CryptoServiceProviders.getAlgorithm("SHA256")); | |||
| return defCryptoSetting; | |||
| } | |||
| @@ -335,9 +341,9 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI | |||
| // 校验当前的公钥、私钥是否匹配; | |||
| byte[] testBytes = BytesUtils.toBytes(currentId); | |||
| SignatureDigest testSign = CryptoUtils.sign(SIGN_ALG).sign(privKey, testBytes); | |||
| SignatureDigest testSign = SIGN_FUNC.sign(privKey, testBytes); | |||
| PubKey myPubKey = orderedParties[currentId].getPubKey(); | |||
| if (!CryptoUtils.sign(SIGN_ALG).verify(testSign, myPubKey, testBytes)) { | |||
| if (!SIGN_FUNC.verify(testSign, myPubKey, testBytes)) { | |||
| throw new LedgerInitException("Your pub-key specified in the init-settings isn't match your priv-key!"); | |||
| } | |||
| this.initializerAddresses = new NetworkAddress[orderedParties.length]; | |||
| @@ -370,7 +376,8 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI | |||
| private LedgerInitDecision makeDecision(int participantId, HashDigest ledgerHash, PrivKey privKey) { | |||
| byte[] dataBytes = getDecisionBytes(participantId, ledgerHash); | |||
| SignatureDigest signature = CryptoUtils.sign(privKey.getAlgorithm()).sign(privKey, dataBytes); | |||
| SignatureFunction signFunc = CryptoServiceProviders.getSignatureFunction(privKey.getAlgorithm()); | |||
| SignatureDigest signature = signFunc.sign(privKey, dataBytes); | |||
| LedgerInitDecisionData decision = new LedgerInitDecisionData(); | |||
| decision.setParticipantId(participantId); | |||
| @@ -496,7 +503,7 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI | |||
| public SignatureDigest signPermissionRequest(int requesterId, PrivKey privKey) { | |||
| byte[] reqAuthBytes = BytesUtils.concat(BytesUtils.toBytes(requesterId), ledgerInitSetting.getLedgerSeed()); | |||
| SignatureDigest reqAuthSign = CryptoUtils.sign(SIGN_ALG).sign(privKey, reqAuthBytes); | |||
| SignatureDigest reqAuthSign = SIGN_FUNC.sign(privKey, reqAuthBytes); | |||
| return reqAuthSign; | |||
| } | |||
| @@ -561,7 +568,7 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI | |||
| } | |||
| byte[] requestCodeBytes = BytesUtils.concat(BytesUtils.toBytes(requesterId), ledgerInitSetting.getLedgerSeed()); | |||
| PubKey requesterPubKey = participants[requesterId].getPubKey(); | |||
| if (!CryptoUtils.sign(SIGN_ALG).verify(signature, requesterPubKey, requestCodeBytes)) { | |||
| if (!SIGN_FUNC.verify(signature, requesterPubKey, requestCodeBytes)) { | |||
| throw new LedgerInitException("The requester signature is invalid!"); | |||
| } | |||
| return localPermission; | |||
| @@ -700,7 +707,7 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI | |||
| PubKey targetPubKey = ledgerInitSetting.getConsensusParticipants()[targetDecision.getParticipantId()] | |||
| .getPubKey(); | |||
| byte[] deciBytes = getDecisionBytes(targetDecision.getParticipantId(), targetDecision.getLedgerHash()); | |||
| if ((!CryptoUtils.sign(hashAlgorithm).verify(targetDecision.getSignature(), targetPubKey, deciBytes)) | |||
| if ((!SIGN_FUNC.verify(targetDecision.getSignature(), targetPubKey, deciBytes)) | |||
| && resultHandle.getResult() == null) { | |||
| prompter.error("The signature of received decision is invalid! --[Id=%s]", | |||
| targetDecision.getParticipantId()); | |||
| @@ -8,15 +8,14 @@ import java.util.List; | |||
| import javax.crypto.SecretKey; | |||
| import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
| import com.jd.blockchain.crypto.CryptoUtils; | |||
| import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
| import com.jd.blockchain.crypto.PrivKey; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; | |||
| import com.jd.blockchain.utils.ArgumentSet; | |||
| import com.jd.blockchain.utils.ConsoleUtils; | |||
| import com.jd.blockchain.utils.ArgumentSet.ArgEntry; | |||
| import com.jd.blockchain.utils.ArgumentSet.Setting; | |||
| import com.jd.blockchain.utils.ConsoleUtils; | |||
| import com.jd.blockchain.utils.codec.Base58Utils; | |||
| import com.jd.blockchain.utils.io.BytesUtils; | |||
| import com.jd.blockchain.utils.io.FileUtils; | |||
| @@ -105,7 +104,7 @@ public class KeyGenCommand { | |||
| * @param outputDir | |||
| */ | |||
| private static void generateKeyPair(String name, String outputDir, String localConfPath) { | |||
| CryptoKeyPair kp = CryptoUtils.sign(CryptoAlgorithm.ED25519).generateKeyPair(); | |||
| CryptoKeyPair kp = CryptoServiceProviders.getSignatureFunction("ED25519").generateKeyPair(); | |||
| String base58PubKey = encodePubKey(kp.getPubKey()); | |||