Skip to content

Commit

Permalink
adding extra indexing of accounts by address auths
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemaster committed Aug 20, 2015
1 parent 2f0065d commit 4228360
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 27 deletions.
21 changes: 21 additions & 0 deletions libraries/app/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1090,17 +1090,38 @@ namespace graphene { namespace app {
*/
vector<vector<account_id_type>> database_api::get_key_references( vector<public_key_type> keys )const
{
wdump( (keys) );
vector< vector<account_id_type> > final_result;
final_result.reserve(keys.size());

for( auto& key : keys )
{
address a1( pts_address(key, false, 56) );
address a2( pts_address(key, true, 56) );
address a3( pts_address(key, false, 0) );
address a4( pts_address(key, true, 0) );
address a5( key );

const auto& idx = _db.get_index_type<account_index>();
const auto& aidx = dynamic_cast<const primary_index<account_index>&>(idx);
const auto& refs = aidx.get_secondary_index<graphene::chain::account_member_index>();
auto itr = refs.account_to_key_memberships.find(key);
vector<account_id_type> result;

for( auto& a : {a1,a2,a3,a4,a5} )
{
auto itr = refs.account_to_address_memberships.find(a);
if( itr != refs.account_to_address_memberships.end() )
{
result.reserve( itr->second.size() );
for( auto item : itr->second )
{
wdump((a)(item)(item(_db).name));
result.push_back(item);
}
}
}

if( itr != refs.account_to_key_memberships.end() )
{
result.reserve( itr->second.size() );
Expand Down
89 changes: 62 additions & 27 deletions libraries/chain/account_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ set<public_key_type> account_member_index::get_key_members(const account_object&
result.insert(auth.first);
return result;
}
set<address> account_member_index::get_address_members(const account_object& a)const
{
set<address> result;
for( auto auth : a.owner.address_auths )
result.insert(auth.first);
return result;
}

void account_member_index::object_inserted(const object& obj)
{
Expand All @@ -153,6 +160,10 @@ void account_member_index::object_inserted(const object& obj)
auto key_members = get_key_members(a);
for( auto item : key_members )
account_to_key_memberships[item].insert(obj.id);

auto address_members = get_address_members(a);
for( auto item : address_members )
account_to_address_memberships[item].insert(obj.id);
}

void account_member_index::object_removed(const object& obj)
Expand All @@ -163,6 +174,11 @@ void account_member_index::object_removed(const object& obj)
auto key_members = get_key_members(a);
for( auto item : key_members )
account_to_key_memberships[item].erase( obj.id );

auto address_members = get_address_members(a);
for( auto item : address_members )
account_to_address_memberships[item].erase( obj.id );

auto account_members = get_account_members(a);
for( auto item : account_members )
account_to_account_memberships[item].erase( obj.id );
Expand All @@ -175,54 +191,73 @@ void account_member_index::about_to_modify(const object& before)
assert( dynamic_cast<const account_object*>(&before) ); // for debug only
const account_object& a = static_cast<const account_object&>(before);
before_key_members = get_key_members(a);
before_address_members = get_address_members(a);
before_account_members = get_account_members(a);
}

void account_member_index::object_modified(const object& after)
{
assert( dynamic_cast<const account_object*>(&after) ); // for debug only
const account_object& a = static_cast<const account_object&>(after);
set<account_id_type> after_account_members = get_account_members(a);

{
vector<account_id_type> removed; removed.reserve(before_account_members.size());
std::set_difference(before_account_members.begin(), before_account_members.end(),
after_account_members.begin(), after_account_members.end(),
std::inserter(removed, removed.end()));
set<account_id_type> after_account_members = get_account_members(a);
vector<account_id_type> removed; removed.reserve(before_account_members.size());
std::set_difference(before_account_members.begin(), before_account_members.end(),
after_account_members.begin(), after_account_members.end(),
std::inserter(removed, removed.end()));

for( auto itr = removed.begin(); itr != removed.end(); ++itr )
account_to_account_memberships[*itr].erase(after.id);

vector<object_id_type> added; added.reserve(after_account_members.size());
std::set_difference(after_account_members.begin(), after_account_members.end(),
before_account_members.begin(), before_account_members.end(),
std::inserter(added, added.end()));

for( auto itr = added.begin(); itr != added.end(); ++itr )
account_to_account_memberships[*itr].insert(after.id);
}

for( auto itr = removed.begin(); itr != removed.end(); ++itr )
account_to_account_memberships[*itr].erase(after.id);

vector<object_id_type> added; added.reserve(after_account_members.size());
std::set_difference(after_account_members.begin(), after_account_members.end(),
before_account_members.begin(), before_account_members.end(),
std::inserter(added, added.end()));
{
set<public_key_type> after_key_members = get_key_members(a);

for( auto itr = added.begin(); itr != added.end(); ++itr )
account_to_account_memberships[*itr].insert(after.id);
}
vector<public_key_type> removed; removed.reserve(before_key_members.size());
std::set_difference(before_key_members.begin(), before_key_members.end(),
after_key_members.begin(), after_key_members.end(),
std::inserter(removed, removed.end()));

for( auto itr = removed.begin(); itr != removed.end(); ++itr )
account_to_key_memberships[*itr].erase(after.id);

vector<public_key_type> added; added.reserve(after_key_members.size());
std::set_difference(after_key_members.begin(), after_key_members.end(),
before_key_members.begin(), before_key_members.end(),
std::inserter(added, added.end()));

for( auto itr = added.begin(); itr != added.end(); ++itr )
account_to_key_memberships[*itr].insert(after.id);
}

{
set<public_key_type> after_key_members = get_key_members(a);
set<address> after_address_members = get_address_members(a);

vector<public_key_type> removed; removed.reserve(before_key_members.size());
std::set_difference(before_key_members.begin(), before_key_members.end(),
after_key_members.begin(), after_key_members.end(),
std::inserter(removed, removed.end()));
vector<address> removed; removed.reserve(before_address_members.size());
std::set_difference(before_address_members.begin(), before_address_members.end(),
after_address_members.begin(), after_address_members.end(),
std::inserter(removed, removed.end()));

for( auto itr = removed.begin(); itr != removed.end(); ++itr )
account_to_key_memberships[*itr].erase(after.id);
for( auto itr = removed.begin(); itr != removed.end(); ++itr )
account_to_address_memberships[*itr].erase(after.id);

vector<public_key_type> added; added.reserve(after_key_members.size());
std::set_difference(after_key_members.begin(), after_key_members.end(),
before_key_members.begin(), before_key_members.end(),
std::inserter(added, added.end()));
vector<address> added; added.reserve(after_address_members.size());
std::set_difference(after_address_members.begin(), after_address_members.end(),
before_address_members.begin(), before_address_members.end(),
std::inserter(added, added.end()));

for( auto itr = added.begin(); itr != added.end(); ++itr )
account_to_key_memberships[*itr].insert(after.id);
for( auto itr = added.begin(); itr != added.end(); ++itr )
account_to_address_memberships[*itr].insert(after.id);
}

}
Expand Down
4 changes: 4 additions & 0 deletions libraries/chain/include/graphene/chain/account_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,18 @@ namespace graphene { namespace chain {
/** given an account or key, map it to the set of accounts that reference it in an active or owner authority */
map< account_id_type, set<account_id_type> > account_to_account_memberships;
map< public_key_type, set<account_id_type> > account_to_key_memberships;
/** some accounts use address authorities in the genesis block */
map< address, set<account_id_type> > account_to_address_memberships;


protected:
set<account_id_type> get_account_members( const account_object& a )const;
set<public_key_type> get_key_members( const account_object& a )const;
set<address> get_address_members( const account_object& a )const;

set<account_id_type> before_account_members;
set<public_key_type> before_key_members;
set<address> before_address_members;
};

/**
Expand Down

0 comments on commit 4228360

Please sign in to comment.