Electroneum
Electroneum::TransactionHistoryImpl Class Reference

#include <transaction_history.h>

Inheritance diagram for Electroneum::TransactionHistoryImpl:
Collaboration diagram for Electroneum::TransactionHistoryImpl:

Public Member Functions

 TransactionHistoryImpl (WalletImpl *wallet)
 
 ~TransactionHistoryImpl ()
 
virtual int count () const
 
virtual TransactionInfotransaction (int index) const
 
virtual TransactionInfotransaction (const std::string &id) const
 
virtual std::vector< TransactionInfo * > getAll () const
 
virtual void refresh ()
 
- Public Member Functions inherited from Electroneum::TransactionHistory
virtual ~TransactionHistory ()=0
 

Detailed Description

Definition at line 39 of file transaction_history.h.

Constructor & Destructor Documentation

◆ TransactionHistoryImpl()

Electroneum::TransactionHistoryImpl::TransactionHistoryImpl ( WalletImpl wallet)

Definition at line 51 of file transaction_history.cpp.

52  : m_wallet(wallet)
53 {
54 
55 }

◆ ~TransactionHistoryImpl()

Electroneum::TransactionHistoryImpl::~TransactionHistoryImpl ( )

Definition at line 57 of file transaction_history.cpp.

58 {
59  for (auto t : m_history)
60  delete t;
61 }

Member Function Documentation

◆ count()

int Electroneum::TransactionHistoryImpl::count ( ) const
virtual

Implements Electroneum::TransactionHistory.

Definition at line 63 of file transaction_history.cpp.

64 {
65  boost::shared_lock<boost::shared_mutex> lock(m_historyMutex);
66  int result = m_history.size();
67  return result;
68 }

◆ getAll()

std::vector< TransactionInfo * > Electroneum::TransactionHistoryImpl::getAll ( ) const
virtual

Implements Electroneum::TransactionHistory.

Definition at line 90 of file transaction_history.cpp.

91 {
92  boost::shared_lock<boost::shared_mutex> lock(m_historyMutex);
93  return m_history;
94 }

◆ refresh()

void Electroneum::TransactionHistoryImpl::refresh ( )
virtual

Implements Electroneum::TransactionHistory.

Definition at line 96 of file transaction_history.cpp.

97 {
98  // multithreaded access:
99  // boost::lock_guard<boost::mutex> guarg(m_historyMutex);
100  // for "write" access, locking exclusively
101  boost::unique_lock<boost::shared_mutex> lock(m_historyMutex);
102 
103  // TODO: configurable values;
104  uint64_t min_height = 0;
105  uint64_t max_height = (uint64_t)-1;
106  uint64_t wallet_height = m_wallet->blockChainHeight();
107 
108  // delete old transactions;
109  for (auto t : m_history)
110  delete t;
111  m_history.clear();
112 
113  // transactions are stored in wallet2:
114  // - confirmed_transfer_details - out transfers
115  // - unconfirmed_transfer_details - pending out transfers
116  // - payment_details - input transfers
117 
118  // payments are "input transactions";
119  // one input transaction contains only one transfer. e.g. <transaction_id> - <100ETN>
120 
121  std::list<std::pair<crypto::hash, tools::wallet2::payment_details>> in_payments;
122  m_wallet->m_wallet->get_payments(in_payments, min_height, max_height);
123  for (std::list<std::pair<crypto::hash, tools::wallet2::payment_details>>::const_iterator i = in_payments.begin(); i != in_payments.end(); ++i) {
124  const tools::wallet2::payment_details &pd = i->second;
125  std::string payment_id = string_tools::pod_to_hex(i->first);
126  if (payment_id.substr(16).find_first_not_of('0') == std::string::npos)
127  payment_id = payment_id.substr(0,16);
128  TransactionInfoImpl * ti = new TransactionInfoImpl();
129  ti->m_paymentid = payment_id;
130  ti->m_amount = pd.m_amount;
131  ti->m_direction = TransactionInfo::Direction_In;
132  ti->m_hash = string_tools::pod_to_hex(pd.m_tx_hash);
133  ti->m_blockheight = pd.m_block_height;
134  ti->m_subaddrIndex = { pd.m_subaddr_index.minor };
135  ti->m_subaddrAccount = pd.m_subaddr_index.major;
136  ti->m_label = m_wallet->m_wallet->get_subaddress_label(pd.m_subaddr_index);
137  ti->m_timestamp = pd.m_timestamp;
138  ti->m_confirmations = (wallet_height > pd.m_block_height) ? wallet_height - pd.m_block_height : 0;
139  ti->m_unlock_time = pd.m_unlock_time;
140  m_history.push_back(ti);
141 
142  }
143 
144  // confirmed output transactions
145  // one output transaction may contain more than one etn transfer, e.g.
146  // <transaction_id>:
147  // transfer1: 100ETN to <address_1>
148  // transfer2: 50ETN to <address_2>
149  // fee: fee charged per transaction
150  //
151 
152  std::list<std::pair<crypto::hash, tools::wallet2::confirmed_transfer_details>> out_payments;
153  m_wallet->m_wallet->get_payments_out(out_payments, min_height, max_height);
154 
155  for (std::list<std::pair<crypto::hash, tools::wallet2::confirmed_transfer_details>>::const_iterator i = out_payments.begin();
156  i != out_payments.end(); ++i) {
157 
158  const crypto::hash &hash = i->first;
159  const tools::wallet2::confirmed_transfer_details &pd = i->second;
160 
161  uint64_t change = pd.m_change == (uint64_t)-1 ? 0 : pd.m_change; // change may not be known
162  uint64_t fee = pd.m_amount_in - pd.m_amount_out;
163 
164 
165  std::string payment_id = string_tools::pod_to_hex(i->second.m_payment_id);
166  if (payment_id.substr(16).find_first_not_of('0') == std::string::npos)
167  payment_id = payment_id.substr(0,16);
168 
169 
170  TransactionInfoImpl * ti = new TransactionInfoImpl();
171  ti->m_paymentid = payment_id;
172  ti->m_amount = pd.m_amount_in - change - fee;
173  ti->m_fee = fee;
174  ti->m_direction = TransactionInfo::Direction_Out;
175  ti->m_hash = string_tools::pod_to_hex(hash);
176  ti->m_blockheight = pd.m_block_height;
177  ti->m_subaddrIndex = pd.m_subaddr_indices;
178  ti->m_subaddrAccount = pd.m_subaddr_account;
179  ti->m_label = pd.m_subaddr_indices.size() == 1 ? m_wallet->m_wallet->get_subaddress_label({pd.m_subaddr_account, *pd.m_subaddr_indices.begin()}) : "";
180  ti->m_timestamp = pd.m_timestamp;
181  ti->m_confirmations = (wallet_height > pd.m_block_height) ? wallet_height - pd.m_block_height : 0;
182 
183  // single output transaction might contain multiple transfers
184  for (const auto &d: pd.m_dests) {
185  ti->m_transfers.push_back({d.amount, get_account_address_as_str(m_wallet->m_wallet->nettype(), d.is_subaddress, d.addr)});
186  }
187  m_history.push_back(ti);
188  }
189 
190  // unconfirmed output transactions
191  std::list<std::pair<crypto::hash, tools::wallet2::unconfirmed_transfer_details>> upayments_out;
192  m_wallet->m_wallet->get_unconfirmed_payments_out(upayments_out);
193  for (std::list<std::pair<crypto::hash, tools::wallet2::unconfirmed_transfer_details>>::const_iterator i = upayments_out.begin(); i != upayments_out.end(); ++i) {
194  const tools::wallet2::unconfirmed_transfer_details &pd = i->second;
195  const crypto::hash &hash = i->first;
196  uint64_t amount = pd.m_amount_in;
197  uint64_t fee = amount - pd.m_amount_out;
198  std::string payment_id = string_tools::pod_to_hex(i->second.m_payment_id);
199  if (payment_id.substr(16).find_first_not_of('0') == std::string::npos)
200  payment_id = payment_id.substr(0,16);
202 
203  TransactionInfoImpl * ti = new TransactionInfoImpl();
204  ti->m_paymentid = payment_id;
205  ti->m_amount = amount - pd.m_change - fee;
206  ti->m_fee = fee;
207  ti->m_direction = TransactionInfo::Direction_Out;
208  ti->m_failed = is_failed;
209  ti->m_pending = true;
210  ti->m_hash = string_tools::pod_to_hex(hash);
211  ti->m_subaddrIndex = pd.m_subaddr_indices;
212  ti->m_subaddrAccount = pd.m_subaddr_account;
213  ti->m_label = pd.m_subaddr_indices.size() == 1 ? m_wallet->m_wallet->get_subaddress_label({pd.m_subaddr_account, *pd.m_subaddr_indices.begin()}) : "";
214  ti->m_timestamp = pd.m_timestamp;
215  ti->m_confirmations = 0;
216  m_history.push_back(ti);
217  }
218 
219 
220  // unconfirmed payments (tx pool)
221  std::list<std::pair<crypto::hash, tools::wallet2::pool_payment_details>> upayments;
222  m_wallet->m_wallet->get_unconfirmed_payments(upayments);
223  for (std::list<std::pair<crypto::hash, tools::wallet2::pool_payment_details>>::const_iterator i = upayments.begin(); i != upayments.end(); ++i) {
224  const tools::wallet2::payment_details &pd = i->second.m_pd;
225  std::string payment_id = string_tools::pod_to_hex(i->first);
226  if (payment_id.substr(16).find_first_not_of('0') == std::string::npos)
227  payment_id = payment_id.substr(0,16);
228  TransactionInfoImpl * ti = new TransactionInfoImpl();
229  ti->m_paymentid = payment_id;
230  ti->m_amount = pd.m_amount;
231  ti->m_direction = TransactionInfo::Direction_In;
232  ti->m_hash = string_tools::pod_to_hex(pd.m_tx_hash);
233  ti->m_blockheight = pd.m_block_height;
234  ti->m_pending = true;
235  ti->m_subaddrIndex = { pd.m_subaddr_index.minor };
236  ti->m_subaddrAccount = pd.m_subaddr_index.major;
237  ti->m_label = m_wallet->m_wallet->get_subaddress_label(pd.m_subaddr_index);
238  ti->m_timestamp = pd.m_timestamp;
239  ti->m_confirmations = 0;
240  m_history.push_back(ti);
241 
242  LOG_PRINT_L1(__FUNCTION__ << ": Unconfirmed payment found " << pd.m_amount);
243  }
244 
245 }
std::string get_account_address_as_str(network_type nettype, bool subaddress, account_public_address const &adr)
cryptonote::subaddress_index m_subaddr_index
Definition: wallet2.h:362
#define LOG_PRINT_L1(x)
Definition: misc_log_ex.h:100
enum tools::wallet2::unconfirmed_transfer_details::@63 m_state
::std::string string
Definition: gtest-port.h:1097
std::vector< cryptonote::tx_destination_entry > m_dests
Definition: wallet2.h:400
for(i=1;i< 1;++i) fe_sq(t0
std::string pod_to_hex(const t_pod_type &s)
Definition: string_tools.h:317
unsigned __int64 uint64_t
Definition: stdint.h:136
uint64_t blockChainHeight() const override
blockChainHeight - returns current blockchain height
Definition: wallet.cpp:1006
std::set< uint32_t > m_subaddr_indices
Definition: wallet2.h:405
POD_CLASS hash
Definition: hash.h:50
Here is the call graph for this function:

◆ transaction() [1/2]

TransactionInfo * Electroneum::TransactionHistoryImpl::transaction ( int  index) const
virtual

Implements Electroneum::TransactionHistory.

Definition at line 70 of file transaction_history.cpp.

71 {
72  boost::shared_lock<boost::shared_mutex> lock(m_historyMutex);
73  // sanity check
74  if (index < 0)
75  return nullptr;
76  unsigned index_ = static_cast<unsigned>(index);
77  return index_ < m_history.size() ? m_history[index_] : nullptr;
78 }

◆ transaction() [2/2]

TransactionInfo * Electroneum::TransactionHistoryImpl::transaction ( const std::string &  id) const
virtual

Implements Electroneum::TransactionHistory.

Definition at line 80 of file transaction_history.cpp.

81 {
82  boost::shared_lock<boost::shared_mutex> lock(m_historyMutex);
83  auto itr = std::find_if(m_history.begin(), m_history.end(),
84  [&](const TransactionInfo * ti) {
85  return ti->hash() == id;
86  });
87  return itr != m_history.end() ? *itr : nullptr;
88 }

The documentation for this class was generated from the following files: