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 #4470 from wanderingbort/feature/get_scheduled_tra…
Browse files Browse the repository at this point in the history
…nsactions

Add an RPC endpoint to fetch the scheduled transactions
  • Loading branch information
heifner authored Jul 14, 2018
2 parents a743c5d + 257badf commit c879b6a
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions plugins/chain_api_plugin/chain_api_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ void chain_api_plugin::plugin_startup() {
CHAIN_RO_CALL(get_currency_stats, 200),
CHAIN_RO_CALL(get_producers, 200),
CHAIN_RO_CALL(get_producer_schedule, 200),
CHAIN_RO_CALL(get_scheduled_transactions, 200),
CHAIN_RO_CALL(abi_json_to_bin, 200),
CHAIN_RO_CALL(abi_bin_to_json, 200),
CHAIN_RO_CALL(get_required_keys, 200),
Expand Down
75 changes: 75 additions & 0 deletions plugins/chain_plugin/chain_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <eosio/chain/resource_limits.hpp>
#include <eosio/chain/reversible_block_object.hpp>
#include <eosio/chain/controller.hpp>
#include <eosio/chain/generated_transaction_object.hpp>

#include <eosio/chain/eosio_contract.hpp>

Expand Down Expand Up @@ -1079,6 +1080,80 @@ auto make_resolver(const Api* api, const fc::microseconds& max_serialization_tim
return resolver_factory<Api>::make(api, max_serialization_time);
}


read_only::get_scheduled_transactions_result
read_only::get_scheduled_transactions( const read_only::get_scheduled_transactions_params& p ) const {
const auto& d = db.db();

const auto& idx_by_delay = d.get_index<generated_transaction_multi_index,by_delay>();
auto itr = ([&](){
if (!p.lower_bound.empty()) {
try {
auto when = time_point::from_iso_string( p.lower_bound );
return idx_by_delay.lower_bound(boost::make_tuple(when));
} catch (...) {
try {
auto txid = transaction_id_type(p.lower_bound);
const auto& by_txid = d.get_index<generated_transaction_multi_index,by_trx_id>();
auto itr = by_txid.find( txid );
if (itr == by_txid.end()) {
EOS_THROW(transaction_exception, "Unknown Transaction ID: ${txid}", ("txid", txid));
}

return d.get_index<generated_transaction_multi_index>().indices().project<by_delay>(itr);

} catch (...) {
return idx_by_delay.end();
}
}
} else {
return idx_by_delay.begin();
}
})();

read_only::get_scheduled_transactions_result result;

auto resolver = make_resolver(this, abi_serializer_max_time);

uint32_t remaining = p.limit;
auto time_limit = fc::time_point::now() + fc::microseconds(1000 * 10); /// 10ms max time
while (itr != idx_by_delay.end() && remaining > 0 && time_limit > fc::time_point::now()) {
auto row = fc::mutable_variant_object()
("trx_id", itr->trx_id)
("sender", itr->sender)
("sender_id", itr->sender_id)
("payer", itr->payer)
("delay_until", itr->delay_until)
("expiration", itr->expiration)
("published", itr->published)
;

if (p.json) {
fc::variant pretty_transaction;

transaction trx;
fc::datastream<const char*> ds( itr->packed_trx.data(), itr->packed_trx.size() );
fc::raw::unpack(ds,trx);

abi_serializer::to_variant(trx, pretty_transaction, resolver, abi_serializer_max_time);
row("transaction", pretty_transaction);
} else {
auto packed_transaction = bytes(itr->packed_trx.begin(), itr->packed_trx.end());
row("transaction", packed_transaction);
}

result.transactions.emplace_back(std::move(row));
++itr;
remaining--;
}

if (itr != idx_by_delay.end()) {
result.more = string(itr->trx_id);
}

return result;
}

fc::variant read_only::get_block(const read_only::get_block_params& params) const {
signed_block_ptr block;
EOS_ASSERT(!params.block_num_or_id.empty() && params.block_num_or_id.size() <= 64, chain::block_id_type_exception, "Invalid Block number or ID, must be greater than 0 and less than 64 characters" );
Expand Down
16 changes: 16 additions & 0 deletions plugins/chain_plugin/include/eosio/chain_plugin/chain_plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,19 @@ class read_only {

get_producer_schedule_result get_producer_schedule( const get_producer_schedule_params& params )const;

struct get_scheduled_transactions_params {
bool json = false;
string lower_bound; /// timestamp OR transaction ID
uint32_t limit = 50;
};

struct get_scheduled_transactions_result {
fc::variants transactions;
string more; ///< fill lower_bound with this to fetch next set of transactions
};

get_scheduled_transactions_result get_scheduled_transactions( const get_scheduled_transactions_params& params ) const;

static void copy_inline_row(const chain::key_value_object& obj, vector<char>& data) {
data.resize( obj.value.size() );
memcpy( data.data(), obj.value.data(), obj.value.size() );
Expand Down Expand Up @@ -540,6 +553,9 @@ FC_REFLECT( eosio::chain_apis::read_only::get_producers_result, (rows)(total_pro
FC_REFLECT_EMPTY( eosio::chain_apis::read_only::get_producer_schedule_params )
FC_REFLECT( eosio::chain_apis::read_only::get_producer_schedule_result, (active)(pending)(proposed) );

FC_REFLECT( eosio::chain_apis::read_only::get_scheduled_transactions_params, (json)(lower_bound)(limit) )
FC_REFLECT( eosio::chain_apis::read_only::get_scheduled_transactions_result, (transactions)(more) );

FC_REFLECT( eosio::chain_apis::read_only::get_account_results,
(account_name)(head_block_num)(head_block_time)(privileged)(last_code_update)(created)
(core_liquid_balance)(ram_quota)(net_weight)(cpu_weight)(net_limit)(cpu_limit)(ram_usage)(permissions)
Expand Down

0 comments on commit c879b6a

Please sign in to comment.