Skip to content

Commit

Permalink
Adding basic consensus test
Browse files Browse the repository at this point in the history
- This test checks against only a very basic case, that none of the
  fundamental results required for network consensus are broken by
  any changes we are doing.
  • Loading branch information
aionick committed Jan 17, 2019
1 parent 13555b9 commit 103c613
Show file tree
Hide file tree
Showing 3 changed files with 233 additions and 26 deletions.
58 changes: 32 additions & 26 deletions modAionImpl/src/org/aion/zero/impl/AionBlockchainImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
import org.aion.zero.types.AionTxExecSummary;
import org.aion.zero.types.AionTxReceipt;
import org.aion.zero.types.IAionBlock;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.aion.vm.api.interfaces.Address;
Expand Down Expand Up @@ -527,21 +528,17 @@ public synchronized void compactState() {
repository.compactState();
}

/**
* Processes a new block and potentially appends it to the blockchain, thereby changing the
* state of the world. Decoupled from wrapper function {@link #tryToConnect(AionBlock)} so we
* can feed timestamps manually
*/
ImportResult tryToConnectInternal(final AionBlock block, long currTimeSeconds) {
// TEMPORARY: here to support the ConsensusTest
public Pair<ImportResult, AionBlockSummary> tryToConnectAndFetchSummary(AionBlock block, long currTimeSeconds) {
// Check block exists before processing more rules
if (getBlockStore().getMaxNumber() >= block.getNumber()
&& getBlockStore().isBlockExist(block.getHash())) {
&& getBlockStore().isBlockExist(block.getHash())) {

if (LOG.isDebugEnabled()) {
LOG.debug(
"Block already exists hash: {}, number: {}",
block.getShortHash(),
block.getNumber());
"Block already exists hash: {}, number: {}",
block.getShortHash(),
block.getNumber());
}

if (!repository.isValidRoot(block.getStateRoot())) {
Expand All @@ -555,26 +552,26 @@ && getBlockStore().isBlockExist(block.getHash())) {
}

// retry of well known block
return EXIST;
return Pair.of(EXIST, null);
}

if (block.getTimestamp()
> (currTimeSeconds
+ this.chainConfiguration.getConstants().getClockDriftBufferTime())) {
> (currTimeSeconds
+ this.chainConfiguration.getConstants().getClockDriftBufferTime())) {
if (LOG.isDebugEnabled()) {
LOG.debug(
"Block {} invalid due to timestamp {}.",
Hex.toHexString(block.getHash()),
block.getTimestamp());
"Block {} invalid due to timestamp {}.",
Hex.toHexString(block.getHash()),
block.getTimestamp());
}
return INVALID_BLOCK;
return Pair.of(INVALID_BLOCK, null);
}

if (LOG.isDebugEnabled()) {
LOG.debug(
"Try connect block hash: {}, number: {}",
block.getShortHash(),
block.getNumber());
"Try connect block hash: {}, number: {}",
block.getShortHash(),
block.getNumber());
}

final ImportResult ret;
Expand All @@ -591,11 +588,11 @@ && getBlockStore().isBlockExist(block.getHash())) {
BigInteger oldTotalDiff = getInternalTD();
summary = tryConnectAndFork(block);
ret =
summary == null
? INVALID_BLOCK
: (isMoreThan(getInternalTD(), oldTotalDiff)
? IMPORTED_BEST
: IMPORTED_NOT_BEST);
summary == null
? INVALID_BLOCK
: (isMoreThan(getInternalTD(), oldTotalDiff)
? IMPORTED_BEST
: IMPORTED_NOT_BEST);
} else {
summary = null;
ret = NO_PARENT;
Expand Down Expand Up @@ -631,7 +628,16 @@ && getBlockStore().isBlockExist(block.getHash())) {
}
}

return ret;
return Pair.of(ret, summary);
}

/**
* Processes a new block and potentially appends it to the blockchain, thereby changing the
* state of the world. Decoupled from wrapper function {@link #tryToConnect(AionBlock)} so we
* can feed timestamps manually
*/
ImportResult tryToConnectInternal(final AionBlock block, long currTimeSeconds) {
return tryToConnectAndFetchSummary(block, currTimeSeconds).getLeft();
}

/**
Expand Down
7 changes: 7 additions & 0 deletions modAionImpl/src/org/aion/zero/impl/StandaloneBlockchain.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@
import org.aion.zero.impl.db.AionRepositoryImpl;
import org.aion.zero.impl.db.ContractDetailsAion;
import org.aion.zero.impl.types.AionBlock;
import org.aion.zero.impl.types.AionBlockSummary;
import org.aion.zero.impl.valid.AionExtraDataRule;
import org.aion.zero.impl.valid.AionHeaderVersionRule;
import org.aion.zero.impl.valid.EnergyConsumedRule;
import org.aion.zero.types.A0BlockHeader;
import org.apache.commons.lang3.tuple.Pair;

/**
* Used mainly for debugging and testing purposes, provides codepaths for easy setup, into standard
Expand Down Expand Up @@ -366,6 +368,11 @@ public synchronized ImportResult tryToConnect(final AionBlock block) {
return result;
}

// TEMPORARY: here to support the ConsensusTest
public synchronized Pair<ImportResult, AionBlockSummary> tryToConnectAndFetchSummary(AionBlock block) {
return tryToConnectAndFetchSummary(block, System.currentTimeMillis() / 1000);
}

/**
* @apiNote users should beware that this will cause a disconnect in the blockchain, one should
* not expect to use any block that is below {@code blockNumber}
Expand Down
Loading

0 comments on commit 103c613

Please sign in to comment.