use of org.aion.zero.types.AionTransaction in project aion by aionnetwork.
the class ApiAion method getTransactionByBlockHashAndIndex.
public AionTransaction getTransactionByBlockHashAndIndex(byte[] hash, long index) {
AionBlock pBlk = this.getBlockByHash(hash);
if (pBlk == null) {
if (LOG.isErrorEnabled()) {
LOG.error("ApiAion.getTransactionByBlockHashAndIndex - can't find the block by the block hash");
}
return null;
}
List<AionTransaction> txList = pBlk.getTransactionsList();
AionTransaction tx = txList.get((int) index);
if (tx == null) {
if (LOG.isErrorEnabled()) {
LOG.error("Can't find the transaction!");
}
return null;
}
TxRecpt receipt = this.getTransactionReceipt(tx.getHash());
// TODO
if (receipt == null) {
throw new NullPointerException();
}
tx.setBlockNumber(pBlk.getNumber());
tx.setBlockHash(pBlk.getHash());
tx.setTxIndexInBlock(index);
tx.setNrgConsume(receipt.nrgUsed);
return tx;
}
use of org.aion.zero.types.AionTransaction in project aion by aionnetwork.
the class PendingTxCache method addCacheTx.
List<AionTransaction> addCacheTx(AionTransaction tx) {
if (tx == null) {
throw new NullPointerException();
}
int txSize = tx.getEncoded().length;
if (isCacheMax(txSize)) {
if (LOG.isTraceEnabled()) {
LOG.trace("PendingTx reached the max Memory settings");
}
if (cacheTxMap.get(tx.getFrom()) == null) {
// no tx belong to the account, return directly
return Collections.singletonList(tx);
} else {
// calculate replaced nonce tx size
BigInteger nonce = tx.getNonceBI();
List<BigInteger> removeTx;
boolean findPosition = false;
removeTx = new ArrayList<>();
int tempCacheSize = currentSize.get();
if (cacheTxMap.get(tx.getFrom()).get(nonce) != null) {
// case 1: found tx has same nonce in the cachemap
removeTx.add(nonce);
int oldTxSize = cacheTxMap.get(tx.getFrom()).get(nonce).getEncoded().length;
tempCacheSize -= oldTxSize;
if (!isCacheMax(txSize - oldTxSize)) {
// case 1a: replace nonce within the cachelimit, replace it
findPosition = true;
} else {
// case 1b: replace nonce still over the cachelimit, replace it and find the best remove list
for (Map.Entry<BigInteger, AionTransaction> e : cacheTxMap.get(tx.getFrom()).descendingMap().entrySet()) {
if (e.getKey().compareTo(nonce) > 0) {
removeTx.add(e.getKey());
tempCacheSize -= e.getValue().getEncoded().length;
if (tempCacheSize + txSize < CacheMax) {
findPosition = true;
break;
}
}
}
}
} else {
// case 2: backward iterate the cache to remove bigger nonce tx until find the enough cache size
for (Map.Entry<BigInteger, AionTransaction> e : cacheTxMap.get(tx.getFrom()).descendingMap().entrySet()) {
if (e.getKey().compareTo(nonce) > 0) {
removeTx.add(e.getKey());
tempCacheSize -= e.getValue().getEncoded().length;
if (tempCacheSize + txSize < CacheMax) {
findPosition = true;
break;
}
}
}
}
if (findPosition) {
for (BigInteger bi : removeTx) {
cacheTxMap.get(tx.getFrom()).remove(bi);
}
cacheTxMap.get(tx.getFrom()).put(nonce, tx);
currentSize.set(tempCacheSize + txSize);
}
}
} else {
if (cacheTxMap.size() == cacheAccountLimit) {
// remove firstAccount in pendingTxCache
Iterator<Map.Entry<Address, TreeMap<BigInteger, AionTransaction>>> it = cacheTxMap.entrySet().iterator();
if (it.hasNext()) {
currentSize.addAndGet(-getAccountSize(it.next().getValue()));
it.remove();
}
}
cacheTxMap.computeIfAbsent(tx.getFrom(), k -> new TreeMap<>());
cacheTxMap.get(tx.getFrom()).put(tx.getNonceBI(), tx);
currentSize.addAndGet(txSize);
}
return new ArrayList<>(cacheTxMap.get(tx.getFrom()).values());
}
use of org.aion.zero.types.AionTransaction in project aion by aionnetwork.
the class PendingTxCache method flush.
public List<AionTransaction> flush(Map<Address, BigInteger> nonceMap) {
if (nonceMap == null) {
throw new NullPointerException();
}
List<AionTransaction> processableTx = new ArrayList<>();
for (Address addr : nonceMap.keySet()) {
BigInteger bn = nonceMap.get(addr);
if (LOG.isDebugEnabled()) {
LOG.debug("cacheTx.flush addr[{}] bn[{}] size[{}], cache_size[{}]", addr.toString(), bn.toString(), cacheTxMap.get(addr).size(), currentSize.get());
}
if (cacheTxMap.get(addr) != null) {
currentSize.addAndGet(-getAccountSize(cacheTxMap.get(addr)));
cacheTxMap.get(addr).headMap(bn).clear();
currentSize.addAndGet(getAccountSize(cacheTxMap.get(addr)));
if (LOG.isDebugEnabled()) {
LOG.debug("cacheTx.flush after addr[{}] size[{}], cache_size[{}]", addr.toString(), cacheTxMap.get(addr).size(), currentSize.get());
}
if (cacheTxMap.get(addr).get(bn) != null) {
processableTx.add(cacheTxMap.get(addr).get(bn));
}
}
}
return processableTx;
}
use of org.aion.zero.types.AionTransaction in project aion by aionnetwork.
the class AionPoW method createNewBlockTemplate.
/**
* Creates a new block template.
*/
protected synchronized void createNewBlockTemplate() {
if (!shutDown.get()) {
if (!config.getConsensus().getMining()) {
return;
}
// it be used in DDOS?
if (this.syncMgr.getNetworkBestBlockNumber() - blockchain.getBestBlock().getNumber() > syncLimit) {
return;
}
if (LOG.isDebugEnabled()) {
LOG.debug("Creating a new block template");
}
AionBlock bestBlock = blockchain.getBlockByNumber(blockchain.getBestBlock().getNumber());
List<AionTransaction> txs = pendingState.getPendingTransactions();
AionBlock newBlock = blockchain.createNewBlock(bestBlock, txs, false);
EventConsensus ev = new EventConsensus(EventConsensus.CALLBACK.ON_BLOCK_TEMPLATE);
ev.setFuncArgs(Collections.singletonList(newBlock));
eventMgr.newEvent(ev);
// update last timestamp
lastUpdate.set(System.currentTimeMillis());
}
}
use of org.aion.zero.types.AionTransaction in project aion by aionnetwork.
the class AionTransactionTest method testClone.
@Test
public void testClone() {
byte[] nonce = RandomUtils.nextBytes(16);
Address to = Address.wrap(RandomUtils.nextBytes(32));
byte[] value = RandomUtils.nextBytes(16);
byte[] data = RandomUtils.nextBytes(64);
long nrg = RandomUtils.nextLong(0, Long.MAX_VALUE);
long nrgPrice = RandomUtils.nextLong(0, Long.MAX_VALUE);
byte type = 1;
AionTransaction tx = new AionTransaction(nonce, to, value, data, nrg, nrgPrice, type);
tx.sign(ECKeyFac.inst().create());
AionTransaction tx2 = tx.clone();
assertTransactionEquals(tx, tx2);
}
Aggregations