Skip to content

Commit

Permalink
refined TxPoolV1 for adjusting the object initialization position and…
Browse files Browse the repository at this point in the history
… map get/put
  • Loading branch information
AionJayT committed Feb 25, 2020
1 parent 1830321 commit d3fbb5e
Showing 1 changed file with 30 additions and 69 deletions.
99 changes: 30 additions & 69 deletions modTxPool/src/org/aion/txpool/v1/TxPoolV1.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ public TxPoolV1(Properties config) {
public List<PooledTransaction> add(List<PooledTransaction> list) {
Objects.requireNonNull(list);
if (list.isEmpty()) {
return new ArrayList<>();
return Collections.emptyList();
}

List<PooledTransaction> addedTransactions = new ArrayList<>();
lock.lock();
List<PooledTransaction> addedTransactions = new ArrayList<>();
try {
for (PooledTransaction poolTx : list) {

Expand Down Expand Up @@ -148,28 +148,18 @@ private PooledTransaction poolAdd(PooledTransaction poolTx) {

AionTransaction tx = poolTx.tx;
long txTime = TimeUnit.MICROSECONDS.toSeconds(tx.getTimeStampBI().longValue()) + transactionTimeout;
Set<ByteArrayWrapper> timeSet = timeView.get(txTime);
if (timeSet == null) {
timeSet = new LinkedHashSet<>();
}
Set<ByteArrayWrapper> timeSet = timeView.getOrDefault(txTime, new LinkedHashSet<>());
timeView.putIfAbsent(txTime, timeSet);
timeSet.add(txHash);
timeView.put(txTime, timeSet);

long txEnergyPrice = tx.getEnergyPrice();
Set<ByteArrayWrapper> feeSet = feeView.get(txEnergyPrice);
if (feeSet == null) {
feeSet = new LinkedHashSet<>();
}
Set<ByteArrayWrapper> feeSet = feeView.getOrDefault(txEnergyPrice, new LinkedHashSet<>());
feeView.putIfAbsent(txEnergyPrice, feeSet);
feeSet.add(txHash);
feeView.put(txEnergyPrice, feeSet);

SortedMap<BigInteger, ByteArrayWrapper> accountInfo =
accountView.get(tx.getSenderAddress());
if (accountInfo == null) {
accountInfo = new TreeMap<>();
}
SortedMap<BigInteger, ByteArrayWrapper> accountInfo = accountView.getOrDefault(tx.getSenderAddress(), new TreeMap<>());
accountView.putIfAbsent(tx.getSenderAddress(), accountInfo);
accountInfo.put(tx.getNonceBI(), txHash);
accountView.put(tx.getSenderAddress(), accountInfo);

LOG_TXPOOL.debug("Added tx[{}]", poolTx.tx);
return poolTx;
Expand All @@ -186,48 +176,23 @@ private PooledTransaction poolRemove(ByteArrayWrapper txHash) {

long time = TimeUnit.MICROSECONDS.toSeconds(removedTx.tx.getTimeStampBI().longValue()) + transactionTimeout;
Set<ByteArrayWrapper> timeSet = timeView.get(time);
if (timeSet == null) {
throw new IllegalStateException("the timeView data has broken!, cannot find the data relate with time:" + time);
} else if (!timeSet.remove(txHash)) {
throw new IllegalStateException("the timeView data has broken, cannot remove txHash:" + txHash);
} else {
if (timeSet.isEmpty()) {
timeView.remove(time);
} else {
timeView.put(time, timeSet);
}
timeSet.remove(txHash);
if (timeSet.isEmpty()) {
timeView.remove(time);
}

Set<ByteArrayWrapper> feeSet = feeView.get(removedTx.tx.getEnergyPrice());
if (feeSet == null) {
throw new IllegalStateException("the feeView data has broken!, cannot find the data relate with fee:" + removedTx.tx.getEnergyPrice());
} else if (!feeSet.remove(txHash)) {
throw new IllegalStateException("the feeView data has broken, cannot remove txHash:" + txHash);
} else {
if (feeSet.isEmpty()) {
feeView.remove(removedTx.tx.getEnergyPrice());
} else {
feeView.put(removedTx.tx.getEnergyPrice(), feeSet);
}
feeSet.remove(txHash);
if (feeSet.isEmpty()) {
feeView.remove(removedTx.tx.getEnergyPrice());
}

SortedMap<BigInteger, ByteArrayWrapper> accountMap =
SortedMap<BigInteger, ByteArrayWrapper> accountInfo =
accountView.get(removedTx.tx.getSenderAddress());
if (accountMap == null) {
throw new IllegalStateException("the accountView data has broken!, cannot find the data relate with the account:" + removedTx.tx.getSenderAddress());
}

ByteArrayWrapper removedTxHash = accountMap.remove(removedTx.tx.getNonceBI());
if (removedTxHash == null) {
throw new IllegalStateException("the accountView data has broken!, cannot find the tx nonce:" + removedTx.tx.getNonceBI() + " relate with account:" + removedTx.tx.getSenderAddress());
} else if (!removedTxHash.equals(txHash)) {
throw new IllegalStateException("the accountView data has broken!, removed hash doesn't match, removedTxHash:" + removedTxHash + " txHash:" + txHash);
} else {
if (accountMap.isEmpty()) {
accountView.remove(removedTx.tx.getSenderAddress());
} else {
accountView.put(removedTx.tx.getSenderAddress(), accountMap);
}
accountInfo.remove(removedTx.tx.getNonceBI());
if (accountInfo.isEmpty()) {
accountView.remove(removedTx.tx.getSenderAddress());
}

LOG_TXPOOL.debug("Removed tx[{}]", removedTx.tx);
Expand Down Expand Up @@ -282,8 +247,8 @@ public PooledTransaction add(PooledTransaction tx) {
public List<PooledTransaction> remove(List<PooledTransaction> tx) {
Objects.requireNonNull(tx);

List<PooledTransaction> removedTx = new ArrayList<>();
lock.lock();
List<PooledTransaction> removedTx = new ArrayList<>();
try {
for (PooledTransaction pTx : tx) {
ByteArrayWrapper txHash = ByteArrayWrapper.wrap(pTx.tx.getTransactionHash());
Expand Down Expand Up @@ -316,19 +281,17 @@ public PooledTransaction remove(PooledTransaction tx) {
*/
public List<PooledTransaction> removeTxsWithNonceLessThan(Map<AionAddress, BigInteger> accountsWithNonce) {
Objects.requireNonNull(accountsWithNonce);
Objects.requireNonNull(accountsWithNonce.entrySet());

if (accountsWithNonce.isEmpty()) {
return new ArrayList<>();
return Collections.emptyList();
}

lock.lock();
List<PooledTransaction> removedTransaction = new ArrayList<>();
List<ByteArrayWrapper> removeTxHash = new ArrayList<>();

lock.lock();
try {
for (Map.Entry<AionAddress, BigInteger> account : accountsWithNonce.entrySet()) {
Objects.requireNonNull(account.getKey());
Objects.requireNonNull(account.getValue());
if (accountView.containsKey(account.getKey())) {
SortedMap<BigInteger, ByteArrayWrapper> accountInfo = accountView.get(account.getKey());
removeTxHash.addAll(accountInfo.headMap(account.getValue()).values());
Expand Down Expand Up @@ -381,10 +344,10 @@ public boolean isFull() {
* @return the transactions ready to be seal into the new blocks.
*/
public List<AionTransaction> snapshot() {
AtomicLong cumulatedTxSize = new AtomicLong();
AtomicLong cumulatedTxEnergy = new AtomicLong();

lock.lock();
AtomicLong cumulatedTxSize = new AtomicLong();
AtomicLong cumulatedTxEnergy = new AtomicLong();
try {
if (poolTransactions.isEmpty()) {
return new ArrayList<>();
Expand Down Expand Up @@ -434,12 +397,8 @@ private List<AionTransaction> pickTransaction(
}

AionAddress sender = pendingTx.tx.getSenderAddress();
BigInteger bestPickingNonce = accountPickingInfo.get(sender);
if (bestPickingNonce == null) {
accountPickingInfo.put(sender, getAccountFirstPickingNonce(sender));
}
BigInteger currentAccountPickingNonce = accountPickingInfo.getOrDefault(sender, getAccountFirstPickingNonce(sender));

BigInteger currentAccountPickingNonce = accountPickingInfo.get(sender);
if (currentAccountPickingNonce.equals(pendingTx.tx.getNonceBI())) {
long txEncodedSize = pendingTx.tx.getEncoded().length;
long txEnergyConsumed =
Expand All @@ -452,7 +411,9 @@ private List<AionTransaction> pickTransaction(
.get()) {
pickedTx.add(pendingTx.tx);
pickedTxHash.add(hash);
accountPickingInfo.put(sender, currentAccountPickingNonce.add(BigInteger.ONE));

currentAccountPickingNonce = currentAccountPickingNonce.add(BigInteger.ONE);
accountPickingInfo.put(sender, currentAccountPickingNonce);
} else {
return pickedTx;
}
Expand All @@ -478,8 +439,8 @@ private BigInteger getAccountFirstPickingNonce(AionAddress sender) {
*/
public List<PooledTransaction> clearOutDateTransaction() {

List<PooledTransaction> clearedTransactions = new ArrayList<>();
lock.lock();
List<PooledTransaction> clearedTransactions = new ArrayList<>();
try {
Map<Long, Set<ByteArrayWrapper>> timeoutTxHashes = timeView.headMap(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));

Expand Down Expand Up @@ -582,9 +543,9 @@ public String getVersion() {
* @exception IllegalStateException if cannot find the transaction hash in the mainMap.
*/
public List<AionTransaction> snapshotAll() {
List<AionTransaction> allPoolTransactions = new ArrayList<>();

lock.lock();
List<AionTransaction> allPoolTransactions = new ArrayList<>();
try {
for (SortedMap<BigInteger, ByteArrayWrapper> txHashes : accountView.values()) {
for (ByteArrayWrapper hash : txHashes.values()) {
Expand Down

0 comments on commit d3fbb5e

Please sign in to comment.