Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support account names or ids in all api calls #989

Merged
merged 16 commits into from
Jun 21, 2018

Conversation

oxarbitrage
Copy link
Member

The following is an initial work in #969 giving support of account by name to a few selected calls to discuss if it is the right approach or something else will be better.

In this approach, non exposed function get_account_from_string is created, the scope is only database_api.cpp file. Code for it is present(and duplicated) in several api calls like verify_account_authority or get_full_accounts. This duplicated code is what it is added to the new function.

Next, verify_account_authority was refactored to use the new created call and also get_full_accounts. Please note this 2 calls were already supporting account as a string.

Next, api call get_account_balances is changed to support account by name. In this change, the wallet is impacted so had to make changes there too.

There are actually a lot of calls that need to be changed just inside database_api.cpp, more in api.cpp and some require wallet.cpp changes so decided to submit pull now before going further as the approach can be wrong.

I will post a list of all the impacted apis in the issue soon.

Note: There is a similar issue with assets, it should be better to be able to use all api calls by asset_id or name, probably for another issue.
Note 2: A local cache will be a good idea for get_account_from_string as it will be called constantly, other suggestions to improve performance of it will be great.

@@ -198,6 +198,24 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>
return tmp;
}

const account_object* get_account_from_string( const std::string& name_or_id )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not believe this method changes anything internally. So you should be able to mark this const, and keep the const qualifier on the methods that call it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey John, thank you for that. i had the intention of fix that before submitting but it seems i forgot. fixed now at 30eda4a thanks again!

Copy link
Contributor

@jmjatlanta jmjatlanta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thanks Alfredo!

{
FC_ASSERT( limit <= 101 );
vector<withdraw_permission_object> result;

const auto& withdraw_idx = _db.get_index_type<withdraw_permission_index>().indices().get<by_authorized>();
auto withdraw_index_end = withdraw_idx.end();
const account_id_type account = get_account_from_string(account_id_or_name)->id; // ask this, maybe not safe
Copy link
Contributor

@jmjatlanta jmjatlanta Jun 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the account is not found, get_account_from_string throws. So this is safe, at least from the common error.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you, removed the comments from code.

@@ -198,6 +198,24 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>
return tmp;
}

const account_object* get_account_from_string( const std::string& name_or_id ) const
{
// TODO cache the result to avoid repeatly fetching from db
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible solution: Put the result in a static, always check to see if the names/ids match.
Caution: It is my understanding that references to db objects can turn stale. That could mean that you'll be pointing to an address that could now be non-null and not an account_object.
Question: Would keeping a copy be a better choice? While the data within it could grow stale, at least you would control its lifetime.

@oxarbitrage
Copy link
Member Author

@abitmore , @pmconrad I will like you guys to take a look to this when you get the time.

I know is a lot of changes but here is a summary about that this pull do:

  • The list of affected calls is detailed in issue op: Update all account-related API's to support account name as parameter #969
  • Function get_account_from_string(local scope) was added to database_api and called in all database_api related functions to get the account ID from a string when needed. This is used in al lthe impacted calls of database_api as the account params changed to string.
  • account_id_to_string was added to wallet to cast account_id_type to string. When the wallet need to call a database_api function with an account, it converts to string if needed and then makes the call with the string.
  • api now can connect to database_api and call functions(thanks @jmjatlanta), i exposed get_account_id_from_string so api can call this and get an ID from a string parameter and continue doing the usual stuff with the ID.

Thank you guys.

@oxarbitrage
Copy link
Member Author

I forgot to mention i had to made changes to history_api_tests as they were all using account_id_type.

Apart from that i don't have test cases for everything changed but all tests are passing with the current state and i had tried all the calls impacted by curl and wallet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants