Skip to content

Commit

Permalink
Use get_block_hash_from_height(height) instead of block.hash, post ob…
Browse files Browse the repository at this point in the history
…solete stake transactions with zero amount to supernode to indicate supernode presense, stable_sort instead of sort for blockchain based list building
  • Loading branch information
LenyKholodov committed Mar 8, 2019
1 parent 8d2584a commit 7a0e731
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
6 changes: 3 additions & 3 deletions src/cryptonote_core/blockchain_based_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ void BlockchainBasedList::apply_block(uint64_t block_height, const crypto::hash&
if (!m_history.empty())
prev_supernodes = m_history.back()[i];

std::remove_if(prev_supernodes.begin(), prev_supernodes.end(), [block_height](const supernode& desc) {
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());

current_supernodes.reserve(stake_txs.size());

Expand Down Expand Up @@ -136,7 +136,7 @@ void BlockchainBasedList::apply_block(uint64_t block_height, const crypto::hash&

//sort valid supernodes by the age of stake

std::sort(current_supernodes.begin(), current_supernodes.end(), [](const supernode& s1, const supernode& s2) {
std::stable_sort(current_supernodes.begin(), current_supernodes.end(), [](const supernode& s1, const supernode& s2) {
return s1.block_height < s2.block_height || (s1.block_height == s2.block_height && s1.supernode_public_id < s2.supernode_public_id);
});

Expand Down
38 changes: 25 additions & 13 deletions src/cryptonote_core/stake_transaction_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ using namespace cryptonote;
namespace
{

const char* STAKE_TRANSACTION_STORAGE_FILE_NAME = "stake_transactions.bin";
const char* BLOCKCHAIN_BASED_LIST_FILE_NAME = "blockchain_based_list.bin";
const char* STAKE_TRANSACTION_STORAGE_FILE_NAME = "stake_transactions.bin";
const char* BLOCKCHAIN_BASED_LIST_FILE_NAME = "blockchain_based_list.v1.bin";
const uint64_t STAKE_TRANSACTIONS_INVOKE_DELAY = 100;

unsigned int get_tier(uint64_t stake)
{
Expand Down Expand Up @@ -122,7 +123,7 @@ void StakeTransactionProcessor::init_storages(const std::string& config_dir)
m_blockchain_based_list.reset(new BlockchainBasedList(config_dir + "/" + BLOCKCHAIN_BASED_LIST_FILE_NAME));
}

void StakeTransactionProcessor::process_block_stake_transaction(uint64_t block_index, const block& block, bool update_storage)
void StakeTransactionProcessor::process_block_stake_transaction(uint64_t block_index, const block& block, const crypto::hash& block_hash, bool update_storage)
{
if (block_index <= m_storage->get_last_processed_block_index())
return;
Expand Down Expand Up @@ -220,17 +221,17 @@ void StakeTransactionProcessor::process_block_stake_transaction(uint64_t block_i
}
}

m_storage->add_last_processed_block(block_index, db.get_block_hash_from_height(block_index));
m_storage->add_last_processed_block(block_index, block_hash);

if (update_storage)
m_storage->store();
}

void StakeTransactionProcessor::process_block_blockchain_based_list(uint64_t block_index, const block& block, bool update_storage)
void StakeTransactionProcessor::process_block_blockchain_based_list(uint64_t block_index, const block& block, const crypto::hash& block_hash, bool update_storage)
{
uint64_t prev_block_height = m_blockchain_based_list->block_height();

m_blockchain_based_list->apply_block(block_index, block.hash, *m_storage);
m_blockchain_based_list->apply_block(block_index, block_hash, *m_storage);

if (m_blockchain_based_list->need_store() || prev_block_height != m_blockchain_based_list->block_height())
{
Expand All @@ -241,10 +242,10 @@ void StakeTransactionProcessor::process_block_blockchain_based_list(uint64_t blo
}
}

void StakeTransactionProcessor::process_block(uint64_t block_index, const block& block, bool update_storage)
void StakeTransactionProcessor::process_block(uint64_t block_index, const block& block, const crypto::hash& block_hash, bool update_storage)
{
process_block_stake_transaction(block_index, block, update_storage);
process_block_blockchain_based_list(block_index, block, update_storage);
process_block_stake_transaction(block_index, block, block_hash, update_storage);
process_block_blockchain_based_list(block_index, block, block_hash, update_storage);
}

void StakeTransactionProcessor::synchronize()
Expand Down Expand Up @@ -311,7 +312,7 @@ void StakeTransactionProcessor::synchronize()
crypto::hash block_hash = db.get_block_hash_from_height(i);
const block& block = db.get_block(block_hash);

process_block(i, block, false);
process_block(i, block, block_hash, false);
}

if (m_blockchain_based_list->need_store())
Expand Down Expand Up @@ -349,10 +350,21 @@ void StakeTransactionProcessor::invoke_update_stake_transactions_handler_impl()

for (const stake_transaction& tx : stake_txs)
{
if (!tx.is_valid(top_block_index))
continue;
stake_transaction tx_copy = tx;

valid_stake_txs.push_back(tx);
if (!tx_copy.is_valid(top_block_index))
{
uint64_t first_history_block = top_block_index - STAKE_TRANSACTIONS_INVOKE_DELAY;

if (tx_copy.block_height + tx_copy.unlock_time < first_history_block)
continue;

//add stake transaction with zero amount to indicate correspondent node presense for search in supernode

tx_copy.amount = 0;
}

valid_stake_txs.emplace_back(std::move(tx_copy));
}

m_on_stake_transactions_update(valid_stake_txs);
Expand Down
6 changes: 3 additions & 3 deletions src/cryptonote_core/stake_transaction_processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ class StakeTransactionProcessor
void invoke_update_blockchain_based_list_handler(bool force = true, size_t depth = 1);

private:
void process_block(uint64_t block_index, const block& block, bool update_storage = true);
void process_block(uint64_t block_index, const block& block, const crypto::hash& block_hash, bool update_storage = true);
void invoke_update_stake_transactions_handler_impl();
void invoke_update_blockchain_based_list_handler_impl(size_t depth);
void process_block_stake_transaction(uint64_t block_index, const block& block, bool update_storage = true);
void process_block_blockchain_based_list(uint64_t block_index, const block& block, bool update_storage = true);
void process_block_stake_transaction(uint64_t block_index, const block& block, const crypto::hash& block_hash, bool update_storage = true);
void process_block_blockchain_based_list(uint64_t block_index, const block& block, const crypto::hash& block_hash, bool update_storage = true);

private:
Blockchain& m_blockchain;
Expand Down
4 changes: 2 additions & 2 deletions src/cryptonote_core/stake_transaction_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ void StakeTransactionStorage::remove_last_processed_block()

m_need_store = true;

std::remove_if(m_stake_txs.begin(), m_stake_txs.end(), [&](const stake_transaction& tx) {
m_stake_txs.erase(std::remove_if(m_stake_txs.begin(), m_stake_txs.end(), [&](const stake_transaction& tx) {
return tx.block_height == m_last_processed_block_index;
});
}), m_stake_txs.end());

m_last_processed_block_hashes_count--;
m_last_processed_block_index--;
Expand Down

0 comments on commit 7a0e731

Please sign in to comment.