diff --git a/bigchaindb/tendermint/utils.py b/bigchaindb/tendermint/utils.py index 28c4bb3c..3deb8b2c 100644 --- a/bigchaindb/tendermint/utils.py +++ b/bigchaindb/tendermint/utils.py @@ -4,13 +4,13 @@ import sha3 def encode_transaction(value): - """Encode a transaction to Base64.""" + """Encode a transaction (dict) to Base64.""" return base64.b64encode(json.dumps(value).encode('utf8')).decode('utf8') def decode_transaction(raw): - """Decode a transaction from Base64 to a dict.""" + """Decode a transaction from bytes to a dict.""" return json.loads(raw.decode('utf8')) diff --git a/tests/tendermint/test_lib.py b/tests/tendermint/test_lib.py index 714f0b2d..6cf28719 100644 --- a/tests/tendermint/test_lib.py +++ b/tests/tendermint/test_lib.py @@ -3,6 +3,7 @@ import os import pytest from bigchaindb import backend +from unittest.mock import patch pytestmark = pytest.mark.tendermint @@ -46,3 +47,39 @@ def test_get_latest_block(b): block = b.get_latest_block() assert block['height'] == 9 + + +def test_validation_error(b): + from bigchaindb.models import Transaction + from bigchaindb.common.crypto import generate_key_pair + + alice = generate_key_pair() + tx = Transaction.create([alice.public_key], + [([alice.public_key], 1)], + asset=None)\ + .sign([alice.private_key]).to_dict() + + tx['metadata'] = '' + assert not b.validate_transaction(tx) + + +@patch('requests.post') +def test_write_and_post_transaction(mock_post, b): + from bigchaindb.models import Transaction + from bigchaindb.common.crypto import generate_key_pair + from bigchaindb.tendermint.utils import encode_transaction + + alice = generate_key_pair() + tx = Transaction.create([alice.public_key], + [([alice.public_key], 1)], + asset=None)\ + .sign([alice.private_key]).to_dict() + + tx = b.validate_transaction(tx) + b.write_transaction(tx) + + assert mock_post.called + args, kwargs = mock_post.call_args + assert 'broadcast_tx_async' == kwargs['json']['method'] + encoded_tx = [encode_transaction(tx.to_dict())] + assert encoded_tx == kwargs['json']['params'] diff --git a/tests/tendermint/test_utils.py b/tests/tendermint/test_utils.py index e69de29b..ce91c173 100644 --- a/tests/tendermint/test_utils.py +++ b/tests/tendermint/test_utils.py @@ -0,0 +1,27 @@ +import base64 +import json + + +def test_encode_decode_transaction(b): + from bigchaindb.tendermint.utils import (encode_transaction, + decode_transaction) + + asset = { + 'value': 'key' + } + + encode_tx = encode_transaction(asset) + new_encode_tx = base64.b64encode(json.dumps(asset). + encode('utf8')).decode('utf8') + + assert encode_tx == new_encode_tx + + de64 = base64.b64decode(encode_tx) + assert asset == decode_transaction(de64) + + +def test_calculate_hash_no_key(b): + from bigchaindb.tendermint.utils import calculate_hash + + # pass an empty list + assert calculate_hash([]) == ''