public inbox for [email protected]
 help / color / mirror / Atom feed
From: Alviro Iskandar Setiawan <[email protected]>
To: Ammar Faizi <[email protected]>
Cc: GNU/Weeb Mailing List <[email protected]>,
	Alviro Iskandar Setiawan <[email protected]>
Subject: [RFC PATCH v1 1/1] db: Create starting DDL for saving transaction
Date: Tue, 28 Jun 2022 06:21:39 +0000	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>

In order to keep the transaction record consistent, we can use a
chaining hash system that generates the transaction ID by using its
parent ID and the current transaction details.

For example, to create a new transaction, we have several things to
note, like:

  1. date
  2. description
  3. commiter
  4. amount

  [ other metadata stuff may be included ]

We can then generate the transaction id by using a hash function,
let's say SHA1:

 trx_info = date . description . commiter . amount . parent_trx_id
 trx_id   = SHA1(info)
 SAVE(trx_id, date, description, commiter, amount, parent_trx_id)

Note that since the current trx ID uses its parent trx ID to generate
itself, it makes the situation impossible to rewrite the history
without breaking the whole chain. That's the point of the consistency
in this design. We can verify that the data is consistent by just
re-hashing the transaction info together with its parent trx id.

Also note that, for the first transaction, it doesn't have a parent
trx id. So only the first transaction doesn't use a parent trx id to
generate itself. Let's call it the genesis transaction.

Signed-off-by: Alviro Iskandar Setiawan <[email protected]>
---
 database.sql | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
 create mode 100644 database.sql

diff --git a/database.sql b/database.sql
new file mode 100644
index 0000000..d191a45
--- /dev/null
+++ b/database.sql
@@ -0,0 +1,25 @@
+SET NAMES utf8;
+SET time_zone = '+00:00';
+SET foreign_key_checks = 0;
+SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';
+
+SET NAMES utf8mb4;
+
+DROP TABLE IF EXISTS `transactions`;
+CREATE TABLE `transactions` (
+  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
+  `trx_id` binary(20) NOT NULL,
+  `parent_trx_id` binary(20) NOT NULL,
+  `committer` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NOT NULL,
+  `amount` bigint unsigned NOT NULL,
+  `notes` text COLLATE utf8mb4_unicode_520_ci NOT NULL,
+  `created_at` datetime NOT NULL,
+  PRIMARY KEY (`id`),
+  KEY `committer` (`committer`),
+  KEY `amount` (`amount`),
+  KEY `created_at` (`created_at`),
+  KEY `trx_id` (`trx_id`),
+  KEY `parent_trx_id` (`parent_trx_id`),
+  FULLTEXT KEY `notes` (`notes`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
+
-- 
2.34.1


  reply	other threads:[~2022-06-28  6:21 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-28  6:21 [RFC PATCH v1 0/1] GNU/Weeb Financial Transaction Record Design Alviro Iskandar Setiawan
2022-06-28  6:21 ` Alviro Iskandar Setiawan [this message]
2022-06-28  7:27   ` [RFC PATCH v1 1/1] db: Create starting DDL for saving transaction Ammar Faizi
2022-06-28  7:31     ` Alviro Iskandar Setiawan
2022-06-28  7:47       ` Ammar Faizi
2022-06-28  7:49         ` Ammar Faizi
2022-06-28  8:03         ` Alviro Iskandar Setiawan
2022-06-28  8:07           ` Ammar Faizi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220628062139.262361-2-alviro.iskandar@gnuweeb.org \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox