Skip to content

Commit

Permalink
Fix blockchain based list usage of previous blockchain based list
Browse files Browse the repository at this point in the history
  • Loading branch information
LenyKholodov committed Mar 18, 2019
1 parent a5002c8 commit c9de8b7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 36 deletions.
45 changes: 18 additions & 27 deletions src/cryptonote_core/blockchain_based_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,6 @@ const BlockchainBasedList::supernode_tier_array& BlockchainBasedList::tiers(size
return *it;
}

namespace
{

bool is_valid_stake(uint64_t block_height, uint64_t stake_block_height, uint64_t stake_unlock_time)
{
if (block_height < stake_block_height)
return false; //stake transaction block is in future

uint64_t stake_first_valid_block = stake_block_height,
stake_last_valid_block = stake_block_height + stake_unlock_time;

if (stake_last_valid_block <= block_height)
return false; //stake transaction is not valid

return true;
}

}

void BlockchainBasedList::select_supernodes(size_t items_count, const supernode_array& src_list, supernode_array& dst_list)
{
size_t src_list_size = src_list.size();
Expand Down Expand Up @@ -98,14 +79,27 @@ void BlockchainBasedList::apply_block(uint64_t block_height, const crypto::hash&
prev_supernodes.clear();
current_supernodes.clear();

//prepare lists of valid supernodes (stake period is valid)
//prepare lists of valid supernodes for this tier

if (!m_history.empty())
prev_supernodes = m_history.back()[i];
{
const supernode_array& full_prev_supernodes = m_history.back()[i];

prev_supernodes.reserve(full_prev_supernodes.size());

for (const supernode& sn : full_prev_supernodes)
{
const supernode_stake* stake = stake_txs_storage.find_supernode_stake(block_height, sn.supernode_public_id);

if (!stake || !stake->amount)
continue;

prev_supernodes.erase(std::remove_if(prev_supernodes.begin(), prev_supernodes.end(), [block_height](const supernode& desc) {
return !is_valid_stake(block_height, desc.block_height, desc.unlock_time);
}), prev_supernodes.end());
if (stake->tier != i + 1)
continue;

prev_supernodes.push_back(sn);
}
}

current_supernodes.reserve(stakes.size());

Expand All @@ -114,9 +108,6 @@ void BlockchainBasedList::apply_block(uint64_t block_height, const crypto::hash&
if (!stake.amount)
continue;

if (!is_valid_stake(block_height, stake.block_height, stake.unlock_time))
continue;

if (stake.tier != i + 1)
continue;

Expand Down
2 changes: 1 addition & 1 deletion src/cryptonote_core/stake_transaction_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace
{

const char* STAKE_TRANSACTION_STORAGE_FILE_NAME = "stake_transactions.v2.bin";
const char* BLOCKCHAIN_BASED_LIST_FILE_NAME = "blockchain_based_list.v4.bin";
const char* BLOCKCHAIN_BASED_LIST_FILE_NAME = "blockchain_based_list.v5.bin";

}

Expand Down
33 changes: 25 additions & 8 deletions src/cryptonote_core/stake_transaction_storage.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "blockchain.h"
#include "stake_transaction_storage.h"
#include "file_io_utils.h"
#include "serialization/binary_utils.h"
Expand Down Expand Up @@ -143,6 +144,8 @@ void StakeTransactionStorage::update_supernode_stakes(uint64_t block_number)
if (block_number == m_supernode_stakes_update_block_number)
return;

MDEBUG("Build stakes for block " << block_number);

m_supernode_stakes.clear();
m_supernode_stake_indexes.clear();

Expand All @@ -166,6 +169,13 @@ void StakeTransactionStorage::update_supernode_stakes(uint64_t block_number)
obsolete_stake = true;
}

MDEBUG("...use stake transaction " << tx.hash << " as " << (obsolete_stake ? "obsolete" : "normal") << " stake transaction ");

//compute stake validity period

uint64_t min_tx_block_height = tx.block_height + config::graft::STAKE_VALIDATION_PERIOD,
max_tx_block_height = tx.block_height + tx.unlock_time + config::graft::TRUSTED_RESTAKING_PERIOD;

//search for a stake of the corresponding supernode

supernode_stake_index_map::iterator it = m_supernode_stake_indexes.find(tx.supernode_public_id);
Expand All @@ -187,8 +197,11 @@ void StakeTransactionStorage::update_supernode_stakes(uint64_t block_number)
{
new_stake.amount = tx.amount;
new_stake.tier = get_tier(new_stake.amount);
new_stake.block_height = tx.block_height + config::graft::STAKE_VALIDATION_PERIOD;
new_stake.unlock_time = tx.unlock_time + config::graft::TRUSTED_RESTAKING_PERIOD - config::graft::STAKE_VALIDATION_PERIOD;
new_stake.block_height = min_tx_block_height;
new_stake.unlock_time = max_tx_block_height - min_tx_block_height;

MDEBUG("...first stake transaction for supernode " << tx.supernode_public_id << ": amount=" << tx.amount << ", tier=" <<
new_stake.tier << ", validity=[" << min_tx_block_height << ";" << max_tx_block_height << ")");
}

new_stake.supernode_public_id = tx.supernode_public_id;
Expand All @@ -206,6 +219,9 @@ void StakeTransactionStorage::update_supernode_stakes(uint64_t block_number)
if (obsolete_stake)
continue; //no need to aggregate fields from obsolete stake

MDEBUG("...accumulate stake transaction for supernode " << tx.supernode_public_id << ": amount=" << tx.amount <<
", validity=[" << min_tx_block_height << ";" << max_tx_block_height << ")");

supernode_stake& stake = m_supernode_stakes[it->second];

if (!stake.amount)
Expand All @@ -214,8 +230,8 @@ void StakeTransactionStorage::update_supernode_stakes(uint64_t block_number)

stake.amount = tx.amount;
stake.tier = get_tier(stake.amount);
stake.block_height = tx.block_height + config::graft::STAKE_VALIDATION_PERIOD;
stake.unlock_time = tx.unlock_time + config::graft::TRUSTED_RESTAKING_PERIOD - config::graft::STAKE_VALIDATION_PERIOD;
stake.block_height = min_tx_block_height;
stake.unlock_time = max_tx_block_height - min_tx_block_height;

continue;
}
Expand All @@ -227,10 +243,8 @@ void StakeTransactionStorage::update_supernode_stakes(uint64_t block_number)

//find intersection of stake transaction intervals

uint64_t min_block_height = stake.block_height,
max_block_height = min_block_height + stake.unlock_time,
min_tx_block_height = tx.block_height,
max_tx_block_height = min_tx_block_height + tx.unlock_time;
uint64_t min_block_height = stake.block_height,
max_block_height = min_block_height + stake.unlock_time;

if (min_tx_block_height > min_block_height)
min_block_height = min_tx_block_height;
Expand All @@ -243,6 +257,9 @@ void StakeTransactionStorage::update_supernode_stakes(uint64_t block_number)

stake.block_height = min_block_height;
stake.unlock_time = max_block_height - min_block_height;

MDEBUG("...stake for supernode " << tx.supernode_public_id << ": amount=" << stake.amount << ", tier=" << stake.tier <<
", validity=[" << min_block_height << ";" << max_block_height << ")");
}
}
catch (...)
Expand Down

0 comments on commit c9de8b7

Please sign in to comment.