Skip to content
This repository has been archived by the owner on Oct 28, 2021. It is now read-only.

EIP100 - Difficulty adjustment including uncles #4017

Merged
merged 2 commits into from
Apr 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions libethashseal/Ethash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,14 @@ u256 Ethash::calculateDifficulty(BlockHeader const& _bi, BlockHeader const& _par
// Frontier-era difficulty adjustment
target = _bi.timestamp() >= _parent.timestamp() + durationLimit ? _parent.difficulty() - (_parent.difficulty() / difficultyBoundDivisor) : (_parent.difficulty() + (_parent.difficulty() / difficultyBoundDivisor));
else
// Homestead-era difficulty adjustment
target = _parent.difficulty() + _parent.difficulty() / 2048 * max<bigint>(1 - (bigint(_bi.timestamp()) - _parent.timestamp()) / 10, -99);
{
bigint const timestampDiff = bigint(_bi.timestamp()) - _parent.timestamp();
bigint const adjFactor = _bi.number() < chainParams().u256Param("metropolisForkBlock") ?
max<bigint>(1 - timestampDiff / 10, -99) : // Homestead-era difficulty adjustment
max<bigint>((_parent.hasUncles() ? 2 : 1) - timestampDiff / 9, -99); // Metropolis-era difficulty adjustment

target = _parent.difficulty() + _parent.difficulty() / 2048 * adjFactor;
}

bigint o = target;
unsigned periodCount = unsigned(_parent.number() + 1) / c_expDiffPeriod;
Expand Down
1 change: 1 addition & 0 deletions libethcore/BlockHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class BlockHeader

h256 const& parentHash() const { return m_parentHash; }
h256 const& sha3Uncles() const { return m_sha3Uncles; }
bool hasUncles() const { return m_sha3Uncles != EmptyListSHA3; }
u256 const& timestamp() const { return m_timestamp; }
Address const& author() const { return m_author; }
h256 const& stateRoot() const { return m_stateRoot; }
Expand Down
99 changes: 99 additions & 0 deletions test/unittests/libethashseal/EthashTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
This file is part of cpp-ethereum.

cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file ethash.cpp
* Ethash class testing.
*/

#include <boost/test/unit_test.hpp>
#include <test/tools/libtesteth/TestHelper.h>
#include <libethashseal/Ethash.h>

using namespace std;
using namespace dev;
using namespace dev::eth;
using namespace dev::test;

BOOST_FIXTURE_TEST_SUITE(EthashTests, TestOutputHelper)

BOOST_AUTO_TEST_CASE(calculateDifficultyMetropolisWithoutUncles)
{
ChainOperationParams params;
params.otherParams["metropolisForkBlock"] = "0x1000";

Ethash ethash;
ethash.setChainParams(params);

BlockHeader parentHeader;
parentHeader.clear();
parentHeader.setNumber(0x2000);
parentHeader.setTimestamp(100);
parentHeader.setDifficulty(1000000);

BlockHeader header;
header.clear();
header.setNumber(0x2001);
header.setTimestamp(130);

BOOST_REQUIRE_EQUAL(ethash.calculateDifficulty(header, parentHeader), 999024);
}

BOOST_AUTO_TEST_CASE(calculateDifficultyMetropolisWithUncles)
{
ChainOperationParams params;
params.otherParams["metropolisForkBlock"] = "0x1000";

Ethash ethash;
ethash.setChainParams(params);

BlockHeader parentHeader;
parentHeader.clear();
parentHeader.setNumber(0x2000);
parentHeader.setTimestamp(100);
parentHeader.setDifficulty(1000000);
parentHeader.setSha3Uncles(h256("0x949d991d685738352398dff73219ab19c62c06e6f8ce899fbae755d5127ed1ef"));

BlockHeader header;
header.clear();
header.setNumber(0x2001);
header.setTimestamp(130);

BOOST_REQUIRE_EQUAL(ethash.calculateDifficulty(header, parentHeader), 999512);
}

BOOST_AUTO_TEST_CASE(calculateDifficultyMetropolisMaxAdjustment)
{
ChainOperationParams params;
params.otherParams["metropolisForkBlock"] = "0x1000";

Ethash ethash;
ethash.setChainParams(params);

BlockHeader parentHeader;
parentHeader.clear();
parentHeader.setNumber(0x2000);
parentHeader.setTimestamp(100);
parentHeader.setDifficulty(1000000);

BlockHeader header;
header.clear();
header.setNumber(0x2001);
header.setTimestamp(1100);

BOOST_REQUIRE_EQUAL(ethash.calculateDifficulty(header, parentHeader), 951688);
}

BOOST_AUTO_TEST_SUITE_END()