Search in sources :

Example 26 with Transaction

use of io.nuls.kernel.model.Transaction in project nuls by nuls-io.

the class LimitHashMapTest method test1.

@Test
public void test1() throws IOException {
    CacheMap<NulsDigestData, Transaction> map = new CacheMap<>("a-test", 128, NulsDigestData.class, Transaction.class);
    long use = 0;
    List<NulsDigestData> hashList = new ArrayList<>();
    for (int i = 0; i < 200000; i++) {
        Transaction transaction = new TransferTransaction();
        transaction.setTime(System.currentTimeMillis());
        transaction.setHash(NulsDigestData.calcDigestData(transaction.serializeForHash()));
        hashList.add(transaction.getHash());
        long start = System.nanoTime();
        map.put(transaction.getHash(), transaction);
        use += (System.nanoTime() - start);
    }
    System.out.println("插入20万条累计用时:" + use + "纳秒");
    long start = System.currentTimeMillis();
    for (NulsDigestData key : hashList) {
        map.get(key);
    }
    System.out.println("查询200000次用时:" + (System.currentTimeMillis() - start) + "ms");
    start = System.currentTimeMillis();
    for (NulsDigestData key : hashList) {
        map.containsKey(key);
    }
    System.out.println("判断是否包含200000次用时:" + (System.currentTimeMillis() - start) + "ms");
    start = System.currentTimeMillis();
    for (NulsDigestData key : hashList) {
        map.remove(key);
    }
    System.out.println("删除200000次用时:" + (System.currentTimeMillis() - start) + "ms");
    assertTrue(true);
}
Also used : Transaction(io.nuls.kernel.model.Transaction) TransferTransaction(io.nuls.protocol.model.tx.TransferTransaction) ArrayList(java.util.ArrayList) NulsDigestData(io.nuls.kernel.model.NulsDigestData) CacheMap(io.nuls.cache.CacheMap) TransferTransaction(io.nuls.protocol.model.tx.TransferTransaction) Test(org.junit.Test)

Example 27 with Transaction

use of io.nuls.kernel.model.Transaction in project nuls by nuls-io.

the class TxProcessTask method processTx.

private boolean processTx(Transaction tx, boolean isOrphanTx) {
    try {
        Result result = tx.verify();
        if (result.isFailed()) {
            return false;
        }
        Transaction tempTx = ledgerService.getTx(tx.getHash());
        if (tempTx != null) {
            return isOrphanTx;
        }
        ValidateResult validateResult = ledgerService.verifyCoinData(tx, temporaryToMap, temporaryFromSet);
        if (validateResult.isSuccess()) {
            pool.add(tx, false);
            List<Coin> fromCoins = tx.getCoinData().getFrom();
            for (Coin coin : fromCoins) {
                String key = LedgerUtil.asString(coin.getOwner());
                temporaryFromSet.remove(key);
                temporaryToMap.remove(key);
            }
            // count++;
            transactionCacheStorageService.putTx(tx);
            transactionService.forwardTx(tx, null);
            return true;
        } else if (validateResult.getErrorCode().equals(TransactionErrorCode.ORPHAN_TX) && !isOrphanTx) {
            processOrphanTx(tx);
        } else if (isOrphanTx) {
            return tx.getTime() < (TimeService.currentTimeMillis() - 3600000L);
        }
    } catch (Exception e) {
        Log.error(e);
    }
    return false;
}
Also used : Coin(io.nuls.kernel.model.Coin) Transaction(io.nuls.kernel.model.Transaction) ValidateResult(io.nuls.kernel.validate.ValidateResult) NulsException(io.nuls.kernel.exception.NulsException) ValidateResult(io.nuls.kernel.validate.ValidateResult) Result(io.nuls.kernel.model.Result)

Example 28 with Transaction

use of io.nuls.kernel.model.Transaction in project nuls by nuls-io.

the class TxProcessTask method doOrphanTxTask.

private void doOrphanTxTask() {
    orphanTxList.sort(txComparator);
    Iterator<Transaction> it = orphanTxList.iterator();
    while (it.hasNext()) {
        Transaction tx = it.next();
        boolean success = processTx(tx, true);
        if (success) {
            it.remove();
        }
    }
}
Also used : Transaction(io.nuls.kernel.model.Transaction)

Example 29 with Transaction

use of io.nuls.kernel.model.Transaction in project nuls by nuls-io.

the class BlockProcessTask method doTask.

private void doTask() {
    // wait consensus ready running
    if (ConsensusStatusContext.getConsensusStatus().ordinal() <= ConsensusStatus.LOADING_CACHE.ordinal()) {
        return;
    }
    BlockContainer blockContainer;
    while ((blockContainer = blockQueueProvider.get()) != null) {
        try {
            if (first) {
                List<Transaction> txList = blockContainer.getBlock().getTxs();
                for (int index = txList.size() - 1; index >= 0; index--) {
                    Transaction tx = blockContainer.getBlock().getTxs().get(index);
                    Transaction localTx = ledgerService.getTx(tx.getHash());
                    if (null == localTx || tx.getBlockHeight() != localTx.getBlockHeight()) {
                        continue;
                    }
                    try {
                        transactionService.rollbackTx(tx, blockContainer.getBlock().getHeader());
                    } catch (Exception e) {
                        Log.error(e);
                    }
                }
                first = false;
            }
            // long time = System.currentTimeMillis();
            blockProcess.addBlock(blockContainer);
        // Log.info("add 区块 " + blockContainer.getBlock().getHeader().getHeight() + " 耗时 " + (System.currentTimeMillis() - time) + " ms , tx count : " + blockContainer.getBlock().getHeader().getTxCount());
        } catch (Exception e) {
            e.printStackTrace();
            Log.error("add block fail , error : " + e.getMessage(), e);
        }
    }
    // 系统启动,本地高度和网络高度一致,不需要下载区块时,系统需要知道并设置共识状态为运行中
    if (downloadService.isDownloadSuccess().isSuccess() && ConsensusStatusContext.getConsensusStatus() == ConsensusStatus.WAIT_RUNNING && (blockContainer == null || blockContainer.getStatus() == BlockContainerStatus.RECEIVED)) {
        ConsensusStatusContext.setConsensusStatus(ConsensusStatus.RUNNING);
        NulsContext.WALLET_STATUS = NulsConstant.RUNNING;
    }
}
Also used : Transaction(io.nuls.kernel.model.Transaction) BlockContainer(io.nuls.consensus.poc.container.BlockContainer)

Example 30 with Transaction

use of io.nuls.kernel.model.Transaction in project nuls by nuls-io.

the class CancelDepositTxProcessor method conflictDetect.

@Override
public ValidateResult conflictDetect(List<Transaction> txList) {
    if (txList == null || txList.size() <= 1) {
        return ValidateResult.getSuccessResult();
    }
    Set<NulsDigestData> hashSet = new HashSet<>();
    Set<NulsDigestData> agentHashSet = new HashSet<>();
    Set<String> addressSet = new HashSet<>();
    for (Transaction tx : txList) {
        if (tx.getType() == ConsensusConstant.TX_TYPE_RED_PUNISH) {
            RedPunishTransaction transaction = (RedPunishTransaction) tx;
            addressSet.add(AddressTool.getStringAddressByBytes(transaction.getTxData().getAddress()));
        } else if (tx.getType() == ConsensusConstant.TX_TYPE_STOP_AGENT) {
            StopAgentTransaction transaction = (StopAgentTransaction) tx;
            agentHashSet.add(transaction.getTxData().getCreateTxHash());
        }
    }
    for (Transaction tx : txList) {
        if (tx.getType() == ConsensusConstant.TX_TYPE_CANCEL_DEPOSIT) {
            CancelDepositTransaction transaction = (CancelDepositTransaction) tx;
            if (!hashSet.add(transaction.getTxData().getJoinTxHash())) {
                return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TRANSACTION_REPEATED);
            }
            if (agentHashSet.contains(transaction.getTxData().getJoinTxHash())) {
                return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_STOPPED);
            }
            DepositPo depositPo = depositStorageService.get(transaction.getTxData().getJoinTxHash());
            if (depositPo.getDelHeight() > 0) {
                return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TRANSACTION_REPEATED);
            }
            AgentPo agentPo = agentStorageService.get(depositPo.getAgentHash());
            if (null == agentPo || agentPo.getDelHeight() > 0) {
                return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_NOT_EXIST);
            }
            if (addressSet.contains(AddressTool.getStringAddressByBytes(agentPo.getAgentAddress()))) {
                return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_PUNISHED);
            }
        }
    }
    return ValidateResult.getSuccessResult();
}
Also used : DepositPo(io.nuls.consensus.poc.storage.po.DepositPo) StopAgentTransaction(io.nuls.consensus.poc.protocol.tx.StopAgentTransaction) DepositTransaction(io.nuls.consensus.poc.protocol.tx.DepositTransaction) RedPunishTransaction(io.nuls.consensus.poc.protocol.tx.RedPunishTransaction) Transaction(io.nuls.kernel.model.Transaction) CancelDepositTransaction(io.nuls.consensus.poc.protocol.tx.CancelDepositTransaction) RedPunishTransaction(io.nuls.consensus.poc.protocol.tx.RedPunishTransaction) CancelDepositTransaction(io.nuls.consensus.poc.protocol.tx.CancelDepositTransaction) NulsDigestData(io.nuls.kernel.model.NulsDigestData) StopAgentTransaction(io.nuls.consensus.poc.protocol.tx.StopAgentTransaction) HashSet(java.util.HashSet) AgentPo(io.nuls.consensus.poc.storage.po.AgentPo)

Aggregations

Transaction (io.nuls.kernel.model.Transaction)38 NulsDigestData (io.nuls.kernel.model.NulsDigestData)12 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)8 NulsException (io.nuls.kernel.exception.NulsException)7 ValidateResult (io.nuls.kernel.validate.ValidateResult)7 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)6 IOException (java.io.IOException)5 HashSet (java.util.HashSet)5 Result (io.nuls.kernel.model.Result)4 TransferTransaction (io.nuls.protocol.model.tx.TransferTransaction)4 CreateAgentTransaction (io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction)3 RedPunishTransaction (io.nuls.consensus.poc.protocol.tx.RedPunishTransaction)3 NulsByteBuffer (io.nuls.kernel.utils.NulsByteBuffer)3 UnconfirmedTxPo (io.nuls.account.ledger.storage.po.UnconfirmedTxPo)2 StopAgentTransaction (io.nuls.consensus.poc.protocol.tx.StopAgentTransaction)2 AgentPo (io.nuls.consensus.poc.storage.po.AgentPo)2 Coin (io.nuls.kernel.model.Coin)2 TransactionProcessor (io.nuls.kernel.processor.TransactionProcessor)2 Alias (io.nuls.account.model.Alias)1