Skip to content

Commit

Permalink
Merge pull request #640 from aionnetwork/monitoring-system
Browse files Browse the repository at this point in the history
Monitoring system
  • Loading branch information
AionJayT authored Sep 25, 2018
2 parents 31e7534 + 325d559 commit 4aa3ba8
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 21 deletions.
14 changes: 8 additions & 6 deletions modAionImpl/src/org/aion/zero/impl/AionBlockchainImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -474,13 +474,15 @@ private AionBlockSummary tryConnectAndFork(final AionBlock block) {

if (summary != null && isMoreThan(this.totalDifficulty, savedState.savedTD)) {

if (LOG.isInfoEnabled())
if (LOG.isInfoEnabled()) {
LOG.info(
"branching: from = {}/{}, to = {}/{}",
savedState.savedBest.getNumber(),
toHexString(savedState.savedBest.getHash()),
block.getNumber(),
toHexString(block.getHash()));
"branching: from = {}/{}, to = {}/{}",
savedState.savedBest.getNumber(),
toHexString(savedState.savedBest.getHash()),
block.getNumber(),
toHexString(block.getHash()));
}

// main branch become this branch
// cause we proved that total difficulty
// is greater
Expand Down
85 changes: 70 additions & 15 deletions modAionImpl/src/org/aion/zero/impl/db/AionBlockStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,27 @@
*/
package org.aion.zero.impl.db;

import static java.math.BigInteger.ZERO;
import static org.aion.crypto.HashUtil.shortHash;

import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectStreamClass;
import java.io.Serializable;
import java.math.BigInteger;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.aion.base.db.IByteArrayKeyValueDatabase;
import org.aion.base.util.ByteUtil;
import org.aion.base.util.Hex;
Expand All @@ -39,18 +60,6 @@
import org.aion.zero.types.IAionBlock;
import org.slf4j.Logger;

import java.io.*;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import static java.math.BigInteger.ZERO;
import static org.aion.crypto.HashUtil.shortHash;

public class AionBlockStore extends AbstractPowBlockstore<AionBlock, A0BlockHeader> {

private static final Logger LOG = AionLoggerFactory.getLogger(LogEnum.DB.name());
Expand All @@ -63,6 +72,9 @@ public class AionBlockStore extends AbstractPowBlockstore<AionBlock, A0BlockHead

private boolean checkIntegrity = true;

private Deque<IAionBlock> branchingBlk = new ArrayDeque<>(), preBranchingBlk= new ArrayDeque<>();
private long branchingLevel;

public AionBlockStore(IByteArrayKeyValueDatabase index, IByteArrayKeyValueDatabase blocks) {
init(index, blocks);
}
Expand Down Expand Up @@ -426,13 +438,17 @@ public void reBranch(AionBlock forkBlock) {
// 1. First ensure that you are one the save level
IAionBlock forkLine = forkBlock;
if (forkBlock.getNumber() > bestBlock.getNumber()) {
branchingLevel = currentLevel;

while (currentLevel > bestBlock.getNumber()) {
List<BlockInfo> blocks = getBlockInfoForLevel(currentLevel);
BlockInfo blockInfo = getBlockInfoForHash(blocks, forkLine.getHash());
if (blockInfo != null) {
blockInfo.setMainChain(true);
setBlockInfoForLevel(currentLevel, blocks);

//For collecting branching blocks
branchingBlk.push(getBlockByHash(blockInfo.getHash()));
} else {
LOG.error("Null block information found at " + currentLevel + " when data should exist.");
}
Expand All @@ -451,6 +467,9 @@ public void reBranch(AionBlock forkBlock) {
if (blockInfo != null) {
blockInfo.setMainChain(false);
setBlockInfoForLevel(currentLevel, blocks);

//For collecting prebranching blocks
preBranchingBlk.push(getBlockByHash(blockInfo.getHash()));
} else {
LOG.error("Null block information found at " + currentLevel + " when data should exist.");
}
Expand All @@ -461,11 +480,39 @@ public void reBranch(AionBlock forkBlock) {

// 2. Loop back on each level until common block
loopBackToCommonBlock(bestLine, forkLine);

logBranchingDetails();

} finally {
lock.writeLock().unlock();
}
}

private void logBranchingDetails() {
if (branchingLevel > 0 && LOG_CONS.isDebugEnabled()) {
LOG_CONS.debug("Branching details start: level[{}]", branchingLevel);

LOG_CONS.debug("===== Block details before branch =====");
while (!preBranchingBlk.isEmpty()) {
IAionBlock blk = preBranchingBlk.pop();
LOG_CONS.debug("blk: {}", blk.toString());
}

LOG_CONS.debug("===== Block details after branch =====");
while (!branchingBlk.isEmpty()) {
IAionBlock blk = branchingBlk.pop();
LOG_CONS.debug("blk: {}", blk.toString());
}

LOG_CONS.debug("Branching details end");
}

//reset branching block details
branchingLevel = 0;
branchingBlk.clear();
preBranchingBlk.clear();
}

/**
* @implNote The method calling this method must handle the locking.
*/
Expand All @@ -478,12 +525,14 @@ private void loopBackToCommonBlock(IAionBlock bestLine, IAionBlock forkLine) {
}

while (!bestLine.isEqual(forkLine)) {

List<BlockInfo> levelBlocks = getBlockInfoForLevel(currentLevel);
BlockInfo bestInfo = getBlockInfoForHash(levelBlocks, bestLine.getHash());
if (bestInfo != null) {
bestInfo.setMainChain(false);
setBlockInfoForLevel(currentLevel, levelBlocks);

//For collecting preBranching blocks
preBranchingBlk.push(getBlockByHash(bestInfo.getHash()));
} else {
LOG.error("Null block information found at " + currentLevel + " when information should exist.");
}
Expand All @@ -492,6 +541,9 @@ private void loopBackToCommonBlock(IAionBlock bestLine, IAionBlock forkLine) {
if (forkInfo != null) {
forkInfo.setMainChain(true);
setBlockInfoForLevel(currentLevel, levelBlocks);

//For collecting branching blocks
branchingBlk.push(getBlockByHash(forkInfo.getHash()));
} else {
LOG.error("Null block information found at " + currentLevel + " when information should exist.");
}
Expand All @@ -502,8 +554,11 @@ private void loopBackToCommonBlock(IAionBlock bestLine, IAionBlock forkLine) {
--currentLevel;
}

AionLoggerFactory.getLogger(LogEnum.CONS.name())
.info("branching: common block = {}/{}", forkLine.getNumber(), Hex.toHexString(forkLine.getHash()));
branchingLevel -= currentLevel;

if (LOG_CONS.isInfoEnabled()) {
LOG_CONS.info("branching: common block = {}/{}", forkLine.getNumber(), Hex.toHexString(forkLine.getHash()));
}
}

@Override
Expand Down

0 comments on commit 4aa3ba8

Please sign in to comment.