Track total difficulty in chain

This commit is contained in:
nicksavers 2014-10-07 16:24:14 +02:00
parent 56a96e8f75
commit 040455d868
4 changed files with 10 additions and 10 deletions

View File

@ -166,13 +166,13 @@ public class Block {
return this.header.getDifficulty();
}
public BigInteger getTotalDifficulty() {
public BigInteger getCumulativeDifficulty() {
if (!parsed) parseRLP();
BigInteger totalDifficulty = new BigInteger(1, this.header.getDifficulty());
BigInteger calcDifficulty = new BigInteger(1, this.header.getDifficulty());
for (BlockHeader uncle : uncleList) {
totalDifficulty.add(new BigInteger(1, uncle.getDifficulty()));
calcDifficulty.add(new BigInteger(1, uncle.getDifficulty()));
}
return totalDifficulty;
return calcDifficulty;
}
public long getTimestamp() {

View File

@ -165,7 +165,7 @@ public class BlockchainImpl implements Blockchain {
}
this.addReward(block);
this.addTotalDifficulty(block.getTotalDifficulty());
this.updateTotalDifficulty(block);
if(block.getNumber() >= CONFIG.traceStartBlock())
repository.dumpState(block, totalGasUsed, 0, null);
@ -471,10 +471,10 @@ public class BlockchainImpl implements Blockchain {
}
@Override
public void addTotalDifficulty(BigInteger totalDifficulty) {
public void updateTotalDifficulty(Block block) {
if (this.totalDifficulty == null)
this.totalDifficulty = totalDifficulty;
this.totalDifficulty = block.getCumulativeDifficulty();
else
this.totalDifficulty.add(totalDifficulty);
this.totalDifficulty.add(block.getCumulativeDifficulty());
}
}

View File

@ -154,7 +154,7 @@ public class RepositoryImpl implements Repository {
Block block = new Block(iterator.next().getValue());
blockchain.getBlockCache().put(block.getNumber(), block.getHash());
blockchain.setLastBlock(block);
blockchain.addTotalDifficulty(block.getTotalDifficulty());
blockchain.updateTotalDifficulty(block);
EthereumListener listener = WorldManager.getInstance().getListener();
if (listener != null){
listener.onPreloadedBlock(block);

View File

@ -21,7 +21,7 @@ public interface Blockchain {
public Block getLastBlock();
public BlockQueue getQueue();
public void close();
public void addTotalDifficulty(BigInteger totalDifficulty);
public void updateTotalDifficulty(Block block);
public BigInteger getTotalDifficulty();
public byte[] getLatestBlockHash();
}