Skip to content

Commit

Permalink
Merge pull request #120 from Beldex-Coin/bns-rework
Browse files Browse the repository at this point in the history
BNS refactoring
  • Loading branch information
sanada08 authored Aug 30, 2023
2 parents 8c92c60 + 83d379d commit fe4024c
Show file tree
Hide file tree
Showing 33 changed files with 1,575 additions and 1,231 deletions.
49 changes: 34 additions & 15 deletions src/beldex_economy.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,46 +58,65 @@ enum struct mapping_type : uint16_t
{
bchat = 0,
wallet = 1,
belnet = 2, // the type value stored in the database; counts as 1-year when used in a buy tx.
belnet = 2,
belnet_2years,
belnet_5years,
belnet_10years,
_count,
update_record_internal,
};

constexpr bool is_belnet_type(mapping_type t) { return t >= mapping_type::belnet && t <= mapping_type::belnet_10years; }
enum struct mapping_years : uint16_t
{
bns_1year =0,
bns_2years =1,
bns_5years =2,
bns_10years,
_count,
update_owner_record,
update_record_internal,
};

constexpr bool is_renewal_type(mapping_years y) { return y >= mapping_years::bns_1year && y <= mapping_years::bns_10years; }

// How many days we add per "year" of BNS belnet registration. We slightly extend this to the 368
// days per registration "year" to allow for some blockchain time drift + leap years.
constexpr uint64_t REGISTRATION_YEAR_DAYS = 368;

constexpr uint64_t burn_needed(uint8_t hf_version, mapping_type type)
constexpr uint64_t burn_needed(uint8_t hf_version, mapping_years map_years)
{
uint64_t result = 0;

// The base amount for bchat/wallet/belnet-1year:
const uint64_t basic_fee = (
hf_version >= 16 ? 15*COIN : // cryptonote::network_version_16_POS -- but don't want to add cryptonote_config.h include
20*COIN // cryptonote::network_version_15_bns
const uint64_t basic_fee = (hf_version >= 18 ? 500 * COIN : // cryptonote::network_version_18_bns -- but don't want to add cryptonote_config.h include
15 * COIN // cryptonote::network_version_17_POS
);
switch (type)

switch (map_years)
{
case mapping_type::update_record_internal:
case mapping_years::update_record_internal:
result = 0;
break;

case mapping_type::belnet: /* FALLTHRU */
case mapping_type::bchat: /* FALLTHRU */
case mapping_type::wallet: /* FALLTHRU */
case mapping_years::update_owner_record:
result = basic_fee * 10/100 ; // 10% from the basic fee
break;

case mapping_years::bns_1year: /* FALLTHRU */
default:
result = basic_fee;
break;

case mapping_type::belnet_2years: result = 2 * basic_fee; break;
case mapping_type::belnet_5years: result = 4 * basic_fee; break;
case mapping_type::belnet_10years: result = 6 * basic_fee; break;
case mapping_years::bns_2years:
result = 2 * basic_fee;
break;
case mapping_years::bns_5years:
result = 4 * basic_fee;
break;
case mapping_years::bns_10years:
result = 8 * basic_fee;
break;
}

return result;
}
}; // namespace bns
Expand Down
2 changes: 1 addition & 1 deletion src/cryptonote_basic/cryptonote_basic.h
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ namespace cryptonote
constexpr txtype transaction_prefix::get_max_type_for_hf(uint8_t hf_version)
{
txtype result = txtype::standard;
if (hf_version >= network_version_16_bns) result = txtype::beldex_name_system;
if (hf_version >= network_version_16) result = txtype::beldex_name_system;
else if (hf_version >= network_version_15_flash) result = txtype::stake;
else if (hf_version >= network_version_11_infinite_staking) result = txtype::key_image_unlock;
else if (hf_version >= network_version_9_master_nodes) result = txtype::state_change;
Expand Down
2 changes: 1 addition & 1 deletion src/cryptonote_basic/cryptonote_basic_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ namespace cryptonote {

uint64_t base_reward =
version >= network_version_17_POS ? BLOCK_REWARD_HF17_POS :
version >= network_version_16_bns ? BLOCK_REWARD_HF16 :
version >= network_version_16 ? BLOCK_REWARD_HF16 :
block_reward_unpenalized_formula_v7(version, already_generated_coins, height);

uint64_t full_reward_zone = get_min_block_weight(version);
Expand Down
67 changes: 53 additions & 14 deletions src/cryptonote_basic/tx_extra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ namespace cryptonote {
tx_extra_beldex_name_system tx_extra_beldex_name_system::make_buy(
bns::generic_owner const& owner,
bns::generic_owner const* backup_owner,
bns::mapping_type type,
bns::mapping_years mapping_years,
const crypto::hash& name_hash,
const std::string& encrypted_value,
const std::string& encrypted_bchat_value,
const std::string& encrypted_wallet_value,
const std::string& encrypted_belnet_value,
const crypto::hash& prev_txid)
{
tx_extra_beldex_name_system result{};
result.version = 1;
result.fields = bns::extra_field::buy;
result.owner = owner;

Expand All @@ -19,45 +22,81 @@ tx_extra_beldex_name_system tx_extra_beldex_name_system::make_buy(
else
result.fields = bns::extra_field::buy_no_backup;

result.type = type;
result.mapping_years = mapping_years;
result.name_hash = name_hash;
result.encrypted_value = encrypted_value;

if (encrypted_bchat_value.size())
{
result.fields |= bns::extra_field::encrypted_bchat_value;
result.encrypted_bchat_value = encrypted_bchat_value;
}

if (encrypted_wallet_value.size())
{
result.fields |= bns::extra_field::encrypted_wallet_value;
result.encrypted_wallet_value = encrypted_wallet_value;
}

if (encrypted_belnet_value.size())
{
result.fields |= bns::extra_field::encrypted_belnet_value;
result.encrypted_belnet_value = encrypted_belnet_value;
}

result.prev_txid = prev_txid;
return result;
}

tx_extra_beldex_name_system tx_extra_beldex_name_system::make_renew(
bns::mapping_type type, crypto::hash const &name_hash, crypto::hash const &prev_txid)
const bns::generic_signature& signature,
bns::mapping_years mapping_years,
crypto::hash const &name_hash,
crypto::hash const &prev_txid)
{
assert(is_belnet_type(type) && prev_txid);
assert(is_renewal_type(mapping_years) && prev_txid);

tx_extra_beldex_name_system result{};
result.fields = bns::extra_field::none;
result.type = type;
result.version = 1;
result.fields = bns::extra_field::signature;
result.signature = signature;
result.mapping_years=mapping_years;
result.name_hash = name_hash;
result.prev_txid = prev_txid;
return result;
}

tx_extra_beldex_name_system tx_extra_beldex_name_system::make_update(
const bns::generic_signature& signature,
bns::mapping_type type,
const crypto::hash& name_hash,
std::string_view encrypted_value,
std::string_view encrypted_bchat_value,
std::string_view encrypted_wallet_value,
std::string_view encrypted_belnet_value,
const bns::generic_owner* owner,
const bns::generic_owner* backup_owner,
const crypto::hash& prev_txid)
{
tx_extra_beldex_name_system result{};
result.version = 1;
result.signature = signature;
result.type = type;
result.name_hash = name_hash;
result.fields |= bns::extra_field::signature;

if (encrypted_value.size())
if (encrypted_bchat_value.size())
{
result.fields |= bns::extra_field::encrypted_bchat_value;
result.encrypted_bchat_value = std::string{encrypted_bchat_value};
}

if (encrypted_wallet_value.size())
{
result.fields |= bns::extra_field::encrypted_wallet_value;
result.encrypted_wallet_value = std::string{encrypted_wallet_value};
}

if (encrypted_belnet_value.size())
{
result.fields |= bns::extra_field::encrypted_value;
result.encrypted_value = std::string{encrypted_value};
result.fields |= bns::extra_field::encrypted_belnet_value;
result.encrypted_belnet_value = std::string{encrypted_belnet_value};
}

if (owner)
Expand Down
44 changes: 31 additions & 13 deletions src/cryptonote_basic/tx_extra.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,14 @@ enum struct extra_field : uint8_t
owner = 1 << 0,
backup_owner = 1 << 1,
signature = 1 << 2,
encrypted_value = 1 << 3,
encrypted_bchat_value = 1 << 3,
encrypted_wallet_value = 1 << 4,
encrypted_belnet_value = 1 << 5,

// Bit Masks
updatable_fields = (extra_field::owner | extra_field::backup_owner | extra_field::encrypted_value),
buy_no_backup = (extra_field::owner | extra_field::encrypted_value),
updatable_fields = (extra_field::owner | extra_field::backup_owner | extra_field::encrypted_bchat_value | extra_field::encrypted_wallet_value | extra_field::encrypted_belnet_value),
buy_no_backup = (extra_field::owner),
buy_any_value = (extra_field::encrypted_bchat_value | extra_field::encrypted_wallet_value | extra_field::encrypted_belnet_value),
buy = (extra_field::buy_no_backup | extra_field::backup_owner),
all = (extra_field::updatable_fields | extra_field::signature),
};
Expand Down Expand Up @@ -544,54 +547,69 @@ namespace cryptonote
{
uint8_t version = 0;
bns::mapping_type type;
bns::mapping_years mapping_years;
crypto::hash name_hash;
crypto::hash prev_txid = crypto::null_hash; // previous txid that purchased the mapping
bns::extra_field fields;
bns::generic_owner owner = {};
bns::generic_owner backup_owner = {};
bns::generic_signature signature = {};
std::string encrypted_value; // binary format of the name->value mapping
std::string encrypted_bchat_value; // binary format of the name->bchat_value mapping
std::string encrypted_wallet_value; // binary format of the name->wallet_value mapping
std::string encrypted_belnet_value; // binary format of the name->belnet_value mapping

bool field_is_set (bns::extra_field bit) const { return (fields & bit) == bit; }
bool field_any_set(bns::extra_field bit) const { return (fields & bit) != bns::extra_field::none; }

// True if this is updating some BNS info: has a signature and 1 or more updating field
bool is_updating() const { return field_is_set(bns::extra_field::signature) && field_any_set(bns::extra_field::updatable_fields); }
// True if this is buying a new BNS record
bool is_buying() const { return (fields == bns::extra_field::buy || fields == bns::extra_field::buy_no_backup); }
bool is_buying() const { return (field_is_set(bns::extra_field::buy) || field_is_set(bns::extra_field::buy_no_backup)) && field_any_set(bns::extra_field::buy_any_value); }
// True if this is renewing an existing BNS: has no fields at all, is a renewal registration (i.e. belnet),
// and has a non-null txid set (which should point to the most recent registration or update).
bool is_renewing() const { return fields == bns::extra_field::none && prev_txid && is_belnet_type(type); }
bool is_renewing() const { return fields == bns::extra_field::signature && prev_txid && is_renewal_type(mapping_years); }

static tx_extra_beldex_name_system make_buy(
bns::generic_owner const& owner,
bns::generic_owner const* backup_owner,
bns::mapping_type type,
bns::mapping_years mapping_years,
const crypto::hash& name_hash,
const std::string& encrypted_value,
const std::string& encrypted_bchat_value,
const std::string& encrypted_wallet_value,
const std::string& encrypted_belnet_value,
const crypto::hash& prev_txid);

static tx_extra_beldex_name_system make_renew(bns::mapping_type type, const crypto::hash& name_hash, const crypto::hash& prev_txid);
static tx_extra_beldex_name_system make_renew(
const bns::generic_signature& signature,
bns::mapping_years mapping_years,
const crypto::hash& name_hash,
const crypto::hash& prev_txid);

static tx_extra_beldex_name_system make_update(
const bns::generic_signature& signature,
bns::mapping_type type,
const crypto::hash& name_hash,
std::string_view encrypted_value,
std::string_view encrypted_bchat_value,
std::string_view encrypted_wallet_value,
std::string_view encrypted_belnet_value,
const bns::generic_owner* owner,
const bns::generic_owner* backup_owner,
const crypto::hash& prev_txid);

BEGIN_SERIALIZE()
FIELD(version)
ENUM_FIELD(type, type < bns::mapping_type::_count)
if(version >=1)
ENUM_FIELD(mapping_years, mapping_years < bns::mapping_years::_count)
else
ENUM_FIELD(type, type < bns::mapping_type::_count)
FIELD(name_hash)
FIELD(prev_txid)
ENUM_FIELD(fields, fields <= bns::extra_field::all)
if (field_is_set(bns::extra_field::owner)) FIELD(owner);
if (field_is_set(bns::extra_field::backup_owner)) FIELD(backup_owner);
if (field_is_set(bns::extra_field::signature)) FIELD(signature);
if (field_is_set(bns::extra_field::encrypted_value)) FIELD(encrypted_value);
if (field_is_set(bns::extra_field::encrypted_bchat_value)) FIELD(encrypted_bchat_value);
if (field_is_set(bns::extra_field::encrypted_wallet_value)) FIELD(encrypted_wallet_value);
if (field_is_set(bns::extra_field::encrypted_belnet_value)) FIELD(encrypted_belnet_value);
END_SERIALIZE()
};

Expand Down
Loading

0 comments on commit fe4024c

Please sign in to comment.