Skip to content

Commit

Permalink
Merge pull request #1019 from bitshares/jmj_issue_960
Browse files Browse the repository at this point in the history
Add index on short_backing_asset (Issue 960)
  • Loading branch information
jmjatlanta authored Jun 24, 2018
2 parents eb13e71 + ccd8ff2 commit 27880b1
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 113 deletions.
77 changes: 39 additions & 38 deletions libraries/chain/asset_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,54 +378,55 @@ void check_children_of_bitasset(database& d, const asset_update_bitasset_operati
return;

// loop through all assets that have this asset as a backing asset
const auto& idx = d.get_index_type<asset_index>().indices().get<by_type>();

for( auto itr = idx.lower_bound(true); itr != idx.end(); ++itr )
{
const auto& child = *itr;
if ( child.bitasset_data(d).options.short_backing_asset == op.asset_to_update )
{
if ( after_hf_922_931 )
const auto& idx = d.get_index_type<graphene::chain::asset_bitasset_data_index>()
.indices()
.get<by_short_backing_asset>();
auto backed_range = idx.equal_range(op.asset_to_update);
std::for_each( backed_range.first, backed_range.second,
[after_hf_922_931, &new_backing_asset, &d, &op](const asset_bitasset_data_object& bitasset_data)
{
FC_ASSERT( child.get_id() != op.new_options.short_backing_asset,
"A BitAsset would be invalidated by changing this backing asset ('A' backed by 'B' backed by 'A')." );
const auto& child = bitasset_data.asset_id(d);
if ( after_hf_922_931 )
{
FC_ASSERT( child.get_id() != op.new_options.short_backing_asset,
"A BitAsset would be invalidated by changing this backing asset ('A' backed by 'B' backed by 'A')." );

FC_ASSERT( child.issuer != GRAPHENE_COMMITTEE_ACCOUNT,
"A blockchain-controlled market asset would be invalidated by changing this backing asset." );
FC_ASSERT( child.issuer != GRAPHENE_COMMITTEE_ACCOUNT,
"A blockchain-controlled market asset would be invalidated by changing this backing asset." );

FC_ASSERT( !new_backing_asset.is_market_issued(),
"A non-blockchain controlled BitAsset would be invalidated by changing this backing asset.");
FC_ASSERT( !new_backing_asset.is_market_issued(),
"A non-blockchain controlled BitAsset would be invalidated by changing this backing asset.");

}
else
{
if( child.get_id() == op.new_options.short_backing_asset )
{
wlog( "Before hf-922-931, modified an asset to be backed by another, but would cause a continuous "
"loop. A cannot be backed by B which is backed by A." );
return;
}

if( child.issuer == GRAPHENE_COMMITTEE_ACCOUNT )
{
wlog( "before hf-922-931, modified an asset to be backed by a non-CORE, but this asset "
"is a backing asset for a committee-issued asset. This occurred at block ${b}",
("b", d.head_block_num()));
return;
}
else
{
if ( new_backing_asset.is_market_issued() ) { // a.k.a. !UIA
wlog( "before hf-922-931, modified an asset to be backed by an MPA, but this asset "
"is a backing asset for another MPA, which would cause MPA backed by MPA backed by MPA. "
"This occurred at block ${b}",
if( child.get_id() == op.new_options.short_backing_asset )
{
wlog( "Before hf-922-931, modified an asset to be backed by another, but would cause a continuous "
"loop. A cannot be backed by B which is backed by A." );
return;
}

if( child.issuer == GRAPHENE_COMMITTEE_ACCOUNT )
{
wlog( "before hf-922-931, modified an asset to be backed by a non-CORE, but this asset "
"is a backing asset for a committee-issued asset. This occurred at block ${b}",
("b", d.head_block_num()));
return;
}
} // if child.issuer
} // if hf 922/931
} // if this child is backed by the asset being adjusted
} // for each asset
else
{
if ( new_backing_asset.is_market_issued() ) // a.k.a. !UIA
{
wlog( "before hf-922-931, modified an asset to be backed by an MPA, but this asset "
"is a backing asset for another MPA, which would cause MPA backed by MPA backed by MPA. "
"This occurred at block ${b}",
("b", d.head_block_num()));
return;
}
} // if child.issuer
} // if hf 922/931
} ); // end of lambda and std::for_each()
} // check_children_of_bitasset

void_result asset_update_bitasset_evaluator::do_evaluate(const asset_update_bitasset_operation& op)
Expand Down
14 changes: 13 additions & 1 deletion libraries/chain/include/graphene/chain/asset_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,22 @@ namespace graphene { namespace chain {
void update_median_feeds(time_point_sec current_time);
};

// key extractor for short backing asset
struct bitasset_short_backing_asset_extractor
{
typedef asset_id_type result_type;
result_type operator() (const asset_bitasset_data_object& obj) const
{
return obj.options.short_backing_asset;
}
};

struct by_short_backing_asset;
typedef multi_index_container<
asset_bitasset_data_object,
indexed_by<
ordered_unique< tag<by_id>, member< object, object_id_type, &object::id > >
ordered_unique< tag<by_id>, member< object, object_id_type, &object::id > >,
ordered_non_unique< tag<by_short_backing_asset>, bitasset_short_backing_asset_extractor >
>
> asset_bitasset_data_object_multi_index_type;
typedef generic_index<asset_bitasset_data_object, asset_bitasset_data_object_multi_index_type> asset_bitasset_data_index;
Expand Down
7 changes: 4 additions & 3 deletions tests/common/database_fixture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include <graphene/app/application.hpp>
#include <graphene/chain/database.hpp>
#include <graphene/chain/protocol/types.hpp>
#include <fc/io/json.hpp>
#include <fc/smart_ref_impl.hpp>

Expand Down Expand Up @@ -150,18 +151,18 @@ extern uint32_t GRAPHENE_TESTING_GENESIS_TIMESTAMP;

#define PREP_ACTOR(name) \
fc::ecc::private_key name ## _private_key = generate_private_key(BOOST_PP_STRINGIZE(name)); \
public_key_type name ## _public_key = name ## _private_key.get_public_key(); \
graphene::chain::public_key_type name ## _public_key = name ## _private_key.get_public_key(); \
BOOST_CHECK( name ## _public_key != public_key_type() );

#define ACTOR(name) \
PREP_ACTOR(name) \
const auto& name = create_account(BOOST_PP_STRINGIZE(name), name ## _public_key); \
account_id_type name ## _id = name.id; (void)name ## _id;
graphene::chain::account_id_type name ## _id = name.id; (void)name ## _id;

#define GET_ACTOR(name) \
fc::ecc::private_key name ## _private_key = generate_private_key(BOOST_PP_STRINGIZE(name)); \
const account_object& name = get_account(BOOST_PP_STRINGIZE(name)); \
account_id_type name ## _id = name.id; \
graphene::chain::account_id_type name ## _id = name.id; \
(void)name ##_id

#define ACTORS_IMPL(r, data, elem) ACTOR(elem)
Expand Down
Loading

0 comments on commit 27880b1

Please sign in to comment.