use of io.nuls.core.chain.entity.NulsDigestData in project nuls by nuls-io.
the class SmallBlockHandler method onEvent.
@Override
public void onEvent(SmallBlockEvent event, String fromId) {
ValidateResult result = event.getEventBody().verify();
if (result.isFailed()) {
return;
}
temporaryCacheManager.cacheSmallBlock(event.getEventBody());
downloadDataUtils.removeSmallBlock(event.getEventBody().getBlockHash().getDigestHex());
GetTxGroupRequest request = new GetTxGroupRequest();
GetTxGroupParam param = new GetTxGroupParam();
param.setBlockHash(event.getEventBody().getBlockHash());
for (NulsDigestData hash : event.getEventBody().getTxHashList()) {
if (!receivedTxCacheManager.txExist(hash)) {
param.addHash(hash);
}
}
request.setEventBody(param);
this.eventBroadcaster.sendToNode(request, fromId);
}
use of io.nuls.core.chain.entity.NulsDigestData in project nuls by nuls-io.
the class BlockManager method rollbackTxList.
private void rollbackTxList(List<Transaction> txList, int start, int end) {
List<NulsDigestData> txHashList = new ArrayList<>();
for (int i = start; i <= end && i < txList.size(); i++) {
Transaction tx = txList.get(i);
if (tx.getStatus() == TxStatusEnum.AGREED) {
try {
ledgerService.rollbackTx(tx);
} catch (NulsException e) {
Log.error(e);
}
txHashList.add(tx.getHash());
}
}
confirmingTxCacheManager.removeTxList(txHashList);
}
use of io.nuls.core.chain.entity.NulsDigestData in project nuls by nuls-io.
the class GetBlocksHashHandler method onEvent.
@Override
public void onEvent(GetBlocksHashRequest event, String fromId) {
if (event.getEventBody().getEnd() > NulsContext.getInstance().getBestBlock().getHeader().getHeight()) {
return;
}
boolean b = event.getEventBody().getStart() == event.getEventBody().getEnd();
if (b) {
BlockHashResponse response = new BlockHashResponse();
Block block;
if (event.getEventBody().getEnd() <= 0) {
block = NulsContext.getInstance().getBestBlock();
} else {
block = blockService.getBlock(event.getEventBody().getEnd());
}
if (null == block) {
Log.warn("block can not get:" + event.getEventBody().getEnd());
return;
}
response.put(block.getHeader().getHeight(), block.getHeader().getHash());
sendResponse(response, fromId);
} else {
List<BlockHeader> list = this.blockService.getBlockHeaderList(event.getEventBody().getStart(), event.getEventBody().getEnd(), event.getEventBody().getSplit());
List<Long> resultHeightList = new ArrayList<>();
List<NulsDigestData> resultHashList = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
resultHeightList.add(list.get(i).getHeight());
resultHashList.add(list.get(i).getHash());
}
if (resultHeightList.isEmpty() || resultHeightList.get(resultHeightList.size() - 1) < event.getEventBody().getEnd()) {
Block block = this.blockService.getBlock(event.getEventBody().getEnd());
if (block == null) {
// todo why?
Log.warn("block can not get:" + event.getEventBody().getEnd());
return;
}
resultHeightList.add(block.getHeader().getHeight());
resultHashList.add(block.getHeader().getHash());
}
final int size = 50000;
for (int i = 0; i < resultHashList.size(); i += size) {
BlockHashResponse response = new BlockHashResponse();
int end = i + size;
if (end > resultHeightList.size()) {
end = resultHeightList.size();
}
response.setHeightList(resultHeightList.subList(i, end));
response.setHashList(resultHashList.subList(i, end));
sendResponse(response, fromId);
}
}
}
use of io.nuls.core.chain.entity.NulsDigestData in project nuls by nuls-io.
the class GetTxGroupHandler method getTxList.
private List<Transaction> getTxList(Block block, List<NulsDigestData> txHashList) {
List<Transaction> txList = new ArrayList<>();
Map<String, Integer> allTxMap = new HashMap<>();
for (int i = 0; i < block.getHeader().getTxCount(); i++) {
Transaction tx = block.getTxs().get(i);
allTxMap.put(tx.getHash().getDigestHex(), i);
}
for (NulsDigestData hash : txHashList) {
txList.add(block.getTxs().get(allTxMap.get(hash.getDigestHex())));
}
if (txList.size() != txHashList.size()) {
throw new NulsRuntimeException(ErrorCode.DATA_ERROR);
}
return txList;
}
use of io.nuls.core.chain.entity.NulsDigestData in project nuls by nuls-io.
the class HeaderHashValidator method validate.
@Override
public ValidateResult validate(BlockHeader data) {
ValidateResult result = ValidateResult.getSuccessResult();
NulsDigestData hash = data.getHash();
P2PKHScriptSig scriptSig = data.getScriptSig();
NulsDigestData cfmHash = null;
try {
BlockHeader newHeader = new BlockHeader();
newHeader.parse(data.serialize());
cfmHash = newHeader.getHash();
} catch (Exception e) {
Log.error(e);
} finally {
data.setScriptSig(scriptSig);
}
if (!cfmHash.getDigestHex().equals(hash.getDigestHex())) {
result = ValidateResult.getFailedResult(ERROR_MESSAGE);
}
return result;
}
Aggregations