RealInput::identify fixed

new_rpc
moneroexamples 5 years ago
parent b7827081ca
commit e456c9f7cb

@ -153,9 +153,12 @@ public:
virtual void
get_output_tx_and_index(
const uint64_t& amount,
const std::vector<uint64_t> &offsets,
std::vector<tx_out_index> &indices) const
const std::vector<uint64_t>& offsets,
std::vector<tx_out_index>& indices) const
{
// tx_hash , index in tx
// tx_out_index is std::pair<crypto::hash, uint64_t>;
core_storage.get_db().get_output_tx_and_index(
amount, offsets, indices);
}

@ -337,7 +337,7 @@ void RealInput::identify(transaction const& tx,
// reason to keep check remaning outputs in the mixin tx
// instead add its info to identified_inputs and move on
// to the next key image
if (!key_image_info)
if (key_image_info)
{
identified_inputs.push_back(*key_image_info);
total_xmr += key_image_info->amount;

@ -125,6 +125,8 @@ public:
key_image key_img;
uint64_t amount;
public_key out_pub_key;
friend std::ostream& operator<<(std::ostream& os, info const& _info);
};
@ -264,12 +266,21 @@ public:
(void) b;
}
// overload to get value from tuple by type
template <typename U>
U* const get()
auto* const get()
{
return std::get<unique_ptr<U>>(identifiers).get();
}
// overload to get value from tuple by number
template <size_t No>
auto* const get()
{
return std::get<No>(identifiers).get();
}
inline auto get_tx_pub_key() const {return tx_pub_key;}
private:
@ -309,5 +320,13 @@ operator<<(std::ostream& os, xmreg::Output::info const& _info)
<< _info.amount;
}
inline std::ostream&
operator<<(std::ostream& os, xmreg::Input::info const& _info)
{
return os << pod_to_hex(_info.key_img) << ", "
<< pod_to_hex(_info.out_pub_key) << ", "
<< _info.amount;
}
}

@ -18,6 +18,61 @@ JsonTx::JsonTx(string _path): jpath {std::move(_path)}
init();
}
void
JsonTx::get_output_tx_and_index(
uint64_t const& amount,
vector<uint64_t> const& offsets,
vector<tx_out_index>& indices) const
{
for (auto const& jinput: jtx["inputs"])
{
if (jinput["amount"] != amount
&& jinput["absolute_offsets"] != offsets)
continue;
for (auto const& jring_member: jinput["ring_members"])
{
crypto::hash tx_hash;
if (!hex_to_pod(jring_member["tx_hash"], tx_hash))
throw std::runtime_error(
"hex_to_pod(jring_member[\"tx_hash\"], tx_hash)");
indices.emplace_back(tx_hash, jring_member["output_index_in_tx"]);
}
}
}
bool
JsonTx::get_tx(crypto::hash const& tx_hash,
transaction& tx) const
{
for (auto const& jinput: jtx["inputs"])
{
for (auto const& jring_member: jinput["ring_members"])
{
if (jring_member["tx_hash"] != pod_to_hex(tx_hash))
continue;
crypto::hash tx_hash_tmp; \
crypto::hash tx_prefix_hash_tmp; \
if (!xmreg::hex_to_tx(jring_member["tx_hex"],
tx, tx_hash_tmp, tx_prefix_hash_tmp))
throw std::runtime_error(
"xmreg::hex_to_tx(jring_member[\"tx_hash\"]");
(void) tx_hash_tmp;
(void) tx_prefix_hash_tmp;
return true;
}
}
return false;
}
void
JsonTx::init()
{

@ -66,6 +66,23 @@ public:
explicit JsonTx(json _jtx);
explicit JsonTx(string _path);
// generate output which normaly is produced by
// monero's get_output_tx_and_index and blockchain,
// but here we use data from the tx json data file
// need this for mocking blockchain calls in unit tests
void
get_output_tx_and_index(
uint64_t const& amount,
vector<uint64_t> const& offsets,
vector<tx_out_index>& indices) const;
// will use data from the json file
// to set tx. Used for mocking this operation
// that is normaly done using blockchain
bool
get_tx(crypto::hash const& tx_hash,
transaction& tx) const;
private:
void init();
bool read_config();

@ -69,6 +69,11 @@ public:
tx_out_index(uint64_t const& amount,
uint64_t const& index));
MOCK_CONST_METHOD3(get_output_tx_and_index,
void(const uint64_t& amount,
const std::vector<uint64_t> &offsets,
std::vector<tx_out_index> &indices));
MOCK_CONST_METHOD2(get_tx,
bool(crypto::hash const& tx_hash,
transaction& tx));

@ -61,12 +61,12 @@ TEST(MODULAR_IDENTIFIER, OutputsRingCT)
identifier.identify();
ASSERT_EQ(identifier.get<Output>()->get().size(),
ASSERT_EQ(identifier.get<0>()->get().size(),
jtx->sender.outputs.size());
ASSERT_TRUE(identifier.get<Output>()->get() == jtx->sender.outputs);
ASSERT_TRUE(identifier.get<0>()->get() == jtx->sender.outputs);
ASSERT_EQ(identifier.get<Output>()->get_total(),
ASSERT_EQ(identifier.get<0>()->get_total(),
jtx->sender.change);
}
@ -83,10 +83,10 @@ TEST(MODULAR_IDENTIFIER, OutputsRingCTCoinbaseTx)
identifier.identify();
ASSERT_TRUE(identifier.get<Output>()->get()
ASSERT_TRUE(identifier.get<0>()->get()
== jtx->recipients.at(0).outputs);
ASSERT_EQ(identifier.get<Output>()->get_total(),
ASSERT_EQ(identifier.get<0>()->get_total(),
jtx->recipients.at(0).amount);
}
@ -104,7 +104,7 @@ TEST(MODULAR_IDENTIFIER, MultiOutputsRingCT)
identifier.identify();
EXPECT_TRUE(identifier.get<Output>()->get()
EXPECT_TRUE(identifier.get<0>()->get()
== jrecipient.outputs);
}
}
@ -121,9 +121,57 @@ TEST(MODULAR_IDENTIFIER, LegacyPaymentID)
identifier.identify();
EXPECT_TRUE(identifier.get<LegacyPaymentID>()->get()
EXPECT_TRUE(identifier.get<0>()->get()
== jtx->payment_id);
}
TEST(MODULAR_IDENTIFIER, IntegratedPaymentID)
{
auto jtx = construct_jsontx("ddff95211b53c194a16c2b8f37ae44b643b8bd46b4cb402af961ecabeb8417b2");
ASSERT_TRUE(jtx);
auto identifier = make_identifier(jtx->tx,
make_unique<IntegratedPaymentID>(
&jtx->recipients[0].address,
&jtx->recipients[0].viewkey));
identifier.identify();
EXPECT_TRUE(identifier.get<0>()->get()
== jtx->payment_id8e);
}
TEST(MODULAR_IDENTIFIER, RealInputRingCT)
{
auto jtx = construct_jsontx("d7dcb2daa64b5718dad71778112d48ad62f4d5f54337037c420cb76efdd8a21c");
ASSERT_TRUE(jtx);
MockMicroCore mcore;
EXPECT_CALL(mcore, get_output_tx_and_index(_, _, _))
.WillRepeatedly(
Invoke(&*jtx, &JsonTx::get_output_tx_and_index));
EXPECT_CALL(mcore, get_tx(_, _))
.WillRepeatedly(
Invoke(&*jtx, &JsonTx::get_tx));
auto identifier = make_identifier(jtx->tx,
make_unique<RealInput>(
&jtx->sender.address,
&jtx->sender.viewkey,
&jtx->sender.spendkey,
&mcore));
identifier.identify();
for (auto const& input_info: identifier.get<0>()->get())
cout << input_info << endl;
EXPECT_TRUE(identifier.get<0>()->get().size() == 2);
}
//// private testnet wallet 9wq792k9sxVZiLn66S3Qzv8QfmtcwkdXgM5cWGsXAPxoQeMQ79md51PLPCijvzk1iHbuHi91pws5B7iajTX9KTtJ4bh2tCh

Loading…
Cancel
Save