Skip to content

Commit

Permalink
Add api_getblockfromhashextra api call for json-rpc, bump version.
Browse files Browse the repository at this point in the history
  • Loading branch information
EggPool committed Jul 31, 2019
1 parent 20ccc46 commit 18ad7fc
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
49 changes: 45 additions & 4 deletions apihandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import mempool as mp
from polysign.signerfactory import SignerFactory

__version__ = "0.0.7"
__version__ = "0.0.8"


class ApiHandler:
Expand Down Expand Up @@ -214,14 +214,55 @@ def api_getblockfromhash(self, socket_handler, db_handler, peers):

block_hash = connections.receive(socket_handler)

db_handler.execute_param(db_handler.h, ("SELECT * FROM transactions "
"WHERE block_hash = ? "),
db_handler.execute_param(db_handler.h,
"SELECT * FROM transactions "
"WHERE block_hash = ?",
(block_hash,))

result = db_handler.h.fetchall()
blocks = self.blockstojson(result)
connections.send(socket_handler, blocks)

def api_getblockfromhashextra(self, socket_handler, db_handler, peers):
"""
Returns a specific block based on the provided hash.
similar to api_getblockfromhash, but sends block dict, not a dict of a dict.
Also embeds last and next block hash, as well as block difficulty
Needed for json-rpc server and btc like data.
:param socket_handler:
:param db_handler:
:param peers:
:return:
"""
try:
block_hash = connections.receive(socket_handler)

result = db_handler.fetchall(db_handler.h,
"SELECT * FROM transactions "
"WHERE block_hash = ? ",
(block_hash,))
blocks = self.blockstojson(result)
block = list(blocks.values())[0]

block["previous_block_hash"] = db_handler.fetchone(db_handler.h,
"SELECT block_hash FROM transactions WHERE block_height = ?",
(block['block_height'] - 1,))
block["next_block_hash"] = db_handler.fetchone(db_handler.h,
"SELECT block_hash FROM transactions WHERE block_height = ?",
(block['block_height'] + 1,))
block["difficulty"] = int(float(db_handler.fetchone(db_handler.h,
"SELECT difficulty FROM misc WHERE block_height = ?",
(block['block_height'],))))
# print(block)
connections.send(socket_handler, block)
except Exception as e:
self.app_log.warning("api_getblockfromhashextra {}".format(e))
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
self.app_log.warning("{} {} {}".format(exc_type, fname, exc_tb.tb_lineno))
raise

def api_getblockfromheight(self, socket_handler, db_handler, peers):
"""
Returns a specific block based on the provided hash.
Expand Down Expand Up @@ -388,7 +429,7 @@ def api_getblockswhereoflike(self, socket_handler, db_handler, peers):
self.app_log.warning(e)
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
self.app_log.warning(exc_type, fname, exc_tb.tb_lineno)
self.app_log.warning("{} {} {}".format(exc_type, fname, exc_tb.tb_lineno))
raise

def api_getblocksafterwhere(self, socket_handler, db_handler, peers):
Expand Down
19 changes: 19 additions & 0 deletions dbhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,25 @@ def execute_param(self, cursor, query, param):
self.logger.app_log.warning(f"Database retry reason: {e}")
time.sleep(1)

def fetchall(self, cursor, query, param=None):
"""Helper to simplify calling code, execute and fetch in a single line instead of 2"""
if param is None:
self.execute(cursor, query)
else:
self.execute_param(cursor, query, param)
return cursor.fetchall()

def fetchone(self, cursor, query, param=None):
"""Helper to simplify calling code, execute and fetch in a single line instead of 2"""
if param is None:
self.execute(cursor, query)
else:
self.execute_param(cursor, query, param)
res = cursor.fetchone()
if res:
return res[0]
return None

def close(self):
self.index.close()
self.hdd.close()
Expand Down
2 changes: 1 addition & 1 deletion node.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# issues with db? perhaps you missed a commit() or two


VERSION = "4.3.0.6" # Post fork candidate 5
VERSION = "4.3.0.7" # Post fork candidate 7

import functools
import glob
Expand Down

0 comments on commit 18ad7fc

Please sign in to comment.