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

Make difficulty 128 bit #199

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
5 changes: 3 additions & 2 deletions include/BlockchainExplorerData.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
#include <array>
#include <string>
#include <vector>
#include <boost/variant.hpp>

#include "CryptoTypes.h"
#include "CryptoNote.h"
#include "CryptoNoteCore/Difficulty.h"
#include "BlockchainExplorerData.h"
#include <boost/variant.hpp>

namespace CryptoNote {

Expand Down Expand Up @@ -154,7 +155,7 @@ struct BlockDetails {
uint32_t depth = 0;
Crypto::Hash hash;
uint64_t difficulty = 0;
uint64_t cumulativeDifficulty = 0;
CryptoNote::Difficulty cumulativeDifficulty;
uint64_t reward = 0;
uint64_t baseReward = 0;
uint64_t blockSize = 0;
Expand Down
2 changes: 1 addition & 1 deletion include/INode.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ struct BlockHeaderInfo {
uint32_t nonce;
bool isAlternative;
uint32_t depth; // last block index = current block index + depth
difficulty_type difficulty;
uint64_t difficulty;
uint64_t reward;
};

Expand Down
22 changes: 11 additions & 11 deletions src/CryptoNoteCore/Blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -731,14 +731,14 @@ bool Blockchain::getTransactionHeight(const Crypto::Hash &txId, uint32_t& blockH
return false;
}

difficulty_type Blockchain::getDifficultyForNextBlock(const Crypto::Hash &prevHash) {
Difficulty Blockchain::getDifficultyForNextBlock(const Crypto::Hash &prevHash) {
if (prevHash == NULL_HASH) {
return 1;
}

std::lock_guard<decltype(m_blockchain_lock)> lk(m_blockchain_lock);
std::vector<uint64_t> timestamps;
std::vector<difficulty_type> cumulative_difficulties;
std::vector<Difficulty> cumulative_difficulties;

uint32_t height = static_cast<uint32_t>(m_blocks.size());
uint8_t BlockMajorVersion = getBlockMajorVersionForHeight(height);
Expand Down Expand Up @@ -1176,13 +1176,13 @@ bool Blockchain::getHashingBlob(const uint32_t height, BinaryArray& blob) {
return true;
}

bool Blockchain::checkProofOfWork(Crypto::cn_context& context, const Block& block, difficulty_type currentDiffic, Crypto::Hash& proofOfWork) {
bool Blockchain::checkProofOfWork(Crypto::cn_context& context, const Block& block, Difficulty currentDiffic, Crypto::Hash& proofOfWork) {
std::list<Crypto::Hash> dummy_alt_chain;

return checkProofOfWork(context, block, currentDiffic, proofOfWork, dummy_alt_chain, m_no_blobs);
}

bool Blockchain::checkProofOfWork(Crypto::cn_context& context, const Block& block, difficulty_type currentDiffic, Crypto::Hash& proofOfWork, const std::list<Crypto::Hash>& alt_chain, bool no_blobs) {
bool Blockchain::checkProofOfWork(Crypto::cn_context& context, const Block& block, Difficulty currentDiffic, Crypto::Hash& proofOfWork, const std::list<Crypto::Hash>& alt_chain, bool no_blobs) {
if (block.majorVersion < CryptoNote::BLOCK_MAJOR_VERSION_5)
return m_currency.checkProofOfWork(context, block, currentDiffic, proofOfWork);

Expand Down Expand Up @@ -1398,7 +1398,7 @@ bool Blockchain::handle_alternative_block(const Block& b, const Crypto::Hash& id
}

// Check the block's hash against the difficulty target for its alt chain
difficulty_type current_diff = getDifficultyForNextBlock(bei.bl.previousBlockHash);
Difficulty current_diff = getDifficultyForNextBlock(bei.bl.previousBlockHash);
if (!current_diff) {
logger(ERROR, BRIGHT_RED) << "!!!!!!! DIFFICULTY OVERHEAD !!!!!!!";
return false;
Expand Down Expand Up @@ -1688,24 +1688,24 @@ uint64_t Blockchain::blockDifficulty(size_t i) {
std::lock_guard<decltype(m_blockchain_lock)> lk(m_blockchain_lock);
if (!(i < m_blocks.size())) { logger(ERROR, BRIGHT_RED) << "wrong block index i = " << i << " at Blockchain::block_difficulty()"; return false; }
if (i == 0)
return m_blocks[i].cumulative_difficulty;
return m_blocks[i].cumulative_difficulty.convert_to<std::uint64_t>();

return m_blocks[i].cumulative_difficulty - m_blocks[i - 1].cumulative_difficulty;
return (m_blocks[i].cumulative_difficulty - m_blocks[i - 1].cumulative_difficulty).convert_to<std::uint64_t>();
}

uint64_t Blockchain::blockCumulativeDifficulty(size_t i) {
Difficulty Blockchain::blockCumulativeDifficulty(size_t i) {
std::lock_guard<decltype(m_blockchain_lock)> lk(m_blockchain_lock);
if (!(i < m_blocks.size())) { logger(ERROR, BRIGHT_RED) << "wrong block index i = " << i << " at Blockchain::block_difficulty()"; return false; }

return m_blocks[i].cumulative_difficulty;
}

bool Blockchain::getblockEntry(size_t i, uint64_t& block_cumulative_size, difficulty_type& difficulty, uint64_t& already_generated_coins, uint64_t& reward, uint64_t& transactions_count, uint64_t& timestamp) {
bool Blockchain::getblockEntry(size_t i, uint64_t& block_cumulative_size, uint64_t& difficulty, uint64_t& already_generated_coins, uint64_t& reward, uint64_t& transactions_count, uint64_t& timestamp) {
std::lock_guard<decltype(m_blockchain_lock)> lk(m_blockchain_lock);
if (!(i < m_blocks.size())) { logger(ERROR, BRIGHT_RED) << "wrong block index i = " << i << " at Blockchain::get_block_entry()"; return false; }

block_cumulative_size = m_blocks[i].block_cumulative_size;
difficulty = m_blocks[i].cumulative_difficulty - m_blocks[i - 1].cumulative_difficulty;
difficulty = (m_blocks[i].cumulative_difficulty - m_blocks[i - 1].cumulative_difficulty).convert_to<std::uint64_t>();
already_generated_coins = m_blocks[i].already_generated_coins;
reward = m_blocks[i].already_generated_coins - m_blocks[i - 1].already_generated_coins;
timestamp = m_blocks[i].bl.timestamp;
Expand Down Expand Up @@ -2247,7 +2247,7 @@ bool Blockchain::pushBlock(const Block& blockData, const std::vector<Transaction
}

auto targetTimeStart = std::chrono::steady_clock::now();
difficulty_type currentDifficulty = getDifficultyForNextBlock(blockData.previousBlockHash);
Difficulty currentDifficulty = getDifficultyForNextBlock(blockData.previousBlockHash);
auto target_calculating_time = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - targetTimeStart).count();

if (!(currentDifficulty)) {
Expand Down
12 changes: 6 additions & 6 deletions src/CryptoNoteCore/Blockchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ namespace CryptoNote {
uint32_t getCurrentBlockchainHeight(); //TODO rename to getCurrentBlockchainSize
Crypto::Hash getTailId();
Crypto::Hash getTailId(uint32_t& height);
difficulty_type getDifficultyForNextBlock(const Crypto::Hash &prevHash);
Difficulty getDifficultyForNextBlock(const Crypto::Hash &prevHash);
uint64_t getBlockTimestamp(uint32_t height);
uint64_t getCoinsInCirculation();
uint64_t getCoinsInCirculation(uint32_t height);
Expand All @@ -115,8 +115,8 @@ namespace CryptoNote {
bool checkTransactionInputs(const Transaction& tx, uint32_t& pmax_used_block_height, Crypto::Hash& max_used_block_id, BlockInfo* tail = 0);
uint64_t getCurrentCumulativeBlocksizeLimit();
uint64_t blockDifficulty(size_t i);
uint64_t blockCumulativeDifficulty(size_t i);
bool getblockEntry(size_t i, uint64_t& block_cumulative_size, difficulty_type& difficulty, uint64_t& already_generated_coins, uint64_t& reward, uint64_t& transactions_count, uint64_t& timestamp);
Difficulty blockCumulativeDifficulty(size_t i);
bool getblockEntry(size_t i, uint64_t& block_cumulative_size, uint64_t& difficulty, uint64_t& already_generated_coins, uint64_t& reward, uint64_t& transactions_count, uint64_t& timestamp);
bool getBlockContainingTransaction(const Crypto::Hash& txId, Crypto::Hash& blockId, uint32_t& blockHeight);
bool getAlreadyGeneratedCoins(const Crypto::Hash& hash, uint64_t& generatedCoins);
bool getBlockSize(const Crypto::Hash& hash, size_t& size);
Expand Down Expand Up @@ -212,7 +212,7 @@ namespace CryptoNote {
void rebuildCache();
bool storeCache();

bool checkProofOfWork(Crypto::cn_context& context, const Block& block, difficulty_type currentDiffic, Crypto::Hash& proofOfWork);
bool checkProofOfWork(Crypto::cn_context& context, const Block& block, Difficulty currentDiffic, Crypto::Hash& proofOfWork);
bool getBlockLongHash(Crypto::cn_context &context, const Block& b, Crypto::Hash& res);

private:
Expand Down Expand Up @@ -243,7 +243,7 @@ namespace CryptoNote {
Block bl;
uint32_t height;
uint64_t block_cumulative_size;
difficulty_type cumulative_difficulty;
Difficulty cumulative_difficulty;
uint64_t already_generated_coins;
std::vector<TransactionEntry> transactions;

Expand Down Expand Up @@ -312,7 +312,7 @@ namespace CryptoNote {

bool switch_to_alternative_blockchain(const std::list<Crypto::Hash>& alt_chain, bool discard_disconnected_chain);
bool handle_alternative_block(const Block& b, const Crypto::Hash& id, block_verification_context& bvc, bool sendNewAlternativeBlockMessage = true);
bool checkProofOfWork(Crypto::cn_context& context, const Block& block, difficulty_type currentDiffic, Crypto::Hash& proofOfWork, const std::list<Crypto::Hash>& alt_chain, bool no_blobs = false);
bool checkProofOfWork(Crypto::cn_context& context, const Block& block, Difficulty currentDiffic, Crypto::Hash& proofOfWork, const std::list<Crypto::Hash>& alt_chain, bool no_blobs = false);
bool getBlockLongHash(Crypto::cn_context& context, const Block& b, Crypto::Hash& res, const std::list<Crypto::Hash>& alt_chain, bool no_blobs = false);
bool prevalidate_miner_transaction(const Block& b, uint32_t height);
bool validate_miner_transaction(const Block& b, uint32_t height, size_t cumulativeBlockSize, uint64_t alreadyGeneratedCoins, uint64_t fee, uint64_t& reward, int64_t& emissionChange);
Expand Down
10 changes: 5 additions & 5 deletions src/CryptoNoteCore/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ size_t Core::getAlternativeBlocksCount() {
return m_blockchain.getAlternativeBlocksCount();
}

bool Core::getblockEntry(uint32_t height, uint64_t& block_cumulative_size, difficulty_type& difficulty, uint64_t& already_generated_coins, uint64_t& reward, uint64_t& transactions_count, uint64_t& timestamp) {
bool Core::getblockEntry(uint32_t height, uint64_t& block_cumulative_size, uint64_t& difficulty, uint64_t& already_generated_coins, uint64_t& reward, uint64_t& transactions_count, uint64_t& timestamp) {
return m_blockchain.getblockEntry(static_cast<size_t>(height), block_cumulative_size, difficulty, already_generated_coins, reward, transactions_count, timestamp);
}

Expand Down Expand Up @@ -501,7 +501,7 @@ bool Core::add_new_tx(const Transaction& tx, const Crypto::Hash& tx_hash, size_t
return m_mempool.add_tx(tx, tx_hash, blob_size, tvc, keeped_by_block);
}

bool Core::get_block_template(Block& b, const AccountKeys& acc, difficulty_type& diffic, uint32_t& height, const BinaryArray& ex_nonce) {
bool Core::get_block_template(Block& b, const AccountKeys& acc, Difficulty& diffic, uint32_t& height, const BinaryArray& ex_nonce) {
size_t median_size;
uint64_t already_generated_coins;

Expand Down Expand Up @@ -1098,12 +1098,12 @@ bool Core::getBlockTimestamp(uint32_t height, uint64_t& timestamp) {
return true;
}

bool Core::getBlockDifficulty(uint32_t height, difficulty_type& difficulty) {
bool Core::getBlockDifficulty(uint32_t height, uint64_t& difficulty) {
difficulty = m_blockchain.blockDifficulty(height);
return true;
}

bool Core::getBlockCumulativeDifficulty(uint32_t height, difficulty_type& difficulty) {
bool Core::getBlockCumulativeDifficulty(uint32_t height, Difficulty& difficulty) {
difficulty = m_blockchain.blockCumulativeDifficulty(height);
return true;
}
Expand Down Expand Up @@ -1225,7 +1225,7 @@ std::error_code Core::executeLocked(const std::function<std::error_code()>& func
}

uint64_t Core::getNextBlockDifficulty() {
return m_blockchain.getDifficultyForNextBlock(get_tail_id());
return m_blockchain.getDifficultyForNextBlock(get_tail_id()).convert_to<std::uint64_t>();
}

uint64_t Core::getTotalGeneratedAmount() {
Expand Down
8 changes: 4 additions & 4 deletions src/CryptoNoteCore/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace CryptoNote {

//-------------------- IMinerHandler -----------------------
virtual bool handle_block_found(Block& b) override;
virtual bool get_block_template(Block& b, const AccountKeys& acc, difficulty_type& diffic, uint32_t& height, const BinaryArray& ex_nonce) override;
virtual bool get_block_template(Block& b, const AccountKeys& acc, Difficulty& diffic, uint32_t& height, const BinaryArray& ex_nonce) override;
virtual bool getBlockLongHash(Crypto::cn_context &context, const Block& b, Crypto::Hash& res) override;

bool addObserver(ICoreObserver* observer) override;
Expand All @@ -79,8 +79,8 @@ namespace CryptoNote {
virtual bool getBlockReward(uint8_t blockMajorVersion, size_t medianSize, size_t currentBlockSize, uint64_t alreadyGeneratedCoins, uint64_t fee,
uint64_t& reward, int64_t& emissionChange) override;
virtual bool scanOutputkeysForIndices(const KeyInput& txInToKey, std::list<std::pair<Crypto::Hash, size_t>>& outputReferences) override;
virtual bool getBlockDifficulty(uint32_t height, difficulty_type& difficulty) override;
virtual bool getBlockCumulativeDifficulty(uint32_t height, difficulty_type& difficulty) override;
virtual bool getBlockDifficulty(uint32_t height, uint64_t& difficulty) override;
virtual bool getBlockCumulativeDifficulty(uint32_t height, Difficulty& difficulty) override;
virtual bool getBlockTimestamp(uint32_t height, uint64_t& timestamp) override;
virtual bool getBlockContainingTx(const Crypto::Hash& txId, Crypto::Hash& blockId, uint32_t& blockHeight) override;
virtual bool getMultisigOutputReference(const MultisignatureInput& txInMultisig, std::pair<Crypto::Hash, size_t>& output_reference) override;
Expand Down Expand Up @@ -150,7 +150,7 @@ namespace CryptoNote {
virtual std::vector<Crypto::Hash> findBlockchainSupplement(const std::vector<Crypto::Hash>& remoteBlockIds, size_t maxCount,
uint32_t& totalBlockCount, uint32_t& startBlockIndex) override;
bool get_stat_info(core_stat_info& st_inf) override;
virtual bool getblockEntry(uint32_t height, uint64_t& block_cumulative_size, difficulty_type& difficulty, uint64_t& already_generated_coins, uint64_t& reward, uint64_t& transactions_count, uint64_t& timestamp) override;
virtual bool getblockEntry(uint32_t height, uint64_t& block_cumulative_size, uint64_t& difficulty, uint64_t& already_generated_coins, uint64_t& reward, uint64_t& transactions_count, uint64_t& timestamp) override;

virtual bool get_tx_outputs_gindexs(const Crypto::Hash& tx_id, std::vector<uint32_t>& indexs) override;
Crypto::Hash get_tail_id();
Expand Down
28 changes: 28 additions & 0 deletions src/CryptoNoteCore/CryptoNoteSerialization.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// Copyright (c) 2012-2016, The CryptoNote developers, The Bytecoin developers
// Copyright (c) 2014 The Boolberry
// Copyright (c) 2014-2019, The Monero Project
// Copyright (c) 2016-2022, The Karbowanec developers
//
//
// This file is part of Karbo.
//
Expand Down Expand Up @@ -461,5 +465,29 @@ void serialize(KeyPair& keyPair, ISerializer& serializer) {
serializer(keyPair.publicKey, "public_key");
}

void serialize(CryptoNote::Difficulty& difficulty, ISerializer& serializer) {
if (serializer.type() == ISerializer::OUTPUT) {
// store high part
CryptoNote::Difficulty x_ = difficulty >> 64;
uint64_t v = x_.convert_to<uint64_t>();
serializer(v, "hi");

// store low part
x_ = difficulty << 64 >> 64;
v = x_.convert_to<uint64_t>();
serializer(v, "lo");
}
else {
// load high part
uint64_t v = 0;
serializer(v, "hi");
difficulty = v;
// load low part
difficulty = difficulty << 64;
serializer(v, "lo");
difficulty += v;
}
}


} //namespace CryptoNote
8 changes: 7 additions & 1 deletion src/CryptoNoteCore/CryptoNoteSerialization.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Copyright (c) 2012-2016, The CryptoNote developers, The Bytecoin developers
//
// Copyright (c) 2014 The Boolberry
// Copyright (c) 2014-2019, The Monero Project
// Copyright (c) 2016-2022, The Karbowanec developers
//
// This file is part of Karbo.
//
// Karbo is free software: you can redistribute it and/or modify
Expand All @@ -20,6 +23,7 @@
#include "CryptoNoteBasic.h"
#include "crypto/chacha8.h"
#include "Serialization/ISerializer.h"
#include "Difficulty.h"
#include "crypto/crypto.h"

namespace Crypto {
Expand Down Expand Up @@ -66,4 +70,6 @@ void serialize(AccountKeys& keys, ISerializer& s);

void serialize(KeyPair& keyPair, ISerializer& serializer);

void serialize(CryptoNote::Difficulty& difficulty, ISerializer& serializer);

}
Loading