Electroneum
output_selection.cpp File Reference
#include "gtest/gtest.h"
#include "wallet/wallet2.h"
#include <string>
Include dependency graph for output_selection.cpp:

Go to the source code of this file.

Macros

#define SELECT(idx)
 
#define PICK(expected)
 
#define MKOFFSETS(N, n)
 

Functions

 TEST (select_outputs, one_out_of_N)
 
 TEST (select_outputs, order)
 
 TEST (select_outputs, gamma)
 
 TEST (select_outputs, density)
 
 TEST (select_outputs, same_distribution)
 

Macro Definition Documentation

◆ MKOFFSETS

#define MKOFFSETS (   N,
 
)
Value:
offsets.resize(N); \
size_t n_outs = 0; \
for (auto &offset: offsets) \
{ \
offset = n_outs += (n); \
}

Definition at line 105 of file output_selection.cpp.

◆ PICK

#define PICK (   expected)
Value:
do { \
size_t idx = w.pop_best_value_from(transfers, unused_indices, selected); \
ASSERT_EQ(expected, idx); \
selected.push_back(idx); \
} while(0)

Definition at line 63 of file output_selection.cpp.

◆ SELECT

#define SELECT (   idx)
Value:
do { \
auto i = std::find(unused_indices.begin(), unused_indices.end(), idx); \
ASSERT_TRUE(i != unused_indices.end()); \
unused_indices.erase(i); \
selected.push_back(idx); \
} while(0)

Definition at line 55 of file output_selection.cpp.

Function Documentation

◆ TEST() [1/5]

TEST ( select_outputs  ,
one_out_of_N   
)

Definition at line 70 of file output_selection.cpp.

71 {
73 
74  // check that if there are N-1 outputs of the same height, one of them
75  // already selected, the next one selected is the one that's from a
76  // different height
77  tools::wallet2::transfer_container transfers = make_transfers_container(10);
78  transfers[6].m_block_height = 700;
79  std::vector<size_t> unused_indices({0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
80  std::vector<size_t> selected;
81  SELECT(2);
82  PICK(6);
83 }
std::vector< transfer_details > transfer_container
Definition: wallet2.h:449
#define PICK(expected)
#define SELECT(idx)

◆ TEST() [2/5]

TEST ( select_outputs  ,
order   
)

Definition at line 85 of file output_selection.cpp.

86 {
88 
89  // check that most unrelated heights are picked in order
90  tools::wallet2::transfer_container transfers = make_transfers_container(5);
91  transfers[0].m_block_height = 700;
92  transfers[1].m_block_height = 700;
93  transfers[2].m_block_height = 704;
94  transfers[3].m_block_height = 716;
95  transfers[4].m_block_height = 701;
96  std::vector<size_t> unused_indices({0, 1, 2, 3, 4});
97  std::vector<size_t> selected;
98  SELECT(0);
99  PICK(3); // first the one that's far away
100  PICK(2); // then the one that's close
101  PICK(4); // then the one that's adjacent
102  PICK(1); // then the one that's on the same height
103 }
std::vector< transfer_details > transfer_container
Definition: wallet2.h:449
#define PICK(expected)
#define SELECT(idx)

◆ TEST() [3/5]

TEST ( select_outputs  ,
gamma   
)

Definition at line 113 of file output_selection.cpp.

114 {
115  std::vector<uint64_t> offsets;
116 
117  MKOFFSETS(300000, 1);
118  tools::gamma_picker picker(offsets);
119  std::vector<double> ages(100000);
120  double age_scale = 120. * (offsets.size() / (double)n_outs);
121  for (size_t i = 0; i < ages.size(); )
122  {
123  uint64_t o = picker.pick();
124  if (o >= n_outs)
125  continue;
126  ages[i] = (n_outs - 1 - o) * age_scale;
127  ASSERT_GE(ages[i], 0);
128  ASSERT_LE(ages[i], offsets.size() * 120);
129  ++i;
130  }
131  double median = epee::misc_utils::median(ages);
132  MDEBUG("median age: " << median / 86400. << " days");
133  ASSERT_GE(median, 1.3 * 86400);
134  ASSERT_LE(median, 1.4 * 86400);
135 }
#define ASSERT_GE(val1, val2)
Definition: gtest.h:1972
#define MKOFFSETS(N, n)
#define MDEBUG(x)
Definition: misc_log_ex.h:76
unsigned __int64 uint64_t
Definition: stdint.h:136
type_vec_type median(std::vector< type_vec_type > &v)
#define ASSERT_LE(val1, val2)
Definition: gtest.h:1964
Here is the call graph for this function:

◆ TEST() [4/5]

TEST ( select_outputs  ,
density   
)

Definition at line 137 of file output_selection.cpp.

138 {
139  static const size_t NPICKS = 1000000;
140  std::vector<uint64_t> offsets;
141 
142  MKOFFSETS(300000, 1 + (rand() & 0x1f));
143  tools::gamma_picker picker(offsets);
144 
145  std::vector<int> picks(/*n_outs*/offsets.size(), 0);
146  for (int i = 0; i < NPICKS; )
147  {
148  uint64_t o = picker.pick();
149  if (o >= n_outs)
150  continue;
151  auto it = std::lower_bound(offsets.begin(), offsets.end(), o);
152  auto idx = std::distance(offsets.begin(), it);
153  ASSERT_LT(idx, picks.size());
154  ++picks[idx];
155  ++i;
156  }
157 
158  for (int d = 1; d < 0x20; ++d)
159  {
160  // count the number of times an output in a block of d outputs was selected
161  // count how many outputs are in a block of d outputs
162  size_t count_selected = 0, count_chain = 0;
163  for (size_t i = 0; i < offsets.size(); ++i)
164  {
165  size_t n_outputs = offsets[i] - (i == 0 ? 0 : offsets[i - 1]);
166  if (n_outputs == d)
167  {
168  count_selected += picks[i];
169  count_chain += d;
170  }
171  }
172  float selected_ratio = count_selected / (float)NPICKS;
173  float chain_ratio = count_chain / (float)n_outs;
174  MDEBUG(count_selected << "/" << NPICKS << " outputs selected in blocks of density " << d << ", " << 100.0f * selected_ratio << "%");
175  MDEBUG(count_chain << "/" << offsets.size() << " outputs in blocks of density " << d << ", " << 100.0f * chain_ratio << "%");
176  ASSERT_LT(fabsf(selected_ratio - chain_ratio), 0.025f);
177  }
178 }
#define MKOFFSETS(N, n)
#define MDEBUG(x)
Definition: misc_log_ex.h:76
void rand(size_t N, uint8_t *bytes)
Definition: crypto.h:209
unsigned __int64 uint64_t
Definition: stdint.h:136
#define ASSERT_LT(val1, val2)
Definition: gtest.h:1968
Here is the call graph for this function:

◆ TEST() [5/5]

TEST ( select_outputs  ,
same_distribution   
)

Definition at line 180 of file output_selection.cpp.

181 {
182  static const size_t NPICKS = 1000000;
183  std::vector<uint64_t> offsets;
184 
185  MKOFFSETS(300000, 1 + (rand() & 0x1f));
186  tools::gamma_picker picker(offsets);
187 
188  std::vector<int> chain_picks(offsets.size(), 0);
189  std::vector<int> output_picks(n_outs, 0);
190  for (int i = 0; i < NPICKS; )
191  {
192  uint64_t o = picker.pick();
193  if (o >= n_outs)
194  continue;
195  auto it = std::lower_bound(offsets.begin(), offsets.end(), o);
196  auto idx = std::distance(offsets.begin(), it);
197  ASSERT_LT(idx, chain_picks.size());
198  ++chain_picks[idx];
199  ++output_picks[o];
200  ++i;
201  }
202 
203  // scale them both to 0-100
204  std::vector<int> chain_norm(100, 0), output_norm(100, 0);
205  for (size_t i = 0; i < output_picks.size(); ++i)
206  output_norm[i * 100 / output_picks.size()] += output_picks[i];
207  for (size_t i = 0; i < chain_picks.size(); ++i)
208  chain_norm[i * 100 / chain_picks.size()] += chain_picks[i];
209 
210  double max_dev = 0.0, avg_dev = 0.0;
211  for (size_t i = 0; i < 100; ++i)
212  {
213  const double diff = (double)output_norm[i] - (double)chain_norm[i];
214  double dev = fabs(2.0 * diff / (output_norm[i] + chain_norm[i]));
215  ASSERT_LT(dev, 0.1);
216  avg_dev += dev;
217  }
218  avg_dev /= 100;
219  MDEBUG("avg_dev: " << avg_dev);
220  ASSERT_LT(avg_dev, 0.015);
221 }
#define MKOFFSETS(N, n)
#define MDEBUG(x)
Definition: misc_log_ex.h:76
void rand(size_t N, uint8_t *bytes)
Definition: crypto.h:209
unsigned __int64 uint64_t
Definition: stdint.h:136
#define ASSERT_LT(val1, val2)
Definition: gtest.h:1968
Here is the call graph for this function: