mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
Merge branch 'master' into update_cc_b58_crypto
This commit is contained in:
commit
b27f6f4b30
@ -33,4 +33,4 @@ RUN mkdir -p /usr/src/app
|
|||||||
COPY . /usr/src/app/
|
COPY . /usr/src/app/
|
||||||
WORKDIR /usr/src/app
|
WORKDIR /usr/src/app
|
||||||
RUN pip install --no-cache-dir --process-dependency-links -e .[dev]
|
RUN pip install --no-cache-dir --process-dependency-links -e .[dev]
|
||||||
RUN bigchaindb -y configure
|
RUN bigchaindb -y configure
|
||||||
@ -5,5 +5,5 @@ RUN pip install --upgrade \
|
|||||||
pycco \
|
pycco \
|
||||||
websocket-client~=0.47.0 \
|
websocket-client~=0.47.0 \
|
||||||
pytest~=3.0 \
|
pytest~=3.0 \
|
||||||
bigchaindb-driver==0.5.1 \
|
bigchaindb-driver==0.5.2 \
|
||||||
blns
|
blns
|
||||||
|
|||||||
@ -2,7 +2,6 @@
|
|||||||
# SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
|
# SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
|
||||||
# Code is Apache-2.0 and docs are CC-BY-4.0
|
# Code is Apache-2.0 and docs are CC-BY-4.0
|
||||||
|
|
||||||
import time
|
|
||||||
import logging
|
import logging
|
||||||
from ssl import CERT_REQUIRED
|
from ssl import CERT_REQUIRED
|
||||||
|
|
||||||
@ -88,23 +87,6 @@ class LocalMongoDBConnection(Connection):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if self.replicaset:
|
|
||||||
# we should only return a connection if the replica set is
|
|
||||||
# initialized. initialize_replica_set will check if the
|
|
||||||
# replica set is initialized else it will initialize it.
|
|
||||||
initialize_replica_set(self.host,
|
|
||||||
self.port,
|
|
||||||
self.connection_timeout,
|
|
||||||
self.dbname,
|
|
||||||
self.ssl,
|
|
||||||
self.login,
|
|
||||||
self.password,
|
|
||||||
self.ca_cert,
|
|
||||||
self.certfile,
|
|
||||||
self.keyfile,
|
|
||||||
self.keyfile_passphrase,
|
|
||||||
self.crlfile)
|
|
||||||
|
|
||||||
# FYI: the connection process might raise a
|
# FYI: the connection process might raise a
|
||||||
# `ServerSelectionTimeoutError`, that is a subclass of
|
# `ServerSelectionTimeoutError`, that is a subclass of
|
||||||
# `ConnectionFailure`.
|
# `ConnectionFailure`.
|
||||||
@ -140,8 +122,6 @@ class LocalMongoDBConnection(Connection):
|
|||||||
|
|
||||||
return client
|
return client
|
||||||
|
|
||||||
# `initialize_replica_set` might raise `ConnectionFailure`,
|
|
||||||
# `OperationFailure` or `ConfigurationError`.
|
|
||||||
except (pymongo.errors.ConnectionFailure,
|
except (pymongo.errors.ConnectionFailure,
|
||||||
pymongo.errors.OperationFailure) as exc:
|
pymongo.errors.OperationFailure) as exc:
|
||||||
logger.info('Exception in _connect(): {}'.format(exc))
|
logger.info('Exception in _connect(): {}'.format(exc))
|
||||||
@ -153,120 +133,3 @@ class LocalMongoDBConnection(Connection):
|
|||||||
MONGO_OPTS = {
|
MONGO_OPTS = {
|
||||||
'socketTimeoutMS': 20000,
|
'socketTimeoutMS': 20000,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def initialize_replica_set(host, port, connection_timeout, dbname, ssl, login,
|
|
||||||
password, ca_cert, certfile, keyfile,
|
|
||||||
keyfile_passphrase, crlfile):
|
|
||||||
"""Initialize a replica set. If already initialized skip."""
|
|
||||||
|
|
||||||
# Setup a MongoDB connection
|
|
||||||
# The reason we do this instead of `backend.connect` is that
|
|
||||||
# `backend.connect` will connect you to a replica set but this fails if
|
|
||||||
# you try to connect to a replica set that is not yet initialized
|
|
||||||
try:
|
|
||||||
# The presence of ca_cert, certfile, keyfile, crlfile implies the
|
|
||||||
# use of certificates for TLS connectivity.
|
|
||||||
if ca_cert is None or certfile is None or keyfile is None or \
|
|
||||||
crlfile is None:
|
|
||||||
conn = pymongo.MongoClient(host,
|
|
||||||
port,
|
|
||||||
serverselectiontimeoutms=connection_timeout,
|
|
||||||
ssl=ssl,
|
|
||||||
**MONGO_OPTS)
|
|
||||||
if login is not None and password is not None:
|
|
||||||
conn[dbname].authenticate(login, password)
|
|
||||||
else:
|
|
||||||
logger.info('Connecting to MongoDB over TLS/SSL...')
|
|
||||||
conn = pymongo.MongoClient(host,
|
|
||||||
port,
|
|
||||||
serverselectiontimeoutms=connection_timeout,
|
|
||||||
ssl=ssl,
|
|
||||||
ssl_ca_certs=ca_cert,
|
|
||||||
ssl_certfile=certfile,
|
|
||||||
ssl_keyfile=keyfile,
|
|
||||||
ssl_pem_passphrase=keyfile_passphrase,
|
|
||||||
ssl_crlfile=crlfile,
|
|
||||||
ssl_cert_reqs=CERT_REQUIRED,
|
|
||||||
**MONGO_OPTS)
|
|
||||||
if login is not None:
|
|
||||||
logger.info('Authenticating to the database...')
|
|
||||||
conn[dbname].authenticate(login, mechanism='MONGODB-X509')
|
|
||||||
|
|
||||||
except (pymongo.errors.ConnectionFailure,
|
|
||||||
pymongo.errors.OperationFailure) as exc:
|
|
||||||
logger.info('Exception in _connect(): {}'.format(exc))
|
|
||||||
raise ConnectionError(str(exc)) from exc
|
|
||||||
except pymongo.errors.ConfigurationError as exc:
|
|
||||||
raise ConfigurationError from exc
|
|
||||||
|
|
||||||
_check_replica_set(conn)
|
|
||||||
host = '{}:{}'.format(bigchaindb.config['database']['host'],
|
|
||||||
bigchaindb.config['database']['port'])
|
|
||||||
config = {'_id': bigchaindb.config['database']['replicaset'],
|
|
||||||
'members': [{'_id': 0, 'host': host}]}
|
|
||||||
|
|
||||||
try:
|
|
||||||
conn.admin.command('replSetInitiate', config)
|
|
||||||
except pymongo.errors.OperationFailure as exc_info:
|
|
||||||
if exc_info.details['codeName'] == 'AlreadyInitialized':
|
|
||||||
return
|
|
||||||
raise
|
|
||||||
else:
|
|
||||||
_wait_for_replica_set_initialization(conn)
|
|
||||||
logger.info('Initialized replica set')
|
|
||||||
finally:
|
|
||||||
if conn is not None:
|
|
||||||
logger.info('Closing initial connection to MongoDB')
|
|
||||||
conn.close()
|
|
||||||
|
|
||||||
|
|
||||||
def _check_replica_set(conn):
|
|
||||||
"""Checks if the replSet option was enabled either through the command
|
|
||||||
line option or config file and if it matches the one provided by
|
|
||||||
bigchaindb configuration.
|
|
||||||
|
|
||||||
Note:
|
|
||||||
The setting we are looking for will have a different name depending
|
|
||||||
if it was set by the config file (`replSetName`) or by command
|
|
||||||
line arguments (`replSet`).
|
|
||||||
|
|
||||||
Raise:
|
|
||||||
:exc:`~ConfigurationError`: If mongod was not started with the
|
|
||||||
replSet option.
|
|
||||||
"""
|
|
||||||
options = conn.admin.command('getCmdLineOpts')
|
|
||||||
try:
|
|
||||||
repl_opts = options['parsed']['replication']
|
|
||||||
repl_set_name = repl_opts.get('replSetName', repl_opts.get('replSet'))
|
|
||||||
except KeyError:
|
|
||||||
raise ConfigurationError('mongod was not started with'
|
|
||||||
' the replSet option.')
|
|
||||||
|
|
||||||
bdb_repl_set_name = bigchaindb.config['database']['replicaset']
|
|
||||||
if repl_set_name != bdb_repl_set_name:
|
|
||||||
raise ConfigurationError('The replicaset configuration of '
|
|
||||||
'bigchaindb (`{}`) needs to match '
|
|
||||||
'the replica set name from MongoDB'
|
|
||||||
' (`{}`)'.format(bdb_repl_set_name,
|
|
||||||
repl_set_name))
|
|
||||||
|
|
||||||
|
|
||||||
def _wait_for_replica_set_initialization(conn):
|
|
||||||
"""Wait for a replica set to finish initialization.
|
|
||||||
|
|
||||||
If a replica set is being initialized for the first time it takes some
|
|
||||||
time. Nodes need to discover each other and an election needs to take
|
|
||||||
place. During this time the database is not writable so we need to wait
|
|
||||||
before continuing with the rest of the initialization
|
|
||||||
"""
|
|
||||||
|
|
||||||
# I did not find a better way to do this for now.
|
|
||||||
# To check if the database is ready we will poll the mongodb logs until
|
|
||||||
# we find the line that says the database is ready
|
|
||||||
logger.info('Waiting for mongodb replica set initialization')
|
|
||||||
while True:
|
|
||||||
logs = conn.admin.command('getLog', 'rs')['log']
|
|
||||||
if any('database writes are now permitted' in line for line in logs):
|
|
||||||
return
|
|
||||||
time.sleep(0.1)
|
|
||||||
|
|||||||
@ -299,6 +299,18 @@ def get_validator_set(conn, height=None):
|
|||||||
return list(cursor)[0]
|
return list(cursor)[0]
|
||||||
|
|
||||||
|
|
||||||
|
@register_query(LocalMongoDBConnection)
|
||||||
|
def get_validator_set_by_election_id(conn, election_id):
|
||||||
|
query = {'election_id': election_id}
|
||||||
|
|
||||||
|
cursor = conn.run(
|
||||||
|
conn.collection('validators')
|
||||||
|
.find(query, projection={'_id': False})
|
||||||
|
)
|
||||||
|
|
||||||
|
return next(cursor, None)
|
||||||
|
|
||||||
|
|
||||||
@register_query(LocalMongoDBConnection)
|
@register_query(LocalMongoDBConnection)
|
||||||
def get_asset_tokens_for_public_key(conn, asset_id, public_key):
|
def get_asset_tokens_for_public_key(conn, asset_id, public_key):
|
||||||
query = {'outputs.public_keys': [public_key],
|
query = {'outputs.public_keys': [public_key],
|
||||||
|
|||||||
@ -133,3 +133,6 @@ def create_validators_secondary_index(conn, dbname):
|
|||||||
conn.conn[dbname]['validators'].create_index('height',
|
conn.conn[dbname]['validators'].create_index('height',
|
||||||
name='height',
|
name='height',
|
||||||
unique=True,)
|
unique=True,)
|
||||||
|
conn.conn[dbname]['validators'].create_index('election_id',
|
||||||
|
name='election_id',
|
||||||
|
unique=True,)
|
||||||
|
|||||||
@ -360,6 +360,14 @@ def get_validator_set(conn, height):
|
|||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
|
@singledispatch
|
||||||
|
def get_validator_set_by_election_id(conn, election_id):
|
||||||
|
"""Return a validator set change with the specified election_id
|
||||||
|
"""
|
||||||
|
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
@singledispatch
|
@singledispatch
|
||||||
def get_asset_tokens_for_public_key(connection, asset_id,
|
def get_asset_tokens_for_public_key(connection, asset_id,
|
||||||
public_key, operation):
|
public_key, operation):
|
||||||
|
|||||||
@ -27,8 +27,7 @@ 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_utils import public_key_from_base64
|
from bigchaindb.tendermint_utils import public_key_from_base64, public_key_to_base64
|
||||||
|
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@ -191,6 +190,35 @@ def run_upsert_validator_approve(args, bigchain):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def run_upsert_validator_show(args, bigchain):
|
||||||
|
"""Retrieves information about an upsert-validator election
|
||||||
|
|
||||||
|
:param args: dict
|
||||||
|
args = {
|
||||||
|
'election_id': the transaction_id for an election (str)
|
||||||
|
}
|
||||||
|
:param bigchain: an instance of BigchainDB
|
||||||
|
"""
|
||||||
|
|
||||||
|
election = bigchain.get_transaction(args.election_id)
|
||||||
|
if not election:
|
||||||
|
logger.error(f'No election found with election_id {args.election_id}')
|
||||||
|
return
|
||||||
|
|
||||||
|
new_validator = election.asset['data']
|
||||||
|
|
||||||
|
public_key = public_key_to_base64(new_validator['public_key'])
|
||||||
|
power = new_validator['power']
|
||||||
|
node_id = new_validator['node_id']
|
||||||
|
status = election.get_status(bigchain)
|
||||||
|
|
||||||
|
response = f'public_key={public_key}\npower={power}\nnode_id={node_id}\nstatus={status}'
|
||||||
|
|
||||||
|
logger.info(response)
|
||||||
|
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
def _run_init():
|
def _run_init():
|
||||||
bdb = bigchaindb.BigchainDB()
|
bdb = bigchaindb.BigchainDB()
|
||||||
|
|
||||||
@ -320,6 +348,12 @@ def create_parser():
|
|||||||
dest='sk',
|
dest='sk',
|
||||||
help='Path to the private key of the election initiator.')
|
help='Path to the private key of the election initiator.')
|
||||||
|
|
||||||
|
show_election_parser = validator_subparser.add_parser('show',
|
||||||
|
help='Provides information about an election.')
|
||||||
|
|
||||||
|
show_election_parser.add_argument('election_id',
|
||||||
|
help='The transaction id of the election you wish to query.')
|
||||||
|
|
||||||
# parsers for showing/exporting config values
|
# parsers for showing/exporting config values
|
||||||
subparsers.add_parser('show-config',
|
subparsers.add_parser('show-config',
|
||||||
help='Show the current configuration')
|
help='Show the current configuration')
|
||||||
|
|||||||
@ -54,7 +54,7 @@ class App(BaseApplication):
|
|||||||
validator_set = [vutils.decode_validator(v) for v in genesis.validators]
|
validator_set = [vutils.decode_validator(v) for v in genesis.validators]
|
||||||
block = Block(app_hash='', height=0, transactions=[])
|
block = Block(app_hash='', height=0, transactions=[])
|
||||||
self.bigchaindb.store_block(block._asdict())
|
self.bigchaindb.store_block(block._asdict())
|
||||||
self.bigchaindb.store_validator_set(1, validator_set)
|
self.bigchaindb.store_validator_set(1, validator_set, None)
|
||||||
return ResponseInitChain()
|
return ResponseInitChain()
|
||||||
|
|
||||||
def info(self, request):
|
def info(self, request):
|
||||||
|
|||||||
@ -421,24 +421,32 @@ class BigchainDB(object):
|
|||||||
def fastquery(self):
|
def fastquery(self):
|
||||||
return fastquery.FastQuery(self.connection)
|
return fastquery.FastQuery(self.connection)
|
||||||
|
|
||||||
|
def get_validator_change(self, height=None):
|
||||||
|
return backend.query.get_validator_set(self.connection, height)
|
||||||
|
|
||||||
def get_validators(self, height=None):
|
def get_validators(self, height=None):
|
||||||
result = backend.query.get_validator_set(self.connection, height)
|
result = self.get_validator_change(height)
|
||||||
validators = result['validators']
|
validators = result['validators']
|
||||||
return validators
|
return validators
|
||||||
|
|
||||||
|
def get_validators_by_election_id(self, election_id):
|
||||||
|
result = backend.query.get_validator_set_by_election_id(self.connection, election_id)
|
||||||
|
return result
|
||||||
|
|
||||||
def delete_validator_update(self):
|
def delete_validator_update(self):
|
||||||
return backend.query.delete_validator_update(self.connection)
|
return backend.query.delete_validator_update(self.connection)
|
||||||
|
|
||||||
def store_pre_commit_state(self, state):
|
def store_pre_commit_state(self, state):
|
||||||
return backend.query.store_pre_commit_state(self.connection, state)
|
return backend.query.store_pre_commit_state(self.connection, state)
|
||||||
|
|
||||||
def store_validator_set(self, height, validators):
|
def store_validator_set(self, height, validators, election_id):
|
||||||
"""Store validator set at a given `height`.
|
"""Store validator set at a given `height`.
|
||||||
NOTE: If the validator set already exists at that `height` then an
|
NOTE: If the validator set already exists at that `height` then an
|
||||||
exception will be raised.
|
exception will be raised.
|
||||||
"""
|
"""
|
||||||
return backend.query.store_validator_set(self.connection, {'height': height,
|
return backend.query.store_validator_set(self.connection, {'height': height,
|
||||||
'validators': validators})
|
'validators': validators,
|
||||||
|
'election_id': election_id})
|
||||||
|
|
||||||
|
|
||||||
Block = namedtuple('Block', ('app_hash', 'height', 'transactions'))
|
Block = namedtuple('Block', ('app_hash', 'height', 'transactions'))
|
||||||
|
|||||||
@ -29,6 +29,11 @@ class ValidatorElection(Transaction):
|
|||||||
# by renaming CREATE to VALIDATOR_ELECTION
|
# by renaming CREATE to VALIDATOR_ELECTION
|
||||||
CREATE = VALIDATOR_ELECTION
|
CREATE = VALIDATOR_ELECTION
|
||||||
ALLOWED_OPERATIONS = (VALIDATOR_ELECTION,)
|
ALLOWED_OPERATIONS = (VALIDATOR_ELECTION,)
|
||||||
|
# Election Statuses:
|
||||||
|
ONGOING = 'ongoing'
|
||||||
|
CONCLUDED = 'concluded'
|
||||||
|
INCONCLUSIVE = 'inconclusive'
|
||||||
|
ELECTION_THRESHOLD = 2 / 3
|
||||||
|
|
||||||
def __init__(self, operation, asset, inputs, outputs,
|
def __init__(self, operation, asset, inputs, outputs,
|
||||||
metadata=None, version=None, hash_id=None):
|
metadata=None, version=None, hash_id=None):
|
||||||
@ -36,6 +41,21 @@ class ValidatorElection(Transaction):
|
|||||||
# of `CREATE` and any validation on `CREATE` in the parent class should apply to it
|
# of `CREATE` and any validation on `CREATE` in the parent class should apply to it
|
||||||
super().__init__(operation, asset, inputs, outputs, metadata, version, hash_id)
|
super().__init__(operation, asset, inputs, outputs, metadata, version, hash_id)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_validator_change(cls, bigchain, height=None):
|
||||||
|
"""Return the latest change to the validator set
|
||||||
|
|
||||||
|
:return: {
|
||||||
|
'height': <block_height>,
|
||||||
|
'asset': {
|
||||||
|
'height': <block_height>,
|
||||||
|
'validators': <validator_set>,
|
||||||
|
'election_id': <election_id_that_approved_the_change>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
return bigchain.get_validator_change(height)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_validators(cls, bigchain, height=None):
|
def get_validators(cls, bigchain, height=None):
|
||||||
"""Return a dictionary of validators with key as `public_key` and
|
"""Return a dictionary of validators with key as `public_key` and
|
||||||
@ -218,9 +238,27 @@ class ValidatorElection(Transaction):
|
|||||||
validator_updates = [election.asset['data']]
|
validator_updates = [election.asset['data']]
|
||||||
curr_validator_set = bigchain.get_validators(new_height)
|
curr_validator_set = bigchain.get_validators(new_height)
|
||||||
updated_validator_set = new_validator_set(curr_validator_set,
|
updated_validator_set = new_validator_set(curr_validator_set,
|
||||||
new_height, validator_updates)
|
validator_updates)
|
||||||
|
|
||||||
updated_validator_set = [v for v in updated_validator_set if v['voting_power'] > 0]
|
updated_validator_set = [v for v in updated_validator_set if v['voting_power'] > 0]
|
||||||
bigchain.store_validator_set(new_height+1, updated_validator_set)
|
bigchain.store_validator_set(new_height+1, updated_validator_set, election.id)
|
||||||
return [encode_validator(election.asset['data'])]
|
return [encode_validator(election.asset['data'])]
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
def get_validator_update_by_election_id(self, election_id, bigchain):
|
||||||
|
result = bigchain.get_validators_by_election_id(election_id)
|
||||||
|
return result
|
||||||
|
|
||||||
|
def get_status(self, bigchain):
|
||||||
|
concluded = self.get_validator_update_by_election_id(self.id, bigchain)
|
||||||
|
if concluded:
|
||||||
|
return self.CONCLUDED
|
||||||
|
|
||||||
|
latest_change = self.get_validator_change(bigchain)
|
||||||
|
latest_change_height = latest_change['height']
|
||||||
|
election_height = bigchain.get_block_containing_tx(self.id)[0]
|
||||||
|
|
||||||
|
if latest_change_height >= election_height:
|
||||||
|
return self.INCONCLUSIVE
|
||||||
|
else:
|
||||||
|
return self.ONGOING
|
||||||
|
|||||||
@ -21,7 +21,7 @@ def decode_validator(v):
|
|||||||
'voting_power': v.power}
|
'voting_power': v.power}
|
||||||
|
|
||||||
|
|
||||||
def new_validator_set(validators, height, updates):
|
def new_validator_set(validators, updates):
|
||||||
validators_dict = {}
|
validators_dict = {}
|
||||||
for v in validators:
|
for v in validators:
|
||||||
validators_dict[v['pub_key']['data']] = v
|
validators_dict[v['pub_key']['data']] = v
|
||||||
|
|||||||
@ -12,10 +12,6 @@ Ideally, each node in a BigchainDB network is owned and controlled by a differen
|
|||||||
|
|
||||||
We use the phrase "BigchainDB consortium" (or just "consortium") to refer to the set of people and/or organizations who run the nodes of a BigchainDB network. A consortium requires some form of governance to make decisions such as membership and policies. The exact details of the governance process are determined by each consortium, but it can be very decentralized.
|
We use the phrase "BigchainDB consortium" (or just "consortium") to refer to the set of people and/or organizations who run the nodes of a BigchainDB network. A consortium requires some form of governance to make decisions such as membership and policies. The exact details of the governance process are determined by each consortium, but it can be very decentralized.
|
||||||
|
|
||||||
If sharding is turned on (i.e. if the number of shards is larger than one), then the actual data is decentralized in that no one node stores all the data.
|
|
||||||
|
|
||||||
Every node has its own locally-stored list of the public keys of other consortium members: the so-called keyring. There's no centrally-stored or centrally-shared keyring.
|
|
||||||
|
|
||||||
A consortium can increase its decentralization (and its resilience) by increasing its jurisdictional diversity, geographic diversity, and other kinds of diversity. This idea is expanded upon in [the section on node diversity](diversity.html).
|
A consortium can increase its decentralization (and its resilience) by increasing its jurisdictional diversity, geographic diversity, and other kinds of diversity. This idea is expanded upon in [the section on node diversity](diversity.html).
|
||||||
|
|
||||||
There’s no node that has a long-term special position in the BigchainDB network. All nodes run the same software and perform the same duties.
|
There’s no node that has a long-term special position in the BigchainDB network. All nodes run the same software and perform the same duties.
|
||||||
|
|||||||
@ -3,6 +3,8 @@
|
|||||||
SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
|
SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
|
||||||
Code is Apache-2.0 and docs are CC-BY-4.0
|
Code is Apache-2.0 and docs are CC-BY-4.0
|
||||||
|
|
||||||
|
.. _permissions-in-bigchaindb:
|
||||||
|
|
||||||
Permissions in BigchainDB
|
Permissions in BigchainDB
|
||||||
-------------------------
|
-------------------------
|
||||||
|
|
||||||
|
|||||||
@ -8,16 +8,8 @@ BigchainDB and Smart Contracts
|
|||||||
|
|
||||||
One can store the source code of any smart contract (i.e. a computer program) in BigchainDB, but BigchainDB won't run arbitrary smart contracts.
|
One can store the source code of any smart contract (i.e. a computer program) in BigchainDB, but BigchainDB won't run arbitrary smart contracts.
|
||||||
|
|
||||||
BigchainDB will run the subset of smart contracts expressible using `Crypto-Conditions <https://tools.ietf.org/html/draft-thomas-crypto-conditions-03>`_.
|
BigchainDB can be used to enforce who has permission to transfer assets, both fungible assets and non-fungible assets. It will prevent double-spending. In other words, a BigchainDB network could be used instead of an ERC-20 (fungible token) or ERC-721 (non-fungible token) smart contract.
|
||||||
|
|
||||||
The owners of an asset can impose conditions on it that must be met for the asset to be transferred to new owners. Examples of possible conditions (crypto-conditions) include:
|
Asset transfer permissions can also be interpreted as write permissions, so they can be used to control who can write to a log, journal or audit trail. There is more about that idea in :ref:`the page about permissions in BigchainDB <permissions-in-bigchaindb>`.
|
||||||
|
|
||||||
- The current owner must sign the transfer transaction (one which transfers ownership to new owners).
|
A BigchainDB network can be connected to other blockchain networks, via oracles or inter-chain communications protocols. That means BigchainDB can be used as part of a solution that uses *other* blockchains to run arbitrary smart contracts.
|
||||||
- Three out of five current owners must sign the transfer transaction.
|
|
||||||
- (Shannon and Kelly) or Morgan must sign the transfer transaction.
|
|
||||||
|
|
||||||
Crypto-conditions can be quite complex. They can't include loops or recursion and therefore will always run/check in finite time.
|
|
||||||
|
|
||||||
.. note::
|
|
||||||
|
|
||||||
We used the word "owners" somewhat loosely above. A more accurate word might be fulfillers, signers, controllers, or transfer-enablers. See the section titled **A Note about Owners** in the relevant `BigchainDB Transactions Spec <https://github.com/bigchaindb/BEPs/tree/master/tx-specs/>`_.
|
|
||||||
|
|||||||
@ -139,3 +139,19 @@ $ bigchaindb upsert-validator approve 04a067582cf03eba2b53b82e4adb5ece424474cbd4
|
|||||||
```
|
```
|
||||||
|
|
||||||
If the command succeeds a message will be returned stating that the vote was submitted successfully. Once a proposal has been approved by sufficent validators (more than `2/3` of the total voting power) then the proposed change is applied to the network. For example, consider a network wherein the total power is `90` then the proposed changed applied only after `60` (`2/3 * 90`) have been received.
|
If the command succeeds a message will be returned stating that the vote was submitted successfully. Once a proposal has been approved by sufficent validators (more than `2/3` of the total voting power) then the proposed change is applied to the network. For example, consider a network wherein the total power is `90` then the proposed changed applied only after `60` (`2/3 * 90`) have been received.
|
||||||
|
|
||||||
|
#### upsert-validator show
|
||||||
|
|
||||||
|
Retrieves information about an election initiated by `upsert-validator new`.
|
||||||
|
|
||||||
|
Below is the command line syntax and the return value,
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ bigchaindb upsert-validator show ELECTION_ID
|
||||||
|
public_key=<e_pub_key>
|
||||||
|
power=<e_power>
|
||||||
|
node_id=<e_node_id>
|
||||||
|
status=<status>
|
||||||
|
```
|
||||||
|
|
||||||
|
The `public_key`, `power`, and `node_id` are the same values used in the `upsert-validator new` command that originally triggered the election. `status` takes three possible values, `ongoing`, if the election has not yet reached a 2/3 majority, `concluded`, if the election reached the 2/3 majority needed to pass, or `inconclusive`, if the validator set changed while the election was in process, rendering it undecidable.
|
||||||
@ -39,9 +39,34 @@ The settings with names of the form `database.*` are for the backend database
|
|||||||
* `database.name` is a user-chosen name for the database inside MongoDB, e.g. `bigchain`.
|
* `database.name` is a user-chosen name for the database inside MongoDB, e.g. `bigchain`.
|
||||||
* `database.connection_timeout` is the maximum number of milliseconds that BigchainDB will wait before giving up on one attempt to connect to the backend database.
|
* `database.connection_timeout` is the maximum number of milliseconds that BigchainDB will wait before giving up on one attempt to connect to the backend database.
|
||||||
* `database.max_tries` is the maximum number of times that BigchainDB will try to establish a connection with the backend database. If 0, then it will try forever.
|
* `database.max_tries` is the maximum number of times that BigchainDB will try to establish a connection with the backend database. If 0, then it will try forever.
|
||||||
* `database.replicaset` is the name of the MongoDB replica set. The default value is `null` because in BighainDB 2.0+, each BigchainDB node has its own independent MongoDB database and no replica set is necessary.
|
* `database.replicaset` is the name of the MongoDB replica set. The default value is `null` because in BigchainDB 2.0+, each BigchainDB node has its own independent MongoDB database and no replica set is necessary. Replica set must already exist if this option is configured, BigchainDB will not create it.
|
||||||
* `database.login` and `database.password` are the login and password used to authenticate to the backend database, specified in plaintext.
|
* `database.ssl` must be `true` or `false`. It tells BigchainDB Server whether it should connect to MongoDB using TLS/SSL or not. The default value is `false`.
|
||||||
* `database.ssl` determines if BigchainDB connects to MongoDB over TLS/SSL or not. It can be set to `true` or `false`.
|
|
||||||
|
There are three ways for BigchainDB Server to authenticate itself with MongoDB (or a specific MongoDB database): no authentication, username/password, and x.509 certificate authentication.
|
||||||
|
|
||||||
|
**No Authentication**
|
||||||
|
|
||||||
|
If you use all the default BigchainDB configuration settings, then no authentication will be used.
|
||||||
|
|
||||||
|
**Username/Password Authentication**
|
||||||
|
|
||||||
|
To use username/password authentication, a MongoDB instance must already be running somewhere (maybe in another machine), it must already have a database for use by BigchainDB (usually named `bigchain`, which is the default `database.name`), and that database must already have a "readWrite" user with associated username and password. To create such a user, login to your MongoDB instance as Admin and run the following commands:
|
||||||
|
|
||||||
|
```text
|
||||||
|
use <database.name>
|
||||||
|
db.createUser({user: "<database.login>", pwd: "<database.password>", roles: [{role: "readWrite", db: "<database.name>"}]})
|
||||||
|
```
|
||||||
|
|
||||||
|
* `database.login` is the user's username.
|
||||||
|
* `database.password` is the user's password, given in plaintext.
|
||||||
|
* `database.ca_cert`, `database.certfile`, `database.keyfile`, `database.crlfile`, and `database.keyfile_passphrase` are not used so they can have their default values.
|
||||||
|
|
||||||
|
**x.509 Certificate Authentication**
|
||||||
|
|
||||||
|
To use x.509 certificate authentication, a MongoDB instance must be running somewhere (maybe in another machine), it must already have a database for use by BigchainDB (usually named `bigchain`, which is the default `database.name`), and that database must be set up to use x.509 authentication. See the MongoDB docs about how to do that.
|
||||||
|
|
||||||
|
* `database.login` is the user's username.
|
||||||
|
* `database.password` isn't used so the default value (`null`) is fine.
|
||||||
* `database.ca_cert`, `database.certfile`, `database.keyfile` and `database.crlfile` are the paths to the CA, signed certificate, private key and certificate revocation list files respectively.
|
* `database.ca_cert`, `database.certfile`, `database.keyfile` and `database.crlfile` are the paths to the CA, signed certificate, private key and certificate revocation list files respectively.
|
||||||
* `database.keyfile_passphrase` is the private key decryption passphrase, specified in plaintext.
|
* `database.keyfile_passphrase` is the private key decryption passphrase, specified in plaintext.
|
||||||
|
|
||||||
|
|||||||
@ -7,7 +7,6 @@ from unittest import mock
|
|||||||
import pytest
|
import pytest
|
||||||
import pymongo
|
import pymongo
|
||||||
from pymongo import MongoClient
|
from pymongo import MongoClient
|
||||||
from pymongo.database import Database
|
|
||||||
|
|
||||||
|
|
||||||
pytestmark = [pytest.mark.bdb, pytest.mark.tendermint]
|
pytestmark = [pytest.mark.bdb, pytest.mark.tendermint]
|
||||||
@ -109,87 +108,3 @@ def test_connection_with_credentials(mock_authenticate):
|
|||||||
password='secret')
|
password='secret')
|
||||||
conn.connect()
|
conn.connect()
|
||||||
assert mock_authenticate.call_count == 1
|
assert mock_authenticate.call_count == 1
|
||||||
|
|
||||||
|
|
||||||
def test_check_replica_set_not_enabled(mongodb_connection):
|
|
||||||
from bigchaindb.backend.localmongodb.connection import _check_replica_set
|
|
||||||
from bigchaindb.common.exceptions import ConfigurationError
|
|
||||||
|
|
||||||
# no replSet option set
|
|
||||||
cmd_line_opts = {'argv': ['mongod', '--dbpath=/data'],
|
|
||||||
'ok': 1.0,
|
|
||||||
'parsed': {'storage': {'dbPath': '/data'}}}
|
|
||||||
with mock.patch.object(Database, 'command', return_value=cmd_line_opts):
|
|
||||||
with pytest.raises(ConfigurationError):
|
|
||||||
_check_replica_set(mongodb_connection)
|
|
||||||
|
|
||||||
|
|
||||||
def test_check_replica_set_command_line(mongodb_connection,
|
|
||||||
mock_cmd_line_opts):
|
|
||||||
from bigchaindb.backend.localmongodb.connection import _check_replica_set
|
|
||||||
|
|
||||||
# replSet option set through the command line
|
|
||||||
with mock.patch.object(Database, 'command',
|
|
||||||
return_value=mock_cmd_line_opts):
|
|
||||||
assert _check_replica_set(mongodb_connection) is None
|
|
||||||
|
|
||||||
|
|
||||||
def test_check_replica_set_config_file(mongodb_connection, mock_config_opts):
|
|
||||||
from bigchaindb.backend.localmongodb.connection import _check_replica_set
|
|
||||||
|
|
||||||
# replSet option set through the config file
|
|
||||||
with mock.patch.object(Database, 'command', return_value=mock_config_opts):
|
|
||||||
assert _check_replica_set(mongodb_connection) is None
|
|
||||||
|
|
||||||
|
|
||||||
def test_check_replica_set_name_mismatch(mongodb_connection,
|
|
||||||
mock_cmd_line_opts):
|
|
||||||
from bigchaindb.backend.localmongodb.connection import _check_replica_set
|
|
||||||
from bigchaindb.common.exceptions import ConfigurationError
|
|
||||||
|
|
||||||
# change the replica set name so it does not match the bigchaindb config
|
|
||||||
mock_cmd_line_opts['parsed']['replication']['replSet'] = 'rs0'
|
|
||||||
|
|
||||||
with mock.patch.object(Database, 'command',
|
|
||||||
return_value=mock_cmd_line_opts):
|
|
||||||
with pytest.raises(ConfigurationError):
|
|
||||||
_check_replica_set(mongodb_connection)
|
|
||||||
|
|
||||||
|
|
||||||
def test_wait_for_replica_set_initialization(mongodb_connection):
|
|
||||||
from bigchaindb.backend.localmongodb.connection import _wait_for_replica_set_initialization # noqa
|
|
||||||
|
|
||||||
with mock.patch.object(Database, 'command') as mock_command:
|
|
||||||
mock_command.side_effect = [
|
|
||||||
{'log': ['a line']},
|
|
||||||
{'log': ['database writes are now permitted']},
|
|
||||||
]
|
|
||||||
|
|
||||||
# check that it returns
|
|
||||||
assert _wait_for_replica_set_initialization(mongodb_connection) is None
|
|
||||||
|
|
||||||
|
|
||||||
def test_initialize_replica_set(mock_cmd_line_opts):
|
|
||||||
from bigchaindb.backend.localmongodb.connection import initialize_replica_set
|
|
||||||
|
|
||||||
with mock.patch.object(Database, 'command') as mock_command:
|
|
||||||
mock_command.side_effect = [
|
|
||||||
mock_cmd_line_opts,
|
|
||||||
None,
|
|
||||||
{'log': ['database writes are now permitted']},
|
|
||||||
]
|
|
||||||
|
|
||||||
# check that it returns
|
|
||||||
assert initialize_replica_set('host', 1337, 1000, 'dbname', False, None, None,
|
|
||||||
None, None, None, None, None) is None
|
|
||||||
|
|
||||||
# test it raises OperationError if anything wrong
|
|
||||||
with mock.patch.object(Database, 'command') as mock_command:
|
|
||||||
mock_command.side_effect = [
|
|
||||||
mock_cmd_line_opts,
|
|
||||||
pymongo.errors.OperationFailure(None, details={'codeName': ''})
|
|
||||||
]
|
|
||||||
|
|
||||||
with pytest.raises(pymongo.errors.OperationFailure):
|
|
||||||
initialize_replica_set('host', 1337, 1000, 'dbname', False, None,
|
|
||||||
None, None, None, None, None, None) is None
|
|
||||||
|
|||||||
@ -380,7 +380,7 @@ def test_validator_update():
|
|||||||
conn = connect()
|
conn = connect()
|
||||||
|
|
||||||
def gen_validator_update(height):
|
def gen_validator_update(height):
|
||||||
return {'data': 'somedata', 'height': height}
|
return {'data': 'somedata', 'height': height, 'election_id': f'election_id_at_height_{height}'}
|
||||||
|
|
||||||
for i in range(1, 100, 10):
|
for i in range(1, 100, 10):
|
||||||
value = gen_validator_update(i)
|
value = gen_validator_update(i)
|
||||||
|
|||||||
@ -44,7 +44,7 @@ def test_init_creates_db_tables_and_indexes():
|
|||||||
assert set(indexes) == {'_id_', 'pre_commit_id'}
|
assert set(indexes) == {'_id_', 'pre_commit_id'}
|
||||||
|
|
||||||
indexes = conn.conn[dbname]['validators'].index_information().keys()
|
indexes = conn.conn[dbname]['validators'].index_information().keys()
|
||||||
assert set(indexes) == {'_id_', 'height'}
|
assert set(indexes) == {'_id_', 'height', 'election_id'}
|
||||||
|
|
||||||
|
|
||||||
def test_init_database_fails_if_db_exists():
|
def test_init_database_fails_if_db_exists():
|
||||||
|
|||||||
@ -347,34 +347,20 @@ class MockResponse():
|
|||||||
return {'result': {'latest_block_height': self.height}}
|
return {'result': {'latest_block_height': self.height}}
|
||||||
|
|
||||||
|
|
||||||
# @pytest.mark.execute
|
@pytest.mark.abci
|
||||||
# @patch('bigchaindb.lib.BigchainDB.get_validators')
|
def test_upsert_validator_new_with_tendermint(b, priv_validator_path, user_sk, validators):
|
||||||
# @pytest.mark.abci
|
|
||||||
@pytest.mark.skip
|
|
||||||
def test_upsert_validator_new_with_tendermint(b, priv_validator_path, user_sk, monkeypatch):
|
|
||||||
"""WIP: Will be fixed and activated in the next PR
|
|
||||||
"""
|
|
||||||
from bigchaindb.commands.bigchaindb import run_upsert_validator_new
|
from bigchaindb.commands.bigchaindb import run_upsert_validator_new
|
||||||
import time
|
|
||||||
|
|
||||||
time.sleep(3)
|
new_args = Namespace(action='new',
|
||||||
|
public_key='8eJ8q9ZQpReWyQT5aFCiwtZ5wDZC4eDnCen88p3tQ6ie',
|
||||||
|
power=1,
|
||||||
|
node_id='unique_node_id_for_test_upsert_validator_new_with_tendermint',
|
||||||
|
sk=priv_validator_path,
|
||||||
|
config={})
|
||||||
|
|
||||||
# b.get_validators = mock_get
|
election_id = run_upsert_validator_new(new_args, b)
|
||||||
# mock_get_validators = mock_get
|
|
||||||
# monkeypatch.setattr('requests.get', mock_get)
|
|
||||||
|
|
||||||
proposer_key = b.get_validators()[0]['pub_key']['value']
|
assert b.get_transaction(election_id)
|
||||||
|
|
||||||
args = Namespace(action='new',
|
|
||||||
public_key=proposer_key,
|
|
||||||
power=1,
|
|
||||||
node_id='12345',
|
|
||||||
sk=priv_validator_path,
|
|
||||||
config={})
|
|
||||||
resp = run_upsert_validator_new(args, b)
|
|
||||||
time.sleep(3)
|
|
||||||
|
|
||||||
assert b.get_transaction(resp)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.tendermint
|
@pytest.mark.tendermint
|
||||||
@ -386,7 +372,7 @@ def test_upsert_validator_new_without_tendermint(caplog, b, priv_validator_path,
|
|||||||
b.store_bulk_transactions([tx])
|
b.store_bulk_transactions([tx])
|
||||||
return (202, '')
|
return (202, '')
|
||||||
|
|
||||||
b.get_validators = mock_get
|
b.get_validators = mock_get_validators
|
||||||
b.write_transaction = mock_write
|
b.write_transaction = mock_write
|
||||||
|
|
||||||
args = Namespace(action='new',
|
args = Namespace(action='new',
|
||||||
@ -430,7 +416,7 @@ def test_upsert_validator_new_election_invalid_power(caplog, b, priv_validator_p
|
|||||||
return (400, '')
|
return (400, '')
|
||||||
|
|
||||||
b.write_transaction = mock_write
|
b.write_transaction = mock_write
|
||||||
b.get_validators = mock_get
|
b.get_validators = mock_get_validators
|
||||||
args = Namespace(action='new',
|
args = Namespace(action='new',
|
||||||
public_key='CJxdItf4lz2PwEf4SmYNAu/c/VpmX39JEgC5YpH7fxg=',
|
public_key='CJxdItf4lz2PwEf4SmYNAu/c/VpmX39JEgC5YpH7fxg=',
|
||||||
power=10,
|
power=10,
|
||||||
@ -533,7 +519,7 @@ def test_upsert_validator_approve_called_with_bad_key(caplog, b, bad_validator_p
|
|||||||
'the eligible voters in this election.'
|
'the eligible voters in this election.'
|
||||||
|
|
||||||
|
|
||||||
def mock_get(height):
|
def mock_get_validators(height):
|
||||||
keys = node_keys()
|
keys = node_keys()
|
||||||
pub_key = list(keys.keys())[0]
|
pub_key = list(keys.keys())[0]
|
||||||
return [
|
return [
|
||||||
@ -550,7 +536,7 @@ def call_election(b, new_validator, node_key):
|
|||||||
return (202, '')
|
return (202, '')
|
||||||
|
|
||||||
# patch the validator set. We now have one validator with power 10
|
# patch the validator set. We now have one validator with power 10
|
||||||
b.get_validators = mock_get
|
b.get_validators = mock_get_validators
|
||||||
b.write_transaction = mock_write
|
b.write_transaction = mock_write
|
||||||
|
|
||||||
# our voters is a list of length 1, populated from our mocked validator
|
# our voters is a list of length 1, populated from our mocked validator
|
||||||
|
|||||||
@ -72,6 +72,12 @@ def test_configure_bigchaindb_configures_bigchaindb():
|
|||||||
logging.CRITICAL)
|
logging.CRITICAL)
|
||||||
)))
|
)))
|
||||||
def test_configure_bigchaindb_logging(log_level):
|
def test_configure_bigchaindb_logging(log_level):
|
||||||
|
# TODO: See following comment:
|
||||||
|
# This is a dirty test. If a test *preceding* this test makes use of the logger, and then another test *after* this
|
||||||
|
# test also makes use of the logger, somehow we get logger.disabled == True, and the later test fails. We need to
|
||||||
|
# either engineer this somehow to leave the test env in the same state as it finds it, or make an assessment
|
||||||
|
# whether or not we even need this test, and potentially just remove it.
|
||||||
|
|
||||||
from bigchaindb.commands.utils import configure_bigchaindb
|
from bigchaindb.commands.utils import configure_bigchaindb
|
||||||
|
|
||||||
@configure_bigchaindb
|
@configure_bigchaindb
|
||||||
|
|||||||
@ -631,6 +631,10 @@ def bad_validator_path(node_keys):
|
|||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def validators(b, node_keys):
|
def validators(b, node_keys):
|
||||||
from bigchaindb.backend import query
|
from bigchaindb.backend import query
|
||||||
|
import time
|
||||||
|
|
||||||
|
def timestamp(): # we need this to force unique election_ids for setup and teardown of fixtures
|
||||||
|
return str(time.time())
|
||||||
|
|
||||||
height = get_block_height(b)
|
height = get_block_height(b)
|
||||||
|
|
||||||
@ -645,7 +649,8 @@ def validators(b, node_keys):
|
|||||||
'voting_power': 10}]
|
'voting_power': 10}]
|
||||||
|
|
||||||
validator_update = {'validators': validator_set,
|
validator_update = {'validators': validator_set,
|
||||||
'height': height + 1}
|
'height': height + 1,
|
||||||
|
'election_id': f'setup_at_{timestamp()}'}
|
||||||
|
|
||||||
query.store_validator_set(b.connection, validator_update)
|
query.store_validator_set(b.connection, validator_update)
|
||||||
|
|
||||||
@ -654,7 +659,8 @@ def validators(b, node_keys):
|
|||||||
height = get_block_height(b)
|
height = get_block_height(b)
|
||||||
|
|
||||||
validator_update = {'validators': original_validators,
|
validator_update = {'validators': original_validators,
|
||||||
'height': height}
|
'height': height,
|
||||||
|
'election_id': f'teardown_at_{timestamp()}'}
|
||||||
|
|
||||||
query.store_validator_set(b.connection, validator_update)
|
query.store_validator_set(b.connection, validator_update)
|
||||||
|
|
||||||
|
|||||||
@ -237,8 +237,8 @@ def test_new_validator_set(b):
|
|||||||
|
|
||||||
validators = [node1]
|
validators = [node1]
|
||||||
updates = [node1_new_power, node2]
|
updates = [node1_new_power, node2]
|
||||||
b.store_validator_set(1, validators)
|
b.store_validator_set(1, validators, 'election_id')
|
||||||
updated_validator_set = new_validator_set(b.get_validators(1), 1, updates)
|
updated_validator_set = new_validator_set(b.get_validators(1), updates)
|
||||||
|
|
||||||
updated_validators = []
|
updated_validators = []
|
||||||
for u in updates:
|
for u in updates:
|
||||||
|
|||||||
@ -4,6 +4,9 @@
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from bigchaindb import ValidatorElectionVote
|
||||||
|
from bigchaindb.backend.localmongodb import query
|
||||||
|
from bigchaindb.lib import Block
|
||||||
from bigchaindb.upsert_validator import ValidatorElection
|
from bigchaindb.upsert_validator import ValidatorElection
|
||||||
|
|
||||||
|
|
||||||
@ -41,3 +44,56 @@ def valid_election_b(b, node_key, new_validator):
|
|||||||
return ValidatorElection.generate([node_key.public_key],
|
return ValidatorElection.generate([node_key.public_key],
|
||||||
voters,
|
voters,
|
||||||
new_validator, None).sign([node_key.private_key])
|
new_validator, None).sign([node_key.private_key])
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def ongoing_election(b, valid_election, ed25519_node_keys):
|
||||||
|
validators = b.get_validators(height=1)
|
||||||
|
genesis_validators = {'validators': validators,
|
||||||
|
'height': 0,
|
||||||
|
'election_id': None}
|
||||||
|
query.store_validator_set(b.connection, genesis_validators)
|
||||||
|
|
||||||
|
b.store_bulk_transactions([valid_election])
|
||||||
|
block_1 = Block(app_hash='hash_1', height=1, transactions=[valid_election.id])
|
||||||
|
b.store_block(block_1._asdict())
|
||||||
|
return valid_election
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def concluded_election(b, ongoing_election, ed25519_node_keys):
|
||||||
|
validators = b.get_validators(height=1)
|
||||||
|
validator_update = {'validators': validators,
|
||||||
|
'height': 2,
|
||||||
|
'election_id': ongoing_election.id}
|
||||||
|
|
||||||
|
query.store_validator_set(b.connection, validator_update)
|
||||||
|
return ongoing_election
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def inconclusive_election(b, ongoing_election, new_validator):
|
||||||
|
validators = b.get_validators(height=1)
|
||||||
|
validators[0]['voting_power'] = 15
|
||||||
|
validator_update = {'validators': validators,
|
||||||
|
'height': 2,
|
||||||
|
'election_id': 'some_other_election'}
|
||||||
|
|
||||||
|
query.store_validator_set(b.connection, validator_update)
|
||||||
|
return ongoing_election
|
||||||
|
|
||||||
|
|
||||||
|
def vote(election, voter, keys, b):
|
||||||
|
election_input = election.to_inputs()[voter]
|
||||||
|
votes = election.outputs[voter].amount
|
||||||
|
public_key = election_input.owners_before[0]
|
||||||
|
key = keys[public_key]
|
||||||
|
|
||||||
|
election_pub_key = ValidatorElection.to_public_key(election.id)
|
||||||
|
|
||||||
|
v = ValidatorElectionVote.generate([election_input],
|
||||||
|
[([election_pub_key], votes)],
|
||||||
|
election_id=election.id)\
|
||||||
|
.sign([key.private_key])
|
||||||
|
b.store_bulk_transactions([v])
|
||||||
|
return v
|
||||||
|
|||||||
@ -1,9 +1,11 @@
|
|||||||
# Copyright BigchainDB GmbH and BigchainDB contributors
|
# Copyright BigchainDB GmbH and BigchainDB contributors
|
||||||
# SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
|
# SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
|
||||||
# Code is Apache-2.0 and docs are CC-BY-4.0
|
# Code is Apache-2.0 and docs are CC-BY-4.0
|
||||||
|
from argparse import Namespace
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from bigchaindb.tendermint_utils import public_key_to_base64
|
||||||
from bigchaindb.upsert_validator import ValidatorElection
|
from bigchaindb.upsert_validator import ValidatorElection
|
||||||
from bigchaindb.common.exceptions import (DuplicateTransaction,
|
from bigchaindb.common.exceptions import (DuplicateTransaction,
|
||||||
UnequalValidatorSet,
|
UnequalValidatorSet,
|
||||||
@ -57,11 +59,8 @@ def test_upsert_validator_invalid_inputs_election(b_mock, new_validator, node_ke
|
|||||||
election.validate(b_mock)
|
election.validate(b_mock)
|
||||||
|
|
||||||
|
|
||||||
def test_upsert_validator_invalid_election(b_mock, new_validator, node_key):
|
def test_upsert_validator_invalid_election(b_mock, new_validator, node_key, valid_election):
|
||||||
voters = ValidatorElection.recipients(b_mock)
|
voters = ValidatorElection.recipients(b_mock)
|
||||||
valid_election = ValidatorElection.generate([node_key.public_key],
|
|
||||||
voters,
|
|
||||||
new_validator, None).sign([node_key.private_key])
|
|
||||||
duplicate_election = ValidatorElection.generate([node_key.public_key],
|
duplicate_election = ValidatorElection.generate([node_key.public_key],
|
||||||
voters,
|
voters,
|
||||||
new_validator, None).sign([node_key.private_key])
|
new_validator, None).sign([node_key.private_key])
|
||||||
@ -95,3 +94,67 @@ def test_upsert_validator_invalid_election(b_mock, new_validator, node_key):
|
|||||||
|
|
||||||
with pytest.raises(UnequalValidatorSet):
|
with pytest.raises(UnequalValidatorSet):
|
||||||
tx_election.validate(b_mock)
|
tx_election.validate(b_mock)
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_status_ongoing(b, ongoing_election, new_validator):
|
||||||
|
status = ValidatorElection.ONGOING
|
||||||
|
resp = ongoing_election.get_status(b)
|
||||||
|
assert resp == status
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_status_concluded(b, concluded_election, new_validator):
|
||||||
|
status = ValidatorElection.CONCLUDED
|
||||||
|
resp = concluded_election.get_status(b)
|
||||||
|
assert resp == status
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_status_inconclusive(b, inconclusive_election, new_validator):
|
||||||
|
def custom_mock_get_validators(height):
|
||||||
|
if height >= 3:
|
||||||
|
return [{'pub_key': {'data': 'zL/DasvKulXZzhSNFwx4cLRXKkSM9GPK7Y0nZ4FEylM=',
|
||||||
|
'type': 'AC26791624DE60'},
|
||||||
|
'voting_power': 15},
|
||||||
|
{'pub_key': {'data': 'GIijU7GBcVyiVUcB0GwWZbxCxdk2xV6pxdvL24s/AqM=',
|
||||||
|
'type': 'AC26791624DE60'},
|
||||||
|
'voting_power': 7},
|
||||||
|
{'pub_key': {'data': 'JbfwrLvCVIwOPm8tj8936ki7IYbmGHjPiKb6nAZegRA=',
|
||||||
|
'type': 'AC26791624DE60'},
|
||||||
|
'voting_power': 10},
|
||||||
|
{'pub_key': {'data': 'PecJ58SaNRsWJZodDmqjpCWqG6btdwXFHLyE40RYlYM=',
|
||||||
|
'type': 'AC26791624DE60'},
|
||||||
|
'voting_power': 8}]
|
||||||
|
else:
|
||||||
|
return [{'pub_key': {'data': 'zL/DasvKulXZzhSNFwx4cLRXKkSM9GPK7Y0nZ4FEylM=',
|
||||||
|
'type': 'AC26791624DE60'},
|
||||||
|
'voting_power': 9},
|
||||||
|
{'pub_key': {'data': 'GIijU7GBcVyiVUcB0GwWZbxCxdk2xV6pxdvL24s/AqM=',
|
||||||
|
'type': 'AC26791624DE60'},
|
||||||
|
'voting_power': 7},
|
||||||
|
{'pub_key': {'data': 'JbfwrLvCVIwOPm8tj8936ki7IYbmGHjPiKb6nAZegRA=',
|
||||||
|
'type': 'AC26791624DE60'},
|
||||||
|
'voting_power': 10},
|
||||||
|
{'pub_key': {'data': 'PecJ58SaNRsWJZodDmqjpCWqG6btdwXFHLyE40RYlYM=',
|
||||||
|
'type': 'AC26791624DE60'},
|
||||||
|
'voting_power': 8}]
|
||||||
|
|
||||||
|
b.get_validators = custom_mock_get_validators
|
||||||
|
status = ValidatorElection.INCONCLUSIVE
|
||||||
|
resp = inconclusive_election.get_status(b)
|
||||||
|
assert resp == status
|
||||||
|
|
||||||
|
|
||||||
|
def test_upsert_validator_show(caplog, ongoing_election, b):
|
||||||
|
from bigchaindb.commands.bigchaindb import run_upsert_validator_show
|
||||||
|
|
||||||
|
election_id = ongoing_election.id
|
||||||
|
public_key = public_key_to_base64(ongoing_election.asset['data']['public_key'])
|
||||||
|
power = ongoing_election.asset['data']['power']
|
||||||
|
node_id = ongoing_election.asset['data']['node_id']
|
||||||
|
status = ValidatorElection.ONGOING
|
||||||
|
|
||||||
|
show_args = Namespace(action='show',
|
||||||
|
election_id=election_id)
|
||||||
|
|
||||||
|
msg = run_upsert_validator_show(show_args, b)
|
||||||
|
|
||||||
|
assert msg == f'public_key={public_key}\npower={power}\nnode_id={node_id}\nstatus={status}'
|
||||||
|
|||||||
@ -234,7 +234,7 @@ def test_upsert_validator(b, node_key, node_keys, ed25519_node_keys):
|
|||||||
|
|
||||||
latest_block = b.get_latest_block()
|
latest_block = b.get_latest_block()
|
||||||
# reset the validator set
|
# reset the validator set
|
||||||
b.store_validator_set(latest_block['height'], validators)
|
b.store_validator_set(latest_block['height'], validators, 'previous_election_id')
|
||||||
|
|
||||||
power = 1
|
power = 1
|
||||||
public_key = '9B3119650DF82B9A5D8A12E38953EA47475C09F0C48A4E6A0ECE182944B24403'
|
public_key = '9B3119650DF82B9A5D8A12E38953EA47475C09F0C48A4E6A0ECE182944B24403'
|
||||||
@ -368,4 +368,4 @@ def reset_validator_set(b, node_keys, height):
|
|||||||
validators.append({'pub_key': {'type': 'ed25519',
|
validators.append({'pub_key': {'type': 'ed25519',
|
||||||
'data': node_pub},
|
'data': node_pub},
|
||||||
'voting_power': 10})
|
'voting_power': 10})
|
||||||
b.store_validator_set(height, validators)
|
b.store_validator_set(height, validators, 'election_id')
|
||||||
|
|||||||
@ -14,7 +14,7 @@ def test_get_validators_endpoint(b, client):
|
|||||||
'pub_key': {'data': '4E2685D9016126864733225BE00F005515200727FBAB1312FC78C8B76831255A',
|
'pub_key': {'data': '4E2685D9016126864733225BE00F005515200727FBAB1312FC78C8B76831255A',
|
||||||
'type': 'ed25519'},
|
'type': 'ed25519'},
|
||||||
'voting_power': 10}]
|
'voting_power': 10}]
|
||||||
b.store_validator_set(23, validator_set)
|
b.store_validator_set(23, validator_set, 'election_id')
|
||||||
|
|
||||||
res = client.get(VALIDATORS_ENDPOINT)
|
res = client.get(VALIDATORS_ENDPOINT)
|
||||||
assert is_validator(res.json[0])
|
assert is_validator(res.json[0])
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user