| @@ -122,7 +122,9 @@ public class JavaContractCode implements ContractCode { | |||
| // 填充return结果 | |||
| if (this.contractReturn != null) { | |||
| this.contractReturn.complete(contractReturn); | |||
| //TODO: Not implemented; | |||
| throw new IllegalStateException("Not implemented!"); | |||
| // this.contractReturn.complete(contractReturn); | |||
| } | |||
| } catch (NoSuchMethodException e) { | |||
| throw new IllegalArgumentException(e.getMessage()); | |||
| @@ -2,6 +2,7 @@ package com.jd.blockchain.ledger.core; | |||
| import com.jd.blockchain.ledger.TransactionReturnMessage; | |||
| import com.jd.blockchain.ledger.TransactionState; | |||
| import com.jd.blockchain.ledger.OperationResult; | |||
| import com.jd.blockchain.ledger.LedgerTransaction; | |||
| import com.jd.blockchain.ledger.TransactionRequest; | |||
| @@ -35,7 +36,8 @@ public interface LedgerTransactionContext { | |||
| * | |||
| * @return | |||
| */ | |||
| LedgerTransaction commit(TransactionState txResult, TransactionReturnMessage returnMessage); | |||
| LedgerTransaction commit(TransactionState txResult, OperationResult... opResults); | |||
| /** | |||
| * 抛弃对账本数据的修改,以指定的交易状态提交交易;<br> | |||
| @@ -45,7 +47,7 @@ public interface LedgerTransactionContext { | |||
| * @param txResult | |||
| * @return | |||
| */ | |||
| LedgerTransaction discardAndCommit(TransactionState txResult, TransactionReturnMessage returnMessage); | |||
| LedgerTransaction discardAndCommit(TransactionState txResult); | |||
| /** | |||
| * 回滚事务,抛弃本次事务的所有数据更新; | |||
| @@ -25,7 +25,7 @@ public interface OperationHandle { | |||
| * @param previousBlockDataset | |||
| * 新区块的前一个区块的数据集;即未提交新区块之前的经过共识的账本最新数据集; | |||
| */ | |||
| void process(Operation op, LedgerDataSet newBlockDataset, TransactionRequestContext requestContext, | |||
| byte[] process(Operation op, LedgerDataSet newBlockDataset, TransactionRequestContext requestContext, | |||
| LedgerDataSet previousBlockDataset, OperationHandleContext handleContext, LedgerService ledgerService); | |||
| } | |||
| @@ -1,7 +1,15 @@ | |||
| package com.jd.blockchain.ledger.core.impl; | |||
| import java.util.Arrays; | |||
| import java.util.Comparator; | |||
| import com.jd.blockchain.crypto.HashDigest; | |||
| import com.jd.blockchain.ledger.*; | |||
| import com.jd.blockchain.ledger.DigitalSignature; | |||
| import com.jd.blockchain.ledger.LedgerTransaction; | |||
| import com.jd.blockchain.ledger.OperationResult; | |||
| import com.jd.blockchain.ledger.TransactionContent; | |||
| import com.jd.blockchain.ledger.TransactionRequest; | |||
| import com.jd.blockchain.ledger.TransactionState; | |||
| public class LedgerTransactionData implements LedgerTransaction { | |||
| @@ -19,7 +27,7 @@ public class LedgerTransactionData implements LedgerTransaction { | |||
| private long blockHeight; | |||
| private TransactionReturnMessage returnMessage; | |||
| private OperationResult[] operationResults; | |||
| // private HashDigest adminAccountHash; | |||
| // | |||
| @@ -47,7 +55,7 @@ public class LedgerTransactionData implements LedgerTransaction { | |||
| * 交易级的系统快照; | |||
| */ | |||
| public LedgerTransactionData(long blockHeight, TransactionRequest txReq, TransactionState execState, | |||
| TransactionStagedSnapshot txSnapshot, TransactionReturnMessage returnMessage) { | |||
| TransactionStagedSnapshot txSnapshot, OperationResult... opResults) { | |||
| this.blockHeight = blockHeight; | |||
| // this.txSnapshot = txSnapshot == null ? new TransactionStagedSnapshot() : txSnapshot; | |||
| this.txSnapshot = txSnapshot; | |||
| @@ -55,7 +63,15 @@ public class LedgerTransactionData implements LedgerTransaction { | |||
| this.endpointSignatures = txReq.getEndpointSignatures(); | |||
| this.nodeSignatures = txReq.getNodeSignatures(); | |||
| this.executionState = execState; | |||
| this.returnMessage = returnMessage; | |||
| if (opResults != null) { | |||
| Arrays.sort(opResults, new Comparator<OperationResult>() { | |||
| @Override | |||
| public int compare(OperationResult o1, OperationResult o2) { | |||
| return o1.getIndex() - o2.getIndex(); | |||
| } | |||
| }); | |||
| } | |||
| this.operationResults = opResults; | |||
| } | |||
| @Override | |||
| @@ -74,8 +90,8 @@ public class LedgerTransactionData implements LedgerTransaction { | |||
| } | |||
| @Override | |||
| public TransactionReturnMessage getReturnMessage() { | |||
| return returnMessage; | |||
| public OperationResult[] getOperationResults() { | |||
| return operationResults; | |||
| } | |||
| @Override | |||
| @@ -348,9 +348,9 @@ public class LedgerTransactionalEditor implements LedgerEditor { | |||
| public TransactionRequest getRequestTX() { | |||
| return txRequest; | |||
| } | |||
| @Override | |||
| public LedgerTransaction commit(TransactionState txResult, TransactionReturnMessage returnMessage) { | |||
| public LedgerTransaction commit(TransactionState txResult, OperationResult... opResults) { | |||
| checkTxState(); | |||
| // capture snapshot | |||
| @@ -359,7 +359,7 @@ public class LedgerTransactionalEditor implements LedgerEditor { | |||
| // LedgerTransactionData tx = new LedgerTransactionData(blockHeight, txRequest, | |||
| // txResult, txDataSnapshot); | |||
| LedgerTransactionData tx = new LedgerTransactionData(blockHeight, txRequest, txResult, null, returnMessage); | |||
| LedgerTransactionData tx = new LedgerTransactionData(blockHeight, txRequest, txResult, null, opResults); | |||
| this.txset.add(tx); | |||
| // this.txset.commit(); | |||
| @@ -374,9 +374,9 @@ public class LedgerTransactionalEditor implements LedgerEditor { | |||
| committed = true; | |||
| return tx; | |||
| } | |||
| @Override | |||
| public LedgerTransaction discardAndCommit(TransactionState txResult, TransactionReturnMessage returnMessage) { | |||
| public LedgerTransaction discardAndCommit(TransactionState txResult) { | |||
| checkTxState(); | |||
| // 未处理 | |||
| @@ -385,7 +385,7 @@ public class LedgerTransactionalEditor implements LedgerEditor { | |||
| // TransactionStagedSnapshot txDataSnapshot = takeSnapshot(); | |||
| // LedgerTransactionData tx = new LedgerTransactionData(blockHeight, txRequest, | |||
| // txResult, txDataSnapshot); | |||
| LedgerTransactionData tx = new LedgerTransactionData(blockHeight, txRequest, txResult, null, returnMessage); | |||
| LedgerTransactionData tx = new LedgerTransactionData(blockHeight, txRequest, txResult, null); | |||
| this.txset.add(tx); | |||
| // this.txset.commit(); | |||
| @@ -3,15 +3,20 @@ package com.jd.blockchain.ledger.core.impl; | |||
| import java.util.ArrayList; | |||
| import java.util.Iterator; | |||
| import java.util.List; | |||
| import java.util.concurrent.CompletableFuture; | |||
| import com.jd.blockchain.ledger.*; | |||
| import com.jd.blockchain.ledger.core.impl.handles.ContractEventSendOperationHandle; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import com.jd.blockchain.crypto.HashDigest; | |||
| import com.jd.blockchain.ledger.ContractEventSendOperation; | |||
| import com.jd.blockchain.ledger.LedgerBlock; | |||
| import com.jd.blockchain.ledger.LedgerException; | |||
| import com.jd.blockchain.ledger.Operation; | |||
| import com.jd.blockchain.ledger.OperationResult; | |||
| import com.jd.blockchain.ledger.OperationResultData; | |||
| import com.jd.blockchain.ledger.TransactionRequest; | |||
| import com.jd.blockchain.ledger.TransactionResponse; | |||
| import com.jd.blockchain.ledger.TransactionState; | |||
| import com.jd.blockchain.ledger.core.LedgerDataSet; | |||
| import com.jd.blockchain.ledger.core.LedgerEditor; | |||
| import com.jd.blockchain.ledger.core.LedgerService; | |||
| @@ -26,7 +31,7 @@ import com.jd.blockchain.utils.Bytes; | |||
| public class TransactionBatchProcessor implements TransactionBatchProcess { | |||
| private static final Logger LOGGER = LoggerFactory.getLogger(TransactionBatchProcessor.class); | |||
| private LedgerService ledgerService; | |||
| private LedgerEditor newBlockEditor; | |||
| @@ -45,12 +50,9 @@ public class TransactionBatchProcessor implements TransactionBatchProcess { | |||
| private TransactionBatchResult batchResult; | |||
| /** | |||
| * @param newBlockEditor | |||
| * 新区块的数据编辑器; | |||
| * @param previousBlockDataset | |||
| * 新区块的前一个区块的数据集;即未提交新区块之前的经过共识的账本最新数据集; | |||
| * @param opHandles | |||
| * 操作处理对象注册表; | |||
| * @param newBlockEditor 新区块的数据编辑器; | |||
| * @param previousBlockDataset 新区块的前一个区块的数据集;即未提交新区块之前的经过共识的账本最新数据集; | |||
| * @param opHandles 操作处理对象注册表; | |||
| */ | |||
| public TransactionBatchProcessor(LedgerEditor newBlockEditor, LedgerDataSet previousBlockDataset, | |||
| OperationHandleRegisteration opHandles, LedgerService ledgerService) { | |||
| @@ -71,9 +73,8 @@ public class TransactionBatchProcessor implements TransactionBatchProcess { | |||
| public TransactionResponse schedule(TransactionRequest request) { | |||
| // 此调用将会验证交易签名,验签失败将会抛出异常,同时,不记录签名错误的交易到链上; | |||
| LedgerTransactionContext txCtx = newBlockEditor.newTransaction(request); | |||
| TransactionState result; | |||
| TransactionReturnMessageData returnMessageData = new TransactionReturnMessageData(); | |||
| TransactionState txResult; | |||
| OperationResult[] opResults = null; | |||
| try { | |||
| LedgerDataSet dataset = txCtx.getDataSet(); | |||
| TransactionRequestContext reqCtx = new TransactionRequestContextImpl(request); | |||
| @@ -94,58 +95,44 @@ public class TransactionBatchProcessor implements TransactionBatchProcess { | |||
| OperationHandleContext handleContext = new OperationHandleContext() { | |||
| @Override | |||
| public void handle(Operation operation) { | |||
| //assert; Instance of operation are one of User related operations or DataAccount related operations; | |||
| // assert; Instance of operation are one of User related operations or | |||
| // DataAccount related operations; | |||
| OperationHandle hdl = opHandles.getHandle(operation.getClass()); | |||
| hdl.process(operation, dataset, reqCtx, previousBlockDataset, this, ledgerService); | |||
| } | |||
| }; | |||
| OperationHandle opHandle; | |||
| int contractOpIndex = 0; | |||
| List<OperationResult> opResultList = new ArrayList<OperationResult>(); | |||
| int opIdx = 0; | |||
| for (Operation op : ops) { | |||
| opHandle = opHandles.getHandle(op.getClass()); | |||
| // 合约执行需要填充执行结果 | |||
| if (opHandle instanceof ContractEventSendOperationHandle) { | |||
| CompletableFuture<String> currContractReturn = new CompletableFuture<>(); | |||
| ContractReturnMessageData crmd = new ContractReturnMessageData(contractOpIndex++, currContractReturn); | |||
| returnMessageData.addContractReturnMessage(crmd); | |||
| ((ContractEventSendOperationHandle) opHandle).process(op, dataset, reqCtx, previousBlockDataset, handleContext, ledgerService, currContractReturn); | |||
| } else { | |||
| opHandle.process(op, dataset, reqCtx, previousBlockDataset, handleContext, ledgerService); | |||
| byte[] retn = opHandle.process(op, dataset, reqCtx, previousBlockDataset, handleContext, ledgerService); | |||
| if (op instanceof ContractEventSendOperation) { | |||
| // 支持返回值的操作; | |||
| opResultList.add(new OperationResultData(opIdx, retn)); | |||
| } | |||
| opIdx++; | |||
| } | |||
| // 提交交易(事务); | |||
| result = TransactionState.SUCCESS; | |||
| txCtx.commit(result, returnMessageData); | |||
| txResult = TransactionState.SUCCESS; | |||
| opResults = opResultList.toArray(new OperationResult[opResultList.size()]); | |||
| txCtx.commit(txResult, opResults); | |||
| } catch (LedgerException e) { | |||
| // TODO: 识别更详细的异常类型以及执行对应的处理; | |||
| result = TransactionState.LEDGER_ERROR; | |||
| txCtx.discardAndCommit(TransactionState.LEDGER_ERROR, returnMessageData); | |||
| txResult = TransactionState.LEDGER_ERROR; | |||
| txCtx.discardAndCommit(TransactionState.LEDGER_ERROR); | |||
| LOGGER.warn(String.format("Transaction rollback caused by the ledger exception! --[TxHash=%s] --%s", | |||
| request.getHash().toBase58(), e.getMessage()), e); | |||
| } catch (Exception e) { | |||
| result = TransactionState.SYSTEM_ERROR; | |||
| txCtx.discardAndCommit(TransactionState.SYSTEM_ERROR, returnMessageData); | |||
| txResult = TransactionState.SYSTEM_ERROR; | |||
| txCtx.discardAndCommit(TransactionState.SYSTEM_ERROR); | |||
| LOGGER.warn(String.format("Transaction rollback caused by the system exception! --[TxHash=%s] --%s", | |||
| request.getHash().toBase58(), e.getMessage()), e); | |||
| } | |||
| TxResponseHandle resp = new TxResponseHandle(request, result); | |||
| if (!returnMessageData.isContractReturnEmpty()) { | |||
| ContractReturnMessage[] contractReturnMessages = returnMessageData.getContractReturn(); | |||
| // 获取结果中的字符串 | |||
| String[] returnValue = new String[contractReturnMessages.length]; | |||
| try { | |||
| for (int i = 0; i < contractReturnMessages.length; i++) { | |||
| returnValue[i] = contractReturnMessages[i].getReturnMessage(); | |||
| } | |||
| resp.setContractReturn(returnValue); | |||
| } catch (Exception e) { | |||
| throw new IllegalStateException(e); | |||
| } | |||
| } | |||
| TxResponseHandle resp = new TxResponseHandle(request, txResult, opResults); | |||
| responseList.add(resp); | |||
| @@ -226,11 +213,12 @@ public class TransactionBatchProcessor implements TransactionBatchProcess { | |||
| private TransactionState result; | |||
| private String[] contractReturn; | |||
| private OperationResult[] opResults; | |||
| public TxResponseHandle(TransactionRequest request, TransactionState result) { | |||
| public TxResponseHandle(TransactionRequest request, TransactionState result, OperationResult... opResults) { | |||
| this.request = request; | |||
| this.result = result; | |||
| this.opResults = opResults; | |||
| } | |||
| @Override | |||
| @@ -259,12 +247,8 @@ public class TransactionBatchProcessor implements TransactionBatchProcess { | |||
| } | |||
| @Override | |||
| public String[] getContractReturn() { | |||
| return contractReturn; | |||
| } | |||
| public void setContractReturn(String[] contractReturn) { | |||
| this.contractReturn = contractReturn; | |||
| public OperationResult[] getContractReturn() { | |||
| return opResults; | |||
| } | |||
| } | |||
| @@ -14,7 +14,7 @@ import org.springframework.stereotype.Service; | |||
| public class ContractCodeDeployOperationHandle implements OperationHandle { | |||
| @Override | |||
| public void process(Operation op, LedgerDataSet dataset, TransactionRequestContext requestContext, | |||
| public byte[] process(Operation op, LedgerDataSet dataset, TransactionRequestContext requestContext, | |||
| LedgerDataSet previousBlockDataset, OperationHandleContext handleContext, LedgerService ledgerService) { | |||
| ContractCodeDeployOperation contractOP = (ContractCodeDeployOperation) op; | |||
| // TODO: 校验合约代码的正确性; | |||
| @@ -22,6 +22,8 @@ public class ContractCodeDeployOperationHandle implements OperationHandle { | |||
| // TODO: 请求者应该提供合约账户的公钥签名,已确定注册的地址的唯一性; | |||
| dataset.getContractAccountSet().deploy(contractOP.getContractID().getAddress(), | |||
| contractOP.getContractID().getPubKey(), contractOP.getAddressSignature(), contractOP.getChainCode()); | |||
| //No result; | |||
| return null; | |||
| } | |||
| @Override | |||
| @@ -2,6 +2,8 @@ package com.jd.blockchain.ledger.core.impl.handles; | |||
| import static com.jd.blockchain.utils.BaseConstant.CONTRACT_SERVICE_PROVIDER; | |||
| import java.util.concurrent.CompletableFuture; | |||
| import org.springframework.stereotype.Service; | |||
| import com.jd.blockchain.contract.LocalContractEventContext; | |||
| @@ -20,9 +22,6 @@ import com.jd.blockchain.ledger.core.TransactionRequestContext; | |||
| import com.jd.blockchain.ledger.core.impl.LedgerQueryService; | |||
| import com.jd.blockchain.ledger.core.impl.OperationHandleContext; | |||
| import java.util.concurrent.CompletableFuture; | |||
| import java.util.concurrent.Future; | |||
| @Service | |||
| public class ContractEventSendOperationHandle implements OperationHandle { | |||
| @@ -33,9 +32,12 @@ public class ContractEventSendOperationHandle implements OperationHandle { | |||
| } | |||
| @Override | |||
| public void process(Operation op, LedgerDataSet dataset, TransactionRequestContext requestContext, | |||
| public byte[] process(Operation op, LedgerDataSet dataset, TransactionRequestContext requestContext, | |||
| LedgerDataSet previousBlockDataset, OperationHandleContext opHandleContext, LedgerService ledgerService) { | |||
| process(op, dataset, requestContext, previousBlockDataset, opHandleContext, ledgerService, null); | |||
| // TODO: return value; | |||
| return null; | |||
| } | |||
| @Override | |||
| @@ -44,8 +46,8 @@ public class ContractEventSendOperationHandle implements OperationHandle { | |||
| } | |||
| public void process(Operation op, LedgerDataSet dataset, TransactionRequestContext requestContext, | |||
| LedgerDataSet previousBlockDataset, OperationHandleContext opHandleContext, | |||
| LedgerService ledgerService, CompletableFuture<String> contractReturn) { | |||
| LedgerDataSet previousBlockDataset, OperationHandleContext opHandleContext, LedgerService ledgerService, | |||
| CompletableFuture<String> contractReturn) { | |||
| ContractEventSendOperation contractOP = (ContractEventSendOperation) op; | |||
| // 先从账本校验合约的有效性; | |||
| @@ -84,5 +86,4 @@ public class ContractEventSendOperationHandle implements OperationHandle { | |||
| contractCode.processEvent(localContractEventContext, contractReturn); | |||
| } | |||
| } | |||
| @@ -1,24 +1,28 @@ | |||
| package com.jd.blockchain.ledger.core.impl.handles; | |||
| import com.jd.blockchain.binaryproto.BinaryProtocol; | |||
| import org.springframework.stereotype.Service; | |||
| import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
| import com.jd.blockchain.ledger.BytesValue; | |||
| import com.jd.blockchain.ledger.DataAccountKVSetOperation; | |||
| import com.jd.blockchain.ledger.DataAccountKVSetOperation.KVWriteEntry; | |||
| import com.jd.blockchain.ledger.Operation; | |||
| import com.jd.blockchain.ledger.core.*; | |||
| import com.jd.blockchain.ledger.core.DataAccount; | |||
| import com.jd.blockchain.ledger.core.LedgerDataSet; | |||
| import com.jd.blockchain.ledger.core.LedgerService; | |||
| import com.jd.blockchain.ledger.core.OperationHandle; | |||
| import com.jd.blockchain.ledger.core.TransactionRequestContext; | |||
| import com.jd.blockchain.ledger.core.impl.OperationHandleContext; | |||
| import com.jd.blockchain.utils.Bytes; | |||
| import org.springframework.stereotype.Service; | |||
| @Service | |||
| public class DataAccountKVSetOperationHandle implements OperationHandle{ | |||
| public class DataAccountKVSetOperationHandle implements OperationHandle { | |||
| static { | |||
| DataContractRegistry.register(BytesValue.class); | |||
| } | |||
| @Override | |||
| public void process(Operation op, LedgerDataSet dataset, TransactionRequestContext requestContext, | |||
| public byte[] process(Operation op, LedgerDataSet dataset, TransactionRequestContext requestContext, | |||
| LedgerDataSet previousBlockDataset, OperationHandleContext handleContext, LedgerService ledgerService) { | |||
| DataAccountKVSetOperation kvWriteOp = (DataAccountKVSetOperation) op; | |||
| DataAccount account = dataset.getDataAccountSet().getDataAccount(kvWriteOp.getAccountAddress()); | |||
| @@ -26,6 +30,9 @@ public class DataAccountKVSetOperationHandle implements OperationHandle{ | |||
| for (KVWriteEntry kvw : writeset) { | |||
| account.setBytes(Bytes.fromString(kvw.getKey()), kvw.getValue(), kvw.getExpectedVersion()); | |||
| } | |||
| // No return value; | |||
| return null; | |||
| } | |||
| @Override | |||
| @@ -12,18 +12,21 @@ import com.jd.blockchain.ledger.core.impl.OperationHandleContext; | |||
| import org.springframework.stereotype.Service; | |||
| @Service | |||
| public class DataAccountRegisterOperationHandle implements OperationHandle{ | |||
| public class DataAccountRegisterOperationHandle implements OperationHandle { | |||
| @Override | |||
| public void process(Operation op, LedgerDataSet dataset, TransactionRequestContext requestContext, | |||
| public byte[] process(Operation op, LedgerDataSet dataset, TransactionRequestContext requestContext, | |||
| LedgerDataSet previousBlockDataset, OperationHandleContext handleContext, LedgerService ledgerService) { | |||
| DataAccountRegisterOperation dataAccountRegOp = (DataAccountRegisterOperation) op; | |||
| BlockchainIdentity bid = dataAccountRegOp.getAccountID(); | |||
| //TODO: 校验用户身份; | |||
| //TODO: 请求者应该提供数据账户的公钥签名,已确定注册的地址的唯一性; | |||
| // TODO: 校验用户身份; | |||
| // TODO: 请求者应该提供数据账户的公钥签名,已确定注册的地址的唯一性; | |||
| dataset.getDataAccountSet().register(bid.getAddress(), bid.getPubKey(), null); | |||
| // No return value; | |||
| return null; | |||
| } | |||
| @Override | |||
| @@ -9,14 +9,17 @@ import com.jd.blockchain.ledger.core.OperationHandle; | |||
| import com.jd.blockchain.ledger.core.TransactionRequestContext; | |||
| import com.jd.blockchain.ledger.core.impl.OperationHandleContext; | |||
| public class UserRegisterOperationHandle implements OperationHandle{ | |||
| public class UserRegisterOperationHandle implements OperationHandle { | |||
| @Override | |||
| public void process(Operation op, LedgerDataSet dataset, TransactionRequestContext requestContext, | |||
| public byte[] process(Operation op, LedgerDataSet dataset, TransactionRequestContext requestContext, | |||
| LedgerDataSet previousBlockDataset, OperationHandleContext handleContext, LedgerService ledgerService) { | |||
| UserRegisterOperation userRegOp = (UserRegisterOperation) op; | |||
| BlockchainIdentity bid = userRegOp.getUserID(); | |||
| dataset.getUserAccountSet().register(bid.getAddress(), bid.getPubKey()); | |||
| // No return value; | |||
| return null; | |||
| } | |||
| @Override | |||
| @@ -1,17 +0,0 @@ | |||
| package com.jd.blockchain.ledger; | |||
| import com.jd.blockchain.binaryproto.DataContract; | |||
| import com.jd.blockchain.binaryproto.DataField; | |||
| import com.jd.blockchain.binaryproto.PrimitiveType; | |||
| import com.jd.blockchain.consts.DataCodes; | |||
| @DataContract(code= DataCodes.CONTRACT_RETURN) | |||
| public interface ContractReturnMessage { | |||
| @DataField(order=1, primitiveType = PrimitiveType.INT32) | |||
| int getOperationIndex(); | |||
| @DataField(order=2, primitiveType = PrimitiveType.TEXT) | |||
| String getReturnMessage(); | |||
| } | |||
| @@ -1,41 +0,0 @@ | |||
| package com.jd.blockchain.ledger; | |||
| import java.util.concurrent.CompletableFuture; | |||
| public class ContractReturnMessageData implements ContractReturnMessage { | |||
| private int operationIndex; | |||
| private CompletableFuture<String> returnMsgFuture; | |||
| public ContractReturnMessageData() { | |||
| } | |||
| public ContractReturnMessageData(int operationIndex, CompletableFuture<String> returnMsgFuture) { | |||
| this.operationIndex = operationIndex; | |||
| this.returnMsgFuture = returnMsgFuture; | |||
| } | |||
| public void setOperationIndex(int operationIndex) { | |||
| this.operationIndex = operationIndex; | |||
| } | |||
| public void setReturnMsgFuture(CompletableFuture<String> returnMsgFuture) { | |||
| this.returnMsgFuture = returnMsgFuture; | |||
| } | |||
| @Override | |||
| public int getOperationIndex() { | |||
| return operationIndex; | |||
| } | |||
| @Override | |||
| public String getReturnMessage() { | |||
| try { | |||
| return returnMsgFuture.get(); | |||
| } catch (Exception e) { | |||
| e.printStackTrace(); | |||
| throw new IllegalStateException(e); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,17 @@ | |||
| package com.jd.blockchain.ledger; | |||
| import com.jd.blockchain.binaryproto.DataContract; | |||
| import com.jd.blockchain.binaryproto.DataField; | |||
| import com.jd.blockchain.binaryproto.PrimitiveType; | |||
| import com.jd.blockchain.consts.DataCodes; | |||
| @DataContract(code = DataCodes.CONTRACT_RETURN) | |||
| public interface OperationResult { | |||
| @DataField(order = 1, primitiveType = PrimitiveType.INT32) | |||
| int getIndex(); | |||
| @DataField(order = 2, primitiveType = PrimitiveType.BYTES) | |||
| byte[] getResult(); | |||
| } | |||
| @@ -0,0 +1,26 @@ | |||
| package com.jd.blockchain.ledger; | |||
| public class OperationResultData implements OperationResult { | |||
| private int index; | |||
| private byte[] result; | |||
| public OperationResultData() { | |||
| } | |||
| public OperationResultData(int operationIndex, byte[] result) { | |||
| this.index = operationIndex; | |||
| this.result = result; | |||
| } | |||
| @Override | |||
| public int getIndex() { | |||
| return index; | |||
| } | |||
| @Override | |||
| public byte[] getResult() { | |||
| return result; | |||
| } | |||
| } | |||
| @@ -5,7 +5,6 @@ import com.jd.blockchain.binaryproto.DataField; | |||
| import com.jd.blockchain.binaryproto.PrimitiveType; | |||
| import com.jd.blockchain.consts.DataCodes; | |||
| import com.jd.blockchain.crypto.HashDigest; | |||
| import com.jd.blockchain.utils.io.ByteArray; | |||
| /** | |||
| * Transaction 区块链交易,是被原子执行的操作集合; | |||
| @@ -13,7 +12,7 @@ import com.jd.blockchain.utils.io.ByteArray; | |||
| * @author huanghaiquan | |||
| * | |||
| */ | |||
| @DataContract(code= DataCodes.TX) | |||
| @DataContract(code = DataCodes.TX) | |||
| public interface Transaction extends NodeRequest, HashObject { | |||
| /** | |||
| @@ -23,7 +22,7 @@ public interface Transaction extends NodeRequest, HashObject { | |||
| * | |||
| * @return | |||
| */ | |||
| @DataField(order=1, primitiveType = PrimitiveType.BYTES) | |||
| @DataField(order = 1, primitiveType = PrimitiveType.BYTES) | |||
| @Override | |||
| HashDigest getHash(); | |||
| @@ -32,7 +31,7 @@ public interface Transaction extends NodeRequest, HashObject { | |||
| * | |||
| * @return | |||
| */ | |||
| @DataField(order=2, primitiveType=PrimitiveType.INT64) | |||
| @DataField(order = 2, primitiveType = PrimitiveType.INT64) | |||
| long getBlockHeight(); | |||
| /** | |||
| @@ -42,7 +41,7 @@ public interface Transaction extends NodeRequest, HashObject { | |||
| * | |||
| * @return | |||
| */ | |||
| @DataField(order=3, refEnum=true) | |||
| @DataField(order = 3, refEnum = true) | |||
| TransactionState getExecutionState(); | |||
| /** | |||
| @@ -50,6 +49,6 @@ public interface Transaction extends NodeRequest, HashObject { | |||
| * | |||
| * @return | |||
| */ | |||
| @DataField(order=4, refContract=true) | |||
| TransactionReturnMessage getReturnMessage(); | |||
| @DataField(order = 4, refContract = true, list = true) | |||
| OperationResult[] getOperationResults(); | |||
| } | |||
| @@ -27,7 +27,7 @@ public class TransactionRespHandle implements TransactionResponse { | |||
| private TransactionState globalResult; | |||
| private String[] contractReturn; | |||
| private OperationResult[] contractReturn; | |||
| public TransactionRespHandle(TransactionRequest request, TransactionState result, TransactionState globalResult) { | |||
| this.request = request; | |||
| @@ -51,7 +51,7 @@ public class TransactionRespHandle implements TransactionResponse { | |||
| this.result = result; | |||
| } | |||
| public void setContractReturn(String[] contractReturn) { | |||
| public void setContractReturn(OperationResult[] contractReturn) { | |||
| this.contractReturn = contractReturn; | |||
| } | |||
| @@ -97,7 +97,7 @@ public class TransactionRespHandle implements TransactionResponse { | |||
| } | |||
| @Override | |||
| public String[] getContractReturn() { | |||
| public OperationResult[] getContractReturn() { | |||
| return contractReturn; | |||
| } | |||
| } | |||
| @@ -12,7 +12,7 @@ import com.jd.blockchain.crypto.HashDigest; | |||
| * @author huanghaiquan | |||
| * | |||
| */ | |||
| @DataContract(code= DataCodes.TX_RESPONSE) | |||
| @DataContract(code = DataCodes.TX_RESPONSE) | |||
| public interface TransactionResponse { | |||
| /** | |||
| @@ -20,7 +20,7 @@ public interface TransactionResponse { | |||
| * | |||
| * @return | |||
| */ | |||
| @DataField(order=1, primitiveType = PrimitiveType.BYTES) | |||
| @DataField(order = 1, primitiveType = PrimitiveType.BYTES) | |||
| HashDigest getContentHash(); | |||
| /** | |||
| @@ -28,7 +28,7 @@ public interface TransactionResponse { | |||
| * | |||
| * @return | |||
| */ | |||
| @DataField(order=2, refEnum=true) | |||
| @DataField(order = 2, refEnum = true) | |||
| TransactionState getExecutionState(); | |||
| /** | |||
| @@ -36,7 +36,7 @@ public interface TransactionResponse { | |||
| * | |||
| * @return | |||
| */ | |||
| @DataField(order=3, primitiveType = PrimitiveType.BYTES) | |||
| @DataField(order = 3, primitiveType = PrimitiveType.BYTES) | |||
| HashDigest getBlockHash(); | |||
| /** | |||
| @@ -47,7 +47,7 @@ public interface TransactionResponse { | |||
| * | |||
| * @return | |||
| */ | |||
| @DataField(order=4, primitiveType=PrimitiveType.INT64) | |||
| @DataField(order = 4, primitiveType = PrimitiveType.INT64) | |||
| long getBlockHeight(); | |||
| /** | |||
| @@ -55,7 +55,7 @@ public interface TransactionResponse { | |||
| * | |||
| * @return | |||
| */ | |||
| @DataField(order=5, primitiveType=PrimitiveType.BOOLEAN) | |||
| @DataField(order = 5, primitiveType = PrimitiveType.BOOLEAN) | |||
| boolean isSuccess(); | |||
| /** | |||
| @@ -63,6 +63,6 @@ public interface TransactionResponse { | |||
| * | |||
| * @return | |||
| */ | |||
| @DataField(order=6, list=true, primitiveType=PrimitiveType.TEXT) | |||
| String[] getContractReturn(); | |||
| @DataField(order = 6, list = true, refContract = true) | |||
| OperationResult[] getContractReturn(); | |||
| } | |||
| @@ -13,5 +13,5 @@ public interface TransactionReturnMessage { | |||
| * @return | |||
| */ | |||
| @DataField(order=1, list = true, refContract=true) | |||
| ContractReturnMessage[] getContractReturn(); | |||
| OperationResult[] getContractReturn(); | |||
| } | |||
| @@ -5,9 +5,9 @@ import java.util.List; | |||
| public class TransactionReturnMessageData implements TransactionReturnMessage { | |||
| private List<ContractReturnMessage> contractReturnMessages = new ArrayList<>(); | |||
| private List<OperationResult> contractReturnMessages = new ArrayList<>(); | |||
| public void addContractReturnMessage(ContractReturnMessage contractReturnMessage) { | |||
| public void addContractReturnMessage(OperationResult contractReturnMessage) { | |||
| contractReturnMessages.add(contractReturnMessage); | |||
| } | |||
| @@ -16,11 +16,11 @@ public class TransactionReturnMessageData implements TransactionReturnMessage { | |||
| } | |||
| @Override | |||
| public ContractReturnMessage[] getContractReturn() { | |||
| public OperationResult[] getContractReturn() { | |||
| if (isContractReturnEmpty()) { | |||
| return null; | |||
| } | |||
| ContractReturnMessage[] crms = new ContractReturnMessage[contractReturnMessages.size()]; | |||
| OperationResult[] crms = new OperationResult[contractReturnMessages.size()]; | |||
| return contractReturnMessages.toArray(crms); | |||
| } | |||
| } | |||
| @@ -2,6 +2,7 @@ package com.jd.blockchain.transaction; | |||
| import com.jd.blockchain.ledger.TransactionState; | |||
| import com.jd.blockchain.crypto.HashDigest; | |||
| import com.jd.blockchain.ledger.OperationResult; | |||
| import com.jd.blockchain.ledger.TransactionResponse; | |||
| /** | |||
| @@ -18,7 +19,7 @@ public class TxResponseMessage implements TransactionResponse { | |||
| private TransactionState executionState; | |||
| private String[] contractReturn; | |||
| private OperationResult[] contractReturn; | |||
| public TxResponseMessage() { | |||
| } | |||
| @@ -59,7 +60,7 @@ public class TxResponseMessage implements TransactionResponse { | |||
| this.blockHeight = blockHeight; | |||
| } | |||
| public void setContractReturn(String[] contractReturn) { | |||
| public void setContractReturn(OperationResult[] contractReturn) { | |||
| this.contractReturn = contractReturn; | |||
| } | |||
| @@ -69,7 +70,7 @@ public class TxResponseMessage implements TransactionResponse { | |||
| } | |||
| @Override | |||
| public String[] getContractReturn() { | |||
| public OperationResult[] getContractReturn() { | |||
| return contractReturn; | |||
| } | |||
| @@ -2,6 +2,7 @@ package com.jd.blockchain.peer.consensus; | |||
| import com.jd.blockchain.binaryproto.BinaryProtocol; | |||
| import com.jd.blockchain.ledger.LedgerBlock; | |||
| import com.jd.blockchain.ledger.OperationResult; | |||
| import com.jd.blockchain.ledger.TransactionRequest; | |||
| import com.jd.blockchain.ledger.TransactionResponse; | |||
| import com.jd.blockchain.ledger.TransactionState; | |||
| @@ -309,7 +310,7 @@ public class ConsensusMessageDispatcher implements MessageHandle { | |||
| } | |||
| @Override | |||
| public String[] getContractReturn() { | |||
| public OperationResult[] getContractReturn() { | |||
| return txResp.getContractReturn(); | |||
| } | |||
| } | |||
| @@ -20,7 +20,6 @@ import java.util.Map; | |||
| import java.util.Set; | |||
| import java.util.concurrent.ConcurrentHashMap; | |||
| public class MockerContractExeHandle implements OperationHandle { | |||
| private Map<HashDigest, ExecutorProxy> executorProxyMap = new ConcurrentHashMap<>(); | |||
| @@ -30,7 +29,7 @@ public class MockerContractExeHandle implements OperationHandle { | |||
| private HashDigest ledgerHash; | |||
| @Override | |||
| public void process(Operation op, LedgerDataSet dataset, TransactionRequestContext requestContext, | |||
| public byte[] process(Operation op, LedgerDataSet dataset, TransactionRequestContext requestContext, | |||
| LedgerDataSet previousBlockDataset, OperationHandleContext opHandleContext, LedgerService ledgerService) { | |||
| ContractEventSendOperation contractOP = (ContractEventSendOperation) op; | |||
| @@ -42,8 +41,8 @@ public class MockerContractExeHandle implements OperationHandle { | |||
| LedgerQueryService queryService = new LedgerQueryService(ledgerManager); | |||
| ContractLedgerContext ledgerContext = new ContractLedgerContext(queryService, opHandleContext); | |||
| MockerContractEventContext contractEventContext = new MockerContractEventContext( | |||
| ledgerHash, contractOP.getEvent(), requestContext.getRequest(), ledgerContext); | |||
| MockerContractEventContext contractEventContext = new MockerContractEventContext(ledgerHash, | |||
| contractOP.getEvent(), requestContext.getRequest(), ledgerContext); | |||
| EventProcessingAwire eventProcessingAwire = (EventProcessingAwire) executorProxy.getInstance(); | |||
| try { | |||
| @@ -60,6 +59,9 @@ public class MockerContractExeHandle implements OperationHandle { | |||
| removeExecutorProxy(txHash); | |||
| } | |||
| } | |||
| // No return value; | |||
| return null; | |||
| } | |||
| @Override | |||
| @@ -90,8 +92,8 @@ public class MockerContractExeHandle implements OperationHandle { | |||
| private LedgerContext ledgerContext; | |||
| public MockerContractEventContext(HashDigest ledgeHash, String event, | |||
| TransactionRequest transactionRequest, LedgerContext ledgerContext) { | |||
| public MockerContractEventContext(HashDigest ledgeHash, String event, TransactionRequest transactionRequest, | |||
| LedgerContext ledgerContext) { | |||
| this.ledgeHash = ledgeHash; | |||
| this.event = event; | |||
| this.transactionRequest = transactionRequest; | |||