-
Notifications
You must be signed in to change notification settings - Fork 52
/
blockcoin_tests.py
66 lines (53 loc) · 2 KB
/
blockcoin_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import pytest, time, uuid, ecdsa
import identities
from blockcoin import *
def test_blocks():
bank = Bank(id=0, private_key=identities.bank_private_key(0))
# Good block
block = Block(txns=[])
block.sign(bank.private_key)
bank.handle_block(block)
assert len(bank.blocks) == 1
# Wrong bank signs
block = Block(txns=[])
wrong_private_key = identities.bank_private_key(1000)
block.sign(wrong_private_key)
with pytest.raises(ecdsa.keys.BadSignatureError):
bank.handle_block(block)
def test_bad_tx():
bank = Bank(id=0, private_key=identities.bank_private_key(0))
tx = identities.airdrop_tx()
bank.airdrop(tx)
tx = prepare_simple_tx(
utxos=bank.fetch_utxos(identities.alice_public_key),
sender_private_key=identities.alice_private_key,
recipient_public_key=identities.bob_public_key,
amount=10,
)
# Put in a phony signature
tx.tx_ins[0].signature = identities.alice_private_key.sign(b"bad")
with pytest.raises(ecdsa.keys.BadSignatureError):
bank.handle_tx(tx)
def test_airdrop():
bank = Bank(id=0, private_key=identities.bank_private_key(0))
tx = identities.airdrop_tx()
bank.airdrop(tx)
assert 500_000 == bank.fetch_balance(identities.alice_public_key)
assert 500_000 == bank.fetch_balance(identities.bob_public_key)
def test_utxo():
bank = Bank(id=0, private_key=identities.bank_private_key(0))
tx = identities.airdrop_tx()
bank.airdrop(tx)
assert len(bank.blocks) == 1
# Alice sends 10 to Bob
tx = prepare_simple_tx(
utxos=bank.fetch_utxos(identities.alice_public_key),
sender_private_key=identities.alice_private_key,
recipient_public_key=identities.bob_public_key,
amount=10
)
block = Block(txns=[tx])
block.sign(identities.bank_private_key(1))
bank.handle_block(block)
assert 500_000 - 10 == bank.fetch_balance(identities.alice_public_key)
assert 500_000 + 10 == bank.fetch_balance(identities.bob_public_key)