Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hardfork: PoW change to bare yespower #169

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions src/CryptoNoteConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ const uint32_t UPGRADE_HEIGHT_V4_1 = 300000; // LWMA3
const uint32_t UPGRADE_HEIGHT_V4_2 = 500000; // Fee per-byte for extra, ban unmixable denominations
const uint32_t UPGRADE_HEIGHT_V4_3 = 667000; // Fixed min fee + fee per-byte for extra
const uint32_t UPGRADE_HEIGHT_V5 = 700000; // Block v5, back to LWMA1+, Alt. Signed Proof-of-Work
const uint32_t UPGRADE_HEIGHT_V6 = 4294967294; // Block v6

const unsigned UPGRADE_VOTING_THRESHOLD = 90; // percent
const uint32_t UPGRADE_VOTING_WINDOW = EXPECTED_NUMBER_OF_BLOCKS_PER_DAY; // blocks
Expand All @@ -139,6 +140,7 @@ const uint8_t BLOCK_MAJOR_VERSION_2 = 2;
const uint8_t BLOCK_MAJOR_VERSION_3 = 3;
const uint8_t BLOCK_MAJOR_VERSION_4 = 4;
const uint8_t BLOCK_MAJOR_VERSION_5 = 5;
const uint8_t BLOCK_MAJOR_VERSION_6 = 6;
const uint8_t BLOCK_MINOR_VERSION_0 = 0;
const uint8_t BLOCK_MINOR_VERSION_1 = 1;

Expand Down
77 changes: 52 additions & 25 deletions src/CryptoNoteCore/Blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ m_upgradeDetectorV2(currency, m_blocks, BLOCK_MAJOR_VERSION_2, logger),
m_upgradeDetectorV3(currency, m_blocks, BLOCK_MAJOR_VERSION_3, logger),
m_upgradeDetectorV4(currency, m_blocks, BLOCK_MAJOR_VERSION_4, logger),
m_upgradeDetectorV5(currency, m_blocks, BLOCK_MAJOR_VERSION_5, logger),
m_upgradeDetectorV6(currency, m_blocks, BLOCK_MAJOR_VERSION_6, logger),
m_checkpoints(logger),
m_paymentIdIndex(blockchainIndexesEnabled),
m_timestampIndex(blockchainIndexesEnabled),
Expand Down Expand Up @@ -516,7 +517,7 @@ bool Blockchain::init(const std::string& config_folder, bool load_existing) {
rollbackBlockchainTo(lastValidCheckpointHeight);
}

if (!m_upgradeDetectorV2.init() || !m_upgradeDetectorV3.init() || !m_upgradeDetectorV4.init() || !m_upgradeDetectorV5.init()/* || !m_upgradeDetectorV6.init()*/) {
if (!m_upgradeDetectorV2.init() || !m_upgradeDetectorV3.init() || !m_upgradeDetectorV4.init() || !m_upgradeDetectorV5.init() || !m_upgradeDetectorV6.init()) {
logger(ERROR, BRIGHT_RED) << "Failed to initialize upgrade detector. Trying self healing procedure.";
//return false;
}
Expand All @@ -541,15 +542,21 @@ bool Blockchain::init(const std::string& config_folder, bool load_existing) {
" expected=" << static_cast<int>(m_upgradeDetectorV4.targetVersion()) << ". Rollback blockchain to height=" << upgradeHeight;
rollbackBlockchainTo(upgradeHeight);
reinitUpgradeDetectors = true;
} else if (!checkUpgradeHeight(m_upgradeDetectorV5)) {
} else if (!checkUpgradeHeight(m_upgradeDetectorV5)) {
uint32_t upgradeHeight = m_upgradeDetectorV5.upgradeHeight();
logger(WARNING, BRIGHT_YELLOW) << "Invalid block version at " << upgradeHeight + 1 << ": real=" << static_cast<int>(m_blocks[upgradeHeight + 1].bl.majorVersion) <<
" expected=" << static_cast<int>(m_upgradeDetectorV5.targetVersion()) << ". Rollback blockchain to height=" << upgradeHeight;
rollbackBlockchainTo(upgradeHeight);
reinitUpgradeDetectors = true;
} else if (!checkUpgradeHeight(m_upgradeDetectorV6)) {
uint32_t upgradeHeight = m_upgradeDetectorV6.upgradeHeight();
logger(WARNING, BRIGHT_YELLOW) << "Invalid block version at " << upgradeHeight + 1 << ": real=" << static_cast<int>(m_blocks[upgradeHeight + 1].bl.majorVersion) <<
" expected=" << static_cast<int>(m_upgradeDetectorV6.targetVersion()) << ". Rollback blockchain to height=" << upgradeHeight;
rollbackBlockchainTo(upgradeHeight);
reinitUpgradeDetectors = true;
}

if (reinitUpgradeDetectors && (!m_upgradeDetectorV2.init() || !m_upgradeDetectorV3.init() || !m_upgradeDetectorV4.init() || !m_upgradeDetectorV5.init())) {
if (reinitUpgradeDetectors && (!m_upgradeDetectorV2.init() || !m_upgradeDetectorV3.init() || !m_upgradeDetectorV4.init() || !m_upgradeDetectorV5.init() || !m_upgradeDetectorV6.init())) {
logger(ERROR, BRIGHT_RED) << "Failed to initialize upgrade detector";
return false;
}
Expand Down Expand Up @@ -839,7 +846,9 @@ uint64_t Blockchain::getCoinsInCirculation(uint32_t height) {
}

uint8_t Blockchain::getBlockMajorVersionForHeight(uint32_t height) const {
if (height > m_upgradeDetectorV5.upgradeHeight()) {
if (height > m_upgradeDetectorV6.upgradeHeight()) {
return m_upgradeDetectorV6.targetVersion();
} else if (height > m_upgradeDetectorV5.upgradeHeight()) {
return m_upgradeDetectorV5.targetVersion();
} else if (height > m_upgradeDetectorV4.upgradeHeight()) {
return m_upgradeDetectorV4.targetVersion();
Expand Down Expand Up @@ -1233,31 +1242,47 @@ bool Blockchain::get_block_long_hash(Crypto::cn_context &context, const Block& b
}

Crypto::Hash hash_1, hash_2;
uint32_t currentHeight = boost::get<BaseInput>(b.baseTransaction.inputs[0]).blockIndex;
uint32_t maxHeight = std::min<uint32_t>(getCurrentBlockchainHeight() - 1, currentHeight - 1 - m_currency.minedMoneyUnlockWindow());
if (b.majorVersion < CryptoNote::BLOCK_MAJOR_VERSION_6) {
uint32_t currentHeight = boost::get<BaseInput>(b.baseTransaction.inputs[0]).blockIndex;
uint32_t maxHeight = std::min<uint32_t>(getCurrentBlockchainHeight() - 1, currentHeight - 1 - m_currency.minedMoneyUnlockWindow());

#define ITER 128
for (uint32_t i = 0; i < ITER; i++) {
cn_fast_hash(pot.data(), pot.size(), hash_1);

for (uint8_t j = 1; j <= 8; j++) {
uint8_t chunk[4] = {
hash_1.data[j * 4 - 4],
hash_1.data[j * 4 - 3],
hash_1.data[j * 4 - 2],
hash_1.data[j * 4 - 1]
};

uint32_t n = (chunk[0] << 24) |
(chunk[1] << 16) |
(chunk[2] << 8) |
(chunk[3]);

uint32_t height_j = n % maxHeight;
BinaryArray &ba = m_blobs[height_j];
pot.insert(std::end(pot), std::begin(ba), std::end(ba));
for (uint32_t i = 0; i < ITER; i++) {
cn_fast_hash(pot.data(), pot.size(), hash_1);

for (uint8_t j = 1; j <= 8; j++) {
uint8_t chunk[4] = {
hash_1.data[j * 4 - 4],
hash_1.data[j * 4 - 3],
hash_1.data[j * 4 - 2],
hash_1.data[j * 4 - 1]
};

uint32_t n = (chunk[0] << 24) |
(chunk[1] << 16) |
(chunk[2] << 8) |
(chunk[3]);

uint32_t height_j = n % maxHeight;
// use these after removing blobs cache
//std::lock_guard<decltype(m_blockchain_lock)> lk(m_blockchain_lock);
//const Block& bj = m_blocks[height_j].bl;

//BinaryArray ba;
//if (!get_block_hashing_blob(bj, ba)) {
// logger(ERROR, BRIGHT_RED) << "Failed to get_block_hashing_blob of additional block "
// << j << " at height " << height_j;
// return false;
//}
BinaryArray& ba = m_blobs[height_j];

pot.insert(std::end(pot), std::begin(ba), std::end(ba));
}
}
}
else {
cn_fast_hash(pot.data(), pot.size(), hash_1);
}

if (!Crypto::y_slow_hash(pot.data(), pot.size(), hash_1, hash_2)) {
logger(Logging::ERROR, Logging::BRIGHT_RED) << "Error getting Yespower hash";
Expand Down Expand Up @@ -2361,6 +2386,7 @@ bool Blockchain::pushBlock(const Block& blockData, const std::vector<Transaction
m_upgradeDetectorV3.blockPushed();
m_upgradeDetectorV4.blockPushed();
m_upgradeDetectorV5.blockPushed();
m_upgradeDetectorV6.blockPushed();

update_next_cumulative_size_limit();

Expand Down Expand Up @@ -2404,6 +2430,7 @@ void Blockchain::popBlock() {
m_upgradeDetectorV3.blockPopped();
m_upgradeDetectorV4.blockPopped();
m_upgradeDetectorV5.blockPopped();
m_upgradeDetectorV6.blockPopped();
}

bool Blockchain::pushTransaction(BlockEntry& block, const Crypto::Hash& transactionHash, TransactionIndex transactionIndex) {
Expand Down
1 change: 1 addition & 0 deletions src/CryptoNoteCore/Blockchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ namespace CryptoNote {
UpgradeDetector m_upgradeDetectorV3;
UpgradeDetector m_upgradeDetectorV4;
UpgradeDetector m_upgradeDetectorV5;
UpgradeDetector m_upgradeDetectorV6;

PaymentIdIndex m_paymentIdIndex;
TimestampBlocksIndex m_timestampIndex;
Expand Down
2 changes: 1 addition & 1 deletion src/CryptoNoteCore/CryptoNoteSerialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ void serialize(ParentBlockSerializer& pbs, ISerializer& serializer) {

void serializeBlockHeader(BlockHeader& header, ISerializer& serializer) {
serializer(header.majorVersion, "major_version");
if (header.majorVersion > BLOCK_MAJOR_VERSION_5) {
if (header.majorVersion > BLOCK_MAJOR_VERSION_6) {
throw std::runtime_error("Wrong major version");
}

Expand Down
8 changes: 7 additions & 1 deletion src/CryptoNoteCore/Currency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ namespace CryptoNote {
m_upgradeHeightV3 = 60;
m_upgradeHeightV4 = 70;
m_upgradeHeightV5 = 80;
m_upgradeHeightV6 = 100;
m_blocksFileName = "testnet_" + m_blocksFileName;
m_blocksCacheFileName = "testnet_" + m_blocksCacheFileName;
m_blockIndexesFileName = "testnet_" + m_blockIndexesFileName;
Expand Down Expand Up @@ -132,7 +133,10 @@ namespace CryptoNote {
}

uint32_t Currency::upgradeHeight(uint8_t majorVersion) const {
if (majorVersion == BLOCK_MAJOR_VERSION_5) {
if (majorVersion == BLOCK_MAJOR_VERSION_6) {
return m_upgradeHeightV6;
}
else if (majorVersion == BLOCK_MAJOR_VERSION_5) {
return m_upgradeHeightV5;
}
else if (majorVersion == BLOCK_MAJOR_VERSION_4) {
Expand Down Expand Up @@ -781,6 +785,7 @@ namespace CryptoNote {
case BLOCK_MAJOR_VERSION_1:
case BLOCK_MAJOR_VERSION_4:
case BLOCK_MAJOR_VERSION_5:
case BLOCK_MAJOR_VERSION_6:
return checkProofOfWorkV1(context, block, currentDiffic, proofOfWork);

case BLOCK_MAJOR_VERSION_2:
Expand Down Expand Up @@ -870,6 +875,7 @@ namespace CryptoNote {
upgradeHeightV3(parameters::UPGRADE_HEIGHT_V3);
upgradeHeightV4(parameters::UPGRADE_HEIGHT_V4);
upgradeHeightV5(parameters::UPGRADE_HEIGHT_V5);
upgradeHeightV6(parameters::UPGRADE_HEIGHT_V6);
upgradeVotingThreshold(parameters::UPGRADE_VOTING_THRESHOLD);
upgradeVotingWindow(parameters::UPGRADE_VOTING_WINDOW);
upgradeWindow(parameters::UPGRADE_WINDOW);
Expand Down
2 changes: 2 additions & 0 deletions src/CryptoNoteCore/Currency.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ class Currency {
uint32_t m_upgradeHeightV3;
uint32_t m_upgradeHeightV4;
uint32_t m_upgradeHeightV5;
uint32_t m_upgradeHeightV6;
unsigned int m_upgradeVotingThreshold;
uint32_t m_upgradeVotingWindow;
uint32_t m_upgradeWindow;
Expand Down Expand Up @@ -330,6 +331,7 @@ class CurrencyBuilder : boost::noncopyable {
CurrencyBuilder& upgradeHeightV3(uint64_t val) { m_currency.m_upgradeHeightV3 = static_cast<uint32_t>(val); return *this; }
CurrencyBuilder& upgradeHeightV4(uint64_t val) { m_currency.m_upgradeHeightV4 = static_cast<uint32_t>(val); return *this; }
CurrencyBuilder& upgradeHeightV5(uint64_t val) { m_currency.m_upgradeHeightV5 = static_cast<uint32_t>(val); return *this; }
CurrencyBuilder& upgradeHeightV6(uint64_t val) { m_currency.m_upgradeHeightV6 = static_cast<uint32_t>(val); return *this; }
CurrencyBuilder& upgradeVotingThreshold(unsigned int val);
CurrencyBuilder& upgradeVotingWindow(size_t val) { m_currency.m_upgradeVotingWindow = static_cast<uint32_t>(val); return *this; }
CurrencyBuilder& upgradeWindow(size_t val);
Expand Down