use of io.nuls.core.chain.entity.Transaction 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.chain.entity.Transaction 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.chain.entity.Transaction in project nuls by nuls-io.
the class BlockStorageService method getBlock.
public Block getBlock(String hash) throws Exception {
Block block = blockCacheManager.getBlock(hash);
if (null != block) {
return block;
}
BlockHeader header = getBlockHeader(hash);
if (null == header) {
return null;
}
List<Transaction> txList = null;
try {
txList = ledgerService.getTxList(header.getHeight());
} catch (Exception e) {
Log.error(e);
}
if (txList.size() != header.getTxCount()) {
System.out.println();
}
return fillBlock(header, txList);
}
use of io.nuls.core.chain.entity.Transaction in project nuls by nuls-io.
the class ExitConsensusTxService method onApproval.
@Override
public void onApproval(PocExitConsensusTransaction tx) throws NulsException {
Transaction joinTx = ledgerService.getTx(tx.getTxData());
if (joinTx == null) {
joinTx = ConfirmingTxCacheManager.getInstance().getTx(tx.getTxData());
}
if (joinTx.getType() == TransactionConstant.TX_TYPE_REGISTER_AGENT) {
RegisterAgentTransaction raTx = (RegisterAgentTransaction) joinTx;
manager.changeAgentStatusByHash(raTx.getTxData().getHexHash(), ConsensusStatusEnum.NOT_IN);
manager.changeDepositStatusByAgentHash(raTx.getTxData().getHexHash(), ConsensusStatusEnum.NOT_IN);
this.ledgerService.unlockTxApprove(tx.getTxData().getDigestHex());
return;
}
PocJoinConsensusTransaction pjcTx = (PocJoinConsensusTransaction) joinTx;
Consensus<Deposit> cd = pjcTx.getTxData();
manager.changeDepositStatus(cd.getHexHash(), ConsensusStatusEnum.NOT_IN);
this.ledgerService.unlockTxApprove(tx.getTxData().getDigestHex());
}
use of io.nuls.core.chain.entity.Transaction in project nuls by nuls-io.
the class ExitConsensusTxService method onCommit.
@Override
public void onCommit(PocExitConsensusTransaction tx) throws NulsException {
Transaction joinTx = ledgerService.getTx(tx.getTxData());
if (joinTx.getType() == TransactionConstant.TX_TYPE_REGISTER_AGENT) {
RegisterAgentTransaction raTx = (RegisterAgentTransaction) joinTx;
manager.delAgent(raTx.getTxData().getHexHash());
manager.delDepositByAgentHash(raTx.getTxData().getHexHash());
this.ledgerService.unlockTxSave(tx.getTxData().getDigestHex());
Map<String, Object> paramsMap = new HashMap<>();
paramsMap.put("agentHash", raTx.getTxData().getHexHash());
List<DepositPo> poList = depositDataService.getList(paramsMap);
for (DepositPo po : poList) {
this.ledgerService.unlockTxSave(po.getTxHash());
}
this.agentDataService.delete(raTx.getTxData().getHexHash());
this.depositDataService.deleteByAgentHash(raTx.getTxData().getHexHash());
return;
}
PocJoinConsensusTransaction pjcTx = (PocJoinConsensusTransaction) joinTx;
Consensus<Deposit> cd = pjcTx.getTxData();
manager.delDeposit(cd.getHexHash());
this.depositDataService.delete(cd.getHexHash());
this.ledgerService.unlockTxSave(tx.getTxData().getDigestHex());
}
Aggregations