Search in sources :

Example 11 with Transaction

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;
}
Also used : Transaction(io.nuls.core.chain.entity.Transaction) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 12 with Transaction

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;
}
Also used : Transaction(io.nuls.core.chain.entity.Transaction) Block(io.nuls.core.chain.entity.Block) BlockHeader(io.nuls.core.chain.entity.BlockHeader)

Example 13 with Transaction

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());
}
Also used : Transaction(io.nuls.core.chain.entity.Transaction) HashMap(java.util.HashMap) NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException)

Example 14 with Transaction

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());
    }
}
Also used : TransactionDto(io.nuls.rpc.entity.TransactionDto) Transaction(io.nuls.core.chain.entity.Transaction) RpcResult(io.nuls.rpc.entity.RpcResult) ArrayList(java.util.ArrayList) Page(io.nuls.core.dto.Page) ArrayList(java.util.ArrayList) List(java.util.List) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 15 with Transaction

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);
}
Also used : TransactionEvent(io.nuls.ledger.event.TransactionEvent) Transaction(io.nuls.core.chain.entity.Transaction) ValidateResult(io.nuls.core.validate.ValidateResult) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Aggregations

Transaction (io.nuls.core.chain.entity.Transaction)34 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)13 NulsException (io.nuls.core.exception.NulsException)12 Block (io.nuls.core.chain.entity.Block)8 HashMap (java.util.HashMap)7 NulsDigestData (io.nuls.core.chain.entity.NulsDigestData)6 ValidateResult (io.nuls.core.validate.ValidateResult)6 BlockHeader (io.nuls.core.chain.entity.BlockHeader)5 IOException (java.io.IOException)5 Deposit (io.nuls.consensus.entity.member.Deposit)4 PocExitConsensusTransaction (io.nuls.consensus.entity.tx.PocExitConsensusTransaction)4 PocJoinConsensusTransaction (io.nuls.consensus.entity.tx.PocJoinConsensusTransaction)4 RegisterAgentTransaction (io.nuls.consensus.entity.tx.RegisterAgentTransaction)4 ArrayList (java.util.ArrayList)4 RedPunishTransaction (io.nuls.consensus.entity.tx.RedPunishTransaction)3 YellowPunishTransaction (io.nuls.consensus.entity.tx.YellowPunishTransaction)3 NulsByteBuffer (io.nuls.core.utils.io.NulsByteBuffer)3 AbstractCoinTransaction (io.nuls.ledger.entity.tx.AbstractCoinTransaction)3 CoinBaseTransaction (io.nuls.ledger.entity.tx.CoinBaseTransaction)3 Account (io.nuls.account.entity.Account)2