Merge pull request #2106 from kansi/bep/3/get-validators-api

Get validator set from the node
This commit is contained in:
Troy McConaghy 2018-03-28 11:44:09 +02:00 committed by GitHub
commit 03c8f16475
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 85 additions and 0 deletions

View File

@ -332,5 +332,19 @@ class BigchainDB(Bigchain):
def fastquery(self):
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'))

View File

@ -8,6 +8,7 @@ from bigchaindb.web.views import (
transactions as tx,
outputs,
votes,
validators,
)
@ -34,6 +35,7 @@ ROUTES_API_V1 = [
r('transactions', tx.TransactionListApi),
r('outputs/', outputs.OutputListApi),
r('votes/', votes.VotesApi),
r('validators/', validators.ValidatorsApi),
]

View File

@ -50,4 +50,5 @@ def get_api_v1_info(api_prefix):
'outputs': '{}outputs/'.format(api_prefix),
'streams': websocket_root,
'metadata': '{}metadata/'.format(api_prefix),
'validators': '{}validators'.format(api_prefix),
}

View 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

View File

@ -18,6 +18,7 @@ def test_api_root_endpoint(client, wsserver_base_url):
'streams': '{}/api/v1/streams/valid_transactions'.format(
wsserver_base_url),
'metadata': '/api/v1/metadata/',
'validators': '/api/v1/validators',
}
},
'docs': 'https://docs.bigchaindb.com/projects/server/en/vtsttst/',

View 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}]}}