diff --git a/ethereumj-core/src/main/java/org/ethereum/core/Block.java b/ethereumj-core/src/main/java/org/ethereum/core/Block.java index 691c002f..0caf8d1e 100644 --- a/ethereumj-core/src/main/java/org/ethereum/core/Block.java +++ b/ethereumj-core/src/main/java/org/ethereum/core/Block.java @@ -166,6 +166,15 @@ public class Block { return this.header.getDifficulty(); } + public BigInteger getTotalDifficulty() { + if (!parsed) parseRLP(); + BigInteger totalDifficulty = new BigInteger(1, this.header.getDifficulty()); + for (BlockHeader uncle : uncleList) { + totalDifficulty.add(new BigInteger(1, uncle.getDifficulty())); + } + return totalDifficulty; + } + public long getTimestamp() { if (!parsed) parseRLP(); return this.header.getTimestamp(); diff --git a/ethereumj-core/src/main/java/org/ethereum/core/BlockchainImpl.java b/ethereumj-core/src/main/java/org/ethereum/core/BlockchainImpl.java index 4b4e7135..83a31e76 100644 --- a/ethereumj-core/src/main/java/org/ethereum/core/BlockchainImpl.java +++ b/ethereumj-core/src/main/java/org/ethereum/core/BlockchainImpl.java @@ -165,19 +165,12 @@ public class BlockchainImpl implements Blockchain { } this.addReward(block); - this.increaseTotalDifficulty(block); + this.addTotalDifficulty(block.getTotalDifficulty()); if(block.getNumber() >= CONFIG.traceStartBlock()) repository.dumpState(block, totalGasUsed, 0, null); } - private void increaseTotalDifficulty(Block block) { - totalDifficulty.add(new BigInteger(block.getDifficulty())); - for (BlockHeader uncleHeader : block.getUncleList()) { - totalDifficulty.add(new BigInteger(uncleHeader.getDifficulty())); - } - } - /** * Add reward to block- and every uncle coinbase * assuming the entire block is valid. @@ -476,4 +469,12 @@ public class BlockchainImpl implements Blockchain { public BigInteger getTotalDifficulty() { return totalDifficulty; } + + @Override + public void addTotalDifficulty(BigInteger totalDifficulty) { + if (this.totalDifficulty == null) + this.totalDifficulty = totalDifficulty; + else + this.totalDifficulty.add(totalDifficulty); + } } diff --git a/ethereumj-core/src/main/java/org/ethereum/db/RepositoryImpl.java b/ethereumj-core/src/main/java/org/ethereum/db/RepositoryImpl.java index 8b4f0cef..f301e270 100644 --- a/ethereumj-core/src/main/java/org/ethereum/db/RepositoryImpl.java +++ b/ethereumj-core/src/main/java/org/ethereum/db/RepositoryImpl.java @@ -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()); EthereumListener listener = WorldManager.getInstance().getListener(); if (listener != null){ listener.onPreloadedBlock(block); diff --git a/ethereumj-core/src/main/java/org/ethereum/facade/Blockchain.java b/ethereumj-core/src/main/java/org/ethereum/facade/Blockchain.java index 98043612..0da1477e 100644 --- a/ethereumj-core/src/main/java/org/ethereum/facade/Blockchain.java +++ b/ethereumj-core/src/main/java/org/ethereum/facade/Blockchain.java @@ -21,6 +21,7 @@ public interface Blockchain { public Block getLastBlock(); public BlockQueue getQueue(); public void close(); - public BigInteger getTotalDifficulty(); + public void addTotalDifficulty(BigInteger totalDifficulty); + public BigInteger getTotalDifficulty(); public byte[] getLatestBlockHash(); }