Ejemplo n.º 1
0
 def test_difficulty(self):
     transaction = self.create_test_transaction()
     genesis = Block.genesis()
     block1 = Block.mine(genesis, [
         transaction,
     ])
     # Genesis has old timestamp, so this shouldn't fail
     self.assertTrue(block1.hash.startswith('0' * MINE_DIFFICULTY))
Ejemplo n.º 2
0
 def test_mine(self):
     genesis = Block.genesis()
     transaction = self.create_test_transaction()
     block = Block.mine(genesis, [transaction])
     self.assertEqual(block.data[0].outputs[0].amount, self.amount)
     self.assertEqual(block.data[0].outputs[0].address, self.address)
     self.assertEqual(block.data[0].input.amount, self.amount)
     self.assertEqual(block.data[0].input.address, self.address)
     self.assertEqual(genesis.hash, block.prev_hash)
Ejemplo n.º 3
0
 def test_compare_and_hash(self):
     genesis = Block.genesis()
     transaction = self.create_test_transaction()
     block1 = Block.mine(genesis, [
         transaction,
     ])
     block2 = Block(block1.timestamp, block1.prev_hash, block1.hash,
                    block1.nonce, block1.difficulty, block1.data)
     self.assertEqual(block1, block2)
     self.assertEqual(hash(block1), hash(block2))
     transaction = self.create_test_transaction()
     block3 = Block.mine(genesis, [
         transaction,
     ])
     self.assertNotEqual(block1, block3)
     self.assertNotEqual(hash(block1), hash(block3))
Ejemplo n.º 4
0
 def _test_generate_genesis_block(self):
     pb = Block(
         ChainUtil.generate_timestamp(), None,
         '0000000000000000000000000000000000000000000000000000000000000000',
         0, 3, [])
     address = ('000000000000000000000000000000000000000000000000000'
                '0000000000000556e757365642067656e6573697320626c6f63'
                '6b2061646472657373202d2031312f30332f323031382032333'
                'a3036204553540000000000')
     signature = '47656e6573697320626c6f636b2063726561746564206f6e2031312f30332f32303138'
     timestamp = 1541319432152
     transaction = Transaction(
         None,
         TransactionInput(timestamp, amount, address, signature),
         [TransactionOutput(amount, address)],
     )
     block = Block.mine(pb, [transaction])
Ejemplo n.º 5
0
    def test_difficulty_level(self):
        transaction = self.create_test_transaction()
        genesis = Block.genesis()
        block = Block.mine(genesis, [
            transaction,
        ])
        block1 = Block.mine(block, [
            transaction,
        ])

        timestamp = block1.timestamp + ChainUtil.to_milliseconds(hours=1)
        difficulty = Block.calculate_difficulty(block1, timestamp)
        self.assertNotEqual(difficulty, block1.difficulty)
        self.assertTrue(difficulty < block1.difficulty)

        timestamp = block1.timestamp - ChainUtil.to_milliseconds(hours=1)
        difficulty = Block.calculate_difficulty(block1, timestamp)
        self.assertNotEqual(difficulty, block1.difficulty)
        self.assertTrue(difficulty > block1.difficulty)
Ejemplo n.º 6
0
    def is_valid(self, block_chain):
        # Compare existing chain with new chain
        for i in range(0, len(self.chain)):
            if (self.chain[i].hash != block_chain.chain[i].hash or
                    self.chain[i].prev_hash != block_chain.chain[i].prev_hash):
                return False

        # Check additional blocks of the new chain
        for j in range(i, len(block_chain.chain)):
            prev_block = block_chain.chain[i - 1]
            block = block_chain.chain[i]
            if prev_block.hash != block.prev_hash or Block.create_block_hash(
                    block) != block.hash:
                return False
            if not block.is_reward_transaction_valid(j):
                logger.error('Invalid reward transaction for block {}'.format(
                    block.hash))
                return False

        return True
Ejemplo n.º 7
0
 def __init__(self, chain=None):
     self.chain = chain if chain != None else [Block.genesis()]
Ejemplo n.º 8
0
 def append_block(self, data):
     previous_block = self.chain[-1]
     block = Block.mine(previous_block, data)
     self.chain.append(block)
     return block