This repository has been archived by the owner on Jul 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
/
aliases.py
72 lines (60 loc) · 3 KB
/
aliases.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import sqlite3
import re
import log
def replace_regex(string,replace):
replaced_string = re.sub(r'^{}'.format(replace), "", string)
return replaced_string
def aliases_update(f, ledger ,mode, app_log):
"""Where f is the aliases database file"""
# index aliases
if mode not in ("normal", "reindex"):
raise ValueError ("Wrong value for aliases_update function")
# removed `conn.text_factory = str` because sqlites default `text_factory` is `str`
with sqlite3.connect(ledger) as conn:
try:
c = conn.cursor()
except:
app_log.error('Failed to create cursor for ledger')
with sqlite3.connect(f) as ali:
try:
a = ali.cursor()
except:
app_log.error('Failed to create cursor for aliases')
return
try:
a.execute("CREATE TABLE IF NOT EXISTS aliases (block_height INTEGER, address, alias)")
ali.commit()
except:
app_log.error('Failed to create aliases table')
return
if mode == 'reindex':
app_log.warning("Alias database will be reindexed")
try:
a.execute("DELETE FROM aliases")
ali.commit()
except:
app_log.error('Failed to delete content from aliases table')
return
a.execute("SELECT block_height FROM aliases ORDER BY block_height DESC LIMIT 1;")
try:
alias_last_block = int(a.fetchone()[0])
except:
alias_last_block = 0
app_log.warning("Alias anchor block: {}".format(alias_last_block))
c.execute("SELECT block_height, address, openfield FROM transactions WHERE openfield LIKE ? AND block_height >= ? ORDER BY block_height ASC, timestamp ASC;", ("alias=" + '%',)+(alias_last_block,))
#include the anchor block in case indexation stopped there
result = c.fetchall()
for openfield in result:
alias = (replace_regex(openfield[2], "alias="))
app_log.warning("Processing alias registration: {}".format(alias))
try:
a.execute("SELECT * from aliases WHERE alias = ?", (alias,))
dummy = a.fetchall()[0] #check for uniqueness
app_log.warning("Alias already registered: {}".format(alias))
except:
a.execute("INSERT INTO aliases VALUES (?,?,?)", (openfield[0],openfield[1],alias))
ali.commit()
app_log.warning("Added alias to the database: {} from block {}".format (alias,openfield[0]))
if __name__ == "__main__":
app_log = log.log("aliases.log", "WARNING", True)
aliases_update("static/index.db","static/ledger.db","normal",app_log)