Skip to content

Commit

Permalink
Merge pull request #531 from aionnetwork/bc-tests
Browse files Browse the repository at this point in the history
AionBlockchainImpl unit tests and minor bug fixes
  • Loading branch information
AionJayT authored Jun 29, 2018
2 parents 7cd3d19 + 8c515f1 commit 7587bfc
Show file tree
Hide file tree
Showing 10 changed files with 1,832 additions and 332 deletions.
303 changes: 192 additions & 111 deletions modAionImpl/src/org/aion/zero/impl/AionBlockchainImpl.java

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions modAionImpl/src/org/aion/zero/impl/StandaloneBlockchain.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.aion.base.db.IRepositoryCache;
import org.aion.base.db.IRepositoryConfig;
import org.aion.base.type.Address;
import org.aion.base.type.Hash256;
import org.aion.base.util.ByteArrayWrapper;
import org.aion.crypto.ECKey;
import org.aion.crypto.ECKeyFac;
Expand Down Expand Up @@ -354,15 +355,18 @@ public BigInteger getCachedTotalDifficulty() {

public void assertEqualTotalDifficulty() {
BigInteger tdForHash, tdCached, tdPublic;
byte[] bestBlockHash;

synchronized (this) {
bestBlockHash = getBestBlock().getHash();
tdForHash = getBlockStore().getTotalDifficultyForHash(getBestBlock().getHash());
tdCached = getCacheTD();
tdPublic = getTotalDifficulty();
}

assert (tdPublic.equals(tdForHash));
assert (tdPublic.equals(tdCached));
assert (tdForHash.equals(getTotalDifficultyByHash(new Hash256(bestBlockHash))));
}

public synchronized ImportResult tryToConnect(final AionBlock block) {
Expand Down
5 changes: 5 additions & 0 deletions modAionImpl/src/org/aion/zero/impl/db/AionRepositoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,11 @@ public IByteArrayKeyValueDatabase getDetailsDatabase() {
return this.detailsDatabase;
}

/** For testing. */
public IByteArrayKeyValueDatabase getBlockDatabase() {
return this.blockDatabase;
}

/** For testing. */
public IByteArrayKeyValueDatabase getIndexDatabase() {
return this.indexDatabase;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
/*
* Copyright (c) 2017-2018 Aion foundation.
*
* This file is part of the aion network project.
* This file is part of the aion network project.
*
* The aion network project is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or any later version.
* The aion network project is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or any later version.
*
* The aion network project is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* The aion network project is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the aion network project source files.
* If not, see <https://www.gnu.org/licenses/>.
* You should have received a copy of the GNU General Public License
* along with the aion network project source files.
* If not, see <https://www.gnu.org/licenses/>.
*
* The aion network project leverages useful source code from other
* open source projects. We greatly appreciate the effort that was
* invested in these projects and we thank the individual contributors
* for their work. For provenance information and contributors
* please see <https://github.com/aionnetwork/aion/wiki/Contributors>.
* The aion network project leverages useful source code from other
* open source projects. We greatly appreciate the effort that was
* invested in these projects and we thank the individual contributors
* for their work. For provenance information and contributors
* please see <https://github.com/aionnetwork/aion/wiki/Contributors>.
*
* Contributors to the aion source files in decreasing order of code volume:
* Aion foundation.
* <ether.camp> team through the ethereumJ library.
* Ether.Camp Inc. (US) team through Ethereum Harmony.
* John Tromp through the Equihash solver.
* Samuel Neves through the BLAKE2 implementation.
* Zcash project team.
* Bitcoinj team.
* Aion foundation.
* <ether.camp> team through the ethereumJ library.
* Ether.Camp Inc. (US) team through Ethereum Harmony.
* John Tromp through the Equihash solver.
* Samuel Neves through the BLAKE2 implementation.
* Zcash project team.
* Bitcoinj team.
*/

package org.aion.zero.impl.sync.handler;

import org.aion.mcf.types.BlockIdentifier;
import java.util.List;
import org.aion.p2p.Ctrl;
import org.aion.p2p.Handler;
import org.aion.p2p.IP2pMgr;
Expand All @@ -46,17 +45,15 @@
import org.aion.zero.impl.sync.msg.ResBlocksHeaders;
import org.aion.zero.types.A0BlockHeader;
import org.slf4j.Logger;
import java.util.List;

/**
*
* @author chris
* handler for request block headers from network
*
* @author chris
*/
public final class ReqBlocksHeadersHandler extends Handler {

private final static int MAX_NUM_OF_BLOCKS = 96;
private static final int MAX_NUM_OF_BLOCKS = 96;

private final Logger log;

Expand All @@ -66,7 +63,11 @@ public final class ReqBlocksHeadersHandler extends Handler {

private final boolean isSyncOnlyNode;

public ReqBlocksHeadersHandler(final Logger _log, final IAionBlockchain _blockchain, final IP2pMgr _p2pMgr, final boolean isSyncOnlyNode) {
public ReqBlocksHeadersHandler(
final Logger _log,
final IAionBlockchain _blockchain,
final IP2pMgr _p2pMgr,
final boolean isSyncOnlyNode) {
super(Ver.V0, Ctrl.SYNC, Act.REQ_BLOCKS_HEADERS);
this.log = _log;
this.blockchain = _blockchain;
Expand All @@ -76,23 +77,31 @@ public ReqBlocksHeadersHandler(final Logger _log, final IAionBlockchain _blockch

@Override
public void receive(int _nodeIdHashcode, String _displayId, final byte[] _msgBytes) {
if(isSyncOnlyNode)
if (isSyncOnlyNode) {
return;
}

ReqBlocksHeaders reqHeaders = ReqBlocksHeaders.decode(_msgBytes);
if (reqHeaders != null) {
long fromBlock = reqHeaders.getFromBlock();
int take = reqHeaders.getTake();
if (log.isDebugEnabled()) {
this.log.debug("<req-headers from-number={} size={} node={}>", fromBlock, take, _displayId);
this.log.debug(
"<req-headers from-number={} size={} node={}>",
fromBlock,
take,
_displayId);
}
List<A0BlockHeader> headers = this.blockchain.getListOfHeadersStartFrom(
new BlockIdentifier(null, fromBlock), 0, Math.min(take, MAX_NUM_OF_BLOCKS), false);
List<A0BlockHeader> headers =
this.blockchain.getListOfHeadersStartFrom(
fromBlock, Math.min(take, MAX_NUM_OF_BLOCKS));
ResBlocksHeaders rbhs = new ResBlocksHeaders(headers);
this.p2pMgr.send(_nodeIdHashcode, _displayId, rbhs);
} else {
this.log.error("<req-headers decode-error msg-bytes={} node={}>", _msgBytes == null ? 0 : _msgBytes.length,
this.log.error(
"<req-headers decode-error msg-bytes={} node={}>",
_msgBytes == null ? 0 : _msgBytes.length,
_nodeIdHashcode);
}
}
}
}
Loading

0 comments on commit 7587bfc

Please sign in to comment.