use of io.nuls.core.validate.ValidateResult in project nuls by nuls-io.
the class ConsensusMeetingRunner method addOrphanTx.
private void addOrphanTx(List<Transaction> txList, long totalSize, PocMeetingMember self) {
if ((self.getPackTime() - TimeService.currentTimeMillis()) <= 100) {
return;
}
List<Transaction> orphanTxList = orphanTxCacheManager.getTxList();
if (null == orphanTxList || orphanTxList.isEmpty()) {
return;
}
txList.sort(TxTimeComparator.getInstance());
List<NulsDigestData> outHashList = new ArrayList<>();
for (Transaction tx : orphanTxList) {
if ((self.getPackTime() - TimeService.currentTimeMillis()) <= 100) {
break;
}
totalSize += tx.size();
if (totalSize >= PocConsensusConstant.MAX_BLOCK_SIZE) {
break;
}
ValidateResult result = tx.verify();
if (result.isFailed()) {
Log.error(result.getMessage());
continue;
}
try {
ledgerService.approvalTx(tx);
} catch (Exception e) {
Log.error(result.getMessage());
Log.error(e);
continue;
}
confirmingTxCacheManager.putTx(tx);
txList.add(tx);
outHashList.add(tx.getHash());
}
orphanTxCacheManager.removeTx(outHashList);
}
use of io.nuls.core.validate.ValidateResult 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.validate.ValidateResult in project nuls by nuls-io.
the class HeaderContinuityValidator method validate.
@Override
public ValidateResult validate(BlockHeader header) {
ValidateResult result = ValidateResult.getSuccessResult();
boolean failed = false;
do {
if (header.getHeight() == 0) {
failed = !header.getPreHash().equals(NulsDigestData.EMPTY_HASH);
break;
}
BlockHeader preHeader = null;
try {
preHeader = NulsContext.getServiceBean(BlockService.class).getBlockHeader(header.getHeight() - 1);
} catch (NulsException e) {
// todo
e.printStackTrace();
}
if (null == preHeader) {
break;
}
failed = !preHeader.getHash().equals(header.getPreHash());
if (failed) {
break;
}
BlockRoundData roundData = new BlockRoundData();
try {
roundData.parse(header.getExtend());
} catch (NulsException e) {
Log.error(e);
}
long shouldTime = roundData.getRoundStartTime() + roundData.getPackingIndexOfRound() * PocConsensusConstant.BLOCK_TIME_INTERVAL_SECOND * 1000;
// todo 3 seconds error
long difference = header.getTime() - shouldTime;
failed = difference > 3000 || difference < -3000;
if (failed) {
break;
}
} while (false);
if (failed) {
result = ValidateResult.getFailedResult(ERROR_MESSAGE);
}
return result;
}
use of io.nuls.core.validate.ValidateResult 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;
}
use of io.nuls.core.validate.ValidateResult in project nuls by nuls-io.
the class BlockFieldValidator method validate.
@Override
public ValidateResult validate(Block data) {
ValidateResult result = ValidateResult.getSuccessResult();
boolean failed = false;
do {
if (data == null) {
failed = true;
break;
}
if (data.getHeader() == null) {
failed = true;
break;
}
if (data.getTxs() == null || data.getTxs().isEmpty()) {
failed = true;
break;
}
if (data.getHeader().getTxCount() == 0 || data.getTxs().size() != data.getHeader().getTxCount()) {
failed = true;
break;
}
} while (false);
if (failed) {
result = ValidateResult.getFailedResult(ERROR_MESSAGE);
}
return result;
}
Aggregations