Scorum
devcommittee_history_api.cpp
Go to the documentation of this file.
9 
10 #include <map>
11 
12 namespace scorum {
13 namespace blockchain_history {
14 
15 namespace detail {
17 {
18 public:
20 
21 public:
23  : _app(app)
24  {
25  }
26 
27  template <typename history_object_type, typename fill_result_functor>
28  void get_history(uint64_t from, uint32_t limit, fill_result_functor& funct) const
29  {
30  const auto db = _app.chain_database();
31 
32  FC_ASSERT(limit > 0, "Limit must be greater than zero");
33  FC_ASSERT(limit <= MAX_BLOCKCHAIN_HISTORY_DEPTH, "Limit of ${l} is greater than maxmimum allowed ${2} ",
34  ("l", limit)("2", MAX_BLOCKCHAIN_HISTORY_DEPTH));
35 
36  const auto& idx = db->get_index<devcommittee_history_index<history_object_type>, by_id_desc>();
37 
38  auto from_ = std::min((uint64_t)std::numeric_limits<int64_t>::max(), from);
39  for (auto it = idx.lower_bound(from_); it != idx.end() && limit; ++it, --limit)
40  {
41  funct(*it);
42  }
43  }
44 
45  template <typename history_object_type>
46  std::vector<applied_operation> get_history(uint64_t from, uint32_t limit) const
47  {
48  std::vector<applied_operation> result;
49 
50  const auto db = _app.chain_database();
51 
52  auto fill_funct = [&](const history_object_type& hobj) { result.emplace_back(db->get(hobj.op)); };
53  this->template get_history<history_object_type>(from, limit, fill_funct);
54 
55  return result;
56  }
57 };
58 } // namespace detail
59 
61  : _impl(new detail::devcommittee_history_api_impl(ctx.app))
62 {
63 }
64 
66 
68 {
69 }
70 
71 std::vector<applied_operation> devcommittee_history_api::get_scr_to_scr_transfers(uint64_t from, uint32_t limit) const
72 {
73  const auto db = _impl->_app.chain_database();
74  return db->with_read_lock(
75  [&]() { return _impl->get_history<devcommittee_transfers_to_scr_history_object>(from, limit); });
76 }
77 
78 std::vector<applied_operation> devcommittee_history_api::get_history(uint64_t from, uint32_t limit) const
79 {
80  const auto db = _impl->_app.chain_database();
81  return db->with_read_lock([&]() { return _impl->get_history<devcommittee_history_object>(from, limit); });
82 }
83 
84 std::vector<applied_withdraw_operation> devcommittee_history_api::get_sp_to_scr_transfers(uint64_t from,
85  uint32_t limit) const
86 {
87  const auto db = _impl->_app.chain_database();
88  return db->with_read_lock([&]() {
89  std::vector<applied_withdraw_operation> result;
90 
91  auto fill_funct = [&](const devcommittee_withdrawals_to_scr_history_object& obj) {
92  result.emplace_back(db->get(obj.op));
93  auto& applied_op = result.back();
94 
95  share_type to_withdraw = 0;
96  applied_op.op.weak_visit([&](const proposal_virtual_operation& proposal_op) {
97  proposal_op.proposal_op.weak_visit([&](const development_committee_withdraw_vesting_operation& op) {
98  to_withdraw = op.vesting_shares.amount;
99  });
100  });
101 
102  if (to_withdraw == 0u)
103  {
104  // If this is a zero-withdraw (such withdraw closes current active withdraw)
105  applied_op.status = applied_withdraw_operation::empty;
106  }
107  else if (!obj.progress.empty())
108  {
109  auto last_op = fc::raw::unpack<operation>(db->get(obj.progress.back()).serialized_op);
110 
111  last_op.weak_visit(
113  // if last 'progress' operation is 'devpool_finished_' then withdraw was finished
114  applied_op.status = applied_withdraw_operation::finished;
115  },
116  [&](const proposal_virtual_operation& proposal_op) {
117  FC_ASSERT(
118  proposal_op.proposal_op.which()
119  == proposal_operation::tag<development_committee_withdraw_vesting_operation>::value,
120  "The only proposal_virtual_operation which can be in withdraw progress is a "
121  "'development_committee_withdraw_vesting_operation' one");
122  // if last 'progress' operation is 'proposal_virtual_operation' then withdraw was either
123  // interrupted or finished depending on pre-last 'progress' operation
124  applied_op.status = applied_withdraw_operation::interrupted;
125  });
126 
127  if (obj.progress.size() > 1)
128  {
129  auto before_last_op_obj = db->get(*(obj.progress.rbegin() + 1));
130  auto before_last_op = fc::raw::unpack<operation>(before_last_op_obj.serialized_op);
131 
132  before_last_op.weak_visit([&](const devpool_finished_vesting_withdraw_operation&) {
133  // if pre-last 'progress' operation is 'acc_finished_' then withdraw was finished
134  applied_op.status = applied_withdraw_operation::finished;
135  });
136  }
137 
138  for (auto& id : obj.progress)
139  {
140  auto op = fc::raw::unpack<operation>(db->get(id).serialized_op);
141 
142  op.weak_visit([&](const devpool_to_devpool_vesting_withdraw_operation& op) {
143  applied_op.withdrawn += op.withdrawn.amount;
144  });
145  }
146  }
147  };
148  _impl->get_history<devcommittee_withdrawals_to_scr_history_object>(from, limit, fill_funct);
149 
150  return result;
151  });
152 }
153 
154 } // namespace blockchain_history
155 } // namespace scorum
std::shared_ptr< chain::database > chain_database() const
std::vector< applied_operation > get_history(uint64_t from, uint32_t limit) const
void get_history(uint64_t from, uint32_t limit, fill_result_functor &funct) const
devcommittee_history_api(const scorum::app::api_context &ctx)
#define MAX_BLOCKCHAIN_HISTORY_DEPTH
Definition: config_api.hpp:67
std::vector< applied_operation > get_scr_to_scr_transfers(uint64_t from, uint32_t limit) const
std::vector< applied_withdraw_operation > get_sp_to_scr_transfers(uint64_t from, uint32_t limit) const
std::vector< applied_operation > get_history(uint64_t from, uint32_t limit) const
shared_multi_index_container< history_object_t, indexed_by< ordered_unique< tag< by_id >, member< history_object_t, typename history_object_t::id_type, &history_object_t::id > >, ordered_unique< tag< by_id_desc >, member< history_object_t, typename history_object_t::id_type, &history_object_t::id >, std::greater< typename history_object_t::id_type > >> > devcommittee_history_index
fc::safe< share_value_type > share_type
Definition: types.hpp:73
Definition: asset.cpp:15
share_type amount
Definition: asset.hpp:31