Skip to content

Commit

Permalink
core_tests: check service node registration and deregistration
Browse files Browse the repository at this point in the history
  • Loading branch information
msgmaxim committed Aug 30, 2018
1 parent 8da49e9 commit 5e3471a
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 31 deletions.
1 change: 1 addition & 0 deletions src/cryptonote_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#define STAKING_REQUIREMENT_LOCK_BLOCKS_EXCESS 20
#define STAKING_REQUIREMENT_LOCK_BLOCKS (30*24*30)
#define STAKING_REQUIREMENT_LOCK_BLOCKS_TESTNET (30*24*2)
#define STAKING_REQUIREMENT_LOCK_BLOCKS_FAKENET 30
#define STAKING_PORTIONS UINT64_C(0xfffffffffffffffc)
#define MAX_NUMBER_OF_CONTRIBUTORS 4
#define MIN_PORTIONS (STAKING_PORTIONS / MAX_NUMBER_OF_CONTRIBUTORS)
Expand Down
12 changes: 11 additions & 1 deletion src/cryptonote_core/service_node_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ namespace service_nodes

uint64_t service_node_list::get_staking_requirement_lock_blocks() const
{
return m_blockchain.nettype() == cryptonote::TESTNET || m_blockchain.nettype() == cryptonote::FAKECHAIN ? STAKING_REQUIREMENT_LOCK_BLOCKS_TESTNET : STAKING_REQUIREMENT_LOCK_BLOCKS;
return service_nodes::get_staking_requirement_lock_blocks(m_blockchain.nettype());
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1156,6 +1156,16 @@ namespace service_nodes
cmd = stream.str();
return true;
}

uint64_t get_staking_requirement_lock_blocks(cryptonote::network_type nettype)
{
switch(nettype) {
case cryptonote::TESTNET: return STAKING_REQUIREMENT_LOCK_BLOCKS_TESTNET;
case cryptonote::FAKECHAIN: return STAKING_REQUIREMENT_LOCK_BLOCKS_FAKENET;
default: return STAKING_REQUIREMENT_LOCK_BLOCKS;
}
}

uint64_t get_staking_requirement(cryptonote::network_type m_nettype, uint64_t height)
{
if (m_nettype == cryptonote::TESTNET || m_nettype == cryptonote::FAKECHAIN)
Expand Down
2 changes: 2 additions & 0 deletions src/cryptonote_core/service_node_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ namespace service_nodes
bool make_registration_cmd(cryptonote::network_type nettype, const std::vector<std::string> args, const crypto::public_key& service_node_pubkey,
const crypto::secret_key service_node_key, std::string &cmd, bool make_friendly);

uint64_t get_staking_requirement_lock_blocks(cryptonote::network_type m_nettype);

uint64_t get_staking_requirement(cryptonote::network_type nettype, uint64_t height);

uint64_t portions_to_amount(uint64_t portions, uint64_t staking_requirement);
Expand Down
2 changes: 1 addition & 1 deletion src/daemon/rpc_command_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2037,7 +2037,7 @@ static void print_service_node_list_state(cryptonote::network_type nettype, uint
// Print Expiry Info
{
uint64_t expiry_height = entry.registration_height;
expiry_height += (nettype == cryptonote::TESTNET) ? STAKING_REQUIREMENT_LOCK_BLOCKS_TESTNET : STAKING_REQUIREMENT_LOCK_BLOCKS;
expiry_height += get_staking_requirement_lock_blocks(nettype);

if (curr_height)
{
Expand Down
4 changes: 2 additions & 2 deletions src/simplewallet/simplewallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4740,7 +4740,7 @@ bool simple_wallet::register_service_node_main(
return true;
}

uint64_t staking_requirement_lock_blocks = (m_wallet->nettype() == cryptonote::TESTNET ? STAKING_REQUIREMENT_LOCK_BLOCKS_TESTNET : STAKING_REQUIREMENT_LOCK_BLOCKS);
uint64_t staking_requirement_lock_blocks = service_nodes::get_staking_requirement_lock_blocks(m_wallet->nettype());
uint64_t locked_blocks = staking_requirement_lock_blocks + STAKING_REQUIREMENT_LOCK_BLOCKS_EXCESS;

std::string err, err2;
Expand Down Expand Up @@ -5089,7 +5089,7 @@ bool simple_wallet::stake_main(
uint64_t fetched_blocks;
m_wallet->refresh(0, fetched_blocks);

uint64_t staking_requirement_lock_blocks = (m_wallet->nettype() == cryptonote::TESTNET ? STAKING_REQUIREMENT_LOCK_BLOCKS_TESTNET : STAKING_REQUIREMENT_LOCK_BLOCKS);
uint64_t staking_requirement_lock_blocks = get_staking_requirement_lock_blocks(m_wallet->nettype());
uint64_t locked_blocks = staking_requirement_lock_blocks + STAKING_REQUIREMENT_LOCK_BLOCKS_EXCESS;

std::string err, err2;
Expand Down
46 changes: 40 additions & 6 deletions tests/core_tests/chaingen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ void test_generator::add_block(const cryptonote::block& blk, size_t tsx_size, st

bool test_generator::construct_block(cryptonote::block& blk, uint64_t height, const crypto::hash& prev_id,
const cryptonote::account_base& miner_acc, uint64_t timestamp, uint64_t already_generated_coins,
std::vector<size_t>& block_sizes, const std::list<cryptonote::transaction>& tx_list)
std::vector<size_t>& block_sizes, const std::list<cryptonote::transaction>& tx_list,
const crypto::public_key& sn_pub_key /* = crypto::null_key */, const std::vector<sn_contributor_t>& sn_infos)
{
/// a temporary workaround
blk.major_version = hf_version_;
Expand Down Expand Up @@ -137,7 +138,8 @@ bool test_generator::construct_block(cryptonote::block& blk, uint64_t height, co
size_t target_block_size = txs_size + get_object_blobsize(blk.miner_tx);
while (true)
{
if (!construct_miner_tx(height, misc_utils::median(block_sizes), already_generated_coins, target_block_size, total_fee, miner_acc.get_keys().m_account_address, blk.miner_tx, blobdata(), hf_version_))

if (!construct_miner_tx(height, misc_utils::median(block_sizes), already_generated_coins, target_block_size, total_fee, miner_acc.get_keys().m_account_address, blk.miner_tx, blobdata(), hf_version_, cryptonote::MAINNET, sn_pub_key, sn_infos))
return false;

size_t actual_block_size = txs_size + get_object_blobsize(blk.miner_tx);
Expand Down Expand Up @@ -199,7 +201,8 @@ bool test_generator::construct_block(cryptonote::block& blk, const cryptonote::a

bool test_generator::construct_block(cryptonote::block& blk, const cryptonote::block& blk_prev,
const cryptonote::account_base& miner_acc,
const std::list<cryptonote::transaction>& tx_list/* = {}*/)
const std::list<cryptonote::transaction>& tx_list/* = {}*/,
const crypto::public_key& sn_pub_key /* = crypto::null_key */, const std::vector<sn_contributor_t>& sn_infos)
{
uint64_t height = boost::get<txin_gen>(blk_prev.miner_tx.vin.front()).height + 1;
crypto::hash prev_id = get_block_hash(blk_prev);
Expand All @@ -209,7 +212,7 @@ bool test_generator::construct_block(cryptonote::block& blk, const cryptonote::b
std::vector<size_t> block_sizes;
get_last_n_block_sizes(block_sizes, prev_id, CRYPTONOTE_REWARD_BLOCKS_WINDOW);

return construct_block(blk, height, prev_id, miner_acc, timestamp, already_generated_coins, block_sizes, tx_list);
return construct_block(blk, height, prev_id, miner_acc, timestamp, already_generated_coins, block_sizes, tx_list, sn_pub_key, sn_infos);
}

bool test_generator::construct_block_manually(block& blk, const block& prev_block, const account_base& miner_acc,
Expand Down Expand Up @@ -737,7 +740,7 @@ bool construct_tx_to_key(const std::vector<test_event_entry>& events,

bool construct_tx_to_key(const std::vector<test_event_entry>& events, cryptonote::transaction& tx, const block& blk_head,
const cryptonote::account_base& from, const cryptonote::account_base& to, uint64_t amount,
uint64_t fee, size_t nmix, bool stake, uint64_t unlock_time)
uint64_t fee, size_t nmix, bool stake, boost::optional<const register_info> reg_info, uint64_t unlock_time)
{
vector<tx_source_entry> sources;
vector<tx_destination_entry> destinations;
Expand All @@ -746,7 +749,38 @@ bool construct_tx_to_key(const std::vector<test_event_entry>& events, cryptonote
fill_tx_sources_and_destinations(events, blk_head, from, to, amount, fee, nmix, sources, destinations, &change_amount);
tx_destination_entry change_addr{change_amount, from.get_keys().m_account_address, false /* is subaddr */ };

return cryptonote::construct_tx(from.get_keys(), sources, destinations, change_addr, {}, tx, unlock_time, stake, true);

std::vector<uint8_t> extra;

if (stake) {

if (!reg_info) {
LOG_ERROR("Stake tx has not registration info");
return false;
}

add_service_node_pubkey_to_tx_extra(extra, reg_info->service_node_keypair.pub);

const uint64_t exp_timestamp = time(nullptr) + STAKING_AUTHORIZATION_EXPIRATION_WINDOW;

crypto::hash hash;
bool hashed = cryptonote::get_registration_hash(reg_info->addresses, reg_info->operator_cut, reg_info->portions, exp_timestamp, hash);
if (!hashed)
{
MERROR("Could not make registration hash from addresses and portions");
return false;
}

crypto::signature signature;
crypto::generate_signature(hash, reg_info->service_node_keypair.pub, reg_info->service_node_keypair.sec, signature);

add_service_node_register_to_tx_extra(extra, reg_info->addresses, reg_info->operator_cut, reg_info->portions, exp_timestamp, signature);
add_service_node_contributor_to_tx_extra(extra, reg_info->addresses.at(0));

}


return cryptonote::construct_tx(from.get_keys(), sources, destinations, change_addr, extra, tx, unlock_time, stake, true);
}

transaction construct_tx_with_fee(std::vector<test_event_entry>& events, const block& blk_head,
Expand Down
45 changes: 39 additions & 6 deletions tests/core_tests/chaingen.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ class test_generator
bf_hf_version= 1 << 8
};

using sn_contributor_t = std::pair<cryptonote::account_public_address, uint64_t>;

void get_block_chain(std::vector<block_info>& blockchain, const crypto::hash& head, size_t n) const;
void get_last_n_block_sizes(std::vector<size_t>& block_sizes, const crypto::hash& head, size_t n) const;
uint64_t get_already_generated_coins(const crypto::hash& blk_id) const;
Expand All @@ -206,10 +208,12 @@ class test_generator
void add_block(const cryptonote::block& blk, size_t tsx_size, std::vector<size_t>& block_sizes, uint64_t already_generated_coins);
bool construct_block(cryptonote::block& blk, uint64_t height, const crypto::hash& prev_id,
const cryptonote::account_base& miner_acc, uint64_t timestamp, uint64_t already_generated_coins,
std::vector<size_t>& block_sizes, const std::list<cryptonote::transaction>& tx_list);
std::vector<size_t>& block_sizes, const std::list<cryptonote::transaction>& tx_list, const crypto::public_key& sn_pub_key = crypto::null_pkey,
const std::vector<sn_contributor_t>& = {{{crypto::null_pkey, crypto::null_pkey}, STAKING_PORTIONS}});
bool construct_block(cryptonote::block& blk, const cryptonote::account_base& miner_acc, uint64_t timestamp);
bool construct_block(cryptonote::block& blk, const cryptonote::block& blk_prev, const cryptonote::account_base& miner_acc,
const std::list<cryptonote::transaction>& tx_list = std::list<cryptonote::transaction>());
const std::list<cryptonote::transaction>& tx_list = std::list<cryptonote::transaction>(), const crypto::public_key& sn_pub_key = crypto::null_pkey,
const std::vector<sn_contributor_t>& = {{{crypto::null_pkey, crypto::null_pkey}, STAKING_PORTIONS}});

bool construct_block_manually(cryptonote::block& blk, const cryptonote::block& prev_block,
const cryptonote::account_base& miner_acc, int actual_params = bf_none, uint8_t major_ver = 0,
Expand Down Expand Up @@ -246,9 +250,18 @@ bool construct_tx_to_key(const std::vector<test_event_entry>& events,
const cryptonote::account_base& from,
const cryptonote::account_base& to,
uint64_t amount);

struct register_info {
const cryptonote::keypair& service_node_keypair;
const std::vector<uint64_t>& portions;
uint64_t operator_cut;
const std::vector<cryptonote::account_public_address>& addresses;
};

bool construct_tx_to_key(const std::vector<test_event_entry>& events, cryptonote::transaction& tx,
const cryptonote::block& blk_head, const cryptonote::account_base& from, const cryptonote::account_base& to,
uint64_t amount, uint64_t fee, size_t nmix, bool stake=false, uint64_t unlock_time=0);
uint64_t amount, uint64_t fee, size_t nmix, bool stake=false, boost::optional<const register_info> reg_info = boost::none, uint64_t unlock_time=0);

cryptonote::transaction construct_tx_with_fee(std::vector<test_event_entry>& events, const cryptonote::block& blk_head,
const cryptonote::account_base& acc_from, const cryptonote::account_base& acc_to,
uint64_t amount, uint64_t fee);
Expand Down Expand Up @@ -619,6 +632,11 @@ inline bool do_replay_file(const std::string& filename)
generator.construct_block(BLK_NAME, PREV_BLOCK, MINER_ACC); \
VEC_EVENTS.push_back(BLK_NAME);

#define MAKE_NEXT_BLOCK_V2(VEC_EVENTS, BLK_NAME, PREV_BLOCK, MINER_ACC, WINNER, SN_INFO) \
cryptonote::block BLK_NAME; \
generator.construct_block(BLK_NAME, PREV_BLOCK, MINER_ACC, {}, WINNER, SN_INFO); \
VEC_EVENTS.push_back(BLK_NAME);

#define MAKE_NEXT_BLOCK_TX1(VEC_EVENTS, BLK_NAME, PREV_BLOCK, MINER_ACC, TX1) \
cryptonote::block BLK_NAME; \
{ \
Expand All @@ -645,6 +663,18 @@ inline bool do_replay_file(const std::string& filename)
BLK_NAME = blk_last; \
}

#define REWIND_BLOCKS_N_V2(VEC_EVENTS, BLK_NAME, PREV_BLOCK, MINER_ACC, COUNT, WINNER, SN_INFO) \
cryptonote::block BLK_NAME; \
{ \
cryptonote::block blk_last = PREV_BLOCK; \
for (size_t i = 0; i < COUNT; ++i) \
{ \
MAKE_NEXT_BLOCK_V2(VEC_EVENTS, blk, blk_last, MINER_ACC, WINNER, SN_INFO); \
blk_last = blk; \
} \
BLK_NAME = blk_last; \
}

#define REWIND_BLOCKS(VEC_EVENTS, BLK_NAME, PREV_BLOCK, MINER_ACC) REWIND_BLOCKS_N(VEC_EVENTS, BLK_NAME, PREV_BLOCK, MINER_ACC, CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW)

inline cryptonote::transaction make_registration_tx(
Expand All @@ -656,13 +686,16 @@ inline cryptonote::transaction make_registration_tx(
const std::vector<uint64_t>& portions,
const cryptonote::block& head)
{
uint64_t staking_requirement =
service_nodes::get_staking_requirement(cryptonote::FAKECHAIN, cryptonote::get_block_height(head) + 1);

const auto new_height = cryptonote::get_block_height(head) + 1;
const auto staking_requirement = service_nodes::get_staking_requirement(cryptonote::FAKECHAIN, new_height);

uint64_t amount = service_nodes::portions_to_amount(portions[0], staking_requirement);

cryptonote::transaction tx;
construct_tx_to_key(events, tx, head, account, account, amount, TESTS_DEFAULT_FEE, 9, true /* staking */, STAKING_REQUIREMENT_LOCK_BLOCKS_TESTNET);
boost::optional<const register_info> reg_info = register_info{service_node_keys, portions, operator_cut, addresses};
const auto unlock_time = new_height + service_nodes::get_staking_requirement_lock_blocks(cryptonote::FAKECHAIN);
construct_tx_to_key(events, tx, head, account, account, amount, TESTS_DEFAULT_FEE, 9, true /* staking */, reg_info, unlock_time);
events.push_back(tx);
return tx;
}
Expand Down
Loading

0 comments on commit 5e3471a

Please sign in to comment.