1
0
Fork 0
forked from lthn/blockchain

minting refactoring: in progress

This commit is contained in:
cryptozoidberg 2022-09-05 20:18:27 +02:00
parent 27e695d4f1
commit 7384485fe1
No known key found for this signature in database
GPG key ID: 22DEB97A54C6FDEC
6 changed files with 60 additions and 29 deletions

View file

@ -1424,7 +1424,8 @@ bool blockchain_storage::create_block_template(const create_block_template_param
const account_public_address& stakeholder_address = params.stakeholder_address;
const blobdata& ex_nonce = params.ex_nonce;
bool pos = params.pos;
const pos_entry& pe = params.pe;
const pos_entry& pe = params.pe;
fill_block_template_func_t* pcustom_fill_block_template_func = params.pcustom_fill_block_template_func;
uint64_t& height = resp.height;
@ -1435,6 +1436,17 @@ bool blockchain_storage::create_block_template(const create_block_template_param
size_t median_size;
boost::multiprecision::uint128_t already_generated_coins;
CRITICAL_REGION_BEGIN(m_read_lock);
if (pe.g_index == WALLET_GLOBAL_OUTPUT_INDEX_UNDEFINED)
{
std::vector<uint64_t> indexs;
if (!get_tx_outputs_gindexs(pe.tx_id, indexs) || pe.tx_out_index >= indexs.size())
{
CHECK_AND_ASSERT_MES(false, false, "Failed to get_tx_outputs_gindexs() for tx_id " << pe.tx_id);
}
pe.g_index = indexs[pe.tx_out_index];
}
height = m_db_blocks.size();
b.major_version = m_core_runtime_config.hard_forks.get_block_major_version_by_height(height);

View file

@ -964,7 +964,7 @@ namespace currency
struct pos_entry
{
uint64_t amount;
uint64_t index; // global output index. TODO: remove, use tx_id + tx_out_index to get gindex in construct_miner_tx
uint64_t g_index; // global output index. (could be WALLET_GLOBAL_OUTPUT_INDEX_UNDEFINED)
crypto::key_image keyimage;
uint64_t block_timestamp;
uint64_t stake_unlock_time;
@ -977,7 +977,7 @@ namespace currency
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(amount)
KV_SERIALIZE(index)
KV_SERIALIZE(g_index)
KV_SERIALIZE(stake_unlock_time)
KV_SERIALIZE(block_timestamp)
KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE(keyimage)

View file

@ -831,7 +831,9 @@ namespace currency
params.ex_nonce = req.extra_text;
params.pos = req.pos_block;
params.pe.amount = req.pos_amount;
params.pe.index = req.pos_index;
params.pe.g_index = req.pos_g_index;
params.pe.tx_id = req.tx_id;
params.pe.tx_out_index = req.tx_out_index;
params.pe.stake_unlock_time = req.stake_unlock_time;
//params.pe.keyimage key image will be set in the wallet
//params.pe.wallet_index is not included in serialization map, TODO: refactoring here

View file

@ -804,8 +804,10 @@ namespace currency
std::string wallet_address;
std::string stakeholder_address;
bool pos_block; //is pos block
uint64_t pos_amount; //
uint64_t pos_index; //
uint64_t pos_amount; //do we still need it?
uint64_t pos_g_index; //
crypto::hash tx_id;
uint64_t tx_out_index;
uint64_t stake_unlock_time;
BEGIN_KV_SERIALIZE_MAP()
@ -815,7 +817,9 @@ namespace currency
KV_SERIALIZE(stakeholder_address);
KV_SERIALIZE(pos_block)
KV_SERIALIZE(pos_amount)
KV_SERIALIZE(pos_index)
KV_SERIALIZE(pos_g_index)
KV_SERIALIZE_POD_AS_HEX_STRING(tx_id)
KV_SERIALIZE(tx_out_index)
KV_SERIALIZE(stake_unlock_time)
END_KV_SERIALIZE_MAP()
};

View file

@ -3545,7 +3545,7 @@ bool wallet2::get_pos_entries(std::vector<currency::pos_entry>& entries)
pos_entry pe = AUTO_VAL_INIT(pe);
pe.amount = tr.amount();
pe.index = tr.m_global_output_index;
//pe.index = tr.m_global_output_index;
pe.keyimage = tr.m_key_image;
pe.wallet_index = i;
pe.stake_unlock_time = stake_unlock_time;
@ -3633,8 +3633,8 @@ bool wallet2::build_kernel(const pos_entry& pe, const stake_modifier_type& stake
//----------------------------------------------------------------------------------------------------
bool wallet2::fill_mining_context(mining_context& ctx)
{
bool r = get_pos_entries(ctx.sp.pos_entries); // TODO: Remove this call. Transfers are filtered in scan_pos
WLT_CHECK_AND_ASSERT_MES(r, false, "Failed to get_pos_entries()");
//bool r = get_pos_entries(ctx.sp.pos_entries); // TODO: Remove this call. Transfers are filtered in scan_pos
//WLT_CHECK_AND_ASSERT_MES(r, false, "Failed to get_pos_entries()");
currency::COMMAND_RPC_GET_POS_MINING_DETAILS::request pos_details_req = AUTO_VAL_INIT(pos_details_req);
currency::COMMAND_RPC_GET_POS_MINING_DETAILS::response pos_details_resp = AUTO_VAL_INIT(pos_details_resp);
@ -3675,9 +3675,9 @@ bool wallet2::try_mint_pos(const currency::account_public_address& miner_address
return true;
}
uint64_t pos_entries_amount = 0;
for (auto& ent : ctx.sp.pos_entries)
pos_entries_amount += ent.amount;
//uint64_t pos_entries_amount = 0;
//for (auto& ent : ctx.sp.pos_entries)
// pos_entries_amount += ent.amount;
std::atomic<bool> stop(false);
scan_pos(ctx, stop, [this](){
@ -3696,7 +3696,7 @@ bool wallet2::try_mint_pos(const currency::account_public_address& miner_address
build_minted_block(ctx, miner_address);
}
WLT_LOG_L0("PoS mining: " << ctx.iterations_processed << " iterations finished, status: " << ctx.rsp.status << ", used " << ctx.sp.pos_entries.size() << " entries with total amount: " << print_money_brief(pos_entries_amount));
WLT_LOG_L0("PoS mining: " << ctx.iterations_processed << " iterations finished, status: " << ctx.rsp.status << ", " << ctx.total_items_checked << " entries with total amount: " << print_money_brief(ctx.total_amount_checked));
return true;
}
@ -3788,22 +3788,25 @@ bool wallet2::build_minted_block(const mining_context& cxt,
//found a block, construct it, sign and push to daemon
WLT_LOG_GREEN("Found kernel, constructing block", LOG_LEVEL_0);
CHECK_AND_NO_ASSERT_MES(cxt.rsp.index < cxt.sp.pos_entries.size(), false, "call_COMMAND_RPC_SCAN_POS returned wrong index: " << cxt.rsp.index << ", expected less then " << cxt.sp.pos_entries.size());
//CHECK_AND_NO_ASSERT_MES(cxt.rsp.index < cxt.sp.pos_entries.size(), false, "call_COMMAND_RPC_SCAN_POS returned wrong index: " << cxt.rsp.index << ", expected less then " << cxt.sp.pos_entries.size());
const pos_entry& pe = cxt.sp.pos_entries[cxt.rsp.index];
const pos_entry pe = AUTO_VAL_INIT();
pe.
currency::COMMAND_RPC_GETBLOCKTEMPLATE::request tmpl_req = AUTO_VAL_INIT(tmpl_req);
currency::COMMAND_RPC_GETBLOCKTEMPLATE::response tmpl_rsp = AUTO_VAL_INIT(tmpl_rsp);
tmpl_req.wallet_address = get_account_address_as_str(miner_address);
tmpl_req.stakeholder_address = get_account_address_as_str(m_account.get_public_address());
tmpl_req.pos_block = true;
tmpl_req.pos_amount = pe.amount;
tmpl_req.pos_index = pe.index; // gindex <--- this should be removed as soon as pos_entry::index is replaced with tx_id and tx_out_index
tmpl_req.pos_g_index = m_transfers[cxt.rsp.index].m_global_output_index;
tmpl_req.pos_amount = m_transfers[cxt.rsp.index].amount();// pe.amount;
tmpl_req.tx_id = m_transfers[cxt.rsp.index].tx_hash();
tmpl_req.tx_out_index = m_transfers[cxt.rsp.index].m_internal_output_index;
//tmpl_req.pos_index = pe.index; // gindex <--- this should be removed as soon as pos_entry::index is replaced with tx_id and tx_out_index
// TODO: also fill out tx_id and tx_out_index for mining tx creation
tmpl_req.extra_text = m_miner_text_info;
tmpl_req.stake_unlock_time = pe.stake_unlock_time;
tmpl_req.stake_unlock_time = cxt.rsp.stake_unlock_time;
//generate packing tx
transaction pack_tx = AUTO_VAL_INIT(pack_tx);
if (generate_packing_transaction_if_needed(pack_tx, 0))
@ -3828,15 +3831,15 @@ bool wallet2::build_minted_block(const mining_context& cxt,
}
std::vector<const crypto::public_key*> keys_ptrs;
WLT_CHECK_AND_ASSERT_MES(pe.wallet_index < m_transfers.size(),
WLT_CHECK_AND_ASSERT_MES(cxt.rsp.index < m_transfers.size(),
false, "Wrong wallet_index at generating coinbase transacton");
if (m_transfers[pe.wallet_index].m_ptx_wallet_info->m_tx.vout[m_transfers[pe.wallet_index].m_internal_output_index].type() != typeid(tx_out_bare))
if (m_transfers[cxt.rsp.index].m_ptx_wallet_info->m_tx.vout[m_transfers[cxt.rsp.index].m_internal_output_index].type() != typeid(tx_out_bare))
{
//@#@ review zarcanum here
return false;
}
const auto& target = boost::get<tx_out_bare>(m_transfers[pe.wallet_index].m_ptx_wallet_info->m_tx.vout[m_transfers[pe.wallet_index].m_internal_output_index]).target;
const auto& target = boost::get<tx_out_bare>(m_transfers[cxt.rsp.index].m_ptx_wallet_info->m_tx.vout[m_transfers[cxt.rsp.index].m_internal_output_index]).target;
WLT_CHECK_AND_ASSERT_MES(target.type() == typeid(currency::txout_to_key), false, "wrong type_id in source transaction in coinbase tx");
const currency::txout_to_key& txtokey = boost::get<currency::txout_to_key>(target);
@ -3852,7 +3855,7 @@ bool wallet2::build_minted_block(const mining_context& cxt,
res = prepare_and_sign_pos_block(b,
pe,
get_tx_pub_key_from_extra(m_transfers[pe.wallet_index].m_ptx_wallet_info->m_tx),
m_transfers[pe.wallet_index].m_internal_output_index,
m_transfers[cxt.rsp.index].m_internal_output_index,
keys_ptrs);
WLT_CHECK_AND_ASSERT_MES(res, false, "Failed to prepare_and_sign_pos_block");

View file

@ -447,7 +447,7 @@ namespace tools
struct mining_context
{
// from struct COMMAND_RPC_SCAN_POS
struct request_t
/*struct request_t
{
std::vector<currency::pos_entry> pos_entries;
@ -455,11 +455,13 @@ namespace tools
KV_SERIALIZE(pos_entries)
END_KV_SERIALIZE_MAP()
};
*/
struct response_t
{
std::string status;
uint64_t index; // index in pos_entries container
uint64_t index; // index in m_transfers
uint64_t stake_unlock_time;
uint64_t block_timestamp;
uint64_t height;
uint64_t starter_timestamp;
@ -469,6 +471,7 @@ namespace tools
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(status)
KV_SERIALIZE(index)
KV_SERIALIZE(stake_unlock_time)
KV_SERIALIZE(block_timestamp)
KV_SERIALIZE(height)
KV_SERIALIZE(is_pos_allowed)
@ -477,7 +480,7 @@ namespace tools
END_KV_SERIALIZE_MAP()
};
request_t sp;
//request_t sp;
response_t rsp;
currency::wide_difficulty_type basic_diff;
@ -486,7 +489,9 @@ namespace tools
bool zarcanum;
crypto::scalar_t last_pow_block_id_hashed; // Zarcanum notation: f'
uint64_t iterations_processed;
uint64_t iterations_processed = 0;
uint64_t total_items_checked = 0;
uint64_t total_amount_checked = 0;
};
struct expiration_entry_info
@ -1364,6 +1369,7 @@ namespace tools
if (!is_transfer_okay_for_pos(tr, stake_unlock_time))
continue;
bool go_past = true;
uint64_t step = 0;
@ -1384,7 +1390,8 @@ namespace tools
};
do_pos_mining_prepare_entry(cxt, transfer_index);
ctx.total_items_checked++;
ctx.total_amount_checked += tr.amount();
while(step <= ts_window)
{
//check every WALLET_POS_MINT_CHECK_HEIGHT_INTERVAL seconds wheither top block changed, if so - break the loop
@ -1410,8 +1417,11 @@ namespace tools
if (stop)
return false;
cxt.iterations_processed++;
if (do_pos_mining_iteration(cxt, transfer_index, ts))
{
cxt.rsp.index = transfer_index;
cxt.rsp.stake_unlock_time = stake_unlock_time;
cxt.rsp.status = API_RETURN_CODE_OK;
return true;
}