Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #77

Merged
merged 4 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Haven Prometheus v4.0.2
# Haven Prometheus v4.1.0

Copyright (c) 2018-2024 Haven.
Portions Copyright (c) 2014-2022 The Monero Project.
Expand Down
744 changes: 705 additions & 39 deletions contrib/depends/README.md

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions src/blockchain_db/lmdb/db_lmdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1105,8 +1105,11 @@ uint64_t BlockchainLMDB::add_transaction_data(const crypto::hash& blk_hash, cons
throw0(DB_ERROR("Failed to add tx circulating supply to db transaction: get_tx_asset_types fails."));
}

bool is_mint_and_burn_tx = (strSource != strDest);
bool is_burn_tx = (strSource == strDest) && tx.amount_burnt > 0;
// NEAC : check for presence of offshore TX to see if we need to update circulating supply information
if ((tx.version >= OFFSHORE_TRANSACTION_VERSION) && (strSource != strDest)) {
// Tay8NWWFKpz9JT4NXU0w: Also burn transactions affect the supply
if ((tx.version >= OFFSHORE_TRANSACTION_VERSION) && (is_mint_and_burn_tx || is_burn_tx)) {
// Offshore TX - update our records
circ_supply cs;
cs.tx_hash = tx_hash;
Expand Down Expand Up @@ -1216,7 +1219,10 @@ void BlockchainLMDB::remove_transaction_data(const crypto::hash& tx_hash, const
throw0(DB_ERROR("Failed to add tx circulating supply to db transaction: get_tx_asset_types fails."));
}

if ((tx.version >= OFFSHORE_TRANSACTION_VERSION) && (strSource != strDest))
bool is_mint_and_burn_tx = (strSource != strDest);
bool is_burn_tx = (strSource == strDest) && tx.amount_burnt > 0;

if ((tx.version >= OFFSHORE_TRANSACTION_VERSION) && (is_mint_and_burn_tx || is_burn_tx))
{
// Update the tally table
// Get the current tally value for the source currency type
Expand Down
6 changes: 3 additions & 3 deletions src/cryptonote_basic/difficulty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,11 @@ namespace cryptonote {

// To get an average solvetime to within +/- ~0.1%, use an adjustment factor.
// adjust=0.99 for 90 < N < 130
const double adjust = 0.998;
const long double adjust = 0.998;
// The divisor k normalizes LWMA.
const double k = N * (N + 1) / 2;
const long double k = N * (N + 1) / 2;

double LWMA(0), sum_inverse_D(0), harmonic_mean_D(0), nextDifficulty(0);
long double LWMA(0), sum_inverse_D(0), harmonic_mean_D(0), nextDifficulty(0);
int64_t solveTime(0);
uint64_t difficulty(0), next_difficulty(0);

Expand Down
13 changes: 13 additions & 0 deletions src/cryptonote_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
#define BLOCKS_SYNCHRONIZING_MAX_COUNT 2048 //must be a power of 2, greater than 128, equal to SEEDHASH_EPOCH_BLOCKS

#define CRYPTONOTE_MEMPOOL_TX_LIVETIME 86400 //seconds, one day
#define CRYPTONOTE_MEMPOOL_TX_CONVERSION_LIVETIME 3600 //seconds, one hour
#define CRYPTONOTE_MEMPOOL_TX_FROM_ALT_BLOCK_LIVETIME 604800 //seconds, one week


Expand Down Expand Up @@ -255,6 +256,15 @@
#define HF_VERSION_YIELD 23
#define HF_VERSION_CONVERSION_FEES_NOT_BURNT 23

// Haven v4.1 definitions
#define HF_VERSION_SLIPPAGE_V2 24
#define HF_VERSION_BURN 24
#define HF_VERSION_CONVERSION_FEES_NOT_BURNT_FINAL 24
#define HF_VERSION_OFFSHORE_FEES_V3 24
#define HF_VERSION_MAX_CONV_TRANSACTION_FEE 24
#define MAX_CONV_TRANSACTION_FEE ((uint64_t)10000000000000ull)


#define STAGENET_VERSION 0x0e
#define TESTNET_VERSION 0x1b

Expand All @@ -263,6 +273,9 @@
#define BURNT_CONVERSION_FEES_MINT_AMOUNT ((uint64_t)2580000000000000000ull)
#define BURNT_CONVERSION_FEES_MINT_HEIGHT ((uint64_t)1656720)

#define BURNT_CONVERSION_FEES_MINT_AMOUNT_FINAL ((uint64_t)511812000000000000ull)
#define BURNT_CONVERSION_FEES_MINT_HEIGHT_FINAL ((uint64_t)1692780)

#define PER_KB_FEE_QUANTIZATION_DECIMALS 8
#define CRYPTONOTE_SCALING_2021_FEE_ROUNDING_PLACES 2

Expand Down
38 changes: 32 additions & 6 deletions src/cryptonote_core/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1601,6 +1601,10 @@ bool Blockchain::validate_miner_transaction(
if ((version == HF_VERSION_CONVERSION_FEES_NOT_BURNT) && (m_db->height() == BURNT_CONVERSION_FEES_MINT_HEIGHT)) {
governance_reward += BURNT_CONVERSION_FEES_MINT_AMOUNT;
}

if ((version == HF_VERSION_CONVERSION_FEES_NOT_BURNT_FINAL) && (m_db->height() == BURNT_CONVERSION_FEES_MINT_HEIGHT_FINAL)) {
governance_reward += BURNT_CONVERSION_FEES_MINT_AMOUNT_FINAL;
}
// LAND AHOY!!!

if ((b.miner_tx.vout[1].amount != governance_reward) &&
Expand Down Expand Up @@ -1664,7 +1668,20 @@ bool Blockchain::validate_miner_transaction(
uint64_t miner_reward_xasset = fee_map[first_output_asset_type];
uint64_t governance_reward_xasset = get_governance_reward(m_db->height(), miner_reward_xasset);
miner_reward_xasset -= governance_reward_xasset;
governance_reward_xasset += offshore_fee_map[first_output_asset_type];
if (version >= HF_VERSION_OFFSHORE_FEES_V3) {
//split onshore/offshore fees 80% governance wallet, 20% miners
boost::multiprecision::uint128_t fee = offshore_fee_map[first_output_asset_type];
boost::multiprecision::uint128_t fee_miner_xasset = fee / 5;
fee -= fee_miner_xasset;
// 80%
governance_reward_xasset += fee.convert_to<uint64_t>();
// 20%
miner_reward_xasset += fee_miner_xasset.convert_to<uint64_t>();
} else
{
//Full conversion fee goes to the governance wallet before HF_VERSION_OFFSHORE_FEES_V3
governance_reward_xasset += offshore_fee_map[first_output_asset_type];
}

// xAsset fees change fork
if (version >= HF_VERSION_XASSET_FEES_V2) {
Expand Down Expand Up @@ -1724,10 +1741,14 @@ bool Blockchain::validate_miner_transaction(
// from hard fork 2, since a miner can claim less than the full block reward, we update the base_reward
// to show the amount of coins that were actually generated, the remainder will be pushed back for later
// emission. This modifies the emission curve very slightly.
bool is_burnt_fee_mint_block = ((version == HF_VERSION_CONVERSION_FEES_NOT_BURNT) && (m_db->height() == BURNT_CONVERSION_FEES_MINT_HEIGHT));
bool is_burnt_fee_mint_block_final = ((version == HF_VERSION_CONVERSION_FEES_NOT_BURNT_FINAL) && (m_db->height() == BURNT_CONVERSION_FEES_MINT_HEIGHT_FINAL));

if (version >= HF_VERSION_CONVERSION_FEES_NOT_BURNT) {
if ((version == HF_VERSION_CONVERSION_FEES_NOT_BURNT) && (m_db->height() == BURNT_CONVERSION_FEES_MINT_HEIGHT)) {
CHECK_AND_ASSERT_MES(money_in_use_map["XHV"] - fee_map["XHV"] - offshore_fee_map["XHV"] - xasset_fee_map["XHV"] - BURNT_CONVERSION_FEES_MINT_AMOUNT == base_reward, false, "base reward calculation bug when minting previously-burnt fees");
} else {
if (is_burnt_fee_mint_block || is_burnt_fee_mint_block_final ){
uint64_t minted_due_to_burned_fees_bug = is_burnt_fee_mint_block ? BURNT_CONVERSION_FEES_MINT_AMOUNT : BURNT_CONVERSION_FEES_MINT_AMOUNT_FINAL;
CHECK_AND_ASSERT_MES(money_in_use_map["XHV"] - fee_map["XHV"] - offshore_fee_map["XHV"] - xasset_fee_map["XHV"] - minted_due_to_burned_fees_bug == base_reward, false, "base reward calculation bug when minting previously-burnt fees");
} else {
// Additional logging
bool err = (money_in_use_map["XHV"] - fee_map["XHV"] - offshore_fee_map["XHV"] - xasset_fee_map["XHV"] != base_reward);
if (err) {
Expand Down Expand Up @@ -5426,12 +5447,17 @@ bool Blockchain::handle_block_to_main_chain(const block& bl, const crypto::hash&
}
}
} else {
//make sure those values are 0 for transfers.
if (tx.amount_burnt || tx.amount_minted) {
//make sure those values are 0 for transfers, unless it is a burn transaction
if ((tx.amount_burnt && hf_version<HF_VERSION_BURN)|| tx.amount_minted) {
LOG_PRINT_L2("error: Invalid Tx found. Amount burnt/mint > 0 for a transfer tx.");
bvc.m_verifivation_failed = true;
goto leave;
}
if ((hf_version >= HF_VERSION_BURN) && tx.amount_minted) {
LOG_PRINT_L2("error: Invalid Tx found. Amount mint > 0 for a transfer tx.");
bvc.m_verifivation_failed = true;
goto leave;
}
if (hf_version >= HF_VERSION_OFFSHORE_FEES_V2 && tx.pricing_record_height) {
LOG_PRINT_L2("error: Invalid Tx found. Tx pricing_record_height > 0 for a transfer tx.");
bvc.m_verifivation_failed = true;
Expand Down
116 changes: 103 additions & 13 deletions src/cryptonote_core/cryptonote_tx_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,13 @@ namespace cryptonote

// HERE BE DRAGONS!!!
// NEAC: mint the previously-burnt XHV conversion fees, and add to the governance wallet
if ((hard_fork_version == HF_VERSION_CONVERSION_FEES_NOT_BURNT) && (height == BURNT_CONVERSION_FEES_MINT_HEIGHT)) {
governance_reward += BURNT_CONVERSION_FEES_MINT_AMOUNT;

bool is_burnt_fee_mint_block = ((hard_fork_version == HF_VERSION_CONVERSION_FEES_NOT_BURNT) && (height == BURNT_CONVERSION_FEES_MINT_HEIGHT));
bool is_burnt_fee_mint_block_final = ((hard_fork_version == HF_VERSION_CONVERSION_FEES_NOT_BURNT_FINAL) && (height == BURNT_CONVERSION_FEES_MINT_HEIGHT_FINAL));

if (is_burnt_fee_mint_block || is_burnt_fee_mint_block_final ){
uint64_t minted_due_to_burned_fees_bug = is_burnt_fee_mint_block ? BURNT_CONVERSION_FEES_MINT_AMOUNT : BURNT_CONVERSION_FEES_MINT_AMOUNT_FINAL;
governance_reward += minted_due_to_burned_fees_bug;
}
// LAND AHOY!!!
}
Expand Down Expand Up @@ -306,9 +311,21 @@ namespace cryptonote

// Add the conversion fee to the governance payment (if provided)
if (offshore_fee_map[fee_map_entry.first] != 0) {
governance_reward_xasset += offshore_fee_map[fee_map_entry.first];
if (hard_fork_version >= HF_VERSION_OFFSHORE_FEES_V3) {
//split onshore/offshore fees 80% governance wallet, 20% miners
boost::multiprecision::uint128_t fee = offshore_fee_map[fee_map_entry.first];
boost::multiprecision::uint128_t fee_miner_xasset = fee / 5;
fee -= fee_miner_xasset;
// 80%
governance_reward_xasset += fee.convert_to<uint64_t>();
// 20%
block_reward_xasset += fee_miner_xasset.convert_to<uint64_t>();
} else
{
//Full conversion fee goes to the governance wallet before HF_VERSION_OFFSHORE_FEES_V3
governance_reward_xasset += offshore_fee_map[fee_map_entry.first];
}
}

// handle xasset converion fees
if (hard_fork_version >= HF_VERSION_XASSET_FEES_V2) {
if (xasset_fee_map[fee_map_entry.first] != 0) {
Expand Down Expand Up @@ -796,6 +813,7 @@ namespace cryptonote

// Calculate Mcap ratio slippage
cpp_bin_float_quad mcap_ratio_slippage = 0.0;
cpp_bin_float_quad mcap_ratio_onshore_addon_slippage = 0.0;
if (tx_type == tt::ONSHORE || tx_type == tt::OFFSHORE) {

// Calculate Mcap Ratio for XHV spot
Expand All @@ -808,8 +826,24 @@ namespace cryptonote
cpp_bin_float_quad mcr_max = (mcr_sp > mcr_ma) ? mcr_sp : mcr_ma;

// Calculate the Mcap ratio slippage
mcap_ratio_slippage = std::sqrt(std::pow(mcr_max.convert_to<double>(), 1.2)) / 6.0;
LOG_PRINT_L2("*** mcap_ratio_slippage = " << mcap_ratio_slippage.convert_to<double>());
mcap_ratio_slippage = (hf_version < HF_VERSION_SLIPPAGE_V2) ? std::sqrt(std::pow(mcr_max.convert_to<double>(), 1.2)) / 6.0 : std::sqrt(std::pow(mcr_max.convert_to<double>(), 1.7)) / 3.0;
//add-on for onshores, based on XHV price
if ((hf_version >= HF_VERSION_SLIPPAGE_V2) && (tx_type == tt::ONSHORE)) {

uint128_t mraon_numerator = map_amounts["XHV"];
uint128_t mraon_denominator = ((map_amounts["XUSD"] * pr.min("XHV"))/COIN)*100;
if ( mraon_denominator == 0 ){
LOG_ERROR("Invalid denominator (0) in calculation of mcap ratio onshore addon - aborting, XHV price is " << pr.min("XHV") << " XUSD supply is " << map_amounts["XUSD"] );
return false;
}
mcap_ratio_onshore_addon_slippage = mraon_numerator.convert_to<cpp_bin_float_quad>() / mraon_denominator.convert_to<cpp_bin_float_quad>();
}

LOG_PRINT_L2("*** original mcap_ratio_slippage = " << mcap_ratio_slippage.convert_to<double>());
LOG_PRINT_L2("*** mcap_ratio_onshore_addon_slippage = " << mcap_ratio_onshore_addon_slippage.convert_to<double>());
mcap_ratio_slippage+=mcap_ratio_onshore_addon_slippage;
LOG_PRINT_L2("*** final mcap_ratio_slippage (sum of original mcap_ratio_slippage and mcap_ratio_onshore_addon_slippage) = " << mcap_ratio_slippage.convert_to<double>());

}

// Calculate xUSD Peg Slippage
Expand All @@ -818,7 +852,7 @@ namespace cryptonote
xusd_peg_ratio /= COIN;

if (xusd_peg_ratio < 1.0) {
xusd_peg_slippage = std::sqrt(std::pow((1.0 - xusd_peg_ratio), 3.0)) / 1.3;
xusd_peg_slippage = (hf_version < HF_VERSION_SLIPPAGE_V2) ? std::sqrt(std::pow((1.0 - xusd_peg_ratio), 3.0)) / 1.3 : std::sqrt(std::pow((1.0 - xusd_peg_ratio), 2.5)) / 0.5;
LOG_PRINT_L2("*** xusd_peg_slippage = " << xusd_peg_slippage);
}

Expand Down Expand Up @@ -846,20 +880,61 @@ namespace cryptonote
(tx_type == tt::ONSHORE || tx_type == tt::OFFSHORE) ? basic_slippage + std::max(mcap_ratio_slippage, xusd_peg_slippage) :
(tx_type == tt::XUSD_TO_XASSET && dest_asset == "XBTC") ? basic_slippage + std::max(xbtc_mcap_ratio_slippage, xusd_peg_slippage) :
basic_slippage + xusd_peg_slippage;
LOG_PRINT_L1("total_slippage = " << total_slippage.convert_to<double>());
LOG_PRINT_L1("total_slippage (before rounding) = " << total_slippage.convert_to<double>());

// Limit total_slippage to 99% so that the code doesn't break
if (total_slippage > 0.99) total_slippage = 0.99;
total_slippage *= convert_amount.convert_to<cpp_bin_float_quad>();
slippage = total_slippage.convert_to<uint64_t>();
slippage -= (slippage % 100000000);

if (hf_version < HF_VERSION_SLIPPAGE_V2) {
if (total_slippage > 0.99) total_slippage = 0.99;
LOG_PRINT_L1("total_slippage (after rounding) = " << total_slippage.convert_to<double>());
total_slippage *= convert_amount.convert_to<cpp_bin_float_quad>();
slippage = total_slippage.convert_to<uint64_t>();
slippage -= (slippage % 100000000);
}
else {
if (total_slippage > 1) total_slippage = 1.00;
total_slippage=total_slippage*100.0;
//Make slippage a number in an discrete set of values - integers between 0 and 100
uint128_t slippage_rounded_numerator=total_slippage.convert_to<uint128_t>();
//Make slippage a number in an discrete set of values - integers multiples of 10 between 0 and 1000
slippage_rounded_numerator *= 10;
if (slippage_rounded_numerator == 0)
//Minimum slippage is 0.1%
slippage_rounded_numerator = 1;
if (slippage_rounded_numerator > 990)
//Maximum slippage is 99.9%
slippage_rounded_numerator = 999;
uint128_t slippage_rounded_denominator = 1000;
LOG_PRINT_L1("total_slippage (after rounding) = " << slippage_rounded_numerator<< "/1000");
uint128_t slippage_final_before_dust_rounding_128 = (convert_amount*slippage_rounded_numerator)/slippage_rounded_denominator;
uint64_t slippage_final_before_dust_rounding_64 = slippage_final_before_dust_rounding_128.convert_to<uint64_t>();
uint64_t amount_after_slippage_before_dust_rounding = 0;
if (slippage_final_before_dust_rounding_64 < amount)
amount_after_slippage_before_dust_rounding=amount-slippage_final_before_dust_rounding_64;
//If the amount after slippage is less than 0.0001, then fail
if (amount_after_slippage_before_dust_rounding<100000000) {
LOG_ERROR("The whole converted amount will be burnt through slippage - aborting");
return false;
}
//Round the slippage up, so that the remaining amount after slippage has only zeros after the 4 digit after the decimal point
//for example 1234.567800000000
slippage = slippage_final_before_dust_rounding_64+(amount_after_slippage_before_dust_rounding % 100000000);
}

LOG_PRINT_L1("final slippage amount = " << slippage);

// SAnity check that there is _some_ slippage being applied
if (slippage == 0) {
// Not a valid slippage amount
LOG_ERROR("Invalid slippage amount (0) - aborting");
return false;
}

if (slippage >= amount) {
// Not a valid slippage amount
LOG_ERROR("Slippage is not smaller than the converted amount - aborting");
return false;
}

return true;
}
Expand Down Expand Up @@ -1462,6 +1537,12 @@ namespace cryptonote
(tx_type == transaction_type::XUSD_TO_XASSET) ? get_xusd_to_xasset_fee(destinations, hf_version) :
(tx_type == transaction_type::XASSET_TO_XUSD) ? get_xasset_to_xusd_fee(destinations, hf_version) : 0;

uint64_t supply_burnt = 0;

for(const tx_destination_entry& dst_entr: destinations) {
supply_burnt += dst_entr.supply_burnt;
}

if (shuffle_outs)
{
std::shuffle(destinations.begin(), destinations.end(), crypto::random_device{});
Expand Down Expand Up @@ -1513,14 +1594,18 @@ namespace cryptonote
//fill outputs
size_t output_index = 0;
uint64_t summary_outs_slippage = 0;
uint64_t summary_outs_supply_burn = 0;

for(const tx_destination_entry& dst_entr: destinations)
{
CHECK_AND_ASSERT_MES(dst_entr.dest_amount > 0 || tx.version > 1, false, "Destination with wrong amount: " << dst_entr.dest_amount);
CHECK_AND_ASSERT_MES(dst_entr.supply_burnt == 0 || hf_version >= HF_VERSION_BURN, false, "Burn transaction before Haven 4.1");
crypto::public_key out_eph_public_key;
crypto::view_tag view_tag;

// Sum all the slippage across the outputs
summary_outs_slippage += dst_entr.slippage;
summary_outs_supply_burn += dst_entr.supply_burnt;

hwdev.generate_output_ephemeral_keys(tx.version,sender_account_keys, txkey_pub, tx_key,
dst_entr, change_addr, output_index,
Expand Down Expand Up @@ -1562,8 +1647,13 @@ namespace cryptonote
tx.amount_minted += dst_entr.dest_amount;
tx.amount_burnt += dst_entr.amount + dst_entr.slippage;
}
} else {
tx.amount_burnt += dst_entr.supply_burnt;
}
}



CHECK_AND_ASSERT_MES(additional_tx_public_keys.size() == additional_tx_keys.size(), false, "Internal error creating additional public keys");

remove_field_from_tx_extra(tx.extra, typeid(tx_extra_additional_pub_keys));
Expand Down Expand Up @@ -1771,7 +1861,7 @@ namespace cryptonote
if (!use_simple_rct && amount_in > amount_out)
outamounts.push_back(amount_in - amount_out);
else
fee = summary_inputs_money - summary_outs_money - offshore_fee;
fee = summary_inputs_money - summary_outs_money - offshore_fee - supply_burnt;

// since the col ins are added to the summary_inputs_money above for offshores, subtract it.
if (tx_type == transaction_type::OFFSHORE && hf_version >= HF_VERSION_USE_COLLATERAL) {
Expand Down
Loading
Loading