Browse Source

when most of block hash is inconsistent, less than Quorum consistent responses, will return null response

tags/1.1.2^2^2
zhangshuang 5 years ago
parent
commit
e622a7b247
4 changed files with 39 additions and 10 deletions
  1. +15
    -10
      source/consensus/consensus-bftsmart/src/main/java/com/jd/blockchain/consensus/bftsmart/service/BftsmartNodeServer.java
  2. +7
    -0
      source/consensus/consensus-framework/src/main/java/com/jd/blockchain/consensus/service/MessageHandle.java
  3. +4
    -0
      source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/TransactionBatchProcessor.java
  4. +13
    -0
      source/peer/src/main/java/com/jd/blockchain/peer/consensus/ConsensusMessageDispatcher.java

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

@@ -10,6 +10,7 @@ import bftsmart.consensus.app.BatchAppResultImpl;
import bftsmart.tom.*; import bftsmart.tom.*;
import com.jd.blockchain.binaryproto.BinaryProtocol; import com.jd.blockchain.binaryproto.BinaryProtocol;
import com.jd.blockchain.consensus.service.*; import com.jd.blockchain.consensus.service.*;
import com.jd.blockchain.crypto.HashDigest;
import com.jd.blockchain.ledger.*; import com.jd.blockchain.ledger.*;
import com.jd.blockchain.transaction.TxResponseMessage; import com.jd.blockchain.transaction.TxResponseMessage;
import com.jd.blockchain.utils.serialize.binary.BinarySerializeUtils; import com.jd.blockchain.utils.serialize.binary.BinarySerializeUtils;
@@ -363,6 +364,7 @@ public class BftsmartNodeServer extends DefaultRecoverable implements NodeServer
List<byte[]> responseLinkedList = new ArrayList<>(); List<byte[]> responseLinkedList = new ArrayList<>();
StateSnapshot newStateSnapshot = null; StateSnapshot newStateSnapshot = null;
StateSnapshot preStateSnapshot = null; StateSnapshot preStateSnapshot = null;
StateSnapshot genisStateSnapshot = null;
BatchAppResultImpl result = null; BatchAppResultImpl result = null;
String batchId = null; String batchId = null;
int msgId = 0; int msgId = 0;
@@ -370,7 +372,7 @@ public class BftsmartNodeServer extends DefaultRecoverable implements NodeServer
try { try {


batchId = messageHandle.beginBatch(realmName); batchId = messageHandle.beginBatch(realmName);
genisStateSnapshot = messageHandle.getGenisStateSnapshot(realmName);
preStateSnapshot = messageHandle.getStateSnapshot(realmName); preStateSnapshot = messageHandle.getStateSnapshot(realmName);


if (preStateSnapshot == null) { if (preStateSnapshot == null) {
@@ -388,7 +390,7 @@ public class BftsmartNodeServer extends DefaultRecoverable implements NodeServer
responseLinkedList.add(asyncFutureLinkedList.get(i).get()); responseLinkedList.add(asyncFutureLinkedList.get(i).get());
} }


result = new BatchAppResultImpl(responseLinkedList, newStateSnapshot.getSnapshot(), batchId);
result = new BatchAppResultImpl(responseLinkedList, newStateSnapshot.getSnapshot(), batchId, genisStateSnapshot.getSnapshot());
result.setErrorCode((byte) 0); result.setErrorCode((byte) 0);


} catch (Exception e) { } catch (Exception e) {
@@ -397,7 +399,7 @@ public class BftsmartNodeServer extends DefaultRecoverable implements NodeServer
responseLinkedList.add(createAppResponse(commands[i],TransactionState.IGNORED_BY_BLOCK_FULL_ROLLBACK)); responseLinkedList.add(createAppResponse(commands[i],TransactionState.IGNORED_BY_BLOCK_FULL_ROLLBACK));
} }


result = new BatchAppResultImpl(responseLinkedList,preStateSnapshot.getSnapshot(), batchId);
result = new BatchAppResultImpl(responseLinkedList,preStateSnapshot.getSnapshot(), batchId, genisStateSnapshot.getSnapshot());
result.setErrorCode((byte) 1); result.setErrorCode((byte) 1);
} }


@@ -415,20 +417,23 @@ public class BftsmartNodeServer extends DefaultRecoverable implements NodeServer
return BinaryProtocol.encode(resp, TransactionResponse.class); return BinaryProtocol.encode(resp, TransactionResponse.class);
} }


//update batch messages to block full rollback state
public List<byte[]> updateAppResponses(List<byte[]> asyncResponseLinkedList) {
public List<byte[]> updateAppResponses(List<byte[]> asyncResponseLinkedList, byte[] commonHash, boolean isConsistent) {
List<byte[]> updatedResponses = new ArrayList<>(); List<byte[]> updatedResponses = new ArrayList<>();
TxResponseMessage resp = null;


for(int i = 0; i < asyncResponseLinkedList.size(); i++) { for(int i = 0; i < asyncResponseLinkedList.size(); i++) {
TransactionResponse txResponse = BinaryProtocol.decode(asyncResponseLinkedList.get(i)); TransactionResponse txResponse = BinaryProtocol.decode(asyncResponseLinkedList.get(i));
TxResponseMessage resp = new TxResponseMessage(txResponse.getContentHash());
if (isConsistent) {
resp = new TxResponseMessage(txResponse.getContentHash());
}
else {
resp = new TxResponseMessage(new HashDigest(commonHash));
}
resp.setExecutionState(TransactionState.IGNORED_BY_BLOCK_FULL_ROLLBACK); resp.setExecutionState(TransactionState.IGNORED_BY_BLOCK_FULL_ROLLBACK);
updatedResponses.add(BinaryProtocol.encode(resp, TransactionResponse.class)); updatedResponses.add(BinaryProtocol.encode(resp, TransactionResponse.class));
}

return updatedResponses;
} }

return updatedResponses;
}
/** /**
* *
* Decision has been made at the consensus stage, commit block * Decision has been made at the consensus stage, commit block


+ 7
- 0
source/consensus/consensus-framework/src/main/java/com/jd/blockchain/consensus/service/MessageHandle.java View File

@@ -67,4 +67,11 @@ public interface MessageHandle {
*/ */
StateSnapshot getStateSnapshot(String realmName); StateSnapshot getStateSnapshot(String realmName);


/**
* 获得创世区块的状态快照
* @param realmName
* @return 创世区块的状态快照
*/
StateSnapshot getGenisStateSnapshot(String realmName);

} }

+ 4
- 0
source/ledger/ledger-core/src/main/java/com/jd/blockchain/ledger/core/TransactionBatchProcessor.java View File

@@ -43,6 +43,10 @@ public class TransactionBatchProcessor implements TransactionBatchProcess {
return ledger.getLatestBlockHash().toBytes(); return ledger.getLatestBlockHash().toBytes();
} }


public byte[] getGenisBlockHash() {
return ledger.getBlockHash(0).toBytes();
}

public long getPreLatestBlockHeight() { public long getPreLatestBlockHeight() {
return ledger.getLatestBlockHeight(); return ledger.getLatestBlockHeight();
} }


+ 13
- 0
source/peer/src/main/java/com/jd/blockchain/peer/consensus/ConsensusMessageDispatcher.java View File

@@ -75,6 +75,15 @@ public class ConsensusMessageDispatcher implements MessageHandle {


} }


@Override
public StateSnapshot getGenisStateSnapshot(String realmName) {
RealmProcessor realmProcessor = realmProcessorMap.get(realmName);
if (realmProcessor == null) {
throw new IllegalArgumentException("RealmName is not init!");
}
return realmProcessor.getGenisStateSnapshot();
}

@Override @Override
public AsyncFuture<byte[]> processOrdered(int messageId, byte[] message, String realmName, String batchId) { public AsyncFuture<byte[]> processOrdered(int messageId, byte[] message, String realmName, String batchId) {
// TODO 要求messageId在同一个批次不重复,但目前暂不验证 // TODO 要求messageId在同一个批次不重复,但目前暂不验证
@@ -209,6 +218,10 @@ public class ConsensusMessageDispatcher implements MessageHandle {
return new BlockStateSnapshot(((TransactionBatchProcessor)getTxBatchProcess()).getPreLatestBlockHeight(), ((TransactionBatchProcessor)getTxBatchProcess()).getPrevLatestBlockHash()); return new BlockStateSnapshot(((TransactionBatchProcessor)getTxBatchProcess()).getPreLatestBlockHeight(), ((TransactionBatchProcessor)getTxBatchProcess()).getPrevLatestBlockHash());
} }


public StateSnapshot getGenisStateSnapshot() {
return new BlockStateSnapshot(0, ((TransactionBatchProcessor)getTxBatchProcess()).getGenisBlockHash());
}

public AsyncFuture<byte[]> schedule(TransactionRequest txRequest) { public AsyncFuture<byte[]> schedule(TransactionRequest txRequest) {
CompletableAsyncFuture<byte[]> asyncTxResult = new CompletableAsyncFuture<>(); CompletableAsyncFuture<byte[]> asyncTxResult = new CompletableAsyncFuture<>();
TransactionResponse resp = getTxBatchProcess().schedule(txRequest); TransactionResponse resp = getTxBatchProcess().schedule(txRequest);


Loading…
Cancel
Save