Skip to content

Commit 27880b1

Browse files
authored
Merge pull request #1019 from bitshares/jmj_issue_960
Add index on short_backing_asset (Issue 960)
2 parents eb13e71 + ccd8ff2 commit 27880b1

File tree

4 files changed

+225
-113
lines changed

4 files changed

+225
-113
lines changed

libraries/chain/asset_evaluator.cpp

+39-38
Original file line numberDiff line numberDiff line change
@@ -378,54 +378,55 @@ void check_children_of_bitasset(database& d, const asset_update_bitasset_operati
378378
return;
379379

380380
// loop through all assets that have this asset as a backing asset
381-
const auto& idx = d.get_index_type<asset_index>().indices().get<by_type>();
382-
383-
for( auto itr = idx.lower_bound(true); itr != idx.end(); ++itr )
384-
{
385-
const auto& child = *itr;
386-
if ( child.bitasset_data(d).options.short_backing_asset == op.asset_to_update )
387-
{
388-
if ( after_hf_922_931 )
381+
const auto& idx = d.get_index_type<graphene::chain::asset_bitasset_data_index>()
382+
.indices()
383+
.get<by_short_backing_asset>();
384+
auto backed_range = idx.equal_range(op.asset_to_update);
385+
std::for_each( backed_range.first, backed_range.second,
386+
[after_hf_922_931, &new_backing_asset, &d, &op](const asset_bitasset_data_object& bitasset_data)
389387
{
390-
FC_ASSERT( child.get_id() != op.new_options.short_backing_asset,
391-
"A BitAsset would be invalidated by changing this backing asset ('A' backed by 'B' backed by 'A')." );
388+
const auto& child = bitasset_data.asset_id(d);
389+
if ( after_hf_922_931 )
390+
{
391+
FC_ASSERT( child.get_id() != op.new_options.short_backing_asset,
392+
"A BitAsset would be invalidated by changing this backing asset ('A' backed by 'B' backed by 'A')." );
392393

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

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

399-
}
400-
else
401-
{
402-
if( child.get_id() == op.new_options.short_backing_asset )
403-
{
404-
wlog( "Before hf-922-931, modified an asset to be backed by another, but would cause a continuous "
405-
"loop. A cannot be backed by B which is backed by A." );
406-
return;
407-
}
408-
409-
if( child.issuer == GRAPHENE_COMMITTEE_ACCOUNT )
410-
{
411-
wlog( "before hf-922-931, modified an asset to be backed by a non-CORE, but this asset "
412-
"is a backing asset for a committee-issued asset. This occurred at block ${b}",
413-
("b", d.head_block_num()));
414-
return;
415400
}
416401
else
417402
{
418-
if ( new_backing_asset.is_market_issued() ) { // a.k.a. !UIA
419-
wlog( "before hf-922-931, modified an asset to be backed by an MPA, but this asset "
420-
"is a backing asset for another MPA, which would cause MPA backed by MPA backed by MPA. "
421-
"This occurred at block ${b}",
403+
if( child.get_id() == op.new_options.short_backing_asset )
404+
{
405+
wlog( "Before hf-922-931, modified an asset to be backed by another, but would cause a continuous "
406+
"loop. A cannot be backed by B which is backed by A." );
407+
return;
408+
}
409+
410+
if( child.issuer == GRAPHENE_COMMITTEE_ACCOUNT )
411+
{
412+
wlog( "before hf-922-931, modified an asset to be backed by a non-CORE, but this asset "
413+
"is a backing asset for a committee-issued asset. This occurred at block ${b}",
422414
("b", d.head_block_num()));
423415
return;
424416
}
425-
} // if child.issuer
426-
} // if hf 922/931
427-
} // if this child is backed by the asset being adjusted
428-
} // for each asset
417+
else
418+
{
419+
if ( new_backing_asset.is_market_issued() ) // a.k.a. !UIA
420+
{
421+
wlog( "before hf-922-931, modified an asset to be backed by an MPA, but this asset "
422+
"is a backing asset for another MPA, which would cause MPA backed by MPA backed by MPA. "
423+
"This occurred at block ${b}",
424+
("b", d.head_block_num()));
425+
return;
426+
}
427+
} // if child.issuer
428+
} // if hf 922/931
429+
} ); // end of lambda and std::for_each()
429430
} // check_children_of_bitasset
430431

431432
void_result asset_update_bitasset_evaluator::do_evaluate(const asset_update_bitasset_operation& op)

libraries/chain/include/graphene/chain/asset_object.hpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,22 @@ namespace graphene { namespace chain {
232232
void update_median_feeds(time_point_sec current_time);
233233
};
234234

235+
// key extractor for short backing asset
236+
struct bitasset_short_backing_asset_extractor
237+
{
238+
typedef asset_id_type result_type;
239+
result_type operator() (const asset_bitasset_data_object& obj) const
240+
{
241+
return obj.options.short_backing_asset;
242+
}
243+
};
244+
245+
struct by_short_backing_asset;
235246
typedef multi_index_container<
236247
asset_bitasset_data_object,
237248
indexed_by<
238-
ordered_unique< tag<by_id>, member< object, object_id_type, &object::id > >
249+
ordered_unique< tag<by_id>, member< object, object_id_type, &object::id > >,
250+
ordered_non_unique< tag<by_short_backing_asset>, bitasset_short_backing_asset_extractor >
239251
>
240252
> asset_bitasset_data_object_multi_index_type;
241253
typedef generic_index<asset_bitasset_data_object, asset_bitasset_data_object_multi_index_type> asset_bitasset_data_index;

tests/common/database_fixture.hpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include <graphene/app/application.hpp>
2727
#include <graphene/chain/database.hpp>
28+
#include <graphene/chain/protocol/types.hpp>
2829
#include <fc/io/json.hpp>
2930
#include <fc/smart_ref_impl.hpp>
3031

@@ -150,18 +151,18 @@ extern uint32_t GRAPHENE_TESTING_GENESIS_TIMESTAMP;
150151

151152
#define PREP_ACTOR(name) \
152153
fc::ecc::private_key name ## _private_key = generate_private_key(BOOST_PP_STRINGIZE(name)); \
153-
public_key_type name ## _public_key = name ## _private_key.get_public_key(); \
154+
graphene::chain::public_key_type name ## _public_key = name ## _private_key.get_public_key(); \
154155
BOOST_CHECK( name ## _public_key != public_key_type() );
155156

156157
#define ACTOR(name) \
157158
PREP_ACTOR(name) \
158159
const auto& name = create_account(BOOST_PP_STRINGIZE(name), name ## _public_key); \
159-
account_id_type name ## _id = name.id; (void)name ## _id;
160+
graphene::chain::account_id_type name ## _id = name.id; (void)name ## _id;
160161

161162
#define GET_ACTOR(name) \
162163
fc::ecc::private_key name ## _private_key = generate_private_key(BOOST_PP_STRINGIZE(name)); \
163164
const account_object& name = get_account(BOOST_PP_STRINGIZE(name)); \
164-
account_id_type name ## _id = name.id; \
165+
graphene::chain::account_id_type name ## _id = name.id; \
165166
(void)name ##_id
166167

167168
#define ACTORS_IMPL(r, data, elem) ACTOR(elem)

0 commit comments

Comments
 (0)