From 3ddcfb7ea0bf86fc0eee4556aca4e62b74b0db9e Mon Sep 17 00:00:00 2001 From: BikBikBikBikBik Date: Thu, 20 Jul 2017 05:21:31 -0600 Subject: [PATCH 1/2] Implemented APIs related to block retrieval (aside from proper error responses) --- web/api.py | 43 ++++++++++++++++++++++++++++++++++++------- web/web.py | 4 ++-- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/web/api.py b/web/api.py index 12cda52..dc2d019 100644 --- a/web/api.py +++ b/web/api.py @@ -69,6 +69,23 @@ def GetApi() : return html +def GetJsonForBlock(block) : + data = {} + + data['height'] = block[0]['height'] + data['time'] = block[0]['time'] + data['hash'] = block[0]['hash'] + data['previousblockhash'] = block[0]['previousblockhash'] + data['merkleroot'] = block[0]['merkleroot'] + data['miner'] = block[0]['nextminer'] + data['size'] = block[0]['size'] + data['version'] = block[0]['version'] + data['txnum'] = block[0]['txnum'] + + json_str = json.dumps(data) + + return json_str + def Api_V1_Address_Get_Value(address) : data = {} data['address'] = address @@ -101,13 +118,25 @@ def Api_V1_Block_Get_Current_Height() : return json_str def Api_V1_Block_Get_Current_Block() : - return '' - -def Api_V1_Block_Get_Block() : - return '' - -def Api_V1_Block_Get_Block(height,hash) : - return '' + block = web.collection_blocks.find().sort("height",-1).limit(1) + + return GetJsonForBlock(block) + +def Api_V1_Block_Get_Block_By_Height(height) : + block = web.collection_blocks.find_one({"height":height}) + + if block : + return GetJsonForBlock(block) + else + return 'Block not found' + +def Api_V1_Block_Get_Block_By_Hash(hash) : + block = web.collection_blocks.find_one({"hash":hash}) + + if block: + return GetJsonForBlock(block) + else + return 'Block not found' def Api_V1_Tx_Get_Tx(txid) : return '' \ No newline at end of file diff --git a/web/web.py b/web/web.py index a32bd69..c1fa3fe 100644 --- a/web/web.py +++ b/web/web.py @@ -462,11 +462,11 @@ def Api_V1_Block_Get_Current_Block() : @app.route('/api/v1/block/get_block/') def Api_V1_Block_Get_Block_By_Height(height) : - return api.Api_V1_Block_Get_Block(height,None), {'content-type':'application/json'} + return api.Api_V1_Block_Get_Block_By_Height(height), {'content-type':'application/json'} @app.route('/api/v1/block/get_block/') def Api_V1_Block_Get_Block_By_Hash(hash) : - return api.Api_V1_Block_Get_Block(None,hash), {'content-type':'application/json'} + return api.Api_V1_Block_Get_Block_By_Hash(hash), {'content-type':'application/json'} @app.route('/api/v1/tx/get_tx/') def Api_V1_Tx_Get_Tx(txid) : From 9d4b5ac5d3bff281f9e8e2588c79b65ad780e964 Mon Sep 17 00:00:00 2001 From: BikBikBikBikBik Date: Thu, 20 Jul 2017 05:33:31 -0600 Subject: [PATCH 2/2] Fix unnecessary array access --- web/api.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/web/api.py b/web/api.py index dc2d019..78debdd 100644 --- a/web/api.py +++ b/web/api.py @@ -72,15 +72,15 @@ def GetApi() : def GetJsonForBlock(block) : data = {} - data['height'] = block[0]['height'] - data['time'] = block[0]['time'] - data['hash'] = block[0]['hash'] - data['previousblockhash'] = block[0]['previousblockhash'] - data['merkleroot'] = block[0]['merkleroot'] - data['miner'] = block[0]['nextminer'] - data['size'] = block[0]['size'] - data['version'] = block[0]['version'] - data['txnum'] = block[0]['txnum'] + data['height'] = block['height'] + data['time'] = block['time'] + data['hash'] = block['hash'] + data['previousblockhash'] = block['previousblockhash'] + data['merkleroot'] = block['merkleroot'] + data['miner'] = block['nextminer'] + data['size'] = block['size'] + data['version'] = block['version'] + data['txnum'] = block['txnum'] json_str = json.dumps(data) @@ -120,7 +120,7 @@ def Api_V1_Block_Get_Current_Height() : def Api_V1_Block_Get_Current_Block() : block = web.collection_blocks.find().sort("height",-1).limit(1) - return GetJsonForBlock(block) + return GetJsonForBlock(block[0]) def Api_V1_Block_Get_Block_By_Height(height) : block = web.collection_blocks.find_one({"height":height})