1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
#include <boost/optional/optional.hpp>
#include <boost/range/adaptor/indexed.hpp>
#include <gtest/gtest.h>
#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <vector>
#include "byte_stream.h"
#include "crypto/hash.h"
#include "cryptonote_basic/account.h"
#include "cryptonote_basic/cryptonote_basic.h"
#include "cryptonote_basic/cryptonote_format_utils.h"
#include "cryptonote_core/cryptonote_tx_utils.h"
#include "serialization/json_object.h"
namespace test
{
cryptonote::transaction
make_miner_transaction(cryptonote::account_public_address const& to)
{
cryptonote::transaction tx{};
if (!cryptonote::construct_miner_tx(0, 0, 5000, 500, 500, to, tx))
throw std::runtime_error{"transaction construction error"};
crypto::hash id{0};
if (!cryptonote::get_transaction_hash(tx, id))
throw std::runtime_error{"could not get transaction hash"};
return tx;
}
cryptonote::transaction
make_transaction(
cryptonote::account_keys const& from,
std::vector<cryptonote::transaction> const& sources,
std::vector<cryptonote::account_public_address> const& destinations,
bool rct,
bool bulletproof)
{
std::uint64_t source_amount = 0;
std::vector<cryptonote::tx_source_entry> actual_sources;
for (auto const& source : sources)
{
std::vector<cryptonote::tx_extra_field> extra_fields;
if (!cryptonote::parse_tx_extra(source.extra, extra_fields))
throw std::runtime_error{"invalid transaction"};
cryptonote::tx_extra_pub_key key_field{};
if (!cryptonote::find_tx_extra_field_by_type(extra_fields, key_field))
throw std::runtime_error{"invalid transaction"};
for (auto const& input : boost::adaptors::index(source.vout))
{
source_amount += input.value().amount;
auto const& key = boost::get<cryptonote::txout_to_key>(input.value().target);
actual_sources.push_back(
{{}, 0, key_field.pub_key, {}, std::size_t(input.index()), input.value().amount, rct, rct::identity()}
);
for (unsigned ring = 0; ring < 10; ++ring)
actual_sources.back().push_output(input.index(), key.key, input.value().amount);
}
}
std::vector<cryptonote::tx_destination_entry> to;
for (auto const& destination : destinations)
to.push_back({(source_amount / destinations.size()), destination, false});
cryptonote::transaction tx{};
crypto::secret_key tx_key{};
std::vector<crypto::secret_key> extra_keys{};
std::unordered_map<crypto::public_key, cryptonote::subaddress_index> subaddresses;
subaddresses[from.m_account_address.m_spend_public_key] = {0,0};
if (!cryptonote::construct_tx_and_get_tx_key(from, subaddresses, actual_sources, to, boost::none, {}, tx, 0, tx_key, extra_keys, rct, { bulletproof ? rct::RangeProofBulletproof : rct::RangeProofBorromean, bulletproof ? 2 : 0 }))
throw std::runtime_error{"transaction construction error"};
return tx;
}
}
namespace
{
template<typename T>
T test_json(const T& value)
{
epee::byte_stream buffer;
{
rapidjson::Writer<epee::byte_stream> dest{buffer};
cryptonote::json::toJsonValue(dest, value);
}
rapidjson::Document doc;
doc.Parse(reinterpret_cast<const char*>(buffer.data()), buffer.size());
if (doc.HasParseError() || !doc.IsObject())
{
throw cryptonote::json::PARSE_FAIL();
}
T out{};
cryptonote::json::fromJsonValue(doc, out);
return out;
}
} // anonymous
TEST(JsonSerialization, MinerTransaction)
{
cryptonote::account_base acct;
acct.generate();
const auto miner_tx = test::make_miner_transaction(acct.get_keys().m_account_address);
crypto::hash tx_hash{};
ASSERT_TRUE(cryptonote::get_transaction_hash(miner_tx, tx_hash));
cryptonote::transaction miner_tx_copy = test_json(miner_tx);
crypto::hash tx_copy_hash{};
ASSERT_TRUE(cryptonote::get_transaction_hash(miner_tx_copy, tx_copy_hash));
EXPECT_EQ(tx_hash, tx_copy_hash);
cryptonote::blobdata tx_bytes{};
cryptonote::blobdata tx_copy_bytes{};
ASSERT_TRUE(cryptonote::t_serializable_object_to_blob(miner_tx, tx_bytes));
ASSERT_TRUE(cryptonote::t_serializable_object_to_blob(miner_tx_copy, tx_copy_bytes));
EXPECT_EQ(tx_bytes, tx_copy_bytes);
}
TEST(JsonSerialization, RegularTransaction)
{
cryptonote::account_base acct1;
acct1.generate();
cryptonote::account_base acct2;
acct2.generate();
const auto miner_tx = test::make_miner_transaction(acct1.get_keys().m_account_address);
const auto tx = test::make_transaction(
acct1.get_keys(), {miner_tx}, {acct2.get_keys().m_account_address}, false, false
);
crypto::hash tx_hash{};
ASSERT_TRUE(cryptonote::get_transaction_hash(tx, tx_hash));
cryptonote::transaction tx_copy = test_json(tx);
crypto::hash tx_copy_hash{};
ASSERT_TRUE(cryptonote::get_transaction_hash(tx_copy, tx_copy_hash));
EXPECT_EQ(tx_hash, tx_copy_hash);
cryptonote::blobdata tx_bytes{};
cryptonote::blobdata tx_copy_bytes{};
ASSERT_TRUE(cryptonote::t_serializable_object_to_blob(tx, tx_bytes));
ASSERT_TRUE(cryptonote::t_serializable_object_to_blob(tx_copy, tx_copy_bytes));
EXPECT_EQ(tx_bytes, tx_copy_bytes);
}
TEST(JsonSerialization, RingctTransaction)
{
cryptonote::account_base acct1;
acct1.generate();
cryptonote::account_base acct2;
acct2.generate();
const auto miner_tx = test::make_miner_transaction(acct1.get_keys().m_account_address);
const auto tx = test::make_transaction(
acct1.get_keys(), {miner_tx}, {acct2.get_keys().m_account_address}, true, false
);
crypto::hash tx_hash{};
ASSERT_TRUE(cryptonote::get_transaction_hash(tx, tx_hash));
cryptonote::transaction tx_copy = test_json(tx);
crypto::hash tx_copy_hash{};
ASSERT_TRUE(cryptonote::get_transaction_hash(tx_copy, tx_copy_hash));
EXPECT_EQ(tx_hash, tx_copy_hash);
cryptonote::blobdata tx_bytes{};
cryptonote::blobdata tx_copy_bytes{};
ASSERT_TRUE(cryptonote::t_serializable_object_to_blob(tx, tx_bytes));
ASSERT_TRUE(cryptonote::t_serializable_object_to_blob(tx_copy, tx_copy_bytes));
EXPECT_EQ(tx_bytes, tx_copy_bytes);
}
TEST(JsonSerialization, BulletproofTransaction)
{
cryptonote::account_base acct1;
acct1.generate();
cryptonote::account_base acct2;
acct2.generate();
const auto miner_tx = test::make_miner_transaction(acct1.get_keys().m_account_address);
const auto tx = test::make_transaction(
acct1.get_keys(), {miner_tx}, {acct2.get_keys().m_account_address}, true, true
);
crypto::hash tx_hash{};
ASSERT_TRUE(cryptonote::get_transaction_hash(tx, tx_hash));
cryptonote::transaction tx_copy = test_json(tx);
crypto::hash tx_copy_hash{};
ASSERT_TRUE(cryptonote::get_transaction_hash(tx_copy, tx_copy_hash));
EXPECT_EQ(tx_hash, tx_copy_hash);
cryptonote::blobdata tx_bytes{};
cryptonote::blobdata tx_copy_bytes{};
ASSERT_TRUE(cryptonote::t_serializable_object_to_blob(tx, tx_bytes));
ASSERT_TRUE(cryptonote::t_serializable_object_to_blob(tx_copy, tx_copy_bytes));
EXPECT_EQ(tx_bytes, tx_copy_bytes);
}
|