returning correct amount in for rct txs in tests

This commit is contained in:
Maxim Shishmarev 2018-08-21 19:06:38 +10:00
parent 0b07666dde
commit 857aa8e7b6
2 changed files with 63 additions and 53 deletions

View File

@ -298,7 +298,7 @@ struct output_index {
typedef std::map<uint64_t, std::vector<size_t> > map_output_t;
typedef std::map<uint64_t, std::vector<output_index> > map_output_idx_t;
typedef std::vector<output_index> output_idex_vec;
typedef std::vector<output_index> output_index_vec;
typedef std::vector<size_t> output_vec;
typedef pair<uint64_t, size_t> outloc_t;
@ -317,7 +317,58 @@ namespace
}
}
bool init_output_indices(output_idex_vec& outs, output_vec& outs_mine, const std::vector<cryptonote::block>& blockchain, const map_hash2tx_t& mtx, const cryptonote::account_base& from) {
uint64_t get_amount(const cryptonote::account_base& account, const cryptonote::transaction& tx, rct::key& mask, int i)
{
crypto::public_key tx_pub_key = get_tx_pub_key_from_extra(tx);
crypto::key_derivation derivation;
if (!crypto::generate_key_derivation(tx_pub_key, account.get_keys().m_view_secret_key, derivation))
return 0;
if (tx.vout[i].target.type() != typeid(cryptonote::txout_to_key))
return 0;
hw::device& hwdev = hw::get_device("default");
uint64_t money_transferred = 0;
crypto::secret_key scalar1;
hwdev.derivation_to_scalar(derivation, i, scalar1);
try
{
switch (tx.rct_signatures.type)
{
case rct::RCTTypeSimple:
case rct::RCTTypeSimpleBulletproof:
money_transferred = rct::decodeRctSimple(tx.rct_signatures, rct::sk2rct(scalar1), i, mask, hwdev);
break;
case rct::RCTTypeFull:
case rct::RCTTypeFullBulletproof:
money_transferred = rct::decodeRct(tx.rct_signatures, rct::sk2rct(scalar1), i, hwdev);
break;
case rct::RCTTypeNull:
money_transferred = tx.vout[i].amount;
break;
default:
LOG_PRINT_L0("Unsupported rct type: " << tx.rct_signatures.type);
return 0;
}
}
catch (const std::exception &e)
{
LOG_PRINT_L0("Failed to decode input " << i);
return 0;
}
return money_transferred;
}
uint64_t get_amount(const cryptonote::account_base& account, const cryptonote::transaction& tx, int i)
{
rct::key mask_unused;
return get_amount(account, tx, mask_unused, i);
}
bool init_output_indices(output_index_vec& outs, output_vec& outs_mine, const std::vector<cryptonote::block>& blockchain, const map_hash2tx_t& mtx, const cryptonote::account_base& from) {
for (const block& blk : blockchain) {
vector<const transaction*> vtx;
@ -347,6 +398,10 @@ bool init_output_indices(output_idex_vec& outs, output_vec& outs_mine, const std
// Is out to me?
if (is_out_to_acc(from.get_keys(), boost::get<txout_to_key>(out.target), get_tx_pub_key_from_extra(tx), get_additional_tx_pub_keys_from_extra(tx), j)) {
outs_mine.push_back(tx_global_idx);
auto& out = outs.back();
if (out.amount == 0) {
out.amount = get_amount(from, tx, j);
}
}
}
}
@ -356,7 +411,7 @@ bool init_output_indices(output_idex_vec& outs, output_vec& outs_mine, const std
return true;
}
bool init_spent_output_indices(output_idex_vec& outs,
bool init_spent_output_indices(output_index_vec& outs,
const output_vec& outs_mine,
const std::vector<cryptonote::block>& blockchain,
const map_hash2tx_t& mtx,
@ -435,56 +490,10 @@ static bool fill_output_entries(const std::vector<output_index>& out_indices, si
return 0 == rest && sender_out_found;
}
static uint64_t get_amount(const cryptonote::account_base& account, const cryptonote::transaction& tx, int i, rct::key& mask)
{
crypto::public_key tx_pub_key = get_tx_pub_key_from_extra(tx);
crypto::key_derivation derivation;
if (!crypto::generate_key_derivation(tx_pub_key, account.get_keys().m_view_secret_key, derivation))
return 0;
if (tx.vout[i].target.type() != typeid(cryptonote::txout_to_key))
return 0;
hw::device& hwdev = hw::get_device("default");
uint64_t money_transferred = 0;
crypto::secret_key scalar1;
hwdev.derivation_to_scalar(derivation, i, scalar1);
try
{
switch (tx.rct_signatures.type)
{
case rct::RCTTypeSimple:
case rct::RCTTypeSimpleBulletproof:
money_transferred = rct::decodeRctSimple(tx.rct_signatures, rct::sk2rct(scalar1), i, mask, hwdev);
break;
case rct::RCTTypeFull:
case rct::RCTTypeFullBulletproof:
money_transferred = rct::decodeRct(tx.rct_signatures, rct::sk2rct(scalar1), i, mask, hwdev);
break;
case rct::RCTTypeNull:
money_transferred = tx.vout[i].amount;
mask = rct::identity();
break;
default:
LOG_PRINT_L0("Unsupported rct type: " << tx.rct_signatures.type);
return 0;
}
}
catch (const std::exception &e)
{
LOG_PRINT_L0("Failed to decode input " << i);
return 0;
}
return money_transferred;
}
bool fill_tx_sources(std::vector<tx_source_entry>& sources, const std::vector<test_event_entry>& events,
const block& blk_head, const cryptonote::account_base& from, uint64_t amount, size_t nmix)
{
output_idex_vec outs;
output_index_vec outs;
output_vec outs_mine;
std::vector<cryptonote::block> blockchain;
@ -514,7 +523,7 @@ bool fill_tx_sources(std::vector<tx_source_entry>& sources, const std::vector<te
ts.real_output_in_tx_index = oi.out_no;
ts.real_out_tx_key = get_tx_pub_key_from_extra(*oi.p_tx); // incoming tx public key
ts.real_out_additional_tx_keys = get_additional_tx_pub_keys_from_extra(*oi.p_tx);
ts.mask = rct::identity();;
ts.mask = rct::identity();
size_t realOutput;
if (!fill_output_entries(outs, sender_out, nmix, realOutput, ts.outputs)) continue;
@ -644,7 +653,7 @@ transaction construct_tx_with_fee(std::vector<test_event_entry>& events, const b
uint64_t get_balance(const cryptonote::account_base& addr, const std::vector<cryptonote::block>& blockchain, const map_hash2tx_t& mtx) {
uint64_t res = 0;
output_idex_vec outs;
output_index_vec outs;
output_vec outs_mine;
map_hash2tx_t confirmed_txs;

View File

@ -631,7 +631,8 @@ inline bool do_replay_file(const std::string& filename)
VEC_EVENTS.push_back(t); \
}
#define MAKE_TX_LIST(VEC_EVENTS, SET_NAME, FROM, TO, AMOUNT, HEAD) MAKE_TX_MIX_LIST(VEC_EVENTS, SET_NAME, FROM, TO, AMOUNT, 0, HEAD)
#define MAKE_TX_LIST(VEC_EVENTS, SET_NAME, FROM, TO, AMOUNT, HEAD) MAKE_TX_MIX_LIST(VEC_EVENTS, SET_NAME, FROM, TO, AMOUNT, 9, HEAD)
#define MAKE_TX_LIST_START(VEC_EVENTS, SET_NAME, FROM, TO, AMOUNT, HEAD) \
std::list<cryptonote::transaction> SET_NAME; \