Browse Source

solve pre compute null pointer bug

tags/1.1.1^2
zhangshuang 4 years ago
parent
commit
f2d34f56e8
1 changed files with 85 additions and 13 deletions
  1. +85
    -13
      source/consensus/consensus-bftsmart/src/main/java/com/jd/blockchain/consensus/bftsmart/service/BftsmartNodeServer.java

+ 85
- 13
source/consensus/consensus-bftsmart/src/main/java/com/jd/blockchain/consensus/bftsmart/service/BftsmartNodeServer.java View File

@@ -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<AsyncFuture<byte[]>> asyncFutureLinkedList = new ArrayList<>(commands.length);
// List<byte[]> responseLinkedList = new ArrayList<>();
// try {
// int msgId = 0;
// for (byte[] txContent : commands) {
// AsyncFuture<byte[]> 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<AsyncFuture<byte[]>> asyncFutureLinkedList = new ArrayList<>(commands.length);
List<byte[]> responseLinkedList = new ArrayList<>();
BatchAppResultImpl result;
try {
int msgId = 0;
for (byte[] txContent : commands) {
AsyncFuture<byte[]> 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<byte[]> 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);
}

/**


Loading…
Cancel
Save