use of io.nuls.core.chain.entity.BlockHeader 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.chain.entity.BlockHeader in project nuls by nuls-io.
the class HeaderPackerValidator method validate.
@Override
public ValidateResult validate(BlockHeader header) {
BlockHeader preHeader = null;
try {
preHeader = NulsContext.getServiceBean(BlockService.class).getBlockHeader(header.getPreHash());
} catch (NulsException e) {
// todo
}
PocMeetingRound currentRound = consensusManager.getCurrentRound();
if (header.getHeight() == 0) {
return ValidateResult.getSuccessResult();
}
if (preHeader == null) {
return ValidateResult.getSuccessResult();
}
BlockRoundData roundData = null;
try {
roundData = new BlockRoundData(preHeader.getExtend());
} catch (NulsException e) {
Log.error(e);
return ValidateResult.getFailedResult(e.getMessage());
}
if (null == currentRound) {
return ValidateResult.getSuccessResult();
}
if (currentRound.getIndex() == roundData.getRoundIndex() && currentRound.getMemberCount() > (roundData.getPackingIndexOfRound() + 1)) {
if (currentRound.indexOf(header.getPackingAddress()) <= roundData.getPackingIndexOfRound()) {
return ValidateResult.getFailedResult(ERROR_MESSAGE);
}
}
byte[] packingHash160 = AccountTool.getHash160ByAddress(header.getPackingAddress());
byte[] hash160InScriptSig = header.getScriptSig().getSignerHash160();
if (!Arrays.equals(packingHash160, hash160InScriptSig)) {
return ValidateResult.getFailedResult(ERROR_MESSAGE);
}
BlockRoundData nowRoundData = null;
try {
nowRoundData = new BlockRoundData(header.getExtend());
} catch (NulsException e) {
Log.error(e);
return ValidateResult.getFailedResult(ERROR_MESSAGE);
}
if (!isAdjacent(roundData, nowRoundData)) {
return ValidateResult.getFailedResult(ERROR_MESSAGE);
}
return ValidateResult.getSuccessResult();
}
use of io.nuls.core.chain.entity.BlockHeader in project nuls by nuls-io.
the class BlockCacheBuffer method getBlock.
public Block getBlock(String hash) {
if (null == txsCacheMap || headerCacheMap == null) {
return null;
}
List<Transaction> txs = txsCacheMap.get(hash);
BlockHeader header = headerCacheMap.get(hash);
if (null == header || null == txs || txs.isEmpty()) {
return null;
}
Block block = new Block();
block.setHeader(header);
block.setTxs(txs);
return block;
}
use of io.nuls.core.chain.entity.BlockHeader in project nuls by nuls-io.
the class BlockStorageService method save.
public void save(Block block) {
BlockHeader header = block.getHeader();
header.setSize(block.size());
headerDao.save(ConsensusTool.toPojo(header));
}
use of io.nuls.core.chain.entity.BlockHeader in project nuls by nuls-io.
the class BlockStorageService method getBlockHeaderList.
public List<BlockHeader> getBlockHeaderList(long startHeight, long endHeight, long split) {
List<BlockHeaderPo> strList = this.headerDao.getHashList(startHeight, endHeight, split);
Map<Long, BlockHeader> headerMap = new HashMap<>();
for (BlockHeaderPo po : strList) {
BlockHeader header = new BlockHeader();
header.setHash(NulsDigestData.fromDigestHex(po.getHash()));
header.setHeight(po.getHeight());
headerMap.put(po.getHeight(), header);
}
if ((endHeight - startHeight + 1) == headerMap.size()) {
return new ArrayList<>(headerMap.values());
}
List<BlockHeader> headerList = new ArrayList<>();
for (long i = startHeight; i <= endHeight; i++) {
if (headerMap.containsKey(i)) {
headerList.add(headerMap.get(i));
continue;
}
BlockHeader header = blockCacheManager.getBlockHeader(i);
if (null == header) {
Block block = blockCacheManager.getBlock(i);
if (null != block) {
header = block.getHeader();
}
}
if (null != header) {
headerList.add(header);
}
}
return headerList;
}
Aggregations