From e599914f31e535f4c2396a9167c7887f13c6b751 Mon Sep 17 00:00:00 2001 From: z-bowen Date: Fri, 14 Sep 2018 15:18:01 +0200 Subject: [PATCH] Problem: `MigrationElection` naming may conflict with future migration types Solution: Renamed the class `ChainMigrationElection` --- bigchaindb/__init__.py | 4 +- bigchaindb/commands/bigchaindb.py | 6 +-- bigchaindb/commands/election_types.py | 2 +- bigchaindb/common/schema/__init__.py | 4 +- ...action_chain_migration_election_v2.0.yaml} | 4 +- .../common/schema/transaction_v2.0.yaml | 2 +- ...lection.py => chain_migration_election.py} | 8 +-- tests/commands/test_commands.py | 14 +++--- tests/conftest.py | 50 +++++++++---------- tests/elections/test_election.py | 36 +++++++------ tests/migrations/conftest.py | 11 ---- tests/migrations/test_migration_election.py | 10 ++-- 12 files changed, 73 insertions(+), 78 deletions(-) rename bigchaindb/common/schema/{transaction_migration_election_v2.0.yaml => transaction_chain_migration_election_v2.0.yaml} (86%) rename bigchaindb/migrations/{migration_election.py => chain_migration_election.py} (55%) delete mode 100644 tests/migrations/conftest.py diff --git a/bigchaindb/__init__.py b/bigchaindb/__init__.py index 7b10446b..223d7a0e 100644 --- a/bigchaindb/__init__.py +++ b/bigchaindb/__init__.py @@ -7,7 +7,7 @@ import logging from bigchaindb.log import DEFAULT_LOGGING_CONFIG as log_config from bigchaindb.lib import BigchainDB # noqa -from bigchaindb.migrations.migration_election import MigrationElection +from bigchaindb.migrations.chain_migration_election import ChainMigrationElection from bigchaindb.version import __version__ # noqa from bigchaindb.core import App # noqa @@ -100,5 +100,5 @@ from bigchaindb.elections.vote import Vote # noqa Transaction.register_type(Transaction.CREATE, models.Transaction) Transaction.register_type(Transaction.TRANSFER, models.Transaction) Transaction.register_type(ValidatorElection.OPERATION, ValidatorElection) -Transaction.register_type(MigrationElection.OPERATION, MigrationElection) +Transaction.register_type(ChainMigrationElection.OPERATION, ChainMigrationElection) Transaction.register_type(Vote.OPERATION, Vote) diff --git a/bigchaindb/commands/bigchaindb.py b/bigchaindb/commands/bigchaindb.py index a46c5539..f9588dcd 100644 --- a/bigchaindb/commands/bigchaindb.py +++ b/bigchaindb/commands/bigchaindb.py @@ -13,7 +13,7 @@ import copy import json import sys -from bigchaindb.migrations.migration_election import MigrationElection +from bigchaindb.migrations.chain_migration_election import ChainMigrationElection from bigchaindb.utils import load_node_key from bigchaindb.common.exceptions import (DatabaseDoesNotExist, ValidationError) @@ -165,7 +165,7 @@ def run_election_new_upsert_validator(args, bigchain): return create_new_election(args.sk, bigchain, ValidatorElection, new_validator) -def run_election_new_migration(args, bigchain): +def run_election_new_chain_migration(args, bigchain): """Initiates an election to halt block production :param args: dict @@ -176,7 +176,7 @@ def run_election_new_migration(args, bigchain): :return: election_id or `False` in case of failure """ - return create_new_election(args.sk, bigchain, MigrationElection, {}) + return create_new_election(args.sk, bigchain, ChainMigrationElection, {}) def run_election_approve(args, bigchain): diff --git a/bigchaindb/commands/election_types.py b/bigchaindb/commands/election_types.py index 8220f4a0..4745b85d 100644 --- a/bigchaindb/commands/election_types.py +++ b/bigchaindb/commands/election_types.py @@ -17,7 +17,7 @@ elections = { } } }, - 'migration': { + 'chain-migration': { 'help': 'Call for a halt to block production to allow for a version change across breaking changes.', 'args': { '--private-key': { diff --git a/bigchaindb/common/schema/__init__.py b/bigchaindb/common/schema/__init__.py index f2da3c9f..6280b7b9 100644 --- a/bigchaindb/common/schema/__init__.py +++ b/bigchaindb/common/schema/__init__.py @@ -37,8 +37,8 @@ _, TX_SCHEMA_TRANSFER = _load_schema('transaction_transfer_' + _, TX_SCHEMA_VALIDATOR_ELECTION = _load_schema('transaction_validator_election_' + TX_SCHEMA_VERSION) -_, TX_SCHEMA_MIGRATION_ELECTION = _load_schema('transaction_migration_election_' + - TX_SCHEMA_VERSION) +_, TX_SCHEMA_CHAIN_MIGRATION_ELECTION = _load_schema('transaction_chain_migration_election_' + + TX_SCHEMA_VERSION) _, TX_SCHEMA_VOTE = _load_schema('transaction_vote_' + TX_SCHEMA_VERSION) diff --git a/bigchaindb/common/schema/transaction_migration_election_v2.0.yaml b/bigchaindb/common/schema/transaction_chain_migration_election_v2.0.yaml similarity index 86% rename from bigchaindb/common/schema/transaction_migration_election_v2.0.yaml rename to bigchaindb/common/schema/transaction_chain_migration_election_v2.0.yaml index 7905d884..40fa1a7b 100644 --- a/bigchaindb/common/schema/transaction_migration_election_v2.0.yaml +++ b/bigchaindb/common/schema/transaction_chain_migration_election_v2.0.yaml @@ -5,7 +5,7 @@ --- "$schema": "http://json-schema.org/draft-04/schema#" type: object -title: Migration Election Schema - Propose a halt in block production to allow for a version change +title: Chain Migration Election Schema - Propose a halt in block production to allow for a version change required: - operation - asset @@ -13,7 +13,7 @@ required: properties: operation: type: string - value: "MIGRATION_ELECTION" + value: "CHAIN_MIGRATION_ELECTION" asset: additionalProperties: false properties: diff --git a/bigchaindb/common/schema/transaction_v2.0.yaml b/bigchaindb/common/schema/transaction_v2.0.yaml index 30c04064..6c056f7f 100644 --- a/bigchaindb/common/schema/transaction_v2.0.yaml +++ b/bigchaindb/common/schema/transaction_v2.0.yaml @@ -63,7 +63,7 @@ definitions: - CREATE - TRANSFER - VALIDATOR_ELECTION - - MIGRATION_ELECTION + - CHAIN_MIGRATION_ELECTION - VOTE asset: type: object diff --git a/bigchaindb/migrations/migration_election.py b/bigchaindb/migrations/chain_migration_election.py similarity index 55% rename from bigchaindb/migrations/migration_election.py rename to bigchaindb/migrations/chain_migration_election.py index c50d753e..e1e783d0 100644 --- a/bigchaindb/migrations/migration_election.py +++ b/bigchaindb/migrations/chain_migration_election.py @@ -1,13 +1,13 @@ -from bigchaindb.common.schema import TX_SCHEMA_MIGRATION_ELECTION +from bigchaindb.common.schema import TX_SCHEMA_CHAIN_MIGRATION_ELECTION from bigchaindb.elections.election import Election -class MigrationElection(Election): +class ChainMigrationElection(Election): - OPERATION = 'MIGRATION_ELECTION' + OPERATION = 'CHAIN_MIGRATION_ELECTION' CREATE = OPERATION ALLOWED_OPERATIONS = (OPERATION,) - TX_SCHEMA_CUSTOM = TX_SCHEMA_MIGRATION_ELECTION + TX_SCHEMA_CUSTOM = TX_SCHEMA_CHAIN_MIGRATION_ELECTION CHANGES_VALIDATOR_SET = False @classmethod diff --git a/tests/commands/test_commands.py b/tests/commands/test_commands.py index 5d897539..bbeea227 100644 --- a/tests/commands/test_commands.py +++ b/tests/commands/test_commands.py @@ -26,7 +26,7 @@ def test_make_sure_we_dont_remove_any_command(): assert parser.parse_args(['start']).command assert parser.parse_args(['election', 'new', 'upsert-validator', 'TEMP_PUB_KEYPAIR', '10', 'TEMP_NODE_ID', '--private-key', 'TEMP_PATH_TO_PRIVATE_KEY']).command - assert parser.parse_args(['election', 'new', 'migration', + assert parser.parse_args(['election', 'new', 'chain-migration', '--private-key', 'TEMP_PATH_TO_PRIVATE_KEY']).command assert parser.parse_args(['election', 'approve', 'ELECTION_ID', '--private-key', 'TEMP_PATH_TO_PRIVATE_KEY']).command @@ -344,22 +344,22 @@ def test_election_new_upsert_validator_without_tendermint(caplog, b, priv_valida @pytest.mark.abci -def test_election_new_migration_with_tendermint(b, priv_validator_path, user_sk, validators): - from bigchaindb.commands.bigchaindb import run_election_new_migration +def test_election_new_chain_migration_with_tendermint(b, priv_validator_path, user_sk, validators): + from bigchaindb.commands.bigchaindb import run_election_new_chain_migration new_args = Namespace(action='new', election_type='migration', sk=priv_validator_path, config={}) - election_id = run_election_new_migration(new_args, b) + election_id = run_election_new_chain_migration(new_args, b) assert b.get_transaction(election_id) @pytest.mark.bdb -def test_election_new_migration_without_tendermint(caplog, b, priv_validator_path, user_sk): - from bigchaindb.commands.bigchaindb import run_election_new_migration +def test_election_new_chain_migration_without_tendermint(caplog, b, priv_validator_path, user_sk): + from bigchaindb.commands.bigchaindb import run_election_new_chain_migration def mock_write(tx, mode): b.store_bulk_transactions([tx]) @@ -374,7 +374,7 @@ def test_election_new_migration_without_tendermint(caplog, b, priv_validator_pat config={}) with caplog.at_level(logging.INFO): - election_id = run_election_new_migration(args, b) + election_id = run_election_new_chain_migration(args, b) assert caplog.records[0].msg == '[SUCCESS] Submitted proposal with id: ' + election_id assert b.get_transaction(election_id) diff --git a/tests/conftest.py b/tests/conftest.py index a628b404..4c271aad 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -23,7 +23,7 @@ from pymongo import MongoClient from bigchaindb import ValidatorElection from bigchaindb.common import crypto from bigchaindb.log import setup_logging -from bigchaindb.migrations.migration_election import MigrationElection +from bigchaindb.migrations.chain_migration_election import ChainMigrationElection from bigchaindb.tendermint_utils import key_from_base64 from bigchaindb.backend import schema, query from bigchaindb.common.crypto import (key_pair_from_ed25519_key, @@ -715,19 +715,19 @@ def valid_upsert_validator_election_2(b_mock, node_key, new_validator): @pytest.fixture -def valid_migration_election(b_mock, node_key): - voters = MigrationElection.recipients(b_mock) - return MigrationElection.generate([node_key.public_key], - voters, - {}, None).sign([node_key.private_key]) +def valid_chain_migration_election(b_mock, node_key): + voters = ChainMigrationElection.recipients(b_mock) + return ChainMigrationElection.generate([node_key.public_key], + voters, + {}, None).sign([node_key.private_key]) @pytest.fixture -def valid_migration_election_2(b_mock, node_key): - voters = MigrationElection.recipients(b_mock) - return MigrationElection.generate([node_key.public_key], - voters, - {}, None).sign([node_key.private_key]) +def valid_chain_migration_election_2(b_mock, node_key): + voters = ChainMigrationElection.recipients(b_mock) + return ChainMigrationElection.generate([node_key.public_key], + voters, + {}, None).sign([node_key.private_key]) @pytest.fixture @@ -759,21 +759,21 @@ def ongoing_validator_election_2(b, valid_upsert_validator_election_2, ed25519_n @pytest.fixture -def ongoing_migration_election(b, valid_migration_election, ed25519_node_keys): +def ongoing_chain_migration_election(b, valid_chain_migration_election, ed25519_node_keys): - b.store_bulk_transactions([valid_migration_election]) - block_1 = Block(app_hash='hash_1', height=1, transactions=[valid_migration_election.id]) + b.store_bulk_transactions([valid_chain_migration_election]) + block_1 = Block(app_hash='hash_1', height=1, transactions=[valid_chain_migration_election.id]) b.store_block(block_1._asdict()) - return valid_migration_election + return valid_chain_migration_election @pytest.fixture -def ongoing_migration_election_2(b, valid_migration_election_2, ed25519_node_keys): +def ongoing_chain_migration_election_2(b, valid_chain_migration_election_2, ed25519_node_keys): - b.store_bulk_transactions([valid_migration_election_2]) - block_1 = Block(app_hash='hash_2', height=1, transactions=[valid_migration_election_2.id]) + b.store_bulk_transactions([valid_chain_migration_election_2]) + block_1 = Block(app_hash='hash_2', height=1, transactions=[valid_chain_migration_election_2.id]) b.store_block(block_1._asdict()) - return valid_migration_election_2 + return valid_chain_migration_election_2 @pytest.fixture @@ -791,16 +791,16 @@ def validator_election_votes_2(b_mock, ongoing_validator_election_2, ed25519_nod @pytest.fixture -def migration_election_votes(b_mock, ongoing_migration_election, ed25519_node_keys): - voters = MigrationElection.recipients(b_mock) - votes = generate_votes(ongoing_migration_election, voters, ed25519_node_keys) +def chain_migration_election_votes(b_mock, ongoing_chain_migration_election, ed25519_node_keys): + voters = ChainMigrationElection.recipients(b_mock) + votes = generate_votes(ongoing_chain_migration_election, voters, ed25519_node_keys) return votes @pytest.fixture -def migration_election_votes_2(b_mock, ongoing_migration_election_2, ed25519_node_keys): - voters = MigrationElection.recipients(b_mock) - votes = generate_votes(ongoing_migration_election_2, voters, ed25519_node_keys) +def chain_migration_election_votes_2(b_mock, ongoing_chain_migration_election_2, ed25519_node_keys): + voters = ChainMigrationElection.recipients(b_mock) + votes = generate_votes(ongoing_chain_migration_election_2, voters, ed25519_node_keys) return votes diff --git a/tests/elections/test_election.py b/tests/elections/test_election.py index 87283d6c..0e9da188 100644 --- a/tests/elections/test_election.py +++ b/tests/elections/test_election.py @@ -6,37 +6,43 @@ from bigchaindb.elections.election import Election @pytest.mark.bdb -def test_approved_elections_one_migration_one_upsert(b, - ongoing_validator_election, validator_election_votes, - ongoing_migration_election, migration_election_votes): +def test_approved_elections_one_migration_one_upsert( + b, + ongoing_validator_election, validator_election_votes, + ongoing_chain_migration_election, chain_migration_election_votes +): txns = validator_election_votes + \ - migration_election_votes + chain_migration_election_votes mock_chain_migration, mock_store_validator = run_approved_elections(b, txns) mock_chain_migration.assert_called_once() mock_store_validator.assert_called_once() @pytest.mark.bdb -def test_approved_elections_one_migration_two_upsert(b, - ongoing_validator_election, validator_election_votes, - ongoing_validator_election_2, validator_election_votes_2, - ongoing_migration_election, migration_election_votes): +def test_approved_elections_one_migration_two_upsert( + b, + ongoing_validator_election, validator_election_votes, + ongoing_validator_election_2, validator_election_votes_2, + ongoing_chain_migration_election, chain_migration_election_votes +): txns = validator_election_votes + \ validator_election_votes_2 + \ - migration_election_votes + chain_migration_election_votes mock_chain_migration, mock_store_validator = run_approved_elections(b, txns) mock_chain_migration.assert_called_once() mock_store_validator.assert_called_once() @pytest.mark.bdb -def test_approved_elections_two_migrations_one_upsert(b, - ongoing_validator_election, validator_election_votes, - ongoing_migration_election, migration_election_votes, - ongoing_migration_election_2, migration_election_votes_2): +def test_approved_elections_two_migrations_one_upsert( + b, + ongoing_validator_election, validator_election_votes, + ongoing_chain_migration_election, chain_migration_election_votes, + ongoing_chain_migration_election_2, chain_migration_election_votes_2 +): txns = validator_election_votes + \ - migration_election_votes + \ - migration_election_votes_2 + chain_migration_election_votes + \ + chain_migration_election_votes_2 mock_chain_migration, mock_store_validator = run_approved_elections(b, txns) assert mock_chain_migration.call_count == 2 mock_store_validator.assert_called_once() diff --git a/tests/migrations/conftest.py b/tests/migrations/conftest.py deleted file mode 100644 index f0efdcf4..00000000 --- a/tests/migrations/conftest.py +++ /dev/null @@ -1,11 +0,0 @@ -import pytest - -from bigchaindb.migrations.migration_election import MigrationElection - - -@pytest.fixture -def valid_migration_election(b_mock, node_key): - voters = MigrationElection.recipients(b_mock) - return MigrationElection.generate([node_key.public_key], - voters, - {}, None).sign([node_key.private_key]) diff --git a/tests/migrations/test_migration_election.py b/tests/migrations/test_migration_election.py index 837bebbd..b811903b 100644 --- a/tests/migrations/test_migration_election.py +++ b/tests/migrations/test_migration_election.py @@ -1,9 +1,9 @@ -from bigchaindb.migrations.migration_election import MigrationElection +from bigchaindb.migrations.chain_migration_election import ChainMigrationElection def test_valid_migration_election(b_mock, node_key): - voters = MigrationElection.recipients(b_mock) - election = MigrationElection.generate([node_key.public_key], - voters, - {}, None).sign([node_key.private_key]) + voters = ChainMigrationElection.recipients(b_mock) + election = ChainMigrationElection.generate([node_key.public_key], + voters, + {}, None).sign([node_key.private_key]) assert election.validate(b_mock)