Skip to content

Commit

Permalink
Merge pull request #145 from aionnetwork/issue124
Browse files Browse the repository at this point in the history
Issue124
  • Loading branch information
iamyulong authored Mar 7, 2018
2 parents 1912045 + dfffa76 commit 6d200fe
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public int getPendingTxSize() {
}

@Override
public synchronized List<AionTransaction> getPendingTransactions() {
public List<AionTransaction> getPendingTransactions() {
return this.txPool.snapshot();
}

Expand Down Expand Up @@ -353,70 +353,70 @@ private IAionBlock findCommonAncestor(IAionBlock b1, IAionBlock b2) {

@Override
public void processBest(AionBlock newBlock, List receipts) {
synchronized (this) {
if (getBestBlock() != null && !getBestBlock().isParentOf(newBlock)) {

// need to switch the state to another fork
if (getBestBlock() != null && !getBestBlock().isParentOf(newBlock)) {

IAionBlock commonAncestor = findCommonAncestor(getBestBlock(), newBlock);
// need to switch the state to another fork

if (LOG.isDebugEnabled()) {
LOG.debug("New best block from another fork: " + newBlock.getShortDescr() + ", old best: "
+ getBestBlock().getShortDescr() + ", ancestor: " + commonAncestor.getShortDescr());
}

// first return back the transactions from forked blocks
IAionBlock rollback = getBestBlock();
while (!rollback.isEqual(commonAncestor)) {

List<AionTransaction> atl = rollback.getTransactionsList();
if (!atl.isEmpty()) {
this.txPool.add(atl);
}
IAionBlock commonAncestor = findCommonAncestor(getBestBlock(), newBlock);

rollback = blockchain.getBlockByHash(rollback.getParentHash());
}
if (LOG.isDebugEnabled()) {
LOG.debug("New best block from another fork: " + newBlock.getShortDescr() + ", old best: "
+ getBestBlock().getShortDescr() + ", ancestor: " + commonAncestor.getShortDescr());
}

// rollback the state snapshot to the ancestor
pendingState = repository.getSnapshotTo(commonAncestor.getStateRoot()).startTracking();
// first return back the transactions from forked blocks
IAionBlock rollback = getBestBlock();
while (!rollback.isEqual(commonAncestor)) {

// next process blocks from new fork
IAionBlock main = newBlock;
List<IAionBlock> mainFork = new ArrayList<>();
while (!main.isEqual(commonAncestor)) {
mainFork.add(main);
main = blockchain.getBlockByHash(main.getParentHash());
List<AionTransaction> atl = rollback.getTransactionsList();
if (!atl.isEmpty()) {
this.txPool.add(atl);
}

// processing blocks from ancestor to new block
for (int i = mainFork.size() - 1; i >= 0; i--) {
processBestInternal(mainFork.get(i), null);
}
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("PendingStateImpl.processBest: " + newBlock.getShortDescr());
}
processBestInternal(newBlock, receipts);
rollback = blockchain.getBlockByHash(rollback.getParentHash());
}

best = newBlock;
// rollback the state snapshot to the ancestor
pendingState = repository.getSnapshotTo(commonAncestor.getStateRoot()).startTracking();

if (LOG.isTraceEnabled()) {
LOG.trace("PendingStateImpl.processBest: updateState");
// next process blocks from new fork
IAionBlock main = newBlock;
List<IAionBlock> mainFork = new ArrayList<>();
while (!main.isEqual(commonAncestor)) {
mainFork.add(main);
main = blockchain.getBlockByHash(main.getParentHash());
}
updateState(best);

if (LOG.isTraceEnabled()) {
LOG.trace("PendingStateImpl.processBest: nonceMgr.flush()");
// processing blocks from ancestor to new block
for (int i = mainFork.size() - 1; i >= 0; i--) {
processBestInternal(mainFork.get(i), null);
}
nonceMgr.flush();

if (LOG.isTraceEnabled()) {
LOG.trace("PendingStateImpl.processBest: txPool.updateBlkNrgLimit");
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("PendingStateImpl.processBest: " + newBlock.getShortDescr());
}
txPool.updateBlkNrgLimit(best.getNrgLimit());
processBestInternal(newBlock, receipts);
}

best = newBlock;

if (LOG.isTraceEnabled()) {
LOG.trace("PendingStateImpl.processBest: updateState");
}
updateState(best);

if (LOG.isTraceEnabled()) {
LOG.trace("PendingStateImpl.processBest: nonceMgr.flush()");
}
nonceMgr.flush();

if (LOG.isTraceEnabled()) {
LOG.trace("PendingStateImpl.processBest: txPool.updateBlkNrgLimit");
}

txPool.updateBlkNrgLimit(best.getNrgLimit());

IEvent evtChange = new EventTx(EventTx.CALLBACK.PENDINGTXSTATECHANGE0);
this.evtMgr.newEvent(evtChange);
}
Expand Down Expand Up @@ -579,7 +579,7 @@ public List<AionTransaction> newTransactions(List<AionTransaction> txSet) {
* account address
* @return transaction nonce.
*/
public synchronized Map.Entry<BigInteger, BigInteger> bestNonceSet(Address addr) {
public Map.Entry<BigInteger, BigInteger> bestNonceSet(Address addr) {
return this.txPool.bestNonceSet(addr);
}

Expand Down
7 changes: 1 addition & 6 deletions modAionImpl/src/org/aion/zero/impl/pow/AionPoW.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,9 @@ public void onPendingTxReceived(ITransaction tx) {
* @param solution
* The generated equihash solution
*/
protected synchronized void processSolution(Solution solution) {
protected void processSolution(Solution solution) {
if (!shutDown.get()) {
if (LOG.isDebugEnabled()) {
LOG.debug("New solution: block hash [{}]", Hex.toHexString(solution.getBlock().getHash()));
LOG.debug("Best block num [{}]", blockchain.getBestBlock().getNumber());
LOG.debug("Best block nonce [{}]", Hex.toHexString(blockchain.getBestBlock().getNonce()));
LOG.debug("Best block hash [{}]", Hex.toHexString(blockchain.getBestBlock().getHash()));
Expand Down Expand Up @@ -242,10 +241,6 @@ protected synchronized void createNewBlockTemplate() {
return;
}

if (LOG.isDebugEnabled()) {
LOG.debug("New template: block hash [{}]", Hex.toHexString(newBlock.getHash()));
}

EventConsensus ev = new EventConsensus(EventConsensus.CALLBACK.ON_BLOCK_TEMPLATE);
ev.setFuncArgs(Collections.singletonList(newBlock));
eventMgr.newEvent(ev);
Expand Down
15 changes: 9 additions & 6 deletions modTxPoolImpl/src/org/aion/txpool/zero/TxPoolA0.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ public synchronized List<TX> remove(List<TX> txs) {
this.updateAccPoolState();
this.updateFeeMap();

if (LOG.isDebugEnabled()) {
LOG.debug("TxPoolA0.remove TX remove [{}] removed [{}]", txs.size(), removedTxl.size());
if (LOG.isInfoEnabled()) {
LOG.info("TxPoolA0.remove TX remove [{}] removed [{}]", txs.size(), removedTxl.size());
}

return removedTxl;
Expand Down Expand Up @@ -262,12 +262,10 @@ public synchronized List<TX> snapshot() {
int cnt_txSz = blkSizeLimit;
long cnt_nrg = blkNrgLimit.get();
Set<ByteArrayWrapper> snapshotSet = new HashSet<>();

for (Entry<BigInteger, Map<ByteArrayWrapper, TxDependList<ByteArrayWrapper>>> e : this.getFeeView()
.entrySet()) {

Map<ByteArrayWrapper, TxDependList<ByteArrayWrapper>> tpl = e.getValue();

for (Entry<ByteArrayWrapper, TxDependList<ByteArrayWrapper>> pair : tpl.entrySet()) {
// Check the small nonce tx must been picked before put the high nonce tx
ByteArrayWrapper dependTx = pair.getValue().getDependTx();
Expand Down Expand Up @@ -300,22 +298,27 @@ public synchronized List<TX> snapshot() {
LOG.error("Snapshot txn exception", ex.toString());
}

if (LOG.isInfoEnabled()) {
LOG.info("TxPoolA0.snapshot return [{}] TX", rtn.size());
}
return rtn;
}
} else {
if (LOG.isWarnEnabled()) {
LOG.warn("Reach blockLimit: txSize[{}], nrgConsume[{}], txn_number[{}]", cnt_txSz,
cnt_nrg, rtn.size());
}

return rtn;
}
}
}
}
}

if (LOG.isDebugEnabled()) {
LOG.debug("TxPoolA0.snapshot return [{}] TX", rtn.size());

if (LOG.isInfoEnabled()) {
LOG.info("TxPoolA0.snapshot return [{}] TX", rtn.size());
}

return rtn;
Expand Down

0 comments on commit 6d200fe

Please sign in to comment.