diff --git a/ethereumj-core/src/main/java/org/ethereum/config/BlockchainConfig.java b/ethereumj-core/src/main/java/org/ethereum/config/BlockchainConfig.java index 431d56a379..c889d08dab 100644 --- a/ethereumj-core/src/main/java/org/ethereum/config/BlockchainConfig.java +++ b/ethereumj-core/src/main/java/org/ethereum/config/BlockchainConfig.java @@ -52,6 +52,11 @@ public interface BlockchainConfig { */ BigInteger calcDifficulty(BlockHeader curBlock, BlockHeader parent); + /** + * Calculates difficulty adjustment to target mean block time + */ + BigInteger getCalcDifficultyMultiplier(BlockHeader curBlock, BlockHeader parent); + /** * Calculates transaction gas fee */ diff --git a/ethereumj-core/src/main/java/org/ethereum/config/blockchain/AbstractConfig.java b/ethereumj-core/src/main/java/org/ethereum/config/blockchain/AbstractConfig.java index 0d5a562a7b..ad9eba8a90 100644 --- a/ethereumj-core/src/main/java/org/ethereum/config/blockchain/AbstractConfig.java +++ b/ethereumj-core/src/main/java/org/ethereum/config/blockchain/AbstractConfig.java @@ -100,8 +100,6 @@ public BigInteger calcDifficulty(BlockHeader curBlock, BlockHeader parent) { return difficulty; } - protected abstract BigInteger getCalcDifficultyMultiplier(BlockHeader curBlock, BlockHeader parent); - protected int getExplosion(BlockHeader curBlock, BlockHeader parent) { int periodCount = (int) (curBlock.getNumber() / getConstants().getEXP_DIFFICULTY_PERIOD()); return periodCount - 2; diff --git a/ethereumj-core/src/main/java/org/ethereum/config/blockchain/ByzantiumConfig.java b/ethereumj-core/src/main/java/org/ethereum/config/blockchain/ByzantiumConfig.java new file mode 100644 index 0000000000..3dcb50eda9 --- /dev/null +++ b/ethereumj-core/src/main/java/org/ethereum/config/blockchain/ByzantiumConfig.java @@ -0,0 +1,39 @@ +package org.ethereum.config.blockchain; + +import org.ethereum.config.BlockchainConfig; +import org.ethereum.config.Constants; +import org.ethereum.core.Block; +import org.ethereum.core.BlockHeader; +import org.ethereum.core.Repository; +import org.spongycastle.util.encoders.Hex; + +import java.math.BigInteger; + +/** + * EIPs included in the Hard Fork: + * + * + * @author Mikhail Kalinin + * @since 14.08.2017 + */ +public class ByzantiumConfig extends Eip160HFConfig { + + public ByzantiumConfig(BlockchainConfig parent) { + super(parent); + } + + @Override + public BigInteger getCalcDifficultyMultiplier(BlockHeader curBlock, BlockHeader parent) { + long unclesAdj = parent.hasUncles() ? 2 : 1; + return BigInteger.valueOf(Math.max(unclesAdj - (curBlock.getTimestamp() - parent.getTimestamp()) / 9, -99)); + } +} diff --git a/ethereumj-core/src/main/java/org/ethereum/config/blockchain/ETCFork3M.java b/ethereumj-core/src/main/java/org/ethereum/config/blockchain/ETCFork3M.java index 82b8b834e1..77f4a18dd9 100644 --- a/ethereumj-core/src/main/java/org/ethereum/config/blockchain/ETCFork3M.java +++ b/ethereumj-core/src/main/java/org/ethereum/config/blockchain/ETCFork3M.java @@ -66,7 +66,8 @@ public BigInteger calcDifficulty(BlockHeader curBlock, BlockHeader parent) { return difficulty; } - protected BigInteger getCalcDifficultyMultiplier(BlockHeader curBlock, BlockHeader parent) { + @Override + public BigInteger getCalcDifficultyMultiplier(BlockHeader curBlock, BlockHeader parent) { return BigInteger.valueOf(Math.max(1 - (curBlock.getTimestamp() - parent.getTimestamp()) / 10, -99)); } diff --git a/ethereumj-core/src/main/java/org/ethereum/config/blockchain/Eip150HFConfig.java b/ethereumj-core/src/main/java/org/ethereum/config/blockchain/Eip150HFConfig.java index 7e5d1354f2..7e963ff137 100644 --- a/ethereumj-core/src/main/java/org/ethereum/config/blockchain/Eip150HFConfig.java +++ b/ethereumj-core/src/main/java/org/ethereum/config/blockchain/Eip150HFConfig.java @@ -87,6 +87,11 @@ public BigInteger calcDifficulty(BlockHeader curBlock, BlockHeader parent) { return this.parent.calcDifficulty(curBlock, parent); } + @Override + public BigInteger getCalcDifficultyMultiplier(BlockHeader curBlock, BlockHeader parent) { + return this.parent.getCalcDifficultyMultiplier(curBlock, parent); + } + @Override public long getTransactionCost(Transaction tx) { return parent.getTransactionCost(tx); diff --git a/ethereumj-core/src/main/java/org/ethereum/config/blockchain/HomesteadConfig.java b/ethereumj-core/src/main/java/org/ethereum/config/blockchain/HomesteadConfig.java index 45a63f3047..9bfd4648cf 100644 --- a/ethereumj-core/src/main/java/org/ethereum/config/blockchain/HomesteadConfig.java +++ b/ethereumj-core/src/main/java/org/ethereum/config/blockchain/HomesteadConfig.java @@ -52,7 +52,7 @@ public HomesteadConfig(Constants constants) { } @Override - protected BigInteger getCalcDifficultyMultiplier(BlockHeader curBlock, BlockHeader parent) { + public BigInteger getCalcDifficultyMultiplier(BlockHeader curBlock, BlockHeader parent) { return BigInteger.valueOf(Math.max(1 - (curBlock.getTimestamp() - parent.getTimestamp()) / 10, -99)); } diff --git a/ethereumj-core/src/main/java/org/ethereum/config/blockchain/OlympicConfig.java b/ethereumj-core/src/main/java/org/ethereum/config/blockchain/OlympicConfig.java index fd3bb40f93..82f8d8eb97 100644 --- a/ethereumj-core/src/main/java/org/ethereum/config/blockchain/OlympicConfig.java +++ b/ethereumj-core/src/main/java/org/ethereum/config/blockchain/OlympicConfig.java @@ -37,7 +37,7 @@ public OlympicConfig(Constants constants) { } @Override - protected BigInteger getCalcDifficultyMultiplier(BlockHeader curBlock, BlockHeader parent) { + public BigInteger getCalcDifficultyMultiplier(BlockHeader curBlock, BlockHeader parent) { return BigInteger.valueOf(curBlock.getTimestamp() >= parent.getTimestamp() + getConstants().getDURATION_LIMIT() ? -1 : 1); } diff --git a/ethereumj-core/src/main/java/org/ethereum/core/BlockHeader.java b/ethereumj-core/src/main/java/org/ethereum/core/BlockHeader.java index 125d016a7d..976393c17f 100644 --- a/ethereumj-core/src/main/java/org/ethereum/core/BlockHeader.java +++ b/ethereumj-core/src/main/java/org/ethereum/core/BlockHeader.java @@ -148,7 +148,7 @@ public BlockHeader(byte[] parentHash, byte[] unclesHash, byte[] coinbase, this.extraData = extraData; this.mixHash = mixHash; this.nonce = nonce; - this.stateRoot = HashUtil.EMPTY_TRIE_HASH; + this.stateRoot = EMPTY_TRIE_HASH; } public boolean isGenesis() { @@ -374,6 +374,10 @@ public BigInteger calcDifficulty(BlockchainNetConfig config, BlockHeader parent) calcDifficulty(this, parent); } + public boolean hasUncles() { + return !FastByteComparisons.equal(unclesHash, EMPTY_TRIE_HASH); + } + public String toString() { return toStringWithSuffix("\n"); }