def test_example_11(self): hex_hashes = [ "9745f7173ef14ee4155722d1cbf13304339fd00d900b759c6f9d58579b5765fb", "5573c8ede34936c29cdfdfe743f7f5fdfbd4f54ba0705259e62f39917065cb9b", "82a02ecbb6623b4274dfcab82b336dc017a27136e08521091e443e62582e8f05", "507ccae5ed9b340363a0e6d765af148be9cb1c8766ccc922f83e4ae681658308", "a7a4aec28e7162e1e9ef33dfa30f0bc0526e6cf4b11a576f6c5de58593898330", "bb6267664bd833fd9fc82582853ab144fece26b7a8a5bf328f8a059445b59add", "ea6d7ac1ee77fbacee58fc717b990c4fcccf1b19af43103c090f601677fd8836", "457743861de496c429912558a106b810b0507975a49773228aa788df40730d41", "7688029288efc9e9a0011c960a6ed9e5466581abf3e3a6c26ee317461add619a", "b1ae7f15836cb2286cdd4e2c37bf9bb7da0a2846d06867a429f654b2e7f383c9", "9b74f89fa3f93e71ff2c241f32945d877281a6a50a6bf94adac002980aafe5ab", "b3a92b5b255019bdaf754875633c2de9fec2ab03e6b8ce669d07cb5b18804638", "b5c0b915312b9bdaedd2b86aa2d0f8feffc73a2d37668fd9010179261e25e263", "c9d52c5cb1e557b92c84c52e7c4bfbce859408bedffc8a5560fd6e35e10b8800", "c555bc5fc3bc096df0a0c9532f07640bfb76bfe4fc1ace214b8b228a1297a4c2", "f9dbfafc3af3400954975da24eb325e326960a25b87fffe23eef3e7ed2fb610e", ] tree = MerkleTree(len(hex_hashes)) tree.nodes[4] = [bytes.fromhex(h) for h in hex_hashes] tree.nodes[3] = merkle_parent_level(tree.nodes[4]) tree.nodes[2] = merkle_parent_level(tree.nodes[3]) tree.nodes[1] = merkle_parent_level(tree.nodes[2]) tree.nodes[0] = merkle_parent_level(tree.nodes[1]) self.assertEqual( tree.nodes[0][0].hex(), '597c4bafe3832b17cbbabe56f878f4fc2ad0f6a402cee7fa851a9cb205f87ed1')
def test_merkle_parent_level1(self): hex_hashes = [ 'c117ea8ec828342f4dfb0ad6bd140e03a50720ece40169ee38bdc15d9eb64cf5', 'c131474164b412e3406696da1ee20ab0fc9bf41c8f05fa8ceea7a08d672d7cc5', 'f391da6ecfeed1814efae39e7fcb3838ae0b02c02ae7d0a5848a66947c0727b0', '3d238a92a94532b946c90e19c49351c763696cff3db400485b813aecb8a13181', '10092f2633be5f3ce349bf9ddbde36caa3dd10dfa0ec8106bce23acbff637dae', '7d37b3d54fa6a64869084bfd2e831309118b9e833610e6228adacdbd1b4ba161', '8118a77e542892fe15ae3fc771a4abfd2f5d5d5997544c3487ac36b5c85170fc', 'dff6879848c2c9b62fe652720b8df5272093acfaa45a43cdb3696fe2466a3877', 'b825c0745f46ac58f7d3759e6dc535a1fec7820377f24d4c2c6ad2cc55c0cb59', '95513952a04bd8992721e9b7e2937f1c04ba31e0469fbe615a78197f68f52b7c', '2e6d722e5e4dbdf2447ddecc9f7dabb8e299bae921c99ad5b0184cd9eb8e5908', ] tx_hashes = [unhexlify(x) for x in hex_hashes] want_hex_hashes = [ '8b30c5ba100f6f2e5ad1e2a742e5020491240f8eb514fe97c713c31718ad7ecd', '7f4e6f9e224e20fda0ae4c44114237f97cd35aca38d83081c9bfd41feb907800', 'ade48f2bbb57318cc79f3a8678febaa827599c509dce5940602e54c7733332e7', '68b3e2ab8182dfd646f13fdf01c335cf32476482d963f5cd94e934e6b3401069', '43e7274e77fbe8e5a42a8fb58f7decdb04d521f319f332d88e6b06f8e6c09e27', '1796cd3ca4fef00236e07b723d3ed88e1ac433acaaa21da64c4b33c946cf3d10', ] want_tx_hashes = [unhexlify(x) for x in want_hex_hashes] self.assertEqual(merkle_parent_level(tx_hashes), want_tx_hashes)
def calculate_merkle_tree(self): '''Calculate and store the entire Merkle Tree''' # store the result in self.merkle_tree, an array, 0 representing # the bottom level and 1 the parent level of level 0 and so on. # initialize self.merkle_tree to be an empty list self.merkle_tree = [] # reverse all the transaction hashes (self.tx_hashes) store as current level current_level = [h[::-1] for h in self.tx_hashes] # while there is more than 1 hash: while len(current_level) > 1: # store current level in self.merkle_tree self.merkle_tree.append(current_level) # Make current level Merkle Parent level current_level = merkle_parent_level(current_level)
def calculate_merkle_tree(self): '''Calculate and store the entire Merkle Tree''' # store the result in self.merkle_tree, an array, 0 representing # the bottom level and 1 the parent level of level 0 and so on. self.merkle_tree = [] if self.tx_hashes is None: raise RuntimeError( 'Transaction Hashes needed to calculate Merkle Tree') # reverse all the transaction hashes current = [x[::-1] for x in self.tx_hashes] # if there is more than 1 hash: while len(current) > 1: # store current level self.merkle_tree.append(current) # Make current level Merkle Parent level current = merkle_parent_level(current) # store root as the final level self.merkle_tree.append(current)
def test_exercise_8(self): hex_hashes = [ '42f6f52f17620653dcc909e58bb352e0bd4bd1381e2955d19c00959a22122b2e', '94c3af34b9667bf787e1c6a0a009201589755d01d02fe2877cc69b929d2418d4', '959428d7c48113cb9149d0566bde3d46e98cf028053c522b8fa8f735241aa953', 'a9f27b99d5d108dede755710d4a1ffa2c74af70b4ca71726fa57d68454e609a2', '62af110031e29de1efcad103b3ad4bec7bdcf6cb9c9f4afdd586981795516577', '766900590ece194667e9da2984018057512887110bf54fe0aa800157aec796ba', 'e8270fb475763bc8d855cfe45ed98060988c1bdcad2ffc8364f783c98999a208', '921b8cfd3e14bf41f028f0a3aa88c813d5039a2b1bceb12208535b0b43a5d09e', '15535864799652347cec66cba473f6d8291541238e58b2e03b046bc53cfe1321', '1c8af7c502971e67096456eac9cd5407aacf62190fc54188995666a30faf99f0', '3311f8acc57e8a3e9b68e2945fb4f53c07b0fa4668a7e5cda6255c21558c774d', ] hashes = [bytes.fromhex(x) for x in hex_hashes] current_level = hashes while len(current_level) > 1: current_level = merkle_parent_level(current_level) self.assertEqual( current_level[0].hex(), 'a67772634e542799333c6c98bc903e36b652918a8d8a9e069391c55b4276c8a1')
def test_example_8(self): hex_hashes = [ 'c117ea8ec828342f4dfb0ad6bd140e03a50720ece40169ee38bdc15d9eb64cf5', 'c131474164b412e3406696da1ee20ab0fc9bf41c8f05fa8ceea7a08d672d7cc5', 'f391da6ecfeed1814efae39e7fcb3838ae0b02c02ae7d0a5848a66947c0727b0', '3d238a92a94532b946c90e19c49351c763696cff3db400485b813aecb8a13181', '10092f2633be5f3ce349bf9ddbde36caa3dd10dfa0ec8106bce23acbff637dae', '7d37b3d54fa6a64869084bfd2e831309118b9e833610e6228adacdbd1b4ba161', '8118a77e542892fe15ae3fc771a4abfd2f5d5d5997544c3487ac36b5c85170fc', 'dff6879848c2c9b62fe652720b8df5272093acfaa45a43cdb3696fe2466a3877', 'b825c0745f46ac58f7d3759e6dc535a1fec7820377f24d4c2c6ad2cc55c0cb59', '95513952a04bd8992721e9b7e2937f1c04ba31e0469fbe615a78197f68f52b7c', '2e6d722e5e4dbdf2447ddecc9f7dabb8e299bae921c99ad5b0184cd9eb8e5908', 'b13a750047bc0bdceb2473e5fe488c2596d7a7124b4e716fdd29b046ef99bbf0', ] hashes = [bytes.fromhex(x) for x in hex_hashes] current_level = hashes while len(current_level) > 1: current_level = merkle_parent_level(current_level) self.assertEqual( current_level[0].hex(), 'acbcab8bcc1af95d8d563b77d24c3d19b18f1486383d75a5085c4e86c86beed6')
# In[11]: from helper import merkle_parent_level hex_hashes = [ "9745f7173ef14ee4155722d1cbf13304339fd00d900b759c6f9d58579b5765fb", "5573c8ede34936c29cdfdfe743f7f5fdfbd4f54ba0705259e62f39917065cb9b", "82a02ecbb6623b4274dfcab82b336dc017a27136e08521091e443e62582e8f05", "507ccae5ed9b340363a0e6d765af148be9cb1c8766ccc922f83e4ae681658308", "a7a4aec28e7162e1e9ef33dfa30f0bc0526e6cf4b11a576f6c5de58593898330", "bb6267664bd833fd9fc82582853ab144fece26b7a8a5bf328f8a059445b59add", "ea6d7ac1ee77fbacee58fc717b990c4fcccf1b19af43103c090f601677fd8836", "457743861de496c429912558a106b810b0507975a49773228aa788df40730d41", "7688029288efc9e9a0011c960a6ed9e5466581abf3e3a6c26ee317461add619a", "b1ae7f15836cb2286cdd4e2c37bf9bb7da0a2846d06867a429f654b2e7f383c9", "9b74f89fa3f93e71ff2c241f32945d877281a6a50a6bf94adac002980aafe5ab", "b3a92b5b255019bdaf754875633c2de9fec2ab03e6b8ce669d07cb5b18804638", "b5c0b915312b9bdaedd2b86aa2d0f8feffc73a2d37668fd9010179261e25e263", "c9d52c5cb1e557b92c84c52e7c4bfbce859408bedffc8a5560fd6e35e10b8800", "c555bc5fc3bc096df0a0c9532f07640bfb76bfe4fc1ace214b8b228a1297a4c2", "f9dbfafc3af3400954975da24eb325e326960a25b87fffe23eef3e7ed2fb610e", ] tree = MerkleTree(len(hex_hashes)) tree.nodes[4] = [bytes.fromhex(h) for h in hex_hashes] tree.nodes[3] = merkle_parent_level(tree.nodes[4]) tree.nodes[2] = merkle_parent_level(tree.nodes[3]) tree.nodes[1] = merkle_parent_level(tree.nodes[2]) tree.nodes[0] = merkle_parent_level(tree.nodes[1]) print(tree) # In[ ]: