aboutsummaryrefslogtreecommitdiff
path: root/src/cryptonote_basic
diff options
context:
space:
mode:
Diffstat (limited to 'src/cryptonote_basic')
-rw-r--r--src/cryptonote_basic/cryptonote_boost_serialization.h36
-rw-r--r--src/cryptonote_basic/cryptonote_format_utils.cpp21
-rw-r--r--src/cryptonote_basic/difficulty.cpp83
-rw-r--r--src/cryptonote_basic/difficulty.h7
-rw-r--r--src/cryptonote_basic/hardfork.cpp3
-rw-r--r--src/cryptonote_basic/miner.cpp5
6 files changed, 139 insertions, 16 deletions
diff --git a/src/cryptonote_basic/cryptonote_boost_serialization.h b/src/cryptonote_basic/cryptonote_boost_serialization.h
index 4ce9f3eeb..3dd98f0c6 100644
--- a/src/cryptonote_basic/cryptonote_boost_serialization.h
+++ b/src/cryptonote_basic/cryptonote_boost_serialization.h
@@ -40,6 +40,7 @@
#include <boost/archive/portable_binary_iarchive.hpp>
#include <boost/archive/portable_binary_oarchive.hpp>
#include "cryptonote_basic.h"
+#include "difficulty.h"
#include "common/unordered_containers_boost_serialization.h"
#include "crypto/crypto.h"
#include "ringct/rctTypes.h"
@@ -339,6 +340,41 @@ namespace boost
if (x.type == rct::RCTTypeBulletproof || x.type == rct::RCTTypeBulletproof2)
a & x.p.pseudoOuts;
}
+
+ template <class Archive>
+ inline void serialize(Archive &a, rct::RCTConfig &x, const boost::serialization::version_type ver)
+ {
+ a & x.range_proof_type;
+ a & x.bp_version;
+ }
+
+ template <class Archive>
+ inline void serialize(Archive &a, cryptonote::difficulty_type &x, const boost::serialization::version_type ver)
+ {
+ if (Archive::is_loading::value)
+ {
+ // load high part
+ uint64_t v = 0;
+ a & v;
+ x = v;
+ // load low part
+ x = x << 64;
+ a & v;
+ x += v;
+ }
+ else
+ {
+ // store high part
+ cryptonote::difficulty_type x_ = x >> 64;
+ uint64_t v = x_.convert_to<uint64_t>();
+ a & v;
+ // store low part
+ x_ = x << 64 >> 64;
+ v = x_.convert_to<uint64_t>();
+ a & v;
+ }
+ }
+
}
}
diff --git a/src/cryptonote_basic/cryptonote_format_utils.cpp b/src/cryptonote_basic/cryptonote_format_utils.cpp
index f40464bd1..094057b1f 100644
--- a/src/cryptonote_basic/cryptonote_format_utils.cpp
+++ b/src/cryptonote_basic/cryptonote_format_utils.cpp
@@ -282,6 +282,11 @@ namespace cryptonote
//---------------------------------------------------------------
bool generate_key_image_helper_precomp(const account_keys& ack, const crypto::public_key& out_key, const crypto::key_derivation& recv_derivation, size_t real_output_index, const subaddress_index& received_index, keypair& in_ephemeral, crypto::key_image& ki, hw::device &hwdev)
{
+ if (hwdev.compute_key_image(ack, out_key, recv_derivation, real_output_index, received_index, in_ephemeral, ki))
+ {
+ return true;
+ }
+
if (ack.m_spend_secret_key == crypto::null_skey)
{
// for watch-only wallet, simply copy the known output pubkey
@@ -982,15 +987,11 @@ namespace cryptonote
{
if (t.version == 1)
return false;
- static const crypto::hash empty_hash = { (char)0x70, (char)0xa4, (char)0x85, (char)0x5d, (char)0x04, (char)0xd8, (char)0xfa, (char)0x7b, (char)0x3b, (char)0x27, (char)0x82, (char)0xca, (char)0x53, (char)0xb6, (char)0x00, (char)0xe5, (char)0xc0, (char)0x03, (char)0xc7, (char)0xdc, (char)0xb2, (char)0x7d, (char)0x7e, (char)0x92, (char)0x3c, (char)0x23, (char)0xf7, (char)0x86, (char)0x01, (char)0x46, (char)0xd2, (char)0xc5 };
const unsigned int unprunable_size = t.unprunable_size;
if (blob && unprunable_size)
{
CHECK_AND_ASSERT_MES(unprunable_size <= blob->size(), false, "Inconsistent transaction unprunable and blob sizes");
- if (blob->size() - unprunable_size == 0)
- res = empty_hash;
- else
- cryptonote::get_blob_hash(epee::span<const char>(blob->data() + unprunable_size, blob->size() - unprunable_size), res);
+ cryptonote::get_blob_hash(epee::span<const char>(blob->data() + unprunable_size, blob->size() - unprunable_size), res);
}
else
{
@@ -1002,10 +1003,7 @@ namespace cryptonote
const size_t mixin = t.vin.empty() ? 0 : t.vin[0].type() == typeid(txin_to_key) ? boost::get<txin_to_key>(t.vin[0]).key_offsets.size() - 1 : 0;
bool r = tt.rct_signatures.p.serialize_rctsig_prunable(ba, t.rct_signatures.type, inputs, outputs, mixin);
CHECK_AND_ASSERT_MES(r, false, "Failed to serialize rct signatures prunable");
- if (ss.str().empty())
- res = empty_hash;
- else
- cryptonote::get_blob_hash(ss.str(), res);
+ cryptonote::get_blob_hash(ss.str(), res);
}
return true;
}
@@ -1042,7 +1040,10 @@ namespace cryptonote
}
// prunable rct
- hashes[2] = pruned_data_hash;
+ if (t.rct_signatures.type == rct::RCTTypeNull)
+ hashes[2] = crypto::null_hash;
+ else
+ hashes[2] = pruned_data_hash;
// the tx hash is the hash of the 3 hashes
crypto::hash res = cn_fast_hash(hashes, sizeof(hashes));
diff --git a/src/cryptonote_basic/difficulty.cpp b/src/cryptonote_basic/difficulty.cpp
index 89b748a83..5162e53e6 100644
--- a/src/cryptonote_basic/difficulty.cpp
+++ b/src/cryptonote_basic/difficulty.cpp
@@ -102,7 +102,7 @@ namespace cryptonote {
return a + b < a || (c && a + b == (uint64_t) -1);
}
- bool check_hash(const crypto::hash &hash, difficulty_type difficulty) {
+ bool check_hash_64(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);
@@ -119,7 +119,7 @@ namespace cryptonote {
return !carry;
}
- difficulty_type next_difficulty(std::vector<std::uint64_t> timestamps, std::vector<difficulty_type> cumulative_difficulties, size_t target_seconds) {
+ uint64_t next_difficulty_64(std::vector<std::uint64_t> timestamps, std::vector<uint64_t> cumulative_difficulties, size_t target_seconds) {
if(timestamps.size() > DIFFICULTY_WINDOW)
{
@@ -150,7 +150,7 @@ namespace cryptonote {
if (time_span == 0) {
time_span = 1;
}
- difficulty_type total_work = cumulative_difficulties[cut_end - 1] - cumulative_difficulties[cut_begin];
+ uint64_t total_work = cumulative_difficulties[cut_end - 1] - cumulative_difficulties[cut_begin];
assert(total_work > 0);
uint64_t low, high;
mul(total_work, target_seconds, low, high);
@@ -162,4 +162,81 @@ namespace cryptonote {
return (low + time_span - 1) / time_span;
}
+#if defined(_MSC_VER)
+#ifdef max
+#undef max
+#endif
+#endif
+
+ const difficulty_type max64bit(std::numeric_limits<std::uint64_t>::max());
+ const boost::multiprecision::uint256_t max128bit(std::numeric_limits<boost::multiprecision::uint128_t>::max());
+ const boost::multiprecision::uint512_t max256bit(std::numeric_limits<boost::multiprecision::uint256_t>::max());
+
+#define FORCE_FULL_128_BITS
+
+ bool check_hash_128(const crypto::hash &hash, difficulty_type difficulty) {
+#ifndef FORCE_FULL_128_BITS
+ // fast check
+ if (difficulty >= max64bit && ((const uint64_t *) &hash)[3] > 0)
+ return false;
+#endif
+ // usual slow check
+ boost::multiprecision::uint512_t hashVal = 0;
+#ifdef FORCE_FULL_128_BITS
+ for(int i = 0; i < 4; i++) { // highest word is zero
+#else
+ for(int i = 1; i < 4; i++) { // highest word is zero
+#endif
+ hashVal <<= 64;
+ hashVal |= swap64le(((const uint64_t *) &hash)[3 - i]);
+ }
+ return hashVal * difficulty <= max256bit;
+ }
+
+ bool check_hash(const crypto::hash &hash, difficulty_type difficulty) {
+ if (difficulty <= max64bit) // if can convert to small difficulty - do it
+ return check_hash_64(hash, difficulty.convert_to<std::uint64_t>());
+ else
+ return check_hash_128(hash, difficulty);
+ }
+
+ difficulty_type next_difficulty(std::vector<uint64_t> timestamps, std::vector<difficulty_type> cumulative_difficulties, size_t target_seconds) {
+ //cutoff DIFFICULTY_LAG
+ if(timestamps.size() > DIFFICULTY_WINDOW)
+ {
+ timestamps.resize(DIFFICULTY_WINDOW);
+ cumulative_difficulties.resize(DIFFICULTY_WINDOW);
+ }
+
+
+ size_t length = timestamps.size();
+ assert(length == cumulative_difficulties.size());
+ if (length <= 1) {
+ return 1;
+ }
+ static_assert(DIFFICULTY_WINDOW >= 2, "Window is too small");
+ assert(length <= DIFFICULTY_WINDOW);
+ sort(timestamps.begin(), timestamps.end());
+ size_t cut_begin, cut_end;
+ static_assert(2 * DIFFICULTY_CUT <= DIFFICULTY_WINDOW - 2, "Cut length is too large");
+ if (length <= DIFFICULTY_WINDOW - 2 * DIFFICULTY_CUT) {
+ cut_begin = 0;
+ cut_end = length;
+ } else {
+ cut_begin = (length - (DIFFICULTY_WINDOW - 2 * DIFFICULTY_CUT) + 1) / 2;
+ cut_end = cut_begin + (DIFFICULTY_WINDOW - 2 * DIFFICULTY_CUT);
+ }
+ assert(/*cut_begin >= 0 &&*/ cut_begin + 2 <= cut_end && cut_end <= length);
+ uint64_t time_span = timestamps[cut_end - 1] - timestamps[cut_begin];
+ if (time_span == 0) {
+ time_span = 1;
+ }
+ difficulty_type total_work = cumulative_difficulties[cut_end - 1] - cumulative_difficulties[cut_begin];
+ assert(total_work > 0);
+ boost::multiprecision::uint256_t res = (boost::multiprecision::uint256_t(total_work) * target_seconds + time_span - 1) / time_span;
+ if(res > max128bit)
+ return 0; // to behave like previous implementation, may be better return max128bit?
+ return res.convert_to<difficulty_type>();
+ }
+
}
diff --git a/src/cryptonote_basic/difficulty.h b/src/cryptonote_basic/difficulty.h
index 8da355b22..f7a9376fb 100644
--- a/src/cryptonote_basic/difficulty.h
+++ b/src/cryptonote_basic/difficulty.h
@@ -32,12 +32,13 @@
#include <cstdint>
#include <vector>
+#include <boost/multiprecision/cpp_int.hpp>
#include "crypto/hash.h"
namespace cryptonote
{
- typedef std::uint64_t difficulty_type;
+ typedef boost::multiprecision::uint128_t difficulty_type;
/**
* @brief checks if a hash fits the given difficulty
@@ -51,6 +52,10 @@ namespace cryptonote
*
* @return true if valid, else false
*/
+ bool check_hash_64(const crypto::hash &hash, uint64_t difficulty);
+ uint64_t next_difficulty_64(std::vector<std::uint64_t> timestamps, std::vector<uint64_t> cumulative_difficulties, size_t target_seconds);
+
+ bool check_hash_128(const crypto::hash &hash, difficulty_type difficulty);
bool check_hash(const crypto::hash &hash, difficulty_type difficulty);
difficulty_type next_difficulty(std::vector<std::uint64_t> timestamps, std::vector<difficulty_type> cumulative_difficulties, size_t target_seconds);
}
diff --git a/src/cryptonote_basic/hardfork.cpp b/src/cryptonote_basic/hardfork.cpp
index ebfe45f37..89bca2f09 100644
--- a/src/cryptonote_basic/hardfork.cpp
+++ b/src/cryptonote_basic/hardfork.cpp
@@ -317,9 +317,12 @@ void HardFork::on_block_popped(uint64_t nblocks)
uint64_t height;
for (height = old_chain_height - 1; height >= new_chain_height; --height)
{
+ version = versions.back();
+ last_versions[version]--;
versions.pop_back();
version = db.get_hard_fork_version(height);
versions.push_front(version);
+ last_versions[version]++;
}
// does not take voting into account
diff --git a/src/cryptonote_basic/miner.cpp b/src/cryptonote_basic/miner.cpp
index 6628c8448..4e2edc20f 100644
--- a/src/cryptonote_basic/miner.cpp
+++ b/src/cryptonote_basic/miner.cpp
@@ -435,14 +435,15 @@ namespace cryptonote
{
MTRACE("Miner has received stop signal");
- if (!is_mining())
+ CRITICAL_REGION_LOCAL(m_threads_lock);
+ bool mining = !m_threads.empty();
+ if (!mining)
{
MTRACE("Not mining - nothing to stop" );
return true;
}
send_stop_signal();
- CRITICAL_REGION_LOCAL(m_threads_lock);
// In case background mining was active and the miner threads are waiting
// on the background miner to signal start.