diff --git a/source/consensus/consensus-bftsmart/src/main/java/com/jd/blockchain/consensus/bftsmart/service/BftsmartNodeServer.java b/source/consensus/consensus-bftsmart/src/main/java/com/jd/blockchain/consensus/bftsmart/service/BftsmartNodeServer.java index 48c85f79..c0194d64 100644 --- a/source/consensus/consensus-bftsmart/src/main/java/com/jd/blockchain/consensus/bftsmart/service/BftsmartNodeServer.java +++ b/source/consensus/consensus-bftsmart/src/main/java/com/jd/blockchain/consensus/bftsmart/service/BftsmartNodeServer.java @@ -359,33 +359,105 @@ public class BftsmartNodeServer extends DefaultRecoverable implements NodeServer * Used by consensus write phase, pre compute new block hash * */ +// public BatchAppResultImpl preComputeAppHash(byte[][] commands) { +// String batchId = messageHandle.beginBatch(realmName); +// List> asyncFutureLinkedList = new ArrayList<>(commands.length); +// List responseLinkedList = new ArrayList<>(); +// try { +// int msgId = 0; +// for (byte[] txContent : commands) { +// AsyncFuture asyncFuture = messageHandle.processOrdered(msgId++, txContent, realmName, batchId); +// asyncFutureLinkedList.add(asyncFuture); +// } +// StateSnapshot stateSnapshot = messageHandle.completeBatch(realmName, batchId); +// byte[] blockHashBytes = stateSnapshot.getSnapshot(); +// +// for (int i = 0; i< asyncFutureLinkedList.size(); i++) { +// responseLinkedList.add(asyncFutureLinkedList.get(i).get()); +// } +// +// +// return new BatchAppResultImpl(responseLinkedList, blockHashBytes, batchId); +// +// } catch (Exception e) { +// // todo 需要处理应答码 404 +// LOGGER.error("Error occurred while processing ordered messages! --" + e.getMessage(), e); +// messageHandle.rollbackBatch(realmName, batchId, TransactionState.IGNORED_BY_CONSENSUS_PHASE_PRECOMPUTE_ROLLBACK.CODE); +// } +// +// return null; +// } + + /** + * Used by consensus write phase, pre compute new block hash + */ public BatchAppResultImpl preComputeAppHash(byte[][] commands) { String batchId = messageHandle.beginBatch(realmName); List> asyncFutureLinkedList = new ArrayList<>(commands.length); List responseLinkedList = new ArrayList<>(); + BatchAppResultImpl result; try { int msgId = 0; - for (byte[] txContent : commands) { - AsyncFuture asyncFuture = messageHandle.processOrdered(msgId++, txContent, realmName, batchId); - asyncFutureLinkedList.add(asyncFuture); - } - StateSnapshot stateSnapshot = messageHandle.completeBatch(realmName, batchId); - byte[] blockHashBytes = stateSnapshot.getSnapshot(); - for (int i = 0; i< asyncFutureLinkedList.size(); i++) { - responseLinkedList.add(asyncFutureLinkedList.get(i).get()); + boolean isOK = true; + + for (int i = 0; i < commands.length; i++) { + + byte[] txContent = commands[i]; + try { + AsyncFuture asyncFuture = messageHandle.processOrdered(msgId++, txContent, realmName, batchId); + asyncFutureLinkedList.add(asyncFuture); + } catch (BlockRollbackException e) { + + LOGGER.error("Error occurred while processing ordered messages! --" + e.getMessage(), e); + + isOK = false; + + break; + } } + if (isOK) { + StateSnapshot stateSnapshot = messageHandle.completeBatch(realmName, batchId); + byte[] blockHashBytes = stateSnapshot.getSnapshot(); + + for (int i = 0; i < asyncFutureLinkedList.size(); i++) { + responseLinkedList.add(asyncFutureLinkedList.get(i).get()); + } + + result = new BatchAppResultImpl(responseLinkedList, blockHashBytes, batchId); + result.setErrorCode((byte) 0); + + return result; + } else { + + for (int i = 0; i < commands.length; i++) { + responseLinkedList.add(createAppResponse(commands[i])); + } + + Random random = new Random(); + byte[] rand = new byte[4]; + random.nextBytes(rand); - return new BatchAppResultImpl(responseLinkedList, blockHashBytes, batchId); + result = new BatchAppResultImpl(responseLinkedList, rand, batchId); + result.setErrorCode((byte) 1); + + return result; + } } catch (Exception e) { - // todo 需要处理应答码 404 - LOGGER.error("Error occurred while processing ordered messages! --" + e.getMessage(), e); - messageHandle.rollbackBatch(realmName, batchId, TransactionState.IGNORED_BY_CONSENSUS_PHASE_PRECOMPUTE_ROLLBACK.CODE); + LOGGER.error("Error occurred while genearte batch app result! --" + e.getMessage(), e); + throw e; } + } - return null; + public byte[] createAppResponse(byte[] command) { + TransactionRequest txRequest = BinaryProtocol.decode(command); + + TxResponseMessage resp = new TxResponseMessage(txRequest.getTransactionContent().getHash()); + resp.setExecutionState(TransactionState.IGNORED_BY_BLOCK_FULL_ROLLBACK); + + return BinaryProtocol.encode(resp, TransactionResponse.class); } /**