use of io.nuls.core.chain.entity.NulsDigestData in project nuls by nuls-io.
the class BlockHashResponse method size.
@Override
public int size() {
int size = 0;
size += Utils.sizeOfInt48();
size += Utils.sizeOfInt(heightList.size());
for (Long height : heightList) {
size += Utils.sizeOfLong(height);
}
size += Utils.sizeOfInt(hashList.size());
for (NulsDigestData hash : hashList) {
size += Utils.sizeOfNulsData(hash);
}
return size;
}
use of io.nuls.core.chain.entity.NulsDigestData in project nuls by nuls-io.
the class UtxoTransferTool method toInput.
public static UtxoInput toInput(UtxoInputPo po) {
UtxoInput input = new UtxoInput();
input.setTxHash(new NulsDigestData(Hex.decode(po.getTxHash())));
input.setIndex(po.getInIndex());
input.setFromHash(new NulsDigestData(Hex.decode(po.getFromHash())));
input.setFromIndex(po.getFromIndex());
UtxoOutput output = new UtxoOutput();
output.setTxHash(new NulsDigestData(Hex.decode(po.getFromOutPut().getTxHash())));
output.setIndex(po.getFromOutPut().getOutIndex());
output.setLockTime(po.getFromOutPut().getLockTime());
output.setValue(po.getFromOutPut().getValue());
output.setAddress(po.getFromOutPut().getAddress());
input.setFrom(output);
return input;
}
use of io.nuls.core.chain.entity.NulsDigestData in project nuls by nuls-io.
the class TxSignValidator method validate.
@Override
public ValidateResult validate(Transaction tx) {
byte[] scriptSig = tx.getScriptSig();
tx.setScriptSig(null);
NulsDigestData nulsDigestData;
try {
nulsDigestData = NulsDigestData.calcDigestData(tx.serialize());
} catch (Exception e) {
return ValidateResult.getFailedResult(ErrorCode.DATA_ERROR);
} finally {
tx.setScriptSig(scriptSig);
}
if (!Arrays.equals(nulsDigestData.getDigestBytes(), tx.getHash().getDigestBytes())) {
return ValidateResult.getFailedResult(ErrorCode.DATA_ERROR);
}
P2PKHScriptSig p2PKHScriptSig = null;
try {
p2PKHScriptSig = new NulsByteBuffer(scriptSig).readNulsData(new P2PKHScriptSig());
} catch (NulsException e) {
return ValidateResult.getFailedResult(ErrorCode.SIGNATURE_ERROR);
}
return p2PKHScriptSig.verifySign(tx.getHash());
}
use of io.nuls.core.chain.entity.NulsDigestData in project nuls by nuls-io.
the class ConsensusMeetingRunner method packing.
private void packing(PocMeetingMember self) throws NulsException, IOException {
Block bestBlock = context.getBestBlock();
List<Transaction> txList = txCacheManager.getTxList();
txList.sort(TxTimeComparator.getInstance());
BlockData bd = new BlockData();
bd.setHeight(bestBlock.getHeader().getHeight() + 1);
bd.setPreHash(bestBlock.getHeader().getHash());
BlockRoundData roundData = new BlockRoundData();
roundData.setRoundIndex(consensusManager.getCurrentRound().getIndex());
roundData.setConsensusMemberCount(consensusManager.getCurrentRound().getMemberCount());
roundData.setPackingIndexOfRound(self.getIndexOfRound());
roundData.setRoundStartTime(consensusManager.getCurrentRound().getStartTime());
bd.setRoundData(roundData);
List<Integer> outTxList = new ArrayList<>();
List<NulsDigestData> outHashList = new ArrayList<>();
List<NulsDigestData> hashList = new ArrayList<>();
long totalSize = 0L;
for (int i = 0; i < txList.size(); i++) {
if ((self.getPackTime() - TimeService.currentTimeMillis()) <= 100) {
break;
}
Transaction tx = txList.get(i);
totalSize += tx.size();
if (totalSize >= PocConsensusConstant.MAX_BLOCK_SIZE) {
break;
}
outHashList.add(tx.getHash());
ValidateResult result = tx.verify();
if (result.isFailed()) {
Log.error(result.getMessage());
outTxList.add(i);
continue;
}
try {
ledgerService.approvalTx(tx);
} catch (Exception e) {
Log.error(e);
outTxList.add(i);
continue;
}
confirmingTxCacheManager.putTx(tx);
}
txCacheManager.removeTx(hashList);
for (int i = outTxList.size() - 1; i >= 0; i--) {
txList.remove(i);
}
txCacheManager.removeTx(outHashList);
if (totalSize < PocConsensusConstant.MAX_BLOCK_SIZE) {
addOrphanTx(txList, totalSize, self);
}
addConsensusTx(bestBlock, txList, self);
bd.setTxList(txList);
Log.info("txCount:" + txList.size());
Block newBlock = ConsensusTool.createBlock(bd, consensusManager.getConsensusStatusInfo().getAccount());
ValidateResult result = newBlock.verify();
if (result.isFailed()) {
Log.warn("packing block error:" + result.getMessage());
for (Transaction tx : newBlock.getTxs()) {
if (tx.getType() == TransactionConstant.TX_TYPE_COIN_BASE) {
continue;
}
ledgerService.rollbackTx(tx);
}
return;
}
confirmingTxCacheManager.putTx(newBlock.getTxs().get(0));
blockManager.addBlock(newBlock, false, null);
BlockHeaderEvent event = new BlockHeaderEvent();
event.setEventBody(newBlock.getHeader());
eventBroadcaster.broadcastAndCache(event, false);
PackedBlockNotice notice = new PackedBlockNotice();
notice.setEventBody(newBlock.getHeader());
eventBroadcaster.publishToLocal(notice);
}
use of io.nuls.core.chain.entity.NulsDigestData in project nuls by nuls-io.
the class BlockBatchDownloadUtils method checkHash.
private Result checkHash() throws InterruptedException {
for (long i = currentRound.getStart(); i <= currentRound.getEnd(); i++) {
Block block = blockMap.get(i);
if (null == block) {
return Result.getFailed("data error");
}
if ((i - currentRound.getStart()) % DOWNLOAD_BLOCKS_PER_TIME == 0) {
NulsDigestData mustHash = blocksHash.getHash(block.getHeader().getHeight());
NulsDigestData hash = block.getHeader().getHash();
if (null == hash || null == mustHash || !mustHash.getDigestHex().equals(hash.getDigestHex())) {
failedExecute(block.getHeader().getHeight());
return Result.getFailed("hash wrong!");
}
}
String preHash = block.getHeader().getPreHash().getDigestHex();
Block preBlock = blockMap.get(block.getHeader().getHeight() - 1);
if (preBlock == null) {
preBlock = blockService.getBlock(preHash);
}
if (null == preBlock || preBlock.getHeader().getHeight() != (block.getHeader().getHeight() - 1)) {
failedExecute(block.getHeader().getHeight());
return Result.getFailed("prehash wrong!");
}
}
return Result.getSuccess();
}
Aggregations