1
0
Fork 0
forked from lthn/blockchain

wallet: pos code clean up (mining_context, etc)

This commit is contained in:
sowle 2022-09-09 20:16:14 +02:00
parent c7bc50f968
commit 41308d86a6
No known key found for this signature in database
GPG key ID: C07A24B2D89D49FC
4 changed files with 51 additions and 99 deletions

View file

@ -3533,16 +3533,6 @@ bool wallet2::get_pos_entries(std::vector<currency::pos_entry>& entries)
if (!is_transfer_okay_for_pos(tr, stake_unlock_time))
continue;
// We don't need to prefetch PoS outputs anymore as gindex in not required and fetched in construct_miner_tx
// I commented this out, CZ, please, review this change -- sowle
//if (tr.m_global_output_index == WALLET_GLOBAL_OUTPUT_INDEX_UNDEFINED)
//{
// //TODO: this code needed mostly for coretests, since in real life cases only mobile wallet supposed to
// // have WALLET_GLOBAL_OUTPUT_INDEX_UNDEFINED, and mobile wallet is not supposed to do PoS mining
// std::vector<uint64_t> indicies; indicies.push_back(i);
// prefetch_global_indicies_if_needed(indicies);
//}
pos_entry pe = AUTO_VAL_INIT(pe);
pe.amount = tr.amount();
pe.g_index = tr.m_global_output_index;
@ -3621,16 +3611,6 @@ bool wallet2::prepare_and_sign_pos_block(currency::block& b,
return false; // to get rid of warning
}
//------------------------------------------------------------------
bool wallet2::build_kernel(const pos_entry& pe, const stake_modifier_type& stake_modifier, const uint64_t timestamp, stake_kernel& kernel)
{
PROFILE_FUNC("build_kernel");
kernel = stake_kernel();
kernel.kimage = pe.keyimage;
kernel.stake_modifier = stake_modifier;
kernel.block_timestamp = timestamp;
return true;
}
//----------------------------------------------------------------------------------------------------
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
@ -3638,7 +3618,7 @@ bool wallet2::fill_mining_context(mining_context& ctx)
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);
ctx.rsp.status = API_RETURN_CODE_NOT_FOUND;
ctx.status = API_RETURN_CODE_NOT_FOUND;
m_core_proxy->call_COMMAND_RPC_GET_POS_MINING_DETAILS(pos_details_req, pos_details_resp);
if (pos_details_resp.status != API_RETURN_CODE_OK)
return false;
@ -3652,10 +3632,10 @@ bool wallet2::fill_mining_context(mining_context& ctx)
ctx.last_pow_block_id_hashed = crypto::hash_helper_t::hs(CRYPTO_HDS_ZARCANUM_LAST_POW_HASH, ctx.sm.last_pow_id);
}
ctx.rsp.last_block_hash = pos_details_resp.last_block_hash;
ctx.rsp.is_pos_allowed = pos_details_resp.pos_mining_allowed;
ctx.rsp.starter_timestamp = pos_details_resp.starter_timestamp;
ctx.rsp.status = API_RETURN_CODE_OK;
ctx.last_block_hash = pos_details_resp.last_block_hash;
ctx.is_pos_allowed = pos_details_resp.pos_mining_allowed;
ctx.starter_timestamp = pos_details_resp.starter_timestamp;
ctx.status = API_RETURN_CODE_OK;
return true;
}
//------------------------------------------------------------------
@ -3669,7 +3649,7 @@ bool wallet2::try_mint_pos(const currency::account_public_address& miner_address
mining_context ctx = AUTO_VAL_INIT(ctx);
WLT_LOG_L1("Starting PoS mining iteration");
fill_mining_context(ctx);
if (!ctx.rsp.is_pos_allowed)
if (!ctx.is_pos_allowed)
{
WLT_LOG_YELLOW("POS MINING NOT ALLOWED YET", LOG_LEVEL_0);
return true;
@ -3691,12 +3671,12 @@ bool wallet2::try_mint_pos(const currency::account_public_address& miner_address
return true;
}, m_core_runtime_config);
if (ctx.rsp.status == API_RETURN_CODE_OK)
if (ctx.status == API_RETURN_CODE_OK)
{
build_minted_block(ctx, miner_address);
}
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));
WLT_LOG_L0("PoS mining: " << ctx.iterations_processed << " iterations finished, status: " << ctx.status << ", " << ctx.total_items_checked << " entries with total amount: " << print_money_brief(ctx.total_amount_checked));
return true;
}
@ -3747,8 +3727,8 @@ bool wallet2::do_pos_mining_iteration(mining_context& cxt, size_t transfer_index
}
if (found)
{
//cxt.rsp.index = pos_entry_index;
cxt.rsp.block_timestamp = ts;
//cxt.index = pos_entry_index;
cxt.block_timestamp = ts;
LOG_PRINT_GREEN("Found kernel: amount: " << currency::print_money_brief(stake_amount) << ENDL
<< "difficulty: " << cxt.basic_diff << ", final_diff: " << final_diff << ENDL
@ -3788,7 +3768,7 @@ 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.index < cxt.sp.pos_entries.size(), false, "call_COMMAND_RPC_SCAN_POS returned wrong index: " << cxt.index << ", expected less then " << cxt.sp.pos_entries.size());
pos_entry pe = AUTO_VAL_INIT(pe);
@ -3798,14 +3778,14 @@ bool wallet2::build_minted_block(const mining_context& cxt,
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;
pe.g_index = tmpl_req.pos_g_index = m_transfers[cxt.rsp.index].m_global_output_index;
pe.amount = tmpl_req.pos_amount = m_transfers[cxt.rsp.index].amount();// pe.amount;
pe.keyimage = m_transfers[cxt.rsp.index].m_key_image;
pe.block_timestamp = m_transfers[cxt.rsp.index].m_ptx_wallet_info->m_block_timestamp;
pe.stake_unlock_time = tmpl_req.stake_unlock_time = cxt.rsp.stake_unlock_time;
pe.tx_id = tmpl_req.tx_id = m_transfers[cxt.rsp.index].tx_hash();
pe.tx_out_index = tmpl_req.tx_out_index = m_transfers[cxt.rsp.index].m_internal_output_index;
pe.wallet_index = cxt.rsp.index;
pe.g_index = tmpl_req.pos_g_index = m_transfers[cxt.index].m_global_output_index;
pe.amount = tmpl_req.pos_amount = m_transfers[cxt.index].amount();// pe.amount;
pe.keyimage = m_transfers[cxt.index].m_key_image;
pe.block_timestamp = m_transfers[cxt.index].m_ptx_wallet_info->m_block_timestamp;
pe.stake_unlock_time = tmpl_req.stake_unlock_time = cxt.stake_unlock_time;
pe.tx_id = tmpl_req.tx_id = m_transfers[cxt.index].tx_hash();
pe.tx_out_index = tmpl_req.tx_out_index = m_transfers[cxt.index].m_internal_output_index;
pe.wallet_index = cxt.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
@ -3827,29 +3807,29 @@ bool wallet2::build_minted_block(const mining_context& cxt,
res = parse_and_validate_block_from_blob(block_blob, b);
WLT_CHECK_AND_ASSERT_MES(res, false, "Failed to create block template after kernel hash found!");
if (cxt.rsp.last_block_hash != b.prev_id)
if (cxt.last_block_hash != b.prev_id)
{
WLT_LOG_YELLOW("Kernel was found but block is behindhand, b.prev_id=" << b.prev_id << ", last_block_hash=" << cxt.rsp.last_block_hash, LOG_LEVEL_0);
WLT_LOG_YELLOW("Kernel was found but block is behindhand, b.prev_id=" << b.prev_id << ", last_block_hash=" << cxt.last_block_hash, LOG_LEVEL_0);
return false;
}
std::vector<const crypto::public_key*> keys_ptrs;
WLT_CHECK_AND_ASSERT_MES(cxt.rsp.index < m_transfers.size(),
WLT_CHECK_AND_ASSERT_MES(cxt.index < m_transfers.size(),
false, "Wrong wallet_index at generating coinbase transacton");
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))
if (m_transfers[cxt.index].m_ptx_wallet_info->m_tx.vout[m_transfers[cxt.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[cxt.rsp.index].m_ptx_wallet_info->m_tx.vout[m_transfers[cxt.rsp.index].m_internal_output_index]).target;
const auto& target = boost::get<tx_out_bare>(m_transfers[cxt.index].m_ptx_wallet_info->m_tx.vout[m_transfers[cxt.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);
keys_ptrs.push_back(&txtokey.key);
// set a real timestamp
b.timestamp = cxt.rsp.block_timestamp;
b.timestamp = cxt.block_timestamp;
uint64_t current_timestamp = m_core_runtime_config.get_core_time();
set_block_datetime(current_timestamp, b);
WLT_LOG_MAGENTA("Applying actual timestamp: " << current_timestamp, LOG_LEVEL_0);
@ -3858,7 +3838,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[cxt.rsp.index].m_internal_output_index,
m_transfers[cxt.index].m_internal_output_index,
keys_ptrs);
WLT_CHECK_AND_ASSERT_MES(res, false, "Failed to prepare_and_sign_pos_block");

View file

@ -446,52 +446,25 @@ namespace tools
struct mining_context
{
// from struct COMMAND_RPC_SCAN_POS
/*struct request_t
{
std::vector<currency::pos_entry> pos_entries;
std::string status;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(pos_entries)
END_KV_SERIALIZE_MAP()
};
*/
bool is_pos_allowed;
bool zarcanum;
struct response_t
{
std::string status;
uint64_t index; // index in m_transfers
uint64_t stake_unlock_time;
uint64_t block_timestamp;
uint64_t height;
uint64_t starter_timestamp;
crypto::hash last_block_hash;
bool is_pos_allowed;
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)
KV_SERIALIZE(starter_timestamp)
KV_SERIALIZE_VAL_POD_AS_BLOB(last_block_hash);
END_KV_SERIALIZE_MAP()
};
//request_t sp;
response_t rsp;
uint64_t index; // index in m_transfers
uint64_t stake_unlock_time;
uint64_t block_timestamp;
uint64_t height;
uint64_t starter_timestamp;
crypto::hash last_block_hash;
crypto::scalar_t last_pow_block_id_hashed; // Zarcanum notation: f'
currency::wide_difficulty_type basic_diff;
currency::stake_modifier_type sm;
bool zarcanum;
crypto::scalar_t last_pow_block_id_hashed; // Zarcanum notation: f'
uint64_t iterations_processed = 0;
uint64_t total_items_checked = 0;
uint64_t total_amount_checked = 0;
uint64_t iterations_processed = 0;
uint64_t total_items_checked = 0;
uint64_t total_amount_checked = 0;
};
struct expiration_entry_info
@ -1004,7 +977,6 @@ private:
currency::COMMAND_RPC_GET_BLOCKS_DIRECT::response& blocks);
std::string get_alias_for_address(const std::string& addr);
std::vector<std::string> get_aliases_for_address(const std::string& addr);
static bool build_kernel(const currency::pos_entry& pe, const currency::stake_modifier_type& stake_modifier, const uint64_t timestamp, currency::stake_kernel& kernel);
bool is_connected_to_net();
bool is_transfer_okay_for_pos(const transfer_details& tr, uint64_t& stake_unlock_time) const;
bool scan_unconfirmed_outdate_tx();
@ -1347,11 +1319,11 @@ namespace tools
idle_condition_cb_t idle_condition_cb,
const currency::core_runtime_config &runtime_config)
{
cxt.rsp.status = API_RETURN_CODE_NOT_FOUND;
cxt.status = API_RETURN_CODE_NOT_FOUND;
uint64_t timstamp_last_idle_call = runtime_config.get_core_time();
cxt.iterations_processed = 0;
uint64_t ts_from = cxt.rsp.starter_timestamp; // median ts of last BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW blocks
uint64_t ts_from = cxt.starter_timestamp; // median ts of last BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW blocks
ts_from = ts_from - (ts_from % POS_SCAN_STEP) + POS_SCAN_STEP;
uint64_t ts_to = runtime_config.get_core_time() + CURRENCY_POS_BLOCK_FUTURE_TIME_LIMIT - 5;
ts_to = ts_to - (ts_to % POS_SCAN_STEP);
@ -1400,7 +1372,7 @@ namespace tools
if (!idle_condition_cb())
{
LOG_PRINT_L0("Detected new block, minting interrupted");
cxt.rsp.status = API_RETURN_CODE_NOT_FOUND;
cxt.status = API_RETURN_CODE_NOT_FOUND;
return false;
}
timstamp_last_idle_call = runtime_config.get_core_time();
@ -1420,9 +1392,9 @@ namespace tools
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;
cxt.index = transfer_index;
cxt.stake_unlock_time = stake_unlock_time;
cxt.status = API_RETURN_CODE_OK;
return true;
}
@ -1430,7 +1402,7 @@ namespace tools
}
++pos_entry_index;
}
cxt.rsp.status = API_RETURN_CODE_NOT_FOUND;
cxt.status = API_RETURN_CODE_NOT_FOUND;
return false;
}

View file

@ -1987,7 +1987,7 @@ void wallets_manager::wallet_vs_options::worker_func()
pos_minin_interval.do_call([this](){
tools::wallet2::mining_context ctx = AUTO_VAL_INIT(ctx);
LOG_PRINT_L1(get_log_prefix() + " Starting PoS mint iteration");
if (!w->get()->fill_mining_context(ctx) || ctx.rsp.status != API_RETURN_CODE_OK)
if (!w->get()->fill_mining_context(ctx) || ctx.status != API_RETURN_CODE_OK)
{
LOG_PRINT_L1(get_log_prefix() + " cannot obtain PoS mining context, skip iteration");
return true;
@ -2001,11 +2001,11 @@ void wallets_manager::wallet_vs_options::worker_func()
return *plast_daemon_network_state == currency::COMMAND_RPC_GET_INFO::daemon_network_state_online && *plast_daemon_height == last_wallet_synch_height;
}, core_conf);
if (ctx.rsp.status == API_RETURN_CODE_OK)
if (ctx.status == API_RETURN_CODE_OK)
{
w->get()->build_minted_block(ctx);
}
LOG_PRINT_L1(get_log_prefix() << " PoS mining iteration finished, status: " << ctx.rsp.status << ", used " << ctx.total_items_checked << " entries with total amount: " << currency::print_money_brief(ctx.total_amount_checked) << ", processed: " << ctx.iterations_processed << " iter.");
LOG_PRINT_L1(get_log_prefix() << " PoS mining iteration finished, status: " << ctx.status << ", used " << ctx.total_items_checked << " entries with total amount: " << currency::print_money_brief(ctx.total_amount_checked) << ", processed: " << ctx.iterations_processed << " iter.");
return true;
});
}

View file

@ -173,7 +173,7 @@ inline bool mine_next_pos_block_in_playtime_with_wallet(tools::wallet2& w, const
{
tools::wallet2::mining_context ctx = AUTO_VAL_INIT(ctx);
w.fill_mining_context(ctx);
if (!ctx.rsp.is_pos_allowed)
if (!ctx.is_pos_allowed)
return false;
@ -181,7 +181,7 @@ inline bool mine_next_pos_block_in_playtime_with_wallet(tools::wallet2& w, const
w.scan_pos(ctx, stop, [&w](){size_t blocks_fetched; w.refresh(blocks_fetched); return blocks_fetched == 0; }, w.get_core_runtime_config());
pos_entries_count = ctx.total_items_checked;
if (ctx.rsp.status != API_RETURN_CODE_OK)
if (ctx.status != API_RETURN_CODE_OK)
return false;
return w.build_minted_block(ctx, miner_address);