-
Notifications
You must be signed in to change notification settings - Fork 16
AergoLite﹢Python
Here are the instructions for using AergoLite on Python.
First you need to install AergoLite on your computer.
Then follow the instructions for your platform:
There are mainly 2 options to make Python use the modified SQLite library containing AergoLite:
LD_LIBRARY_PATH=/usr/local/lib/aergolite python app.py
For this we need patchelf:
git clone https://github.com/NixOS/patchelf
cd patchelf
./bootstrap.sh
./configure
make
sudo make install
Then execute this command:
sudo patchelf --replace-needed libsqlite3.so.0 libaergolite.so /usr/lib/python2.7/lib-dynload/_sqlite3.so
Note: The
_sqlite3.so
name will be slightly different depending on your platform.
To check if it was successful:
objdump -p /usr/lib/python2.7/lib-dynload/_sqlite3.so | grep NEEDED
The OSX does not allow to modify the pre-installed Python libraries so we need to install another copy of sqlite3 module from here
After installing it, change the SQLite library path in the wrapper:
sudo install_name_tool -change /usr/lib/libsqlite3.dylib /usr/local/lib/libaergolite.dylib /Library/Python/2.7/site-packages/pysqlite2/_sqlite3.so
Or:
sudo install_name_tool -change /usr/lib/libsqlite3.dylib /usr/local/lib/aergolite/libsqlite3.dylib /Library/Python/2.7/site-packages/pysqlite2/_sqlite3.so
To check if it worked:
otool -L /Library/Python/2.7/site-packages/pysqlite2/_sqlite3.so
Replace the sqlite3.dll
file in the folder \Python\DLLs
with the one containing AergoLite.
Then we can use the sqlite3 module normally, passing a URI in the place of the filename.
Example code:
import platform
import time
if platform.system() == "Darwin":
import pysqlite2.dbapi2 as sqlite3
else:
import sqlite3
sqlite_version = "3.27.2"
if sqlite3.sqlite_version != sqlite_version:
print("wrong SQLite version. expected: " + sqlite_version + " found: " + sqlite3.sqlite_version)
import sys
sys.exit(1)
admin_pubkey = 'Am...'
conn = sqlite3.connect('file:data.db?blockchain=on&discovery=local:4329&password=test&admin=' + admin_pubkey)
cur = conn.cursor()
cur.execute("pragma blockchain_status")
print(cur.fetchone()[0])
print("waiting approval...")
while True:
cur.execute("pragma db_is_ready")
if cur.fetchone()[0] == 1: break
time.sleep(60)
print("OK. this node is part of the blockchain network")
cur.execute("select * from sqlite_master where type='table'")
print(cur.fetchall())
conn.close()
Signing a transaction from the blockchain admin:
def on_sign_transaction(data):
print "txn to be signed: " + data
signature = sign(data, privkey)
return hex(pubkey) + ":" + hex(signature)
conn.create_function("sign_transaction", 1, on_sign_transaction)
Receiving a notification of a processed transaction:
def on_processed_transaction(nonce, status):
print "transaction " + str(nonce) + ": " + status
return None
conn.create_function("transaction_notification", 2, on_processed_transaction)
Receiving notification of local db update:
def on_db_update(arg):
print "update received"
return None
conn.create_function("update_notification", 1, on_db_update)