Skip to content

Commit

Permalink
EIP-100 add uncles adjustment to the difficulty
Browse files Browse the repository at this point in the history
  • Loading branch information
mkalinin committed Aug 16, 2017
1 parent 7a7da0c commit 1139a83
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
* <ul>
* <li>100 - Change difficulty adjustment to target mean block time including uncles</li>
* <li>140 - REVERT instruction in the Ethereum Virtual Machine</li>
* <li>196 - Precompiled contracts for addition and scalar multiplication on the elliptic curve alt_bn128</li>
* <li>197 - Precompiled contracts for optimal Ate pairing check on the elliptic curve alt_bn128</li>
* <li>198 - Precompiled contract for bigint modular exponentiation</li>
* <li>211 - New opcodes: RETURNDATASIZE and RETURNDATACOPY</li>
* <li>214 - New opcode STATICCALL</li>
* <li>658 - Embedding transaction return data in receipts</li>
* </ul>
*
* @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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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");
}
Expand Down

0 comments on commit 1139a83

Please sign in to comment.