2018-08-16 12:31:32 +02:00
|
|
|
# Copyright BigchainDB GmbH and BigchainDB contributors
|
|
|
|
# SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
|
|
|
|
# Code is Apache-2.0 and docs are CC-BY-4.0
|
|
|
|
|
2018-08-06 11:37:43 +02:00
|
|
|
import codecs
|
|
|
|
|
2018-06-14 15:19:26 +02:00
|
|
|
import abci.types_pb2 as types
|
2017-11-15 15:41:23 +01:00
|
|
|
import json
|
2017-11-22 18:13:59 +01:00
|
|
|
import pytest
|
2018-06-14 15:19:26 +02:00
|
|
|
|
|
|
|
|
2017-11-22 17:08:26 +01:00
|
|
|
from abci.server import ProtocolHandler
|
2018-07-10 14:16:02 +02:00
|
|
|
from abci.encoding import read_messages
|
2018-06-14 15:19:26 +02:00
|
|
|
from io import BytesIO
|
2017-11-15 15:41:23 +01:00
|
|
|
|
|
|
|
|
2017-11-16 12:22:30 +01:00
|
|
|
@pytest.mark.bdb
|
2018-08-27 09:48:20 +02:00
|
|
|
def test_app(b, init_chain_request):
|
Refactor tendermint directory to project root (#2401)
* Problem: core.py contains an unused class, `Bigchain`
Solution: Remove core.py. Refactor BigchainDB Class to remove inheritance from Bigchain.
* Problem: core.py contains an unused class, `Bigchain`
Solution: Remove core.py. Refactor BigchainDB Class to remove inheritance from Bigchain.
* Fixed flake8 complaint about too many blank lines
* Attempting to fix Sphinx docs. This may result in some redundant commits, as I don't know what I'm doing, and I can't experiment without running the CI...
Sorry in advance!
* Attempting to fix Sphinx docs. This may result in some redundant commits, as I don't know what I'm doing, and I can't experiment without running the CI...
Sorry in advance!
* Updating from master changed BigchainDB.process_post_response to a private method, so I had to align with that.
* Fixed a couple stale references to bigchaindb.Bigchain in docstrings
* Missed a reference to `Bigchain` in a patch call...
* Problem: BigchainDB class should be part of project root
Solution: Removed the /tendermint directory and moved its contents to project root
* Problem: Flake8 complained that imports were not at the top of the file
Solution: Had to play around with the order of imports to avoid cyclic dependencies, but its working and style compliant now
* Problem: Stale reference to /tendermint directory in the index
Solution: Removed the references to /tendermint
* Problem: Flake8 complaining of unused import in __init__.py
Solution: The import is there so I can import App directly from bigchaindb, rather than from bigchaindb.core (which I think improves code readability. I added a # noqa to silence Flake8.
* Problem: Stale references to `bigchaindb.tendermint.BigchainDB` in the rst files cause Sphinx to fail
Solution: Updated the autodoc files to use `bigchaindb.BigchainDB` instead
* Problem: Stale reference to the `tendermint` directory in an @patch in a disabled test
Solution: Updated the @patch for completeness
* Problem: BigchainDB class should be part of project root
Solution: Removed the /tendermint directory and moved its contents to project root
* Problem: Flake8 complained that imports were not at the top of the file
Solution: Had to play around with the order of imports to avoid cyclic dependencies, but its working and style compliant now
* Problem: Stale reference to /tendermint directory in the index
Solution: Removed the references to /tendermint
* Problem: Flake8 complaining of unused import in __init__.py
Solution: The import is there so I can import App directly from bigchaindb, rather than from bigchaindb.core (which I think improves code readability. I added a # noqa to silence Flake8.
* Problem: Stale references to `bigchaindb.tendermint.BigchainDB` in the rst files cause Sphinx to fail
Solution: Updated the autodoc files to use `bigchaindb.BigchainDB` instead
* Problem: Stale reference to the `tendermint` directory in an @patch in a disabled test
Solution: Updated the @patch for completeness
2018-07-25 16:59:25 +02:00
|
|
|
from bigchaindb import App
|
|
|
|
from bigchaindb.tendermint_utils import calculate_hash
|
2017-11-15 15:41:23 +01:00
|
|
|
from bigchaindb.common.crypto import generate_key_pair
|
|
|
|
from bigchaindb.models import Transaction
|
|
|
|
|
|
|
|
app = App(b)
|
|
|
|
p = ProtocolHandler(app)
|
|
|
|
|
2018-07-10 14:16:02 +02:00
|
|
|
data = p.process('info', types.Request(info=types.RequestInfo(version='2')))
|
|
|
|
res = next(read_messages(BytesIO(data), types.Response))
|
2017-11-15 15:41:23 +01:00
|
|
|
assert res
|
|
|
|
assert res.info.last_block_app_hash == b''
|
|
|
|
assert res.info.last_block_height == 0
|
|
|
|
assert not b.get_latest_block()
|
|
|
|
|
2018-08-06 11:37:43 +02:00
|
|
|
p.process('init_chain', types.Request(init_chain=init_chain_request))
|
2017-11-15 15:41:23 +01:00
|
|
|
block0 = b.get_latest_block()
|
|
|
|
assert block0
|
|
|
|
assert block0['height'] == 0
|
2017-11-22 17:08:26 +01:00
|
|
|
assert block0['app_hash'] == ''
|
2017-11-15 15:41:23 +01:00
|
|
|
|
2018-08-06 11:37:43 +02:00
|
|
|
pk = codecs.encode(init_chain_request.validators[0].pub_key.data, 'base64').decode().strip('\n')
|
|
|
|
[validator] = b.get_validators(height=1)
|
2018-09-06 16:06:36 +02:00
|
|
|
assert validator['public_key']['value'] == pk
|
2018-08-06 11:37:43 +02:00
|
|
|
assert validator['voting_power'] == 10
|
|
|
|
|
2017-11-15 15:41:23 +01:00
|
|
|
alice = generate_key_pair()
|
|
|
|
bob = generate_key_pair()
|
|
|
|
tx = Transaction.create([alice.public_key],
|
|
|
|
[([bob.public_key], 1)])\
|
|
|
|
.sign([alice.private_key])
|
|
|
|
etxn = json.dumps(tx.to_dict()).encode('utf8')
|
|
|
|
|
2018-07-10 14:16:02 +02:00
|
|
|
r = types.Request(check_tx=types.RequestCheckTx(tx=etxn))
|
2017-11-15 15:41:23 +01:00
|
|
|
data = p.process('check_tx', r)
|
2018-07-10 14:16:02 +02:00
|
|
|
res = next(read_messages(BytesIO(data), types.Response))
|
2017-11-15 15:41:23 +01:00
|
|
|
assert res
|
|
|
|
assert res.check_tx.code == 0
|
|
|
|
|
|
|
|
r = types.Request()
|
|
|
|
r.begin_block.hash = b''
|
|
|
|
p.process('begin_block', r)
|
|
|
|
|
2018-07-10 14:16:02 +02:00
|
|
|
r = types.Request(deliver_tx=types.RequestDeliverTx(tx=etxn))
|
2017-11-15 15:41:23 +01:00
|
|
|
data = p.process('deliver_tx', r)
|
2018-07-10 14:16:02 +02:00
|
|
|
res = next(read_messages(BytesIO(data), types.Response))
|
2017-11-15 15:41:23 +01:00
|
|
|
assert res
|
|
|
|
assert res.deliver_tx.code == 0
|
|
|
|
|
|
|
|
new_block_txn_hash = calculate_hash([tx.id])
|
|
|
|
|
2018-07-10 14:16:02 +02:00
|
|
|
r = types.Request(end_block=types.RequestEndBlock(height=1))
|
2017-11-15 15:41:23 +01:00
|
|
|
data = p.process('end_block', r)
|
2018-07-10 14:16:02 +02:00
|
|
|
res = next(read_messages(BytesIO(data), types.Response))
|
2017-11-15 15:41:23 +01:00
|
|
|
assert res
|
2017-11-23 10:50:19 +01:00
|
|
|
assert 'end_block' == res.WhichOneof('value')
|
2017-11-15 15:41:23 +01:00
|
|
|
|
2017-11-22 17:08:26 +01:00
|
|
|
new_block_hash = calculate_hash([block0['app_hash'], new_block_txn_hash])
|
2017-11-15 15:41:23 +01:00
|
|
|
|
|
|
|
data = p.process('commit', None)
|
2018-07-10 14:16:02 +02:00
|
|
|
res = next(read_messages(BytesIO(data), types.Response))
|
2017-11-15 15:41:23 +01:00
|
|
|
assert res.commit.data == new_block_hash.encode('utf-8')
|
2018-02-02 14:40:24 +01:00
|
|
|
assert b.get_transaction(tx.id).id == tx.id
|
2017-11-15 15:41:23 +01:00
|
|
|
|
|
|
|
block0 = b.get_latest_block()
|
|
|
|
assert block0
|
|
|
|
assert block0['height'] == 1
|
2017-11-22 17:08:26 +01:00
|
|
|
assert block0['app_hash'] == new_block_hash
|
|
|
|
|
|
|
|
# empty block should not update height
|
|
|
|
r = types.Request()
|
|
|
|
r.begin_block.hash = new_block_hash.encode('utf-8')
|
|
|
|
p.process('begin_block', r)
|
|
|
|
|
|
|
|
r = types.Request()
|
|
|
|
r.end_block.height = 2
|
|
|
|
p.process('end_block', r)
|
|
|
|
|
|
|
|
data = p.process('commit', None)
|
2018-07-10 14:16:02 +02:00
|
|
|
res = next(read_messages(BytesIO(data), types.Response))
|
2017-11-22 17:08:26 +01:00
|
|
|
assert res.commit.data == new_block_hash.encode('utf-8')
|
|
|
|
|
|
|
|
block0 = b.get_latest_block()
|
|
|
|
assert block0
|
2018-09-06 11:27:52 +02:00
|
|
|
assert block0['height'] == 2
|
2017-11-22 17:08:26 +01:00
|
|
|
|
|
|
|
# when empty block is generated hash of previous block should be returned
|
|
|
|
assert block0['app_hash'] == new_block_hash
|
2018-04-24 16:17:49 +02:00
|
|
|
|
|
|
|
|
2018-04-26 11:09:38 +02:00
|
|
|
@pytest.mark.abci
|
|
|
|
def test_post_transaction_responses(tendermint_ws_url, b):
|
|
|
|
from bigchaindb.common.crypto import generate_key_pair
|
|
|
|
from bigchaindb.models import Transaction
|
|
|
|
|
|
|
|
alice = generate_key_pair()
|
|
|
|
bob = generate_key_pair()
|
|
|
|
tx = Transaction.create([alice.public_key],
|
|
|
|
[([alice.public_key], 1)],
|
|
|
|
asset=None)\
|
|
|
|
.sign([alice.private_key])
|
|
|
|
|
|
|
|
code, message = b.write_transaction(tx, 'broadcast_tx_commit')
|
|
|
|
assert code == 202
|
|
|
|
|
|
|
|
tx_transfer = Transaction.transfer(tx.to_inputs(),
|
|
|
|
[([bob.public_key], 1)],
|
|
|
|
asset_id=tx.id)\
|
|
|
|
.sign([alice.private_key])
|
|
|
|
|
|
|
|
code, message = b.write_transaction(tx_transfer, 'broadcast_tx_commit')
|
|
|
|
assert code == 202
|
|
|
|
|
2018-09-06 15:49:00 +02:00
|
|
|
carly = generate_key_pair()
|
|
|
|
double_spend = Transaction.transfer(
|
|
|
|
tx.to_inputs(),
|
|
|
|
[([carly.public_key], 1)],
|
|
|
|
asset_id=tx.id,
|
|
|
|
).sign([alice.private_key])
|
|
|
|
for mode in ('broadcast_tx_sync', 'broadcast_tx_commit'):
|
|
|
|
code, message = b.write_transaction(double_spend, mode)
|
|
|
|
assert code == 500
|
|
|
|
assert message == 'Transaction validation failed'
|