📜Calimero Contracts

1. ModelStore Contract

The ModelStore contract serves as a registry and payment system for machine learning models. It allows models to be securely uploaded, accessed through a proposal-based payment system, and retrieved by users upon payment approval.

Key Features & Functions

  1. Model Storage

    • Models are stored in an unordered map (UnorderedMap<String, String>) using key-value pairs, where the key represents the model identifier, and the value contains the encrypted model data.

    • The model data is expected to be AES-encrypted, with decryption keys exchanged securely (e.g., via Diffie-Hellman key exchange).

  2. Proposal-Based Payment System

    • Users can create a proposal to request access to a model by calling create_proposal(receiver_id, key, model).

    • A proposal ID (ProposalId) is generated and linked to the requested model.

    • If the model already exists, an Event::ModelUpdated is emitted; otherwise, Event::ModelUploaded is triggered.

    • A payment transaction is proposed, requiring approval before access is granted.

  3. Proposal Approval & Payment Handling

    • The contract integrates with external transaction systems to handle payments.

    • Once the payment proposal is approved (approve_proposal(proposal_id)), the contract emits an Event::PaymentApproved.

    • This ensures a decentralized and automated access control mechanism, where only users who pay for a model can retrieve it.

  4. Messaging System for Proposals

    • Users can send and retrieve messages related to a proposal using:

      • send_proposal_messages(proposal_id, message) – Adds a message to a proposal.

      • get_proposal_messages(proposal_id) – Fetches all messages associated with a proposal.

    • Messages are stored in an unordered map (UnorderedMap<ProposalId, Vector<Message>>), allowing multiple messages per proposal.

    • Each message includes:

      • id (message ID)

      • proposal_id (linked proposal)

      • author (sender’s account)

      • text (message content)

      • created_at (timestamp).

  5. Model Retrieval & Management

    • Users can fetch a model using get(key), which retrieves the encrypted model if they have paid for access.

    • All stored models can be retrieved via entries(), which returns a BTreeMap (ordered key-value pairs).

    • Additional utility functions include:

      • set_model_price(price): Updates the model price.

      • get_model_price(): Retrieves the current model price.

      • len(): Returns the number of stored models.


2. WeightedAggregator Contract

The WeightedAggregator contract is designed to perform privacy-preserving federated learning aggregation. It collects locally computed model updates (weights and biases) from multiple users and computes a global weighted average.

Key Features & Functions

  1. Maintains a Sequence of Weighted Values

    • Uses a deque (VecDeque<(f64, f64)>) to store values and their associated weights.

    • Each entry represents a contribution from a participating user in federated learning.

    • The weights reflect how much influence a user's update should have in the final aggregated model.

  2. Efficient Running Totals

    • Instead of recalculating the weighted sum every time, the contract maintains running totals:

      • total_sum: Sum of (value × weight) for all stored values.

      • total_weight: Sum of all weights.

    • This enables fast retrieval of the weighted average without iterating over all entries.

  3. Adding and Removing Values

    • add(number, weight): Adds a new number with a weight and updates the totals.

    • remove_oldest(): Removes the oldest value from the deque to prevent overflow and updates the totals accordingly.

  4. Computing the Weighted Average

    • get_weighted_average(): Computes the weighted mean using the formula: Weighted Average=∑(value×weight)∑weight\text{Weighted Average} = \frac{\sum (\text{value} \times \text{weight})}{\sum \text{weight}}Weighted Average=∑weight∑(value×weight)​

    • If total_weight == 0, returns None to prevent division by zero errors.

  5. Fetching Aggregated Data

    • get_all(): Retrieves all stored values along with their weights.

    • get_total_weight(): Returns the total sum of all weights, useful for normalization.

    • len(): Returns the number of stored entries.

    • is_empty(): Checks if any data exists in the aggregator.


How These Contracts Work Together

  1. Model Training & Storage

    • The ModelStore contract enables users to upload machine learning models securely.

    • Users can request access via the proposal-based payment system and retrieve encrypted models after approval.

  2. Federated Learning with Weighted Aggregation

    • After retrieving the model, users train it locally and compute updates (weights and biases).

    • Instead of sharing raw data, users submit model updates to the WeightedAggregator contract.

    • The aggregator computes the global model update using weighted averaging.

  3. Decentralized Federated Learning Flow

    • A model owner uploads an ML model and sets a price.

    • Users propose payments and retrieve the model after approval.

    • Users train the model locally and send updates to the aggregator.

    • The aggregator continuously updates the global model parameters using the weighted average.

    • This ensures a collaborative, privacy-preserving, decentralized training process.

Last updated