Skip to content

Commit

Permalink
wallet2: more performant tree building
Browse files Browse the repository at this point in the history
  • Loading branch information
j-berman committed Oct 31, 2024
1 parent 3db1aa1 commit c28cc29
Show file tree
Hide file tree
Showing 12 changed files with 744 additions and 178 deletions.
1 change: 1 addition & 0 deletions src/blockchain_db/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ target_link_libraries(blockchain_db_utils
PUBLIC
cncrypto
cryptonote_basic
epee
ringct
)

Expand Down
3 changes: 1 addition & 2 deletions src/blockchain_db/blockchain_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,7 @@ uint64_t BlockchainDB::add_block( const std::pair<block, blobdata>& blck

// When adding a block, we also need to keep track of when outputs unlock, so
// we can use them to grow the merkle tree used in fcmp's at that point.
fcmp_pp::curve_trees::OutputsByUnlockBlock outs_by_unlock_block;
cryptonote::get_outs_by_unlock_block(blk.miner_tx, _txs, total_n_outputs, prev_height, outs_by_unlock_block);
const auto outs_by_unlock_block = cryptonote::get_outs_by_unlock_block(blk.miner_tx, _txs, total_n_outputs, prev_height).first;

// call out to subclass implementation to add the block & metadata
time1 = epee::misc_utils::get_tick_count();
Expand Down
27 changes: 22 additions & 5 deletions src/blockchain_db/blockchain_db_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include "cryptonote_basic/cryptonote_format_utils.h"
#include "ringct/rctOps.h"

#include "profile_tools.h"

//----------------------------------------------------------------------------------------------------------------------
// Helper function to group outputs by unlock block
static uint64_t set_tx_outs_by_unlock_block(const cryptonote::transaction &tx,
Expand All @@ -42,6 +44,9 @@ static uint64_t set_tx_outs_by_unlock_block(const cryptonote::transaction &tx,
{
const uint64_t unlock_block = cryptonote::get_unlock_block_index(tx.unlock_time, block_idx);

uint64_t getting_commitment_ns = 0;
uint64_t setting_unlock_block_ns = 0;

for (std::size_t i = 0; i < tx.vout.size(); ++i)
{
const auto &out = tx.vout[i];
Expand All @@ -57,10 +62,14 @@ static uint64_t set_tx_outs_by_unlock_block(const cryptonote::transaction &tx,
if (!miner_tx && tx.version == 2)
CHECK_AND_ASSERT_THROW_MES(tx.rct_signatures.outPk.size() > i, "unexpected size of outPk");

TIME_MEASURE_NS_START(getting_commitment);

rct::key commitment = (miner_tx || tx.version < 2)
? rct::zeroCommitVartime(out.amount)
: tx.rct_signatures.outPk[i].mask;

TIME_MEASURE_NS_FINISH(getting_commitment);

auto output_pair = fcmp_pp::curve_trees::OutputPair{
.output_pubkey = std::move(output_public_key),
.commitment = std::move(commitment)
Expand All @@ -71,6 +80,8 @@ static uint64_t set_tx_outs_by_unlock_block(const cryptonote::transaction &tx,
.output_pair = std::move(output_pair)
};

TIME_MEASURE_NS_START(setting_unlock_block);

if (outs_by_unlock_block_inout.find(unlock_block) == outs_by_unlock_block_inout.end())
{
auto new_vec = std::vector<fcmp_pp::curve_trees::OutputContext>{std::move(output_context)};
Expand All @@ -80,22 +91,28 @@ static uint64_t set_tx_outs_by_unlock_block(const cryptonote::transaction &tx,
{
outs_by_unlock_block_inout[unlock_block].emplace_back(std::move(output_context));
}

TIME_MEASURE_NS_FINISH(setting_unlock_block);

getting_commitment_ns += getting_commitment;
setting_unlock_block_ns += setting_unlock_block;
}

LOG_PRINT_L3("getting_commitment_ms: " << getting_commitment_ns / 1000 << " , setting_unlock_block_ms: " << setting_unlock_block_ns / 1000);

return tx.vout.size();
}
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
namespace cryptonote
{
uint64_t get_outs_by_unlock_block(
std::pair<fcmp_pp::curve_trees::OutputsByUnlockBlock, uint64_t> get_outs_by_unlock_block(
const cryptonote::transaction &miner_tx,
const std::vector<std::reference_wrapper<const cryptonote::transaction>> &txs,
const uint64_t first_output_id,
const uint64_t block_idx,
fcmp_pp::curve_trees::OutputsByUnlockBlock &outs_by_unlock_block_out)
const uint64_t block_idx)
{
outs_by_unlock_block_out.clear();
fcmp_pp::curve_trees::OutputsByUnlockBlock outs_by_unlock_block_out;

uint64_t output_id = first_output_id;

Expand All @@ -118,7 +135,7 @@ uint64_t get_outs_by_unlock_block(
outs_by_unlock_block_out);
}

return output_id;
return { outs_by_unlock_block_out, output_id };
}
//----------------------------------------------------------------------------------------------------------------------
}//namespace cryptonote
11 changes: 6 additions & 5 deletions src/blockchain_db/blockchain_db_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,18 @@
#include "fcmp_pp/curve_trees.h"

#include <functional>
#include <utility>
#include <vector>

namespace cryptonote
{
// This function internally relies on ringct for zeroCommit. I implemented in this blockchain_db_utils file instead of
// cryptonote_basic (where it would seem the better place to put it) to avoid a circular dependency between
// These functions internally rely on ringct for zeroCommitVartime. I implemented in this blockchain_db_utils file
// instead of cryptonote_basic (where it would seem the better place to put it) to avoid a circular dependency between
// ringct <> cryptonote_basic.
uint64_t get_outs_by_unlock_block(
// Note that zeroCommitVartime causes this function to execute slowly.
std::pair<fcmp_pp::curve_trees::OutputsByUnlockBlock, uint64_t> get_outs_by_unlock_block(
const cryptonote::transaction &miner_tx,
const std::vector<std::reference_wrapper<const cryptonote::transaction>> &txs,
const uint64_t first_output_id,
const uint64_t block_idx,
fcmp_pp::curve_trees::OutputsByUnlockBlock &outs_by_unlock_out);
const uint64_t block_idx);
}
4 changes: 2 additions & 2 deletions src/cryptonote_basic/account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ DISABLE_VS_WARNINGS(4244 4345)

struct tm timestamp = {0};
timestamp.tm_year = 2014 - 1900; // year 2014
timestamp.tm_mon = 6 - 1; // month june
timestamp.tm_mday = 8; // 8th of june
timestamp.tm_mon = 4 - 1; // month april
timestamp.tm_mday = 15; // 15th of june
timestamp.tm_hour = 0;
timestamp.tm_min = 0;
timestamp.tm_sec = 0;
Expand Down
Loading

0 comments on commit c28cc29

Please sign in to comment.