port to contact4, minor code changes

This commit is contained in:
Green Sky 2025-03-10 16:31:28 +01:00
parent bb45379199
commit c731d51a46
No known key found for this signature in database
GPG Key ID: DBE05085D874AB4A
10 changed files with 94 additions and 76 deletions

View File

@ -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<CRDTNotes>();
g_crdtns = std::make_unique<CRDTNotesSync>(*g_crdtn, *cr);
g_crdtns = std::make_unique<CRDTNotesSync>(*g_crdtn, *cs);
// register types
PLUG_PROVIDE_INSTANCE(CRDTNotesSync, plugin_name, g_crdtns.get());

View File

@ -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<CRDTNotesImGui>(*crdtns, *cr);
g_crdtn_imgui = std::make_unique<CRDTNotesImGui>(*crdtns, *cs);
// register types
PLUG_PROVIDE_INSTANCE(CRDTNotesImGui, plugin_name, g_crdtn_imgui.get());

View File

@ -3,7 +3,6 @@
#include <solanaceae/crdtnotes/crdtnotes.hpp>
#include <solanaceae/crdtnotes/crdtnotes_sync.hpp>
#include <solanaceae/crdtnotes_toxsync/crdtnotes_toxsync.hpp>
#include <solanaceae/contact/contact_model3.hpp>
#include <solanaceae/toxcore/tox_interface.hpp>
#include <solanaceae/toxcore/tox_event_interface.hpp>
#include <solanaceae/tox_contacts/tox_contact_model2.hpp>
@ -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<CRDTNotesToxSync>(*notes_sync, *cr, *t, *tep, *tcm);
g_crdtn_ts = std::make_unique<CRDTNotesToxSync>(*notes_sync, *cs, *t, *tep, *tcm);
// register types
PLUG_PROVIDE_INSTANCE(CRDTNotesToxSync, plugin_name, g_crdtn_ts.get());

View File

@ -2,7 +2,7 @@
#include "./crdtnotes.hpp"
#include <solanaceae/contact/contact_model3.hpp>
#include <solanaceae/contact/fwd.hpp>
// 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<CRDTNotes::Frontier>& 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<CRDTNotes::Doc::Op>&

View File

@ -1,5 +1,6 @@
#include "./crdtnotes_sync.hpp"
#include <solanaceae/contact/contact_store_i.hpp>
#include <solanaceae/crdtnotes/crdtnotes_contact_sync_model.hpp>
#include <solanaceae/contact/components.hpp>
@ -17,7 +18,7 @@ static ID32 id_from_vec(const std::vector<uint8_t>& 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<CRDTNotes::DocID> CRDTNotesSync::addNewDoc(Contact3Handle c, bool secret) {
std::optional<CRDTNotes::DocID> CRDTNotesSync::addNewDoc(ContactHandle4 c, bool secret) {
if (!static_cast<bool>(c)) {
std::cerr << "CRDTNS error: invalid contact\n";
return std::nullopt;
}
const auto& self = c.get<Contact::Components::Self>().self;
assert(_cr.all_of<Contact::Components::ID>(self));
const auto& self_id = _cr.get<Contact::Components::ID>(self);
const auto& cr = _cs.registry();
const auto self = c.get<Contact::Components::Self>().self;
assert(cr.all_of<Contact::Components::ID>(self));
const auto& self_id = cr.get<Contact::Components::ID>(self);
assert(!self_id.data.empty());
CRDTNotes::CRDTAgent self_agent_id = id_from_vec(self_id.data);
@ -127,15 +130,17 @@ std::optional<CRDTNotes::DocID> 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<bool>(c)) {
std::cerr << "CRDTNS error: invalid contact\n";
return false;
}
const auto& cr = _cs.registry();
const auto& self = c.get<Contact::Components::Self>().self;
assert(_cr.all_of<Contact::Components::ID>(self));
const auto& self_id = _cr.get<Contact::Components::ID>(self);
assert(cr.all_of<Contact::Components::ID>(self));
const auto& self_id = cr.get<Contact::Components::ID>(self);
assert(!self_id.data.empty());
CRDTNotes::CRDTAgent self_agent_id = id_from_vec(self_id.data);
@ -158,12 +163,12 @@ std::vector<CRDTNotes::DocID> CRDTNotesSync::getDocList(void) {
return _notes.getDocList();
}
std::vector<CRDTNotes::DocID> CRDTNotesSync::getDocList(Contact3Handle c) {
std::vector<CRDTNotes::DocID> CRDTNotesSync::getDocList(ContactHandle4 c) {
std::vector<CRDTNotes::DocID> list;
Contact3Handle parent;
ContactHandle4 parent;
if (c.all_of<Contact::Components::Parent>()) {
parent = Contact3Handle{*c.registry(), c.get<Contact::Components::Parent>().parent};
parent = ContactHandle4{*c.registry(), c.get<Contact::Components::Parent>().parent};
}
for (const auto& [k, v] : _docs_contacts) {

View File

@ -2,10 +2,15 @@
#include "./crdtnotes.hpp"
#include <solanaceae/contact/contact_model3.hpp>
#include <solanaceae/contact/fwd.hpp>
#include <entt/entity/registry.hpp>
#include <entt/entity/handle.hpp>
#include <set>
#include <random>
#include <unordered_map>
#include <map>
// 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<CRDTNotes::Frontier> 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<CRDTNotes::Doc::Op> 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<CRDTNotes::DocID, std::set<Contact3Handle>> _docs_contacts;
std::unordered_map<CRDTNotes::DocID, std::set<ContactHandle4>> _docs_contacts;
struct Peer {
// global frontier
// what we know the peer knows(/gossiped) about
std::unordered_map<decltype(CRDTNotes::Frontier::agent), decltype(CRDTNotes::Frontier::seq)> other_frontier;
};
std::unordered_map<CRDTNotes::DocID, std::map<ContactHandle4, Peer>> _docs_peers;
// if a doc is eg new, it is added here
std::set<CRDTNotes::DocID> _gossip_queue;
std::set<CRDTNotes::DocID> _gossip_queue; // TODO: no
std::set<CRDTNotes::DocID> _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<CRDTNotes::DocID> addNewDoc(Contact3Handle c, bool secret = false);
std::optional<CRDTNotes::DocID> 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<CRDTNotes::DocID> getDocList(void);
std::vector<CRDTNotes::DocID> getDocList(Contact3Handle c);
std::vector<CRDTNotes::DocID> getDocList(ContactHandle4 c);
void merge(const CRDTNotes::DocID& doc_id, std::string_view new_text);

View File

@ -1,5 +1,6 @@
#include "./crdtnotes_imgui.hpp"
#include <solanaceae/contact/contact_store_i.hpp>
#include <solanaceae/contact/components.hpp>
#include <cstdint>
@ -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<Contact::Components::TagBig>()) {
for (const auto& c : _cs.registry().view<Contact::Components::TagBig>()) {
if (renderContactListContactSmall(c, false)) {
//const auto& self = _cr.get<Contact::Components::Self>(c).self;
//assert(_cr.all_of<Contact::Components::ID>(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<Contact::Components::Name>(c) ? _cr.get<Contact::Components::Name>(c).name.c_str() : "<unk>");
const auto& cr = _cs.registry();
label += (cr.all_of<Contact::Components::Name>(c) ? cr.get<Contact::Components::Name>(c).name.c_str() : "<unk>");
label += "###";
label += std::to_string(entt::to_integral(c));

View File

@ -1,24 +1,24 @@
#pragma once
#include <solanaceae/crdtnotes/crdtnotes_sync.hpp>
#include <solanaceae/contact/contact_model3.hpp>
#include <solanaceae/contact/fwd.hpp>
#include <set>
class CRDTNotesImGui {
CRDTNotesSync& _notes_sync;
Contact3Registry& _cr;
ContactStore4I& _cs;
bool _show_global_list {true};
std::set<CRDTNotes::DocID> _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;

View File

@ -1,5 +1,6 @@
#include "./crdtnotes_toxsync.hpp"
#include <solanaceae/contact/contact_store_i.hpp>
#include <solanaceae/toxcore/tox_interface.hpp>
#include <solanaceae/tox_contacts/components.hpp>
@ -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<Contact3> to_remove_self;
_cr.view<CRDTNotesContactSyncModelI*>().each([&to_remove_self, this](Contact3 c, const auto* csm) {
std::vector<Contact4> to_remove_self;
_cs.registry().view<CRDTNotesContactSyncModelI*>().each([&to_remove_self, this](Contact4 c, const auto* csm) {
if (this == csm) {
to_remove_self.push_back(c);
}
});
_cr.remove<CRDTNotesContactSyncModelI*>(to_remove_self.cbegin(), to_remove_self.cend());
_cs.registry().remove<CRDTNotesContactSyncModelI*>(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<Contact::Components::ToxGroupPeerEphemeral>()) {
@ -98,7 +99,7 @@ void CRDTNotesToxSync::SendGossip(
}
void CRDTNotesToxSync::SendGossip(
Contact3Handle c,
ContactHandle4 c,
const CRDTNotes::DocID& doc_id,
const std::vector<CRDTNotes::Frontier>& 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<Contact::Components::ToxGroupPeerEphemeral>()) {
@ -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<CRDTNotes::Doc::Op>& 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
) {

View File

@ -1,9 +1,8 @@
#pragma once
#include "solanaceae/crdtnotes/crdtnotes_sync.hpp"
#include <solanaceae/crdtnotes/crdtnotes.hpp>
#include <solanaceae/crdtnotes/crdtnotes_sync.hpp>
#include <solanaceae/crdtnotes/crdtnotes_contact_sync_model.hpp>
#include <solanaceae/contact/contact_model3.hpp>
#include <solanaceae/contact/fwd.hpp>
#include <solanaceae/toxcore/tox_event_interface.hpp>
#include <solanaceae/tox_contacts/tox_contact_model2.hpp>
@ -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<CRDTNotes::Frontier>& 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<CRDTNotes::Doc::Op>&
) 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
);