Skip to content

Commit

Permalink
build, core: switch to std::bit_cast + bump min Clang to 14 (#1378)
Browse files Browse the repository at this point in the history
  • Loading branch information
yperbasis authored Aug 8, 2023
1 parent d7b4abd commit 3b90f04
Show file tree
Hide file tree
Showing 17 changed files with 54 additions and 58 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ jobs:

linux-wasm-build:
environment:
WASI_SDK_VERSION: 14
WASI_SDK_VERSION: 15
machine:
image: ubuntu-2204:2023.04.2
steps:
Expand Down Expand Up @@ -327,7 +327,7 @@ jobs:
parameters:
clang_version_min:
type: integer
default: 13
default: 14
clang_version_latest:
type: integer
default: 15
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/macOS.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ concurrency:

jobs:
osx:
runs-on: macOS-latest
runs-on: macos-13
env:
DEVELOPER_DIR: /Applications/Xcode_14.3.app/Contents/Developer

# Disable on external PRs
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.repository
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ git submodule update --init --recursive
## Building on Linux & macOS

Building Silkworm requires:
* C++20 compiler: [GCC](https://www.gnu.org/software/gcc/) >= 11.2 or [Clang](https://clang.llvm.org/) >= 13
* C++20 compiler: [GCC](https://www.gnu.org/software/gcc/) >= 11.2 or [Clang](https://clang.llvm.org/) >= 14
or AppleClang ([Xcode](https://developer.apple.com/xcode/) >= 14.3)
* [CMake](https://cmake.org)
* [Conan](https://conan.io)

Expand Down
24 changes: 13 additions & 11 deletions cmd/state-transition/state_transition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,24 @@

#include "state_transition.hpp"

#include <bit>
#include <fstream>
#include <iostream>
#include <memory>
#include <stdexcept>

#include <ethash/keccak.hpp>
#include <nlohmann/json.hpp>

#include "cmd/state-transition/expected_state.hpp"
#include "silkworm/core/common/cast.hpp"
#include "silkworm/core/common/util.hpp"
#include "silkworm/core/execution/execution.hpp"
#include "silkworm/core/protocol/param.hpp"
#include "silkworm/core/protocol/rule_set.hpp"
#include "silkworm/core/rlp/encode_vector.hpp"
#include "silkworm/core/state/in_memory_state.hpp"
#include "silkworm/sentry/common/ecc_key_pair.hpp"
#include <silkworm/core/common/util.hpp>
#include <silkworm/core/execution/execution.hpp>
#include <silkworm/core/protocol/param.hpp>
#include <silkworm/core/protocol/rule_set.hpp>
#include <silkworm/core/rlp/encode_vector.hpp>
#include <silkworm/core/state/in_memory_state.hpp>
#include <silkworm/sentry/common/ecc_key_pair.hpp>

#include "expected_state.hpp"

namespace silkworm::cmd::state_transition {

Expand Down Expand Up @@ -154,7 +156,7 @@ std::unique_ptr<InMemoryState> StateTransition::get_state() {
account.nonce = std::stoull(std::string(preStateValue.at("nonce")), nullptr, 16);

const Bytes code{from_hex(std::string(preStateValue.at("code"))).value()};
account.code_hash = bit_cast<evmc_bytes32>(keccak256(code));
account.code_hash = std::bit_cast<evmc_bytes32>(keccak256(code));
account.incarnation = kDefaultIncarnation;

state->update_account(address, /*initial=*/std::nullopt, account);
Expand Down Expand Up @@ -269,7 +271,7 @@ void StateTransition::validate_transition(const Receipt& receipt, const Expected
} else {
Bytes encoded;
rlp::encode(encoded, receipt.logs);
if (bit_cast<evmc_bytes32>(keccak256(encoded)) != expected_sub_state.logsHash) {
if (std::bit_cast<evmc_bytes32>(keccak256(encoded)) != expected_sub_state.logsHash) {
print_error_message(expected_state, expected_sub_state, "Failed: Logs hash does not match");
failed_count_++;
} else {
Expand Down
4 changes: 2 additions & 2 deletions cmd/test/ethereum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include <atomic>
#include <bit>
#include <filesystem>
#include <iostream>
#include <map>
Expand All @@ -29,7 +30,6 @@

#include <silkworm/core/chain/config.hpp>
#include <silkworm/core/common/as_range.hpp>
#include <silkworm/core/common/cast.hpp>
#include <silkworm/core/common/test_util.hpp>
#include <silkworm/core/execution/evm.hpp>
#include <silkworm/core/protocol/blockchain.hpp>
Expand Down Expand Up @@ -80,7 +80,7 @@ void init_pre_state(const nlohmann::json& pre, State& state) {
const Bytes code{from_hex(j["code"].get<std::string>()).value()};
if (!code.empty()) {
account.incarnation = kDefaultIncarnation;
account.code_hash = bit_cast<evmc_bytes32>(keccak256(code));
account.code_hash = std::bit_cast<evmc_bytes32>(keccak256(code));
state.update_account_code(address, account.incarnation, account.code_hash, code);
}

Expand Down
9 changes: 5 additions & 4 deletions silkworm/core/common/base_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

#include "base.hpp"

#include <bit>

#include <catch2/catch.hpp>

#include <silkworm/core/rlp/encode.hpp>

#include "cast.hpp"
#include "util.hpp"

namespace silkworm {
Expand All @@ -40,16 +41,16 @@ TEST_CASE("Byteviews") {
TEST_CASE("Empty hashes") {
const ByteView empty_string;
const ethash::hash256 hash_of_empty_string{keccak256(empty_string)};
CHECK(bit_cast<evmc_bytes32>(hash_of_empty_string) == kEmptyHash);
CHECK(std::bit_cast<evmc_bytes32>(hash_of_empty_string) == kEmptyHash);

const Bytes empty_list_rlp(1, rlp::kEmptyListCode);
const ethash::hash256 hash_of_empty_list_rlp{keccak256(empty_list_rlp)};
CHECK(bit_cast<evmc_bytes32>(hash_of_empty_list_rlp) == kEmptyListHash);
CHECK(std::bit_cast<evmc_bytes32>(hash_of_empty_list_rlp) == kEmptyListHash);

// See https://github.com/ethereum/yellowpaper/pull/852
const Bytes empty_string_rlp(1, rlp::kEmptyStringCode);
const ethash::hash256 hash_of_empty_string_rlp{keccak256(empty_string_rlp)};
CHECK(bit_cast<evmc_bytes32>(hash_of_empty_string_rlp) == kEmptyRoot);
CHECK(std::bit_cast<evmc_bytes32>(hash_of_empty_string_rlp) == kEmptyRoot);
}

} // namespace silkworm
13 changes: 0 additions & 13 deletions silkworm/core/common/cast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@

// Utilities for type casting

#include <cstring>
#include <string_view>
#include <type_traits>

#include <silkworm/core/common/base.hpp>

Expand All @@ -32,17 +30,6 @@ inline const char* byte_ptr_cast(const uint8_t* ptr) { return reinterpret_cast<c
inline uint8_t* byte_ptr_cast(char* ptr) { return reinterpret_cast<uint8_t*>(ptr); }
inline const uint8_t* byte_ptr_cast(const char* ptr) { return reinterpret_cast<const uint8_t*>(ptr); }

// Backport of C++20 std::bit_cast
// https://en.cppreference.com/w/cpp/numeric/bit_cast
template <class To, class From>
typename std::enable_if_t<
sizeof(To) == sizeof(From) && std::is_trivially_copyable_v<From> && std::is_trivially_copyable_v<To>, To>
bit_cast(const From& src) noexcept {
To dst;
std::memcpy(&dst, &src, sizeof(To));
return dst;
}

inline ByteView string_view_to_byte_view(std::string_view v) { return {byte_ptr_cast(v.data()), v.length()}; }

inline std::string_view byte_view_to_string_view(ByteView v) { return {byte_ptr_cast(v.data()), v.length()}; }
Expand Down
5 changes: 3 additions & 2 deletions silkworm/core/protocol/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

#include "validation.hpp"

#include <silkworm/core/common/cast.hpp>
#include <bit>

#include <silkworm/core/crypto/secp256k1n.hpp>
#include <silkworm/core/rlp/encode_vector.hpp>
#include <silkworm/core/trie/vector_root.hpp>
Expand Down Expand Up @@ -235,7 +236,7 @@ evmc::bytes32 compute_ommers_hash(const BlockBody& body) {

Bytes ommers_rlp;
rlp::encode(ommers_rlp, body.ommers);
return bit_cast<evmc_bytes32>(keccak256(ommers_rlp));
return std::bit_cast<evmc_bytes32>(keccak256(ommers_rlp));
}

} // namespace silkworm::protocol
5 changes: 3 additions & 2 deletions silkworm/core/state/intra_block_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

#include "intra_block_state.hpp"

#include <bit>

#include <ethash/keccak.hpp>

#include <silkworm/core/common/cast.hpp>
#include <silkworm/core/common/util.hpp>

namespace silkworm {
Expand Down Expand Up @@ -222,7 +223,7 @@ evmc::bytes32 IntraBlockState::get_code_hash(const evmc::address& address) const
void IntraBlockState::set_code(const evmc::address& address, ByteView code) noexcept {
auto& obj{get_or_create_object(address)};
journal_.emplace_back(new state::UpdateDelta{address, obj});
obj.current->code_hash = bit_cast<evmc_bytes32>(keccak256(code));
obj.current->code_hash = std::bit_cast<evmc_bytes32>(keccak256(code));

// Don't overwrite already existing code so that views of it
// that were previously returned by get_code() are still valid.
Expand Down
6 changes: 3 additions & 3 deletions silkworm/core/state/intra_block_state_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

#include "intra_block_state.hpp"

#include <bit>
#include <unordered_map>
#include <utility>
#include <vector>

#include <catch2/catch.hpp>

#include <silkworm/core/common/cast.hpp>
#include <silkworm/core/common/random_number.hpp>

#include "in_memory_state.hpp"
Expand Down Expand Up @@ -61,7 +61,7 @@ TEST_CASE("Code view stability") {
Bytes code(random_code());
existing_codes[i] = {addr, code};

evmc_bytes32 code_hash{bit_cast<evmc_bytes32>(keccak256(code))};
evmc_bytes32 code_hash{std::bit_cast<evmc_bytes32>(keccak256(code))};
Account account{.code_hash = code_hash, .incarnation = kDefaultIncarnation};
db.update_account(addr, /*initial=*/std::nullopt, /*current=*/account);
db.update_account_code(addr, kDefaultIncarnation, code_hash, code);
Expand Down Expand Up @@ -94,7 +94,7 @@ TEST_CASE("Code view stability") {

// Check that all previously returned code views have correct code hashes
for (const auto& cv : code_views) {
evmc_bytes32 code_hash{bit_cast<evmc_bytes32>(keccak256(cv.second))};
evmc_bytes32 code_hash{std::bit_cast<evmc_bytes32>(keccak256(cv.second))};
CHECK(state.get_code_hash(cv.first) == code_hash);
}
}
Expand Down
3 changes: 1 addition & 2 deletions silkworm/core/trie/hash_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <ethash/keccak.hpp>

#include <silkworm/core/common/assert.hpp>
#include <silkworm/core/common/cast.hpp>
#include <silkworm/core/common/util.hpp>
#include <silkworm/core/rlp/encode.hpp>

Expand Down Expand Up @@ -132,7 +131,7 @@ evmc::bytes32 HashBuilder::root_hash(bool auto_finalize) {
if (node_ref.length() == kHashLength + 1) {
std::memcpy(res.bytes, &node_ref[1], kHashLength);
} else {
res = bit_cast<evmc_bytes32>(keccak256(node_ref));
res = std::bit_cast<evmc_bytes32>(keccak256(node_ref));
}
return res;
}
Expand Down
5 changes: 3 additions & 2 deletions silkworm/core/types/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

#include "block.hpp"

#include <silkworm/core/common/cast.hpp>
#include <bit>

#include <silkworm/core/protocol/param.hpp>
#include <silkworm/core/rlp/decode_vector.hpp>
#include <silkworm/core/rlp/encode_vector.hpp>
Expand All @@ -28,7 +29,7 @@ BlockNum height(const BlockId& b) { return b.number; }
evmc::bytes32 BlockHeader::hash(bool for_sealing, bool exclude_extra_data_sig) const {
Bytes rlp;
rlp::encode(rlp, *this, for_sealing, exclude_extra_data_sig);
return bit_cast<evmc_bytes32>(keccak256(rlp));
return std::bit_cast<evmc_bytes32>(keccak256(rlp));
}

ethash::hash256 BlockHeader::boundary() const {
Expand Down
5 changes: 3 additions & 2 deletions silkworm/core/types/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

#include "transaction.hpp"

#include <bit>

#include <ethash/keccak.hpp>

#include <silkworm/core/common/cast.hpp>
#include <silkworm/core/common/util.hpp>
#include <silkworm/core/crypto/ecdsa.h>
#include <silkworm/core/protocol/param.hpp>
Expand Down Expand Up @@ -46,7 +47,7 @@ bool Transaction::set_v(const intx::uint256& v) {
evmc::bytes32 Transaction::hash() const {
Bytes rlp;
rlp::encode(rlp, *this, /*wrap_eip2718_into_string=*/false);
return bit_cast<evmc_bytes32>(keccak256(rlp));
return std::bit_cast<evmc_bytes32>(keccak256(rlp));
}

namespace rlp {
Expand Down
4 changes: 2 additions & 2 deletions silkworm/node/db/access_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

#include "access_layer.hpp"

#include <bit>
#include <stdexcept>

#include <silkworm/core/common/assert.hpp>
#include <silkworm/core/common/cast.hpp>
#include <silkworm/core/common/endian.hpp>
#include <silkworm/infra/common/decoding_exception.hpp>
#include <silkworm/infra/common/ensure.hpp>
Expand Down Expand Up @@ -170,7 +170,7 @@ void write_header(RWTxn& txn, const BlockHeader& header, bool with_header_number
evmc::bytes32 write_header_ex(RWTxn& txn, const BlockHeader& header, bool with_header_numbers) {
Bytes value{};
rlp::encode(value, header);
auto header_hash = bit_cast<evmc_bytes32>(keccak256(value)); // avoid header.hash() because it re-does rlp encoding
auto header_hash = std::bit_cast<evmc_bytes32>(keccak256(value)); // avoid header.hash() because it re-does rlp encoding
auto key{db::block_key(header.number, header_hash.bytes)};
auto skey = db::to_slice(key);
auto svalue = db::to_slice(value);
Expand Down
9 changes: 4 additions & 5 deletions silkworm/silkrpc/commands/rpc_api_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "rpc_api.hpp"

#include <bit>
#include <filesystem>
#include <fstream>
#include <iostream>
Expand All @@ -28,20 +29,18 @@
#include <nlohmann/json.hpp>

#include <silkworm/core/chain/genesis.hpp>
#include <silkworm/core/execution/execution.hpp>
#include <silkworm/core/state/in_memory_state.hpp>
#include <silkworm/core/types/block.hpp>
#include <silkworm/core/types/receipt.hpp>
#include <silkworm/infra/common/directories.hpp>
#include <silkworm/node/db/access_layer.hpp>
#include <silkworm/node/db/buffer.hpp>
#include <silkworm/silkrpc/common/constants.hpp>
#include <silkworm/silkrpc/ethdb/file/local_database.hpp>
#include <silkworm/silkrpc/http/request_handler.hpp>
#include <silkworm/silkrpc/test/context_test_base.hpp>

#include "silkworm/core/common/cast.hpp"
#include "silkworm/core/execution/execution.hpp"
#include "silkworm/silkrpc/common/constants.hpp"

namespace silkworm::rpc::commands {

using boost::asio::awaitable;
Expand Down Expand Up @@ -98,7 +97,7 @@ std::unique_ptr<InMemoryState> populate_genesis(db::RWTxn& txn, const std::files

if (account_alloc_json.contains("code")) {
const auto acc_code{from_hex(std::string(account_alloc_json.at("code"))).value()};
const auto acc_codehash{bit_cast<evmc_bytes32>(keccak256(acc_code))};
const auto acc_codehash{std::bit_cast<evmc_bytes32>(keccak256(acc_code))};
account.code_hash = acc_codehash;
state_buffer->update_account_code(account_address, account.incarnation, acc_codehash, acc_code);
}
Expand Down
4 changes: 2 additions & 2 deletions silkworm/silkrpc/core/override_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "override_state.hpp"

#include <bit>
#include <future>
#include <unordered_map>
#include <utility>
Expand All @@ -24,7 +25,6 @@
#include <boost/asio/use_future.hpp>
#include <ethash/keccak.hpp>

#include <silkworm/core/common/cast.hpp>
#include <silkworm/core/common/util.hpp>
#include <silkworm/infra/common/log.hpp>
#include <silkworm/silkrpc/core/rawdb/chain.hpp>
Expand All @@ -47,7 +47,7 @@ OverrideState::OverrideState(silkworm::State& inner_state, const AccountsOverrid
: inner_state_{inner_state}, accounts_overrides_{accounts_overrides} {
for (const auto& [key, value] : accounts_overrides_) {
if (value.code) {
evmc::bytes32 code_hash{bit_cast<evmc_bytes32>(keccak256(value.code.value()))};
evmc::bytes32 code_hash{std::bit_cast<evmc_bytes32>(keccak256(value.code.value()))};
code_hash_.emplace(code_hash, value.code.value());
}
}
Expand Down
Loading

0 comments on commit 3b90f04

Please sign in to comment.