From 80fd54dc365411ce2bfbd0d2197848ef8ba6e01d Mon Sep 17 00:00:00 2001 From: iceseer Date: Fri, 22 Nov 2024 13:01:08 +0300 Subject: [PATCH] refactoring Signed-off-by: iceseer --- core/scale/encoder/concepts.hpp | 20 +++ core/scale/encoder/primitives.hpp | 75 +++++---- core/scale/kagome_scale.hpp | 251 ++++++++++++++---------------- 3 files changed, 179 insertions(+), 167 deletions(-) create mode 100644 core/scale/encoder/concepts.hpp diff --git a/core/scale/encoder/concepts.hpp b/core/scale/encoder/concepts.hpp new file mode 100644 index 0000000000..f9b9638f4b --- /dev/null +++ b/core/scale/encoder/concepts.hpp @@ -0,0 +1,20 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include + +namespace kagome::scale { + template + concept Invocable = std::is_invocable_v; + + template + concept IsEnum = std::is_enum_v>; + + template + concept IsNotEnum = !std::is_enum_v>; +} // namespace kagome::scale diff --git a/core/scale/encoder/primitives.hpp b/core/scale/encoder/primitives.hpp index db2b8bd179..7fd185c838 100644 --- a/core/scale/encoder/primitives.hpp +++ b/core/scale/encoder/primitives.hpp @@ -7,37 +7,34 @@ #ifndef KAGOME_SCALE_ENCODER_PRIMITIVES_HPP #define KAGOME_SCALE_ENCODER_PRIMITIVES_HPP +#include #include +#include #include #include #include #include -#include "crypto/ecdsa_types.hpp" #include #include #include #include -#include +#include "crypto/ecdsa_types.hpp" +#include "scale/encoder/concepts.hpp" #include "utils/struct_to_tuple.hpp" namespace kagome::scale { - template - concept Invocable = std::is_invocable_v; - - template - concept IsEnum = std::is_enum_v>; - - template - concept IsNotEnum = !std::is_enum_v>; - - constexpr void putByte(const Invocable auto &func, const uint8_t *const val, size_t count); + constexpr void putByte(const Invocable auto &func, + const uint8_t *const val, + size_t count); template constexpr void encode(const Invocable auto &func, const std::tuple &v); template - constexpr void encode(const Invocable auto &func, const T &t, const Args &...args); + constexpr void encode(const Invocable auto &func, + const T &t, + const Args &...args); template constexpr void encode(const Invocable auto &func, const std::vector &c); @@ -52,7 +49,8 @@ namespace kagome::scale { constexpr void encode(const Invocable auto &func, const std::span &c); template - constexpr void encode(const Invocable auto &func, const std::array &c); + constexpr void encode(const Invocable auto &func, + const std::array &c); template constexpr void encode(const Invocable auto &func, const T (&c)[N]); @@ -61,14 +59,16 @@ namespace kagome::scale { constexpr void encode(const Invocable auto &func, const std::map &c); template - constexpr void encode(const Invocable auto &func, const std::shared_ptr &v); + constexpr void encode(const Invocable auto &func, + const std::shared_ptr &v); constexpr void encode(const Invocable auto &func, const std::string_view &v); constexpr void encode(const Invocable auto &func, const std::string &v); template - constexpr void encode(const Invocable auto &func, const std::unique_ptr &v); + constexpr void encode(const Invocable auto &func, + const std::unique_ptr &v); template constexpr void encode(const Invocable auto &func, const std::list &c); @@ -121,11 +121,16 @@ namespace kagome::scale { } constexpr void encode(const Invocable auto &func, const IsEnum auto &value) { - kagome::scale::encode(func, static_cast>>(value)); + kagome::scale::encode( + func, + static_cast>>( + value)); } template - constexpr void encode(const Invocable auto &func, const T &t, const Args &...args) { + constexpr void encode(const Invocable auto &func, + const T &t, + const Args &...args) { kagome::scale::encode(func, t); kagome::scale::encode(func, args...); } @@ -263,9 +268,7 @@ namespace kagome::scale { template void encode(const Invocable auto &func, const std::variant &v) { kagome::scale::encode(func, (uint8_t)v.index()); - std::visit([&](const auto &s) { - kagome::scale::encode(func, s); - }, v); + std::visit([&](const auto &s) { kagome::scale::encode(func, s); }, v); } constexpr void encode(const Invocable auto &func, const std::string &v) { @@ -302,7 +305,8 @@ namespace kagome::scale { } } - void encode(const Invocable auto &func, const ::scale::CompactInteger &value) { + void encode(const Invocable auto &func, + const ::scale::CompactInteger &value) { if (value < 0) { raise(::scale::EncodeError::NEGATIVE_COMPACT_INTEGER); } @@ -382,7 +386,8 @@ namespace kagome::scale { } template - constexpr void encode(const Invocable auto &func, const std::array &c) { + constexpr void encode(const Invocable auto &func, + const std::array &c) { for (const auto &e : c) { kagome::scale::encode(func, e); } @@ -407,7 +412,8 @@ namespace kagome::scale { } template - constexpr void encode(const Invocable auto &func, const std::shared_ptr &v){ + constexpr void encode(const Invocable auto &func, + const std::shared_ptr &v) { if (v == nullptr) { raise(::scale::EncodeError::DEREF_NULLPOINTER); } @@ -415,7 +421,8 @@ namespace kagome::scale { } template - constexpr void encode(const Invocable auto &func, const std::unique_ptr &v){ + constexpr void encode(const Invocable auto &func, + const std::unique_ptr &v) { if (v == nullptr) { raise(::scale::EncodeError::DEREF_NULLPOINTER); } @@ -435,13 +442,15 @@ namespace kagome::scale { } template - constexpr void encode(const Invocable auto &func, const std::tuple &v) { + constexpr void encode(const Invocable auto &func, + const std::tuple &v) { if constexpr (sizeof...(Ts) > 0) { - std::apply([&](const auto &...s) { (..., kagome::scale::encode(func, s)); }, v); + std::apply( + [&](const auto &...s) { (..., kagome::scale::encode(func, s)); }, v); } } - void encode(const Invocable auto &func, const std::optional &v) { + void encode(const Invocable auto &func, const std::optional &v) { enum class OptionalBool : uint8_t { NONE = 0u, OPT_TRUE = 1u, @@ -468,11 +477,17 @@ namespace kagome::scale { } void encode(const Invocable auto &func, const crypto::EcdsaSignature &data) { - kagome::scale::encode(func, static_cast &>(data)); + kagome::scale::encode( + func, + static_cast + &>(data)); } void encode(const Invocable auto &func, const crypto::EcdsaPublicKey &data) { - kagome::scale::encode(func, static_cast &>(data)); + kagome::scale::encode( + func, + static_cast< + const common::Blob &>(data)); } } // namespace kagome::scale diff --git a/core/scale/kagome_scale.hpp b/core/scale/kagome_scale.hpp index 441ca43ad4..f679c8ecee 100644 --- a/core/scale/kagome_scale.hpp +++ b/core/scale/kagome_scale.hpp @@ -12,7 +12,11 @@ #include "common/blob.hpp" #include "consensus/babe/types/babe_block_header.hpp" #include "consensus/babe/types/seal.hpp" +#include "consensus/beefy/types.hpp" +#include "consensus/grandpa/types/equivocation_proof.hpp" #include "network/types/blocks_response.hpp" +#include "network/types/collator_messages_vstaging.hpp" +#include "network/types/dispute_messages.hpp" #include "network/types/roles.hpp" #include "primitives/block_header.hpp" #include "primitives/block_id.hpp" @@ -20,11 +24,8 @@ #include "runtime/runtime_api/parachain_host_types.hpp" #include "scale/big_fixed_integers.hpp" #include "scale/encode_append.hpp" +#include "scale/encoder/concepts.hpp" #include "scale/libp2p_types.hpp" -#include "consensus/beefy/types.hpp" -#include "consensus/grandpa/types/equivocation_proof.hpp" -#include "network/types/collator_messages_vstaging.hpp" -#include "network/types/dispute_messages.hpp" namespace kagome::scale { using CompactInteger = ::scale::CompactInteger; @@ -41,98 +42,86 @@ namespace kagome::scale { using ::scale::decode; - template - constexpr void encode(const F &func, const primitives::BlockHeader &bh) requires std::is_invocable_v; - - template - constexpr void encode(const F &func, const consensus::grandpa::Equivocation &bh) requires std::is_invocable_v; + constexpr void encode(const Invocable auto &func, + const primitives::BlockHeader &bh); - template - constexpr void encode(const F &func, const primitives::BlockHeaderReflection &bh) requires std::is_invocable_v; + constexpr void encode(const Invocable auto &func, + const consensus::grandpa::Equivocation &bh); - template - constexpr void encode(const F &func, const primitives::BlockReflection &bh) requires std::is_invocable_v; + constexpr void encode(const Invocable auto &func, + const primitives::BlockHeaderReflection &bh); - template - constexpr void encode(const F &func, const network::BlocksResponse &b) requires std::is_invocable_v; - - //template - //constexpr void encode(const F &func, const kagome::consensus::beefy::SignedCommitment &b) requires std::is_invocable_v; + constexpr void encode(const Invocable auto &func, + const primitives::BlockReflection &bh); + constexpr void encode(const Invocable auto &func, + const network::BlocksResponse &b); - template - constexpr void encode(const F &func, const kagome::network::vstaging::CompactStatement &c) requires std::is_invocable_v; + constexpr void encode(const Invocable auto &func, + const kagome::network::vstaging::CompactStatement &c); - template + template constexpr void encode( - const F &func, - const common::SLVector &c) requires std::is_invocable_v; + const Invocable auto &func, + const common::SLVector &c); - template - constexpr void encode(const F &func, const Tagged &c) requires std::is_invocable_v; + template + constexpr void encode(const Invocable auto &func, + const Tagged &c); - template - constexpr void encode(const F &func, const common::SLBuffer &c) requires std::is_invocable_v; + template + constexpr void encode(const Invocable auto &func, + const common::SLBuffer &c); - template - constexpr void encode(const F &func, const network::Roles &c) requires std::is_invocable_v; + constexpr void encode(const Invocable auto &func, const network::Roles &c); - template - constexpr void encode(const F &func, const primitives::Other &c) requires std::is_invocable_v; + constexpr void encode(const Invocable auto &func, const primitives::Other &c); - template - constexpr void encode(const F &func, const primitives::Consensus &c) requires std::is_invocable_v; + constexpr void encode(const Invocable auto &func, + const primitives::Consensus &c); - template - constexpr void encode(const F &func, - const runtime::PersistedValidationData &c) requires std::is_invocable_v; + constexpr void encode(const Invocable auto &func, + const runtime::PersistedValidationData &c); - template - constexpr void encode(const F &func, const primitives::Seal &c) requires std::is_invocable_v; + constexpr void encode(const Invocable auto &func, const primitives::Seal &c); - template - constexpr void encode(const F &func, const primitives::PreRuntime &c) requires std::is_invocable_v; + constexpr void encode(const Invocable auto &func, + const primitives::PreRuntime &c); - template - constexpr void encode(const F &func, const primitives::BlockInfo &c) requires std::is_invocable_v; + constexpr void encode(const Invocable auto &func, + const primitives::BlockInfo &c); - template - constexpr void encode(const F &func, - const primitives::RuntimeEnvironmentUpdated &c) requires std::is_invocable_v; + constexpr void encode(const Invocable auto &func, + const primitives::RuntimeEnvironmentUpdated &c); - template - constexpr void encode(const F &func, const ::scale::EncodeOpaqueValue &c) requires std::is_invocable_v; + constexpr void encode(const Invocable auto &func, + const ::scale::EncodeOpaqueValue &c); - template - constexpr void encode(const F &func, - const consensus::babe::BabeBlockHeader &bh) requires std::is_invocable_v; + constexpr void encode(const Invocable auto &func, + const consensus::babe::BabeBlockHeader &bh); - template - constexpr void encode(const F &func, const kagome::network::CandidateCommitments &c) requires std::is_invocable_v; + constexpr void encode(const Invocable auto &func, + const kagome::network::CandidateCommitments &c); - template - constexpr void encode(const F &func, const kagome::network::CandidateReceipt &c) requires std::is_invocable_v; + constexpr void encode(const Invocable auto &func, + const kagome::network::CandidateReceipt &c); - template - constexpr void encode(const F &func, const kagome::network::InvalidDisputeVote &c) requires std::is_invocable_v; + constexpr void encode(const Invocable auto &func, + const kagome::network::InvalidDisputeVote &c); - template - constexpr void encode(const F &func, const kagome::network::ValidDisputeVote &c) requires std::is_invocable_v; + constexpr void encode(const Invocable auto &func, + const kagome::network::ValidDisputeVote &c); - template - constexpr void encode(const F &func, const consensus::grandpa::SignedPrecommit &c) requires std::is_invocable_v; - + constexpr void encode(const Invocable auto &func, + const consensus::grandpa::SignedPrecommit &c); } // namespace kagome::scale #include "scale/encoder/primitives.hpp" namespace kagome::scale { - template - constexpr void encode(const F &func, const primitives::BlockHeader &bh) requires std::is_invocable_v{ + constexpr void encode(const Invocable auto &func, + const primitives::BlockHeader &bh) { kagome::scale::encode(func, bh.parent_hash); kagome::scale::encodeCompact(func, bh.number); kagome::scale::encode(func, bh.state_root); @@ -140,14 +129,14 @@ namespace kagome::scale { kagome::scale::encode(func, bh.digest); } - template - constexpr void encode(const F &func, const primitives::BlockReflection &bh) requires std::is_invocable_v{ + constexpr void encode(const Invocable auto &func, + const primitives::BlockReflection &bh) { kagome::scale::encode(func, bh.header); kagome::scale::encode(func, bh.body); } - template - constexpr void encode(const F &func, const primitives::BlockHeaderReflection &bh) requires std::is_invocable_v{ + constexpr void encode(const Invocable auto &func, + const primitives::BlockHeaderReflection &bh) { kagome::scale::encode(func, bh.parent_hash); kagome::scale::encodeCompact(func, bh.number); kagome::scale::encode(func, bh.state_root); @@ -155,20 +144,13 @@ namespace kagome::scale { kagome::scale::encode(func, bh.digest); } - template - constexpr void encode(const F &func, const network::BlocksResponse &b) requires std::is_invocable_v { + constexpr void encode(const Invocable auto &func, + const network::BlocksResponse &b) { kagome::scale::encode(func, b.blocks); } -// template -// constexpr void encode(const F &func, const kagome::network::vstaging::AttestedCandidateRequest &b) { -// encode(func, b.candidate_hash); -// encode(func, b.mask); -// } - - template - constexpr void encode(const F &func, - const consensus::babe::BabeBlockHeader &bh) requires std::is_invocable_v{ + constexpr void encode(const Invocable auto &func, + const consensus::babe::BabeBlockHeader &bh) { kagome::scale::encode(func, bh.slot_assignment_type); kagome::scale::encode(func, bh.authority_index); kagome::scale::encode(func, bh.slot_number); @@ -178,18 +160,17 @@ namespace kagome::scale { } } - template + template constexpr void encode( - const F &func, - const common::SLVector &c) requires std::is_invocable_v{ - kagome::scale::encode(func, static_cast &>(c)); + const Invocable auto &func, + const common::SLVector &c) { + kagome::scale::encode( + func, static_cast &>(c)); } - template - constexpr void encode(const F &func, const Tagged &c) requires std::is_invocable_v{ + template + constexpr void encode(const Invocable auto &func, + const Tagged &c) { if constexpr (std::is_scalar_v) { kagome::scale::encode(func, c.Wrapper::value); } else { @@ -197,76 +178,71 @@ namespace kagome::scale { } } - //template - //constexpr void encode(const F &func, const kagome::consensus::beefy::SignedCommitment &b) requires std::is_invocable_v { - // kagome::scale::encode(func, b.commitment); - // kagome::scale::encode(func, b.signatures); - //} - - template - constexpr void encode(const F &func, const common::SLBuffer &c) requires std::is_invocable_v { - kagome::scale::encode(func, static_cast &>(c)); + template + constexpr void encode(const Invocable auto &func, + const common::SLBuffer &c) { + kagome::scale::encode( + func, static_cast &>(c)); } - template - constexpr void encode(const F &func, const primitives::Other &c) requires std::is_invocable_v{ + constexpr void encode(const Invocable auto &func, + const primitives::Other &c) { kagome::scale::encode(func, static_cast(c)); } - template - constexpr void encode(const F &func, const primitives::Consensus &c) requires std::is_invocable_v { - kagome::scale::encode(func, static_cast(c)); + constexpr void encode(const Invocable auto &func, + const primitives::Consensus &c) { + kagome::scale::encode( + func, static_cast(c)); } - template - constexpr void encode(const F &func, - const kagome::runtime::PersistedValidationData &c) requires std::is_invocable_v { + constexpr void encode(const Invocable auto &func, + const kagome::runtime::PersistedValidationData &c) { kagome::scale::encode(func, c.parent_head); kagome::scale::encode(func, c.relay_parent_number); kagome::scale::encode(func, c.relay_parent_storage_root); kagome::scale::encode(func, c.max_pov_size); } - template - constexpr void encode(const F &func, const primitives::Seal &c) requires std::is_invocable_v { - kagome::scale::encode(func, static_cast(c)); + constexpr void encode(const Invocable auto &func, const primitives::Seal &c) { + kagome::scale::encode( + func, static_cast(c)); } - template - constexpr void encode(const F &func, const primitives::PreRuntime &c) requires std::is_invocable_v { - kagome::scale::encode(func, static_cast(c)); + constexpr void encode(const Invocable auto &func, + const primitives::PreRuntime &c) { + kagome::scale::encode( + func, static_cast(c)); } - template - constexpr void encode(const F &func, const primitives::BlockInfo &c) requires std::is_invocable_v{ + constexpr void encode(const Invocable auto &func, + const primitives::BlockInfo &c) { kagome::scale::encode(func, c.number); kagome::scale::encode(func, c.hash); } - template - constexpr void encode(const F &func, - const primitives::RuntimeEnvironmentUpdated &c) requires std::is_invocable_v{} + constexpr void encode(const Invocable auto &func, + const primitives::RuntimeEnvironmentUpdated &c) {} - template - constexpr void encode(const F &func, const ::scale::EncodeOpaqueValue &c) requires std::is_invocable_v { + constexpr void encode(const Invocable auto &func, + const ::scale::EncodeOpaqueValue &c) { putByte(func, c.v.data(), c.v.size()); } - template - constexpr void encode(const F &func, const network::Roles &c) requires std::is_invocable_v{ + constexpr void encode(const Invocable auto &func, const network::Roles &c) { kagome::scale::encode(func, c.value); } - template - constexpr void encode(const F &func, const consensus::grandpa::Equivocation &bh) requires std::is_invocable_v { + constexpr void encode(const Invocable auto &func, + const consensus::grandpa::Equivocation &bh) { kagome::scale::encode(func, bh.stage); kagome::scale::encode(func, bh.round_number); kagome::scale::encode(func, bh.first); kagome::scale::encode(func, bh.second); } - template - constexpr void encode(const F &func, const kagome::network::CandidateCommitments &c) requires std::is_invocable_v { + constexpr void encode(const Invocable auto &func, + const kagome::network::CandidateCommitments &c) { kagome::scale::encode(func, c.upward_msgs); kagome::scale::encode(func, c.outbound_hor_msgs); kagome::scale::encode(func, c.opt_para_runtime); @@ -275,35 +251,36 @@ namespace kagome::scale { kagome::scale::encode(func, c.watermark); } - template - constexpr void encode(const F &func, const kagome::network::CandidateReceipt &c) requires std::is_invocable_v { + constexpr void encode(const Invocable auto &func, + const kagome::network::CandidateReceipt &c) { kagome::scale::encode(func, c.descriptor); kagome::scale::encode(func, c.commitments_hash); } - template - constexpr void encode(const F &func, const kagome::network::vstaging::CompactStatement &c) requires std::is_invocable_v { + constexpr void encode(const Invocable auto &func, + const kagome::network::vstaging::CompactStatement &c) { kagome::scale::encode(func, c.header); kagome::scale::encode(func, c.inner_value); } - template - constexpr void encode(const F &func, const kagome::network::InvalidDisputeVote &c) requires std::is_invocable_v { + constexpr void encode(const Invocable auto &func, + const kagome::network::InvalidDisputeVote &c) { kagome::scale::encode(func, c.index); kagome::scale::encode(func, c.signature); kagome::scale::encode(func, c.kind); } - template - constexpr void encode(const F &func, const kagome::network::ValidDisputeVote &c) requires std::is_invocable_v { + constexpr void encode(const Invocable auto &func, + const kagome::network::ValidDisputeVote &c) { kagome::scale::encode(func, c.index); kagome::scale::encode(func, c.signature); kagome::scale::encode(func, c.kind); } - template - constexpr void encode(const F &func, const consensus::grandpa::SignedPrecommit &c) requires std::is_invocable_v { - kagome::scale::encode(func, static_cast(c)); + constexpr void encode(const Invocable auto &func, + const consensus::grandpa::SignedPrecommit &c) { + kagome::scale::encode( + func, static_cast(c)); } } // namespace kagome::scale