Skip to content

Commit

Permalink
Finish testing of plugin objects. API needs testing for correctness #69
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Vandeberg committed Jun 3, 2016
1 parent 277b8c4 commit 6026799
Show file tree
Hide file tree
Showing 7 changed files with 408 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -31,6 +31,7 @@ programs/util/inflation_model
tests/app_test
tests/chain_bench
tests/chain_test
tests/plugin_test
tests/intense_test
tests/performance_test

Expand Down
Expand Up @@ -59,8 +59,8 @@ struct bucket_object : public abstract_object< bucket_object >
static const uint8_t space_id = MARKET_HISTORY_SPACE_ID;
static const uint8_t type_id = 1;

price high()const { return asset( high_steem, STEEM_SYMBOL ) / asset( high_sbd, SBD_SYMBOL ); }
price low()const { return asset( low_steem, STEEM_SYMBOL ) / asset( high_sbd, SBD_SYMBOL ); }
price high()const { return asset( high_sbd, SBD_SYMBOL ) / asset( high_steem, STEEM_SYMBOL ); }
price low()const { return asset( low_sbd, SBD_SYMBOL ) / asset( low_steem, STEEM_SYMBOL ); }

fc::time_point_sec open;
uint32_t seconds = 0;
Expand All @@ -78,12 +78,11 @@ struct bucket_object : public abstract_object< bucket_object >

struct order_history_object : public abstract_object< order_history_object >
{
uint64_t sequence;
fc::time_point_sec time;
fill_order_operation op;
};

struct by_id;
//struct by_id;
struct by_bucket;
typedef multi_index_container<
bucket_object,
Expand All @@ -99,14 +98,12 @@ typedef multi_index_container<
>
> bucket_object_multi_index_type;

struct by_sequence;
struct by_time;
typedef multi_index_container<
order_history_object,
indexed_by<
hashed_unique< tag< by_id >, member< object, object_id_type, &object::id > >,
ordered_unique< tag< by_sequence >, member< order_history_object, uint64_t, &order_history_object::sequence > >,
ordered_unique< tag< by_time >, member< order_history_object, time_point_sec, &order_history_object::time > >
ordered_non_unique< tag< by_time >, member< order_history_object, time_point_sec, &order_history_object::time > >
>
> order_history_multi_index_type;

Expand All @@ -124,6 +121,5 @@ FC_REFLECT_DERIVED( steemit::market_history::bucket_object, (graphene::db::objec
(steem_volume)(sbd_volume) )

FC_REFLECT_DERIVED( steemit::market_history::order_history_object, (graphene::db::object),
(sequence)
(time)
(op) )
21 changes: 5 additions & 16 deletions libraries/plugins/market_history/market_history_plugin.cpp
Expand Up @@ -21,32 +21,23 @@ class market_history_plugin_impl
void update_market_histories( const operation_object& b );

market_history_plugin& _self;
flat_set<uint32_t> _tracked_buckets = { 15, 50, 300, 3600, 86400 };
flat_set<uint32_t> _tracked_buckets = { 15, 60, 300, 3600, 86400 };
int32_t _maximum_history_per_bucket_size = 1000;
};

void market_history_plugin_impl::update_market_histories( const operation_object& o )
{
try
if( o.op.which() == operation::tag< fill_order_operation >::value )
{
fill_order_operation op = o.op.get< fill_order_operation >();

auto& db = _self.database();
const auto& bucket_idx = db.get_index_type< bucket_index >().indices().get< by_bucket >();
const auto& history_idx = db.get_index_type< order_history_index >().indices().get< by_sequence >();

uint64_t history_seq = std::numeric_limits< uint64_t >::max();

auto hist_itr = history_idx.upper_bound( history_seq );

if ( hist_itr != history_idx.end() )
history_seq = hist_itr->sequence + 1;
else
history_seq = 0;
uint64_t history_seq = std::numeric_limits< uint64_t >::min();

db.create< order_history_object >( [&]( order_history_object& ho )
{
ho.sequence = history_seq;
ho.time = db.head_block_time();
ho.op = op;
});
Expand Down Expand Up @@ -79,7 +70,6 @@ void market_history_plugin_impl::update_market_histories( const operation_object
b.close_sbd = op.receives.amount;
b.steem_volume = op.pays.amount;
b.sbd_volume = op.receives.amount;

});
}
else
Expand All @@ -90,13 +80,13 @@ void market_history_plugin_impl::update_market_histories( const operation_object
b.close_steem = op.pays.amount;
b.close_sbd = op.receives.amount;

if( b.high() < price( op.pays, op.receives ) )
if( b.high() < price( op.receives, op.pays ) )
{
b.high_steem = op.pays.amount;
b.high_sbd = op.receives.amount;
}

if( b.low() > price( op.pays, op.receives ) )
if( b.low() > price( op.receives, op.pays ) )
{
b.low_steem = op.pays.amount;
b.low_sbd = op.receives.amount;
Expand All @@ -118,7 +108,6 @@ void market_history_plugin_impl::update_market_histories( const operation_object
}
}
}
catch ( fc::assert_exception ) { return; }
}

} // detail
Expand Down
6 changes: 5 additions & 1 deletion tests/CMakeLists.txt
Expand Up @@ -8,7 +8,11 @@ endif()

file(GLOB UNIT_TESTS "tests/*.cpp")
add_executable( chain_test ${UNIT_TESTS} ${COMMON_SOURCES} )
target_link_libraries( chain_test steemit_chain steemit_app steemit_account_history fc ${PLATFORM_SPECIFIC_LIBS} )
target_link_libraries( chain_test steemit_chain steemit_app steemit_account_history steemit_market_history fc ${PLATFORM_SPECIFIC_LIBS} )

file(GLOB PLUGIN_TESTS "plugin_tests/*.cpp")
add_executable( plugin_test ${PLUGIN_TESTS} ${COMMON_SOURCES} )
target_link_libraries( plugin_test steemit_chain steemit_app steemit_account_history steemit_market_history fc ${PLATFORM_SPECIFIC_LIBS} )

if(MSVC)
set_source_files_properties( tests/serialization_tests.cpp PROPERTIES COMPILE_FLAGS "/bigobj" )
Expand Down
40 changes: 40 additions & 0 deletions tests/plugin_tests/main.cpp
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2015 Cryptonomex, Inc., and contributors.
*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <cstdlib>
#include <iostream>
#include <boost/test/included/unit_test.hpp>

extern uint32_t STEEMIT_TESTING_GENESIS_TIMESTAMP;

boost::unit_test::test_suite* init_unit_test_suite(int argc, char* argv[]) {
std::srand(time(NULL));
std::cout << "Random number generator seeded to " << time(NULL) << std::endl;
const char* genesis_timestamp_str = getenv("STEEMIT_TESTING_GENESIS_TIMESTAMP");
if( genesis_timestamp_str != nullptr )
{
STEEMIT_TESTING_GENESIS_TIMESTAMP = std::stoul( genesis_timestamp_str );
}
std::cout << "STEEMIT_TESTING_GENESIS_TIMESTAMP is " << STEEMIT_TESTING_GENESIS_TIMESTAMP << std::endl;
return nullptr;
}

0 comments on commit 6026799

Please sign in to comment.