Skip to content

Commit

Permalink
Merge pull request #487 from aionnetwork/db-td-refactor
Browse files Browse the repository at this point in the history
Refactoring the public TD management
  • Loading branch information
AionJayT authored Jun 1, 2018
2 parents 36b1dc7 + 6ad5911 commit 4b5e4f7
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 136 deletions.
14 changes: 5 additions & 9 deletions modAionImpl/src/org/aion/zero/impl/AionBlockchainImpl.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*******************************************************************************
/*
* Copyright (c) 2017-2018 Aion foundation.
*
* This file is part of the aion network project.
Expand All @@ -19,8 +19,7 @@
*
* Contributors:
* Aion foundation.
******************************************************************************/

*/
package org.aion.zero.impl;

import static java.lang.Math.max;
Expand Down Expand Up @@ -139,8 +138,6 @@ public class AionBlockchainImpl implements IAionBlockchain {
*/
private volatile AionBlock pubBestBlock;

private volatile BigInteger pubBestTD = ZERO;

private volatile BigInteger totalDifficulty = ZERO;
private ChainStatistics chainStats;

Expand Down Expand Up @@ -572,7 +569,6 @@ ImportResult tryToConnectInternal(final AionBlock block, long currTimeSeconds) {
// update best block reference
if (ret == IMPORTED_BEST) {
pubBestBlock = bestBlock;
pubBestTD = summary.getTotalDifficulty();
}

// fire block events
Expand Down Expand Up @@ -721,7 +717,7 @@ public synchronized AionBlockSummary add(AionBlock block) {
List<AionTxReceipt> receipts = summary.getReceipts();

updateTotalDifficulty(block);
summary.setTotalDifficulty(getInternalTD());
summary.setTotalDifficulty(block.getCumulativeDifficulty());

storeBlock(block, receipts);

Expand Down Expand Up @@ -1139,7 +1135,7 @@ public synchronized void close() {

@Override
public BigInteger getTotalDifficulty() {
return pubBestTD;
return getBestBlock().getCumulativeDifficulty();
}

// this method is for the testing purpose
Expand All @@ -1153,14 +1149,14 @@ private BigInteger getInternalTD() {

private void updateTotalDifficulty(AionBlock block) {
totalDifficulty = totalDifficulty.add(block.getDifficultyBI());
block.setCumulativeDifficulty(totalDifficulty);
if (LOG.isDebugEnabled()) {
LOG.debug("TD: updated to {}", totalDifficulty);
}
}

@Override
public void setTotalDifficulty(BigInteger totalDifficulty) {
this.pubBestTD = totalDifficulty;
this.totalDifficulty = totalDifficulty;
}

Expand Down
13 changes: 7 additions & 6 deletions modAionImpl/src/org/aion/zero/impl/AionHub.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*******************************************************************************
/*
* Copyright (c) 2017-2018 Aion foundation.
*
* This file is part of the aion network project.
Expand All @@ -19,9 +19,7 @@
*
* Contributors:
* Aion foundation.
*
******************************************************************************/

*/
package org.aion.zero.impl;

import org.aion.base.db.IRepository;
Expand Down Expand Up @@ -261,6 +259,7 @@ private void loadBlockchain() {
this.repository.getBlockStore().load();

AionBlock bestBlock = this.repository.getBlockStore().getBestBlock();
bestBlock.setCumulativeDifficulty(repository.getBlockStore().getTotalDifficultyForHash(bestBlock.getHash()));

boolean recovered = true;
boolean bestBlockShifted = true;
Expand Down Expand Up @@ -290,6 +289,8 @@ private void loadBlockchain() {

if (recovered) {
bestBlock = this.repository.getBlockStore().getBestBlock();
bestBlock.setCumulativeDifficulty(repository.getBlockStore()
.getTotalDifficultyForHash(bestBlock.getHash()));

// checking is the best block has changed since attempting recovery
if (bestBlock == null) {
Expand Down Expand Up @@ -340,9 +341,9 @@ private void loadBlockchain() {
track.flush();

repository.commitBlock(genesis.getHeader());
this.repository.getBlockStore().saveBlock(genesis, genesis.getCumulativeDifficulty(), true);
this.repository.getBlockStore().saveBlock(genesis, genesis.getDifficultyBI(), true);
blockchain.setBestBlock(genesis);
blockchain.setTotalDifficulty(genesis.getCumulativeDifficulty());
blockchain.setTotalDifficulty(genesis.getDifficultyBI());

if (this.eventMgr != null) {
List<IEvent> evts = new ArrayList<>();
Expand Down
10 changes: 5 additions & 5 deletions modAionImpl/src/org/aion/zero/impl/StandaloneBlockchain.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* ******************************************************************************
/*
* Copyright (c) 2017-2018 Aion foundation.
*
* This file is part of the aion network project.
Expand All @@ -19,7 +19,7 @@
*
* Contributors:
* Aion foundation.
******************************************************************************/
*/
package org.aion.zero.impl;

import java.math.BigInteger;
Expand Down Expand Up @@ -336,10 +336,10 @@ public AbstractEnergyStrategyLimit getEnergyLimitStrategy() {
// TODO: violates abstraction, consider adding to interface after
// stable
((AionRepositoryImpl) bc.getRepository()).commitBlock(genesis.getHeader());
((AionBlockStore) bc.getRepository().getBlockStore())
.saveBlock(genesis, genesis.getCumulativeDifficulty(), true);
((AionBlockStore) bc.getRepository().getBlockStore()).saveBlock(genesis, genesis.getDifficultyBI(),
true);
bc.setBestBlock(genesis);
bc.setTotalDifficulty(genesis.getCumulativeDifficulty());
bc.setTotalDifficulty(genesis.getDifficultyBI());

return new Bundle(this.defaultKeys, bc);
}
Expand Down
11 changes: 6 additions & 5 deletions modAionImpl/src/org/aion/zero/impl/db/AionBlockStore.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*******************************************************************************
/*
* Copyright (c) 2017-2018 Aion foundation.
*
* This file is part of the aion network project.
Expand All @@ -19,8 +19,7 @@
*
* Contributors:
* Aion foundation.
******************************************************************************/

*/
package org.aion.zero.impl.db;

import org.aion.base.db.IByteArrayKeyValueDatabase;
Expand Down Expand Up @@ -635,7 +634,8 @@ public void pruneAndCorrect() {
pruneSideChains(block);

// bottom up repair of information
BigInteger parentTotalDifficulty = block.getCumulativeDifficulty();
// initial TD set to genesis TD
BigInteger parentTotalDifficulty = block.getHeader().getDifficultyBI();
level = 1;
while (level <= initialLevel) {
parentTotalDifficulty = correctTotalDifficulty(level, parentTotalDifficulty);
Expand Down Expand Up @@ -672,7 +672,7 @@ private void pruneSideChains(IAionBlock block) {
blocks.delete(wrongBlock.getHash());
}

// set new block info without total difficulty
// set new block info with total difficulty = block difficulty
blockInfo = new BlockInfo();
blockInfo.setCummDifficulty(block.getHeader().getDifficultyBI());
blockInfo.setHash(blockHash);
Expand All @@ -697,6 +697,7 @@ private BigInteger correctTotalDifficulty(long level, BigInteger parentTotalDiff
} else {
// correct block info
BlockInfo blockInfo = levelBlocks.remove(0);
// total difficulty previously set to block difficulty
blockInfo.setCummDifficulty(blockInfo.getCummDifficulty().add(parentTotalDifficulty));
levelBlocks.add(blockInfo);
setBlockInfoForLevel(level, levelBlocks);
Expand Down
36 changes: 15 additions & 21 deletions modAionImpl/src/org/aion/zero/impl/sync/TaskShowStatus.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
/*
* 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>.
*
* Contributors to the aion source files in decreasing order of code volume:
* Aion foundation.
* Contributors:
* Aion foundation.
*/
package org.aion.zero.impl.sync;

Expand Down Expand Up @@ -78,7 +72,7 @@ public void run() {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (this.start.get()) {
AionBlock selfBest = this.chain.getBestBlock();
String selfTd = this.chain.getTotalDifficulty().toString(10);
String selfTd = selfBest.getCumulativeDifficulty().toString(10);

String status = "<sync-status avg-import=" + String.format("%.2f", this.statics.getAvgBlocksPerSec()) //
+ " b/s" //
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
/*
* 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.base.util.ByteArrayWrapper;
Expand Down Expand Up @@ -181,12 +180,13 @@ public PropStatus processIncomingBlock(final int nodeId, final String _displayId
}

// notify higher td peers in order to limit the rebroadcast on delay of res status updating
if(result.isBest()){
BigInteger td = blockchain.getTotalDifficulty();
if (result.isBest()) {
AionBlock bestBlock = blockchain.getBestBlock();
BigInteger td = bestBlock.getCumulativeDifficulty();
ResStatus rs = new ResStatus(
blockchain.getBestBlock().getNumber(),
bestBlock.getNumber(),
td.toByteArray(),
blockchain.getBestBlockHash(),
bestBlock.getHash(),
genesis
);

Expand Down
Loading

0 comments on commit 4b5e4f7

Please sign in to comment.