Skip to content

Commit

Permalink
[BUGFIX] LWMA-2 Tweaks, move difficulty to separate file, remove type…
Browse files Browse the repository at this point in the history
…def (#448)

* Make chacha8_key trivially copyable

* LWMA-2 Tweaks, move difficulty to separate file, remove typedef

* [BUGFIX] Scan height typo (#449)

* Make chacha8_key trivially copyable

* Fix typo in the wallet service configuration

* Update version.h.in

* We need <algorithm> for min/max in Windows

* Remove un-needed difficulty algorithm
  • Loading branch information
zpalmtree authored and brandonlehmann committed Aug 28, 2018
1 parent 04130f5 commit 3055107
Show file tree
Hide file tree
Showing 21 changed files with 311 additions and 342 deletions.
2 changes: 1 addition & 1 deletion include/INode.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ struct BlockHeaderInfo {
uint32_t nonce;
bool isAlternative;
uint32_t depth; // last block index = current block index + depth
Difficulty difficulty;
uint64_t difficulty;
uint64_t reward;
};

Expand Down
20 changes: 10 additions & 10 deletions src/CryptoNoteCore/BlockchainCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,21 +129,21 @@ BlockchainCache::BlockchainCache(const std::string& filename, const Currency& cu
void BlockchainCache::pushBlock(const CachedBlock& cachedBlock,
const std::vector<CachedTransaction>& cachedTransactions,
const TransactionValidatorState& validatorState, size_t blockSize,
uint64_t generatedCoins, Difficulty blockDifficulty, RawBlock&& rawBlock) {
uint64_t generatedCoins, uint64_t blockDifficulty, RawBlock&& rawBlock) {
//we have to call this function from constructor so it has to be non-virtual
doPushBlock(cachedBlock, cachedTransactions, validatorState, blockSize, generatedCoins, blockDifficulty, std::move(rawBlock));
}

void BlockchainCache::doPushBlock(const CachedBlock& cachedBlock,
const std::vector<CachedTransaction>& cachedTransactions,
const TransactionValidatorState& validatorState, size_t blockSize,
uint64_t generatedCoins, Difficulty blockDifficulty, RawBlock&& rawBlock) {
uint64_t generatedCoins, uint64_t blockDifficulty, RawBlock&& rawBlock) {
logger(Logging::DEBUGGING) << "Pushing block " << cachedBlock.getBlockHash() << " at index " << cachedBlock.getBlockIndex();

assert(blockSize > 0);
assert(blockDifficulty > 0);

Difficulty cumulativeDifficulty = 0;
uint64_t cumulativeDifficulty = 0;
uint64_t alreadyGeneratedCoins = 0;
uint64_t alreadyGeneratedTransactions = 0;

Expand Down Expand Up @@ -221,7 +221,7 @@ PushedBlockInfo BlockchainCache::getPushedBlockInfo(uint32_t blockIndex) const {
pushedBlockInfo.blockDifficulty = cachedBlock.cumulativeDifficulty;
pushedBlockInfo.generatedCoins = cachedBlock.alreadyGeneratedCoins;
} else {
Difficulty cumulativeDifficulty = parent->getLastCumulativeDifficulties(1, startIndex - 1, addGenesisBlock)[0];
uint64_t cumulativeDifficulty = parent->getLastCumulativeDifficulties(1, startIndex - 1, addGenesisBlock)[0];
uint64_t alreadyGeneratedCoins = parent->getAlreadyGeneratedCoins(startIndex - 1);

pushedBlockInfo.blockDifficulty = cachedBlock.cumulativeDifficulty - cumulativeDifficulty;
Expand Down Expand Up @@ -934,11 +934,11 @@ std::vector<uint64_t> BlockchainCache::getLastBlocksSizes(size_t count, uint32_t
return getLastUnits(count, blockIndex, useGenesis, [](const CachedBlockInfo& cb) { return cb.blockSize; });
}

Difficulty BlockchainCache::getDifficultyForNextBlock() const {
uint64_t BlockchainCache::getDifficultyForNextBlock() const {
return getDifficultyForNextBlock(getTopBlockIndex());
}

Difficulty BlockchainCache::getDifficultyForNextBlock(uint32_t blockIndex) const {
uint64_t BlockchainCache::getDifficultyForNextBlock(uint32_t blockIndex) const {
assert(blockIndex <= getTopBlockIndex());
uint8_t nextBlockMajorVersion = getBlockMajorVersionForHeight(blockIndex+1);
auto timestamps = getLastTimestamps(currency.difficultyBlocksCountByBlockVersion(nextBlockMajorVersion, blockIndex), blockIndex, skipGenesisBlock);
Expand All @@ -947,12 +947,12 @@ Difficulty BlockchainCache::getDifficultyForNextBlock(uint32_t blockIndex) const
return currency.getNextDifficulty(nextBlockMajorVersion, blockIndex, std::move(timestamps), std::move(commulativeDifficulties));
}

Difficulty BlockchainCache::getCurrentCumulativeDifficulty() const {
uint64_t BlockchainCache::getCurrentCumulativeDifficulty() const {
assert(!blockInfos.empty());
return blockInfos.get<BlockIndexTag>().back().cumulativeDifficulty;
}

Difficulty BlockchainCache::getCurrentCumulativeDifficulty(uint32_t blockIndex) const {
uint64_t BlockchainCache::getCurrentCumulativeDifficulty(uint32_t blockIndex) const {
assert(!blockInfos.empty());
assert(blockIndex <= getTopBlockIndex());
return blockInfos.get<BlockIndexTag>().at(blockIndex - startIndex).cumulativeDifficulty;
Expand Down Expand Up @@ -980,13 +980,13 @@ uint64_t BlockchainCache::getAlreadyGeneratedTransactions(uint32_t blockIndex) c
return blockInfos.get<BlockIndexTag>().at(blockIndex - startIndex).alreadyGeneratedTransactions;
}

std::vector<Difficulty> BlockchainCache::getLastCumulativeDifficulties(size_t count, uint32_t blockIndex,
std::vector<uint64_t> BlockchainCache::getLastCumulativeDifficulties(size_t count, uint32_t blockIndex,
UseGenesis useGenesis) const {
return getLastUnits(count, blockIndex, useGenesis,
[](const CachedBlockInfo& info) { return info.cumulativeDifficulty; });
}

std::vector<Difficulty> BlockchainCache::getLastCumulativeDifficulties(size_t count) const {
std::vector<uint64_t> BlockchainCache::getLastCumulativeDifficulties(size_t count) const {
return getLastCumulativeDifficulties(count, getTopBlockIndex(), skipGenesisBlock);
}

Expand Down
19 changes: 9 additions & 10 deletions src/CryptoNoteCore/BlockchainCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#include "BlockchainStorage.h"
#include "Common/StringView.h"
#include "Currency.h"
#include "Difficulty.h"
#include "IBlockchainCache.h"
#include "CryptoNoteCore/UpgradeManager.h"

Expand Down Expand Up @@ -61,7 +60,7 @@ struct CachedTransactionInfo {
struct CachedBlockInfo {
Crypto::Hash blockHash;
uint64_t timestamp;
Difficulty cumulativeDifficulty;
uint64_t cumulativeDifficulty;
uint64_t alreadyGeneratedCoins;
uint64_t alreadyGeneratedTransactions;
uint32_t blockSize;
Expand Down Expand Up @@ -102,7 +101,7 @@ class BlockchainCache : public IBlockchainCache {
const TransactionValidatorState& validatorState,
size_t blockSize,
uint64_t generatedCoins,
Difficulty blockDifficulty,
uint64_t blockDifficulty,
RawBlock&& rawBlock) override;

virtual PushedBlockInfo getPushedBlockInfo(uint32_t index) const override;
Expand Down Expand Up @@ -132,14 +131,14 @@ class BlockchainCache : public IBlockchainCache {
std::vector<uint64_t> getLastBlocksSizes(size_t count) const override;
std::vector<uint64_t> getLastBlocksSizes(size_t count, uint32_t blockIndex, UseGenesis) const override;

std::vector<Difficulty> getLastCumulativeDifficulties(size_t count, uint32_t blockIndex, UseGenesis) const override;
std::vector<Difficulty> getLastCumulativeDifficulties(size_t count) const override;
std::vector<uint64_t> getLastCumulativeDifficulties(size_t count, uint32_t blockIndex, UseGenesis) const override;
std::vector<uint64_t> getLastCumulativeDifficulties(size_t count) const override;

Difficulty getDifficultyForNextBlock() const override;
Difficulty getDifficultyForNextBlock(uint32_t blockIndex) const override;
uint64_t getDifficultyForNextBlock() const override;
uint64_t getDifficultyForNextBlock(uint32_t blockIndex) const override;

virtual Difficulty getCurrentCumulativeDifficulty() const override;
virtual Difficulty getCurrentCumulativeDifficulty(uint32_t blockIndex) const override;
virtual uint64_t getCurrentCumulativeDifficulty() const override;
virtual uint64_t getCurrentCumulativeDifficulty(uint32_t blockIndex) const override;

uint64_t getAlreadyGeneratedCoins() const override;
uint64_t getAlreadyGeneratedCoins(uint32_t blockIndex) const override;
Expand Down Expand Up @@ -314,7 +313,7 @@ uint8_t getBlockMajorVersionForHeight(uint32_t height) const;
const TransactionValidatorState& validatorState,
size_t blockSize,
uint64_t generatedCoins,
Difficulty blockDifficulty,
uint64_t blockDifficulty,
RawBlock&& rawBlock);
};

Expand Down
75 changes: 75 additions & 0 deletions src/CryptoNoteCore/CheckDifficulty.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright (c) 2012-2017, The CryptoNote developers, The Bytecoin developers
//
// This file is part of Bytecoin.
//
// Bytecoin is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Bytecoin 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Bytecoin. If not, see <http://www.gnu.org/licenses/>.

#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <vector>

#include "Common/int-util.h"
#include "crypto/hash.h"
#include "CryptoNoteConfig.h"
#include "CheckDifficulty.h"

namespace CryptoNote {

using std::uint64_t;
using std::vector;

#if defined(__SIZEOF_INT128__)

static inline void mul(uint64_t a, uint64_t b, uint64_t &low, uint64_t &high) {
typedef unsigned __int128 uint128_t;
uint128_t res = (uint128_t) a * (uint128_t) b;
low = (uint64_t) res;
high = (uint64_t) (res >> 64);
}

#else

static inline void mul(uint64_t a, uint64_t b, uint64_t &low, uint64_t &high) {
low = mul128(a, b, &high);
}

#endif

static inline bool cadd(uint64_t a, uint64_t b) {
return a + b < a;
}

static inline bool cadc(uint64_t a, uint64_t b, bool c) {
return a + b < a || (c && a + b == (uint64_t) -1);
}

bool check_hash(const Crypto::Hash &hash, uint64_t difficulty) {
uint64_t low, high, top, cur;
// First check the highest word, this will most likely fail for a random hash.
mul(swap64le(((const uint64_t *) &hash)[3]), difficulty, top, high);
if (high != 0) {
return false;
}
mul(swap64le(((const uint64_t *) &hash)[0]), difficulty, low, cur);
mul(swap64le(((const uint64_t *) &hash)[1]), difficulty, low, high);
bool carry = cadd(cur, low);
cur = high;
mul(swap64le(((const uint64_t *) &hash)[2]), difficulty, low, high);
carry = cadc(cur, low, carry);
carry = cadc(high, top, carry);
return !carry;
}
}
27 changes: 27 additions & 0 deletions src/CryptoNoteCore/CheckDifficulty.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2012-2017, The CryptoNote developers, The Bytecoin developers
//
// This file is part of Bytecoin.
//
// Bytecoin is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Bytecoin 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Bytecoin. If not, see <http://www.gnu.org/licenses/>.

#pragma once

#include <cstdint>
#include <vector>

#include "crypto/hash.h"

namespace CryptoNote {
bool check_hash(const Crypto::Hash &hash, uint64_t difficulty);
}
6 changes: 3 additions & 3 deletions src/CryptoNoteCore/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ void Core::getTransactions(const std::vector<Crypto::Hash>& transactionHashes, s
missedHashes.insert(missedHashes.end(), leftTransactions.begin(), leftTransactions.end());
}

Difficulty Core::getBlockDifficulty(uint32_t blockIndex) const {
uint64_t Core::getBlockDifficulty(uint32_t blockIndex) const {
throwIfNotInitialized();
IBlockchainCache* mainChain = chainsLeaves[0];
auto difficulties = mainChain->getLastCumulativeDifficulties(2, blockIndex, addGenesisBlock);
Expand All @@ -500,7 +500,7 @@ Difficulty Core::getBlockDifficulty(uint32_t blockIndex) const {
}

// TODO: just use mainChain->getDifficultyForNextBlock() ?
Difficulty Core::getDifficultyForNextBlock() const {
uint64_t Core::getDifficultyForNextBlock() const {
throwIfNotInitialized();
IBlockchainCache* mainChain = chainsLeaves[0];

Expand Down Expand Up @@ -1095,7 +1095,7 @@ bool Core::getPoolChangesLite(const Crypto::Hash& lastBlockHash, const std::vect
}

bool Core::getBlockTemplate(BlockTemplate& b, const AccountPublicAddress& adr, const BinaryArray& extraNonce,
Difficulty& difficulty, uint32_t& height) const {
uint64_t& difficulty, uint32_t& height) const {
throwIfNotInitialized();

height = getTopBlockIndex() + 1;
Expand Down
6 changes: 3 additions & 3 deletions src/CryptoNoteCore/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ class Core : public ICore, public ICoreInformation {
virtual bool hasTransaction(const Crypto::Hash& transactionHash) const override;
virtual void getTransactions(const std::vector<Crypto::Hash>& transactionHashes, std::vector<BinaryArray>& transactions, std::vector<Crypto::Hash>& missedHashes) const override;

virtual Difficulty getBlockDifficulty(uint32_t blockIndex) const override;
virtual Difficulty getDifficultyForNextBlock() const override;
virtual uint64_t getBlockDifficulty(uint32_t blockIndex) const override;
virtual uint64_t getDifficultyForNextBlock() const override;

virtual std::error_code addBlock(const CachedBlock& cachedBlock, RawBlock&& rawBlock) override;
virtual std::error_code addBlock(RawBlock&& rawBlock) override;
Expand All @@ -95,7 +95,7 @@ class Core : public ICore, public ICoreInformation {
virtual bool getPoolChangesLite(const Crypto::Hash& lastBlockHash, const std::vector<Crypto::Hash>& knownHashes, std::vector<TransactionPrefixInfo>& addedTransactions,
std::vector<Crypto::Hash>& deletedTransactions) const override;

virtual bool getBlockTemplate(BlockTemplate& b, const AccountPublicAddress& adr, const BinaryArray& extraNonce, Difficulty& difficulty, uint32_t& height) const override;
virtual bool getBlockTemplate(BlockTemplate& b, const AccountPublicAddress& adr, const BinaryArray& extraNonce, uint64_t& difficulty, uint32_t& height) const override;

virtual CoreStatistics getCoreStatistics() const override;

Expand Down
Loading

0 comments on commit 3055107

Please sign in to comment.