mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
parent
044a052644
commit
9d38d3d405
@ -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'))
|
||||
|
||||
|
@ -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']
|
||||
|
@ -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([]) == ''
|
Loading…
x
Reference in New Issue
Block a user