def test_merkle_branch(self): left = sha256("test") right = sha256("test2") branch = MerkleBranch(left, right) ans = '694299f8eb01a328732fb21f4163fbfaa8f60d5662f04f52ad33bec63953ec7f' self.assertEqual(branch.get_parent(), ans)
def test_data_block_json(self): block = DataBlock(1) block.add_hash(sha256("test")) block.add_hash(sha256("test2")) block.add_hash(sha256("test3")) json_data = block.to_json() self.assertEqual(json_data['block_num'], 1) self.assertFalse(json_data['closed']) self.assertEqual(json_data['merkle_root'], None) self.assertEqual(json_data['tx_id'], None) self.assertEqual(len(json_data['leaves']), 3) # close block and try again block.close() json_data = block.to_json() ans = 'd49e815a91a26d399f8c2fba429e6ef7e472e54b6eb1e04341d207eee219f6c0' self.assertEqual(json_data['block_num'], 1) self.assertTrue(json_data['closed']) self.assertEqual(json_data['merkle_root'], ans) self.assertEqual(json_data['tx_id'], None) self.assertEqual(len(json_data['leaves']), 3)
def test_merkle_proof_simple_false(self): left = sha256("test") right = sha256("test2") branch = MerkleBranch(left, right) target = sha256("notinproof") proof = MerkleProof() proof.add(branch) self.assertFalse(proof.is_valid(target))
def test_merkle_proof_simple_true(self): left = sha256("test") right = sha256("test2") branch = MerkleBranch(left, right) target = left proof = MerkleProof() proof.add(branch) self.assertTrue(proof.is_valid(target))
def test_merkle_proof_simple_true(self): tree = MerkleTree() tree.add_content("test") tree.add_content("test2") left = sha256("test") right = sha256("test2") branch = MerkleBranch(left, right) target = left proof = MerkleProof(target, tree) proof.add(branch) self.assertTrue(proof.is_valid())
def test_create_data_block(self): block = DataBlock(1) block.add_hash(sha256("test")) block.close() ans = '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08' self.assertEqual(block.merkle_root(), ans)
def test_proof_false2(self): tree = MerkleTree() tree.add_content("test") tree.add_content("test2") tree.add_content("test3") proof = tree.merkle_proof(sha256("test4")) self.assertFalse(proof.is_valid())
import time import random import urllib.request from hashserv.MerkleTree import sha256 url = "http://localhost:5000/api/submit/{0}" delay = 0.5 loop = True try: print("Will insert data until you CTRL+C...") while loop: ahash = sha256(str(random.random())) time.sleep(delay) urllib.request.urlopen(url.format(ahash)) print(ahash) except KeyboardInterrupt: loop = False print("Have a nice day!")
def test_proof_single_true(self): tree = MerkleTree() tree.add_content("test") proof = tree.merkle_proof(sha256("test")) self.assertTrue(proof.is_valid())