mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
Merge pull request #2106 from kansi/bep/3/get-validators-api
Get validator set from the node
This commit is contained in:
commit
03c8f16475
@ -332,5 +332,19 @@ class BigchainDB(Bigchain):
|
|||||||
def fastquery(self):
|
def fastquery(self):
|
||||||
return fastquery.FastQuery(self.connection)
|
return fastquery.FastQuery(self.connection)
|
||||||
|
|
||||||
|
def get_validators(self):
|
||||||
|
try:
|
||||||
|
resp = requests.get(f'{ENDPOINT}validators')
|
||||||
|
validators = resp.json()['result']['validators']
|
||||||
|
for v in validators:
|
||||||
|
v.pop('accum')
|
||||||
|
v.pop('address')
|
||||||
|
|
||||||
|
return validators
|
||||||
|
|
||||||
|
except requests.exceptions.RequestException as e:
|
||||||
|
logger.error('Error while connecting to Tendermint HTTP API')
|
||||||
|
raise e
|
||||||
|
|
||||||
|
|
||||||
Block = namedtuple('Block', ('app_hash', 'height', 'transactions'))
|
Block = namedtuple('Block', ('app_hash', 'height', 'transactions'))
|
||||||
|
@ -8,6 +8,7 @@ from bigchaindb.web.views import (
|
|||||||
transactions as tx,
|
transactions as tx,
|
||||||
outputs,
|
outputs,
|
||||||
votes,
|
votes,
|
||||||
|
validators,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -34,6 +35,7 @@ ROUTES_API_V1 = [
|
|||||||
r('transactions', tx.TransactionListApi),
|
r('transactions', tx.TransactionListApi),
|
||||||
r('outputs/', outputs.OutputListApi),
|
r('outputs/', outputs.OutputListApi),
|
||||||
r('votes/', votes.VotesApi),
|
r('votes/', votes.VotesApi),
|
||||||
|
r('validators/', validators.ValidatorsApi),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -50,4 +50,5 @@ def get_api_v1_info(api_prefix):
|
|||||||
'outputs': '{}outputs/'.format(api_prefix),
|
'outputs': '{}outputs/'.format(api_prefix),
|
||||||
'streams': websocket_root,
|
'streams': websocket_root,
|
||||||
'metadata': '{}metadata/'.format(api_prefix),
|
'metadata': '{}metadata/'.format(api_prefix),
|
||||||
|
'validators': '{}validators'.format(api_prefix),
|
||||||
}
|
}
|
||||||
|
18
bigchaindb/web/views/validators.py
Normal file
18
bigchaindb/web/views/validators.py
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
from flask import current_app
|
||||||
|
from flask_restful import Resource
|
||||||
|
|
||||||
|
|
||||||
|
class ValidatorsApi(Resource):
|
||||||
|
def get(self):
|
||||||
|
"""API endpoint to get validators set.
|
||||||
|
|
||||||
|
Return:
|
||||||
|
A JSON string containing the validator set of the current node.
|
||||||
|
"""
|
||||||
|
|
||||||
|
pool = current_app.config['bigchain_pool']
|
||||||
|
|
||||||
|
with pool() as bigchain:
|
||||||
|
validators = bigchain.get_validators()
|
||||||
|
|
||||||
|
return validators
|
@ -18,6 +18,7 @@ def test_api_root_endpoint(client, wsserver_base_url):
|
|||||||
'streams': '{}/api/v1/streams/valid_transactions'.format(
|
'streams': '{}/api/v1/streams/valid_transactions'.format(
|
||||||
wsserver_base_url),
|
wsserver_base_url),
|
||||||
'metadata': '/api/v1/metadata/',
|
'metadata': '/api/v1/metadata/',
|
||||||
|
'validators': '/api/v1/validators',
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
'docs': 'https://docs.bigchaindb.com/projects/server/en/vtsttst/',
|
'docs': 'https://docs.bigchaindb.com/projects/server/en/vtsttst/',
|
||||||
|
49
tests/web/test_validators.py
Normal file
49
tests/web/test_validators.py
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
import pytest
|
||||||
|
|
||||||
|
from requests.exceptions import RequestException
|
||||||
|
|
||||||
|
pytestmark = pytest.mark.tendermint
|
||||||
|
|
||||||
|
VALIDATORS_ENDPOINT = '/api/v1/validators/'
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_validators_endpoint(b, client, monkeypatch):
|
||||||
|
|
||||||
|
def mock_get(uri):
|
||||||
|
return MockResponse()
|
||||||
|
monkeypatch.setattr('requests.get', mock_get)
|
||||||
|
|
||||||
|
res = client.get(VALIDATORS_ENDPOINT)
|
||||||
|
|
||||||
|
assert is_validator(res.json[0])
|
||||||
|
assert res.status_code == 200
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_validators_500_endpoint(b, client, monkeypatch):
|
||||||
|
|
||||||
|
def mock_get(uri):
|
||||||
|
raise RequestException
|
||||||
|
monkeypatch.setattr('requests.get', mock_get)
|
||||||
|
|
||||||
|
with pytest.raises(RequestException):
|
||||||
|
client.get(VALIDATORS_ENDPOINT)
|
||||||
|
|
||||||
|
|
||||||
|
# Helper
|
||||||
|
def is_validator(v):
|
||||||
|
return ('pub_key' in v) and ('voting_power' in v)
|
||||||
|
|
||||||
|
|
||||||
|
class MockResponse():
|
||||||
|
|
||||||
|
def json(self):
|
||||||
|
return {'id': '',
|
||||||
|
'jsonrpc': '2.0',
|
||||||
|
'result':
|
||||||
|
{'block_height': 5,
|
||||||
|
'validators': [
|
||||||
|
{'accum': 0,
|
||||||
|
'address': 'F5426F0980E36E03044F74DD414248D29ABCBDB2',
|
||||||
|
'pub_key': {'data': '4E2685D9016126864733225BE00F005515200727FBAB1312FC78C8B76831255A',
|
||||||
|
'type': 'ed25519'},
|
||||||
|
'voting_power': 10}]}}
|
Loading…
x
Reference in New Issue
Block a user