| 1 | |
| 2 | #include <boost/optional/optional.hpp> |
| 3 | #include <boost/range/adaptor/indexed.hpp> |
| 4 | #include <gtest/gtest.h> |
| 5 | #include <rapidjson/document.h> |
| 6 | #include <rapidjson/writer.h> |
| 7 | #include <vector> |
| 8 | |
| 9 | #include "byte_stream.h" |
| 10 | #include "crypto/hash.h" |
| 11 | #include "cryptonote_basic/account.h" |
| 12 | #include "cryptonote_basic/cryptonote_basic.h" |
| 13 | #include "cryptonote_basic/cryptonote_format_utils.h" |
| 14 | #include "cryptonote_core/cryptonote_tx_utils.h" |
| 15 | #include "serialization/json_object.h" |
| 16 | #include "rpc/daemon_messages.h" |
| 17 | |
| 18 | |
| 19 | namespace test |
| 20 | { |
| 21 | cryptonote::transaction |
| 22 | make_miner_transaction(cryptonote::account_public_address const& to) |
| 23 | { |
| 24 | cryptonote::transaction tx{}; |
| 25 | if (!cryptonote::construct_miner_tx(0, 0, 5000, 500, 500, to, tx)) |
| 26 | throw std::runtime_error{"transaction construction error"}; |
| 27 | |
| 28 | crypto::hash id{0}; |
| 29 | if (!cryptonote::get_transaction_hash(tx, id)) |
| 30 | throw std::runtime_error{"could not get transaction hash"}; |
| 31 | |
| 32 | return tx; |
| 33 | } |
| 34 | |
| 35 | cryptonote::transaction |
| 36 | make_transaction( |
| 37 | cryptonote::account_keys const& from, |
| 38 | std::vector<cryptonote::transaction> const& sources, |
| 39 | std::vector<cryptonote::account_public_address> const& destinations, |
| 40 | bool rct, |
| 41 | bool bulletproof) |
| 42 | { |
| 43 | std::uint64_t source_amount = 0; |
| 44 | std::vector<cryptonote::tx_source_entry> actual_sources; |
| 45 | for (auto const& source : sources) |
| 46 | { |
| 47 | std::vector<cryptonote::tx_extra_field> extra_fields; |
| 48 | if (!cryptonote::parse_tx_extra(source.extra, extra_fields)) |
| 49 | throw std::runtime_error{"invalid transaction"}; |
| 50 | |
| 51 | cryptonote::tx_extra_pub_key key_field{}; |
| 52 | if (!cryptonote::find_tx_extra_field_by_type(extra_fields, key_field)) |
| 53 | throw std::runtime_error{"invalid transaction"}; |
| 54 | |
| 55 | for (auto const input : boost::adaptors::index(source.vout)) |
| 56 | { |
| 57 | source_amount += input.value().amount; |
| 58 | auto const& key = boost::get<cryptonote::txout_to_key>(input.value().target); |
| 59 | |
| 60 | actual_sources.push_back( |
| 61 | {{}, 0, key_field.pub_key, {}, std::size_t(input.index()), input.value().amount, rct, rct::identity()} |
| 62 | ); |
| 63 | |
| 64 | for (unsigned ring = 0; ring < 10; ++ring) |
| 65 | actual_sources.back().push_output(input.index(), key.key, input.value().amount); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | std::vector<cryptonote::tx_destination_entry> to; |
| 70 | for (auto const& destination : destinations) |
| 71 | to.push_back({(source_amount / destinations.size()), destination, false}); |
| 72 | |
| 73 | cryptonote::transaction tx{}; |
| 74 | |
| 75 | crypto::secret_key tx_key{}; |
| 76 | std::vector<crypto::secret_key> extra_keys{}; |
| 77 | |
| 78 | std::unordered_map<crypto::public_key, cryptonote::subaddress_index> subaddresses; |
| 79 | subaddresses[from.m_account_address.m_spend_public_key] = {0,0}; |
| 80 | |
| 81 | if (!cryptonote::construct_tx_and_get_tx_key(from, subaddresses, actual_sources, to, boost::none, {}, tx, tx_key, extra_keys, rct, { bulletproof ? rct::RangeProofBulletproof : rct::RangeProofBorromean, bulletproof ? 2 : 0 })) |
| 82 | throw std::runtime_error{"transaction construction error"}; |
| 83 | |
| 84 | return tx; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | namespace |
| 89 | { |
| 90 | template<typename T> |
| 91 | T test_json(const T& value) |
| 92 | { |
| 93 | epee::byte_stream buffer; |
| 94 | { |
| 95 | rapidjson::Writer<epee::byte_stream> dest{buffer}; |
| 96 | cryptonote::json::toJsonValue(dest, value); |
| 97 | } |
| 98 | |
| 99 | rapidjson::Document doc; |
| 100 | doc.Parse(reinterpret_cast<const char*>(buffer.data()), buffer.size()); |
| 101 | if (doc.HasParseError()) |
| 102 | { |
| 103 | throw cryptonote::json::PARSE_FAIL(); |
| 104 | } |
| 105 | |
| 106 | T out{}; |
| 107 | cryptonote::json::fromJsonValue(doc, out); |
| 108 | return out; |
| 109 | } |
| 110 | } // anonymous |
| 111 | |
| 112 | TEST(JsonSerialization, VectorBytes) |
| 113 | { |
| 114 | EXPECT_EQ(std::vector<std::uint8_t>{}, test_json(std::vector<std::uint8_t>{})); |
| 115 | EXPECT_EQ(std::vector<std::uint8_t>{0x00}, test_json(std::vector<std::uint8_t>{0x00})); |
| 116 | } |
| 117 | |
| 118 | TEST(JsonSerialization, InvalidVectorBytes) |
| 119 | { |
| 120 | rapidjson::Document doc; |
| 121 | doc.SetString("1"); |
| 122 | |
| 123 | std::vector<std::uint8_t> out; |
| 124 | EXPECT_THROW(cryptonote::json::fromJsonValue(doc, out), cryptonote::json::BAD_INPUT); |
| 125 | } |
| 126 | |
| 127 | TEST(JsonSerialization, DaemonInfo) |
| 128 | { |
| 129 | cryptonote::rpc::DaemonInfo info{}; |
| 130 | info.height = 154544; |
| 131 | info.target_height = 15345435; |
| 132 | info.top_block_height = 2344; |
| 133 | info.wide_difficulty = cryptonote::difficulty_type{"100000000000000000005443"}; |
| 134 | info.difficulty = 200376420520695107; |
| 135 | info.target = 7657567; |
| 136 | info.tx_count = 355; |
| 137 | info.tx_pool_size = 45435; |
| 138 | info.alt_blocks_count = 43535; |
| 139 | info.outgoing_connections_count = 1444; |
| 140 | info.incoming_connections_count = 1444; |
| 141 | info.white_peerlist_size = 14550; |
| 142 | info.grey_peerlist_size = 34324; |
| 143 | info.mainnet = true; |
| 144 | info.testnet = true; |
| 145 | info.stagenet = true; |
| 146 | info.nettype = "main"; |
| 147 | info.top_block_hash = crypto::hash{1}; |
| 148 | info.wide_cumulative_difficulty = cryptonote::difficulty_type{"200000000000000000005543"}; |
| 149 | info.cumulative_difficulty = 400752841041384871; |
| 150 | info.block_size_limit = 4324234; |
| 151 | info.block_weight_limit = 3434; |
| 152 | info.block_size_median = 3434; |
| 153 | info.adjusted_time = 4535; |
| 154 | info.block_weight_median = 43535; |
| 155 | info.start_time = 34535; |
| 156 | info.version = "1.0"; |
| 157 | |
| 158 | const auto info_copy = test_json(info); |
| 159 | |
| 160 | EXPECT_EQ(info.height, info_copy.height); |
| 161 | EXPECT_EQ(info.target_height, info_copy.target_height); |
| 162 | EXPECT_EQ(info.top_block_height, info_copy.top_block_height); |
| 163 | EXPECT_EQ(info.wide_difficulty, info_copy.wide_difficulty); |
| 164 | EXPECT_EQ(info.difficulty, info_copy.difficulty); |
| 165 | EXPECT_EQ(info.target, info_copy.target); |
| 166 | EXPECT_EQ(info.tx_count, info_copy.tx_count); |
| 167 | EXPECT_EQ(info.tx_pool_size, info_copy.tx_pool_size); |
| 168 | EXPECT_EQ(info.alt_blocks_count, info_copy.alt_blocks_count); |
| 169 | EXPECT_EQ(info.outgoing_connections_count, info_copy.outgoing_connections_count); |
| 170 | EXPECT_EQ(info.incoming_connections_count, info_copy.incoming_connections_count); |
| 171 | EXPECT_EQ(info.white_peerlist_size, info_copy.white_peerlist_size); |
| 172 | EXPECT_EQ(info.grey_peerlist_size, info_copy.grey_peerlist_size); |
| 173 | EXPECT_EQ(info.mainnet, info_copy.mainnet); |
| 174 | EXPECT_EQ(info.testnet, info_copy.testnet); |
| 175 | EXPECT_EQ(info.stagenet, info_copy.stagenet); |
| 176 | EXPECT_EQ(info.nettype, info_copy.nettype); |
| 177 | EXPECT_EQ(info.top_block_hash, info_copy.top_block_hash); |
| 178 | EXPECT_EQ(info.wide_cumulative_difficulty, info_copy.wide_cumulative_difficulty); |
| 179 | EXPECT_EQ(info.cumulative_difficulty, info_copy.cumulative_difficulty); |
| 180 | EXPECT_EQ(info.block_size_limit, info_copy.block_size_limit); |
| 181 | EXPECT_EQ(info.block_weight_limit, info_copy.block_weight_limit); |
| 182 | EXPECT_EQ(info.block_size_median, info_copy.block_size_median); |
| 183 | EXPECT_EQ(info.adjusted_time, info_copy.adjusted_time); |
| 184 | EXPECT_EQ(info.block_weight_median, info_copy.block_weight_median); |
| 185 | EXPECT_EQ(info.start_time, info_copy.start_time); |
| 186 | EXPECT_EQ(info.version, info_copy.version); |
| 187 | } |
| 188 | |
| 189 | TEST(JsonSerialization, MinerTransaction) |
| 190 | { |
| 191 | cryptonote::account_base acct; |
| 192 | acct.generate(); |
| 193 | const auto miner_tx = test::make_miner_transaction(acct.get_keys().m_account_address); |
| 194 | |
| 195 | crypto::hash tx_hash{}; |
| 196 | ASSERT_TRUE(cryptonote::get_transaction_hash(miner_tx, tx_hash)); |
| 197 | |
| 198 | cryptonote::transaction miner_tx_copy = test_json(miner_tx); |
| 199 | |
| 200 | crypto::hash tx_copy_hash{}; |
| 201 | ASSERT_TRUE(cryptonote::get_transaction_hash(miner_tx_copy, tx_copy_hash)); |
| 202 | EXPECT_EQ(tx_hash, tx_copy_hash); |
| 203 | |
| 204 | cryptonote::blobdata tx_bytes{}; |
| 205 | cryptonote::blobdata tx_copy_bytes{}; |
| 206 | |
| 207 | ASSERT_TRUE(cryptonote::t_serializable_object_to_blob(miner_tx, tx_bytes)); |
| 208 | ASSERT_TRUE(cryptonote::t_serializable_object_to_blob(miner_tx_copy, tx_copy_bytes)); |
| 209 | |
| 210 | EXPECT_EQ(tx_bytes, tx_copy_bytes); |
| 211 | } |
| 212 | |
| 213 | TEST(JsonSerialization, RegularTransaction) |
| 214 | { |
| 215 | cryptonote::account_base acct1; |
| 216 | acct1.generate(); |
| 217 | |
| 218 | cryptonote::account_base acct2; |
| 219 | acct2.generate(); |
| 220 | |
| 221 | const auto miner_tx = test::make_miner_transaction(acct1.get_keys().m_account_address); |
| 222 | const auto tx = test::make_transaction( |
| 223 | acct1.get_keys(), {miner_tx}, {acct2.get_keys().m_account_address}, false, false |
| 224 | ); |
| 225 | |
| 226 | crypto::hash tx_hash{}; |
| 227 | ASSERT_TRUE(cryptonote::get_transaction_hash(tx, tx_hash)); |
| 228 | |
| 229 | cryptonote::transaction tx_copy = test_json(tx); |
| 230 | |
| 231 | crypto::hash tx_copy_hash{}; |
| 232 | ASSERT_TRUE(cryptonote::get_transaction_hash(tx_copy, tx_copy_hash)); |
| 233 | EXPECT_EQ(tx_hash, tx_copy_hash); |
| 234 | |
| 235 | cryptonote::blobdata tx_bytes{}; |
| 236 | cryptonote::blobdata tx_copy_bytes{}; |
| 237 | |
| 238 | ASSERT_TRUE(cryptonote::t_serializable_object_to_blob(tx, tx_bytes)); |
| 239 | ASSERT_TRUE(cryptonote::t_serializable_object_to_blob(tx_copy, tx_copy_bytes)); |
| 240 | |
| 241 | EXPECT_EQ(tx_bytes, tx_copy_bytes); |
| 242 | } |
| 243 | |
| 244 | TEST(JsonSerialization, RingctTransaction) |
| 245 | { |
| 246 | cryptonote::account_base acct1; |
| 247 | acct1.generate(); |
| 248 | |
| 249 | cryptonote::account_base acct2; |
| 250 | acct2.generate(); |
| 251 | |
| 252 | const auto miner_tx = test::make_miner_transaction(acct1.get_keys().m_account_address); |
| 253 | const auto tx = test::make_transaction( |
| 254 | acct1.get_keys(), {miner_tx}, {acct2.get_keys().m_account_address}, true, false |
| 255 | ); |
| 256 | |
| 257 | crypto::hash tx_hash{}; |
| 258 | ASSERT_TRUE(cryptonote::get_transaction_hash(tx, tx_hash)); |
| 259 | |
| 260 | cryptonote::transaction tx_copy = test_json(tx); |
| 261 | |
| 262 | crypto::hash tx_copy_hash{}; |
| 263 | ASSERT_TRUE(cryptonote::get_transaction_hash(tx_copy, tx_copy_hash)); |
| 264 | EXPECT_EQ(tx_hash, tx_copy_hash); |
| 265 | |
| 266 | cryptonote::blobdata tx_bytes{}; |
| 267 | cryptonote::blobdata tx_copy_bytes{}; |
| 268 | |
| 269 | ASSERT_TRUE(cryptonote::t_serializable_object_to_blob(tx, tx_bytes)); |
| 270 | ASSERT_TRUE(cryptonote::t_serializable_object_to_blob(tx_copy, tx_copy_bytes)); |
| 271 | |
| 272 | EXPECT_EQ(tx_bytes, tx_copy_bytes); |
| 273 | } |
| 274 | |
| 275 | TEST(JsonSerialization, BulletproofTransaction) |
| 276 | { |
| 277 | cryptonote::account_base acct1; |
| 278 | acct1.generate(); |
| 279 | |
| 280 | cryptonote::account_base acct2; |
| 281 | acct2.generate(); |
| 282 | |
| 283 | const auto miner_tx = test::make_miner_transaction(acct1.get_keys().m_account_address); |
| 284 | const auto tx = test::make_transaction( |
| 285 | acct1.get_keys(), {miner_tx}, {acct2.get_keys().m_account_address}, true, true |
| 286 | ); |
| 287 | |
| 288 | crypto::hash tx_hash{}; |
| 289 | ASSERT_TRUE(cryptonote::get_transaction_hash(tx, tx_hash)); |
| 290 | |
| 291 | cryptonote::transaction tx_copy = test_json(tx); |
| 292 | |
| 293 | crypto::hash tx_copy_hash{}; |
| 294 | ASSERT_TRUE(cryptonote::get_transaction_hash(tx_copy, tx_copy_hash)); |
| 295 | EXPECT_EQ(tx_hash, tx_copy_hash); |
| 296 | |
| 297 | cryptonote::blobdata tx_bytes{}; |
| 298 | cryptonote::blobdata tx_copy_bytes{}; |
| 299 | |
| 300 | ASSERT_TRUE(cryptonote::t_serializable_object_to_blob(tx, tx_bytes)); |
| 301 | ASSERT_TRUE(cryptonote::t_serializable_object_to_blob(tx_copy, tx_copy_bytes)); |
| 302 | |
| 303 | EXPECT_EQ(tx_bytes, tx_copy_bytes); |
| 304 | } |
| 305 | |
| 306 | TEST(JsonRpcSerialization, HandlerFromJson) |
| 307 | { |
| 308 | cryptonote::rpc::FullMessage req_full("{\"jsonrpc\":\"2.0\",\"method\":\"get_hashes_fast\",\"params\":[1]}", true); |
| 309 | cryptonote::rpc::GetHashesFast::Request request{}; |
| 310 | EXPECT_THROW(request.fromJson(req_full.getMessage()), cryptonote::json::WRONG_TYPE); |
| 311 | } |