Problem: Tendermint configuration not present in BigchainDB config (#2342)

* Problem: Tendermint configuration not present in BigchainDB config

* Handle tendermint configurations properly

- Update docs

* Nitpick

* Missed some conflicts

* Make changes in tendermint/lib.py instead of deprecated core.py

- Also remove redundant info from docs about default values

* Fix docsserver automethod
This commit is contained in:
Muawia Khan 2018-06-29 18:45:22 +05:00 committed by GitHub
parent af996650c3
commit 1bad851e07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 63 additions and 34 deletions

View File

@ -59,6 +59,10 @@ config = {
'advertised_host': 'localhost', 'advertised_host': 'localhost',
'advertised_port': 9985, 'advertised_port': 9985,
}, },
'tendermint': {
'host': 'localhost',
'port': 46657,
},
# FIXME: hardcoding to localmongodb for now # FIXME: hardcoding to localmongodb for now
'database': _database_map['localmongodb'], 'database': _database_map['localmongodb'],
'log': { 'log': {

View File

@ -21,7 +21,6 @@ from bigchaindb.commands import utils
from bigchaindb.commands.utils import (configure_bigchaindb, from bigchaindb.commands.utils import (configure_bigchaindb,
input_on_stderr) input_on_stderr)
from bigchaindb.log import setup_logging from bigchaindb.log import setup_logging
from bigchaindb.tendermint.lib import BigchainDB
from bigchaindb.tendermint.utils import public_key_from_base64 from bigchaindb.tendermint.utils import public_key_from_base64
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
@ -82,6 +81,10 @@ def run_configure(args):
val = conf['database'][key] val = conf['database'][key]
conf['database'][key] = input_on_stderr('Database {}? (default `{}`): '.format(key, val), val) conf['database'][key] = input_on_stderr('Database {}? (default `{}`): '.format(key, val), val)
for key in ('host', 'port'):
val = conf['tendermint'][key]
conf['tendermint'][key] = input_on_stderr('Tendermint {}? (default `{}`)'.format(key, val), val)
if config_path != '-': if config_path != '-':
bigchaindb.config_utils.write_config(conf, config_path) bigchaindb.config_utils.write_config(conf, config_path)
else: else:
@ -94,7 +97,7 @@ def run_configure(args):
def run_upsert_validator(args): def run_upsert_validator(args):
"""Store validators which should be synced with Tendermint""" """Store validators which should be synced with Tendermint"""
b = bigchaindb.Bigchain() b = bigchaindb.tendermint.lib.BigchainDB()
public_key = public_key_from_base64(args.public_key) public_key = public_key_from_base64(args.public_key)
validator = {'pub_key': {'type': 'ed25519', validator = {'pub_key': {'type': 'ed25519',
'data': public_key}, 'data': public_key},
@ -110,7 +113,7 @@ def run_upsert_validator(args):
def _run_init(): def _run_init():
bdb = bigchaindb.Bigchain() bdb = bigchaindb.tendermint.lib.BigchainDB()
schema.init_database(connection=bdb.connection) schema.init_database(connection=bdb.connection)
@ -167,7 +170,7 @@ def run_start(args):
setup_logging() setup_logging()
logger.info('BigchainDB Version %s', bigchaindb.__version__) logger.info('BigchainDB Version %s', bigchaindb.__version__)
run_recover(BigchainDB()) run_recover(bigchaindb.tendermint.lib.BigchainDB())
try: try:
if not args.skip_initialize_database: if not args.skip_initialize_database:

View File

@ -248,10 +248,8 @@ def autoconfigure(filename=None, config=None, force=False):
# override configuration with env variables # override configuration with env variables
newconfig = env_config(newconfig) newconfig = env_config(newconfig)
if config: if config:
newconfig = update(newconfig, config) newconfig = update(newconfig, config)
set_config(newconfig) # sets bigchaindb.config set_config(newconfig) # sets bigchaindb.config

View File

@ -41,8 +41,6 @@ class Bigchain(object):
connection (:class:`~bigchaindb.backend.connection.Connection`): connection (:class:`~bigchaindb.backend.connection.Connection`):
A connection to the database. A connection to the database.
""" """
config_utils.autoconfigure()
consensusPlugin = bigchaindb.config.get('consensus_plugin') consensusPlugin = bigchaindb.config.get('consensus_plugin')
if consensusPlugin: if consensusPlugin:
@ -50,8 +48,6 @@ class Bigchain(object):
else: else:
self.consensus = BaseConsensusRules self.consensus = BaseConsensusRules
self.connection = connection if connection else backend.connect(**bigchaindb.config['database'])
def delete_transaction(self, *transaction_id): def delete_transaction(self, *transaction_id):
"""Delete a transaction from the backlog. """Delete a transaction from the backlog.

View File

@ -2,17 +2,17 @@ import asyncio
import json import json
import logging import logging
import time import time
from os import getenv
import aiohttp import aiohttp
from bigchaindb import config
from bigchaindb.common.utils import gen_timestamp from bigchaindb.common.utils import gen_timestamp
from bigchaindb.events import EventTypes, Event from bigchaindb.events import EventTypes, Event
from bigchaindb.tendermint.utils import decode_transaction_base64 from bigchaindb.tendermint.utils import decode_transaction_base64
HOST = getenv('BIGCHAINDB_TENDERMINT_HOST', 'localhost') HOST = config['tendermint']['host']
PORT = int(getenv('BIGCHAINDB_TENDERMINT_PORT', 46657)) PORT = config['tendermint']['port']
URL = 'ws://{}:{}/websocket'.format(HOST, PORT) URL = 'ws://{}:{}/websocket'.format(HOST, PORT)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)

View File

@ -5,7 +5,6 @@ MongoDB.
import logging import logging
from collections import namedtuple from collections import namedtuple
from copy import deepcopy from copy import deepcopy
from os import getenv
from uuid import uuid4 from uuid import uuid4
try: try:
@ -15,8 +14,8 @@ except ImportError:
from sha3 import sha3_256 from sha3 import sha3_256
import requests import requests
import bigchaindb
from bigchaindb import backend from bigchaindb import backend, config_utils
from bigchaindb import Bigchain from bigchaindb import Bigchain
from bigchaindb.models import Transaction from bigchaindb.models import Transaction
from bigchaindb.common.exceptions import (SchemaValidationError, from bigchaindb.common.exceptions import (SchemaValidationError,
@ -28,24 +27,26 @@ from bigchaindb import exceptions as core_exceptions
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
BIGCHAINDB_TENDERMINT_HOST = getenv('BIGCHAINDB_TENDERMINT_HOST',
'localhost')
BIGCHAINDB_TENDERMINT_PORT = getenv('BIGCHAINDB_TENDERMINT_PORT',
'46657')
ENDPOINT = 'http://{}:{}/'.format(BIGCHAINDB_TENDERMINT_HOST,
BIGCHAINDB_TENDERMINT_PORT)
MODE_LIST = ('broadcast_tx_async',
'broadcast_tx_sync',
'broadcast_tx_commit')
class BigchainDB(Bigchain): class BigchainDB(Bigchain):
def __init__(self, connection=None, **kwargs):
super().__init__(**kwargs)
config_utils.autoconfigure()
self.mode_list = ('broadcast_tx_async',
'broadcast_tx_sync',
'broadcast_tx_commit')
self.tendermint_host = bigchaindb.config['tendermint']['host']
self.tendermint_port = bigchaindb.config['tendermint']['port']
self.endpoint = 'http://{}:{}/'.format(self.tendermint_host, self.tendermint_port)
self.connection = connection if connection else backend.connect(**bigchaindb.config['database'])
def post_transaction(self, transaction, mode): def post_transaction(self, transaction, mode):
"""Submit a valid transaction to the mempool.""" """Submit a valid transaction to the mempool."""
if not mode or mode not in MODE_LIST: if not mode or mode not in self.mode_list:
raise ValidationError(('Mode must be one of the following {}.') raise ValidationError(('Mode must be one of the following {}.')
.format(', '.join(MODE_LIST))) .format(', '.join(self.mode_list)))
payload = { payload = {
'method': mode, 'method': mode,
@ -54,7 +55,7 @@ class BigchainDB(Bigchain):
'id': str(uuid4()) 'id': str(uuid4())
} }
# TODO: handle connection errors! # TODO: handle connection errors!
return requests.post(ENDPOINT, json=payload) return requests.post(self.endpoint, json=payload)
def write_transaction(self, transaction, mode): def write_transaction(self, transaction, mode):
# This method offers backward compatibility with the Web API. # This method offers backward compatibility with the Web API.
@ -69,7 +70,7 @@ class BigchainDB(Bigchain):
return (202, '') return (202, '')
# result = response['result'] # result = response['result']
# if mode == MODE_LIST[2]: # if mode == self.mode_list[2]:
# return self._process_commit_mode_response(result) # return self._process_commit_mode_response(result)
# else: # else:
# status_code = result['code'] # status_code = result['code']
@ -348,7 +349,7 @@ class BigchainDB(Bigchain):
def get_validators(self): def get_validators(self):
try: try:
resp = requests.get('{}validators'.format(ENDPOINT)) resp = requests.get('{}validators'.format(self.endpoint))
validators = resp.json()['result']['validators'] validators = resp.json()['result']['validators']
for v in validators: for v in validators:
v.pop('accum') v.pop('accum')

View File

@ -3,5 +3,3 @@ The BigchainDB Class
#################### ####################
.. autoclass:: bigchaindb.tendermint.BigchainDB .. autoclass:: bigchaindb.tendermint.BigchainDB
.. automethod:: bigchaindb.tendermint.lib.BigchainDB.__init__

View File

@ -33,6 +33,8 @@ For convenience, here's a list of all the relevant environment variables (docume
`BIGCHAINDB_LOG_DATEFMT_LOGFILE`<br> `BIGCHAINDB_LOG_DATEFMT_LOGFILE`<br>
`BIGCHAINDB_LOG_FMT_CONSOLE`<br> `BIGCHAINDB_LOG_FMT_CONSOLE`<br>
`BIGCHAINDB_LOG_FMT_LOGFILE`<br> `BIGCHAINDB_LOG_FMT_LOGFILE`<br>
`BIGCHAINDB_TENDERMINT_HOST`<br>
`BIGCHAINDB_TENDERMINT_PORT`<br>
The local config file is `$HOME/.bigchaindb` by default (a file which might not even exist), but you can tell BigchainDB to use a different file by using the `-c` command-line option, e.g. `bigchaindb -c path/to/config_file.json start` The local config file is `$HOME/.bigchaindb` by default (a file which might not even exist), but you can tell BigchainDB to use a different file by using the `-c` command-line option, e.g. `bigchaindb -c path/to/config_file.json start`
@ -426,3 +428,26 @@ logging of the `core.py` module to be more verbose, you would set the
``` ```
**Defaults to**: `{}` **Defaults to**: `{}`
## tendermint.host & tendermint.port
The settings with names of the form `tendermint.*` are for
consensus(Tendermint) backend that we are using:
* `tendermint.host` is the hostname (FQDN)/IP address of the tendermint backend.
* `tendermint.port` is self-explanatory.
**Example using environment variables**
```text
export BIGCHAINDB_TENDERMINT_HOST=tendermint
export BIGCHAINDB_TENDERMINT_PORT=46657
```
**Default values**
```js
"tendermint": {
"host": "localhost",
"port": 46657,
}
```

View File

@ -90,7 +90,7 @@ def test_bigchain_run_init_when_db_exists(mocker, capsys):
def test__run_init(mocker): def test__run_init(mocker):
from bigchaindb.commands.bigchaindb import _run_init from bigchaindb.commands.bigchaindb import _run_init
bigchain_mock = mocker.patch( bigchain_mock = mocker.patch(
'bigchaindb.commands.bigchaindb.bigchaindb.Bigchain') 'bigchaindb.commands.bigchaindb.bigchaindb.tendermint.lib.BigchainDB')
init_db_mock = mocker.patch( init_db_mock = mocker.patch(
'bigchaindb.commands.bigchaindb.schema.init_database', 'bigchaindb.commands.bigchaindb.schema.init_database',
autospec=True, autospec=True,

View File

@ -107,6 +107,10 @@ def _configure_bigchaindb(request):
config = { config = {
'database': bigchaindb._database_map[backend], 'database': bigchaindb._database_map[backend],
'tendermint': {
'host': 'localhost',
'port': 46657,
}
} }
config['database']['name'] = test_db_name config['database']['name'] = test_db_name
config = config_utils.env_config(config) config = config_utils.env_config(config)