Skip to content

Commit

Permalink
Merge pull request #231 from VanGrx/develop
Browse files Browse the repository at this point in the history
Develop stagenet 1.1
  • Loading branch information
VanGrx authored Jun 19, 2020
2 parents bfcbb6c + 2d49c02 commit 9fc5d4a
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 100 deletions.
2 changes: 1 addition & 1 deletion src/blockchain_db/blockchain_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ namespace cryptonote
uint8_t relayed;
uint8_t do_not_relay;
uint8_t double_spend_seen: 1;
uint8_t safex_failed{0};
uint8_t safex_tx{0};

uint8_t padding[75]; // till 192 bytes
};
Expand Down
1 change: 1 addition & 0 deletions src/cryptonote_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@

#define CRYPTONOTE_MEMPOOL_TX_LIVETIME (86400*3) //seconds, three days
#define CRYPTONOTE_MEMPOOL_TX_FROM_ALT_BLOCK_LIVETIME 604800 //seconds, one week
#define CRYPTONOTE_MEMPOOL_SAFEX_TX_LIVETIME 3600 //seconds, 1 hour

#define COMMAND_RPC_GET_BLOCKS_FAST_MAX_COUNT 1000

Expand Down
36 changes: 3 additions & 33 deletions src/cryptonote_core/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ static const struct {
{ 4, config::stagenet::HARDFORK_V4_START_HEIGHT, 0, 1565962165},
{ 5, config::stagenet::HARDFORK_V4_START_HEIGHT + 1, 0, 1565962166},
{ 6, config::stagenet::HARDFORK_V4_START_HEIGHT + 2, 0, 1592478292},
{ 7, 89400, 0, 1565972167}
{ 7, 90150, 0, 1592578800}
};

//------------------------------------------------------------------
Expand Down Expand Up @@ -4003,22 +4003,6 @@ bool Blockchain::check_tx_inputs(transaction& tx, tx_verification_context &tvc,
return false;
}
}

// min/max tx version based on HF, and we accept v1 txes if having a non mixable
const size_t max_tx_version = get_maximum_tx_version_supported();
if (tx.version > max_tx_version)
{
MERROR_VER("transaction version " << (unsigned)tx.version << " is higher than max accepted version " << max_tx_version);
tvc.m_verifivation_failed = true;
return false;
}
const size_t min_tx_version = MIN_SUPPORTED_TX_VERSION;
if (tx.version < min_tx_version)
{
MERROR_VER("transaction version " << (unsigned)tx.version << " is lower than min accepted version " << min_tx_version);
tvc.m_verifivation_failed = true;
return false;
}
}

//sorted ins
Expand Down Expand Up @@ -4152,8 +4136,6 @@ bool Blockchain::check_tx_inputs(transaction& tx, tx_verification_context &tvc,
return false;
}

if (tx.version >= MIN_SUPPORTED_TX_VERSION && tx.version <= get_maximum_tx_version_supported())
{
if (threads > 1)
{
// ND: Speedup
Expand Down Expand Up @@ -4261,18 +4243,13 @@ bool Blockchain::check_tx_inputs(transaction& tx, tx_verification_context &tvc,
}
it->second[k_image] = true;
}
}

sig_index++;
}

if ((tx.version >= MIN_SUPPORTED_TX_VERSION && tx.version <= get_maximum_tx_version_supported()) && threads > 1)
waiter.wait();

if (tx.version >= MIN_SUPPORTED_TX_VERSION && tx.version <= get_maximum_tx_version_supported())
{
if (threads > 1)
{
waiter.wait();
// save results to table, passed or otherwise
bool failed = false;
for (size_t i = 0; i < tx.vin.size(); i++)
Expand All @@ -4291,12 +4268,6 @@ bool Blockchain::check_tx_inputs(transaction& tx, tx_verification_context &tvc,
return false;
}
}
}
else
{
MERROR_VER("Transaction of version " << tx.version<<" not yet supported");
return false;
}

if(!check_safex_tx(tx,tvc)){
tvc.m_verifivation_failed = true;
Expand Down Expand Up @@ -6660,9 +6631,8 @@ bool Blockchain::are_safex_tokens_unlocked(const std::vector<txin_v> &tx_vin) {
return true;
}

uint8_t Blockchain::get_maximum_tx_version_supported() const
uint8_t Blockchain::get_maximum_tx_version_supported(uint8_t hf_version) const
{
auto hf_version = get_current_hard_fork_version();

switch (m_nettype) {
case cryptonote::network_type::FAKECHAIN:
Expand Down
2 changes: 1 addition & 1 deletion src/cryptonote_core/blockchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ namespace cryptonote
*
* @return return max tx_version that is supported for current hardfork
*/
uint8_t get_maximum_tx_version_supported() const;
uint8_t get_maximum_tx_version_supported(uint8_t hf_version) const;

/**
* @brief get dynamic per kB fee for a given block size
Expand Down
26 changes: 13 additions & 13 deletions src/cryptonote_core/cryptonote_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -675,14 +675,6 @@ namespace cryptonote
}
bad_semantics_txes_lock.unlock();

if (tx.version == 0 || tx.version > m_blockchain_storage.get_maximum_tx_version_supported())
{
// MAX_SUPPORTED_TX_VERSION is the latest transaction version
// we know in the current protocol version
tvc.m_verifivation_failed = true;
return false;
}

return true;
}
//-----------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -848,11 +840,10 @@ namespace cryptonote
return false;
}


const uint8_t version = m_blockchain_storage.get_current_hard_fork_version();

if(!check_money_overflow(tx))
{
const uint8_t version = m_blockchain_storage.get_current_hard_fork_version();
bool known_problem = false;

if(version < HF_VERSION_STOP_COUNTERFEIT_TOKENS) {
Expand All @@ -875,7 +866,7 @@ namespace cryptonote
}
}

if (tx.version >= MIN_SUPPORTED_TX_VERSION && tx.version <= m_blockchain_storage.get_maximum_tx_version_supported())
if (tx.version >= MIN_SUPPORTED_TX_VERSION && tx.version <= m_blockchain_storage.get_maximum_tx_version_supported(version))
{
uint64_t amount_in = 0;
get_inputs_cash_amount(tx, amount_in);
Expand Down Expand Up @@ -1348,7 +1339,12 @@ namespace cryptonote
m_miner.resume();
return false;
}
prepare_handle_incoming_blocks(blocks);
if (!prepare_handle_incoming_blocks(blocks))
{
MERROR("Block found, but failed to prepare to add");
m_miner.resume();
return false;
}
m_blockchain_storage.add_new_block(b, bvc);
cleanup_handle_incoming_blocks(true);
//anyway - update miner template
Expand Down Expand Up @@ -1402,7 +1398,11 @@ namespace cryptonote
bool core::prepare_handle_incoming_blocks(const std::list<block_complete_entry> &blocks)
{
m_incoming_tx_lock.lock();
m_blockchain_storage.prepare_handle_incoming_blocks(blocks);
if (!m_blockchain_storage.prepare_handle_incoming_blocks(blocks))
{
cleanup_handle_incoming_blocks(false);
return false;
}
return true;
}

Expand Down
126 changes: 75 additions & 51 deletions src/cryptonote_core/tx_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ namespace cryptonote
// fee per kilobyte, size rounded up.
uint64_t fee;

if (tx.version >= MIN_SUPPORTED_TX_VERSION && tx.version <= m_blockchain.get_maximum_tx_version_supported())
if (tx.version >= MIN_SUPPORTED_TX_VERSION && tx.version <= m_blockchain.get_maximum_tx_version_supported(version))
{
uint64_t inputs_amount = 0;
if(!get_inputs_cash_amount(tx, inputs_amount))
Expand Down Expand Up @@ -285,6 +285,7 @@ namespace cryptonote
meta.relayed = relayed;
meta.do_not_relay = do_not_relay;
meta.double_spend_seen = have_tx_keyimges_as_spent(tx);
meta.safex_tx = tx.version >=2;
memset(meta.padding, 0, sizeof(meta.padding));
try
{
Expand Down Expand Up @@ -328,6 +329,7 @@ namespace cryptonote
meta.relayed = relayed;
meta.do_not_relay = do_not_relay;
meta.double_spend_seen = false;
meta.safex_tx = tx.version >=2;
memset(meta.padding, 0, sizeof(meta.padding));

try
Expand Down Expand Up @@ -519,6 +521,18 @@ namespace cryptonote
const cryptonote::blobdata pricepegblob(std::begin(out.data), std::end(out.data));
cryptonote::parse_and_validate_from_blob(pricepegblob, price_peg);
m_safex_price_peg_update_in_progress.push_back(price_peg.price_peg_id);
} else if(vout.target.type() == typeid(txout_to_script) && get_tx_out_type(vout.target) == cryptonote::tx_out_type::out_safex_purchase)
{
const txout_to_script &out = boost::get<txout_to_script>(vout.target);
safex::create_purchase_data purchase;
const cryptonote::blobdata purchaseblob(std::begin(out.data), std::end(out.data));
cryptonote::parse_and_validate_from_blob(purchaseblob, purchase);
auto it = std::find_if(m_safex_offers_to_purchase.begin(),m_safex_offers_to_purchase.end(), [&purchase](const std::pair<crypto::hash, uint64_t>& item){ return purchase.offer_id == item.first;});
if( it != m_safex_offers_to_purchase.end()){
it->second += purchase.quantity;
}
else
m_safex_offers_to_purchase.push_back(std::make_pair(purchase.offer_id, purchase.quantity));
}
}
return true;
Expand Down Expand Up @@ -633,6 +647,20 @@ namespace cryptonote
auto it = std::find(m_safex_price_peg_update_in_progress.begin(), m_safex_price_peg_update_in_progress.end(), price_peg.price_peg_id);
CHECK_AND_ASSERT_MES(it != m_safex_price_peg_update_in_progress.end(), false, "failed to find safex restriction for type out_safex_price_peg_update" << ENDL << "transaction id = " << get_transaction_hash(tx));
m_safex_price_peg_update_in_progress.erase(it);
} else if (vout.target.type() == typeid(txout_to_script) && get_tx_out_type(vout.target) == cryptonote::tx_out_type::out_safex_purchase)
{
const txout_to_script &out = boost::get<txout_to_script>(vout.target);
safex::create_purchase_data purchase;
const cryptonote::blobdata purchaseblob(std::begin(out.data), std::end(out.data));
cryptonote::parse_and_validate_from_blob(purchaseblob, purchase);
auto it = std::find_if(m_safex_offers_to_purchase.begin(),m_safex_offers_to_purchase.end(), [&purchase](const std::pair<crypto::hash, uint64_t>& item){ return purchase.offer_id == item.first;});
CHECK_AND_ASSERT_MES(it != m_safex_offers_to_purchase.end(), false, "failed to find safex restriction for type out_safex_purchase" << ENDL << "transaction id = " << get_transaction_hash(tx));
if(it->second < purchase.quantity){
LOG_ERROR("This should not happen! As tx_pool internal calculation is wrong");
it->second = 0;
} else
it->second -= purchase.quantity;

}
}
return true;
Expand Down Expand Up @@ -709,8 +737,7 @@ namespace cryptonote
uint64_t tx_age = time(nullptr) - meta.receive_time;

if((tx_age > CRYPTONOTE_MEMPOOL_TX_LIVETIME && !meta.kept_by_block) ||
(tx_age > CRYPTONOTE_MEMPOOL_TX_FROM_ALT_BLOCK_LIVETIME && meta.kept_by_block) ||
meta.safex_failed)
(tx_age > CRYPTONOTE_MEMPOOL_TX_FROM_ALT_BLOCK_LIVETIME && meta.kept_by_block))
{
LOG_PRINT_L1("Tx " << txid << " removed from tx pool due to outdated, age: " << tx_age );
auto sorted_it = find_tx_in_sorted_container(txid);
Expand All @@ -724,6 +751,31 @@ namespace cryptonote
}
m_timed_out_transactions.insert(txid);
remove.insert(txid);
} else if (tx_age > CRYPTONOTE_MEMPOOL_SAFEX_TX_LIVETIME && meta.safex_tx)
{
cryptonote::blobdata txblob = m_blockchain.get_txpool_tx_blob(txid);
cryptonote::transaction tx;
if (!parse_and_validate_tx_from_blob(txblob, tx))
{
MERROR("Failed to parse tx from txpool");
return true;
}
tx_verification_context tvc;
if(!m_blockchain.check_safex_tx(tx, tvc))
{
LOG_PRINT_L1("Tx " << txid << " removed from tx pool due to outdated, age: " << tx_age );
auto sorted_it = find_tx_in_sorted_container(txid);
if (sorted_it == m_txs_by_fee_and_receive_time.end())
{
LOG_PRINT_L1("Removing tx " << txid << " from tx pool, but it was not found in the sorted txs container!");
}
else
{
m_txs_by_fee_and_receive_time.erase(sorted_it);
}
m_timed_out_transactions.insert(txid);
remove.insert(txid);
}
}
return true;
}, false);
Expand Down Expand Up @@ -1091,53 +1143,6 @@ namespace cryptonote
//---------------------------------------------------------------------------------
bool tx_memory_pool::on_blockchain_inc(uint64_t new_block_height, const crypto::hash& top_block_id)
{

CRITICAL_REGION_LOCAL(m_transactions_lock);
CRITICAL_REGION_LOCAL1(m_blockchain);

for(auto& sorted_it : m_txs_by_fee_and_receive_time)
{
txpool_tx_meta_t meta;

if (!m_blockchain.get_txpool_tx_meta(sorted_it.second, meta))
{
MERROR(" failed to find tx meta");
continue;
}

cryptonote::blobdata txblob = m_blockchain.get_txpool_tx_blob(sorted_it.second);
cryptonote::transaction tx;
if (!parse_and_validate_tx_from_blob(txblob, tx))
{
MERROR("Failed to parse tx from txpool");
continue;
}

// Skip transactions that are not ready to be
// included into the blockchain or that are
// missing key images
const cryptonote::txpool_tx_meta_t original_meta = meta;
tx_verification_context tvc;
if(!m_blockchain.check_tx_inputs(tx, meta.max_used_block_height, meta.max_used_block_id, tvc))
{
meta.safex_failed = tvc.m_safex_invalid_input || tvc.m_safex_invalid_command || tvc.m_safex_verification_failed ||
tvc.m_safex_invalid_command_params || tvc.m_safex_command_execution_failed;
}
if (memcmp(&original_meta, &meta, sizeof(meta)))
{
LockedTXN lock(m_blockchain);
try
{
m_blockchain.update_txpool_tx(sorted_it.second, meta);
}
catch (const std::exception &e)
{
MERROR("Failed to update tx meta: " << e.what());
// continue, not fatal
}
lock.commit();
}
}
return true;
}
//---------------------------------------------------------------------------------
Expand Down Expand Up @@ -1240,8 +1245,19 @@ namespace cryptonote
safex::create_purchase_data purchase;
const cryptonote::blobdata purchaseblob(std::begin(out.data), std::end(out.data));
cryptonote::parse_and_validate_from_blob(purchaseblob, purchase);
uint64_t purchased_quantity = have_tx_safex_purchase_in_use(purchase.offer_id);
if(have_tx_safex_offer_in_use(purchase.offer_id))
return true;
if(purchased_quantity != 0){
uint64_t quantity;
bool res = m_blockchain.get_safex_offer_quantity(purchase.offer_id, quantity);
if(!res){
LOG_ERROR("Error getin offer quantity from blockchain");
return true;
}
if(quantity < purchased_quantity + purchase.quantity)
return true;
}
}
}
return false;
Expand Down Expand Up @@ -1271,6 +1287,13 @@ namespace cryptonote
return m_safex_price_peg_update_in_progress.end() != std::find(m_safex_price_peg_update_in_progress.begin(), m_safex_price_peg_update_in_progress.end(), price_peg_id);
}
//---------------------------------------------------------------------------------
uint64_t tx_memory_pool::have_tx_safex_purchase_in_use(const crypto::hash &offer_id) const
{
CRITICAL_REGION_LOCAL(m_transactions_lock);
auto it = std::find_if(m_safex_offers_to_purchase.begin(),m_safex_offers_to_purchase.end(), [&offer_id](const std::pair<crypto::hash, uint64_t>& item){ return offer_id == item.first;});
return it != m_safex_offers_to_purchase.end() ? it->second : 0;
}
//---------------------------------------------------------------------------------
void tx_memory_pool::lock() const
{
m_transactions_lock.lock();
Expand Down Expand Up @@ -1539,7 +1562,7 @@ namespace cryptonote
uint64_t best_coinbase = 0, coinbase = 0;
total_size = 0;
fee = 0;

//baseline empty block
get_block_reward(median_size, total_size, already_generated_coins, best_coinbase, version, height);

Expand Down Expand Up @@ -1742,6 +1765,7 @@ namespace cryptonote
m_spent_key_images.clear();
m_safex_accounts_in_use.clear();
m_safex_offers_in_use.clear();
m_safex_offers_to_purchase.clear();
m_txpool_size = 0;
std::vector<crypto::hash> remove;

Expand Down
Loading

0 comments on commit 9fc5d4a

Please sign in to comment.