From 157f841a7a5d45cdc11de90808d24c5e9d070473 Mon Sep 17 00:00:00 2001 From: huanghaiquan Date: Thu, 20 Jun 2019 18:40:01 +0800 Subject: [PATCH] Fixed the error caused by missing created-time property in ledger.init file; --- .../src/test/resources/ledger_init_test.init | 3 + .../resources/ledger_init_test_integration.init | 3 + .../src/test/resources/ledger_init_test_web1.init | 3 + .../src/test/resources/ledger_init_test_web2.init | 3 + source/tools/tools-initializer/local.conf | 25 - source/tools/tools-initializer/mq.config | 6 - source/tools/tools-initializer/system.config | 121 --- .../jd/blockchain/mocker/MockerNodeContext.java | 826 +++++++++++---------- 8 files changed, 430 insertions(+), 560 deletions(-) delete mode 100644 source/tools/tools-initializer/local.conf delete mode 100644 source/tools/tools-initializer/mq.config delete mode 100644 source/tools/tools-initializer/system.config diff --git a/source/test/test-integration/src/test/resources/ledger_init_test.init b/source/test/test-integration/src/test/resources/ledger_init_test.init index 518d88c1..a1dde11f 100644 --- a/source/test/test-integration/src/test/resources/ledger_init_test.init +++ b/source/test/test-integration/src/test/resources/ledger_init_test.init @@ -5,6 +5,9 @@ ledger.seed=932dfe23-fe23232f-283f32fa-dd32aa76-8322ca2f-56236cda-7136b322-cb323 #账本的描述名称;此属性不参与共识,仅仅在当前参与方的本地节点用于描述用途; ledger.name= +#声明的账本创建时间;格式为 “yyyy-MM-dd HH:mm:ss.SSSZ”,表示”年-月-日 时:分:秒:毫秒时区“;例如:“2019-08-01 14:26:58.069+0800”,其中,+0800 表示时区是东8区 +created-time=2019-08-01 14:26:58.069+0800 + #共识服务提供者;必须; consensus.service-provider=com.jd.blockchain.consensus.bftsmart.BftsmartConsensusProvider diff --git a/source/test/test-integration/src/test/resources/ledger_init_test_integration.init b/source/test/test-integration/src/test/resources/ledger_init_test_integration.init index 6783d588..24499052 100644 --- a/source/test/test-integration/src/test/resources/ledger_init_test_integration.init +++ b/source/test/test-integration/src/test/resources/ledger_init_test_integration.init @@ -5,6 +5,9 @@ ledger.seed=932dfe23-fe23232f-283f32fa-dd32aa76-8322ca2f-56236cda-7136b322-cb323 #账本的描述名称;此属性不参与共识,仅仅在当前参与方的本地节点用于描述用途; ledger.name= +#声明的账本创建时间;格式为 “yyyy-MM-dd HH:mm:ss.SSSZ”,表示”年-月-日 时:分:秒:毫秒时区“;例如:“2019-08-01 14:26:58.069+0800”,其中,+0800 表示时区是东8区 +created-time=2019-08-01 14:26:58.069+0800 + #参与方的个数,后续以 cons_parti.id 分别标识每一个参与方的配置; cons_parti.count=4 diff --git a/source/test/test-integration/src/test/resources/ledger_init_test_web1.init b/source/test/test-integration/src/test/resources/ledger_init_test_web1.init index 111a5912..09274151 100644 --- a/source/test/test-integration/src/test/resources/ledger_init_test_web1.init +++ b/source/test/test-integration/src/test/resources/ledger_init_test_web1.init @@ -5,6 +5,9 @@ ledger.seed=932dfe23-fe23232f-283f32fa-dd32aa76-8322ca2f-56236cda-7136b322-cb323 #账本的描述名称;此属性不参与共识,仅仅在当前参与方的本地节点用于描述用途; ledger.name= +#声明的账本创建时间;格式为 “yyyy-MM-dd HH:mm:ss.SSSZ”,表示”年-月-日 时:分:秒:毫秒时区“;例如:“2019-08-01 14:26:58.069+0800”,其中,+0800 表示时区是东8区 +created-time=2019-08-01 14:26:58.069+0800 + #参与方的个数,后续以 cons_parti.id 分别标识每一个参与方的配置; cons_parti.count=4 diff --git a/source/test/test-integration/src/test/resources/ledger_init_test_web2.init b/source/test/test-integration/src/test/resources/ledger_init_test_web2.init index 5d64bdb6..90dbedc1 100644 --- a/source/test/test-integration/src/test/resources/ledger_init_test_web2.init +++ b/source/test/test-integration/src/test/resources/ledger_init_test_web2.init @@ -4,6 +4,9 @@ ledger.seed=932dfe23-fe23232f-283f32fa-dd32aa76-8322ca2f-56236cda-7136b322-cb323 #账本的描述名称;此属性不参与共识,仅仅在当前参与方的本地节点用于描述用途; #ledger.name= +#声明的账本创建时间;格式为 “yyyy-MM-dd HH:mm:ss.SSSZ”,表示”年-月-日 时:分:秒:毫秒时区“;例如:“2019-08-01 14:26:58.069+0800”,其中,+0800 表示时区是东8区 +created-time=2019-08-01 14:26:58.069+0800 + #共识服务提供者;必须; consensus.service-provider=com.jd.blockchain.consensus.bftsmart.BftsmartConsensusProvider diff --git a/source/tools/tools-initializer/local.conf b/source/tools/tools-initializer/local.conf deleted file mode 100644 index 9df0303c..00000000 --- a/source/tools/tools-initializer/local.conf +++ /dev/null @@ -1,25 +0,0 @@ - -#当前参与方的公钥; -local.parti.pubkey=endPsK36sC5JdPCDPDAXUwZtS3sxEmqEhFcC4whayAsTTh8Z6eoZ - -#当前参与方的私钥(密文编码); -local.parti.privkey=177gjsj5PHeCpbAtJE7qnbmhuZMHAEKuMsd45zHkv8F8AWBvTBbff8yRKdCyT3kwrmAjSnY - -#当前参与方的私钥解密密钥(原始口令的一次哈希,Base58格式),如果不设置,则启动过程中需要从控制台输入; -local.parti.pwd=DYu3G8aGTMBW1WrTw76zxQJQU4DHLw9MLyy7peG4LKkY - -#账本初始化完成后生成的"账本绑定配置文件"的输出目录; -ledger.binding.out=./ - -#账本数据库的连接字符串; -ledger.db.uri=redis://127.0.0.1/0 - -#账本数据库的连接口令; -ledger.db.pwd= - -#共识协议的参数配置;必须参数; -consensus.conf=mq.config - -#共识协议的实现类型;必须参数; -consensus.service-provider=com.jd.blockchain.consensus.mq.MsgQueueConsensusProvider - diff --git a/source/tools/tools-initializer/mq.config b/source/tools/tools-initializer/mq.config deleted file mode 100644 index 9795a9de..00000000 --- a/source/tools/tools-initializer/mq.config +++ /dev/null @@ -1,6 +0,0 @@ -system.msg.queue.server=nats://127.0.0.1:4222 -system.msg.queue.topic.tx=tx-topic -system.msg.queue.topic.bl=bl-topic -system.msg.queue.topic.msg=msg-topic -system.msg.queue.block.txsize=1000 -system.msg.queue.block.maxdelay=2000 \ No newline at end of file diff --git a/source/tools/tools-initializer/system.config b/source/tools/tools-initializer/system.config deleted file mode 100644 index ada42b49..00000000 --- a/source/tools/tools-initializer/system.config +++ /dev/null @@ -1,121 +0,0 @@ -# Copyright (c) 2007-2013 Alysson Bessani, Eduardo Alchieri, Paulo Sousa, and the authors indicated in the @author tags -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -############################################ -####### Communication Configurations ####### -############################################ - -#HMAC algorithm used to authenticate messages between processes (HmacMD5 is the default value) -#This parameter is not currently being used being used -#system.authentication.hmacAlgorithm = HmacSHA1 - -#Specify if the communication system should use a thread to send data (true or false) -system.communication.useSenderThread = true - -#Force all processes to use the same public/private keys pair and secret key. This is useful when deploying experiments -#and benchmarks, but must not be used in production systems. -system.communication.defaultkeys = true - -############################################ -### Replication Algorithm Configurations ### -############################################ - -#Number of servers in the group -system.servers.num = 4 - -#Maximum number of faulty replicas -system.servers.f = 1 - -#Timeout to asking for a client request -system.totalordermulticast.timeout = 2000 - - -#Maximum batch size (in number of messages) -system.totalordermulticast.maxbatchsize = 400 - -#Number of nonces (for non-determinism actions) generated -system.totalordermulticast.nonces = 10 - -#if verification of leader-generated timestamps are increasing -#it can only be used on systems in which the network clocks -#are synchronized -system.totalordermulticast.verifyTimestamps = false - -#Quantity of messages that can be stored in the receive queue of the communication system -system.communication.inQueueSize = 500000 - -# Quantity of messages that can be stored in the send queue of each replica -system.communication.outQueueSize = 500000 - -#Set to 1 if SMaRt should use signatures, set to 0 if otherwise -system.communication.useSignatures = 0 - -#Set to 1 if SMaRt should use MAC's, set to 0 if otherwise -system.communication.useMACs = 1 - -#Set to 1 if SMaRt should use the standard output to display debug messages, set to 0 if otherwise -system.debug = 0 - -#Print information about the replica when it is shutdown -system.shutdownhook = true - -############################################ -###### State Transfer Configurations ####### -############################################ - -#Activate the state transfer protocol ('true' to activate, 'false' to de-activate) -system.totalordermulticast.state_transfer = true - -#Maximum ahead-of-time message not discarded -system.totalordermulticast.highMark = 10000 - -#Maximum ahead-of-time message not discarded when the replica is still on EID 0 (after which the state transfer is triggered) -system.totalordermulticast.revival_highMark = 10 - -#Number of ahead-of-time messages necessary to trigger the state transfer after a request timeout occurs -system.totalordermulticast.timeout_highMark = 200 - -############################################ -###### Log and Checkpoint Configurations ### -############################################ - -system.totalordermulticast.log = true -system.totalordermulticast.log_parallel = false -system.totalordermulticast.log_to_disk = false -system.totalordermulticast.sync_log = false - -#Period at which BFT-SMaRt requests the state to the application (for the state transfer state protocol) -system.totalordermulticast.checkpoint_period = 1000 -system.totalordermulticast.global_checkpoint_period = 120000 - -system.totalordermulticast.checkpoint_to_disk = false -system.totalordermulticast.sync_ckp = false - - -############################################ -###### Reconfiguration Configurations ###### -############################################ - -#Replicas ID for the initial view, separated by a comma. -# The number of replicas in this parameter should be equal to that specified in 'system.servers.num' -system.initial.view = 0,1,2,3 - -#The ID of the trust third party (TTP) -system.ttp.id = 7002 - -#This sets if the system will function in Byzantine or crash-only mode. Set to "true" to support Byzantine faults -system.bft = true - -#Custom View Storage; -#view.storage.handler=bftsmart.reconfiguration.views.DefaultViewStorage diff --git a/source/tools/tools-mocker/src/main/java/com/jd/blockchain/mocker/MockerNodeContext.java b/source/tools/tools-mocker/src/main/java/com/jd/blockchain/mocker/MockerNodeContext.java index d7daceaf..f852563f 100644 --- a/source/tools/tools-mocker/src/main/java/com/jd/blockchain/mocker/MockerNodeContext.java +++ b/source/tools/tools-mocker/src/main/java/com/jd/blockchain/mocker/MockerNodeContext.java @@ -2,6 +2,8 @@ package com.jd.blockchain.mocker; import static java.lang.reflect.Proxy.newProxyInstance; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.Properties; @@ -46,424 +48,432 @@ import com.jd.blockchain.web.serializes.ByteArrayObjectUtil; public class MockerNodeContext implements BlockchainQueryService { - private static final String[] SUPPORTED_PROVIDERS = { ClassicCryptoService.class.getName(), - SMCryptoService.class.getName() }; + private static final String[] SUPPORTED_PROVIDERS = { ClassicCryptoService.class.getName(), + SMCryptoService.class.getName() }; + + private static final DBConnectionConfig dbConnectionConfig = new DBConnectionConfig("memory://local/0"); - private static final DBConnectionConfig dbConnectionConfig = new DBConnectionConfig("memory://local/0"); + private DbConnectionFactory dbConnFactory = new MemoryDBConnFactory(); - private DbConnectionFactory dbConnFactory = new MemoryDBConnFactory(); + private MockerOperationHandleRegister opHandler = new MockerOperationHandleRegister(); - private MockerOperationHandleRegister opHandler = new MockerOperationHandleRegister(); + private MockerContractExeHandle contractExeHandle = new MockerContractExeHandle(); - private MockerContractExeHandle contractExeHandle = new MockerContractExeHandle(); + private Map participants = new HashMap<>(); - private Map participants = new HashMap<>(); + private LedgerManager ledgerManager = new LedgerManager(); - private LedgerManager ledgerManager = new LedgerManager(); + private BlockchainKeypair defaultKeypair; - private BlockchainKeypair defaultKeypair; + private LedgerRepository ledgerRepository; - private LedgerRepository ledgerRepository; + private LedgerQueryService queryService; - private LedgerQueryService queryService; + private HashDigest ledgerHash; - private HashDigest ledgerHash; + private String ledgerSeed; - private String ledgerSeed; + static { + DataContractRegistry.register(TransactionContent.class); + DataContractRegistry.register(TransactionContentBody.class); + DataContractRegistry.register(TransactionRequest.class); + DataContractRegistry.register(NodeRequest.class); + DataContractRegistry.register(EndpointRequest.class); + DataContractRegistry.register(TransactionResponse.class); - static { - DataContractRegistry.register(TransactionContent.class); - DataContractRegistry.register(TransactionContentBody.class); - DataContractRegistry.register(TransactionRequest.class); - DataContractRegistry.register(NodeRequest.class); - DataContractRegistry.register(EndpointRequest.class); - DataContractRegistry.register(TransactionResponse.class); - - DataContractRegistry.register(Operation.class); - DataContractRegistry.register(ContractCodeDeployOperation.class); - DataContractRegistry.register(ContractEventSendOperation.class); - DataContractRegistry.register(DataAccountRegisterOperation.class); - DataContractRegistry.register(UserRegisterOperation.class); - DataContractRegistry.register(DataAccountKVSetOperation.class); - DataContractRegistry.register(DataAccountKVSetOperation.KVWriteEntry.class); - - DataContractRegistry.register(ActionRequest.class); - DataContractRegistry.register(ActionResponse.class); - DataContractRegistry.register(ClientIdentifications.class); - DataContractRegistry.register(ClientIdentification.class); - - ByteArrayObjectUtil.init(); - } - - public MockerNodeContext() { - } - - public MockerNodeContext(String ledgerSeed) { - this.ledgerSeed = ledgerSeed; - } - - public MockerNodeContext participants(String name, BlockchainKeypair partiKey) { - participants.put(name, partiKey); - return this; - } - - public MockerNodeContext participants(BlockchainKeypair partiKey) { - participants.put(partiKey.getPubKey().toBase58(), partiKey); - return this; - } - - public MockerNodeContext build() { - if (ledgerSeed == null || ledgerSeed.length() == 0) { - ledgerSeed = MockerConstant.DEFAULT_LEDGER_SEED; - } - if (participants.size() < 4) { - // 缺少的需要补充,使用常量中的内容进行补充 - for (int i = 0; i < MockerConstant.PUBLIC_KEYS.length; i++) { - String pubKeyString = MockerConstant.PUBLIC_KEYS[i]; - boolean isExist = false; - // 通过公钥进行判断 - for (Map.Entry entry : participants.entrySet()) { - String existPubKey = KeyGenCommand.encodePubKey(entry.getValue().getPubKey()); - if (pubKeyString.equals(existPubKey)) { - isExist = true; - } - } - if (!isExist) { - // 加入系统中 - PrivKey privKey = KeyGenCommand.decodePrivKeyWithRawPassword(MockerConstant.PRIVATE_KEYS[i], MockerConstant.PASSWORD); - PubKey pubKey = KeyGenCommand.decodePubKey(MockerConstant.PUBLIC_KEYS[i]); - participants(new BlockchainKeypair(pubKey, privKey)); - } - if (participants.size() >= 4) { - break; - } - } - } - - LedgerInitProperties ledgerInitProperties = loadInitSetting(); - - MockerLedgerInitializer mockLedgerInitializer = new MockerLedgerInitializer(dbConnFactory, ledgerManager); - - ledgerHash = mockLedgerInitializer.initialize(0, defaultKeypair.getPrivKey(), - ledgerInitProperties, dbConnectionConfig, new PresetAnswerPrompter("N"), cryptoConfig()); - - ledgerRepository = registerLedger(ledgerHash, dbConnectionConfig); - - queryService = new LedgerQueryService(ledgerManager); - - contractExeHandle.initLedger(ledgerManager, ledgerHash); - - opHandler.registerHandler(contractExeHandle); - - return this; - } - - public String registerUser(BlockchainKeypair user) { - TxBuilder txBuilder = txBuilder(); - txBuilder.users().register(user.getIdentity()); - txProcess(txRequest(txBuilder)); - return user.getAddress().toBase58(); - } - - public String registerDataAccount(BlockchainKeypair dataAccount) { - TxBuilder txBuilder = txBuilder(); - txBuilder.dataAccounts().register(dataAccount.getIdentity()); - txProcess(txRequest(txBuilder)); - return dataAccount.getAddress().toBase58(); - } - - public String registerDataAccount() { - return registerDataAccount(BlockchainKeyGenerator.getInstance().generate()); - } - - public void writeKv(String address, String key, String value, long version) { - TxBuilder txBuilder = txBuilder(); - txBuilder.dataAccount(address).setText(key, value, version); - txProcess(txRequest(txBuilder)); - } - - public void writeKv(String address, String key, long value, long version) { - TxBuilder txBuilder = txBuilder(); - txBuilder.dataAccount(address).setInt64(key, value, version); - txProcess(txRequest(txBuilder)); - } - - public void writeKv(String address, String key, byte[] value, long version) { - TxBuilder txBuilder = txBuilder(); - txBuilder.dataAccount(address).setBytes(key, value, version); - txProcess(txRequest(txBuilder)); - } - - public void writeKv(String address, String key, Bytes value, long version) { - TxBuilder txBuilder = txBuilder(); - txBuilder.dataAccount(address).setBytes(key, value, version); - txProcess(txRequest(txBuilder)); - } - - public T deployContract(T contract) { - // 首先发布合约 - BlockchainIdentity identity = deployContract2Ledger(contract); - // 生成代理对象 - ContractProxy contractProxy = new ContractProxy<>(identity, this, - contract, contractExeHandle); - - T proxy = (T) newProxyInstance(contract.getClass().getClassLoader(), - contract.getClass().getInterfaces(), contractProxy); - - return proxy; - } - - private BlockchainIdentity deployContract2Ledger(Object contract) { - BlockchainIdentity contractIdentity = BlockchainKeyGenerator.getInstance().generate().getIdentity(); - // 合约发布 - // 注意此处只是将其放入内存中,而不需要真正编译为字节码 - TxBuilder txBuilder = txBuilder(); - txBuilder.contracts().deploy(contractIdentity, BytesUtils.toBytes(contract.getClass().getName())); - // 执行 - txProcess(txRequest(txBuilder)); - return contractIdentity; - } - - public String getLedgerSeed() { - return ledgerSeed; - } - - public HashDigest getLedgerHash() { - return ledgerHash; - } - - @Override - public HashDigest[] getLedgerHashs() { - return queryService.getLedgerHashs(); - } - - @Override - public LedgerInfo getLedger(HashDigest ledgerHash) { - return queryService.getLedger(ledgerHash); - } - - @Override - public ParticipantNode[] getConsensusParticipants(HashDigest ledgerHash) { - return queryService.getConsensusParticipants(ledgerHash); - } - - @Override - public LedgerMetadata getLedgerMetadata(HashDigest ledgerHash) { - return queryService.getLedgerMetadata(ledgerHash); - } - - @Override - public LedgerBlock getBlock(HashDigest ledgerHash, long height) { - return queryService.getBlock(ledgerHash, height); - } - - @Override - public LedgerBlock getBlock(HashDigest ledgerHash, HashDigest blockHash) { - return queryService.getBlock(ledgerHash, blockHash); - } - - @Override - public long getTransactionCount(HashDigest ledgerHash, long height) { - return queryService.getTransactionCount(ledgerHash, height); - } - - @Override - public long getTransactionCount(HashDigest ledgerHash, HashDigest blockHash) { - return queryService.getTransactionCount(ledgerHash, blockHash); - } - - @Override - public long getTransactionTotalCount(HashDigest ledgerHash) { - return queryService.getTransactionTotalCount(ledgerHash); - } - - @Override - public long getDataAccountCount(HashDigest ledgerHash, long height) { - return queryService.getDataAccountCount(ledgerHash, height); - } - - @Override - public long getDataAccountCount(HashDigest ledgerHash, HashDigest blockHash) { - return queryService.getDataAccountCount(ledgerHash, blockHash); - } - - @Override - public long getDataAccountTotalCount(HashDigest ledgerHash) { - return queryService.getDataAccountTotalCount(ledgerHash); - } - - @Override - public long getUserCount(HashDigest ledgerHash, long height) { - return queryService.getUserCount(ledgerHash, height); - } - - @Override - public long getUserCount(HashDigest ledgerHash, HashDigest blockHash) { - return queryService.getUserCount(ledgerHash, blockHash); - } - - @Override - public long getUserTotalCount(HashDigest ledgerHash) { - return queryService.getUserTotalCount(ledgerHash); - } - - @Override - public long getContractCount(HashDigest ledgerHash, long height) { - return queryService.getContractCount(ledgerHash, height); - } - - @Override - public long getContractCount(HashDigest ledgerHash, HashDigest blockHash) { - return queryService.getContractCount(ledgerHash, blockHash); - } - - @Override - public long getContractTotalCount(HashDigest ledgerHash) { - return queryService.getContractTotalCount(ledgerHash); - } - - @Override - public LedgerTransaction[] getTransactions(HashDigest ledgerHash, long height, int fromIndex, int count) { - return queryService.getTransactions(ledgerHash, height, fromIndex, count); - } - - @Override - public LedgerTransaction[] getTransactions(HashDigest ledgerHash, HashDigest blockHash, int fromIndex, int count) { - return queryService.getTransactions(ledgerHash, blockHash, fromIndex, count); - } - - @Override - public LedgerTransaction getTransactionByContentHash(HashDigest ledgerHash, HashDigest contentHash) { - return queryService.getTransactionByContentHash(ledgerHash, contentHash); - } - - @Override - public TransactionState getTransactionStateByContentHash(HashDigest ledgerHash, HashDigest contentHash) { - return queryService.getTransactionStateByContentHash(ledgerHash, contentHash); - } - - @Override - public UserInfo getUser(HashDigest ledgerHash, String address) { - return queryService.getUser(ledgerHash, address); - } - - @Override - public AccountHeader getDataAccount(HashDigest ledgerHash, String address) { - return queryService.getDataAccount(ledgerHash, address); - } - - @Override - public KVDataEntry[] getDataEntries(HashDigest ledgerHash, String address, String... keys) { - return queryService.getDataEntries(ledgerHash, address, keys); - } - - @Override - public KVDataEntry[] getDataEntries(HashDigest ledgerHash, String address, KVInfoVO kvInfoVO) { - return queryService.getDataEntries(ledgerHash, address, kvInfoVO); - } - - @Override - public long getDataEntriesTotalCount(HashDigest ledgerHash, String address) { - return queryService.getDataEntriesTotalCount(ledgerHash, address); - } - - @Override - public KVDataEntry[] getDataEntries(HashDigest ledgerHash, String address, int fromIndex, int count) { - return queryService.getDataEntries(ledgerHash, address, fromIndex, count); - } - - @Override - public AccountHeader getContract(HashDigest ledgerHash, String address) { - return queryService.getContract(ledgerHash, address); - } - - @Override - public AccountHeader[] getUsers(HashDigest ledgerHash, int fromIndex, int count) { - return queryService.getUsers(ledgerHash, fromIndex, count); - } - - @Override - public AccountHeader[] getDataAccounts(HashDigest ledgerHash, int fromIndex, int count) { - return queryService.getDataAccounts(ledgerHash, fromIndex, count); - } - - @Override - public AccountHeader[] getContractAccounts(HashDigest ledgerHash, int fromIndex, int count) { - return queryService.getContractAccounts(ledgerHash, fromIndex, count); - } - - public TxBuilder txBuilder() { - return new TxBuilder(ledgerHash); - } - - public TransactionRequest txRequest(TxBuilder txBuilder) { - TransactionRequestBuilder reqBuilder = txBuilder.prepareRequest(); - reqBuilder.signAsEndpoint(defaultKeypair); - return reqBuilder.buildRequest(); - } - - public OperationResult[] txProcess(TransactionRequest txRequest) { - LedgerEditor newEditor = ledgerRepository.createNextBlock(); - LedgerBlock latestBlock = ledgerRepository.getLatestBlock(); - LedgerDataSet previousDataSet = ledgerRepository.getDataSet(latestBlock); - TransactionBatchProcessor txProc = new TransactionBatchProcessor(newEditor, previousDataSet, opHandler, - ledgerManager); - TransactionResponse txResp = txProc.schedule(txRequest); - TransactionBatchResultHandle handle = txProc.prepare(); - handle.commit(); - return txResp.getOperationResults(); - } - - private LedgerRepository registerLedger(HashDigest ledgerHash, DBConnectionConfig dbConnConf) { - return ledgerManager.register(ledgerHash, dbConnFactory.connect(dbConnConf.getUri()).getStorageService()); - } - - private CryptoConfig cryptoConfig() { - CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; - for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { - supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); - } - CryptoConfig cryptoSetting = new CryptoConfig(); - cryptoSetting.setSupportedProviders(supportedProviders); - cryptoSetting.setAutoVerifyHash(false); - cryptoSetting.setHashAlgorithm(ClassicAlgorithm.SHA256); - return cryptoSetting; - } - - private LedgerInitProperties loadInitSetting() { - - Properties ledgerProp = new Properties(); - - ledgerProp.put(LedgerInitProperties.LEDGER_SEED, ledgerSeed); - - ledgerProp.put("ledger.name", MockerConstant.LEDGER_NAME); - - ledgerProp.put(LedgerInitProperties.CONSENSUS_SERVICE_PROVIDER, "com.jd.blockchain.consensus.bftsmart.BftsmartConsensusProvider"); - - ledgerProp.put(LedgerInitProperties.CONSENSUS_CONFIG, "classpath:bftsmart.config"); - - ledgerProp.put(LedgerInitProperties.CRYPTO_SERVICE_PROVIDERS, "com.jd.blockchain.crypto.service.classic.ClassicCryptoService," + - "com.jd.blockchain.crypto.service.sm.SMCryptoService"); - - ledgerProp.put(LedgerInitProperties.PART_COUNT, String.valueOf(participants.size())); - - int partiIndex = 0; - for (Map.Entry entry : participants.entrySet()) { - String name = entry.getKey(); - BlockchainKeypair keypair = entry.getValue(); - if (partiIndex == 0) { - defaultKeypair = keypair; - } - String partiPrefix = String.format(LedgerInitProperties.PART_ID_PATTERN, partiIndex) + "."; - ledgerProp.put(partiPrefix + LedgerInitProperties.PART_NAME, name); - ledgerProp.put(partiPrefix + LedgerInitProperties.PART_PUBKEY_PATH, ""); - ledgerProp.put(partiPrefix + LedgerInitProperties.PART_PUBKEY, KeyGenCommand.encodePubKey(keypair.getPubKey())); - ledgerProp.put(partiPrefix + LedgerInitProperties.PART_INITIALIZER_HOST, MockerConstant.LOCAL_ADDRESS); - ledgerProp.put(partiPrefix + LedgerInitProperties.PART_INITIALIZER_PORT, String.valueOf(MockerConstant.LEDGER_INIT_PORT_START + partiIndex * 10)); - ledgerProp.put(partiPrefix + LedgerInitProperties.PART_INITIALIZER_SECURE, String.valueOf(false)); - partiIndex++; - } - - return LedgerInitProperties.resolve(ledgerProp); - } + DataContractRegistry.register(Operation.class); + DataContractRegistry.register(ContractCodeDeployOperation.class); + DataContractRegistry.register(ContractEventSendOperation.class); + DataContractRegistry.register(DataAccountRegisterOperation.class); + DataContractRegistry.register(UserRegisterOperation.class); + DataContractRegistry.register(DataAccountKVSetOperation.class); + DataContractRegistry.register(DataAccountKVSetOperation.KVWriteEntry.class); + + DataContractRegistry.register(ActionRequest.class); + DataContractRegistry.register(ActionResponse.class); + DataContractRegistry.register(ClientIdentifications.class); + DataContractRegistry.register(ClientIdentification.class); + + ByteArrayObjectUtil.init(); + } + + public MockerNodeContext() { + } + + public MockerNodeContext(String ledgerSeed) { + this.ledgerSeed = ledgerSeed; + } + + public MockerNodeContext participants(String name, BlockchainKeypair partiKey) { + participants.put(name, partiKey); + return this; + } + + public MockerNodeContext participants(BlockchainKeypair partiKey) { + participants.put(partiKey.getPubKey().toBase58(), partiKey); + return this; + } + + public MockerNodeContext build() { + if (ledgerSeed == null || ledgerSeed.length() == 0) { + ledgerSeed = MockerConstant.DEFAULT_LEDGER_SEED; + } + if (participants.size() < 4) { + // 缺少的需要补充,使用常量中的内容进行补充 + for (int i = 0; i < MockerConstant.PUBLIC_KEYS.length; i++) { + String pubKeyString = MockerConstant.PUBLIC_KEYS[i]; + boolean isExist = false; + // 通过公钥进行判断 + for (Map.Entry entry : participants.entrySet()) { + String existPubKey = KeyGenCommand.encodePubKey(entry.getValue().getPubKey()); + if (pubKeyString.equals(existPubKey)) { + isExist = true; + } + } + if (!isExist) { + // 加入系统中 + PrivKey privKey = KeyGenCommand.decodePrivKeyWithRawPassword(MockerConstant.PRIVATE_KEYS[i], + MockerConstant.PASSWORD); + PubKey pubKey = KeyGenCommand.decodePubKey(MockerConstant.PUBLIC_KEYS[i]); + participants(new BlockchainKeypair(pubKey, privKey)); + } + if (participants.size() >= 4) { + break; + } + } + } + + LedgerInitProperties ledgerInitProperties = loadInitSetting(); + + MockerLedgerInitializer mockLedgerInitializer = new MockerLedgerInitializer(dbConnFactory, ledgerManager); + + ledgerHash = mockLedgerInitializer.initialize(0, defaultKeypair.getPrivKey(), ledgerInitProperties, + dbConnectionConfig, new PresetAnswerPrompter("N"), cryptoConfig()); + + ledgerRepository = registerLedger(ledgerHash, dbConnectionConfig); + + queryService = new LedgerQueryService(ledgerManager); + + contractExeHandle.initLedger(ledgerManager, ledgerHash); + + opHandler.registerHandler(contractExeHandle); + + return this; + } + + public String registerUser(BlockchainKeypair user) { + TxBuilder txBuilder = txBuilder(); + txBuilder.users().register(user.getIdentity()); + txProcess(txRequest(txBuilder)); + return user.getAddress().toBase58(); + } + + public String registerDataAccount(BlockchainKeypair dataAccount) { + TxBuilder txBuilder = txBuilder(); + txBuilder.dataAccounts().register(dataAccount.getIdentity()); + txProcess(txRequest(txBuilder)); + return dataAccount.getAddress().toBase58(); + } + + public String registerDataAccount() { + return registerDataAccount(BlockchainKeyGenerator.getInstance().generate()); + } + + public void writeKv(String address, String key, String value, long version) { + TxBuilder txBuilder = txBuilder(); + txBuilder.dataAccount(address).setText(key, value, version); + txProcess(txRequest(txBuilder)); + } + + public void writeKv(String address, String key, long value, long version) { + TxBuilder txBuilder = txBuilder(); + txBuilder.dataAccount(address).setInt64(key, value, version); + txProcess(txRequest(txBuilder)); + } + + public void writeKv(String address, String key, byte[] value, long version) { + TxBuilder txBuilder = txBuilder(); + txBuilder.dataAccount(address).setBytes(key, value, version); + txProcess(txRequest(txBuilder)); + } + + public void writeKv(String address, String key, Bytes value, long version) { + TxBuilder txBuilder = txBuilder(); + txBuilder.dataAccount(address).setBytes(key, value, version); + txProcess(txRequest(txBuilder)); + } + + public T deployContract(T contract) { + // 首先发布合约 + BlockchainIdentity identity = deployContract2Ledger(contract); + // 生成代理对象 + ContractProxy contractProxy = new ContractProxy<>(identity, this, contract, contractExeHandle); + + T proxy = (T) newProxyInstance(contract.getClass().getClassLoader(), contract.getClass().getInterfaces(), + contractProxy); + + return proxy; + } + + private BlockchainIdentity deployContract2Ledger(Object contract) { + BlockchainIdentity contractIdentity = BlockchainKeyGenerator.getInstance().generate().getIdentity(); + // 合约发布 + // 注意此处只是将其放入内存中,而不需要真正编译为字节码 + TxBuilder txBuilder = txBuilder(); + txBuilder.contracts().deploy(contractIdentity, BytesUtils.toBytes(contract.getClass().getName())); + // 执行 + txProcess(txRequest(txBuilder)); + return contractIdentity; + } + + public String getLedgerSeed() { + return ledgerSeed; + } + + public HashDigest getLedgerHash() { + return ledgerHash; + } + + @Override + public HashDigest[] getLedgerHashs() { + return queryService.getLedgerHashs(); + } + + @Override + public LedgerInfo getLedger(HashDigest ledgerHash) { + return queryService.getLedger(ledgerHash); + } + + @Override + public ParticipantNode[] getConsensusParticipants(HashDigest ledgerHash) { + return queryService.getConsensusParticipants(ledgerHash); + } + + @Override + public LedgerMetadata getLedgerMetadata(HashDigest ledgerHash) { + return queryService.getLedgerMetadata(ledgerHash); + } + + @Override + public LedgerBlock getBlock(HashDigest ledgerHash, long height) { + return queryService.getBlock(ledgerHash, height); + } + + @Override + public LedgerBlock getBlock(HashDigest ledgerHash, HashDigest blockHash) { + return queryService.getBlock(ledgerHash, blockHash); + } + + @Override + public long getTransactionCount(HashDigest ledgerHash, long height) { + return queryService.getTransactionCount(ledgerHash, height); + } + + @Override + public long getTransactionCount(HashDigest ledgerHash, HashDigest blockHash) { + return queryService.getTransactionCount(ledgerHash, blockHash); + } + + @Override + public long getTransactionTotalCount(HashDigest ledgerHash) { + return queryService.getTransactionTotalCount(ledgerHash); + } + + @Override + public long getDataAccountCount(HashDigest ledgerHash, long height) { + return queryService.getDataAccountCount(ledgerHash, height); + } + + @Override + public long getDataAccountCount(HashDigest ledgerHash, HashDigest blockHash) { + return queryService.getDataAccountCount(ledgerHash, blockHash); + } + + @Override + public long getDataAccountTotalCount(HashDigest ledgerHash) { + return queryService.getDataAccountTotalCount(ledgerHash); + } + + @Override + public long getUserCount(HashDigest ledgerHash, long height) { + return queryService.getUserCount(ledgerHash, height); + } + + @Override + public long getUserCount(HashDigest ledgerHash, HashDigest blockHash) { + return queryService.getUserCount(ledgerHash, blockHash); + } + + @Override + public long getUserTotalCount(HashDigest ledgerHash) { + return queryService.getUserTotalCount(ledgerHash); + } + + @Override + public long getContractCount(HashDigest ledgerHash, long height) { + return queryService.getContractCount(ledgerHash, height); + } + + @Override + public long getContractCount(HashDigest ledgerHash, HashDigest blockHash) { + return queryService.getContractCount(ledgerHash, blockHash); + } + + @Override + public long getContractTotalCount(HashDigest ledgerHash) { + return queryService.getContractTotalCount(ledgerHash); + } + + @Override + public LedgerTransaction[] getTransactions(HashDigest ledgerHash, long height, int fromIndex, int count) { + return queryService.getTransactions(ledgerHash, height, fromIndex, count); + } + + @Override + public LedgerTransaction[] getTransactions(HashDigest ledgerHash, HashDigest blockHash, int fromIndex, int count) { + return queryService.getTransactions(ledgerHash, blockHash, fromIndex, count); + } + + @Override + public LedgerTransaction getTransactionByContentHash(HashDigest ledgerHash, HashDigest contentHash) { + return queryService.getTransactionByContentHash(ledgerHash, contentHash); + } + + @Override + public TransactionState getTransactionStateByContentHash(HashDigest ledgerHash, HashDigest contentHash) { + return queryService.getTransactionStateByContentHash(ledgerHash, contentHash); + } + + @Override + public UserInfo getUser(HashDigest ledgerHash, String address) { + return queryService.getUser(ledgerHash, address); + } + + @Override + public AccountHeader getDataAccount(HashDigest ledgerHash, String address) { + return queryService.getDataAccount(ledgerHash, address); + } + + @Override + public KVDataEntry[] getDataEntries(HashDigest ledgerHash, String address, String... keys) { + return queryService.getDataEntries(ledgerHash, address, keys); + } + + @Override + public KVDataEntry[] getDataEntries(HashDigest ledgerHash, String address, KVInfoVO kvInfoVO) { + return queryService.getDataEntries(ledgerHash, address, kvInfoVO); + } + + @Override + public long getDataEntriesTotalCount(HashDigest ledgerHash, String address) { + return queryService.getDataEntriesTotalCount(ledgerHash, address); + } + + @Override + public KVDataEntry[] getDataEntries(HashDigest ledgerHash, String address, int fromIndex, int count) { + return queryService.getDataEntries(ledgerHash, address, fromIndex, count); + } + + @Override + public AccountHeader getContract(HashDigest ledgerHash, String address) { + return queryService.getContract(ledgerHash, address); + } + + @Override + public AccountHeader[] getUsers(HashDigest ledgerHash, int fromIndex, int count) { + return queryService.getUsers(ledgerHash, fromIndex, count); + } + + @Override + public AccountHeader[] getDataAccounts(HashDigest ledgerHash, int fromIndex, int count) { + return queryService.getDataAccounts(ledgerHash, fromIndex, count); + } + + @Override + public AccountHeader[] getContractAccounts(HashDigest ledgerHash, int fromIndex, int count) { + return queryService.getContractAccounts(ledgerHash, fromIndex, count); + } + + public TxBuilder txBuilder() { + return new TxBuilder(ledgerHash); + } + + public TransactionRequest txRequest(TxBuilder txBuilder) { + TransactionRequestBuilder reqBuilder = txBuilder.prepareRequest(); + reqBuilder.signAsEndpoint(defaultKeypair); + return reqBuilder.buildRequest(); + } + + public OperationResult[] txProcess(TransactionRequest txRequest) { + LedgerEditor newEditor = ledgerRepository.createNextBlock(); + LedgerBlock latestBlock = ledgerRepository.getLatestBlock(); + LedgerDataSet previousDataSet = ledgerRepository.getDataSet(latestBlock); + TransactionBatchProcessor txProc = new TransactionBatchProcessor(newEditor, previousDataSet, opHandler, + ledgerManager); + TransactionResponse txResp = txProc.schedule(txRequest); + TransactionBatchResultHandle handle = txProc.prepare(); + handle.commit(); + return txResp.getOperationResults(); + } + + private LedgerRepository registerLedger(HashDigest ledgerHash, DBConnectionConfig dbConnConf) { + return ledgerManager.register(ledgerHash, dbConnFactory.connect(dbConnConf.getUri()).getStorageService()); + } + + private CryptoConfig cryptoConfig() { + CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; + for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { + supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); + } + CryptoConfig cryptoSetting = new CryptoConfig(); + cryptoSetting.setSupportedProviders(supportedProviders); + cryptoSetting.setAutoVerifyHash(false); + cryptoSetting.setHashAlgorithm(ClassicAlgorithm.SHA256); + return cryptoSetting; + } + + private LedgerInitProperties loadInitSetting() { + + Properties ledgerProp = new Properties(); + + ledgerProp.put(LedgerInitProperties.LEDGER_SEED, ledgerSeed); + + Date now = new Date(); + SimpleDateFormat format = new SimpleDateFormat(LedgerInitProperties.CREATED_TIME_FORMAT); + ledgerProp.put(LedgerInitProperties.CREATED_TIME, format.format(now)); + + ledgerProp.put("ledger.name", MockerConstant.LEDGER_NAME); + + ledgerProp.put(LedgerInitProperties.CONSENSUS_SERVICE_PROVIDER, + "com.jd.blockchain.consensus.bftsmart.BftsmartConsensusProvider"); + + ledgerProp.put(LedgerInitProperties.CONSENSUS_CONFIG, "classpath:bftsmart.config"); + + ledgerProp.put(LedgerInitProperties.CRYPTO_SERVICE_PROVIDERS, + "com.jd.blockchain.crypto.service.classic.ClassicCryptoService," + + "com.jd.blockchain.crypto.service.sm.SMCryptoService"); + + ledgerProp.put(LedgerInitProperties.PART_COUNT, String.valueOf(participants.size())); + + int partiIndex = 0; + for (Map.Entry entry : participants.entrySet()) { + String name = entry.getKey(); + BlockchainKeypair keypair = entry.getValue(); + if (partiIndex == 0) { + defaultKeypair = keypair; + } + String partiPrefix = String.format(LedgerInitProperties.PART_ID_PATTERN, partiIndex) + "."; + ledgerProp.put(partiPrefix + LedgerInitProperties.PART_NAME, name); + ledgerProp.put(partiPrefix + LedgerInitProperties.PART_PUBKEY_PATH, ""); + ledgerProp.put(partiPrefix + LedgerInitProperties.PART_PUBKEY, + KeyGenCommand.encodePubKey(keypair.getPubKey())); + ledgerProp.put(partiPrefix + LedgerInitProperties.PART_INITIALIZER_HOST, MockerConstant.LOCAL_ADDRESS); + ledgerProp.put(partiPrefix + LedgerInitProperties.PART_INITIALIZER_PORT, + String.valueOf(MockerConstant.LEDGER_INIT_PORT_START + partiIndex * 10)); + ledgerProp.put(partiPrefix + LedgerInitProperties.PART_INITIALIZER_SECURE, String.valueOf(false)); + partiIndex++; + } + + return LedgerInitProperties.resolve(ledgerProp); + } }