Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from bismuthfoundation/postfork
Merge Postfork Request
- Loading branch information
Showing
101 changed files
with
2,174 additions
and
6,959 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,36 @@ | ||
import sqlite3 | ||
import re | ||
import log | ||
import functools | ||
import re | ||
|
||
def replace_regex(string,replace): | ||
replaced_string = re.sub(r'^{}'.format(replace), "", string) | ||
return replaced_string | ||
|
||
def sql_trace_callback(log, id, statement): | ||
line = f"SQL[{id}] {statement}" | ||
log.warning(line) | ||
|
||
def aliases_update(f, ledger ,mode, app_log, trace_db_calls=False): | ||
"""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: | ||
if trace_db_calls: | ||
conn.set_trace_callback(functools.partial(sql_trace_callback,app_log,"ALIASES-LEDGER")) | ||
try: | ||
c = conn.cursor() | ||
except: | ||
app_log.error('Failed to create cursor for ledger') | ||
def aliases_update(node, db_handler_instance): | ||
db_handler_instance.index_cursor.execute("SELECT block_height FROM aliases ORDER BY block_height DESC LIMIT 1;") | ||
try: | ||
alias_last_block = int(db_handler_instance.index_cursor.fetchone()[0]) | ||
except: | ||
alias_last_block = 0 | ||
|
||
with sqlite3.connect(f) as ali: | ||
if trace_db_calls: | ||
ali.set_trace_callback(functools.partial(sql_trace_callback,app_log,"ALIASES-F")) | ||
try: | ||
a = ali.cursor() | ||
except: | ||
app_log.error('Failed to create cursor for aliases') | ||
return | ||
node.logger.app_log.warning("Alias anchor block: {}".format(alias_last_block)) | ||
|
||
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 | ||
db_handler_instance.h.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 = db_handler_instance.h.fetchall() | ||
|
||
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])) | ||
for openfield in result: | ||
alias = (replace_regex(openfield[2], "alias=")) | ||
node.logger.app_log.warning(f"Processing alias registration: {alias}") | ||
try: | ||
db_handler_instance.index_cursor.execute("SELECT * from aliases WHERE alias = ?", (alias,)) | ||
dummy = db_handler_instance.index_cursor.fetchall()[0] # check for uniqueness | ||
node.logger.app_log.warning(f"Alias already registered: {alias}") | ||
except: | ||
db_handler_instance.index_cursor.execute("INSERT INTO aliases VALUES (?,?,?)", (openfield[0], openfield[1], alias)) | ||
db_handler_instance.index.commit() | ||
node.logger.app_log.warning(f"Added alias to the database: {alias} from block {openfield[0]}") | ||
|
||
|
||
if __name__ == "__main__": | ||
app_log = log.log("aliases.log", "WARNING", True) | ||
aliases_update("static/index.db","static/ledger.db","normal",app_log) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import sqlite3 | ||
import re | ||
import log | ||
import functools | ||
|
||
def aliases_update(node, db_handler_instance): | ||
|
||
db_handler_instance.index_cursor.execute("SELECT block_height FROM aliases ORDER BY block_height DESC LIMIT 1;") | ||
try: | ||
alias_last_block = int(db_handler_instance.index_cursor.fetchone()[0]) | ||
except: | ||
alias_last_block = 0 | ||
|
||
node.logger.app_log.warning("Alias anchor block: {}".format(alias_last_block)) | ||
|
||
db_handler_instance.h.execute("SELECT block_height, address, openfield FROM transactions WHERE operation = ? AND block_height >= ? AND reward = 0 ORDER BY block_height ASC, timestamp ASC;", ("alias:register", alias_last_block,)) | ||
#include the anchor block in case indexation stopped there | ||
result = db_handler_instance.h.fetchall() | ||
|
||
for openfield in result: | ||
node.logger.app_log.warning(f"Processing alias registration: {openfield[2]}") | ||
try: | ||
db_handler_instance.index_cursor.execute("SELECT * from aliases WHERE alias = ?", (openfield[2],)) | ||
dummy = db_handler_instance.index_cursor.fetchall()[0] #check for uniqueness | ||
node.logger.app_log.warning(f"Alias already registered: {openfield[2]}") | ||
except: | ||
db_handler_instance.index_cursor.execute("INSERT INTO aliases VALUES (?,?,?)", (openfield[0],openfield[1],openfield[2])) | ||
db_handler_instance.index.commit() | ||
node.logger.app_log.warning(f"Added alias to the database: {openfield[2]} from block {openfield[0]}") | ||
|
||
|
||
if __name__ == "__main__": | ||
app_log = log.log("aliases.log", "WARNING", True) |
Oops, something went wrong.