Beispiel #1
0
 def test_block_hash(self):
     with open(TestBlockParsing.block_data_path + 'valid_block.txt',
               'r') as data:
         string = data.read()
         block = parse_block(string)
         block_hash = str(hashlib.sha512(string.encode()).hexdigest())
         self.assertEqual(block.block_hash, block_hash)
Beispiel #2
0
 def test(self):
     with open(TestBlockParsing.block_data_path + file, 'r') as data:
         string = data.read()
         block = parse_block(string)
         if result:
             self.assertTrue(block)
         else:
             self.assertFalse(block)
Beispiel #3
0
 def test_parsing_data(self):
     with open(TestBlockParsing.block_data_path + 'valid_block.txt',
               'r') as data:
         block = parse_block(data.read())
         self.assertEqual(block.nonce,
                          280815619281315998298257152433917011173307790385)
         self.assertEqual(block.parent_hash,
                          "000000000000000000000000000000000000")
         self.assertEqual(block.create_time, 1519773893.9249759)
         self.assertEqual(
             block.miner_key_hash,
             "03586f81a493d12fc4c71a01d648f83ac5d544e7168f96dcc32fa6bd4d54992e"
         )
         self.assertEqual(len(block.posts), MSGS_PER_BLOCK)
Beispiel #4
0
    def test_decrypt_public_messages(self):
        with open(TestBlockParsing.block_data_path + 'valid_block.txt',
                  'r') as data:
            string = data.read()
            block = parse_block(string)
            key_manager = get_test_key_manager()

            msg_list = [
                'Project Gutenberg’s The Complete Works of William Shakespeare, by William',
                'Shakespeare',
                'This eBook is for the use of anyone anywhere in the United States and',
                'most other parts of the world at no cost and with almost no restrictions',
                'whatsoever.  You may copy it, give it away or re-use it under the terms',
                'of the Project Gutenberg License included with this eBook or online at',
                'www.gutenberg.org.  If you are not located in the United States, you’ll',
                'have to check the laws of the country where you are located before using',
                'this ebook.',
                'See at the end of this file: * CONTENT NOTE (added in 2017) *'
            ]

            self.assertEqual(msg_list, block.decrypt_messages(key_manager))
Beispiel #5
0
    def _add_block_str(self, block_str, write_to_ledger=True, mined_ourselves=False):
        block = parse_block(block_str)
        if block is None:
            self.log.debug(RED + "%s - Block ill-formed" + NC, block.miner_key_hash[:5])
            self.rejects[block.block_hash] = "ill-formed"
            return False

        if not block.verify_pow():
            self.log.debug(RED + "%s - Block invalid POW" + NC + "\n\t%s", block.miner_key_hash[:5], repr(block))
            self.rejects[block.block_hash] = "invalid POW"
            return False

        if block.parent_hash not in self.blocks and not block.is_root():
            self.log.debug("%s - Block has non-existent parent", block.miner_key_hash[:5])
            if block.parent_hash in self.rejects:
                self.log.warning("\t" + RED + "Block parent %s in rejected: %s" + NC, block.parent_hash, self.rejects[block.parent_hash])
            self.rejects[block.block_hash] = "non-existent parent"
            return False

        if block.block_hash in self.blocks:
            self.log.debug(RED + "%s - Block is a duplicate" + NC, block.miner_key_hash[:5])
            return False

        return self._add_block(block, write_to_ledger, mined_ourselves)
Beispiel #6
0
 def test_not_parent_block(self):
     with open(TestBlockParsing.block_data_path + 'non_genesis_block.txt',
               'r') as data:
         block = parse_block(data.read())
         self.assertFalse(block.is_root())
Beispiel #7
0
 def test_parent_block(self):
     with open(TestBlockParsing.block_data_path + 'valid_block.txt',
               'r') as data:
         block = parse_block(data.read())
         self.assertTrue(block.is_root())
Beispiel #8
0
 def test_wrong_pow(self):
     with open(TestBlockParsing.block_data_path + 'invalid_nonce.txt',
               'r') as data:
         block = parse_block(data.read())
         self.assertFalse(block.verify_pow())
Beispiel #9
0
 def test_correct_pow(self):
     with open(TestBlockParsing.block_data_path + 'valid_block.txt',
               'r') as data:
         block = parse_block(data.read())
         self.assertTrue(block.verify_pow())
Beispiel #10
0
 def test_to_string(self):
     with open(TestBlockParsing.block_data_path + 'valid_block.txt',
               'r') as data:
         string = data.read()
         self.assertEqual(repr(parse_block(string)), string)