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);
}
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;
}
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();
}
}
}
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;
}
}
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();
}
Aggregations