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(); return this.header.getDifficulty();
} }
public BigInteger getTotalDifficulty() { public BigInteger getCumulativeDifficulty() {
if (!parsed) parseRLP(); if (!parsed) parseRLP();
BigInteger totalDifficulty = new BigInteger(1, this.header.getDifficulty()); BigInteger calcDifficulty = new BigInteger(1, this.header.getDifficulty());
for (BlockHeader uncle : uncleList) { 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() { public long getTimestamp() {

View File

@ -165,7 +165,7 @@ public class BlockchainImpl implements Blockchain {
} }
this.addReward(block); this.addReward(block);
this.addTotalDifficulty(block.getTotalDifficulty()); this.updateTotalDifficulty(block);
if(block.getNumber() >= CONFIG.traceStartBlock()) if(block.getNumber() >= CONFIG.traceStartBlock())
repository.dumpState(block, totalGasUsed, 0, null); repository.dumpState(block, totalGasUsed, 0, null);
@ -471,10 +471,10 @@ public class BlockchainImpl implements Blockchain {
} }
@Override @Override
public void addTotalDifficulty(BigInteger totalDifficulty) { public void updateTotalDifficulty(Block block) {
if (this.totalDifficulty == null) if (this.totalDifficulty == null)
this.totalDifficulty = totalDifficulty; this.totalDifficulty = block.getCumulativeDifficulty();
else 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()); Block block = new Block(iterator.next().getValue());
blockchain.getBlockCache().put(block.getNumber(), block.getHash()); blockchain.getBlockCache().put(block.getNumber(), block.getHash());
blockchain.setLastBlock(block); blockchain.setLastBlock(block);
blockchain.addTotalDifficulty(block.getTotalDifficulty()); blockchain.updateTotalDifficulty(block);
EthereumListener listener = WorldManager.getInstance().getListener(); EthereumListener listener = WorldManager.getInstance().getListener();
if (listener != null){ if (listener != null){
listener.onPreloadedBlock(block); listener.onPreloadedBlock(block);

View File

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