From c731d51a468e1238ff383419963cebe991d4b1de Mon Sep 17 00:00:00 2001 From: Green Sky Date: Mon, 10 Mar 2025 16:31:28 +0100 Subject: [PATCH] port to contact4, minor code changes --- plugins/plugin_crdtnotes.cpp | 4 +- plugins/plugin_crdtnotes_imgui.cpp | 4 +- plugins/plugin_crdtnotes_toxsync.cpp | 5 +-- .../crdtnotes_contact_sync_model.hpp | 12 +++--- src/solanaceae/crdtnotes/crdtnotes_sync.cpp | 27 ++++++++------ src/solanaceae/crdtnotes/crdtnotes_sync.hpp | 37 ++++++++++++------- .../crdtnotes_imgui/crdtnotes_imgui.cpp | 13 ++++--- .../crdtnotes_imgui/crdtnotes_imgui.hpp | 8 ++-- .../crdtnotes_toxsync/crdtnotes_toxsync.cpp | 31 ++++++++-------- .../crdtnotes_toxsync/crdtnotes_toxsync.hpp | 29 +++++++-------- 10 files changed, 94 insertions(+), 76 deletions(-) diff --git a/plugins/plugin_crdtnotes.cpp b/plugins/plugin_crdtnotes.cpp index 1ce1cd2..7f05e0c 100644 --- a/plugins/plugin_crdtnotes.cpp +++ b/plugins/plugin_crdtnotes.cpp @@ -32,12 +32,12 @@ SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api) } try { - auto* cr = PLUG_RESOLVE_INSTANCE_VERSIONED(Contact3Registry, "1"); + auto* cs = PLUG_RESOLVE_INSTANCE(ContactStore4I); // static store, could be anywhere tho // construct with fetched dependencies g_crdtn = std::make_unique(); - g_crdtns = std::make_unique(*g_crdtn, *cr); + g_crdtns = std::make_unique(*g_crdtn, *cs); // register types PLUG_PROVIDE_INSTANCE(CRDTNotesSync, plugin_name, g_crdtns.get()); diff --git a/plugins/plugin_crdtnotes_imgui.cpp b/plugins/plugin_crdtnotes_imgui.cpp index 8cc6473..b932320 100644 --- a/plugins/plugin_crdtnotes_imgui.cpp +++ b/plugins/plugin_crdtnotes_imgui.cpp @@ -33,7 +33,7 @@ SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api) try { auto* crdtns = PLUG_RESOLVE_INSTANCE(CRDTNotesSync); - auto* cr = PLUG_RESOLVE_INSTANCE_VERSIONED(Contact3Registry, "1"); + auto* cs = PLUG_RESOLVE_INSTANCE(ContactStore4I); auto* imguic = PLUG_RESOLVE_INSTANCE_VERSIONED(ImGuiContext, ImGui::GetVersion()); auto* imguimemaf = PLUG_RESOLVE_INSTANCE_VERSIONED(ImGuiMemAllocFunc, ImGui::GetVersion()); auto* imguimemff = PLUG_RESOLVE_INSTANCE_VERSIONED(ImGuiMemFreeFunc, ImGui::GetVersion()); @@ -45,7 +45,7 @@ SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api) // static store, could be anywhere tho // construct with fetched dependencies - g_crdtn_imgui = std::make_unique(*crdtns, *cr); + g_crdtn_imgui = std::make_unique(*crdtns, *cs); // register types PLUG_PROVIDE_INSTANCE(CRDTNotesImGui, plugin_name, g_crdtn_imgui.get()); diff --git a/plugins/plugin_crdtnotes_toxsync.cpp b/plugins/plugin_crdtnotes_toxsync.cpp index 70c8f6d..dfd5220 100644 --- a/plugins/plugin_crdtnotes_toxsync.cpp +++ b/plugins/plugin_crdtnotes_toxsync.cpp @@ -3,7 +3,6 @@ #include #include #include -#include #include #include #include @@ -37,14 +36,14 @@ SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api) try { auto* notes_sync = PLUG_RESOLVE_INSTANCE(CRDTNotesEventI); - auto* cr = PLUG_RESOLVE_INSTANCE_VERSIONED(Contact3Registry, "1"); + auto* cs = PLUG_RESOLVE_INSTANCE(ContactStore4I); auto* t = PLUG_RESOLVE_INSTANCE(ToxI); auto* tep = PLUG_RESOLVE_INSTANCE(ToxEventProviderI); auto* tcm = PLUG_RESOLVE_INSTANCE(ToxContactModel2); // static store, could be anywhere tho // construct with fetched dependencies - g_crdtn_ts = std::make_unique(*notes_sync, *cr, *t, *tep, *tcm); + g_crdtn_ts = std::make_unique(*notes_sync, *cs, *t, *tep, *tcm); // register types PLUG_PROVIDE_INSTANCE(CRDTNotesToxSync, plugin_name, g_crdtn_ts.get()); diff --git a/src/solanaceae/crdtnotes/crdtnotes_contact_sync_model.hpp b/src/solanaceae/crdtnotes/crdtnotes_contact_sync_model.hpp index d3e82dd..11103e8 100644 --- a/src/solanaceae/crdtnotes/crdtnotes_contact_sync_model.hpp +++ b/src/solanaceae/crdtnotes/crdtnotes_contact_sync_model.hpp @@ -2,7 +2,7 @@ #include "./crdtnotes.hpp" -#include +#include // send api struct CRDTNotesContactSyncModelI { @@ -12,12 +12,12 @@ struct CRDTNotesContactSyncModelI { public: // notify of doc existing virtual void SendGossip( - Contact3Handle c, + ContactHandle4 c, const CRDTNotes::DocID& doc_id ) = 0; virtual void SendGossip( - Contact3Handle c, + ContactHandle4 c, const CRDTNotes::DocID& doc_id, const std::vector& selected_frontier ) = 0; @@ -26,13 +26,13 @@ struct CRDTNotesContactSyncModelI { public: // causes the other peer to send gossip with all known frontiers (on cool down) virtual void SendFetchCompleteFrontier( - Contact3Handle c, + ContactHandle4 c, const CRDTNotes::DocID& doc_id ) = 0; // action range request virtual void SendFetchOps( - Contact3Handle c, + ContactHandle4 c, const CRDTNotes::DocID& doc_id, const CRDTNotes::CRDTAgent& agent, const uint64_t seq_from, @@ -41,7 +41,7 @@ struct CRDTNotesContactSyncModelI { public: // ops response virtual void SendOps( - Contact3Handle c, + ContactHandle4 c, const CRDTNotes::DocID& doc_id, // TODO: optimize this const std::vector& diff --git a/src/solanaceae/crdtnotes/crdtnotes_sync.cpp b/src/solanaceae/crdtnotes/crdtnotes_sync.cpp index 54a7ebf..cf7766f 100644 --- a/src/solanaceae/crdtnotes/crdtnotes_sync.cpp +++ b/src/solanaceae/crdtnotes/crdtnotes_sync.cpp @@ -1,5 +1,6 @@ #include "./crdtnotes_sync.hpp" +#include #include #include @@ -17,7 +18,7 @@ static ID32 id_from_vec(const std::vector& vec) { return new_id; } -CRDTNotesSync::CRDTNotesSync(CRDTNotes& notes, Contact3Registry& cr) : _notes(notes), _cr(cr) { +CRDTNotesSync::CRDTNotesSync(CRDTNotes& notes, ContactStore4I& cs) : _notes(notes), _cs(cs) { _rng.seed(std::random_device{}()); _rng.discard(707); } @@ -87,15 +88,17 @@ CRDTNotes::Doc* CRDTNotesSync::getDoc(const CRDTNotes::DocID& doc_id) { return _notes.getDoc(doc_id); } -std::optional CRDTNotesSync::addNewDoc(Contact3Handle c, bool secret) { +std::optional CRDTNotesSync::addNewDoc(ContactHandle4 c, bool secret) { if (!static_cast(c)) { std::cerr << "CRDTNS error: invalid contact\n"; return std::nullopt; } - const auto& self = c.get().self; - assert(_cr.all_of(self)); - const auto& self_id = _cr.get(self); + const auto& cr = _cs.registry(); + + const auto self = c.get().self; + assert(cr.all_of(self)); + const auto& self_id = cr.get(self); assert(!self_id.data.empty()); CRDTNotes::CRDTAgent self_agent_id = id_from_vec(self_id.data); @@ -127,15 +130,17 @@ std::optional CRDTNotesSync::addNewDoc(Contact3Handle c, bool return new_id; } -bool CRDTNotesSync::addDoc(const CRDTNotes::DocID& doc_id, Contact3Handle c) { +bool CRDTNotesSync::addDoc(const CRDTNotes::DocID& doc_id, ContactHandle4 c) { if (!static_cast(c)) { std::cerr << "CRDTNS error: invalid contact\n"; return false; } + const auto& cr = _cs.registry(); + const auto& self = c.get().self; - assert(_cr.all_of(self)); - const auto& self_id = _cr.get(self); + assert(cr.all_of(self)); + const auto& self_id = cr.get(self); assert(!self_id.data.empty()); CRDTNotes::CRDTAgent self_agent_id = id_from_vec(self_id.data); @@ -158,12 +163,12 @@ std::vector CRDTNotesSync::getDocList(void) { return _notes.getDocList(); } -std::vector CRDTNotesSync::getDocList(Contact3Handle c) { +std::vector CRDTNotesSync::getDocList(ContactHandle4 c) { std::vector list; - Contact3Handle parent; + ContactHandle4 parent; if (c.all_of()) { - parent = Contact3Handle{*c.registry(), c.get().parent}; + parent = ContactHandle4{*c.registry(), c.get().parent}; } for (const auto& [k, v] : _docs_contacts) { diff --git a/src/solanaceae/crdtnotes/crdtnotes_sync.hpp b/src/solanaceae/crdtnotes/crdtnotes_sync.hpp index a7c2008..e9ad822 100644 --- a/src/solanaceae/crdtnotes/crdtnotes_sync.hpp +++ b/src/solanaceae/crdtnotes/crdtnotes_sync.hpp @@ -2,10 +2,15 @@ #include "./crdtnotes.hpp" -#include +#include + +#include +#include #include #include +#include +#include // fwd struct CRDTNotesContactSyncModelI; @@ -14,7 +19,7 @@ namespace Events { // - DocID struct NGCEXT_crdtns_gossip { - Contact3Handle c; + ContactHandle4 c; CRDTNotes::DocID doc_id; }; @@ -24,14 +29,14 @@ namespace Events { // - seq (frontier) // - ] struct NGCEXT_crdtns_gossip_frontier { - Contact3Handle c; + ContactHandle4 c; CRDTNotes::DocID doc_id; std::vector selected_frontier; }; // - DocID struct NGCEXT_crdtns_fetch_complete_frontier { - Contact3Handle c; + ContactHandle4 c; CRDTNotes::DocID doc_id; }; @@ -40,7 +45,7 @@ namespace Events { // - seq_from // - seq_to struct NGCEXT_crdtns_fetch_op_range { - Contact3Handle c; + ContactHandle4 c; CRDTNotes::DocID doc_id; CRDTNotes::CRDTAgent agent; uint64_t seq_from; @@ -52,7 +57,7 @@ namespace Events { // - op // - ] struct NGCEXT_crdtns_ops { - Contact3Handle c; + ContactHandle4 c; CRDTNotes::DocID doc_id; std::vector ops; }; @@ -76,18 +81,24 @@ struct CRDTNotesEventI { class CRDTNotesSync final : public CRDTNotesEventI { // pull inside???? CRDTNotes& _notes; - Contact3Registry& _cr; + ContactStore4I& _cs; std::default_random_engine _rng; - std::unordered_map> _docs_contacts; + std::unordered_map> _docs_contacts; + struct Peer { + // global frontier + // what we know the peer knows(/gossiped) about + std::unordered_map other_frontier; + }; + std::unordered_map> _docs_peers; // if a doc is eg new, it is added here - std::set _gossip_queue; + std::set _gossip_queue; // TODO: no std::set _fetch_frontier_queue; public: - CRDTNotesSync(CRDTNotes& notes, Contact3Registry& cr); + CRDTNotesSync(CRDTNotes& notes, ContactStore4I& cs); ~CRDTNotesSync(void); @@ -99,14 +110,14 @@ class CRDTNotesSync final : public CRDTNotesEventI { // adds a doc and assosiates contact (and self) // if secret, only self is added (and thats why contact is needed) - std::optional addNewDoc(Contact3Handle c, bool secret = false); + std::optional addNewDoc(ContactHandle4 c, bool secret = false); // adds a doc by id to a contact // (for gossip or manual add) - bool addDoc(const CRDTNotes::DocID& doc_id, Contact3Handle c); + bool addDoc(const CRDTNotes::DocID& doc_id, ContactHandle4 c); std::vector getDocList(void); - std::vector getDocList(Contact3Handle c); + std::vector getDocList(ContactHandle4 c); void merge(const CRDTNotes::DocID& doc_id, std::string_view new_text); diff --git a/src/solanaceae/crdtnotes_imgui/crdtnotes_imgui.cpp b/src/solanaceae/crdtnotes_imgui/crdtnotes_imgui.cpp index 9eedd55..678f7fb 100644 --- a/src/solanaceae/crdtnotes_imgui/crdtnotes_imgui.cpp +++ b/src/solanaceae/crdtnotes_imgui/crdtnotes_imgui.cpp @@ -1,5 +1,6 @@ #include "./crdtnotes_imgui.hpp" +#include #include #include @@ -47,7 +48,7 @@ namespace detail { } // detail -CRDTNotesImGui::CRDTNotesImGui(CRDTNotesSync& notes_sync, Contact3Registry& cr) : _notes_sync(notes_sync), _cr(cr) { +CRDTNotesImGui::CRDTNotesImGui(CRDTNotesSync& notes_sync, ContactStore4I& cs) : _notes_sync(notes_sync), _cs(cs) { } float CRDTNotesImGui::render(void) { @@ -58,7 +59,7 @@ float CRDTNotesImGui::render(void) { } if (ImGui::BeginPopup("create new doc contact")) { - for (const auto& c : _cr.view()) { + for (const auto& c : _cs.registry().view()) { if (renderContactListContactSmall(c, false)) { //const auto& self = _cr.get(c).self; //assert(_cr.all_of(self)); @@ -76,7 +77,7 @@ float CRDTNotesImGui::render(void) { //// tox id (id from self) //self_agent_id //); - _notes_sync.addNewDoc({_cr, c}, false); + _notes_sync.addNewDoc(_cs.contactHandle(c), false); //// and open the doc } @@ -122,10 +123,12 @@ float CRDTNotesImGui::render(void) { return 1.f; } -bool CRDTNotesImGui::renderContactListContactSmall(const Contact3 c, const bool selected) const { +bool CRDTNotesImGui::renderContactListContactSmall(const Contact4 c, const bool selected) const { std::string label; - label += (_cr.all_of(c) ? _cr.get(c).name.c_str() : ""); + const auto& cr = _cs.registry(); + + label += (cr.all_of(c) ? cr.get(c).name.c_str() : ""); label += "###"; label += std::to_string(entt::to_integral(c)); diff --git a/src/solanaceae/crdtnotes_imgui/crdtnotes_imgui.hpp b/src/solanaceae/crdtnotes_imgui/crdtnotes_imgui.hpp index 9033255..9b6c4be 100644 --- a/src/solanaceae/crdtnotes_imgui/crdtnotes_imgui.hpp +++ b/src/solanaceae/crdtnotes_imgui/crdtnotes_imgui.hpp @@ -1,24 +1,24 @@ #pragma once #include -#include +#include #include class CRDTNotesImGui { CRDTNotesSync& _notes_sync; - Contact3Registry& _cr; + ContactStore4I& _cs; bool _show_global_list {true}; std::set _open_docs; public: - CRDTNotesImGui(CRDTNotesSync& notes_sync, Contact3Registry& cr); + CRDTNotesImGui(CRDTNotesSync& notes_sync, ContactStore4I& cs); float render(void); - bool renderContactListContactSmall(const Contact3 c, const bool selected) const; + bool renderContactListContactSmall(const Contact4 c, const bool selected) const; bool renderDoc(const CRDTNotes::DocID& doc_id); bool renderDocText(std::string& text) const; diff --git a/src/solanaceae/crdtnotes_toxsync/crdtnotes_toxsync.cpp b/src/solanaceae/crdtnotes_toxsync/crdtnotes_toxsync.cpp index c20cbe2..ba54b1d 100644 --- a/src/solanaceae/crdtnotes_toxsync/crdtnotes_toxsync.cpp +++ b/src/solanaceae/crdtnotes_toxsync/crdtnotes_toxsync.cpp @@ -1,5 +1,6 @@ #include "./crdtnotes_toxsync.hpp" +#include #include #include @@ -39,11 +40,11 @@ enum class NGCEXT_Event : uint8_t { CRDTNotesToxSync::CRDTNotesToxSync( CRDTNotesEventI& notes_sync, - Contact3Registry& cr, + ContactStore4I& cs, ToxI& t, ToxEventProviderI& tep, ToxContactModel2& tcm -) : _notes_sync(notes_sync), _cr(cr), _t(t), _tep_sr(tep.newSubRef(this)), _tcm(tcm) { +) : _notes_sync(notes_sync), _cs(cs), _t(t), _tep_sr(tep.newSubRef(this)), _tcm(tcm) { // TODO: non groups // should be called for every peer (except self) @@ -58,13 +59,13 @@ CRDTNotesToxSync::CRDTNotesToxSync( CRDTNotesToxSync::~CRDTNotesToxSync(void) { // TODO: find a better way to remove dangling pointers - std::vector to_remove_self; - _cr.view().each([&to_remove_self, this](Contact3 c, const auto* csm) { + std::vector to_remove_self; + _cs.registry().view().each([&to_remove_self, this](Contact4 c, const auto* csm) { if (this == csm) { to_remove_self.push_back(c); } }); - _cr.remove(to_remove_self.cbegin(), to_remove_self.cend()); + _cs.registry().remove(to_remove_self.cbegin(), to_remove_self.cend()); } float CRDTNotesToxSync::iterate(float time_delta) { @@ -73,7 +74,7 @@ float CRDTNotesToxSync::iterate(float time_delta) { } void CRDTNotesToxSync::SendGossip( - Contact3Handle c, + ContactHandle4 c, const CRDTNotes::DocID& doc_id ) { if (!c.all_of()) { @@ -98,7 +99,7 @@ void CRDTNotesToxSync::SendGossip( } void CRDTNotesToxSync::SendGossip( - Contact3Handle c, + ContactHandle4 c, const CRDTNotes::DocID& doc_id, const std::vector& selected_frontier ) { @@ -139,7 +140,7 @@ void CRDTNotesToxSync::SendGossip( } void CRDTNotesToxSync::SendFetchCompleteFrontier( - Contact3Handle c, + ContactHandle4 c, const CRDTNotes::DocID& doc_id ) { if (!c.all_of()) { @@ -164,7 +165,7 @@ void CRDTNotesToxSync::SendFetchCompleteFrontier( } void CRDTNotesToxSync::SendFetchOps( - Contact3Handle c, + ContactHandle4 c, const CRDTNotes::DocID& doc_id, const CRDTNotes::CRDTAgent& agent, const uint64_t seq_from, @@ -207,7 +208,7 @@ void CRDTNotesToxSync::SendFetchOps( } void CRDTNotesToxSync::SendOps( - Contact3Handle c, + ContactHandle4 c, const CRDTNotes::DocID& doc_id, const std::vector& ops ) { @@ -288,7 +289,7 @@ void CRDTNotesToxSync::SendOps( } bool CRDTNotesToxSync::parse_crdtn_gossip( - Contact3Handle c, + ContactHandle4 c, const uint8_t* data, size_t data_size, bool // dont care private ) { @@ -313,7 +314,7 @@ bool CRDTNotesToxSync::parse_crdtn_gossip( } bool CRDTNotesToxSync::parse_crdtn_gossip_frontier( - Contact3Handle c, + ContactHandle4 c, const uint8_t* data, size_t data_size, bool // dont care private ) { @@ -351,7 +352,7 @@ bool CRDTNotesToxSync::parse_crdtn_gossip_frontier( } bool CRDTNotesToxSync::parse_crdtn_fetch_complete_frontier( - Contact3Handle c, + ContactHandle4 c, const uint8_t* data, size_t data_size, bool // dont care private ) { @@ -371,7 +372,7 @@ bool CRDTNotesToxSync::parse_crdtn_fetch_complete_frontier( } bool CRDTNotesToxSync::parse_crdtn_fetch_op_range( - Contact3Handle c, + ContactHandle4 c, const uint8_t* data, size_t data_size, bool // dont care private ) { @@ -408,7 +409,7 @@ bool CRDTNotesToxSync::parse_crdtn_fetch_op_range( } bool CRDTNotesToxSync::parse_crdtn_ops( - Contact3Handle c, + ContactHandle4 c, const uint8_t* data, size_t data_size, bool // dont care private ) { diff --git a/src/solanaceae/crdtnotes_toxsync/crdtnotes_toxsync.hpp b/src/solanaceae/crdtnotes_toxsync/crdtnotes_toxsync.hpp index a8e301b..1856e55 100644 --- a/src/solanaceae/crdtnotes_toxsync/crdtnotes_toxsync.hpp +++ b/src/solanaceae/crdtnotes_toxsync/crdtnotes_toxsync.hpp @@ -1,9 +1,8 @@ #pragma once -#include "solanaceae/crdtnotes/crdtnotes_sync.hpp" -#include +#include #include -#include +#include #include #include @@ -14,7 +13,7 @@ struct ToxEventProviderI; // implements CRDTNotesContactSyncModelI and attaches itself to tox contacts class CRDTNotesToxSync : public CRDTNotesContactSyncModelI, public ToxEventI { CRDTNotesEventI& _notes_sync; - Contact3Registry& _cr; + ContactStore4I& _cs; ToxI& _t; ToxEventProviderI::SubscriptionReference _tep_sr; ToxContactModel2& _tcm; @@ -22,7 +21,7 @@ class CRDTNotesToxSync : public CRDTNotesContactSyncModelI, public ToxEventI { public: CRDTNotesToxSync( CRDTNotesEventI& notes_sync, - Contact3Registry& cr, + ContactStore4I& cs, ToxI& t, ToxEventProviderI& tep, ToxContactModel2& tcm @@ -33,23 +32,23 @@ class CRDTNotesToxSync : public CRDTNotesContactSyncModelI, public ToxEventI { public: // sync api void SendGossip( - Contact3Handle c, + ContactHandle4 c, const CRDTNotes::DocID& doc_id ) override; void SendGossip( - Contact3Handle c, + ContactHandle4 c, const CRDTNotes::DocID& doc_id, const std::vector& selected_frontier ) override; void SendFetchCompleteFrontier( - Contact3Handle c, + ContactHandle4 c, const CRDTNotes::DocID& doc_id ) override; void SendFetchOps( - Contact3Handle c, + ContactHandle4 c, const CRDTNotes::DocID& doc_id, const CRDTNotes::CRDTAgent& agent, const uint64_t seq_from, @@ -57,34 +56,34 @@ class CRDTNotesToxSync : public CRDTNotesContactSyncModelI, public ToxEventI { ) override; void SendOps( - Contact3Handle c, + ContactHandle4 c, const CRDTNotes::DocID& doc_id, const std::vector& ) override; private: bool parse_crdtn_gossip( - Contact3Handle c, + ContactHandle4 c, const uint8_t* data, size_t data_size, bool _private ); bool parse_crdtn_gossip_frontier( - Contact3Handle c, + ContactHandle4 c, const uint8_t* data, size_t data_size, bool _private ); bool parse_crdtn_fetch_complete_frontier( - Contact3Handle c, + ContactHandle4 c, const uint8_t* data, size_t data_size, bool _private ); bool parse_crdtn_fetch_op_range( - Contact3Handle c, + ContactHandle4 c, const uint8_t* data, size_t data_size, bool _private ); bool parse_crdtn_ops( - Contact3Handle c, + ContactHandle4 c, const uint8_t* data, size_t data_size, bool _private );