wallet.cpp: Ask remote DB for accounts on startup
This commit is contained in:
parent
65165c416f
commit
0b6f7fe430
1 changed files with 43 additions and 9 deletions
|
|
@ -649,15 +649,49 @@ public:
|
|||
("wallet.chain_id", _wallet.chain_id)
|
||||
("chain_id", _chain_id) );
|
||||
|
||||
vector< account_id_type > my_account_ids;
|
||||
my_account_ids.reserve( _wallet.my_accounts.size() );
|
||||
for( const account_object& acct : _wallet.my_accounts )
|
||||
my_account_ids.push_back( acct.id );
|
||||
// TODO: Batch requests using _remote_db->get_accounts()
|
||||
// to reduce number of round-trips. Remember get_accounts() has
|
||||
// a limit of 100 results per call!
|
||||
for( const account_id_type& acct_id : my_account_ids )
|
||||
_wallet.update_account( get_account( acct_id ) );
|
||||
size_t account_pagination = 100;
|
||||
vector< account_id_type > account_ids_to_send;
|
||||
size_t n = _wallet.my_accounts.size();
|
||||
account_ids_to_send.reserve( std::min( account_pagination, n ) );
|
||||
auto it = _wallet.my_accounts.begin();
|
||||
|
||||
for( size_t start=0; start<n; start+=account_pagination )
|
||||
{
|
||||
size_t end = std::min( start+account_pagination, n );
|
||||
assert( end > start );
|
||||
account_ids_to_send.clear();
|
||||
std::vector< account_object > old_accounts;
|
||||
for( size_t i=start; i<end; i++ )
|
||||
{
|
||||
assert( it != _wallet.my_accounts.end() );
|
||||
old_accounts.push_back( *it );
|
||||
account_ids_to_send.push_back( old_accounts.back().id );
|
||||
++it;
|
||||
}
|
||||
std::vector< optional< account_object > > accounts = _remote_db->get_accounts(account_ids_to_send);
|
||||
// server response should be same length as request
|
||||
FC_ASSERT( accounts.size() == account_ids_to_send.size() );
|
||||
size_t i = 0;
|
||||
for( const optional< account_object >& acct : accounts )
|
||||
{
|
||||
account_object& old_acct = old_accounts[i];
|
||||
if( !acct.valid() )
|
||||
{
|
||||
elog( "Could not find account ${id} : \"${name}\" does not exist on the chain!", ("id", old_acct.id)("name", old_acct.name) );
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
// this check makes sure the server didn't send results
|
||||
// in a different order, or accounts we didn't request
|
||||
FC_ASSERT( acct->id == old_acct.id );
|
||||
if( fc::json::to_string(*acct) != fc::json::to_string(old_acct) )
|
||||
{
|
||||
wlog( "Account ${id} : \"${name}\" updated on chain", ("id", acct->id)("name", acct->name) );
|
||||
}
|
||||
_wallet.update_account( *acct );
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue