Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

Upgrade to bigchaindb 2.0 #49

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var/
*.egg-info/
.installed.cfg
*.egg
settings.json

# PyInstaller
# Usually these files are written by a python script from a template
Expand Down
46 changes: 30 additions & 16 deletions coalaip_bigchaindb/plugin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from bigchaindb_driver import BigchainDB
from bigchaindb_driver.transport import Transport
from bigchaindb_driver.crypto import generate_keypair
from bigchaindb_driver.exceptions import (
BigchaindbException,
Expand Down Expand Up @@ -28,26 +29,41 @@ class Plugin(AbstractPlugin):
related actions.
"""

def __init__(self, *nodes):
def __init__(self, *nodes, transport_class=Transport, headers=None, timeout=20):
"""Initialize a :class:`~.Plugin` instance and connect to one or
more BigchainDB nodes.

Args:
*nodes (str): One or more URLs of BigchainDB nodes to
connect to as the persistence layer
*nodes (list of (str or dict)): BigchainDB nodes to connect to.
Currently, the full URL must be given. In the absence of any
node, the default(``'http://localhost:9984'``) will be used.
If node is passed as a dict, `endpoint` is a required key;
`headers` is an optional `dict` of headers.
transport_class: Optional transport class to use.
Defaults to :class:`~bigchaindb_driver.transport.Transport`.
headers (dict): Optional headers that will be passed with
each request. To pass headers only on a per-request
basis, you can pass the headers to the method of choice
(e.g. :meth:`BigchainDB().transactions.send_commit()
<.TransactionsEndpoint.send_commit>`).
timeout (int): Optional timeout in seconds that will be passed
to each request.
"""

self.driver = BigchainDB(*nodes)
self.driver = BigchainDB(*nodes, transport_class=transport_class, headers=headers, timeout=timeout)

@property
def type(self):
"""str: the type of this plugin (``'BigchainDB'``)"""
return 'BigchainDB'

def generate_user(self):
def generate_user(self, seed=None):
"""Create a new public/private keypair for use with
BigchainDB.

Args:
seed (bytes): 32-byte seed for deterministic generation.
Defaults to `None`.

Returns:
dict: A dict containing a new user's public and private
keys::
Expand All @@ -58,7 +74,7 @@ def generate_user(self):
}
"""

return generate_keypair()._asdict()
return generate_keypair(seed)._asdict()

def is_same_user(self, user_a, user_b):
"""Check if :attr:`user_a` represents the same user as
Expand Down Expand Up @@ -124,21 +140,19 @@ def get_status(self, persist_id):
str: the status of the entity; one of::

'valid': the transaction has been written in a validated block
'invalid': the block the transaction was in was voted invalid
'undecided': the block the transaction is in is still undecided
'backlog': the transaction is still in the backlog

Raises:
:exc:`coalaip.EntityNotFoundError`: If no transaction whose
'uuid' matches :attr:`persist_id` could be found in the
connected BigchainDB instance
connected BigchainDB instance. This could be because the
transaction is still processing.
:exc:`~.PersistenceError`: If any other unhandled error
from the BigchainDB driver occurred.
"""

try:
return self.driver.transactions.status(persist_id)
except NotFoundError:
if self.driver.blocks.get(txid=persist_id):
return 'valid'
else:
raise EntityNotFoundError()

@reraise_as_persistence_error_if_not(EntityCreationError)
Expand Down Expand Up @@ -183,7 +197,7 @@ def save(self, entity_data, *, user):
except MissingPrivateKeyError as ex:
raise EntityCreationError(error=ex) from ex
try:
self.driver.transactions.send(fulfilled_tx)
self.driver.transactions.send_commit(fulfilled_tx)
except (TransportError, ConnectionError) as ex:
raise EntityCreationError(error=ex) from ex

Expand Down Expand Up @@ -274,7 +288,7 @@ def transfer(self, persist_id, transfer_payload=None, *, from_user,
raise EntityTransferError(error=ex) from ex

try:
transfer_json = self.driver.transactions.send(fulfilled_tx)
transfer_json = self.driver.transactions.send_commit(fulfilled_tx)
except (TransportError, ConnectionError) as ex:
raise EntityTransferError(error=ex) from ex

Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

install_requires = [
'coalaip==0.0.3',
'bigchaindb_driver~=0.4.0',
'bigchaindb_driver~=0.6.2',
]

tests_require = [
Expand All @@ -21,7 +21,7 @@
'pytest>=3.0.1',
'pytest-cov',
'pytest-mock',
'bigchaindb~=1.0.1',
'bigchaindb~=2.0.0b7',
]

dev_require = [
Expand Down
2 changes: 1 addition & 1 deletion setup_deps.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# than enabling notifications for outdated dependencies.

coalaip==0.0.1
bigchaindb-driver==0.2.1
bigchaindb-driver==0.6.2
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def created_manifestation(bdb_driver, manifestation_model_jsonld,
asset={'data': manifestation_model_jsonld})
fulfilled_tx = bdb_driver.transactions.fulfill(
tx, private_keys=alice_keypair['private_key'])
bdb_driver.transactions.send(fulfilled_tx)
bdb_driver.transactions.send_commit(fulfilled_tx)
return fulfilled_tx


Expand Down Expand Up @@ -140,7 +140,7 @@ def transferred_manifestation_tx(bdb_driver, persisted_manifestation,

fulfilled_transfer_tx = bdb_driver.transactions.fulfill(
transfer_tx, private_keys=alice_keypair['private_key'])
bdb_driver.transactions.send(fulfilled_transfer_tx)
bdb_driver.transactions.send_commit(fulfilled_transfer_tx)

# Poll BigchainDB until the transfer becomes valid (and 'persisted')
poll_bdb_transaction_valid(bdb_driver, fulfilled_transfer_tx['id'])
Expand Down
20 changes: 6 additions & 14 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_get_history(plugin, bdb_driver, alice_keypair, bob_keypair,
recipients=bob_keypair['public_key'])
transfer_to_bob_tx = bdb_driver.transactions.fulfill(
transfer_to_bob_tx, private_keys=alice_keypair['private_key'])
bdb_driver.transactions.send(transfer_to_bob_tx)
bdb_driver.transactions.send_commit(transfer_to_bob_tx)

poll_bdb_transaction_valid(bdb_driver, transfer_to_bob_tx['id'])

Expand All @@ -59,7 +59,7 @@ def test_get_history(plugin, bdb_driver, alice_keypair, bob_keypair,
recipients=alice_keypair['public_key'])
transfer_back_to_alice_tx = bdb_driver.transactions.fulfill(
transfer_back_to_alice_tx, private_keys=bob_keypair['private_key'])
bdb_driver.transactions.send(transfer_back_to_alice_tx)
bdb_driver.transactions.send_commit(transfer_back_to_alice_tx)

poll_bdb_transaction_valid(bdb_driver, transfer_back_to_alice_tx['id'])

Expand All @@ -81,18 +81,12 @@ def test_get_history(plugin, bdb_driver, alice_keypair, bob_keypair,


def test_get_status(plugin, created_manifestation_id):
# Poll BigchainDB for the initial status
poll_result(
lambda: plugin.get_status(created_manifestation_id),
lambda result: result['status'] in (
'valid', 'invalid', 'undecided', 'backlog'))

# Poll BigchainDB until the transaction validates; will fail test if the
# transaction's status doesn't become valid by the end of the timeout
# period.
poll_result(
lambda: plugin.get_status(created_manifestation_id),
lambda result: result['status'] == 'valid')
lambda result: result == 'valid' )


@mark.parametrize('model_name', [
Expand Down Expand Up @@ -247,7 +241,7 @@ def mock_driver_error(*args, **kwargs):
ExceptionType = getattr(bdb_exceptions, error_type_name)
raise ExceptionType()

monkeypatch.setattr(plugin.driver.transactions, 'send',
monkeypatch.setattr(plugin.driver.transactions, 'send_commit',
mock_driver_error)

with raises(EntityCreationError):
Expand All @@ -267,7 +261,7 @@ def mock_driver_error(*args, **kwargs):
ExceptionType = getattr(bdb_exceptions, error_type_name)
raise ExceptionType()

monkeypatch.setattr(plugin.driver.transactions, 'send',
monkeypatch.setattr(plugin.driver.transactions, 'send_commit',
mock_driver_error)

with raises(EntityTransferError):
Expand All @@ -280,7 +274,6 @@ def mock_driver_error(*args, **kwargs):

@mark.parametrize('func_name,driver_tx_func_name', [
('get_history', 'get'),
('get_status', 'status'),
('load', 'retrieve')
])
def test_generic_plugin_func_on_id_raises_not_found_error_on_not_found(
Expand Down Expand Up @@ -321,7 +314,6 @@ def mock_driver_not_found_error(*args, **kwargs):

@mark.parametrize('func_name,driver_tx_func_name', [
('get_history', 'get'),
('get_status', 'status'),
('load', 'retrieve')
])
def test_generic_plugin_func_on_id_raises_persistence_error_on_error(
Expand Down Expand Up @@ -357,7 +349,7 @@ def mock_driver_error(*args, **kwargs):
plugin.save(manifestation_model_json, user=alice_keypair)


@mark.parametrize('driver_method_erroring', ['get', 'send'])
@mark.parametrize('driver_method_erroring', ['get', 'send_commit'])
def test_transfer_raises_persistence_error_on_error(
monkeypatch, plugin, alice_keypair, bob_keypair,
persisted_manifestation, driver_method_erroring):
Expand Down
14 changes: 9 additions & 5 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ def test_order_transactions_fails_with_multiple_endings(
create_tx = bdb_driver.transactions.prepare(
operation='CREATE',
signers=alice_keypair['public_key'])
transfer_to_bob_tx = make_transfer_tx(bdb_driver, input_tx=create_tx,
fulfill_tx = bdb_driver.transactions.fulfill(create_tx, alice_keypair['private_key'])
transfer_to_bob_tx = make_transfer_tx(bdb_driver, input_tx=fulfill_tx,
recipients=bob_keypair['public_key'])
transfer_to_carly_tx = make_transfer_tx(
bdb_driver, input_tx=create_tx, recipients=carly_keypair['public_key'])
bdb_driver, input_tx=fulfill_tx, recipients=carly_keypair['public_key'])

with raises(ValueError):
# Transfer to both bob and carly should result in a multiple endings
Expand All @@ -113,7 +114,8 @@ def test_order_transactions_fails_with_cyclic_tx(
create_tx = bdb_driver.transactions.prepare(
operation='CREATE',
signers=alice_keypair['public_key'])
transfer_to_bob_tx = make_transfer_tx(bdb_driver, input_tx=create_tx,
fulfill_tx = bdb_driver.transactions.fulfill(create_tx, alice_keypair['private_key'])
transfer_to_bob_tx = make_transfer_tx(bdb_driver, input_tx=fulfill_tx,
recipients=bob_keypair['public_key'])
transfer_to_carly_tx = make_transfer_tx(
bdb_driver, input_tx=transfer_to_bob_tx, recipients=carly_keypair['public_key'])
Expand Down Expand Up @@ -143,8 +145,9 @@ def test_order_transactions_fails_with_multiple_starts(
create_tx_bob = bdb_driver.transactions.prepare(
operation='CREATE',
signers=bob_keypair['public_key'])
fulfill_tx_alice = bdb_driver.transactions.fulfill(create_tx_alice, alice_keypair['private_key'])
transfer_to_carly_tx = make_transfer_tx(
bdb_driver, input_tx=create_tx_alice,
bdb_driver, input_tx=fulfill_tx_alice,
recipients=carly_keypair['public_key'])

with raises(ValueError):
Expand All @@ -163,7 +166,8 @@ def test_order_transactions_fails_with_missing_tx(bdb_driver, alice_keypair,
create_tx = bdb_driver.transactions.prepare(
operation='CREATE',
signers=alice_keypair['public_key'])
transfer_to_bob_tx = make_transfer_tx(bdb_driver, input_tx=create_tx,
fulfill_tx = bdb_driver.transactions.fulfill(create_tx, alice_keypair['private_key'])
transfer_to_bob_tx = make_transfer_tx(bdb_driver, input_tx=fulfill_tx,
recipients=bob_keypair['public_key'])
transfer_to_carly_tx = make_transfer_tx(
bdb_driver, input_tx=transfer_to_bob_tx, recipients=carly_keypair['public_key'])
Expand Down
4 changes: 2 additions & 2 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

def poll_bdb_transaction_valid(driver, tx_id):
return poll_result(
lambda: driver.transactions.status(tx_id),
lambda result: result.get('status') == 'valid')
lambda: driver.blocks.get(txid=tx_id),
lambda result: result is not None )


def poll_bdb_transaction(driver, tx_id):
Expand Down