From 37c9b057df6d942bbca90dd42faa5740d25c5277 Mon Sep 17 00:00:00 2001 From: Vlad Dobromyslov Date: Thu, 24 Feb 2022 09:41:25 +0300 Subject: [PATCH] #235 Add custom funtions for tournament_object --- .../graphene/chain/tournament_object.hpp | 143 ++++++++++++++++-- libraries/chain/tournament_object.cpp | 52 +++++++ 2 files changed, 182 insertions(+), 13 deletions(-) diff --git a/libraries/chain/include/graphene/chain/tournament_object.hpp b/libraries/chain/include/graphene/chain/tournament_object.hpp index 3dcc38ca..f3c8e610 100644 --- a/libraries/chain/include/graphene/chain/tournament_object.hpp +++ b/libraries/chain/include/graphene/chain/tournament_object.hpp @@ -3,6 +3,17 @@ #include #include #include +#include +#include + +namespace graphene { namespace chain { +class tournament_object; +} } + +namespace fc { +void to_variant(const graphene::chain::tournament_object& tournament_obj, fc::variant& v, uint32_t max_depth = 1); +void from_variant(const fc::variant& v, graphene::chain::tournament_object& tournament_obj, uint32_t max_depth = 1); +} //end namespace fc namespace graphene { namespace chain { class database; @@ -87,6 +98,21 @@ namespace graphene { namespace chain { time_point_sec get_registration_deadline() const { return options.registration_deadline; } + // serialization functions: + // for serializing to raw, go through a temporary sstream object to avoid + // having to implement serialization in the header file + template + friend Stream& operator<<( Stream& s, const tournament_object& tournament_obj ); + + template + friend Stream& operator>>( Stream& s, tournament_object& tournament_obj ); + + friend void ::fc::to_variant(const graphene::chain::tournament_object& tournament_obj, fc::variant& v, uint32_t max_depth); + friend void ::fc::from_variant(const fc::variant& v, graphene::chain::tournament_object& tournament_obj, uint32_t max_depth); + + void pack_impl(std::ostream& stream) const; + void unpack_impl(std::istream& stream); + /// called by database maintenance code when registration for this contest has expired void on_registration_deadline_passed(database& db); void on_player_registered(database& db, account_id_type payer_id, account_id_type player_id); @@ -127,6 +153,59 @@ namespace graphene { namespace chain { > tournament_details_object_multi_index_type; typedef generic_index tournament_details_index; + template + inline Stream& operator<<( Stream& s, const tournament_object& tournament_obj ) + { + fc_elog(fc::logger::get("tournament"), "In tournament_obj to_raw"); + // pack all fields exposed in the header in the usual way + // instead of calling the derived pack, just serialize the one field in the base class + // fc::raw::pack >(s, tournament_obj); + fc::raw::pack(s, tournament_obj.id); + fc::raw::pack(s, tournament_obj.creator); + fc::raw::pack(s, tournament_obj.options); + fc::raw::pack(s, tournament_obj.start_time); + fc::raw::pack(s, tournament_obj.end_time); + fc::raw::pack(s, tournament_obj.prize_pool); + fc::raw::pack(s, tournament_obj.registered_players); + fc::raw::pack(s, tournament_obj.tournament_details_id); + + // fc::raw::pack the contents hidden in the impl class + std::ostringstream stream; + tournament_obj.pack_impl(stream); + std::string stringified_stream(stream.str()); + fc_elog(fc::logger::get("tournament"), "Serialized state ${state} to bytes ${bytes}", + ("state", tournament_obj.get_state())("bytes", fc::to_hex(stringified_stream.c_str(), stringified_stream.size()))); + fc::raw::pack(s, stream.str()); + + return s; + } + + template + inline Stream& operator>>( Stream& s, tournament_object& tournament_obj ) + { + fc_elog(fc::logger::get("tournament"), "In tournament_obj from_raw"); + // unpack all fields exposed in the header in the usual way + //fc::raw::unpack >(s, tournament_obj); + fc::raw::unpack(s, tournament_obj.id); + fc::raw::unpack(s, tournament_obj.creator); + fc::raw::unpack(s, tournament_obj.options); + fc::raw::unpack(s, tournament_obj.start_time); + fc::raw::unpack(s, tournament_obj.end_time); + fc::raw::unpack(s, tournament_obj.prize_pool); + fc::raw::unpack(s, tournament_obj.registered_players); + fc::raw::unpack(s, tournament_obj.tournament_details_id); + + // fc::raw::unpack the contents hidden in the impl class + std::string stringified_stream; + fc::raw::unpack(s, stringified_stream); + std::istringstream stream(stringified_stream); + tournament_obj.unpack_impl(stream); + fc_elog(fc::logger::get("tournament"), "Deserialized state ${state} from bytes ${bytes}", + ("state", tournament_obj.get_state())("bytes", fc::to_hex(stringified_stream.c_str(), stringified_stream.size()))); + + return s; + } + /** * @brief This secondary index will allow a reverse lookup of all tournaments * a particular account has registered for. This will be attached @@ -159,21 +238,59 @@ FC_REFLECT_DERIVED(graphene::chain::tournament_details_object, (graphene::db::ob (players_payers) (matches)) -//FC_REFLECT_TYPENAME(graphene::chain::tournament_object) // manually serialized -FC_REFLECT_DERIVED(graphene::chain::tournament_object, (graphene::db::object), - (creator) - (options) - (start_time) - (end_time) - (prize_pool) - (registered_players) - (tournament_details_id)) - -GRAPHENE_EXTERNAL_SERIALIZATION( extern, graphene::chain::tournament_object ) - FC_REFLECT_ENUM(graphene::chain::tournament_state, (accepting_registrations) (awaiting_start) (in_progress) (registration_period_expired) - (concluded)) \ No newline at end of file + (concluded)) + +namespace fc { + +template<> +template<> +inline void if_enum::from_variant(const variant &vo, graphene::chain::tournament_object &v, uint32_t max_depth) { + from_variant(vo, v, max_depth); +} + +template<> +template<> +inline void if_enum::to_variant(const graphene::chain::tournament_object &v, variant &vo, uint32_t max_depth) { + to_variant(v, vo, max_depth); +} + +namespace raw { namespace detail { + +template<> +template<> +inline void if_enum::pack(fc::datastream &s, const graphene::chain::tournament_object &v, uint32_t) { + s << v; +} + +template<> +template<> +inline void if_enum::pack(fc::datastream &s, const graphene::chain::tournament_object &v, uint32_t) { + s << v; +} + +template<> +template<> +inline void if_enum::unpack(fc::datastream &s, graphene::chain::tournament_object &v, uint32_t) { + s >> v; +} + +} } + +template <> +struct get_typename { + static const char *name() { + return "graphene::chain::tournament_object"; + } +}; +template <> +struct reflector { + typedef graphene::chain::tournament_object type; + typedef fc::true_type is_defined; + typedef fc::false_type is_enum; +}; +} // namespace fc \ No newline at end of file diff --git a/libraries/chain/tournament_object.cpp b/libraries/chain/tournament_object.cpp index 7babb1ba..680943bd 100644 --- a/libraries/chain/tournament_object.cpp +++ b/libraries/chain/tournament_object.cpp @@ -28,6 +28,8 @@ #include #include +#include +#include #include namespace graphene { namespace chain { @@ -531,6 +533,18 @@ namespace graphene { namespace chain { return state; } + void tournament_object::pack_impl(std::ostream& stream) const + { + boost::archive::binary_oarchive oa(stream, boost::archive::no_header|boost::archive::no_codecvt|boost::archive::no_xml_tag_checking); + oa << my->state_machine; + } + + void tournament_object::unpack_impl(std::istream& stream) + { + boost::archive::binary_iarchive ia(stream, boost::archive::no_header|boost::archive::no_codecvt|boost::archive::no_xml_tag_checking); + ia >> my->state_machine; + } + void tournament_object::on_registration_deadline_passed(database& db) { my->state_machine.process_event(registration_deadline_passed(db)); @@ -707,4 +721,42 @@ namespace graphene { namespace chain { } } } // graphene::chain +namespace fc { +// Manually reflect tournament_object to variant to properly reflect "state" +void to_variant(const graphene::chain::tournament_object& tournament_obj, fc::variant& v, uint32_t max_depth) +{ + fc_elog(fc::logger::get("tournament"), "In tournament_obj to_variant"); + elog("In tournament_obj to_variant"); + fc::mutable_variant_object o; + o("id", fc::variant(tournament_obj.id, max_depth)) + ("creator", fc::variant(tournament_obj.creator, max_depth)) + ("options", fc::variant(tournament_obj.options, max_depth)) + ("start_time", fc::variant(tournament_obj.start_time, max_depth)) + ("end_time", fc::variant(tournament_obj.end_time, max_depth)) + ("prize_pool", fc::variant(tournament_obj.prize_pool, max_depth)) + ("registered_players", fc::variant(tournament_obj.registered_players, max_depth)) + ("tournament_details_id", fc::variant(tournament_obj.tournament_details_id, max_depth)) + ("state", fc::variant(tournament_obj.get_state(), max_depth)); + + v = o; +} + +// Manually reflect tournament_object to variant to properly reflect "state" +void from_variant(const fc::variant& v, graphene::chain::tournament_object& tournament_obj, uint32_t max_depth) +{ + fc_elog(fc::logger::get("tournament"), "In tournament_obj from_variant"); + tournament_obj.id = v["id"].as( max_depth ); + tournament_obj.creator = v["creator"].as( max_depth ); + tournament_obj.options = v["options"].as( max_depth ); + tournament_obj.start_time = v["start_time"].as >( max_depth ); + tournament_obj.end_time = v["end_time"].as >( max_depth ); + tournament_obj.prize_pool = v["prize_pool"].as( max_depth ); + tournament_obj.registered_players = v["registered_players"].as( max_depth ); + tournament_obj.tournament_details_id = v["tournament_details_id"].as( max_depth ); + graphene::chain::tournament_state state = v["state"].as( max_depth ); + const_cast(tournament_obj.my->state_machine.current_state())[0] = (int)state; +} +} //end namespace fc + +