Skip to content

Commit

Permalink
serialize enote_store
Browse files Browse the repository at this point in the history
  • Loading branch information
DangerousFreedom1984 committed May 31, 2024
1 parent ace9228 commit a54bbcc
Show file tree
Hide file tree
Showing 14 changed files with 832 additions and 50 deletions.
58 changes: 58 additions & 0 deletions src/seraphis_core/legacy_enote_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,62 @@ LegacyEnoteV5 gen_legacy_enote_v5()
return temp;
}
//-------------------------------------------------------------------------------------------------------------------
bool operator==(const LegacyEnoteV1 &a, const LegacyEnoteV1 &b)
{
return a.amount == b.amount &&
a.onetime_address == b.onetime_address;
}
//-------------------------------------------------------------------------------------------------------------------
bool operator==(const LegacyEnoteV2 &a, const LegacyEnoteV2 &b)
{
return a.onetime_address == b.onetime_address &&
a.amount_commitment == b.amount_commitment &&
a.encoded_amount == b.encoded_amount &&
a.encoded_amount_blinding_factor == b.encoded_amount_blinding_factor;
}
//-------------------------------------------------------------------------------------------------------------------
bool operator==(const LegacyEnoteV3 &a, const LegacyEnoteV3 &b)
{
return a.onetime_address == b.onetime_address &&
a.amount_commitment == b.amount_commitment &&
a.encoded_amount == b.encoded_amount;
}
//-------------------------------------------------------------------------------------------------------------------
bool operator==(const LegacyEnoteV4 &a, const LegacyEnoteV4 &b)
{
return a.amount == b.amount &&
a.onetime_address == b.onetime_address &&
a.view_tag == b.view_tag;
}
//-------------------------------------------------------------------------------------------------------------------
bool operator==(const LegacyEnoteV5 &a, const LegacyEnoteV5 &b)
{
return a.onetime_address == b.onetime_address &&
a.amount_commitment == b.amount_commitment &&
a.encoded_amount == b.encoded_amount &&
a.view_tag == b.view_tag;
}
//-------------------------------------------------------------------------------------------------------------------
bool operator==(const LegacyEnoteVariant &variant1, const LegacyEnoteVariant &variant2)
{
// check they have the same type
if (!LegacyEnoteVariant::same_type(variant1, variant2))
return false;

// use a visitor to test equality
struct visitor final : public tools::variant_static_visitor<bool>
{
visitor(const LegacyEnoteVariant &other_ref) : other{other_ref} {}
const LegacyEnoteVariant &other;

using variant_static_visitor::operator(); //for blank overload
bool operator()(const LegacyEnoteV1 &enote) const { return enote == other.unwrap<LegacyEnoteV1>(); }
bool operator()(const LegacyEnoteV2 &enote) const { return enote == other.unwrap<LegacyEnoteV2>(); }
bool operator()(const LegacyEnoteV3 &enote) const { return enote == other.unwrap<LegacyEnoteV3>(); }
bool operator()(const LegacyEnoteV4 &enote) const { return enote == other.unwrap<LegacyEnoteV4>(); }
bool operator()(const LegacyEnoteV5 &enote) const { return enote == other.unwrap<LegacyEnoteV5>(); }
};

return variant1.visit(visitor{variant2});
}
} //namespace sp
7 changes: 7 additions & 0 deletions src/seraphis_core/legacy_enote_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,11 @@ LegacyEnoteV4 gen_legacy_enote_v4();
*/
LegacyEnoteV5 gen_legacy_enote_v5();

bool operator==(const LegacyEnoteV1 &a, const LegacyEnoteV1 &b);
bool operator==(const LegacyEnoteV2 &a, const LegacyEnoteV2 &b);
bool operator==(const LegacyEnoteV3 &a, const LegacyEnoteV3 &b);
bool operator==(const LegacyEnoteV4 &a, const LegacyEnoteV4 &b);
bool operator==(const LegacyEnoteV5 &a, const LegacyEnoteV5 &b);
bool operator==(const LegacyEnoteVariant &variant1, const LegacyEnoteVariant &variant2);

} //namespace sp
14 changes: 14 additions & 0 deletions src/seraphis_impl/checkpoint_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@
namespace sp
{
//-------------------------------------------------------------------------------------------------------------------
bool operator==(const CheckpointCacheConfig &a, const CheckpointCacheConfig &b)
{
return a.num_unprunable == b.num_unprunable &&
a.max_separation == b.max_separation &&
a.density_factor == b.density_factor;
}
//-------------------------------------------------------------------------------------------------------------------
CheckpointCache::CheckpointCache(const CheckpointCacheConfig &config, const std::uint64_t min_checkpoint_index) :
m_config{config},
m_min_checkpoint_index{min_checkpoint_index}
Expand Down Expand Up @@ -296,4 +303,11 @@ void CheckpointCache::prune_checkpoints()
}
}
//-------------------------------------------------------------------------------------------------------------------
bool CheckpointCache::operator==(const CheckpointCache &other)
{
return m_min_checkpoint_index == other.m_min_checkpoint_index &&
m_config == other.m_config &&
m_window_size == other.m_window_size &&
m_checkpoints == other.m_checkpoints;
}
} //namespace sp
24 changes: 19 additions & 5 deletions src/seraphis_impl/checkpoint_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

//local headers
#include "ringct/rctTypes.h"
#include "serialization/serialization.h"

//third party headers

Expand All @@ -46,7 +47,6 @@

namespace sp
{

/// Configuration details for a checkpoint cache.
struct CheckpointCacheConfig final
{
Expand All @@ -61,6 +61,8 @@ struct CheckpointCacheConfig final
std::uint64_t density_factor;
};

bool operator==(const CheckpointCacheConfig &a, const CheckpointCacheConfig &b);

////
// CheckpointCache
// - stores a sequence of checkpoints in the range of block ids [refresh index, highest known block index]
Expand All @@ -78,6 +80,7 @@ class CheckpointCache
{
public:
//constructors
CheckpointCache() = default;
CheckpointCache(const CheckpointCacheConfig &config, const std::uint64_t min_checkpoint_index);

//member functions
Expand All @@ -101,6 +104,9 @@ class CheckpointCache
/// insert block ids starting at the specified index (all old blocks >= first_block_index will be removed)
void insert_new_block_ids(const std::uint64_t first_block_index, const std::vector<rct::key> &new_block_ids);

/// comparison operator
bool operator==(const CheckpointCache &other);

private:
/// get the window's prune candidate
std::deque<std::uint64_t>::const_iterator get_window_prune_candidate(const std::deque<std::uint64_t> &window) const;
Expand All @@ -113,14 +119,22 @@ class CheckpointCache

//member variables
/// minimum checkpoint index
const std::uint64_t m_min_checkpoint_index;
std::uint64_t m_min_checkpoint_index;

/// config
const CheckpointCacheConfig m_config;
static const std::uint64_t m_window_size{3};
CheckpointCacheConfig m_config;
std::uint64_t m_window_size{3};

/// stored checkpoints
std::map<std::uint64_t, rct::key> m_checkpoints;
};

public:
/// de/serialize CheckpointCache
BEGIN_SERIALIZE_OBJECT()
FIELD(m_min_checkpoint_index)
FIELD(m_config)
FIELD(m_window_size)
FIELD(m_checkpoints)
END_SERIALIZE()
};
} //namespace sp
15 changes: 15 additions & 0 deletions src/seraphis_impl/enote_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,21 @@ void SpEnoteStore::update_with_sp_records_from_ledger(const rct::key &alignment_
this->handle_legacy_key_images_from_sp_selfsends(legacy_key_images_in_sp_selfsends, events_inout);
}
//-------------------------------------------------------------------------------------------------------------------
bool SpEnoteStore::operator==(const SpEnoteStore &other)
{
return m_legacy_intermediate_contextual_enote_records == other.m_legacy_intermediate_contextual_enote_records &&
m_legacy_contextual_enote_records == other.m_legacy_contextual_enote_records &&
m_sp_contextual_enote_records == other.m_sp_contextual_enote_records &&
m_legacy_key_images == other.m_legacy_key_images &&
m_tracked_legacy_onetime_address_duplicates == other.m_tracked_legacy_onetime_address_duplicates &&
m_legacy_block_id_cache == other.m_legacy_block_id_cache &&
m_sp_block_id_cache == other.m_sp_block_id_cache &&
m_legacy_partialscan_index == other.m_legacy_partialscan_index &&
m_legacy_fullscan_index == other.m_legacy_fullscan_index &&
m_sp_scanned_index == other.m_sp_scanned_index &&
m_default_spendable_age == other.m_default_spendable_age;
}
//-------------------------------------------------------------------------------------------------------------------
// ENOTE STORE INTERNAL
//-------------------------------------------------------------------------------------------------------------------
void SpEnoteStore::update_with_new_blocks_from_ledger_legacy_partialscan(const rct::key &alignment_block_id,
Expand Down
29 changes: 29 additions & 0 deletions src/seraphis_impl/enote_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@
#include "ringct/rctTypes.h"
#include "seraphis_impl/checkpoint_cache.h"
#include "seraphis_impl/enote_store_event_types.h"
#include "seraphis_impl/seraphis_serialization.h"
#include "seraphis_main/contextual_enote_record_types.h"
#include "serialization/serialization.h"
#include "serialization/binary_archive.h"
#include "serialization/containers.h"
#include "serialization/crypto.h"
#include "serialization/string.h"
#include "serialization/pair.h"
#include "serialization/tuple.h"

//third party headers

Expand All @@ -57,6 +65,8 @@ class SpEnoteStore final
{
public:
//constructors
/// default constructor
SpEnoteStore() = default;
/// normal constructor
SpEnoteStore(const std::uint64_t refresh_index,
const std::uint64_t first_sp_enabled_block_in_chain,
Expand Down Expand Up @@ -185,6 +195,8 @@ class SpEnoteStore final
const std::unordered_map<crypto::key_image, SpEnoteSpentContextV1> &legacy_key_images_in_sp_selfsends,
std::list<EnoteStoreEvent> &events_inout);

bool operator==(const SpEnoteStore &other);

private:
/// update the store with a set of new block ids from the ledger
void update_with_new_blocks_from_ledger_legacy_partialscan(const rct::key &alignment_block_id,
Expand Down Expand Up @@ -294,6 +306,23 @@ class SpEnoteStore final
/// on-chain and the next block's index is >= 'enote origin index + max(1, default_spendable_age)'; legacy
/// enotes also have an unlock_time attribute on top of the default spendable age
std::uint64_t m_default_spendable_age{0};

public:
/// de/serialize SpEnoteStore
BEGIN_SERIALIZE_OBJECT()
FIELD(m_legacy_intermediate_contextual_enote_records)
FIELD(m_legacy_contextual_enote_records)
FIELD(m_sp_contextual_enote_records)
FIELD(m_legacy_key_images_in_sp_selfsends)
FIELD(m_tracked_legacy_onetime_address_duplicates)
FIELD(m_legacy_key_images)
FIELD(m_legacy_block_id_cache)
FIELD(m_sp_block_id_cache)
VARINT_FIELD(m_legacy_partialscan_index)
VARINT_FIELD(m_legacy_fullscan_index)
VARINT_FIELD(m_sp_scanned_index)
VARINT_FIELD(m_default_spendable_age)
END_SERIALIZE()
};

} //namespace sp
Loading

0 comments on commit a54bbcc

Please sign in to comment.