ledger-sqlite

Simple Sqlite Blockchain Ledger with easy export of data to JSON for public mobile browser read access.


Keywords
Utility, Sqlite, Ledger, Blockchain, JSON
License
X11
Install
npm install ledger-sqlite@1.0.4

Documentation

Ledger-SQLite

Author

Johnathan Edward Brown Date: October 19, 2024

Overview

Ledger-SQLite is a simple SQLite-based blockchain ledger designed to facilitate easy export of data to JSON for public mobile browser read access. This project aims to provide a robust and efficient solution for maintaining a blockchain ledger with the ability to handle large datasets and ensure data integrity.

Why This Design is Superior

The Ledger-SQLite project represents a significant improvement over the previous JSON-based ledger design. The hybrid approach of combining SQLite with JSON offers several advantages:

  1. Efficient Data Management: SQLite provides a robust database management system that can handle large datasets efficiently, avoiding the memory issues and excessive ledger sizes encountered with the previous design.

  2. Data Integrity: By leveraging SQLite's ACID (Atomicity, Consistency, Isolation, Durability) properties, the new design ensures data integrity and reliability, even in the event of power outages or system crashes.

  3. Scalability: The modular structure and asynchronous task handling allow the system to scale gracefully, managing multiple files and large volumes of data without performance degradation.

  4. Graceful Shutdown: Signal handling for graceful shutdown ensures that the ledger can safely close and save its state, preventing data loss and corruption.

  5. Hybrid Design: The combination of SQLite for storage and JSON for data export provides the best of both worlds—efficient storage and easy data interchange.

Overall, this design addresses the shortcomings of the previous JSON-based ledger, providing a more reliable, scalable, and efficient solution for maintaining a blockchain ledger.

Features

  • Hybrid SQLite and JSON ledger design
  • Efficient data export to JSON
  • Asynchronous task handling
  • Signal handling for graceful shutdown
  • Modular code structure

Installation

To get started with Ledger-SQLite, you can either clone the repository and install the dependencies or install it directly from the npm registry.

Cloning the Repository

git clone https://github.com/john1234brown/ledger-sqlite.git
cd ledger-sqlite
npm install

Installing from npm

npm install ledger-sqlite

Building the Project

To build the project, run the following command:

npm run compile

This command will compile the TypeScript code and bundle the JavaScript files using Rollup.

Usage

CommonJS Usage

Exporting Data to JSON

The ledger allows you to export data from the SQLite database to JSON files. The exportTableToJson function in the ledger-sqlite module handles this process.

Example usage:

const { exportTableToJson } = require('ledger-sqlite');
const tableName = "blockchain";
const jsonFilePath = "blockchain.json";
exportTableToJson(tableName, jsonFilePath).catch(console.error);

Adding a Block to the Ledger

The ledger allows you to add new blocks to the blockchain and manage the size of the blockchain tables. First, you need to construct the Ledger class object from the ledger-sqlite module, and then you can use the addBlock function.

Example usage:

const { Ledger } = require('ledger-sqlite');
const dbLocation = "path/to/database";
const ledger = new Ledger(dbLocation);

const maxBlocks = 7; // A Week
const newBlock = { data: "new block data" };
const maxOldBlockSize = 30; // A Month Roughly
const maxReallyOldBlockchainSize = ((364*4)+365)-37; // The remaining 5 years worth!
const tableName = "blockchain";
const oldTableName = "old_blockchain";
const reallyOldTableName = "really_old_blockchain";

ledger.addBlock(tableName, maxBlocks, newBlock, oldTableName, reallyOldTableName, maxOldBlockSize, maxReallyOldBlockchainSize)
    .then(() => console.log("Block added successfully"))
    .catch(console.error);

Adding a Block to the ThreeWayLedger

The ThreeWayLedger class extends the functionality of the Ledger class by managing three separate tables for current, old, and really old blocks. You can add new blocks to the ledger and manage the size of each table.

Example usage:

const { ThreeWayLedger } = require('ledger-sqlite');

const dbLocation = "path/to/current/database";
const oldDbLocation = "path/to/old/database";
const reallyOldDbLocation = "path/to/really/old/database";

const threeWayLedger = new ThreeWayLedger(dbLocation, oldDbLocation, reallyOldDbLocation);

const maxBlocks = 7; // A Week
const newBlock = { data: "new block data" };
const maxOldBlockSize = 30; // A Month Roughly
const maxReallyOldBlockchainSize = ((364*4)+365)-37; // The remaining 5 years worth!
const tableName = "blockchain";
const oldTableName = "old_blockchain";
const reallyOldTableName = "really_old_blockchain";

threeWayLedger.addBlock(maxBlocks, newBlock, maxOldBlockSize, maxReallyOldBlockchainSize, tableName, oldTableName, reallyOldTableName)
    .then(() => console.log("Block added successfully to ThreeWayLedger"))
    .catch(console.error);

Module Usage

Exporting Data to JSON

The ledger allows you to export data from the SQLite database to JSON files. The exportTableToJson function in the ledger-sqlite module handles this process.

Example usage:

import { exportTableToJson } from 'ledger-sqlite';
const tableName = "blockchain";
const jsonFilePath = "blockchain.json";
await exportTableToJson(tableName, jsonFilePath);

Adding a Block to the Ledger

The ledger allows you to add new blocks to the blockchain and manage the size of the blockchain tables. First, you need to construct the Ledger class object from the ledger-sqlite module, and then you can use the addBlock function.

Example usage:

import { Ledger } from 'ledger-sqlite';

const dbLocation = "path/to/database";
const ledger = new Ledger(dbLocation);

const maxBlocks = 7; // A Week
const newBlock = { data: "new block data" };
const maxOldBlockSize = 30; // A Month Roughly
const maxReallyOldBlockchainSize = ((364*4)+365)-37;//The remaining 5 years worth!
const tableName = "blockchain";
const oldTableName = "old_blockchain";
const reallyOldTableName = "really_old_blockchain";

ledger.addBlock(tableName, maxBlocks, newBlock, oldTableName, reallyOldTableName, maxOldBlockSize, maxReallyOldBlockchainSize)
    .then(() => console.log("Block added successfully"))
    .catch(console.error);

Adding a Block to the ThreeWayLedger

The ThreeWayLedger class extends the functionality of the Ledger class by managing three separate tables for current, old, and really old blocks. You can add new blocks to the ledger and manage the size of each table.

Example usage:

import { ThreeWayLedger } from 'ledger-sqlite';

const dbLocation = "path/to/current/database";
const oldDbLocation = "path/to/old/database";
const reallyOldDbLocation = "path/to/really/old/database";

const threeWayLedger = new ThreeWayLedger(dbLocation, oldDbLocation, reallyOldDbLocation);

const maxBlocks = 7;// A Week
const newBlock = { data: "new block data" };
const maxOldBlockSize = 30;// A Month Roughly
const maxReallyOldBlockchainSize = ((364*4)+365)-37; // The remaining 5 years worth!
const tableName = "blockchain";
const oldTableName = "old_blockchain";
const reallyOldTableName = "really_old_blockchain";

threeWayLedger.addBlock(maxBlocks, newBlock, maxOldBlockSize, maxReallyOldBlockchainSize, tableName, oldTableName, reallyOldTableName)
    .then(() => console.log("Block added successfully to ThreeWayLedger"))
    .catch(console.error);

Real World Examples

Example 1: Supply Chain Management

In a supply chain management system, each transaction or movement of goods can be recorded as a block in the blockchain ledger. This ensures transparency and traceability of goods from the manufacturer to the end consumer. The data can be exported to JSON for audit purposes and real-time tracking.

Example 2: Tax Records

For businesses, maintaining a 5-year ledger system for IRS tax records is crucial. Ledger-SQLite provides a reliable solution to store and export these records efficiently.

Example 3: Voting System

In a voting system, each vote can be recorded as a block in the blockchain ledger. This ensures the integrity and transparency of the voting process. The data can be exported to JSON for public verification and audit purposes.

Example 4: Medical Records

In healthcare, maintaining accurate and secure medical records is essential. Ledger-SQLite can be used to store patient records in a blockchain ledger, ensuring data integrity and easy export to JSON for sharing with authorized medical professionals.

Example 5: Intellectual Property Management

For creators and inventors, managing intellectual property rights is crucial. Ledger-SQLite can be used to record the creation and transfer of intellectual property rights in a blockchain ledger, ensuring transparency and protection of ownership. The data can be exported to JSON for legal verification and record-keeping.

Backstory

How it started

Looking for possible solutions to my previous NodeJS JSON custom ledger design which had potential memory issues and yes capped memory size but held onto too much data which wasn't necessary like excessive ledger sizes and such! Although this old design was good, let me get into its flaws first so no one attempts to use it! LOL, I'll keep it for archive records for a reason on GitHub to reference to on bad practice and design. Let me go into why!

Which is good for your other solutions in development but not for what I'm trying to go for which was basically a 5-year ledger system for public records for IRS tax records for businesses so I set on a journey on how to restructure it!

And finally here today I've come to the conclusion and decided utilizing a Hybrid SQLite Solution with my JSON Ledger design will lead to greater success!

Why The Old Design was a bad Design!

First, this was bad mainly because when I used it for a testnet project for a raffle system we had issues with the ledger size and the memory usage which caused the system to crash and BURN!!! Not really on fire more figuratively speaking but the HDD was indeed full and the system crashed!

Second, I didn't account for multiple files such as JSON being attached in such an asynchronous manner which when a power outage occurred led to entire ledger loss! This would not be good for a real business approach!

Third, this is why you always test for all possible failures as Murphy's Law states anything that can go wrong will and you best prepare for it!

License

This project is licensed under the X11 License. See the LICENSE file for details.

Acknowledgements

Generated by GitHub Copilot.