Skip to content

Commit c7c3249

Browse files
committed
test, stagenets: add hardfork blocks, diardi: fix mining without view tags
1 parent 30afe32 commit c7c3249

File tree

12 files changed

+157
-137
lines changed

12 files changed

+157
-137
lines changed

src/cryptonote_basic/miner.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ const command_line::arg_descriptor<std::string> arg_spendkey = {
131131

132132
miner::miner(i_miner_handler *phandler, const get_block_hash_t &gbh)
133133
: m_stop(1), m_template{}, m_template_no(0), m_diffic(0), m_thread_index(0),
134-
m_phandler(phandler), m_gbh(gbh), m_height(0), m_threads_active(0),
134+
m_phandler(phandler), m_gbh(gbh), m_height(0), m_last_mined(0), m_threads_active(0),
135135
m_pausers_count(0), m_threads_total(0), m_starter_nonce(0),
136136
m_last_hr_merge_time(0), m_hashes(0), m_total_hashes(0),
137137
m_do_print_hashrate(false), m_do_mining(false), m_current_hash_rate(0),
@@ -545,6 +545,15 @@ void miner::resume() {
545545
MDEBUG("MINING RESUMED");
546546
}
547547
//-----------------------------------------------------------------------------------------------------
548+
void miner::stop_mining_for(uint64_t seconds) {
549+
CRITICAL_REGION_LOCAL(m_miners_count_lock);
550+
MGINFO("Mining paused for "
551+
<< seconds << " seconds, since we mined the last diardi block");
552+
++m_pausers_count;
553+
misc_utils::sleep_no_w(seconds * 1000);
554+
--m_pausers_count;
555+
}
556+
//-----------------------------------------------------------------------------------------------------
548557
bool miner::worker_thread() {
549558
const uint32_t th_local_index =
550559
m_thread_index++; // atomically increment, getting value before increment
@@ -610,6 +619,10 @@ bool miner::worker_thread() {
610619
continue;
611620
}
612621

622+
if(m_last_mined == height) {
623+
continue;
624+
}
625+
613626
b.nonce = nonce;
614627
crypto::hash h;
615628

@@ -621,8 +634,8 @@ bool miner::worker_thread() {
621634
m_gbh(b, height, NULL, tools::get_max_concurrency(), h);
622635

623636
if (check_hash(h, local_diff)) {
624-
// we lucky!
625637
++m_config.current_extra_message_index;
638+
m_last_mined = height;
626639
MGINFO_GREEN("Found block " << get_block_hash(b) << " at height "
627640
<< height
628641
<< " for difficulty: " << local_diff);

src/cryptonote_basic/miner.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class miner {
8787
uint32_t get_threads_count() const;
8888
void send_stop_signal();
8989
bool stop();
90+
void stop_mining_for(uint64_t seconds);
9091
bool is_mining() const;
9192
const account_public_address &get_mining_address() const;
9293
bool on_idle();
@@ -149,6 +150,7 @@ class miner {
149150
std::atomic<uint32_t> m_starter_nonce;
150151
difficulty_type m_diffic;
151152
uint64_t m_height;
153+
uint64_t m_last_mined;
152154
std::atomic<uint32_t> m_thread_index;
153155
volatile uint32_t m_threads_total;
154156
std::atomic<uint32_t> m_threads_active;

src/cryptonote_basic/verification_context.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ struct tx_verification_context {
7575
struct block_verification_context {
7676
bool m_added_to_main_chain;
7777
bool m_verifivation_failed; // bad block, should drop connection
78+
bool m_last_diardi_mined;
7879
bool m_marked_as_orphaned;
7980
bool m_already_exists;
8081
bool m_partial_block_reward;

src/cryptonote_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@
191191
#define HF_VERSION_CLSAG 13
192192
#define HF_VERSION_DETERMINISTIC_UNLOCK_TIME 13
193193
#define HF_VERSION_BULLETPROOF_PLUS 15
194-
#define HF_VERSION_VIEW_TAGS 15
194+
#define HF_VERSION_VIEW_TAGS 17
195195
#define HF_VERSION_2021_SCALING 15
196196

197197
#define PER_KB_FEE_QUANTIZATION_DECIMALS 1

src/cryptonote_core/blockchain.cpp

Lines changed: 118 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -859,10 +859,10 @@ difficulty_type Blockchain::get_difficulty_for_next_block() {
859859
return (difficulty_type)120000000;
860860
}
861861

862-
// Stagenet will start with difficulty of 1 KH/s
862+
// Stagenet will start with difficulty of 33.3 H/s
863863
if (m_nettype == STAGENET && (uint64_t)height >= 20 &&
864864
(uint64_t)height <= 20 + (uint64_t)DIFFICULTY_BLOCKS_COUNT) {
865-
return (difficulty_type)40000;
865+
return (difficulty_type)4000;
866866
}
867867

868868
// Testnet will start with difficulty of 16 KH/s
@@ -1441,72 +1441,59 @@ bool Blockchain::validate_diardi_miner_v2(const block &b) {
14411441
b.miner_tx.vin[0].type() != typeid(txin_gen))
14421442
? m_db->height()
14431443
: boost::get<txin_gen>(b.miner_tx.vin[0]).height;
1444-
bool isDiardiBlock = (version >= 13 && block_height % 4 == 0);
14451444

1446-
if (!isDiardiBlock) {
1445+
if (!(version >= 13 && block_height % 4 == 0)) {
14471446
return true;
14481447
}
14491448

14501449
std::list<std::string> diardi_miners_list = diardi_addresses_v2(m_nettype);
1451-
1452-
std::string tM;
1453-
std::string dM;
14541450
std::string vM;
1451+
cryptonote::address_parse_info pVm;
14551452

1456-
cryptonote::address_parse_info temp_miner_address;
1457-
cryptonote::address_parse_info diardi_miner_address;
1458-
1459-
for (auto const &sM : diardi_miners_list) {
1460-
cryptonote::get_account_address_from_str(temp_miner_address, m_nettype, tM);
1461-
cryptonote::get_account_address_from_str(diardi_miner_address, m_nettype,
1462-
sM);
1453+
for(auto const& sM : diardi_miners_list) {
1454+
public_key pKey = boost::get<txout_to_key>(b.miner_tx.vout.back().target).key;
1455+
if(validate_diardi_reward_key(block_height, sM, 0, pKey, m_nettype)) {
1456+
vM = sM;
1457+
cryptonote::get_account_address_from_str(pVm, m_nettype, vM);
1458+
break;
1459+
}
1460+
}
14631461

1464-
if (temp_miner_address.address == diardi_miner_address.address) {
1465-
if (temp_miner_address.address.m_view_public_key ==
1466-
diardi_miner_address.address.m_view_public_key) {
1467-
crypto::hash sig_data = get_sig_data(block_height);
1468-
crypto::signature signature = b.signature;
1469-
crypto::public_key o_pspendkey =
1470-
diardi_miner_address.address.m_spend_public_key;
1462+
if (vM.empty()) {
1463+
return true;
1464+
}
14711465

1472-
vM = sM;
1466+
crypto::hash sig_data = get_sig_data(block_height);
1467+
crypto::signature signature = b.signature;
14731468

1474-
if (!crypto::check_signature(sig_data, o_pspendkey, signature)) {
1475-
LOG_PRINT_L1("Diardi: Miner Signature incorrect");
1476-
return false;
1477-
}
1469+
if (!crypto::check_signature(sig_data, pVm.address.m_spend_public_key, signature)) {
1470+
LOG_PRINT_L1("Diardi: Miner signature incorrect");
1471+
return false;
1472+
}
14781473

1479-
cryptonote::block oDb;
1480-
bool oOb = false;
1481-
bool getOldBlock = get_block_by_hash(
1474+
cryptonote::block oDb;
1475+
bool oOb = false;
1476+
bool getOldBlock = get_block_by_hash(
14821477
m_db->get_block_hash_from_height((block_height - 4)), oDb, &oOb);
14831478

1484-
if (!getOldBlock) {
1485-
LOG_PRINT_L1("Diardi: Could not get old block");
1486-
return false;
1487-
}
1488-
1489-
if (check_last_diardi_miner(this, vM, m_nettype)) {
1490-
LOG_PRINT_L1("Diardi: Miner already mined a block < 4 blocks ago");
1491-
return false;
1492-
}
1479+
if (!getOldBlock) {
1480+
LOG_PRINT_L1("Diardi: Could not get old block");
1481+
return false;
1482+
}
14931483

1494-
return true;
1495-
}
1496-
}
1484+
if (check_last_diardi_miner(this, vM, m_nettype)) {
1485+
LOG_PRINT_L1("Diardi: Miner already mined a block < 4 blocks ago");
1486+
return false;
14971487
}
14981488

1499-
LOG_PRINT_L1("Diardi: Miner not in list");
1500-
return false;
1489+
return true;
15011490
}
15021491
//------------------------------------------------------------------
15031492
// This function validates the miner transaction reward
1504-
bool Blockchain::validate_miner_transaction(const block &b,
1505-
size_t cumulative_block_weight,
1506-
uint64_t fee, uint64_t &base_reward,
1507-
uint64_t already_generated_coins,
1508-
bool &partial_block_reward,
1509-
uint8_t version) {
1493+
bool Blockchain::validate_miner_transaction(
1494+
const block &b, size_t cumulative_block_weight, uint64_t fee,
1495+
uint64_t &base_reward, uint64_t already_generated_coins,
1496+
bool &partial_block_reward, uint8_t version, bool &last_diardi_mined) {
15101497
LOG_PRINT_L3("Blockchain::" << __func__);
15111498
// validate reward
15121499
uint64_t money_in_use = 0;
@@ -1544,33 +1531,6 @@ bool Blockchain::validate_miner_transaction(const block &b,
15441531
return false;
15451532
}
15461533

1547-
if ((version >= 2) && (version <= 12) && (block_height >= 16)) {
1548-
std::string diardi_maintainer_address;
1549-
diardi_maintainer_address = diardi_index_to_reward_v1(block_height);
1550-
1551-
if (already_generated_coins != 0) {
1552-
uint64_t diardi_reward = get_diardi_reward(block_height, base_reward);
1553-
if (b.miner_tx.vout.back().amount != diardi_reward) {
1554-
MERROR("Diardi V1 reward amount incorrect. Should be: "
1555-
<< print_money(diardi_reward)
1556-
<< ", is: " << print_money(b.miner_tx.vout.back().amount));
1557-
return false;
1558-
}
1559-
1560-
if (m_nettype == cryptonote::MAINNET) {
1561-
if (!validate_diardi_reward_key(
1562-
block_height, diardi_maintainer_address,
1563-
b.miner_tx.vout.size() - 1,
1564-
boost::get<txout_to_key>(b.miner_tx.vout.back().target).key)) {
1565-
MERROR("Diardi V1 reward public key incorrect.");
1566-
return false;
1567-
}
1568-
} else {
1569-
return true;
1570-
}
1571-
}
1572-
}
1573-
15741534
if (base_reward + fee < money_in_use) {
15751535
MERROR_VER("coinbase transaction spend too much money ("
15761536
<< print_money(money_in_use) << "). Block reward is "
@@ -1601,6 +1561,73 @@ bool Blockchain::validate_miner_transaction(const block &b,
16011561
partial_block_reward = true;
16021562
base_reward = money_in_use - fee;
16031563
}
1564+
1565+
if ((version >= 2) && (version <= 12) && (block_height >= 16)) {
1566+
std::string diardi_maintainer_address;
1567+
diardi_maintainer_address = diardi_index_to_reward_v1(block_height);
1568+
1569+
if (already_generated_coins != 0) {
1570+
uint64_t diardi_reward = get_diardi_reward(block_height, base_reward);
1571+
if (b.miner_tx.vout.back().amount != diardi_reward) {
1572+
MERROR("Diardi V1 reward amount incorrect. Should be: "
1573+
<< print_money(diardi_reward)
1574+
<< ", is: " << print_money(b.miner_tx.vout.back().amount));
1575+
return false;
1576+
}
1577+
1578+
if (m_nettype == cryptonote::MAINNET) {
1579+
if (!validate_diardi_reward_key(
1580+
block_height, diardi_maintainer_address,
1581+
b.miner_tx.vout.size() - 1,
1582+
boost::get<txout_to_key>(b.miner_tx.vout.back().target).key)) {
1583+
MERROR("Diardi V1 reward public key incorrect.");
1584+
return false;
1585+
}
1586+
} else {
1587+
return true;
1588+
}
1589+
}
1590+
}
1591+
1592+
if (version >= HF_VERSION_DIARDI_V2 && m_db->height() % 4 == 0) {
1593+
std::string vM;
1594+
std::list<std::string> diardi_miners_list = diardi_addresses_v2(m_nettype);
1595+
cryptonote::address_parse_info diardi_miner_address;
1596+
1597+
for (auto const &sM : diardi_miners_list) {
1598+
if (validate_diardi_reward_key(
1599+
m_db->height(), sM, b.miner_tx.vout.size() - 1,
1600+
boost::get<txout_to_key>(b.miner_tx.vout.back().target).key,
1601+
m_nettype)) {
1602+
vM = sM;
1603+
break;
1604+
}
1605+
}
1606+
1607+
if (vM.empty()) {
1608+
MERROR("Diardi: V2 reward public key incorrect");
1609+
return false;
1610+
}
1611+
1612+
cryptonote::get_account_address_from_str(diardi_miner_address, m_nettype,
1613+
vM);
1614+
1615+
crypto::hash sig_data = get_sig_data(m_db->height());
1616+
crypto::signature signature = b.signature;
1617+
if (!crypto::check_signature(
1618+
sig_data, diardi_miner_address.address.m_spend_public_key,
1619+
signature)) {
1620+
MERROR("Diardi: Block signature incorrect");
1621+
return false;
1622+
}
1623+
1624+
if (check_last_diardi_miner(this, vM, m_nettype)) {
1625+
last_diardi_mined = true;
1626+
MERROR("Diardi: Cannot mine 2 diardi blocks in a row");
1627+
return false;
1628+
}
1629+
}
1630+
16041631
return true;
16051632
}
16061633
//------------------------------------------------------------------
@@ -4712,12 +4739,18 @@ bool Blockchain::handle_block_to_main_chain(const block &bl,
47124739
blockchain_height
47134740
? m_db->get_block_already_generated_coins(blockchain_height - 1)
47144741
: 0;
4715-
if (!validate_miner_transaction(bl, cumulative_block_weight, fee_summary,
4716-
base_reward, already_generated_coins,
4717-
bvc.m_partial_block_reward,
4718-
m_hardfork->get_current_version())) {
4719-
MERROR_VER("Block with id: " << id << " has incorrect miner transaction");
4720-
bvc.m_verifivation_failed = true;
4742+
bool last_diardi_miner = false;
4743+
if (!validate_miner_transaction(
4744+
bl, cumulative_block_weight, fee_summary, base_reward,
4745+
already_generated_coins, bvc.m_partial_block_reward,
4746+
m_hardfork->get_current_version(), last_diardi_miner)) {
4747+
if (last_diardi_miner) {
4748+
bvc.m_last_diardi_mined = true;
4749+
} else {
4750+
MERROR_VER("Block with id: " << id << " has incorrect miner transaction");
4751+
bvc.m_verifivation_failed = true;
4752+
}
4753+
47214754
return_tx_to_pool(txs);
47224755
goto leave;
47234756
}
@@ -5030,10 +5063,11 @@ bool Blockchain::add_new_block(const block &bl,
50305063
}
50315064

50325065
if (!validate_diardi_miner_v2(bl)) {
5033-
LOG_PRINT_L1("Diardi validation failed for block with id <" << id << ">");
5066+
LOG_PRINT_L1("Diardi: validation failed for block with id <" << id
5067+
<< ">");
50345068
bvc.m_added_to_main_chain = false;
50355069
m_blocks_txs_check.clear();
5036-
return true;
5070+
return false;
50375071
}
50385072

50395073
// check that block refers to chain tail

src/cryptonote_core/blockchain.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1654,7 +1654,8 @@ class Blockchain {
16541654
size_t cumulative_block_weight, uint64_t fee,
16551655
uint64_t &base_reward,
16561656
uint64_t already_generated_coins,
1657-
bool &partial_block_reward, uint8_t version);
1657+
bool &partial_block_reward, uint8_t version,
1658+
bool &last_diardi_mined);
16581659

16591660
/**
16601661
* @brief reverts the blockchain to its previous state following a failed

src/cryptonote_core/cryptonote_core.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,6 +1529,11 @@ bool core::handle_block_found(block &b, block_verification_context &bvc) {
15291529

15301530
CHECK_AND_ASSERT_MES(!bvc.m_verifivation_failed, false,
15311531
"mined block failed verification");
1532+
1533+
if (bvc.m_last_diardi_mined) {
1534+
m_miner.stop_mining_for(550);
1535+
}
1536+
15321537
if (bvc.m_added_to_main_chain) {
15331538
cryptonote_connection_context exclude_context = {};
15341539
NOTIFY_NEW_BLOCK::request arg = AUTO_VAL_INIT(arg);

src/cryptonote_core/cryptonote_tx_utils.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,11 @@ bool construct_miner_tx(size_t height, size_t median_weight,
449449
tk.key = out_eph_public_key;
450450

451451
tx_out out;
452+
uint64_t amount = out_amounts[no];
452453
summary_amounts += out.amount = out_amounts[no];
454+
453455
out.target = tk;
456+
454457
tx.vout.push_back(out);
455458

456459
if (hard_fork_version >= 2 && hard_fork_version <= 12 && (height >= 16)) {

src/cryptonote_core/tx_pool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
#define scala_DEFAULT_LOG_CATEGORY "txpool"
5959

6060
DISABLE_VS_WARNINGS(4244 4345 4503) //'boost::foreach_detail_::or_' : decorated
61-
//name length exceeded, name was truncated
61+
// name length exceeded, name was truncated
6262

6363
using namespace crypto;
6464

0 commit comments

Comments
 (0)