1
0
mirror of https://github.com/bigchaindb/bigchaindb.git synced 2024-06-17 18:13:22 +02:00
bigchaindb/tests/tendermint/test_core.py

145 lines
4.1 KiB
Python
Raw Normal View History

2017-11-11 02:26:50 +01:00
import json
import pytest
2017-11-11 02:26:50 +01:00
pytestmark = [pytest.mark.tendermint, pytest.mark.bdb]
2017-11-11 02:26:50 +01:00
def encode_tx_to_bytes(transaction):
return json.dumps(transaction.to_dict()).encode('utf8')
def test_check_tx__signed_create_is_ok(b):
from bigchaindb.tendermint import App
from bigchaindb.models import Transaction
from bigchaindb.common.crypto import generate_key_pair
alice = generate_key_pair()
bob = generate_key_pair()
tx = Transaction.create([alice.public_key],
[([bob.public_key], 1)])\
.sign([alice.private_key])
app = App(b)
2017-11-11 02:26:50 +01:00
result = app.check_tx(encode_tx_to_bytes(tx))
assert result.is_ok()
def test_check_tx__unsigned_create_is_error(b):
from bigchaindb.tendermint import App
from bigchaindb.models import Transaction
from bigchaindb.common.crypto import generate_key_pair
alice = generate_key_pair()
bob = generate_key_pair()
tx = Transaction.create([alice.public_key],
[([bob.public_key], 1)])
app = App(b)
2017-11-11 02:26:50 +01:00
result = app.check_tx(encode_tx_to_bytes(tx))
assert result.is_error()
@pytest.mark.bdb
def test_deliver_tx__valid_create_updates_db(b):
from bigchaindb.tendermint import App
from bigchaindb.models import Transaction
from bigchaindb.common.crypto import generate_key_pair
alice = generate_key_pair()
bob = generate_key_pair()
tx = Transaction.create([alice.public_key],
[([bob.public_key], 1)])\
.sign([alice.private_key])
app = App(b)
app.init_chain(['ignore'])
app.begin_block('ignore')
2017-11-11 02:26:50 +01:00
result = app.deliver_tx(encode_tx_to_bytes(tx))
assert result.is_ok()
app.end_block(99)
app.commit()
assert b.get_transaction(tx.id).id == tx.id
# unspent_outputs = b.get_unspent_outputs()
# unspent_output = next(unspent_outputs)
# expected_unspent_output = next(tx.unspent_outputs)._asdict()
# assert unspent_output == expected_unspent_output
# with pytest.raises(StopIteration):
# next(unspent_outputs)
def test_deliver_tx__double_spend_fails(b):
from bigchaindb.tendermint import App
from bigchaindb.models import Transaction
from bigchaindb.common.crypto import generate_key_pair
alice = generate_key_pair()
bob = generate_key_pair()
tx = Transaction.create([alice.public_key],
[([bob.public_key], 1)])\
.sign([alice.private_key])
app = App(b)
app.init_chain(['ignore'])
app.begin_block('ignore')
2017-11-11 02:26:50 +01:00
result = app.deliver_tx(encode_tx_to_bytes(tx))
assert result.is_ok()
app.end_block(99)
app.commit()
assert b.get_transaction(tx.id).id == tx.id
2017-11-11 02:26:50 +01:00
result = app.deliver_tx(encode_tx_to_bytes(tx))
assert result.is_error()
2017-11-16 17:14:21 +01:00
def test_deliver_transfer_tx__double_spend_fails(b):
from bigchaindb.tendermint import App
from bigchaindb.models import Transaction
from bigchaindb.common.crypto import generate_key_pair
2017-11-23 15:53:43 +01:00
app = App(b)
app.init_chain(['ignore'])
app.begin_block('ignore')
alice = generate_key_pair()
bob = generate_key_pair()
carly = generate_key_pair()
asset = {
2017-11-30 19:29:42 +01:00
'msg': 'live long and prosper'
}
tx = Transaction.create([alice.public_key],
[([alice.public_key], 1)],
2017-11-16 17:14:21 +01:00
asset=asset)\
.sign([alice.private_key])
result = app.deliver_tx(encode_tx_to_bytes(tx))
assert result.is_ok()
tx_transfer = Transaction.transfer(tx.to_inputs(),
2017-11-16 17:14:21 +01:00
[([bob.public_key], 1)],
asset_id=tx.id)\
.sign([alice.private_key])
result = app.deliver_tx(encode_tx_to_bytes(tx_transfer))
assert result.is_ok()
double_spend = Transaction.transfer(tx.to_inputs(),
2017-11-16 17:14:21 +01:00
[([carly.public_key], 1)],
asset_id=tx.id)\
.sign([alice.private_key])
result = app.deliver_tx(encode_tx_to_bytes(double_spend))
assert result.is_error()