Electroneum
output_distribution.cpp
Go to the documentation of this file.
1 // Copyright (c) 2018, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #include "gtest/gtest.h"
30 #include "misc_log_ex.h"
31 #include "rpc/rpc_handler.h"
36 #include "blockchain_db/testdb.h"
37 
38 static const uint64_t test_distribution[32] = {
39  0, 0, 0, 0, 0, 1, 5, 1, 4, 0, 0, 1, 0, 1, 2, 3, 1, 0, 2, 0, 1, 3, 8, 1, 3, 5, 7, 1, 5, 0, 2, 3
40 };
41 static const size_t test_distribution_size = sizeof(test_distribution) / sizeof(test_distribution[0]);
42 
43 namespace
44 {
45 
46 class TestDB: public cryptonote::BaseTestDB
47 {
48 public:
49  TestDB(size_t bc_height = test_distribution_size): blockchain_height(bc_height) { m_open = true; }
50  virtual uint64_t height() const override { return blockchain_height; }
51 
52  std::vector<uint64_t> get_block_cumulative_rct_outputs(const std::vector<uint64_t> &heights) const override
53  {
54  std::vector<uint64_t> d;
55  for (uint64_t h: heights)
56  {
57  uint64_t c = 0;
58  for (uint64_t i = 0; i <= h; ++i)
59  c += test_distribution[i];
60  d.push_back(c);
61  }
62  return d;
63  }
64 
65  std::vector<uint64_t> get_block_weights(uint64_t start_offset, size_t count) const override
66  {
67  std::vector<uint64_t> weights;
68  while (count--) weights.push_back(1);
69  return weights;
70  }
71 
72  uint64_t blockchain_height;
73 };
74 
75 }
76 
77 bool get_output_distribution(uint64_t amount, uint64_t from, uint64_t to, uint64_t &start_height, std::vector<uint64_t> &distribution, uint64_t &base)
78 {
79  std::unique_ptr<cryptonote::Blockchain> bc;
80  cryptonote::tx_memory_pool txpool(*bc);
81  bc.reset(new cryptonote::Blockchain(txpool));
82  struct get_test_options {
83  const std::pair<uint8_t, uint64_t> hard_forks[2];
86  };
87  get_test_options():hard_forks{std::make_pair((uint8_t)1, (uint64_t)0), std::make_pair((uint8_t)0, (uint64_t)0)}{}
88  } opts;
89  cryptonote::Blockchain *blockchain = bc.get();
90  bool r = blockchain->init(new TestDB(test_distribution_size), cryptonote::FAKECHAIN, true, &opts.test_options, 0, NULL);
91  return r && bc->get_output_distribution(amount, from, to, start_height, distribution, base);
92 }
93 
95 {
97  *((uint64_t*)&hash) = height;
98  return hash;
99 }
100 
101 TEST(output_distribution, extend)
102 {
103  boost::optional<cryptonote::rpc::output_distribution_data> res;
104 
106  ASSERT_TRUE(res != boost::none);
107  ASSERT_EQ(res->distribution.size(), 2);
108  ASSERT_EQ(res->distribution, std::vector<uint64_t>({5, 0}));
109 
111  ASSERT_TRUE(res != boost::none);
112  ASSERT_EQ(res->distribution.size(), 2);
113  ASSERT_EQ(res->distribution, std::vector<uint64_t>({55, 55}));
114 
116  ASSERT_TRUE(res != boost::none);
117  ASSERT_EQ(res->distribution.size(), 3);
118  ASSERT_EQ(res->distribution, std::vector<uint64_t>({5, 0, 2}));
119 
121  ASSERT_TRUE(res != boost::none);
122  ASSERT_EQ(res->distribution.size(), 3);
123  ASSERT_EQ(res->distribution, std::vector<uint64_t>({55, 55, 57}));
124 
126  ASSERT_TRUE(res != boost::none);
127  ASSERT_EQ(res->distribution.size(), 4);
128  ASSERT_EQ(res->distribution, std::vector<uint64_t>({5, 0, 2, 3}));
129 
131  ASSERT_TRUE(res != boost::none);
132  ASSERT_EQ(res->distribution.size(), 4);
133  ASSERT_EQ(res->distribution, std::vector<uint64_t>({55, 55, 57, 60}));
134 }
135 
136 TEST(output_distribution, one)
137 {
138  boost::optional<cryptonote::rpc::output_distribution_data> res;
139 
141  ASSERT_TRUE(res != boost::none);
142  ASSERT_EQ(res->distribution.size(), 1);
143  ASSERT_EQ(res->distribution.back(), 0);
144 }
145 
146 TEST(output_distribution, full_cumulative)
147 {
148  boost::optional<cryptonote::rpc::output_distribution_data> res;
149 
151  ASSERT_TRUE(res != boost::none);
152  ASSERT_EQ(res->distribution.size(), 32);
153  ASSERT_EQ(res->distribution.back(), 60);
154 }
155 
156 TEST(output_distribution, full_noncumulative)
157 {
158  boost::optional<cryptonote::rpc::output_distribution_data> res;
159 
161  ASSERT_TRUE(res != boost::none);
162  ASSERT_EQ(res->distribution.size(), 32);
163  for (size_t i = 0; i < 32; ++i)
164  ASSERT_EQ(res->distribution[i], test_distribution[i]);
165 }
166 
167 TEST(output_distribution, part_cumulative)
168 {
169  boost::optional<cryptonote::rpc::output_distribution_data> res;
170 
172  ASSERT_TRUE(res != boost::none);
173  ASSERT_EQ(res->distribution.size(), 5);
174  ASSERT_EQ(res->distribution, std::vector<uint64_t>({0, 1, 6, 7, 11}));
175 }
176 
177 TEST(output_distribution, part_noncumulative)
178 {
179  boost::optional<cryptonote::rpc::output_distribution_data> res;
180 
182  ASSERT_TRUE(res != boost::none);
183  ASSERT_EQ(res->distribution.size(), 5);
184  ASSERT_EQ(res->distribution, std::vector<uint64_t>({0, 1, 5, 1, 4}));
185 }
const char * res
Definition: hmac_keccak.cpp:41
uint64_t height
Definition: blockchain.cpp:91
unsigned char uint8_t
Definition: stdint.h:124
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
mdb_size_t count(MDB_cursor *cur)
const std::pair< uint8_t, uint64_t > hard_forks[2]
Definition: chaingen.h:698
bool get_output_distribution(uint64_t amount, uint64_t from_height, uint64_t to_height, uint64_t &start_height, std::vector< uint64_t > &distribution, uint64_t &base) const
gets per block distribution of outputs of a given amount
unsigned __int64 uint64_t
Definition: stdint.h:136
Transaction pool, handles transactions which are not part of a block.
Definition: tx_pool.h:94
TEST(output_distribution, extend)
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
const cryptonote::test_options test_options
Definition: chaingen.h:699
bool init(BlockchainDB *db, const network_type nettype=MAINNET, bool offline=false, const cryptonote::test_options *test_options=NULL, difficulty_type fixed_difficulty=0, const GetCheckpointsCallback &get_checkpoints=nullptr, bool ignore_bsig=false, bool fallback_to_pow=false)
Initialize the Blockchain state.
Definition: blockchain.cpp:334
bool get_output_distribution(uint64_t amount, uint64_t from, uint64_t to, uint64_t &start_height, std::vector< uint64_t > &distribution, uint64_t &base)
crypto::hash get_block_hash(uint64_t height)
POD_CLASS hash
Definition: hash.h:50
static boost::optional< output_distribution_data > get_output_distribution(const std::function< bool(uint64_t, uint64_t, uint64_t, uint64_t &, std::vector< uint64_t > &, uint64_t &)> &f, uint64_t amount, uint64_t from_height, uint64_t to_height, const std::function< crypto::hash(uint64_t)> &get_hash, bool cumulative, uint64_t blockchain_height)
Definition: rpc_handler.cpp:29