diff --git a/web/api.py b/web/api.py index 12cda52..78debdd 100644 --- a/web/api.py +++ b/web/api.py @@ -69,6 +69,23 @@ def GetApi() : return html +def GetJsonForBlock(block) : + data = {} + + 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) + + 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[0]) + +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) :