use of io.nuls.core.exception.NulsRuntimeException in project nuls by nuls-io.
the class BlockServiceImpl method saveBlock.
@Override
@DbSession
public boolean saveBlock(Block block) throws IOException {
block.verifyWithException();
boolean b = false;
for (int x = 0; x < block.getHeader().getTxCount(); x++) {
Transaction tx = block.getTxs().get(x);
if (tx.getStatus() == TxStatusEnum.CACHED) {
b = true;
tx.setBlockHeight(block.getHeader().getHeight());
try {
ledgerService.approvalTx(tx);
} catch (Exception e) {
Log.error(e);
rollback(block.getTxs(), x);
throw new NulsRuntimeException(e);
}
}
}
if (b) {
block.verifyWithException();
}
for (int x = 0; x < block.getHeader().getTxCount(); x++) {
Transaction tx = block.getTxs().get(x);
if (tx.getStatus() == TxStatusEnum.AGREED) {
tx.setBlockHeight(block.getHeader().getHeight());
try {
ledgerService.commitTx(tx);
} catch (Exception e) {
Log.error(e);
rollback(block.getTxs(), x);
throw new NulsRuntimeException(e);
}
}
}
ledgerService.saveTxList(block.getTxs());
blockStorageService.save(block);
return true;
}
use of io.nuls.core.exception.NulsRuntimeException 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.NulsRuntimeException 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.NulsRuntimeException in project nuls by nuls-io.
the class ConsensusMeetingRunner method calcRound.
private PocMeetingRound calcRound() {
PocMeetingRound round = new PocMeetingRound(this.consensusManager.getCurrentRound());
Block bestBlock = context.getBestBlock();
BlockRoundData lastRoundData;
try {
lastRoundData = new BlockRoundData(bestBlock.getHeader().getExtend());
} catch (NulsException e) {
Log.error(e);
throw new NulsRuntimeException(e);
}
if (round.getPreviousRound() == null || round.getPreviousRound().getIndex() <= lastRoundData.getRoundIndex()) {
while (true) {
if (lastRoundData.getPackingIndexOfRound() == lastRoundData.getConsensusMemberCount() || lastRoundData.getRoundEndTime() <= TimeService.currentTimeMillis()) {
break;
}
try {
bestBlock = context.getBestBlock();
lastRoundData = new BlockRoundData(bestBlock.getHeader().getExtend());
} catch (NulsException e) {
Log.error(e);
}
try {
Thread.sleep(100L);
} catch (InterruptedException e) {
Log.error(e);
}
}
PocMeetingRound preRound = new PocMeetingRound(null);
preRound.setIndex(lastRoundData.getRoundIndex());
preRound.setStartTime(lastRoundData.getRoundStartTime());
preRound.setMemberCount(lastRoundData.getConsensusMemberCount());
round.setPreviousRound(preRound);
}
round.setStartTime(round.getPreviousRound().getEndTime());
round.setIndex(lastRoundData.getRoundIndex() + 1);
return round;
}
use of io.nuls.core.exception.NulsRuntimeException in project nuls by nuls-io.
the class DistributedBlockInfoRequestUtils method calc.
private void calc() {
if (null == nodeIdList || nodeIdList.isEmpty()) {
throw new NulsRuntimeException(ErrorCode.FAILED, "success list of nodes is empty!");
}
int size = nodeIdList.size();
int halfSize = (size + 1) / 2;
//
if (hashesMap.size() < halfSize) {
return;
}
BlockInfo result = null;
for (String key : calcMap.keySet()) {
List<String> nodes = calcMap.get(key);
if (nodes == null) {
continue;
}
// todo =
if (nodes.size() >= halfSize) {
result = new BlockInfo();
BlockHashResponse response = hashesMap.get(nodes.get(0));
if (response == null || response.getHeightList() == null) {
// todo check it
continue;
}
Long bestHeight = 0L;
NulsDigestData bestHash = null;
for (int i = 0; i < response.getHeightList().size(); i++) {
Long height = response.getHeightList().get(i);
NulsDigestData hash = response.getHashList().get(i);
if (height >= bestHeight) {
bestHash = hash;
bestHeight = height;
}
result.putHash(height, hash);
}
result.setBestHash(bestHash);
result.setBestHeight(bestHeight);
result.setNodeIdList(nodes);
result.setFinished(true);
break;
}
}
if (null != result) {
bestBlockInfo = result;
}
// else if (size == calcMap.size()) {
// try {
// Thread.sleep(2000L);
// } catch (InterruptedException e) {
// Log.error(e);
// }
// try {
// this.request(start, end, split);
// } catch (Exception e) {
// Log.error(e.getMessage());
// }
// }
}
Aggregations