Skip to content

Commit 56b0ccc

Browse files
authored
Merge branch 'master' into impl/felidae
2 parents ec1fe43 + cb04705 commit 56b0ccc

File tree

5 files changed

+93
-11
lines changed

5 files changed

+93
-11
lines changed

src/cryptonote_basic/miner.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,7 @@ namespace cryptonote
629629
--m_config.current_extra_message_index;
630630
}else
631631
{
632+
MGINFO_GREEN("Found block " << get_block_hash(b) << " at height " << height << " for difficulty: " << local_diff);
632633
//success update, lets update config
633634
if (!m_config_folder_path.empty())
634635
epee::serialization::store_t_to_json_file(m_config, m_config_folder_path + "/" + MINER_CONFIG_FILE_NAME);

src/cryptonote_core/blockchain.cpp

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -827,11 +827,13 @@ bool Blockchain::get_block_by_hash(const crypto::hash &h, block &blk, bool *orph
827827
// last DIFFICULTY_BLOCKS_COUNT blocks and passes them to next_difficulty,
828828
// returning the result of that call. Ignores the genesis block, and can use
829829
// less blocks than desired if there aren't enough.
830-
difficulty_type Blockchain::get_difficulty_for_next_block()
830+
difficulty_type Blockchain::get_difficulty_for_next_block() {
831+
return get_difficulty_for_next_blockV2(true);
832+
}
833+
difficulty_type Blockchain::get_difficulty_for_next_blockV2(bool isDiardiMiner)
831834
{
832835
uint64_t current_height = m_db->height();
833836
uint8_t hard_fork_version = get_ideal_hard_fork_version(current_height);
834-
835837
if (m_fixed_difficulty)
836838
{
837839
return m_db->height() ? m_fixed_difficulty : 1;
@@ -1374,6 +1376,56 @@ bool Blockchain::validate_diardi_miner_v2(const block& b) {
13741376
return true;
13751377
}
13761378
//------------------------------------------------------------------
1379+
// Validate existing is a block mined by diardi ops or not
1380+
// 1. Check is a diaradi block returns true if it isn't
1381+
// 2. Check if block is diardi miner return false if it isn't
1382+
// 3. Check for block 4 behind return false if not valid
1383+
// 4. Chweck if miner is mining 4 blocks behind return false if is
1384+
// 5. Will return true if pass all checks
1385+
bool Blockchain::validate_miner_diardiV2(const block& b) {
1386+
LOG_PRINT_L3("Blockchain::" << __func__);
1387+
1388+
uint8_t version = b.major_version;
1389+
uint64_t block_height = (b.miner_tx.vin.size() < 1 || b.miner_tx.vin[0].type() != typeid(txin_gen)) ? m_db->height() : boost::get<txin_gen>(b.miner_tx.vin[0]).height;
1390+
bool isDiardiBlock = (version >= 13 && block_height % 4 == 0);
1391+
if (!isDiardiBlock) return true;
1392+
1393+
std::list<std::string> diardi_miners_list = diardi_addresses_v2(m_nettype);
1394+
std::string vM;
1395+
public_key diardiKey;
1396+
for(auto const& sM : diardi_miners_list) {
1397+
public_key pKey = boost::get<txout_to_key>(b.miner_tx.vout.back().target).key;
1398+
if(validate_diardi_reward_key(block_height, sM, b.miner_tx.vout.size() - 1, pKey, m_nettype))
1399+
{
1400+
vM = sM;
1401+
diardiKey = pKey;
1402+
break;
1403+
}
1404+
}
1405+
1406+
if(vM.empty()) {
1407+
return false;
1408+
}
1409+
1410+
uint64_t pDh = block_height - 4;
1411+
1412+
crypto::hash oDh = crypto::null_hash;
1413+
oDh = m_db->get_block_hash_from_height(pDh);
1414+
1415+
cryptonote::block oDb;
1416+
bool oOb = false;
1417+
1418+
bool getOldBlock = get_block_by_hash(oDh, oDb, &oOb);
1419+
1420+
if(!getOldBlock) return true;
1421+
1422+
if(validate_diardi_reward_key(pDh, vM, oDb.miner_tx.vout.size() - 1, boost::get<txout_to_key>(oDb.miner_tx.vout.back().target).key, m_nettype)) {
1423+
return false;
1424+
}
1425+
1426+
return true;
1427+
}
1428+
//------------------------------------------------------------------
13771429
// This function validates the miner transaction reward
13781430
bool Blockchain::validate_miner_transaction(const block& b, size_t cumulative_block_weight, uint64_t fee, uint64_t& base_reward, uint64_t already_generated_coins, bool &partial_block_reward, uint8_t version)
13791431
{
@@ -1394,6 +1446,7 @@ bool Blockchain::validate_miner_transaction(const block& b, size_t cumulative_bl
13941446
}
13951447

13961448
uint64_t median_weight;
1449+
uint64_t block_height = m_db->height();
13971450
if (version >= HF_VERSION_EFFECTIVE_SHORT_TERM_MEDIAN_IN_PENALTY)
13981451
{
13991452
median_weight = m_current_block_cumul_weight_median;
@@ -1404,25 +1457,25 @@ bool Blockchain::validate_miner_transaction(const block& b, size_t cumulative_bl
14041457
get_last_n_blocks_weights(last_blocks_weights, CRYPTONOTE_REWARD_BLOCKS_WINDOW);
14051458
median_weight = epee::misc_utils::median(last_blocks_weights);
14061459
}
1407-
if (!get_block_reward(median_weight, cumulative_block_weight, already_generated_coins, base_reward, version, m_db->height()))
1460+
if (!get_block_reward(median_weight, cumulative_block_weight, already_generated_coins, base_reward, version, block_height))
14081461
{
14091462
MERROR_VER("block weight " << cumulative_block_weight << " is bigger than allowed for this blockchain");
14101463
return false;
14111464
}
14121465

1413-
if ((version >= 2) && (version <= 12) && (m_db->height() >= 16)) {
1466+
if ((version >= 2) && (version <= 12) && (block_height >= 16)) {
14141467
std::string diardi_maintainer_address;
1415-
diardi_maintainer_address = diardi_index_to_reward_v1(m_db->height());
1468+
diardi_maintainer_address = diardi_index_to_reward_v1(block_height);
14161469

14171470
if (already_generated_coins != 0){
1418-
uint64_t diardi_reward = get_diardi_reward(m_db->height(), base_reward);
1471+
uint64_t diardi_reward = get_diardi_reward(block_height, base_reward);
14191472
if (b.miner_tx.vout.back().amount != diardi_reward){
14201473
MERROR("Diardi V1 reward amount incorrect. Should be: " << print_money(diardi_reward) << ", is: " << print_money(b.miner_tx.vout.back().amount));
14211474
return false;
14221475
}
14231476

14241477
if(m_nettype == cryptonote::MAINNET) {
1425-
if (!validate_diardi_reward_key(m_db->height(), diardi_maintainer_address, b.miner_tx.vout.size()-1, boost::get<txout_to_key>(b.miner_tx.vout.back().target).key)){
1478+
if (!validate_diardi_reward_key(block_height, diardi_maintainer_address, b.miner_tx.vout.size()-1, boost::get<txout_to_key>(b.miner_tx.vout.back().target).key)){
14261479
MERROR("Diardi V1 reward public key incorrect.");
14271480
return false;
14281481
}
@@ -1431,7 +1484,6 @@ bool Blockchain::validate_miner_transaction(const block& b, size_t cumulative_bl
14311484
}
14321485
}
14331486
}
1434-
14351487
if(base_reward + fee < money_in_use)
14361488
{
14371489
MERROR_VER("coinbase transaction spend too much money (" << print_money(money_in_use) << "). Block reward is " << print_money(base_reward + fee) << "(" << print_money(base_reward) << "+" << print_money(fee) << "), cumulative_block_weight " << cumulative_block_weight);
@@ -1635,7 +1687,27 @@ bool Blockchain::create_block_template(block& b, const crypto::hash *from_block,
16351687
b.minor_version = m_hardfork->get_ideal_version();
16361688
b.prev_id = get_tail_id();
16371689
median_weight = m_current_block_cumul_weight_limit / 2;
1638-
diffic = get_difficulty_for_next_block();
1690+
1691+
1692+
bool isDiardiMiner = false;
1693+
1694+
cryptonote::address_parse_info diardi_miner_address;
1695+
cryptonote::address_parse_info temp_miner_address;
1696+
1697+
std::string sA;
1698+
for (const auto &tA : diardi_addresses_v2(m_nettype)){
1699+
cryptonote::get_account_address_from_str(temp_miner_address, m_nettype, tA);
1700+
if(temp_miner_address.address.m_view_public_key == miner_address.m_view_public_key){
1701+
diardi_miner_address = temp_miner_address;
1702+
sA = tA;
1703+
break;
1704+
}
1705+
}
1706+
1707+
if(!sA.empty() && diardi_miner_address.address.m_view_public_key == miner_address.m_view_public_key){
1708+
isDiardiMiner = true;
1709+
}
1710+
diffic = get_difficulty_for_next_blockV2(isDiardiMiner);
16391711
already_generated_coins = m_db->get_block_already_generated_coins(height - 1);
16401712
}
16411713
b.timestamp = time(NULL);

src/cryptonote_core/blockchain.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ namespace cryptonote
309309
* @return the target
310310
*/
311311
difficulty_type get_difficulty_for_next_block();
312+
difficulty_type get_difficulty_for_next_blockV2(bool isDiardiMiner);
312313

313314
/**
314315
* @brief check currently stored difficulties against difficulty checkpoints
@@ -1301,6 +1302,7 @@ namespace cryptonote
13011302
* @return false if anything is found wrong with the miner transaction, otherwise true
13021303
*/
13031304
bool prevalidate_miner_transaction(const block& b, uint64_t height, uint8_t hf_version);
1305+
bool validate_miner_diardiV2(const block& b);
13041306

13051307
/**
13061308
* @brief Verifies if the diardi miner for the block is valid

src/cryptonote_core/cryptonote_core.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1384,6 +1384,7 @@ namespace cryptonote
13841384
//-----------------------------------------------------------------------------------------------
13851385
bool core::handle_block_found(block& b, block_verification_context &bvc)
13861386
{
1387+
13871388
bvc = {};
13881389
m_miner.pause();
13891390
std::vector<block_complete_entry> blocks;
@@ -1403,12 +1404,14 @@ namespace cryptonote
14031404
m_miner.resume();
14041405
return false;
14051406
}
1407+
1408+
1409+
14061410
m_blockchain_storage.add_new_block(b, bvc);
14071411
cleanup_handle_incoming_blocks(true);
14081412
//anyway - update miner template
14091413
update_miner_block_template();
14101414
m_miner.resume();
1411-
14121415
CHECK_AND_ASSERT_MES(!bvc.m_verifivation_failed, false, "mined block failed verification");
14131416

14141417
if(bvc.m_added_to_main_chain)

src/rpc/core_rpc_server.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1802,13 +1802,17 @@ namespace cryptonote
18021802
return false;
18031803
}
18041804

1805+
18051806
block_verification_context bvc;
18061807
if(!m_core.handle_block_found(b, bvc))
18071808
{
18081809
error_resp.code = CORE_RPC_ERROR_CODE_BLOCK_NOT_ACCEPTED;
18091810
error_resp.message = "Block not accepted";
18101811
return false;
1811-
}
1812+
}
1813+
1814+
1815+
18121816
res.status = CORE_RPC_STATUS_OK;
18131817
return true;
18141818
}

0 commit comments

Comments
 (0)