Skip to content

Commit

Permalink
rta-tx: check if supernode is valid
Browse files Browse the repository at this point in the history
  • Loading branch information
mbg033 committed Mar 11, 2019
1 parent 3dc55ad commit 9941710
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 16 deletions.
5 changes: 4 additions & 1 deletion src/cryptonote_basic/cryptonote_basic.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,16 +215,19 @@ namespace cryptonote
static constexpr size_t POS_KEY_INDEX = 0;
static constexpr size_t POS_PROXY_KEY_INDEX = 1;
static constexpr size_t WALLET_PROXY_KEY_INDEX = 2;
uint64_t auth_sample_height = 0; // block height for auth sample generation

std::vector<crypto::public_key> keys;
BEGIN_SERIALIZE_OBJECT()
FIELD(payment_id)
FIELD(auth_sample_height)
FIELD(keys)
END_SERIALIZE()
bool operator== (const rta_header &other) const
{
return this->payment_id == other.payment_id
&& this->keys == other.keys;
&& this->keys == other.keys
&& this->auth_sample_height == other.auth_sample_height;
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/cryptonote_core/cryptonote_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ namespace cryptonote
blocks_per_sync, sync_mode, fast_sync);

r = m_blockchain_storage.init(db, m_testnet, test_options);

m_mempool.set_stake_transaction_processor(&m_graft_stake_transaction_processor);
r = m_mempool.init();
CHECK_AND_ASSERT_MES(r, false, "Failed to initialize memory pool");

Expand Down
26 changes: 18 additions & 8 deletions src/cryptonote_core/tx_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "common/perf_timer.h"
#include "crypto/hash.h"
#include "stake_transaction_processor.h"
#include "graft_rta_config.h"

#undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "txpool"
Expand Down Expand Up @@ -1073,10 +1074,16 @@ namespace cryptonote
{
bool result = true;

if (rta_hdr.keys.size() == 0) {
MERROR("Failed to validate rta tx, missing auth sample keys for tx: " << txid );
return false;
}
#if 0 // don't validate signatures for rta mining
if (rta_hdr.keys.size() != rta_signs.size()) {
MERROR("Failed to validate rta tx: " << txid << ", keys.size() != signatures.size()");
return false;
}

for (const auto &rta_sign : rta_signs) {
// check if key index is in range
if (rta_sign.key_index >= rta_hdr.keys.size()) {
Expand All @@ -1085,25 +1092,28 @@ namespace cryptonote
break;
}

result &= validate_supernode(m_blockchain.get_current_blockchain_height(), rta_hdr.keys[rta_sign.key_index]);
if (!result) {
MERROR("Failed to validate rta tx: " << epee::string_tools::pod_to_hex(txid) << ", key: " << rta_hdr.keys[rta_sign.key_index] << " doesn't belong to a valid supernode");
break;
}

result &= crypto::check_signature(txid, rta_hdr.keys[rta_sign.key_index], rta_sign.signature);
if (!result) {
MERROR("Failed to validate rta tx signature: " << epee::string_tools::pod_to_hex(txid) << " for key: " << rta_hdr.keys[rta_sign.key_index]);
break;
}
}
#endif
for (const crypto::public_key &key : rta_hdr.keys) {
result &= validate_supernode(rta_hdr.auth_sample_height, key);
if (!result) {
MERROR("Failed to validate rta tx: " << epee::string_tools::pod_to_hex(txid) << ", key: " << key << " doesn't belong to a valid supernode");
break;
}
}

return result;
}

bool tx_memory_pool::validate_supernode(uint64_t height, const public_key &id) const
{
// TODO: add pointer/reference to the StakeTransactionProcessor to this class and
// check if key belongs to a valid supernode
return true;
supernode_stake * stake = const_cast<supernode_stake*>(m_stp->find_supernode_stake(height, epee::string_tools::pod_to_hex(id)));
return stake ? stake->amount >= config::graft::TIER1_STAKE_AMOUNT : false;
};
}
8 changes: 7 additions & 1 deletion src/cryptonote_core/tx_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,11 @@ namespace cryptonote
size_t validate(uint8_t version);


void set_stake_transaction_processor(StakeTransactionProcessor * arg)
{
m_stp = arg;
}

#define CURRENT_MEMPOOL_ARCHIVE_VER 11
#define CURRENT_MEMPOOL_TX_DETAILS_ARCHIVE_VER 12

Expand Down Expand Up @@ -460,7 +465,7 @@ namespace cryptonote

bool validate_rta_tx(const crypto::hash &txid, const std::vector<cryptonote::rta_signature> &rta_signs, const cryptonote::rta_header &rta_hdr) const;

bool validate_supernode(uint64_t heigh, const crypto::public_key &id) const;
bool validate_supernode(uint64_t height, const crypto::public_key &id) const;

//TODO: confirm the below comments and investigate whether or not this
// is the desired behavior
Expand Down Expand Up @@ -508,6 +513,7 @@ namespace cryptonote
std::unordered_set<crypto::hash> m_timed_out_transactions;

Blockchain& m_blockchain; //!< reference to the Blockchain object
StakeTransactionProcessor * m_stp = nullptr;
};
}

Expand Down
14 changes: 11 additions & 3 deletions src/wallet/wallet_rpc_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,8 @@ namespace tools

cryptonote::rta_header rta_header;
rta_header.payment_id = req.graft_payment_id;
rta_header.auth_sample_height = req.auth_sample_height;

for (const std::string &key_str : req.supernode_keys) {
crypto::public_key key;
if (!epee::string_tools::hex_to_pod(key_str, key)) {
Expand All @@ -583,6 +585,9 @@ namespace tools
return false;
}

if (!req.do_not_relay)
m_wallet->commit_tx(ptx_vector);

// populate response with tx hash
res.tx_hash = epee::string_tools::pod_to_hex(cryptonote::get_transaction_hash(ptx_vector.back().tx));
if (req.get_tx_key)
Expand All @@ -591,9 +596,12 @@ namespace tools
}
res.fee = ptx_vector.back().fee;

cryptonote::blobdata blob;
tx_to_blob(ptx_vector.back().tx, blob);
res.tx_blob = epee::string_tools::buff_to_hex_nodelimer(blob);
if (req.get_tx_hex)
{
cryptonote::blobdata blob;
tx_to_blob(ptx_vector.back().tx, blob);
res.tx_blob = epee::string_tools::buff_to_hex_nodelimer(blob);
}

// return encrypted tx key
std::string encrypted_key_blob;
Expand Down
3 changes: 2 additions & 1 deletion src/wallet/wallet_rpc_server_commands_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ namespace wallet_rpc
{
std::list<std::string> supernode_keys;
std::string graft_payment_id;

uint64_t auth_sample_height;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(destinations)
KV_SERIALIZE(priority)
Expand All @@ -220,6 +220,7 @@ namespace wallet_rpc
KV_SERIALIZE(get_tx_key)
KV_SERIALIZE(supernode_keys)
KV_SERIALIZE(graft_payment_id)
KV_SERIALIZE(auth_sample_height)
KV_SERIALIZE_OPT(do_not_relay, false)
KV_SERIALIZE_OPT(get_tx_hex, false)
END_KV_SERIALIZE_MAP()
Expand Down
6 changes: 5 additions & 1 deletion tests/supernode_tests/graft_wallet_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ TEST_F(RtaRpcTest, TransfersRtaTransaction)
req.id = epee::serialization::storage_entry(0);

req.params.graft_payment_id = "test-test-test";
req.params.auth_sample_height = 243285;
req.params.priority = 0;
req.params.mixin = 7;
req.params.get_tx_key = 1;
Expand Down Expand Up @@ -436,8 +437,9 @@ TEST_F(RtaRpcTest, TransfersRtaTransaction)

bool r = net_utils::invoke_http_json("/json_rpc", req, res, http_client_wallet);
ASSERT_TRUE(r);
ASSERT_FALSE(res.result.tx_hash.empty());


#if 0
cryptonote::transaction tx;
std::string tx_blob;
ASSERT_TRUE(string_tools::parse_hexstr_to_binbuff(res.result.tx_blob, tx_blob));
Expand Down Expand Up @@ -487,4 +489,6 @@ TEST_F(RtaRpcTest, TransfersRtaTransaction)
r = net_utils::invoke_http_json("/sendrawtransaction", tx_req, tx_resp, http_client_daemon);
ASSERT_TRUE(r);
ASSERT_FALSE(tx_resp.rta_validation_failed);
#endif

}

0 comments on commit 9941710

Please sign in to comment.