def test_blockchain_wrong(blockchain : Blockchain): blockchain.chain[-2].miner_pub_key = b"zzz" barray = BlockArray(blockchain) assert not barray.is_correct() assert not blockchain.substitute(barray) with pytest.raises(Exception) as excinfo: blockchain.add(blockchain.chain[0]) assert str(excinfo.value) == 'Blockchain invalid'
def test_change_blockchain(blockchain : Blockchain): block = transaction_to_block(transactions[4], 11, blockchain) blockArray = BlockArray(blockchain) blockArray.add(block) previous_len = len(blockchain) assert blockchain.substitute(blockArray) assert len(blockchain) == len(blockArray) assert len(blockchain) == previous_len + 1
def test_receive_blockchain(client, blockchain): blockArray = BlockArray(blockchain) blockchain_bytes = b64encode(pickle.dumps(blockArray)) res = client.post(url_for('receive_blockchain'), \ data=blockchain_bytes) assert res.status_code == 200 res = client.get('blockchain') blockchain = res.json assert len(blockchain['blocks']) == 3
def test_block_array_receive_blocks(blockchain : Blockchain): blockArray = BlockArray(blockchain.chain) assert len(blockArray.chain) == 3
def test_dont_change_blockchain(blockchain : Blockchain): blockArray = BlockArray(blockchain) previous_len = len(blockchain) assert blockchain.substitute(blockArray) assert len(blockchain) == previous_len
def test_blockchain_not_mined(blockchain : Blockchain): block = blockchain.chain.pop() new_blockchain = BlockArray(blockchain).to_blockchain() if not new_blockchain: return new_blockchain.difficult = 5 assert not new_blockchain.is_new_block_OK(block)
def test_blockchain_right(blockchain : Blockchain): assert BlockArray(blockchain).is_correct()
def test_blockchain_small(blockchain : Blockchain): small_blockchain = Blockchain() small_blockchain.copy(blockchain) small_blockchain.chain.pop() assert not blockchain.substitute(BlockArray(small_blockchain))
def send_blockchain(): for server in servers: blockArray = BlockArray(blockchain) blockchain_bytes = b64encode(pickle.dumps(blockArray)) requests.post(f'http://{server}/receiveblockchain', \ data=blockchain_bytes)