Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #8899 from EOSIO/feature/account_query_db
Browse files Browse the repository at this point in the history
Account Query DB : Proposal to maintain get_(key|controlled)_accounts [2.0]
  • Loading branch information
b1bart authored May 18, 2020
2 parents adc65b7 + e208262 commit b40015f
Show file tree
Hide file tree
Showing 10 changed files with 775 additions and 10 deletions.
60 changes: 60 additions & 0 deletions plugins/chain_api_plugin/chain.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -680,3 +680,63 @@ paths:
more:
type: integer
description: "In case there's more activated protocol features than the input parameter `limit` requested, returns the ordinal of the next activated protocol feature which was not returned, otherwise zero."
/get_accounts_by_authorizers:
post:
description: Given a set of account names and public keys, find all account permission authorities that are, in part or whole, satisfiable
operationId: get_accounts_by_authorizers
requestBody:
content:
application/json:
schema:
type: object
properties:
accounts:
type: array
description: List of authorizing accounts and/or actor/permissions
items:
anyOf:
- $ref: "https://eosio.github.io/schemata/v2.0/oas/Name.yaml"
- $ref: "https://eosio.github.io/schemata/v2.0/oas/Authority.yaml"
keys:
type: array
description: List of authorizing keys
items:
$ref: "https://eosio.github.io/schemata/v2.0/oas/PublicKey.yaml"
responses:
"200":
description: OK
content:
application/json:
schema:
type: object
description: Result containing a list of accounts which are authorized, in whole or part, by the provided accounts and keys
required:
- accounts
properties:
accounts:
type: array
description: An array of each account,permission,authorizing-data triplet in the response
items:
type: object
description: the information for a single account,permission,authorizing-data triplet
required:
- account_name
- permission_name
- authorizer
- weight
- threshold
properties:
account_name:
$ref: "https://eosio.github.io/schemata/v2.0/oas/Name.yaml"
permission_name:
$ref: "https://eosio.github.io/schemata/v2.0/oas/Name.yaml"
authorizer:
oneOf:
- $ref: "https://eosio.github.io/schemata/v2.0/oas/PublicKey.yaml"
- $ref: "https://eosio.github.io/schemata/v2.0/oas/Authority.yaml"
weight:
type: "integer"
description: the weight that this authorizer adds to satisfy the permission
threshold:
type: "integer"
description: the sum of weights that must be met or exceeded to satisfy the permission
43 changes: 41 additions & 2 deletions plugins/chain_api_plugin/chain_api_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ struct async_result_visitor : public fc::visitor<fc::variant> {
}
};

namespace {
template<typename T>
T parse_params(const std::string& body) {
if (body.empty()) {
EOS_THROW(chain::invalid_http_request, "A Request body is required");
}

try {
try {
return fc::json::from_string(body).as<T>();
} catch (const chain::chain_exception& e) { // EOS_RETHROW_EXCEPTIONS does not re-type these so, re-code it
throw fc::exception(e);
}
} EOS_RETHROW_EXCEPTIONS(chain::invalid_http_request, "Unable to parse valid input from POST body");
}
}

#define CALL(api_name, api_handle, api_namespace, call_name, http_response_code) \
{std::string("/v1/" #api_name "/" #call_name), \
[api_handle](string, string body, url_response_callback cb) mutable { \
Expand All @@ -44,6 +61,19 @@ struct async_result_visitor : public fc::visitor<fc::variant> {
} \
}}

#define CALL_WITH_400(api_name, api_handle, api_namespace, call_name, http_response_code) \
{std::string("/v1/" #api_name "/" #call_name), \
[api_handle](string, string body, url_response_callback cb) mutable { \
api_handle.validate(); \
try { \
auto params = parse_params<api_namespace::call_name ## _params>(body);\
fc::variant result( api_handle.call_name( std::move(params) ) ); \
cb(http_response_code, std::move(result)); \
} catch (...) { \
http_plugin::handle_exception(#api_name, #call_name, body, cb); \
} \
}}

#define CALL_ASYNC(api_name, api_handle, api_namespace, call_name, call_result, http_response_code) \
{std::string("/v1/" #api_name "/" #call_name), \
[api_handle](string, string body, url_response_callback cb) mutable { \
Expand All @@ -69,11 +99,14 @@ struct async_result_visitor : public fc::visitor<fc::variant> {
#define CHAIN_RO_CALL_ASYNC(call_name, call_result, http_response_code) CALL_ASYNC(chain, ro_api, chain_apis::read_only, call_name, call_result, http_response_code)
#define CHAIN_RW_CALL_ASYNC(call_name, call_result, http_response_code) CALL_ASYNC(chain, rw_api, chain_apis::read_write, call_name, call_result, http_response_code)

#define CHAIN_RO_CALL_WITH_400(call_name, http_response_code) CALL_WITH_400(chain, ro_api, chain_apis::read_only, call_name, http_response_code)

void chain_api_plugin::plugin_startup() {
ilog( "starting chain_api_plugin" );
my.reset(new chain_api_plugin_impl(app().get_plugin<chain_plugin>().chain()));
auto ro_api = app().get_plugin<chain_plugin>().get_read_only_api();
auto rw_api = app().get_plugin<chain_plugin>().get_read_write_api();
auto& chain = app().get_plugin<chain_plugin>();
auto ro_api = chain.get_read_only_api();
auto rw_api = chain.get_read_write_api();

auto& _http_plugin = app().get_plugin<http_plugin>();
ro_api.set_shorten_abi_errors( !_http_plugin.verbose_errors() );
Expand Down Expand Up @@ -106,6 +139,12 @@ void chain_api_plugin::plugin_startup() {
CHAIN_RW_CALL_ASYNC(push_transactions, chain_apis::read_write::push_transactions_results, 202),
CHAIN_RW_CALL_ASYNC(send_transaction, chain_apis::read_write::send_transaction_results, 202)
});

if (chain.account_queries_enabled()) {
_http_plugin.add_async_api({
CHAIN_RO_CALL_WITH_400(get_accounts_by_authorizers, 200),
});
}
}

void chain_api_plugin::plugin_shutdown() {}
Expand Down
1 change: 1 addition & 0 deletions plugins/chain_plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
file(GLOB HEADERS "include/eosio/chain_plugin/*.hpp")
add_library( chain_plugin
account_query_db.cpp
chain_plugin.cpp
${HEADERS} )

Expand Down
Loading

0 comments on commit b40015f

Please sign in to comment.