Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the query_txid_plugin as a option #1954

Closed
wants to merge 5 commits into from
Closed
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
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ set( INSTALLER_APP_ID "68ad7005-8eee-49c9-95ce-9eed97e5b347" )
set( CMAKE_CXX_STANDARD 14 )
set( CMAKE_CXX_STANDARD_REQUIRED ON )
set( CMAKE_CXX_EXTENSIONS OFF )

option(LOAD_QUERY_TXID_PLUGIN OFF)
if(${LOAD_QUERY_TXID_PLUGIN} STREQUAL "ON")
add_definitions(-DQUERY_TXID_PLUGIN_ABLE)
set( QUERY_TXID graphene_query_txid)
endif()
MESSAGE("the query_txid plugin is ${LOAD_QUERY_TXID_PLUGIN}")
# http://stackoverflow.com/a/18369825
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.8)
Expand Down
2 changes: 1 addition & 1 deletion libraries/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ add_library( graphene_app
)

# need to link graphene_debug_witness because plugins aren't sufficiently isolated #246
target_link_libraries( graphene_app graphene_market_history graphene_account_history graphene_grouped_orders graphene_chain fc graphene_db graphene_net graphene_utilities graphene_debug_witness )
target_link_libraries( graphene_app graphene_market_history graphene_account_history graphene_grouped_orders graphene_chain fc graphene_db graphene_net graphene_utilities graphene_debug_witness ${QUERY_TXID})
target_include_directories( graphene_app
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include"
"${CMAKE_CURRENT_SOURCE_DIR}/../egenesis/include" )
Expand Down
44 changes: 42 additions & 2 deletions libraries/app/database_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@

#include <cfenv>
#include <iostream>

#ifdef QUERY_TXID_PLUGIN_ABLE
#include <graphene/query_txid/query_txid_plugin.hpp>
#endif
#define GET_REQUIRED_FEES_MAX_RECURSION 4

typedef std::map< std::pair<graphene::chain::asset_id_type, graphene::chain::asset_id_type>, std::vector<fc::variant> > market_queue_type;
Expand Down Expand Up @@ -73,7 +75,7 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>
map<uint32_t, optional<block_header>> get_block_header_batch(const vector<uint32_t> block_nums)const;
optional<signed_block> get_block(uint32_t block_num)const;
processed_transaction get_transaction( uint32_t block_num, uint32_t trx_in_block )const;

optional<processed_transaction> get_transaction_by_txid(transaction_id_type txid)const;
// Globals
chain_property_object get_chain_properties()const;
global_property_object get_global_properties()const;
Expand Down Expand Up @@ -634,6 +636,10 @@ processed_transaction database_api::get_transaction( uint32_t block_num, uint32_
{
return my->get_transaction( block_num, trx_in_block );
}
optional<processed_transaction> database_api::get_transaction_by_txid(transaction_id_type txid)const
{
return my->get_transaction_by_txid(txid);
}

optional<signed_transaction> database_api::get_recent_transaction_by_id( const transaction_id_type& id )const
{
Expand All @@ -652,6 +658,40 @@ processed_transaction database_api_impl::get_transaction(uint32_t block_num, uin
return opt_block->transactions[trx_num];
}

optional<processed_transaction> database_api_impl::get_transaction_by_txid(transaction_id_type txid)const
{
#ifdef QUERY_TXID_PLUGIN_ABLE
auto &txid_index = _db.get_index_type<trx_entry_index>().indices().get<by_txid>();
auto itor = txid_index.find(txid);
if (itor == txid_index.end()) {
std::string txid_str(txid);
auto result = query_txid::query_txid_plugin::query_trx_by_id(txid_str);
if (result) {
const auto &trx_entry = *result;
auto opt_block = _db.fetch_block_by_number(trx_entry.block_num);
FC_ASSERT(opt_block);
FC_ASSERT(opt_block->transactions.size() > trx_entry.trx_in_block);
optional<processed_transaction> res = opt_block->transactions[trx_entry.trx_in_block];
return res;
}
return {};
} else {
const auto &dpo = _db.get_dynamic_global_properties();
if (itor->block_num <= dpo.last_irreversible_block_num) {
const auto &trx_entry = *itor;
auto opt_block = _db.fetch_block_by_number(trx_entry.block_num);
FC_ASSERT(opt_block);
FC_ASSERT(opt_block->transactions.size() > trx_entry.trx_in_block);
optional<processed_transaction> res = opt_block->transactions[trx_entry.trx_in_block];
return res;
} else {
return {};
}
}
#endif
return {};
}

//////////////////////////////////////////////////////////////////////
// //
// Globals //
Expand Down
7 changes: 4 additions & 3 deletions libraries/app/include/graphene/app/database_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#include <graphene/chain/worker_object.hpp>
#include <graphene/chain/witness_object.hpp>
#include <graphene/chain/htlc_object.hpp>

#include <graphene/chain/transaction_entry_object.hpp>
#include <graphene/market_history/market_history_plugin.hpp>

#include <fc/api.hpp>
Expand Down Expand Up @@ -232,7 +232,7 @@ class database_api
* @brief used to fetch an individual transaction.
*/
processed_transaction get_transaction( uint32_t block_num, uint32_t trx_in_block )const;

optional<processed_transaction> get_transaction_by_txid(transaction_id_type txid)const;
/**
* If the transaction has not expired, this method will return the transaction for the given ID or
* it will return NULL if it is not known. Just because it is not known does not mean it wasn't
Expand Down Expand Up @@ -853,7 +853,8 @@ FC_API(graphene::app::database_api,
(get_block)
(get_transaction)
(get_recent_transaction_by_id)

(get_transaction_by_txid)

// Globals
(get_chain_properties)
(get_global_properties)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once
#include <graphene/db/object.hpp>
//#include <graphene/chain/protocol/types.hpp>
//#include <graphene/db/object.hpp>
#include <graphene/protocol/types.hpp>
#include <boost/multi_index/composite_key.hpp>

namespace graphene { namespace chain {

class trx_entry_object : public abstract_object<trx_entry_object>
{
public:
static const uint8_t space_id = implementation_ids;
static const uint8_t type_id = impl_trx_entry_history_object_type;

trx_entry_object(){}

transaction_id_type txid;
uint32_t block_num;
uint32_t trx_in_block;
};

// struct by_id;
struct by_txid;
struct by_blocknum;

typedef multi_index_container<
trx_entry_object,
indexed_by<
ordered_unique< tag<by_id>, member< object, object_id_type, &object::id > >,
ordered_unique< tag<by_txid>, member< trx_entry_object, transaction_id_type, &trx_entry_object::txid > >,
ordered_non_unique< tag<by_blocknum>, member< trx_entry_object, uint32_t, &trx_entry_object::block_num > >
>

> trx_entry_multi_index_type;

typedef generic_index<trx_entry_object, trx_entry_multi_index_type> trx_entry_index;

} } // graphene::chain

FC_REFLECT_DERIVED( graphene::chain::trx_entry_object, (graphene::chain::object),
(txid)(block_num)(trx_in_block))
3 changes: 2 additions & 1 deletion libraries/chain/include/graphene/chain/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ GRAPHENE_DEFINE_IDS(chain, implementation_ids, impl_,
(special_authority)
(buyback)
(fba_accumulator)
(collateral_bid))
(collateral_bid)
(trx_entry_history))
1 change: 1 addition & 0 deletions libraries/plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ add_subdirectory( delayed_node )
add_subdirectory( debug_witness )
add_subdirectory( snapshot )
add_subdirectory( es_objects )
add_subdirectory( query_txid )
24 changes: 24 additions & 0 deletions libraries/plugins/query_txid/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
file(GLOB HEADERS "include/graphene/query_txid/*.hpp")

add_library( graphene_query_txid
query_txid_plugin.cpp
)
find_path(LevelDB_INCLUDE_PATH NAMES leveldb/db.h leveldb/write_batch.h)
find_library(LevelDB_LIBRARY NAMES libleveldb.a)
find_library(Snappy_LIBRARY NAMES libsnappy.a)

if(LevelDB_INCLUDE_PATH AND LevelDB_LIBRARY AND Snappy_LIBRARY)
target_link_libraries( graphene_query_txid graphene_chain graphene_app ${LevelDB_LIBRARY} ${Snappy_LIBRARY})
target_include_directories( graphene_query_txid
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" ${LevelDB_INCLUDE_PATH})
install( TARGETS
graphene_query_txid

RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
)
else(LevelDB_INCLUDE_PATH AND LevelDB_LIBRARY AND Snappy_LIBRARY)
message(FATAL_ERROR "You need leveldb and snappy")
endif()

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once
#include <graphene/app/plugin.hpp>
#include <graphene/chain/transaction_entry_object.hpp>
#include <graphene/chain/database.hpp>


namespace graphene
{
namespace query_txid
{

using namespace chain;
namespace detail
{
class query_txid_plugin_impl;
}
class query_txid_plugin : public graphene::app::plugin
{
public:
query_txid_plugin();
virtual ~query_txid_plugin();

std::string plugin_name() const override;

virtual void plugin_set_program_options(
boost::program_options::options_description &cli,
boost::program_options::options_description &cfg) override;

virtual void plugin_initialize(const boost::program_options::variables_map &options) override;
virtual void plugin_startup() override;

static optional<trx_entry_object> query_trx_by_id(std::string txid);

friend class detail::query_txid_plugin_impl;

std::unique_ptr<detail::query_txid_plugin_impl> my;
};
} // namespace query_txid
} // namespace graphene
Loading