GeniusVentures / SuperGenius

MIT License
4 stars 3 forks source link

Implement selective subscription/update into CRDTs #49

Open Super-Genius opened 6 months ago

Super-Genius commented 6 months ago

We've forked Carlos Baquero's delta-enabled-crdts library into our thirdparty repo.

Carlos Baquero's delta-enabled CRDTs in C++ can be adapted to support selective subscriptions as mentioned. However, the original implementation might not have this feature out of the box. You'll need to extend the implementation to handle subscriptions and selective synchronization. Below is a conceptual approach on how you can extend delta-enabled CRDTs in C++ to include the selective sync feature:

Conceptual Approach to Extend Delta-enabled CRDTs in C++

  1. Define Subscription Mechanism:

    • Extend the CRDT class to include a subscription mechanism.
    • Allow nodes to subscribe to specific key/value pairs.
  2. Metadata Management:

    • Implement metadata structures to track which nodes are subscribed to which keys.
  3. Selective Update Propagation:

    • Modify the update propagation logic to send updates only to subscribed nodes.

Implementation Steps

  1. Extend CRDT Class:
    • Add methods to handle subscriptions.
    • Store a map of node IDs to subscribed keys.
  2. Handle Updates:
    • Override the `apply_delta` method to propagate updates selectively.
  3. Network Communication:
    • Ensure that only relevant updates are sent over the network based on subscriptions.

Sample Code Outline

Here’s a high-level outline of what the extended C++ code might look like:

#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <string>

class DeltaCRDT {
public:
    using Key = std::string;
    using Value = std::string;
    using NodeID = std::string;

    void subscribe(NodeID node_id, Key key) {
        subscriptions[node_id].insert(key);
    }

    void update(Key key, Value value) {
        data[key] = value;
        propagate_update(key, value);
    }

    Value get_value(Key key) {
        return data[key];
    }

private:
    std::unordered_map<Key, Value> data;
    std::unordered_map<NodeID, std::unordered_set<Key>> subscriptions;

    void propagate_update(Key key, Value value) {
        for (const auto& [node_id, keys] : subscriptions) {
            if (keys.find(key) != keys.end()) {
                send_update(node_id, key, value);
            }
        }
    }

    void send_update(NodeID node_id, Key key, Value value) {
        // Implement the logic to send the update to the specified node.
        std::cout << "Sending update to " << node_id << ": " << key << " -> " << value << std::endl;
    }
};

int main() {
    DeltaCRDT crdt;

    crdt.subscribe("node1", "genesis_proof");
    crdt.subscribe("node1", "branch_proof");
    crdt.subscribe("node1", "utxo1");
    crdt.subscribe("node1", "utxo2");

    crdt.update("genesis_proof", "proof_data");
    crdt.update("branch_proof", "branch_data");
    crdt.update("utxo1", "utxo_data_1");
    crdt.update("utxo2", "utxo_data_2");

    return 0;
}

Adapting to Carlos Baquero's Delta-enabled CRDTs

If you are using Carlos Baquero's library, the specific functions and data structures might differ. Here’s how you might adapt the general concept:

  1. Integrate Subscription Management:
    • Modify the CRDT class or create a wrapper class to handle subscriptions.
  2. Selective Delta Propagation:
    • Extend delta propagation mechanisms to check subscriptions before sending deltas.
  3. Optimize Network Traffic:
    • Ensure that network communication is optimized to handle selective updates efficiently.

Example Adaptation Pseudocode

Assuming you have a `DeltaState` class from Baquero's library, you could extend it as follows:

class DeltaState {
    // Original delta state implementation
};

class ExtendedDeltaState : public DeltaState {
public:
    using Key = std::string;
    using Value = std::string;
    using NodeID = std::string;

    void subscribe(NodeID node_id, Key key) {
        subscriptions[node_id].insert(key);
    }

    void update(Key key, Value value) {
        DeltaState::update(key, value);
        propagate_update(key, value);
    }

private:
    std::unordered_map<NodeID, std::unordered_set<Key>> subscriptions;

    void propagate_update(Key key, Value value) {
        for (const auto& [node_id, keys] : subscriptions) {
            if (keys.find(key) != keys.end()) {
                send_update(node_id, key, value);
            }
        }
    }

    void send_update(NodeID node_id, Key key, Value value) {
        // Implement the logic to send the update to the specified node.
        // This might involve network communication using Baquero's library functions.
    }
};

This approach provides a starting point for integrating selective subscriptions into a delta-enabled CRDT system in C++. You will need to adapt the specific details to fit the APIs and structures provided by Carlos Baquero's library.