Skip to content

Commit bf8bb00

Browse files
authored
Merge pull request #179 from victor-tucci/dev
Correct emission/fee/burn accumulation by migrating from int64_t to unsigned long long in get_coinbase_tx_sum
2 parents 817556e + a11763b commit bf8bb00

7 files changed

Lines changed: 49 additions & 27 deletions

File tree

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@ Portions Copyright (c) 2012-2013 The Cryptonote developers.
1010
## Development resources
1111

1212
- Web: [beldex.io](https://beldex.io)
13-
- Telegram: [t.me/beldexcoin](https://t.me/beldex_official)
13+
- Telegram: [t.me/beldexcoin](https://t.me/beldexcoin)
1414
- GitHub: [https://github.com/Beldex-Coin/beldex](https://github.com/Beldex-Coin/beldex)
1515

16-
## Vulnerability disclosure
17-
18-
- Check out our [Vulnerability Response Process](https://beldex-project.github.io/beldex-docs/Contributing/VULNERABILITY_RESPONSE_BELDEX), encourages prompt disclosure of any Vulnerabilities
1916

2017
## Information
2118

contrib/epee/include/epee/storages/portable_storage.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,18 @@ namespace epee
180180
bool portable_storage::get_value(const std::string& value_name, T& val, section* parent_section)
181181
{
182182
static_assert(variant_contains<T, storage_entry>);
183-
//TRY_ENTRY();
184-
if(!parent_section) parent_section = &m_root;
183+
if (!parent_section)
184+
parent_section = &m_root;
185185
storage_entry* pentry = find_storage_entry(value_name, parent_section);
186-
if(!pentry)
187-
return false;
186+
if (!pentry)
187+
return false;
188188

189-
var::visit([&val](const auto& v) { convert_t(v, val); }, *pentry);
190-
return true;
191-
//CATCH_ENTRY("portable_storage::template<>get_value", false);
189+
try {
190+
var::visit([&val](const auto& v) { convert_t(v, val); }, *pentry);
191+
return true;
192+
} catch (const std::exception&) {
193+
return false;
194+
}
192195
}
193196
//---------------------------------------------------------------------------------------------------------------
194197
template <typename T>

contrib/epee/src/portable_storage.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,18 @@ namespace epee {
169169

170170
bool portable_storage::get_value(const std::string& value_name, storage_entry& val, section* parent_section)
171171
{
172-
//TRY_ENTRY();
173-
if(!parent_section) parent_section = &m_root;
172+
if (!parent_section)
173+
parent_section = &m_root;
174174
storage_entry* pentry = find_storage_entry(value_name, parent_section);
175175
if(!pentry)
176176
return false;
177177

178-
val = *pentry;
179-
return true;
180-
//CATCH_ENTRY("portable_storage::template<>get_value", false);
178+
try {
179+
val = *pentry;
180+
return true;
181+
} catch (const std::exception&) {
182+
return false;
183+
}
181184
}
182185

183186
storage_entry* portable_storage::find_storage_entry(const std::string& pentry_name, section* psection)

src/cryptonote_core/cryptonote_core.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,9 +1758,10 @@ namespace cryptonote
17581758
return m_mempool.check_for_key_images(key_im, spent);
17591759
}
17601760
//-----------------------------------------------------------------------------------------------
1761-
std::optional<std::tuple<int64_t, int64_t, int64_t>> core::get_coinbase_tx_sum(uint64_t start_offset, size_t count)
1761+
std::optional<std::tuple<unsigned long long, unsigned long long, unsigned long long>>
1762+
core::get_coinbase_tx_sum(uint64_t start_offset, size_t count)
17621763
{
1763-
std::optional<std::tuple<int64_t, int64_t, int64_t>> result{{0, 0, 0}};
1764+
std::optional<std::tuple<unsigned long long, unsigned long long, unsigned long long>> result{{0ULL, 0ULL, 0ULL}};
17641765
if (count == 0)
17651766
return result;
17661767

@@ -1787,6 +1788,8 @@ namespace cryptonote
17871788
{
17881789
std::shared_lock lock{m_coinbase_cache.mutex};
17891790
if (count >= m_coinbase_cache.height) {
1791+
// NOTE: if m_coinbase_cache.emissions/fees/burnt are signed types, they will be implicitly
1792+
// converted to unsigned here. If they are signed, consider static_cast<unsigned long long>.
17901793
emission_amount = m_coinbase_cache.emissions;
17911794
total_fee_amount = m_coinbase_cache.fees;
17921795
burnt_beldex = m_coinbase_cache.burnt;
@@ -1837,20 +1840,24 @@ namespace cryptonote
18371840
[this, &cache_to, &result, &cache_build_started](uint64_t height, const crypto::hash& hash, const block& b){
18381841
auto& [emission_amount, total_fee_amount, burnt_beldex] = *result;
18391842
std::vector<transaction> txs;
1840-
auto coinbase_amount = static_cast<int64_t>(get_outs_money_amount(b.miner_tx));
1843+
1844+
// Convert amounts into unsigned accumulators
1845+
const unsigned long long coinbase_amount = static_cast<unsigned long long>(get_outs_money_amount(b.miner_tx));
18411846
get_transactions(b.tx_hashes, txs);
1842-
int64_t tx_fee_amount = 0;
1843-
for(const auto& tx: txs)
1847+
1848+
unsigned long long tx_fee_amount = 0ULL;
1849+
for (const auto& tx: txs)
18441850
{
1845-
tx_fee_amount += static_cast<int64_t>(get_tx_miner_fee(tx, b.major_version >= feature::FEE_BURNING));
1846-
if(b.major_version >= feature::FEE_BURNING)
1851+
tx_fee_amount += static_cast<unsigned long long>(get_tx_miner_fee(tx, b.major_version >= feature::FEE_BURNING));
1852+
if (b.major_version >= feature::FEE_BURNING)
18471853
{
1848-
burnt_beldex += static_cast<int64_t>(get_burned_amount_from_tx_extra(tx.extra));
1854+
burnt_beldex += static_cast<unsigned long long>(get_burned_amount_from_tx_extra(tx.extra));
18491855
}
18501856
}
18511857

18521858
emission_amount += coinbase_amount - tx_fee_amount;
18531859
total_fee_amount += tx_fee_amount;
1860+
18541861
if (cache_to && cache_to == height)
18551862
{
18561863
std::unique_lock lock{m_coinbase_cache.mutex};

src/cryptonote_core/cryptonote_core.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ namespace cryptonote
810810
* requested range. The optional value will be empty only if requesting the full chain *and*
811811
* another thread is already calculating it.
812812
*/
813-
std::optional<std::tuple<int64_t, int64_t, int64_t>> get_coinbase_tx_sum(uint64_t start_offset, size_t count);
813+
std::optional<std::tuple<unsigned long long, unsigned long long, unsigned long long>> get_coinbase_tx_sum(uint64_t start_offset, size_t count);
814814

815815
/**
816816
* @brief get the network type we're on

src/cryptonote_core/master_node_list.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1589,7 +1589,6 @@ namespace master_nodes
15891589
// NOTE: No POS quorums are generated when the network has insufficient nodes to generate quorums
15901590
// Or, block specifies time after all the rounds have timed out
15911591
bool miner_block = !POS_hf || !POS_quorum;
1592-
// std::cout << "miner_block : " << miner_block << std::endl;
15931592

15941593
result = verify_block_components(m_blockchain.nettype(),
15951594
block,

src/wallet/wallet_rpc_server.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,21 @@ namespace tools
223223
if(!ps.load_from_json(body))
224224
return jsonrpc_error_response(res, -32700, "Parse error", {});
225225

226+
nlohmann::json id;
226227
epee::serialization::storage_entry epee_id{std::string{}};
227-
ps.get_value("id", epee_id, nullptr);
228+
if (std::string str_val; ps.get_value("id", str_val, nullptr)) {
229+
epee_id = str_val;
230+
id = std::move(str_val);
231+
} else if (int64_t i64_val; ps.get_value("id", i64_val, nullptr)) {
232+
epee_id = i64_val;
233+
id = i64_val;
234+
} else {
235+
return jsonrpc_error_response(
236+
res,
237+
-32700,
238+
"Parse error, missing a valid (string or integer) JSON RPC 'id'",
239+
{});
240+
}
228241

229242
nlohmann::json id = var::get<std::string>(epee_id);
230243

0 commit comments

Comments
 (0)