use of io.nuls.core.exception.NulsException 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.exception.NulsException in project nuls by nuls-io.
the class ConsensusTool method toPojo.
public static final BlockHeaderPo toPojo(BlockHeader header) {
BlockHeaderPo po = new BlockHeaderPo();
po.setTxCount(header.getTxCount());
po.setPreHash(header.getPreHash().getDigestHex());
po.setMerkleHash(header.getMerkleHash().getDigestHex());
po.setHeight(header.getHeight());
po.setCreateTime(header.getTime());
po.setHash(header.getHash().getDigestHex());
po.setSize(header.getSize());
if (null != header.getScriptSig()) {
try {
po.setScriptSig(header.getScriptSig().serialize());
} catch (IOException e) {
Log.error(e);
}
}
po.setTxCount(header.getTxCount());
po.setConsensusAddress(header.getPackingAddress());
po.setExtend(header.getExtend());
BlockRoundData data = new BlockRoundData();
try {
data.parse(header.getExtend());
} catch (NulsException e) {
Log.error(e);
}
po.setRoundIndex(data.getRoundIndex());
return po;
}
use of io.nuls.core.exception.NulsException in project nuls by nuls-io.
the class PocConsensusServiceImpl method registerAgent.
private Transaction registerAgent(Agent agent, Account account, String password) throws IOException, NulsException {
TransactionEvent event = new TransactionEvent();
CoinTransferData data = new CoinTransferData(OperationType.LOCK, this.ledgerService.getTxFee(TransactionConstant.TX_TYPE_REGISTER_AGENT));
data.setTotalNa(agent.getDeposit());
data.addFrom(account.getAddress().toString());
Coin coin = new Coin();
coin.setUnlockHeight(0);
coin.setUnlockTime(0);
coin.setNa(agent.getDeposit());
data.addTo(account.getAddress().toString(), coin);
RegisterAgentTransaction tx = null;
try {
tx = new RegisterAgentTransaction(data, password);
} catch (NulsException e) {
Log.error(e);
throw new NulsRuntimeException(e);
}
Consensus<Agent> con = new ConsensusAgentImpl();
con.setAddress(account.getAddress().toString());
agent.setStartTime(TimeService.currentTimeMillis());
con.setExtend(agent);
tx.setTxData(con);
tx.setHash(NulsDigestData.calcDigestData(tx.serialize()));
tx.setScriptSig(accountService.createP2PKHScriptSigFromDigest(tx.getHash(), account, password).serialize());
tx.verifyWithException();
event.setEventBody(tx);
List<String> nodeList = eventBroadcaster.broadcastHashAndCache(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 PocConsensusServiceImpl method stopConsensus.
@Override
public Transaction stopConsensus(String address, String password, Map<String, Object> paramsMap) throws NulsException, IOException {
AbstractCoinTransaction joinTx = null;
if (null != paramsMap && StringUtils.isNotBlank((String) paramsMap.get("txHash"))) {
PocJoinConsensusTransaction tx = (PocJoinConsensusTransaction) ledgerService.getTx(NulsDigestData.fromDigestHex((String) paramsMap.get("txHash")));
joinTx = tx;
} else {
try {
List<Transaction> txlist = this.ledgerService.getTxList(address, TransactionConstant.TX_TYPE_REGISTER_AGENT);
if (null != txlist || !txlist.isEmpty()) {
joinTx = (AbstractCoinTransaction) txlist.get(0);
}
} catch (Exception e) {
Log.error(e);
}
}
if (null == joinTx) {
throw new NulsRuntimeException(ErrorCode.FAILED, "The related transaction is not exist!");
}
Account account = this.accountService.getAccount(address);
if (null == account) {
throw new NulsRuntimeException(ErrorCode.ACCOUNT_NOT_EXIST, "address:" + address.toString());
}
if (!account.validatePassword(password)) {
throw new NulsRuntimeException(ErrorCode.PASSWORD_IS_WRONG);
}
TransactionEvent event = new TransactionEvent();
CoinTransferData coinTransferData = new CoinTransferData(OperationType.UNLOCK, this.ledgerService.getTxFee(TransactionConstant.TX_TYPE_EXIT_CONSENSUS));
coinTransferData.setTotalNa(Na.ZERO);
PocExitConsensusTransaction tx = new PocExitConsensusTransaction(coinTransferData, password);
tx.setTxData(joinTx.getHash());
try {
tx.setHash(NulsDigestData.calcDigestData(tx.serialize()));
} catch (IOException e) {
Log.error(e);
throw new NulsRuntimeException(ErrorCode.HASH_ERROR, e);
}
tx.setScriptSig(accountService.createP2PKHScriptSigFromDigest(tx.getHash(), account, password).serialize());
event.setEventBody(tx);
eventBroadcaster.broadcastHashAndCache(event, true);
return tx;
}
use of io.nuls.core.exception.NulsException in project nuls by nuls-io.
the class Block method parse.
@Override
protected void parse(NulsByteBuffer byteBuffer) throws NulsException {
header = new BlockHeader();
header.parse(byteBuffer);
try {
txs = TransactionManager.getInstances(byteBuffer, header.getTxCount());
} catch (Exception e) {
throw new NulsRuntimeException(ErrorCode.PARSE_OBJECT_ERROR, e.getMessage());
}
}
Aggregations