mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
return list of block_id
implement status query param test
This commit is contained in:
parent
1b57ace714
commit
1cc46fb86e
@ -34,27 +34,28 @@ class BlockApi(Resource):
|
||||
|
||||
class BlockListApi(Resource):
|
||||
def get(self):
|
||||
"""API endpoint to get the related blocks and statuses of a transaction.
|
||||
"""API endpoint to get the related blocks for a transaction.
|
||||
|
||||
Return:
|
||||
A ``dict`` in the format ``{'block_id': <status>}``, where
|
||||
``<status>`` is one of "valid", "invalid", "undecided", "backlog".
|
||||
It's possible to return multiple keys of 'block_id'.
|
||||
A ``list`` of ``block_id`` that may be filtered when provided
|
||||
a status query parameter: "valid", "invalid", "undecided".
|
||||
"""
|
||||
parser = reqparse.RequestParser()
|
||||
parser.add_argument('tx_id', type=str)
|
||||
parser.add_argument('tx_id', type=str, required=True)
|
||||
parser.add_argument('status', type=str, choices=['valid', 'invalid', 'undecided'])
|
||||
|
||||
args = parser.parse_args(strict=True)
|
||||
|
||||
if sum(arg is not None for arg in args.values()) != 1:
|
||||
return make_error(400, "Provide exactly one query parameter. Choices are: block_id, tx_id")
|
||||
tx_id = args['tx_id']
|
||||
status = args['status']
|
||||
|
||||
pool = current_app.config['bigchain_pool']
|
||||
blocks = None
|
||||
|
||||
with pool() as bigchain:
|
||||
if args['tx_id']:
|
||||
blocks = bigchain.get_blocks_status_containing_tx(args['tx_id'])
|
||||
blocks = bigchain.get_blocks_status_containing_tx(tx_id)
|
||||
if blocks:
|
||||
if status:
|
||||
blocks = {k: v for k, v in blocks.items() if v == status}
|
||||
blocks = list(blocks.keys())
|
||||
|
||||
if not blocks:
|
||||
return make_error(404)
|
||||
|
@ -45,8 +45,6 @@ def test_get_blocks_by_txid_endpoint(b, client):
|
||||
# test if block is retrieved as undecided
|
||||
assert res.status_code == 200
|
||||
assert block_invalid.id in res.json
|
||||
assert res.json[block_invalid.id] == b.block_election_status(block_invalid.id, block_invalid.voters)
|
||||
assert res.json[block_invalid.id] == 'undecided'
|
||||
assert len(res.json) == 1
|
||||
|
||||
# vote the block invalid
|
||||
@ -57,8 +55,6 @@ def test_get_blocks_by_txid_endpoint(b, client):
|
||||
# test if block is retrieved as invalid
|
||||
assert res.status_code == 200
|
||||
assert block_invalid.id in res.json
|
||||
assert res.json[block_invalid.id] == b.block_election_status(block_invalid.id, block_invalid.voters)
|
||||
assert res.json[block_invalid.id] == 'invalid'
|
||||
assert len(res.json) == 1
|
||||
|
||||
# create a new block containing the same tx (and tx2 to avoid block id collision)
|
||||
@ -69,8 +65,6 @@ def test_get_blocks_by_txid_endpoint(b, client):
|
||||
# test if block is retrieved as undecided
|
||||
assert res.status_code == 200
|
||||
assert block_valid.id in res.json
|
||||
assert res.json[block_valid.id] == b.block_election_status(block_valid.id, block_valid.voters)
|
||||
assert res.json[block_valid.id] == 'undecided'
|
||||
assert len(res.json) == 2
|
||||
|
||||
# vote the block valid
|
||||
@ -81,11 +75,52 @@ def test_get_blocks_by_txid_endpoint(b, client):
|
||||
# test if block is retrieved as valid
|
||||
assert res.status_code == 200
|
||||
assert block_valid.id in res.json
|
||||
assert res.json[block_valid.id] == b.block_election_status(block_valid.id, block_valid.voters)
|
||||
assert res.json[block_valid.id] == 'valid'
|
||||
assert len(res.json) == 2
|
||||
|
||||
|
||||
@pytest.mark.bdb
|
||||
@pytest.mark.usefixtures('inputs')
|
||||
def test_get_blocks_by_txid_and_status_endpoint(b, client):
|
||||
tx = Transaction.create([b.me], [([b.me], 1)])
|
||||
tx = tx.sign([b.me_private])
|
||||
|
||||
tx2 = Transaction.create([b.me], [([b.me], 10)])
|
||||
tx2 = tx2.sign([b.me_private])
|
||||
|
||||
block_invalid = b.create_block([tx])
|
||||
b.write_block(block_invalid)
|
||||
|
||||
# vote the block invalid
|
||||
vote = b.vote(block_invalid.id, b.get_last_voted_block().id, False)
|
||||
b.write_vote(vote)
|
||||
|
||||
# create a new block containing the same tx (and tx2 to avoid block id collision)
|
||||
block_valid = b.create_block([tx, tx2])
|
||||
b.write_block(block_valid)
|
||||
|
||||
res = client.get("{}?tx_id={}&status={}".format(BLOCKS_ENDPOINT, tx.id, 'invalid'))
|
||||
# test if block is retrieved as invalid
|
||||
assert res.status_code == 200
|
||||
assert block_invalid.id in res.json
|
||||
assert len(res.json) == 1
|
||||
|
||||
res = client.get("{}?tx_id={}&status={}".format(BLOCKS_ENDPOINT, tx.id, 'undecided'))
|
||||
# test if block is retrieved as undecided
|
||||
assert res.status_code == 200
|
||||
assert block_valid.id in res.json
|
||||
assert len(res.json) == 1
|
||||
|
||||
# vote the block valid
|
||||
vote = b.vote(block_valid.id, block_invalid.id, True)
|
||||
b.write_vote(vote)
|
||||
|
||||
res = client.get("{}?tx_id={}&status={}".format(BLOCKS_ENDPOINT, tx.id, 'valid'))
|
||||
# test if block is retrieved as valid
|
||||
assert res.status_code == 200
|
||||
assert block_valid.id in res.json
|
||||
assert len(res.json) == 1
|
||||
|
||||
|
||||
@pytest.mark.bdb
|
||||
def test_get_blocks_by_txid_endpoint_returns_404_if_not_found(client):
|
||||
res = client.get(BLOCKS_ENDPOINT + "?tx_id=123")
|
||||
@ -99,6 +134,23 @@ def test_get_blocks_by_txid_endpoint_returns_400_bad_query_params(client):
|
||||
|
||||
res = client.get(BLOCKS_ENDPOINT + "?ts_id=123")
|
||||
assert res.status_code == 400
|
||||
assert res.json == {
|
||||
'message': {
|
||||
'tx_id': 'Missing required parameter in the JSON body or the post body or the query string'
|
||||
}
|
||||
}
|
||||
|
||||
res = client.get(BLOCKS_ENDPOINT + "?tx_id=123&block_id=123")
|
||||
res = client.get(BLOCKS_ENDPOINT + "?tx_id=123&foo=123")
|
||||
assert res.status_code == 400
|
||||
assert res.json == {
|
||||
'message': 'Unknown arguments: foo'
|
||||
}
|
||||
|
||||
res = client.get(BLOCKS_ENDPOINT + "?tx_id=123&status=123")
|
||||
assert res.status_code == 400
|
||||
assert res.json == {
|
||||
'message': {
|
||||
'status': '123 is not a valid choice'
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user