Skip to content

Commit

Permalink
Linking blockchain based list with supernode stakes based on block nu…
Browse files Browse the repository at this point in the history
…mber. Passing stake amount along with blockchain based list to supernode
  • Loading branch information
LenyKholodov committed Mar 10, 2019
1 parent ad0241f commit 4d4be6b
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 66 deletions.
3 changes: 2 additions & 1 deletion src/cryptonote_core/blockchain_based_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void BlockchainBasedList::apply_block(uint64_t block_height, const crypto::hash&
if (block_height != m_block_height + 1)
throw std::runtime_error("block_height should be next after the block already processed");

const StakeTransactionStorage::supernode_stake_array& stakes = stake_txs_storage.get_supernode_stakes();
const StakeTransactionStorage::supernode_stake_array& stakes = stake_txs_storage.get_supernode_stakes(block_height);

//build blockchain based list for each tier

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

sn.supernode_public_id = stake.supernode_public_id;
sn.supernode_public_address = stake.supernode_public_address;
sn.amount = stake.amount;
sn.block_height = stake.block_height;
sn.unlock_time = stake.unlock_time;

Expand Down
2 changes: 2 additions & 0 deletions src/cryptonote_core/blockchain_based_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ class BlockchainBasedList
{
std::string supernode_public_id;
cryptonote::account_public_address supernode_public_address;
uint64_t amount;
uint64_t block_height;
uint64_t unlock_time;

BEGIN_SERIALIZE_OBJECT()
FIELD(amount)
FIELD(block_height)
FIELD(unlock_time)
FIELD(supernode_public_id)
Expand Down
19 changes: 8 additions & 11 deletions src/cryptonote_core/stake_transaction_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ using namespace cryptonote;
namespace
{

const char* STAKE_TRANSACTION_STORAGE_FILE_NAME = "stake_transactions.v1.bin";
const char* BLOCKCHAIN_BASED_LIST_FILE_NAME = "blockchain_based_list.v2.bin";
const char* STAKE_TRANSACTION_STORAGE_FILE_NAME = "stake_transactions.v2.bin";
const char* BLOCKCHAIN_BASED_LIST_FILE_NAME = "blockchain_based_list.v3.bin";

}

Expand All @@ -34,12 +34,12 @@ StakeTransactionProcessor::StakeTransactionProcessor(Blockchain& blockchain)
{
}

const supernode_stake* StakeTransactionProcessor::find_supernode_stake(const std::string& supernode_public_id) const
const supernode_stake* StakeTransactionProcessor::find_supernode_stake(uint64_t block_number, const std::string& supernode_public_id) const
{
if (!m_storage)
return nullptr;

return m_storage->find_supernode_stake(supernode_public_id);
return m_storage->find_supernode_stake(block_number, supernode_public_id);
}

namespace
Expand Down Expand Up @@ -195,14 +195,11 @@ void StakeTransactionProcessor::process_block_stake_transaction(uint64_t block_i
<< "', amount=" << amount / double(COIN));
}

//remove obsolete stake transactions from a storage

m_storage->remove_obsolete_txs(block_index);

//update supernode stakes

if (m_storage->update_supernode_stakes(block_index))
m_stakes_need_update = true;
m_storage->update_supernode_stakes(block_index);

m_stakes_need_update = true; //TODO: cache for stakes

//update cache entries and save storage

Expand Down Expand Up @@ -326,7 +323,7 @@ void StakeTransactionProcessor::invoke_update_stakes_handler_impl()
{
try
{
m_on_stakes_update(m_storage->get_supernode_stakes());
m_on_stakes_update(m_storage->get_supernode_stakes(m_blockchain.get_db().height() - 1));

m_stakes_need_update = false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/cryptonote_core/stake_transaction_processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class StakeTransactionProcessor
void init_storages(const std::string& config_dir);

/// Search supernode stake by supernode public id (returns nullptr if no stake is found)
const supernode_stake* find_supernode_stake(const std::string& supernode_public_id) const;
const supernode_stake* find_supernode_stake(uint64_t block_number, const std::string& supernode_public_id) const;

/// Synchronize with blockchain
void synchronize();
Expand Down
54 changes: 8 additions & 46 deletions src/cryptonote_core/stake_transaction_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,10 @@ void StakeTransactionStorage::remove_last_processed_block()
}
}

void StakeTransactionStorage::remove_obsolete_txs(uint64_t block_number)
const StakeTransactionStorage::supernode_stake_array& StakeTransactionStorage::get_supernode_stakes(uint64_t block_number)
{
if (block_number < STAKE_TRANSACTIONS_HISTORY_DEPTH)
return;

stake_transaction_array::iterator it = std::remove_if(m_stake_txs.begin(), m_stake_txs.end(), [&](const stake_transaction& tx) {
return tx.block_height + tx.unlock_time < block_number - STAKE_TRANSACTIONS_HISTORY_DEPTH;
});

if (it == m_stake_txs.end())
return;

m_need_store = true;

m_stake_txs.erase(it, m_stake_txs.end());
update_supernode_stakes(block_number);
return m_supernode_stakes;
}

void StakeTransactionStorage::clear_supernode_stakes()
Expand All @@ -149,37 +138,10 @@ unsigned int get_tier(uint64_t stake)

}

bool StakeTransactionStorage::update_supernode_stakes(uint64_t block_number)
void StakeTransactionStorage::update_supernode_stakes(uint64_t block_number)
{
if (block_number == m_supernode_stakes_update_block_number)
return false;

if (m_supernode_stakes_update_block_number)
{
bool updated = false;

if (m_stake_txs.empty() && !m_supernode_stakes.empty())
updated = true;

for (const stake_transaction& tx : m_stake_txs)
{
//compare validity of transaction between this and previous updates

bool is_valid_for_this_block = tx.is_valid(block_number),
is_valid_for_prev_block = tx.is_valid(m_supernode_stakes_update_block_number);

if (is_valid_for_prev_block != is_valid_for_this_block)
{
updated = true;
break;
}
}

if (!updated)
return false;
}

//because transactions validity has changed, update all stakes
return;

m_supernode_stakes.clear();
m_supernode_stake_indexes.clear();
Expand Down Expand Up @@ -294,12 +256,12 @@ bool StakeTransactionStorage::update_supernode_stakes(uint64_t block_number)
}

m_supernode_stakes_update_block_number = block_number;

return true;
}

const supernode_stake* StakeTransactionStorage::find_supernode_stake(const std::string& supernode_public_id) const
const supernode_stake* StakeTransactionStorage::find_supernode_stake(uint64_t block_number, const std::string& supernode_public_id)
{
update_supernode_stakes(block_number);

supernode_stake_index_map::const_iterator it = m_supernode_stake_indexes.find(supernode_public_id);

if (it == m_supernode_stake_indexes.end())
Expand Down
11 changes: 4 additions & 7 deletions src/cryptonote_core/stake_transaction_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,14 @@ class StakeTransactionStorage
/// Add transaction
void add_tx(const stake_transaction&);

/// Remove obsolete stake transactions
void remove_obsolete_txs(uint64_t block_number);

/// List of supernode stakes
const supernode_stake_array& get_supernode_stakes() const { return m_supernode_stakes; }
const supernode_stake_array& get_supernode_stakes(uint64_t block_number);

/// Search supernode stake by supernode public id (returns nullptr if no stake is found)
const supernode_stake* find_supernode_stake(const std::string& supernode_public_id) const;
const supernode_stake* find_supernode_stake(uint64_t block_number, const std::string& supernode_public_id);

/// Update supernode stakes (returns true if stakes have been updated, false if the result is same)
bool update_supernode_stakes(uint64_t block_number);
/// Update supernode stakes
void update_supernode_stakes(uint64_t block_number);

/// Clear supernode stakes
void clear_supernode_stakes();
Expand Down
1 change: 1 addition & 0 deletions src/p2p/net_node.inl
Original file line number Diff line number Diff line change
Expand Up @@ -3000,6 +3000,7 @@ namespace nodetool

dst_supernode.supernode_public_id = src_supernode.supernode_public_id;
dst_supernode.supernode_public_address = cryptonote::get_account_address_as_str(m_testnet, src_supernode.supernode_public_address);
dst_supernode.amount = src_supernode.amount;

dst_tier.supernodes.emplace_back(std::move(dst_supernode));
}
Expand Down
2 changes: 2 additions & 0 deletions src/rpc/core_rpc_server_commands_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1792,9 +1792,11 @@ namespace cryptonote
{
std::string supernode_public_id;
std::string supernode_public_address;
uint64_t amount;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(supernode_public_id)
KV_SERIALIZE(supernode_public_address)
KV_SERIALIZE(amount)
END_KV_SERIALIZE_MAP()
};

Expand Down

0 comments on commit 4d4be6b

Please sign in to comment.