Skip to content

Commit

Permalink
Merge pull request Beldex-Coin#125 from victor-tucci/decomission-cred…
Browse files Browse the repository at this point in the history
…it-calc

Decomission credit calc
  • Loading branch information
sanada08 authored Sep 28, 2023
2 parents f25df80 + 154150c commit 5c3acd6
Show file tree
Hide file tree
Showing 31 changed files with 216 additions and 274 deletions.
4 changes: 2 additions & 2 deletions src/blockchain_db/lmdb/db_lmdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4702,13 +4702,13 @@ void BlockchainLMDB::fixup(cryptonote::network_type nettype)
if (is_hard_fork_at_least(nettype, cryptonote::network_version_17_POS, curr_height)
&& block_header_has_POS_components(get_block_header_from_height(curr_height)))
{
diff = POS_FIXED_DIFFICULTY; // TARGET_BLOCK_TIME_V17
diff = POS_FIXED_DIFFICULTY; // TARGET_BLOCK_TIME
}
else
{
diff = next_difficulty_v2(timestamps,
difficulties,
tools::to_seconds(TARGET_BLOCK_TIME), //use OLD BLOCK_TIME
tools::to_seconds(TARGET_BLOCK_TIME_OLD), //use OLD BLOCK_TIME
difficulty_mode(nettype, curr_height + 1));
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/cryptonote_basic/cryptonote_basic_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,22 +119,22 @@ namespace cryptonote {
return result;
}

bool get_base_block_reward(size_t median_weight, size_t current_block_weight, uint64_t already_generated_coins, uint64_t &reward, uint64_t &reward_unpenalized, uint8_t version, uint64_t height) {

bool get_base_block_reward(size_t median_weight, size_t current_block_weight, uint64_t already_generated_coins, uint64_t &reward, uint64_t &reward_unpenalized, uint8_t version, uint64_t height)
{
//premine reward
if (height == 1)
{
reward = 1400000000000000000;
return true;
}

if((height>=56500) && (version<network_version_17_POS))
{
reward = COIN * 2;
return true;
}
static_assert(TARGET_BLOCK_TIME % 1 == 0s, "difficulty targets must be a multiple of 60");
static_assert(TARGET_BLOCK_TIME_V17 % 1 == 0s, "difficulty targets must be a multiple of 60");
if((height>=56500) && (version<network_version_17_POS))
{
reward = COIN * 2;
return true;
}
static_assert(TARGET_BLOCK_TIME_OLD % 1 == 0s, "difficulty targets must be a multiple of 60");
static_assert(TARGET_BLOCK_TIME % 1 == 0s, "difficulty targets must be a multiple of 60");

uint64_t base_reward =
version >= network_version_17_POS ? BLOCK_REWARD_HF17_POS :
Expand Down
33 changes: 13 additions & 20 deletions src/cryptonote_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,14 @@ static_assert(STAKING_PORTIONS % 12 == 0, "Use a multiple of twelve, so that it

#define DIFFICULTY_TARGET_V2 120 // seconds
#define DIFFICULTY_TARGET_V1 60 // seconds - before first fork
constexpr auto TARGET_BLOCK_TIME = 2min;
constexpr auto TARGET_BLOCK_TIME_V17 = 30s;
constexpr auto TARGET_BLOCK_TIME_OLD = 2min;
constexpr uint64_t BLOCKS_PER_HOUR_OLD = 1h / TARGET_BLOCK_TIME_OLD;
constexpr uint64_t BLOCKS_PER_DAY_OLD = 24h / TARGET_BLOCK_TIME_OLD;

constexpr auto TARGET_BLOCK_TIME = 30s;
constexpr uint64_t BLOCKS_PER_HOUR = 1h / TARGET_BLOCK_TIME;
constexpr uint64_t BLOCKS_PER_DAY = 24h / TARGET_BLOCK_TIME;

constexpr uint64_t DIFFICULTY_WINDOW = 59;
constexpr uint64_t DIFFICULTY_BLOCKS_COUNT(bool before_hf16)
{
Expand All @@ -109,8 +115,8 @@ constexpr uint64_t DIFFICULTY_BLOCKS_COUNT(bool before_hf16)



#define CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_SECONDS_V2 TARGET_BLOCK_TIME * CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_BLOCKS
#define CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_SECONDS_V3 TARGET_BLOCK_TIME_V17 * CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_BLOCKS
#define CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_SECONDS_V2 TARGET_BLOCK_TIME_OLD * CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_BLOCKS
#define CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_SECONDS_V3 TARGET_BLOCK_TIME * CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_BLOCKS
#define CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_BLOCKS 1


Expand Down Expand Up @@ -192,9 +198,6 @@ constexpr uint64_t DIFFICULTY_BLOCKS_COUNT(bool before_hf16)

#define HASH_OF_HASHES_STEP 256

#define VOTE_LIFETIME_HOURS 2
#define MINIMUM_CREDIT_HOURS 2

#define DEFAULT_TXPOOL_MAX_WEIGHT 648000000ull // 3 days at 300000, in bytes

#define BULLETPROOF_MAX_OUTPUTS 16
Expand All @@ -204,10 +207,6 @@ constexpr uint64_t DIFFICULTY_BLOCKS_COUNT(bool before_hf16)
#define CRYPTONOTE_PRUNING_TIP_BLOCKS 5500 // the smaller, the more space saved
//#define CRYPTONOTE_PRUNING_DEBUG_SPOOF_SEED

constexpr uint64_t PRE_POS_BLOCKS_EXPECTED_IN_HOURS(int hours) { return (1h / TARGET_BLOCK_TIME) * hours; }
constexpr uint64_t PRE_POS_BLOCKS_EXPECTED_IN_DAYS(int days) { return PRE_POS_BLOCKS_EXPECTED_IN_HOURS(24) * days; }
constexpr uint64_t PRE_POS_BLOCKS_EXPECTED_IN_YEARS(int years) { return PRE_POS_BLOCKS_EXPECTED_IN_DAYS(365) * years; }

// New constants are intended to go here
namespace config
{
Expand Down Expand Up @@ -243,7 +242,7 @@ namespace config
inline constexpr std::string_view GENESIS_TX = "013c01ff0005978c390224a302c019c844f7141f35bf7f0fc5b02ada055e4ba897557b17ac6ccf88f0a2c09fab030276d443549feee11fe325048eeea083fcb7535312572d255ede1ecb58f84253b480e89226023b7d7c5e6eff4da699393abf12b6e3d04eae7909ae21932520fb3166b8575bb180cab5ee0102e93beb645ce7d5574d6a5ed5d9b8aadec7368342d08a7ca7b342a428353a10df80e497d01202b6e6844c1e9a478d0e4f7f34e455b26077a51f0005357aa19a49ca16eb373f622101f7c2a3a2ed7011b61998b1cd4f45b4d3c1daaa82908a10ca191342297eef1cf8"sv;
inline constexpr uint32_t GENESIS_NONCE = 11011;

inline constexpr uint64_t GOVERNANCE_REWARD_INTERVAL_IN_BLOCKS = PRE_POS_BLOCKS_EXPECTED_IN_DAYS(7);//Governance added from V17
inline constexpr uint64_t GOVERNANCE_REWARD_INTERVAL_IN_BLOCKS = 7 * BLOCKS_PER_DAY_OLD;//Governance added from V17
inline constexpr std::array GOVERNANCE_WALLET_ADDRESS =
{
"bxcguQiBhYaDW5wAdPLSwRHA6saX1nCEYUF89SPKZfBY1BENdLQWjti59aEtAEgrVZjnCJEVFoCDrG1DCoz2HeeN2pxhxL9xa"sv, // <V17
Expand Down Expand Up @@ -273,7 +272,7 @@ namespace config
namespace testnet
{
inline constexpr uint64_t HEIGHT_ESTIMATE_HEIGHT = 169960;
inline constexpr uint64_t BNS_VALIDATION_HEIGHT = 992219;
inline constexpr uint64_t BNS_VALIDATION_HEIGHT = 1028065;
inline constexpr time_t HEIGHT_ESTIMATE_TIMESTAMP = 1668622463;
inline constexpr uint64_t CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX = 53;
inline constexpr uint64_t CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX = 54;
Expand Down Expand Up @@ -317,7 +316,7 @@ namespace config
inline constexpr std::string_view GENESIS_TX = "023c01ff0001d7c1c4e81402a25ba172ed7bca3b35e0be2f097b743973cf3c26777342032bed1036b19ab7a4420145706ec71eec5d57962c225b0615c172f8429984ec4954ba8b05bdad3f454f0472000000000000000000000000000000000000000000000000000000000000000000"sv;
inline constexpr uint32_t GENESIS_NONCE = 11013;

inline constexpr uint64_t GOVERNANCE_REWARD_INTERVAL_IN_BLOCKS = PRE_POS_BLOCKS_EXPECTED_IN_DAYS(7);//governance added from V17
inline constexpr uint64_t GOVERNANCE_REWARD_INTERVAL_IN_BLOCKS = 7 * BLOCKS_PER_DAY_OLD;//governance added from V17
inline constexpr std::array GOVERNANCE_WALLET_ADDRESS =
{
"59XZKiAFwAKVyWN1CuuyFqMTTFLu9PEjpb3WhXfVuStgdoCZM1MtyJ2C41qijqfbdnY844F3boaW29geb8pT3mfrV9QQSRB"sv, // hardfork v7-9
Expand Down Expand Up @@ -512,9 +511,3 @@ namespace cryptonote
}
}
}

constexpr uint64_t BLOCKS_EXPECTED_IN_HOURS(int hours, uint8_t hf_version) { return (1h / (hf_version>=cryptonote::network_version_17_POS?TARGET_BLOCK_TIME_V17:TARGET_BLOCK_TIME)) * hours; }
constexpr uint64_t BLOCKS_EXPECTED_IN_DAYS(int days, uint8_t hf_version=17) { return BLOCKS_EXPECTED_IN_HOURS(24,hf_version) * days; }
constexpr uint64_t BLOCKS_EXPECTED_IN_YEARS(int years, uint8_t hf_version) { return BLOCKS_EXPECTED_IN_DAYS(365,hf_version) * years; }

constexpr uint64_t BLOCKS_PER_CREDIT_EARNED = 30;
50 changes: 20 additions & 30 deletions src/cryptonote_core/beldex_name_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,28 +645,22 @@ std::vector<mapping_type> all_mapping_types(uint8_t hf_version) {
return result;
}

std::optional<uint64_t> expiry_blocks(cryptonote::network_type nettype, mapping_years map_years, uint8_t hf_version)
std::optional<uint64_t> expiry_blocks(cryptonote::network_type nettype, mapping_years map_years)
{
std::optional<uint64_t> result;

// For testnet we shorten 1-, 2-, and 5-year renewals to 1/2/5 days with 1-day renewal.
const bool testnet_short = nettype == cryptonote::TESTNET;

if (map_years == mapping_years::bns_1year)
result = BLOCKS_EXPECTED_IN_DAYS(1 * REGISTRATION_YEAR_DAYS,hf_version);
else if (map_years == mapping_years::bns_2years)
result = BLOCKS_EXPECTED_IN_DAYS(2 * REGISTRATION_YEAR_DAYS,hf_version);
else if (map_years == mapping_years::bns_5years)
result = BLOCKS_EXPECTED_IN_DAYS(5 * REGISTRATION_YEAR_DAYS,hf_version);
else if (map_years == mapping_years::bns_10years)
result = BLOCKS_EXPECTED_IN_DAYS(10 * REGISTRATION_YEAR_DAYS,hf_version);

assert(result);

if (testnet_short)
result = BLOCKS_PER_DAY * REGISTRATION_YEAR_DAYS * (
map_years == mapping_years::bns_1year ? 1 :
map_years == mapping_years::bns_2years ? 2 :
map_years == mapping_years::bns_5years ? 5 :
map_years == mapping_years::bns_10years ? 10 : 0);

assert(result && *result);

if (nettype == cryptonote::TESTNET) // For testnet we shorten 1/2/5/10 years renewals to 1/2/5 days.
*result /= REGISTRATION_YEAR_DAYS;
else if (nettype == cryptonote::FAKECHAIN) // For fakenet testing we shorten 1/2/5/10 years to 2/4/10/20 blocks
*result /= (BLOCKS_EXPECTED_IN_DAYS(((REGISTRATION_YEAR_DAYS) / 2),hf_version));
*result /= (BLOCKS_PER_DAY * REGISTRATION_YEAR_DAYS / 2);

return result;
}
Expand Down Expand Up @@ -1301,15 +1295,11 @@ bool validate_mapping_type(std::string_view mapping_type_str, uint8_t hf_version
std::optional<bns::mapping_type> mapping_type_;
if (tools::string_iequal(mapping, "bchat"))
mapping_type_ = bns::mapping_type::bchat;
else if (hf_version >= cryptonote::network_version_17_POS)
{
if (tools::string_iequal(mapping, "belnet"))
else if (tools::string_iequal(mapping, "belnet"))
mapping_type_ = bns::mapping_type::belnet;
else if (tools::string_iequal(mapping, "wallet"))
else if (tools::string_iequal(mapping, "wallet"))
mapping_type_ = bns::mapping_type::wallet;
}

if (!mapping_type_)
else
{
if (reason) *reason = "Unsupported BNS type \"" + std::string{mapping_type_str} + "\"; supported types are: bchat, belnet, wallet";
return false;
Expand Down Expand Up @@ -1952,7 +1942,7 @@ std::optional<int64_t> add_or_get_owner_id(bns::name_system_db &bns_db, crypto::
// Build a query and bind values that will create a new row at the given height by copying the
// current highest-height row values and/or updating the given update fields.
using update_variant = std::variant<uint16_t, int64_t, uint64_t, blob_view, std::string>;
std::pair<std::string, std::vector<update_variant>> update_record_query(name_system_db& bns_db, uint64_t height, const cryptonote::tx_extra_beldex_name_system& entry, const crypto::hash& tx_hash,int8_t hf_version)
std::pair<std::string, std::vector<update_variant>> update_record_query(name_system_db& bns_db, uint64_t height, const cryptonote::tx_extra_beldex_name_system& entry, const crypto::hash& tx_hash)
{
assert(entry.is_updating() || entry.is_renewing());

Expand All @@ -1972,7 +1962,7 @@ SELECT name_hash, ?, ?)";
if (entry.is_renewing())
{
sql += ", expiration_height + ?, owner_id, backup_owner_id, encrypted_bchat_value, encrypted_wallet_value, encrypted_belnet_value";
bind.emplace_back(expiry_blocks(bns_db.network_type(), entry.mapping_years, hf_version).value_or(0));
bind.emplace_back(expiry_blocks(bns_db.network_type(), entry.mapping_years).value_or(0));
}
else
{
Expand Down Expand Up @@ -2040,7 +2030,7 @@ SELECT name_hash, ?, ?)";
return result;
}

bool add_bns_entry(bns::name_system_db &bns_db, uint64_t height, cryptonote::tx_extra_beldex_name_system const &entry, crypto::hash const &tx_hash,uint8_t hf_version)
bool add_bns_entry(bns::name_system_db &bns_db, uint64_t height, cryptonote::tx_extra_beldex_name_system const &entry, crypto::hash const &tx_hash)
{
// -----------------------------------------------------------------------------------------------
// New Mapping Insert or Completely Replace
Expand All @@ -2067,7 +2057,7 @@ bool add_bns_entry(bns::name_system_db &bns_db, uint64_t height, cryptonote::tx_
}
}

auto expiry = expiry_blocks(bns_db.network_type(), entry.mapping_years, hf_version);
auto expiry = expiry_blocks(bns_db.network_type(), entry.mapping_years);
if (expiry) *expiry += height;
if (!bns_db.save_mapping(tx_hash, entry, height, expiry, *owner_id, backup_owner_id))
{
Expand All @@ -2080,7 +2070,7 @@ bool add_bns_entry(bns::name_system_db &bns_db, uint64_t height, cryptonote::tx_
// -----------------------------------------------------------------------------------------------
else
{
auto [sql, bind] = update_record_query(bns_db, height, entry, tx_hash,hf_version);
auto [sql, bind] = update_record_query(bns_db, height, entry, tx_hash);

if (sql.empty())
return false; // already MERROR'd
Expand Down Expand Up @@ -2133,7 +2123,7 @@ bool name_system_db::add_block(const cryptonote::block &block, const std::vector
}

crypto::hash const &tx_hash = cryptonote::get_transaction_hash(tx);
if (!add_bns_entry(*this, height, entry, tx_hash,block.major_version))
if (!add_bns_entry(*this, height, entry, tx_hash))
return false;

bns_parsed_from_block = true;
Expand Down
2 changes: 1 addition & 1 deletion src/cryptonote_core/beldex_name_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ constexpr std::string_view db_mapping_value(bns::mapping_type type) {
}
}
// Returns the length of the given mapping type, in blocks, or std::nullopt if the mapping type never expires.
std::optional<uint64_t> expiry_blocks(cryptonote::network_type nettype, mapping_years map_years, uint8_t hf_version);
std::optional<uint64_t> expiry_blocks(cryptonote::network_type nettype, mapping_years map_years);

// Returns *the* proper representation of a name_hash for querying the database, which is 44 base64
// characters (43 significant chars + a padding '='). External input values should always get
Expand Down
8 changes: 4 additions & 4 deletions src/cryptonote_core/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ void Blockchain::pop_blocks(uint64_t nblocks)

bool stop_batch = m_db->batch_start();
uint8_t hf_version = get_network_version();
uint64_t blocks_expected_in_hours = BLOCKS_EXPECTED_IN_HOURS(24,hf_version);
uint64_t blocks_expected_per_day = (hf_version>=cryptonote::network_version_17_POS) ? BLOCKS_PER_DAY : BLOCKS_PER_DAY_OLD ;
try
{
const uint64_t blockchain_height = m_db->height();
Expand All @@ -663,7 +663,7 @@ void Blockchain::pop_blocks(uint64_t nblocks)
tools::PerformanceTimer timer;
for (int progress = 0; i < nblocks; ++i)
{
if (nblocks >= blocks_expected_in_hours && (i != 0 && (i % blocks_per_update == 0)))
if (nblocks >= blocks_expected_per_day && (i != 0 && (i % blocks_per_update == 0)))
{
MGINFO("... popping blocks " << (++progress * PERCENT_PER_PROGRESS_UPDATE) << "% completed, height: " << (blockchain_height - i) << " (" << timer.seconds() << "s)");
timer.reset();
Expand Down Expand Up @@ -954,7 +954,7 @@ difficulty_type Blockchain::get_difficulty_for_next_block(bool POS)
m_nettype, m_cache.m_timestamps, m_cache.m_difficulties, chain_height, m_cache.m_timestamps_and_difficulties_height);
uint64_t diff = next_difficulty_v2(m_cache.m_timestamps,
m_cache.m_difficulties,
tools::to_seconds((hf_version>=cryptonote::network_version_17_POS?TARGET_BLOCK_TIME_V17:TARGET_BLOCK_TIME)),
tools::to_seconds((hf_version>=cryptonote::network_version_17_POS?TARGET_BLOCK_TIME:TARGET_BLOCK_TIME_OLD)),
difficulty_mode(m_nettype, chain_height));

m_cache.m_timestamps_and_difficulties_height = chain_height;
Expand Down Expand Up @@ -1219,7 +1219,7 @@ difficulty_type Blockchain::get_difficulty_for_alternative_chain(const std::list
auto hf_version = cryptonote::get_network_version(m_nettype, height);
return next_difficulty_v2(timestamps,
cumulative_difficulties,
tools::to_seconds((hf_version>=cryptonote::network_version_17_POS?TARGET_BLOCK_TIME_V17:TARGET_BLOCK_TIME)),
tools::to_seconds((hf_version>=cryptonote::network_version_17_POS?TARGET_BLOCK_TIME:TARGET_BLOCK_TIME_OLD)),
difficulty_mode(m_nettype, height));
}
//------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions src/cryptonote_core/cryptonote_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2474,7 +2474,7 @@ namespace cryptonote
const auto hf_version = get_network_version(m_nettype, m_target_blockchain_height);


static double threshold = 1. / ((24h * 10) / (hf_version>=cryptonote::network_version_17_POS?TARGET_BLOCK_TIME_V17:TARGET_BLOCK_TIME)); // one false positive every 10 days
static double threshold = 1. / ((24h * 10) / (hf_version>=cryptonote::network_version_17_POS?TARGET_BLOCK_TIME:TARGET_BLOCK_TIME_OLD)); // one false positive every 10 days
static constexpr unsigned int max_blocks_checked = 150;

const time_t now = time(NULL);
Expand All @@ -2486,7 +2486,7 @@ namespace cryptonote
unsigned int b = 0;
const time_t time_boundary = now - static_cast<time_t>(seconds[n]);
for (time_t ts: timestamps) b += ts >= time_boundary;
const double p = probability(b, seconds[n] / tools::to_seconds((hf_version>=cryptonote::network_version_17_POS?TARGET_BLOCK_TIME_V17:TARGET_BLOCK_TIME)));
const double p = probability(b, seconds[n] / tools::to_seconds((hf_version>=cryptonote::network_version_17_POS?TARGET_BLOCK_TIME:TARGET_BLOCK_TIME_OLD)));
MDEBUG("blocks in the last " << seconds[n] / 60 << " minutes: " << b << " (probability " << p << ")");
if (p < threshold)
{
Expand Down
Loading

0 comments on commit 5c3acd6

Please sign in to comment.