Modules
Sync

Sync Module Documentation

Overview

The Sync module is designed to ensure that the distributed ledger of transactions remains consistent across all nodes within the Synnq network. It provides mechanisms for storing, retrieving, and synchronizing transaction data between nodes. The module also handles the detection of changes in transaction data, ensuring that the latest and most accurate information is always available.


Components and Responsibilities

1. Transaction Storage and Retrieval

The Sync module provides functionality to store and retrieve transaction data from the local storage system. This ensures that all transactions are securely saved and can be accessed whenever needed.

  • Store Transaction: Saves a given transaction into local storage.
  • Get Transaction: Retrieves a specific transaction from storage by its index.
  • Load Transactions: Loads all transactions from local storage.
  • Get Transactions: Retrieves all transactions from local storage.

2. Transaction Integrity Checking

Before writing a transaction to the database, the Sync module checks whether the transaction is already present and unchanged. This helps prevent redundant writes and ensures that only new or modified transactions are stored.

  • Is Transaction Present and Unchanged: Checks if a transaction already exists in the database and whether it has remained unchanged.

3. Database Write Operations

When a new transaction is detected or a modification is made to an existing transaction, the Sync module writes the updated transaction data to the database. This operation includes serialization and storage of the transaction in a manner that ensures data integrity.

  • Write Transaction to Database: Writes a transaction to the database, ensuring that it is only stored if it is new or has been modified.

4. Synchronization with Primary Node

The Sync module synchronizes transaction data between nodes by fetching transactions from a designated primary node. This process ensures that all nodes within the network are up to date with the latest transaction history.

  • Synchronize Data: Fetches transactions from the primary node and stores them locally, ensuring all transactions are consistent across the network.
  • Get All Primary Node Transactions: Fetches all transaction indices from the primary node, which helps in verifying the completeness of local transaction data.

5. Handling Transaction Indexing

The module also provides functionality to retrieve the latest transaction index from local storage, which is essential for ensuring that synchronization operations begin from the correct starting point.

  • Get Latest Transaction Index: Retrieves the highest transaction index stored locally, helping to identify where the next synchronization process should begin.

Use Cases and Workflows

Storing and Retrieving Transactions

  1. Storing a Transaction:

    • When a transaction is processed, it is serialized and stored in local storage using a unique key derived from its index.
    • The module logs the storage action for debugging and verification purposes.
  2. Retrieving a Transaction:

    • A specific transaction can be retrieved from storage using its index.
    • The module deserializes the transaction data and logs the retrieval action.

Synchronizing Transactions

  1. Starting Synchronization:

    • Synchronization begins by determining the latest transaction index stored locally.
    • The system then sends a request to the primary node to fetch transactions starting from this index.
  2. Handling Synchronization Responses:

    • If new transactions are received, they are checked for changes before being written to the local database.
    • The process repeats until all new transactions are synchronized or the specified maximum number of transactions is reached.
  3. Error Handling During Synchronization:

    • The system handles various HTTP response statuses, including success, bad requests, and server errors.
    • It ensures that synchronization halts gracefully if no new transactions are available or an error occurs.

Checking Transaction Integrity

  1. Transaction Integrity Check:
    • Before writing a transaction, the module checks whether the transaction already exists in storage and whether its data is unchanged.
    • If the transaction is unchanged, the system skips the write operation, preventing redundant database entries.

Error Handling and Logging

The Sync module includes extensive error handling and logging to ensure robustness and transparency. It handles potential issues such as network failures, serialization errors, and storage access problems. The module logs each significant action, allowing for easy troubleshooting and monitoring of the synchronization process.