use of io.nuls.core.exception.NulsException in project nuls by nuls-io.
the class BlockServiceImpl method getBlockList.
@Override
public List<Block> getBlockList(long startHeight, long endHeight) throws NulsException {
List<Block> blockList = blockStorageService.getBlockList(startHeight, endHeight);
if (blockList.size() < (endHeight - startHeight + 1)) {
long currentMaxHeight = blockList.get(blockList.size() - 1).getHeader().getHeight();
while (currentMaxHeight < endHeight) {
long next = currentMaxHeight + 1;
Block block = blockManager.getBlock(next);
if (null == block) {
try {
block = blockStorageService.getBlock(next);
} catch (Exception e) {
Log.error(e);
}
}
if (null != block) {
blockList.add(block);
}
}
}
Collections.sort(blockList, BlockHeightComparator.getInstance());
return blockList;
}
use of io.nuls.core.exception.NulsException in project nuls by nuls-io.
the class BlockStorageService method getBlockList.
public List<Block> getBlockList(long startHeight, long endHeight) throws NulsException {
List<Block> blockList = new ArrayList<>();
List<BlockHeaderPo> poList = headerDao.getHeaderList(startHeight, endHeight);
List<Long> heightList = new ArrayList<>();
if (!poList.isEmpty()) {
List<Transaction> txList = null;
try {
txList = ledgerService.getTxList(startHeight, endHeight);
} catch (Exception e) {
Log.error(e);
}
Map<Long, List<Transaction>> txListGroup = txListGrouping(txList);
for (BlockHeaderPo po : poList) {
BlockHeader header = null;
try {
header = ConsensusTool.fromPojo(po);
} catch (NulsException e) {
throw e;
}
heightList.add(header.getHeight());
blockList.add(fillBlock(header, txListGroup.get(header.getHeight())));
}
}
if ((endHeight - startHeight + 1) > blockList.size()) {
for (long i = startHeight; i <= endHeight; i++) {
if (heightList.contains(i)) {
continue;
}
try {
blockList.add(this.getBlock(i));
} catch (Exception e) {
Log.error(e);
}
}
}
return blockList;
}
use of io.nuls.core.exception.NulsException in project nuls by nuls-io.
the class PocConsensusServiceImpl method joinTheConsensus.
private Transaction joinTheConsensus(Account account, String password, long amount, String agentHash) throws IOException, NulsException {
AssertUtil.canNotEmpty(account);
AssertUtil.canNotEmpty(password);
if (amount < PocConsensusConstant.ENTRUSTER_DEPOSIT_LOWER_LIMIT.getValue()) {
throw new NulsRuntimeException(ErrorCode.NULL_PARAMETER);
}
AssertUtil.canNotEmpty(agentHash);
TransactionEvent event = new TransactionEvent();
Consensus<Deposit> ca = new ConsensusDepositImpl();
ca.setAddress(account.getAddress().toString());
Deposit deposit = new Deposit();
deposit.setAgentHash(agentHash);
deposit.setDeposit(Na.valueOf(amount));
deposit.setStartTime(TimeService.currentTimeMillis());
ca.setExtend(deposit);
CoinTransferData data = new CoinTransferData(OperationType.LOCK, this.ledgerService.getTxFee(TransactionConstant.TX_TYPE_JOIN_CONSENSUS));
data.setTotalNa(deposit.getDeposit());
data.addFrom(account.getAddress().toString());
Coin coin = new Coin();
coin.setUnlockHeight(0);
coin.setUnlockTime(0);
coin.setNa(deposit.getDeposit());
data.addTo(account.getAddress().toString(), coin);
PocJoinConsensusTransaction tx = null;
try {
tx = new PocJoinConsensusTransaction(data, password);
} catch (NulsException e) {
throw new NulsRuntimeException(e);
}
tx.setTime(TimeService.currentTimeMillis());
tx.setTxData(ca);
tx.setHash(NulsDigestData.calcDigestData(tx.serialize()));
tx.setScriptSig(accountService.createP2PKHScriptSigFromDigest(tx.getHash(), account, password).serialize());
tx.verifyWithException();
event.setEventBody(tx);
List<String> nodeList = eventBroadcaster.broadcastAndCache(event, true);
if (null == nodeList || nodeList.isEmpty()) {
throw new NulsRuntimeException(ErrorCode.FAILED, "broadcast transaction failed!");
}
return tx;
}
use of io.nuls.core.exception.NulsException in project nuls by nuls-io.
the class BlockMaintenanceThread method getBestCorrectBlock.
private BestCorrectBlock getBestCorrectBlock() {
BestCorrectBlock resultCorrentInfo = new BestCorrectBlock();
Block localBestBlock = this.blockService.getLocalBestBlock();
do {
if (null == localBestBlock || localBestBlock.getHeader().getHeight() <= 1) {
break;
}
BlockInfo netBestBlockInfo = DistributedBlockInfoRequestUtils.getInstance().request(0, null);
resultCorrentInfo.setNetBestBlockInfo(netBestBlockInfo);
if (null == netBestBlockInfo || netBestBlockInfo.getBestHash() == null) {
break;
}
// same to network nodes
if (netBestBlockInfo.getBestHeight() == localBestBlock.getHeader().getHeight() && netBestBlockInfo.getBestHash().equals(localBestBlock.getHeader().getHash())) {
break;
} else if (netBestBlockInfo.getBestHeight() <= localBestBlock.getHeader().getHeight()) {
if (netBestBlockInfo.getBestHeight() == 0) {
break;
}
// local height is highest
BlockHeader header = null;
try {
header = blockService.getBlockHeader(netBestBlockInfo.getBestHeight());
} catch (NulsException e) {
break;
}
if (null != header && header.getHash().equals(netBestBlockInfo.getBestHash())) {
break;
}
if (netBestBlockInfo.getNodeIdList().size() == 1) {
throw new NulsRuntimeException(ErrorCode.FAILED, "node count not enough!");
}
Log.warn("Rollback block start height:{},local is highest and wrong!", localBestBlock.getHeader().getHeight());
// bifurcation
rollbackBlock(localBestBlock.getHeader().getHeight());
localBestBlock = this.blockService.getLocalBestBlock();
break;
} else {
netBestBlockInfo = DistributedBlockInfoRequestUtils.getInstance().request(localBestBlock.getHeader().getHeight(), netBestBlockInfo.getNodeIdList());
if (netBestBlockInfo.getBestHash().equals(localBestBlock.getHeader().getHash())) {
break;
}
if (localBestBlock.getHeader().getHeight() != netBestBlockInfo.getBestHeight()) {
throw new NulsRuntimeException(ErrorCode.FAILED, "answer not asked!");
}
if (netBestBlockInfo.getNodeIdList().size() == 1) {
throw new NulsRuntimeException(ErrorCode.FAILED, "node count not enough!");
}
Log.warn("Rollback block start height:{},local has wrong blocks!", localBestBlock.getHeader().getHeight());
// bifurcation
rollbackBlock(localBestBlock.getHeader().getHeight());
localBestBlock = this.blockService.getLocalBestBlock();
}
} while (false);
resultCorrentInfo.setLocalBestBlock(localBestBlock);
return resultCorrentInfo;
}
use of io.nuls.core.exception.NulsException in project nuls by nuls-io.
the class BlockMaintenanceThread method rollbackBlock.
private void rollbackBlock(long startHeight) {
long height = startHeight - 1;
Block block = this.blockService.getBlock(height);
try {
this.blockService.rollbackBlock(startHeight);
NulsContext.getInstance().setBestBlock(block);
} catch (NulsException e) {
Log.error(e);
return;
}
boolean previousRb = false;
if (height > 0) {
NulsContext.getInstance().setBestBlock(block);
BlockInfo blockInfo = DistributedBlockInfoRequestUtils.getInstance().request(height, null);
if (null != blockInfo && null != blockInfo.getBestHash() && (!blockInfo.getBestHash().equals(block.getHeader().getHash()) && blockInfo.getBestHeight() == height)) {
previousRb = true;
}
}
if (previousRb) {
rollbackBlock(height);
}
}
Aggregations