Skip to content
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
5 changes: 4 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ src_libbitcoin_system_la_SOURCES = \
src/config/transaction.cpp \
src/config/url.cpp \
src/config/utilities.cpp \
src/config/version.cpp \
src/crypto/aes256.cpp \
src/crypto/der_parser.cpp \
src/crypto/ec_context.cpp \
Expand Down Expand Up @@ -261,6 +262,7 @@ test_libbitcoin_system_test_SOURCES = \
test/config/printer.cpp \
test/config/url.cpp \
test/config/utilities.cpp \
test/config/version.cpp \
test/crypto/aes256.cpp \
test/crypto/elliptic_curve.cpp \
test/crypto/pseudo_random.cpp \
Expand Down Expand Up @@ -533,7 +535,8 @@ include_bitcoin_system_config_HEADERS = \
include/bitcoin/system/config/script.hpp \
include/bitcoin/system/config/transaction.hpp \
include/bitcoin/system/config/url.hpp \
include/bitcoin/system/config/utilities.hpp
include/bitcoin/system/config/utilities.hpp \
include/bitcoin/system/config/version.hpp

include_bitcoin_system_cryptodir = ${includedir}/bitcoin/system/crypto
include_bitcoin_system_crypto_HEADERS = \
Expand Down
2 changes: 2 additions & 0 deletions builds/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ add_library( ${CANONICAL_LIB_NAME}
"../../src/config/transaction.cpp"
"../../src/config/url.cpp"
"../../src/config/utilities.cpp"
"../../src/config/version.cpp"
"../../src/crypto/aes256.cpp"
"../../src/crypto/der_parser.cpp"
"../../src/crypto/ec_context.cpp"
Expand Down Expand Up @@ -740,6 +741,7 @@ if (with-tests)
"../../test/config/printer.cpp"
"../../test/config/url.cpp"
"../../test/config/utilities.cpp"
"../../test/config/version.cpp"
"../../test/crypto/aes256.cpp"
"../../test/crypto/elliptic_curve.cpp"
"../../test/crypto/pseudo_random.cpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
<ClCompile Include="..\..\..\..\test\config\printer.cpp" />
<ClCompile Include="..\..\..\..\test\config\url.cpp" />
<ClCompile Include="..\..\..\..\test\config\utilities.cpp" />
<ClCompile Include="..\..\..\..\test\config\version.cpp" />
<ClCompile Include="..\..\..\..\test\constants.cpp" />
<ClCompile Include="..\..\..\..\test\constraints.cpp" />
<ClCompile Include="..\..\..\..\test\crypto\aes256.cpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@
<ClCompile Include="..\..\..\..\test\config\utilities.cpp">
<Filter>src\config</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\config\version.cpp">
<Filter>src\config</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\constants.cpp">
<Filter>src</Filter>
</ClCompile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@
</ClCompile>
<ClCompile Include="..\..\..\..\src\config\url.cpp" />
<ClCompile Include="..\..\..\..\src\config\utilities.cpp" />
<ClCompile Include="..\..\..\..\src\config\version.cpp" />
<ClCompile Include="..\..\..\..\src\crypto\aes256.cpp" />
<ClCompile Include="..\..\..\..\src\crypto\der_parser.cpp" />
<ClCompile Include="..\..\..\..\src\crypto\ec_context.cpp" />
Expand Down Expand Up @@ -357,6 +358,7 @@
<ClInclude Include="..\..\..\..\include\bitcoin\system\config\transaction.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\system\config\url.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\system\config\utilities.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\system\config\version.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\system\constants.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\system\constraints.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\system\crypto\aes256.hpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,9 @@
<ClCompile Include="..\..\..\..\src\config\utilities.cpp">
<Filter>src\config</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\config\version.cpp">
<Filter>src\config</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\crypto\aes256.cpp">
<Filter>src\crypto</Filter>
</ClCompile>
Expand Down Expand Up @@ -821,6 +824,9 @@
<ClInclude Include="..\..\..\..\include\bitcoin\system\config\utilities.hpp">
<Filter>include\bitcoin\system\config</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\system\config\version.hpp">
<Filter>include\bitcoin\system\config</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\system\constants.hpp">
<Filter>include\bitcoin\system</Filter>
</ClInclude>
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/system/chain/header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ class BC_API header
void to_data(std::ostream& stream) const NOEXCEPT;
void to_data(writer& sink) const NOEXCEPT;


/// Properties.
/// -----------------------------------------------------------------------
/// Native properties.
Expand All @@ -96,6 +95,7 @@ class BC_API header
/// Computed properties.
uint256_t proof() const NOEXCEPT;
hash_digest hash() const NOEXCEPT;
double difficulty() const NOEXCEPT;

/// Cache and metadata.
/// -----------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions include/bitcoin/system/config/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@
#include <bitcoin/system/config/transaction.hpp>
#include <bitcoin/system/config/url.hpp>
#include <bitcoin/system/config/utilities.hpp>
#include <bitcoin/system/config/version.hpp>

#endif
104 changes: 104 additions & 0 deletions include/bitcoin/system/config/version.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* Copyright (c) 2011-2025 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBBITCOIN_SYSTEM_CONFIG_VERSION_HPP
#define LIBBITCOIN_SYSTEM_CONFIG_VERSION_HPP

#include <memory>
#include <bitcoin/system/define.hpp>

namespace libbitcoin {
namespace system {
namespace config {

/// Container for a protocol version tuple with up to four segments.
/// Segments are non-negative integers, padded with zeros for fewer than four.
/// The format is major.minor[.subminor[.patch]] with at least one dot.
class BC_API version
{
public:
typedef std::shared_ptr<version> ptr;

DEFAULT_COPY_MOVE_DESTRUCT(version);

version() NOEXCEPT;

/// Deserialize from dotted string (throws on invalid format).
version(const std::string& version_str) THROWS;

/// Construct from individual segments (2 to 4, pads with zeros).
version(uint32_t major, uint32_t minor, uint32_t subminor={},
uint32_t patch={}) NOEXCEPT;

/// Properties.
/// -----------------------------------------------------------------------

/// Access to the internal segments array.
const std::array<uint32_t, 4>& segments() const NOEXCEPT;

/// Methods.
/// -----------------------------------------------------------------------

/// The version is 0.0.0.0.
bool is_default() const NOEXCEPT;

/// Serialize to dot string, omitting trailing zero segments (minimum 2).
std::string to_string() const NOEXCEPT;

/// Operators.
/// -----------------------------------------------------------------------

/// Deserialize from input stream (throws on invalid format).
friend std::istream& operator>>(std::istream& input, version& argument) THROWS;

/// Serialize to output stream.
friend std::ostream& operator<<(std::ostream& output,
const version& argument) NOEXCEPT;

private:
// This is not thread safe.
std::array<uint32_t, 4> segments_;
};

/// Lexicographical comparison.
BC_API bool operator==(const version& left, const version& right) NOEXCEPT;
BC_API bool operator!=(const version& left, const version& right) NOEXCEPT;
BC_API bool operator<(const version& left, const version& right) NOEXCEPT;
BC_API bool operator<=(const version& left, const version& right) NOEXCEPT;
BC_API bool operator>(const version& left, const version& right) NOEXCEPT;
BC_API bool operator>=(const version& left, const version& right) NOEXCEPT;

typedef std::vector<version> versions;

} // namespace config
} // namespace system
} // namespace libbitcoin

namespace std
{
template<>
struct hash<bc::system::config::version>
{
size_t operator()(const bc::system::config::version& value) const NOEXCEPT
{
return std::hash<std::string>{}(value.to_string());
}
};
} // namespace std

#endif
10 changes: 9 additions & 1 deletion include/bitcoin/system/impl/serial/deserialize.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <algorithm>
#include <iterator>
#include <sstream>
#include <utility>
#include <bitcoin/system/data/data.hpp>
#include <bitcoin/system/define.hpp>
#include <bitcoin/system/math/math.hpp>
Expand Down Expand Up @@ -107,7 +108,14 @@ bool deserialize(Value& out, const std::string_view& text) NOEXCEPT
// This can convert garbage to zero, use is_ascii_number for pre-assurance.
try
{
std::istringstream istream(trim_copy(text));
auto trimmed = trim_copy(text);
if constexpr (is_integer<Value> && !is_signed<Value>)
{
if (!trimmed.empty() && trimmed.front() == '-')
return false;
}

std::istringstream istream(std::move(trimmed));
istream >> out;
return !istream.fail();
}
Expand Down
23 changes: 23 additions & 0 deletions src/chain/header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,29 @@ hash_digest header::hash() const NOEXCEPT
return digest;
}

// computed, not used in consensus.
double header::difficulty() const NOEXCEPT
{
auto shift = bit_and(shift_right(bits_, 24), 0xff_u32);
auto difference =
static_cast<double>(0x0000ffff_u32) /
static_cast<double>(bit_and(bits_, 0x00ffffff_u32));

while (shift < 29u)
{
difference *= 256.0;
++shift;
}

while (shift > 29u)
{
difference /= 256.0;
--shift;
}

return difference;
}

// Cache and metadata.
// ----------------------------------------------------------------------------

Expand Down
Loading
Loading