From 6e81377686c0db83dc28dca437f3350e758e5c03 Mon Sep 17 00:00:00 2001 From: Brian Johnson Date: Tue, 23 Jan 2018 16:07:58 -0600 Subject: [PATCH] Fixed get_active_producers for c API passing buffer byte length and added returning the number of bytes written, so user knows how many bytes were populated. --- contracts/eoslib/chain.h | 12 ++++++------ libraries/chain/apply_context.cpp | 5 +++-- .../chain/include/eosio/chain/apply_context.hpp | 2 +- libraries/chain/wasm_interface.cpp | 7 ++++--- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/contracts/eoslib/chain.h b/contracts/eoslib/chain.h index a15e6f4d4..af3757235 100644 --- a/contracts/eoslib/chain.h +++ b/contracts/eoslib/chain.h @@ -21,19 +21,19 @@ extern "C" { /** - * @brief Return the set of active producers - * @details Return the set of active producers - * - * @param producers - location to store the active producers + * Return the set of active producers + * @param producers - a pointer to an buffer of account_names + * @param datalen - byte length of buffer + * @return the number of bytes actually populated * * Example: * @code * account_name producers[21]; - * get_active_producers(producers, sizeof(account_name)*21); + * uint32_t bytes_populated = get_active_producers(producers, sizeof(account_name)*21); * @endcode */ - void get_active_producers( account_name* producers, uint32_t datalen ); + uint32_t get_active_producers( account_name* producers, uint32_t datalen ); ///@ } chaincapi } diff --git a/libraries/chain/apply_context.cpp b/libraries/chain/apply_context.cpp index 54c68ccc3..e0fbeb3a1 100644 --- a/libraries/chain/apply_context.cpp +++ b/libraries/chain/apply_context.cpp @@ -224,15 +224,16 @@ const contracts::table_id_object& apply_context::find_or_create_table( name scop }); } -void apply_context::get_active_producers(account_name* producers, size_t datalen) { +size_t apply_context::get_active_producers(account_name* producers, size_t len) { const auto& gpo = controller.get_global_properties(); int index = 0; for(const auto& producer : gpo.active_producers.producers) { *(producers + index) = producer.producer_name; - if (++index >= datalen) + if (++index >= len) break; } + return index; } } } /// eosio::chain diff --git a/libraries/chain/include/eosio/chain/apply_context.hpp b/libraries/chain/include/eosio/chain/apply_context.hpp index eaf67dc9b..06c6d5c4e 100644 --- a/libraries/chain/include/eosio/chain/apply_context.hpp +++ b/libraries/chain/include/eosio/chain/apply_context.hpp @@ -94,7 +94,7 @@ class apply_context { bool all_authorizations_used()const; vector unused_authorizations()const; - void get_active_producers(account_name* producers, size_t len); + size_t get_active_producers(account_name* producers, size_t len); const chain_controller& controller; const chainbase::database& db; ///< database where state is stored diff --git a/libraries/chain/wasm_interface.cpp b/libraries/chain/wasm_interface.cpp index 4e5df22df..468d392f3 100644 --- a/libraries/chain/wasm_interface.cpp +++ b/libraries/chain/wasm_interface.cpp @@ -448,8 +448,9 @@ class producer_api : public context_aware_api { public: using context_aware_api::context_aware_api; - void get_active_producers(array_ptr producers, size_t datalen) { - context.get_active_producers(producers, datalen); + int get_active_producers(array_ptr producers, size_t datalen) { + auto actual_num_prod = context.get_active_producers(producers, datalen / sizeof(chain::account_name)); + return actual_num_prod * sizeof(chain::account_name); } }; @@ -742,7 +743,7 @@ class transaction_api : public context_aware_api { }; REGISTER_INTRINSICS(producer_api, - (get_active_producers, void(int, int)) + (get_active_producers, int(int, int)) ); REGISTER_INTRINSICS(crypto_api, -- GitLab