Added a simple plugin system for consensus rules using setuputils entry_points

This commit is contained in:
Matt Smith 2016-02-29 18:38:33 -08:00
parent 0ed5341c32
commit 8a9030e5c0
5 changed files with 36 additions and 3 deletions

View File

@ -39,7 +39,10 @@ config = {
'host': e('BIGCHAIN_STATSD_HOST', default='localhost'), 'host': e('BIGCHAIN_STATSD_HOST', default='localhost'),
'port': e('BIGCHAIN_STATSD_PORT', default=8125), 'port': e('BIGCHAIN_STATSD_PORT', default=8125),
'rate': e('BIGCHAIN_STATSD_SAMPLERATE', default=0.01) 'rate': e('BIGCHAIN_STATSD_SAMPLERATE', default=0.01)
} },
'consensus_plugins': [
'base'
],
} }
# We need to maintain a backup copy of the original config dict in case # We need to maintain a backup copy of the original config dict in case

View File

@ -17,6 +17,8 @@ import json
import logging import logging
import collections import collections
from pkg_resources import iter_entry_points
import bigchaindb import bigchaindb
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -100,3 +102,21 @@ def autoconfigure():
except FileNotFoundError: except FileNotFoundError:
logger.warning('Cannot find your config file. Run `bigchaindb configure` to create one') logger.warning('Cannot find your config file. Run `bigchaindb configure` to create one')
def get_plugins(plugin_names):
if not plugin_names:
plugin_names = bigchaindb.config.get('consensus_plugins', [])
plugins = []
# It's important to maintain plugin ordering as stated in the config file.
# e.g. Expensive validation tasks should happen after cheap ones.
#
# TODO: We might want to add some sort of priority system, but for now we
# simply assume everything in a given plugin is designed to run at the
# same time.
for name in plugin_names:
for entry_point in iter_entry_points('bigchaindb.plugins', name):
plugins.append(entry_point.load())
return plugins

View File

View File

@ -30,7 +30,8 @@ class Bigchain(object):
""" """
def __init__(self, host=None, port=None, dbname=None, def __init__(self, host=None, port=None, dbname=None,
public_key=None, private_key=None, keyring=[]): public_key=None, private_key=None, keyring=[],
consensus_plugins=['base']):
"""Initialize the Bigchain instance """Initialize the Bigchain instance
There are three ways in which the Bigchain instance can get its parameters. There are three ways in which the Bigchain instance can get its parameters.
@ -56,6 +57,7 @@ class Bigchain(object):
self.me = public_key or bigchaindb.config['keypair']['public'] self.me = public_key or bigchaindb.config['keypair']['public']
self.me_private = private_key or bigchaindb.config['keypair']['private'] self.me_private = private_key or bigchaindb.config['keypair']['private']
self.federation_nodes = keyring or bigchaindb.config['keyring'] self.federation_nodes = keyring or bigchaindb.config['keyring']
self.consensus_plugins = config_utils.get_plugins(consensus_plugins)
if not self.me or not self.me_private: if not self.me or not self.me_private:
raise KeypairNotFoundException() raise KeypairNotFoundException()

View File

@ -54,13 +54,21 @@ setup(
'Operating System :: POSIX :: Linux', 'Operating System :: POSIX :: Linux',
], ],
packages=['bigchaindb', 'bigchaindb.commands', 'bigchaindb.db'], packages=[
'bigchaindb',
'bigchaindb.commands',
'bigchaindb.db',
'bigchaindb.consensus'
],
entry_points={ entry_points={
'console_scripts': [ 'console_scripts': [
'bigchaindb=bigchaindb.commands.bigchain:main', 'bigchaindb=bigchaindb.commands.bigchain:main',
'bigchaindb-benchmark=bigchaindb.commands.bigchain_benchmark:main' 'bigchaindb-benchmark=bigchaindb.commands.bigchain_benchmark:main'
], ],
'bigchaindb.plugins': [
'base=bigchaindb.consensus.base:ConsensusRules'
]
}, },
install_requires=[ install_requires=[
'rethinkdb==2.2.0.post4', 'rethinkdb==2.2.0.post4',