use of io.nuls.core.chain.entity.Transaction in project nuls by nuls-io.
the class GetTxGroupHandler method getTxList.
private List<Transaction> getTxList(Block block, List<NulsDigestData> txHashList) {
List<Transaction> txList = new ArrayList<>();
Map<String, Integer> allTxMap = new HashMap<>();
for (int i = 0; i < block.getHeader().getTxCount(); i++) {
Transaction tx = block.getTxs().get(i);
allTxMap.put(tx.getHash().getDigestHex(), i);
}
for (NulsDigestData hash : txHashList) {
txList.add(block.getTxs().get(allTxMap.get(hash.getDigestHex())));
}
if (txList.size() != txHashList.size()) {
throw new NulsRuntimeException(ErrorCode.DATA_ERROR);
}
return txList;
}
use of io.nuls.core.chain.entity.Transaction 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.Transaction in project nuls by nuls-io.
the class PocConsensusResource method exitConsensus.
@POST
@Path("/withdraw")
@Produces(MediaType.APPLICATION_JSON)
public RpcResult exitConsensus(WithdrawForm form) {
AssertUtil.canNotEmpty(form);
AssertUtil.canNotEmpty(form.getTxHash());
AssertUtil.canNotEmpty(form.getPassword());
AssertUtil.canNotEmpty(form.getAddress());
Map<String, Object> params = new HashMap<>();
params.put("txHash", form.getTxHash());
Transaction tx = null;
try {
tx = consensusService.stopConsensus(form.getAddress(), form.getPassword(), params);
} catch (NulsException e) {
Log.error(e);
return RpcResult.getFailed(e.getMessage());
} catch (IOException e) {
Log.error(e);
return RpcResult.getFailed(e.getMessage());
}
return RpcResult.getSuccess().setData(tx.getHash().getDigestHex());
}
use of io.nuls.core.chain.entity.Transaction in project nuls by nuls-io.
the class TransactionResource method list.
@GET
@Path("/list")
@Produces(MediaType.APPLICATION_JSON)
public RpcResult list(@QueryParam("blockHeight") Long blockHeight, @QueryParam("address") String address, @QueryParam("type") int type, @QueryParam("pageNumber") int pageNumber, @QueryParam("pageSize") int pageSize) {
if (blockHeight == null && StringUtils.isBlank(address) && type == 0) {
return RpcResult.getFailed(ErrorCode.PARAMETER_ERROR);
}
if ((blockHeight != null && blockHeight < 0) || type < 0 || pageNumber < 0 || pageSize < 0) {
return RpcResult.getFailed(ErrorCode.PARAMETER_ERROR);
}
if ((pageNumber == 0 && pageSize > 0) || (pageNumber > 0 && pageSize == 0)) {
return RpcResult.getFailed(ErrorCode.PARAMETER_ERROR);
}
try {
RpcResult result = RpcResult.getSuccess();
Page<Transaction> pages = new Page<>();
if (StringUtils.isBlank(address)) {
pages = ledgerService.getTxList(blockHeight, type, pageNumber, pageSize);
} else if (Address.validAddress(address)) {
long count = ledgerService.getTxCount(blockHeight, address, type);
if (count < (pageNumber - 1) * pageSize) {
Page page = new Page(pageNumber, pageSize);
return result.setData(page);
}
if (pageSize > 0) {
pages.setPageNumber(pageNumber);
pages.setPageSize(pageSize);
} else {
pages.setPageNumber(pageNumber);
pages.setPages((int) count);
}
pages.setTotal(count);
if (count == 0) {
return RpcResult.getSuccess().setData(pages);
}
List<Transaction> txList = ledgerService.getTxList(blockHeight, address, type, pageNumber, pageSize);
pages.setList(txList);
}
Page<TransactionDto> pageDto = new Page<>(pages);
List<TransactionDto> dtoList = new ArrayList<>();
for (Transaction tx : pages.getList()) {
dtoList.add(new TransactionDto(tx, address));
}
pageDto.setList(dtoList);
result.setData(pageDto);
return result;
} catch (Exception e) {
Log.error(e);
return RpcResult.getFailed(e.getMessage());
}
}
use of io.nuls.core.chain.entity.Transaction in project nuls by nuls-io.
the class TransactionResource method forwardTransaction.
@POST
@Produces(MediaType.APPLICATION_JSON)
public RpcResult forwardTransaction(TxForm form) {
Transaction tx = null;
try {
tx = form.getTx();
} catch (Exception e) {
Log.error(e);
}
if (tx == null) {
throw new NulsRuntimeException(ErrorCode.NULL_PARAMETER);
}
ValidateResult result = tx.verify();
if (result.isFailed() && ErrorCode.ORPHAN_TX != result.getErrorCode()) {
return RpcResult.getFailed(ErrorCode.DATA_ERROR);
}
TransactionEvent event = new TransactionEvent();
event.setEventBody(tx);
List<String> list = eventBroadcaster.broadcastAndCache(event, true);
return RpcResult.getSuccess().setData(list);
}
Aggregations