From 4751d0b75b62cee565f07bec4f48aec3da03f06e Mon Sep 17 00:00:00 2001
From: Hugo Trentesaux <hugo@trentesaux.fr>
Date: Wed, 15 May 2024 16:58:55 +0200
Subject: [PATCH] add tx export

---
 lib/functions.py | 45 +++++++++++++++++++++++++++++++++++++++++++++
 squid-block.py   |  1 -
 squid-tx.py      | 26 ++++++++++++++++++++++++++
 3 files changed, 71 insertions(+), 1 deletion(-)
 create mode 100755 squid-tx.py

diff --git a/lib/functions.py b/lib/functions.py
index e081ab8..6fab9d9 100644
--- a/lib/functions.py
+++ b/lib/functions.py
@@ -196,3 +196,48 @@ def get_blocks(leveldb_path: str) -> list:
         blocks.append(sample)
 
     return blocks
+
+
+def get_tx(leveldb_path: str) -> list:
+    """
+    Get tx,
+    return a list of tx
+    """
+    # Get wallets balances data
+    blocks_repo = LevelDBBlocksRepository(leveldb_path)
+    txs = []
+    for num, block in blocks_repo:
+        if num % 100000 == 0:
+            print(num)
+        for tx in block.get("transactions"):
+            outputs = tx["outputs"]
+            issuers = tx["issuers"]
+            comment = tx["comment"]
+            timestamp = tx["blockstampTime"]
+            # loop on issuers
+            for issuer in issuers:
+                # loop on outputs
+                for output in outputs:
+                    outputparts = output.split(":")
+                    amount = outputparts[0]
+                    receiver = outputparts[2]
+                    # ignore non trivial unlock sources 
+                    # https://git.duniter.org/tools/py-g1-migrator/-/issues/3
+                    if "&&" in receiver or "||" in receiver:
+                        print(num)
+                        print("ignoring " + receiver)
+                        continue
+                    receiver = receiver.split("SIG(")[1].split(")")[0]
+                    sample = {
+                        "blockNumber": num,
+                        "timestamp": timestamp,
+                        "from": issuer,
+                        "to": receiver,
+                        "amount": amount,
+                        "comment": comment,
+                    }
+                    # do not include outputs that go back to sender
+                    if sample["from"] != sample["to"]:
+                        txs.append(sample)
+
+    return txs
diff --git a/squid-block.py b/squid-block.py
index 465b9e4..4710aa4 100755
--- a/squid-block.py
+++ b/squid-block.py
@@ -2,7 +2,6 @@
 
 import json
 import os
-import sys
 
 from lib.functions import get_blocks
 
diff --git a/squid-tx.py b/squid-tx.py
new file mode 100755
index 0000000..70ba4b8
--- /dev/null
+++ b/squid-tx.py
@@ -0,0 +1,26 @@
+#!/usr/bin/env python3
+
+import json
+import os
+
+from lib.functions import get_tx
+
+DEFAULT_LEVELDB_PATH = "./leveldb"
+LEVELDB_PATH = os.getenv("LEVELDB_PATH", DEFAULT_LEVELDB_PATH)
+
+
+def main():
+    # get blocks
+    tx_hist = get_tx(LEVELDB_PATH)
+
+    # Dump JSON to file
+    print("Exporting...")
+    tx_hist_json = json.dumps(tx_hist, indent=2).encode()
+    gtest_json = open("output/tx_hist.json", "wb")
+    gtest_json.write(tx_hist_json)
+
+
+if __name__ == "__main__":
+    print("Prepare tx for squid")
+    main()
+    print("Done\n")
-- 
GitLab