1
0
Fork 0
forked from lthn/blockchain

added code for handling chain detachment(reorg)

This commit is contained in:
cryptozoidberg 2022-09-29 19:18:05 +02:00
parent 1fd6e08e5c
commit 7d8259f58c
No known key found for this signature in database
GPG key ID: 22DEB97A54C6FDEC
3 changed files with 41 additions and 14 deletions

View file

@ -1711,6 +1711,13 @@ namespace currency
return true;
}
crypto::hash get_asset_id_from_descriptor(const asset_descriptor_base& adb)
{
return get_hash_from_POD_objects(CRYPTO_HDS_ASSET_ID, adb.owner);
}
bool construct_tx(const account_keys& sender_account_keys, const finalize_tx_param& ftp, finalized_tx& result)
{
const std::vector<tx_source_entry>& sources = ftp.sources;
@ -1797,6 +1804,19 @@ namespace currency
//std::vector<const tx_source_entry*> zc_sources;
//std::vector<const tx_source_entry*> NLSAG_sources;
crypto::hash asset_id_for_destinations = currency::null_hash;
if (tx.version > TRANSACTION_VERSION_PRE_HF4)
{
asset_descriptor_operation ado = AUTO_VAL_INIT(ado);
if (get_type_in_variant_container(tx.extra, ado))
{
crypto::secret_key stub = AUTO_VAL_INIT(stub);
bool r = derive_key_pair_from_key_pair(sender_account_keys.account_address.spend_public_key, one_time_secret_key, stub, ado.descriptor.owner, CRYPTO_HDS_ASSET_CONTROL_KEY);
CHECK_AND_ASSERT_MES(r, false, "Failed to derive_public_key_from_tx_and_account_pub_key()");
//also assign this asset id to destinations
asset_id_for_destinations = get_asset_id_from_descriptor(ado.descriptor);
}
}
std::vector<input_generation_context_data> in_contexts;
@ -1927,6 +1947,14 @@ namespace currency
// "Shuffle" outs
std::vector<tx_destination_entry> shuffled_dsts(destinations);
if (asset_id_for_destinations != currency::null_hash)
{
//must be asset publication
for (auto& item : shuffled_dsts)
{
item.asset_id = asset_id_for_destinations;
}
}
if (shuffle)
std::sort(shuffled_dsts.begin(), shuffled_dsts.end(), [](const tx_destination_entry& de1, const tx_destination_entry& de2) { return de1.amount < de2.amount; });
@ -1981,18 +2009,6 @@ namespace currency
}
}
if (tx.version > TRANSACTION_VERSION_PRE_HF4)
{
asset_descriptor_operation ado = AUTO_VAL_INIT(ado);
if (get_type_in_variant_container(tx.extra, ado))
{
crypto::secret_key stub = AUTO_VAL_INIT(stub);
bool r = derive_key_pair_from_key_pair(sender_account_keys.account_address.spend_public_key, one_time_secret_key, stub, ado.descriptor.owner, CRYPTO_HDS_ASSET_CONTROL_KEY);
CHECK_AND_ASSERT_MES(r, false, "Failed to derive_public_key_from_tx_and_account_pub_key()");
}
}
//sort inputs and mapping if it's zarcanum hardfork
if (tx.version > TRANSACTION_VERSION_PRE_HF4)
{

View file

@ -775,10 +775,10 @@ void wallet2::process_new_transaction(const currency::transaction& tx, uint64_t
LOG_ERROR("Public key from asset_descriptor_operation(" << ado.descriptor.owner << ") not much with derived public key(" << self_check << "), for tx" << get_transaction_hash(tx));
}
else
{
{
wallet_own_asset_context& asset_context = m_own_asset_descriptors[get_hash_from_POD_objects(CRYPTO_HDS_ASSET_ID, ado.descriptor.owner)];
asset_context.asset_descriptor = ado.descriptor;
asset_context.height = height;
std::stringstream ss;
ss << "New Asset Registered:"
<< ENDL << "Name: " << asset_context.asset_descriptor.full_name
@ -2550,6 +2550,15 @@ void wallet2::detach_blockchain(uint64_t including_height)
++it;
}
//asset descriptors
for (auto it = m_own_asset_descriptors.begin(); it != m_own_asset_descriptors.end(); )
{
if (including_height <= it->second.height)
it = m_own_asset_descriptors.erase(it);
else
++it;
}
WLT_LOG_L0("Detached blockchain on height " << including_height << ", transfers detached " << transfers_detached << ", blocks detached " << blocks_detached);
}
//----------------------------------------------------------------------------------------------------

View file

@ -510,10 +510,12 @@ namespace tools
{
currency::asset_descriptor_base asset_descriptor;
crypto::secret_key control_key;
uint64_t height = 0;
BEGIN_BOOST_SERIALIZATION()
BOOST_SERIALIZE(asset_descriptor)
BOOST_SERIALIZE(control_key)
BOOST_SERIALIZE(height)
END_BOOST_SERIALIZATION()
};