22
33from backend .blockchain .blockchain import Blockchain
44from backend .blockchain .block import GENESIS_DATA
5+ from backend .wallet .wallet import Wallet
6+ from backend .wallet .transaction import Transaction
57
68def test_blockchain_instance ():
79 blockchain = Blockchain ()
@@ -18,7 +20,7 @@ def test_add_block():
1820def blockchain_three_blocks ():
1921 blockchain = Blockchain ()
2022 for i in range (3 ):
21- blockchain .add_block (i )
23+ blockchain .add_block ([ Transaction ( Wallet (), 'recipient' , i ). to_json ()] )
2224 return blockchain
2325
2426def test_is_valid_chain (blockchain_three_blocks ):
@@ -47,4 +49,42 @@ def test_replace_chain_bad_chain(blockchain_three_blocks):
4749 blockchain_three_blocks .chain [1 ].hash = 'evil_hash'
4850
4951 with pytest .raises (Exception , match = 'The incoming chain is invalid' ):
50- blockchain .replace_chain (blockchain_three_blocks .chain )
52+ blockchain .replace_chain (blockchain_three_blocks .chain )
53+
54+ def test_valid_transaction_chain (blockchain_three_blocks ):
55+ Blockchain .is_valid_transaction_chain (blockchain_three_blocks .chain )
56+
57+ def test_is_valid_transaction_chain_duplicate_transactions (blockchain_three_blocks ):
58+ transaction = Transaction (Wallet (), 'recipient' , 1 ).to_json ()
59+ blockchain_three_blocks .add_block ([transaction , transaction ])
60+
61+ with pytest .raises (Exception , match = 'is not unique' ):
62+ Blockchain .is_valid_transaction_chain (blockchain_three_blocks .chain )
63+
64+ def test_is_valid_transaction_chain_multiple_rewards (blockchain_three_blocks ):
65+ reward_1 = Transaction .reward_transaction (Wallet ()).to_json ()
66+ reward_2 = Transaction .reward_transaction (Wallet ()).to_json ()
67+ blockchain_three_blocks .add_block ([reward_1 , reward_2 ])
68+
69+ with pytest .raises (Exception , match = 'one mining reward per block' ):
70+ Blockchain .is_valid_transaction_chain (blockchain_three_blocks .chain )
71+
72+ def test_is_valid_transaction_chain_bad_transaction (blockchain_three_blocks ):
73+ bad_transaction = Transaction (Wallet (), 'recipient' , 1 )
74+ bad_transaction .input ['signature' ] = Wallet ().sign (bad_transaction .output )
75+ blockchain_three_blocks .add_block ([bad_transaction .to_json ()])
76+
77+ with pytest .raises (Exception ):
78+ Blockchain .is_valid_transaction_chain (blockchain_three_blocks .chain )
79+
80+ def test_is_valid_transaction_chain_bad_historic_balance (blockchain_three_blocks ):
81+ wallet = Wallet ()
82+ bad_transaction = Transaction (wallet , 'recipient' , 1 )
83+ bad_transaction .output [wallet .address ] = 9000
84+ bad_transaction .input ['amount' ] = 9001
85+ bad_transaction .input ['signature' ] = wallet .sign (bad_transaction .output )
86+
87+ blockchain_three_blocks .add_block ([bad_transaction .to_json ()])
88+
89+ with pytest .raises (Exception , match = 'has an invalid input amount' ):
90+ Blockchain .is_valid_transaction_chain (blockchain_three_blocks .chain )
0 commit comments