From 3a7e65c888a758b20ddc6a0eb0bdeb153d2dba71 Mon Sep 17 00:00:00 2001 From: theoreticalbts Date: Mon, 17 Aug 2015 12:59:51 -0400 Subject: [PATCH] chain_property_object: Implement chain_property_object as container for unchangeable properties set at genesis #238 --- libraries/app/api.cpp | 6 +++ libraries/app/application.cpp | 4 +- libraries/app/include/graphene/app/api.hpp | 17 +++++--- libraries/chain/db_getter.cpp | 8 +++- libraries/chain/db_init.cpp | 12 +++++- .../graphene/chain/chain_property_object.hpp | 40 +++++++++++++++++++ .../chain/include/graphene/chain/database.hpp | 1 + .../include/graphene/chain/protocol/types.hpp | 6 ++- libraries/wallet/wallet.cpp | 7 +++- tests/tests/operation_tests2.cpp | 2 +- 10 files changed, 90 insertions(+), 13 deletions(-) create mode 100644 libraries/chain/include/graphene/chain/chain_property_object.hpp diff --git a/libraries/app/api.cpp b/libraries/app/api.cpp index 9b20df99..b587dba3 100644 --- a/libraries/app/api.cpp +++ b/libraries/app/api.cpp @@ -136,6 +136,11 @@ namespace graphene { namespace app { return result; } + chain_property_object database_api::get_chain_properties()const + { + return _db.get(chain_property_id_type()); + } + global_property_object database_api::get_global_properties()const { return _db.get(global_property_id_type()); @@ -780,6 +785,7 @@ namespace graphene { namespace app { } case impl_block_summary_object_type:{ } case impl_account_transaction_history_object_type:{ } case impl_witness_schedule_object_type: { + } case impl_chain_property_object_type: { } } } diff --git a/libraries/app/application.cpp b/libraries/app/application.cpp index da87cf93..d7e35936 100644 --- a/libraries/app/application.cpp +++ b/libraries/app/application.cpp @@ -266,7 +266,7 @@ namespace detail { } if (!_options->count("genesis-json") && - _chain_db->get_global_properties().chain_id != graphene::egenesis::get_egenesis_chain_id()) { + _chain_db->get_chain_id() != graphene::egenesis::get_egenesis_chain_id()) { elog("Detected old database. Nuking and starting over."); _chain_db->wipe(_data_dir / "blockchain", true); _chain_db.reset(); @@ -450,7 +450,7 @@ namespace detail { virtual chain_id_type get_chain_id()const override { - return _chain_db->get_global_properties().chain_id; + return _chain_db->get_chain_id(); } /** diff --git a/libraries/app/include/graphene/app/api.hpp b/libraries/app/include/graphene/app/api.hpp index 129da370..639545eb 100644 --- a/libraries/app/include/graphene/app/api.hpp +++ b/libraries/app/include/graphene/app/api.hpp @@ -24,13 +24,14 @@ #include #include -#include #include -#include -#include -#include -#include #include +#include +#include +#include +#include +#include +#include #include @@ -84,6 +85,11 @@ namespace graphene { namespace app { */ processed_transaction get_transaction( uint32_t block_num, uint32_t trx_in_block )const; + /** + * @brief Retrieve the @ref chain_property_object associated with the chain + */ + chain_property_object get_chain_properties()const; + /** * @brief Retrieve the current @ref global_property_object */ @@ -546,6 +552,7 @@ FC_API(graphene::app::database_api, (get_block_header) (get_block) (get_transaction) + (get_chain_properties) (get_global_properties) (get_chain_id) (get_dynamic_global_properties) diff --git a/libraries/chain/db_getter.cpp b/libraries/chain/db_getter.cpp index b10c702d..d9a08105 100644 --- a/libraries/chain/db_getter.cpp +++ b/libraries/chain/db_getter.cpp @@ -19,6 +19,7 @@ #include #include +#include #include namespace graphene { namespace chain { @@ -33,6 +34,11 @@ const global_property_object& database::get_global_properties()const return get( global_property_id_type() ); } +const chain_property_object& database::get_chain_properties()const +{ + return get( chain_property_id_type() ); +} + const dynamic_global_property_object&database::get_dynamic_global_properties() const { return get( dynamic_global_property_id_type() ); @@ -65,7 +71,7 @@ decltype( chain_parameters::block_interval ) database::block_interval( )const const chain_id_type& database::get_chain_id( )const { - return get_global_properties().chain_id; + return get_chain_properties().chain_id; } const node_property_object& database::get_node_properties()const diff --git a/libraries/chain/db_init.cpp b/libraries/chain/db_init.cpp index 2ab28d9f..e043588f 100644 --- a/libraries/chain/db_init.cpp +++ b/libraries/chain/db_init.cpp @@ -20,10 +20,11 @@ #include #include +#include #include +#include #include #include -#include #include #include #include @@ -193,6 +194,7 @@ void database::initialize_indexes() add_index< primary_index> >(); add_index< primary_index> >(); add_index< primary_index> >(); + add_index< primary_index > >(); } void database::init_genesis(const genesis_state_type& genesis_state) @@ -301,9 +303,11 @@ void database::init_genesis(const genesis_state_type& genesis_state) assert( get_balance(account_id_type(), asset_id_type()) == asset(dyn_asset.current_supply) ); (void)core_asset; + chain_id_type chain_id = genesis_state.compute_chain_id(); + // Create global properties create([&](global_property_object& p) { - p.chain_id = genesis_state.compute_chain_id(); + p.chain_id = chain_id; p.parameters = genesis_state.initial_parameters; // Set fees to zero initially, so that genesis initialization needs not pay them // We'll fix it at the end of the function @@ -315,6 +319,10 @@ void database::init_genesis(const genesis_state_type& genesis_state) p.dynamic_flags = 0; p.witness_budget = 0; }); + create([&](chain_property_object& p) + { + p.chain_id = chain_id; + } ); create([&](block_summary_object&) {}); // Create initial accounts diff --git a/libraries/chain/include/graphene/chain/chain_property_object.hpp b/libraries/chain/include/graphene/chain/chain_property_object.hpp new file mode 100644 index 00000000..2b8a8054 --- /dev/null +++ b/libraries/chain/include/graphene/chain/chain_property_object.hpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2015, Cryptonomex, Inc. + * All rights reserved. + * + * This source code is provided for evaluation in private test networks only, until September 8, 2015. After this date, this license expires and + * the code may not be used, modified or distributed for any purpose. Redistribution and use in source and binary forms, with or without modification, + * are permitted until September 8, 2015, provided that the following conditions are met: + * + * 1. The code and/or derivative works are used only for private test networks consisting of no more than 10 P2P nodes. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#pragma once + +namespace graphene { namespace chain { + +class chain_property_object; + +/** + * Contains invariants which are set at genesis and never changed. + */ +class chain_property_object : public abstract_object +{ + public: + static const uint8_t space_id = implementation_ids; + static const uint8_t type_id = impl_chain_property_object_type; + + chain_id_type chain_id; +}; + +} } + +FC_REFLECT_DERIVED( graphene::chain::chain_property_object, (graphene::db::object), + (chain_id) + ) diff --git a/libraries/chain/include/graphene/chain/database.hpp b/libraries/chain/include/graphene/chain/database.hpp index d31dd264..755d18a6 100644 --- a/libraries/chain/include/graphene/chain/database.hpp +++ b/libraries/chain/include/graphene/chain/database.hpp @@ -254,6 +254,7 @@ namespace graphene { namespace chain { const chain_id_type& get_chain_id()const; const asset_object& get_core_asset()const; + const chain_property_object& get_chain_properties()const; const global_property_object& get_global_properties()const; const dynamic_global_property_object& get_dynamic_global_properties()const; const node_property_object& get_node_properties()const; diff --git a/libraries/chain/include/graphene/chain/protocol/types.hpp b/libraries/chain/include/graphene/chain/protocol/types.hpp index e9b5ea7b..af8a2ace 100644 --- a/libraries/chain/include/graphene/chain/protocol/types.hpp +++ b/libraries/chain/include/graphene/chain/protocol/types.hpp @@ -141,7 +141,8 @@ namespace graphene { namespace chain { impl_block_summary_object_type, impl_account_transaction_history_object_type, impl_witness_schedule_object_type, - impl_blinded_balance_object_type + impl_blinded_balance_object_type, + impl_chain_property_object_type }; enum meta_info_object_type @@ -194,6 +195,7 @@ namespace graphene { namespace chain { class transaction_object; class block_summary_object; class account_transaction_history_object; + class chain_property_object; typedef object_id< implementation_ids, impl_global_property_object_type, global_property_object> global_property_id_type; typedef object_id< implementation_ids, impl_dynamic_global_property_object_type, dynamic_global_property_object> dynamic_global_property_id_type; @@ -208,6 +210,7 @@ namespace graphene { namespace chain { impl_account_transaction_history_object_type, account_transaction_history_object> account_transaction_history_id_type; typedef object_id< implementation_ids, impl_witness_schedule_object_type, witness_schedule_object > witness_schedule_id_type; + typedef object_id< implementation_ids, impl_chain_property_object_type, chain_property_object> chain_property_id_type; typedef fc::array symbol_type; typedef fc::ripemd160 block_id_type; @@ -285,6 +288,7 @@ FC_REFLECT_ENUM( graphene::chain::impl_object_type, (impl_account_transaction_history_object_type) (impl_witness_schedule_object_type) (impl_blinded_balance_object_type) + (impl_chain_property_object_type) ) FC_REFLECT_ENUM( graphene::chain::meta_info_object_type, (meta_account_object_type)(meta_asset_object_type) ) diff --git a/libraries/wallet/wallet.cpp b/libraries/wallet/wallet.cpp index 5e697667..0eefe792 100644 --- a/libraries/wallet/wallet.cpp +++ b/libraries/wallet/wallet.cpp @@ -462,6 +462,7 @@ public: variant info() const { + auto chain_props = get_chain_properties(); auto global_props = get_global_properties(); auto dynamic_props = get_dynamic_global_properties(); fc::mutable_variant_object result; @@ -471,12 +472,16 @@ public: time_point_sec(time_point::now()), " old"); result["next_maintenance_time"] = fc::get_approximate_relative_time_string(dynamic_props.next_maintenance_time); - result["chain_id"] = global_props.chain_id; + result["chain_id"] = chain_props.chain_id; result["active_witnesses"] = global_props.active_witnesses; result["active_committee_members"] = global_props.active_committee_members; result["entropy"] = dynamic_props.random; return result; } + chain_property_object get_chain_properties() const + { + return _remote_db->get_chain_properties(); + } global_property_object get_global_properties() const { return _remote_db->get_global_properties(); diff --git a/tests/tests/operation_tests2.cpp b/tests/tests/operation_tests2.cpp index d7d9f299..28192b2e 100644 --- a/tests/tests/operation_tests2.cpp +++ b/tests/tests/operation_tests2.cpp @@ -1204,7 +1204,7 @@ BOOST_AUTO_TEST_CASE(transfer_with_memo) { op.memo = memo_data(); op.memo->set_message(alice_private_key, bob_public_key, "Dear Bob,\n\nMoney!\n\nLove, Alice"); trx.operations = {op}; - trx.sign(alice_private_key, db.get_global_properties().chain_id); + trx.sign(alice_private_key, db.get_chain_id()); db.push_transaction(trx); BOOST_CHECK_EQUAL(get_balance(alice_id, asset_id_type()), 500);