Electroneum
Loading...
Searching...
No Matches
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
38static 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};
41static const size_t test_distribution_size = sizeof(test_distribution) / sizeof(test_distribution[0]);
42
43namespace
44{
45
46class TestDB: public cryptonote::BaseTestDB
47{
48public:
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
77bool 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;
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{
96 crypto::hash hash;
97 *((uint64_t*)&hash) = height;
98 return hash;
99}
100
101TEST(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
136TEST(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
146TEST(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
156TEST(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
167TEST(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
177TEST(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}
uint64_t height
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.
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)
Transaction pool, handles transactions which are not part of a block.
Definition tx_pool.h:95
#define ASSERT_EQ(val1, val2)
Definition gtest.h:1956
#define TEST(test_case_name, test_name)
Definition gtest.h:2187
#define ASSERT_TRUE(condition)
Definition gtest.h:1865
const char * res
POD_CLASS hash
Definition hash.h:50
crypto::hash get_block_hash(uint64_t height)
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)
unsigned char uint8_t
Definition stdint.h:124
unsigned __int64 uint64_t
Definition stdint.h:136
const std::pair< uint8_t, uint64_t > hard_forks[2]
Definition chaingen.h:698
const cryptonote::test_options test_options
Definition chaingen.h:699