Exemplo n.º 1
0
    def get_minable_block(self, reward_address):
        transactions = []
        latest_block = self.get_latest_block()
        new_block_id = latest_block.index + 1
        previous_hash = latest_block.current_hash
        fees = 0

        for pending_transaction in self.pending_transactions:
            if pending_transaction is None:
                break
            if pending_transaction.tx_hash in [
                    transaction.tx_hash for transaction in transactions
            ]:
                continue
            if self.find_duplicate_transactions(pending_transaction.tx_hash):
                continue
            if not pending_transaction.verify():
                continue
            transactions.append(pending_transaction)
            fees += pending_transaction.fee
            if len(transactions) >= self.MAX_TRANSACTIONS_PER_BLOCK:
                break

        timestamp = int(time.time())

        reward_transaction = Transaction('0', reward_address,
                                         self.get_reward(new_block_id) + fees,
                                         0, timestamp, '0')
        transactions.append(reward_transaction)

        return Block(new_block_id, transactions, previous_hash, timestamp)
Exemplo n.º 2
0
 def get_block_by_index(self, index):
     if index >= self._count_blocks():
         return None
     if self.mongo:
         if index < 0:
             index = self._count_blocks() + index
         block = self._blocks.find_one({'index': index}, {'_id': 0})
         if block is not None:
             block = Block.from_dict(block)
     else:
         block = self._blocks[index]
     return block
Exemplo n.º 3
0
def genesis_block():
    genesis_transaction_one = Transaction(
        '0',
        '032b72046d335b5318a672763338b08b9642225189ab3f0cba777622cfee0fc07b',
        10, 0, 0, '')
    genesis_transaction_two = Transaction(
        '0',
        '02f846677f65911f140a42af8fe7c1e5cbc7d148c44057ce49ee0cd0a72b21df4f',
        10, 0, 0, '')
    genesis_transactions = [genesis_transaction_one, genesis_transaction_two]
    return Block(
        0, genesis_transactions,
        '000000000000000000000000000000000000000000000000000000000000000000',
        0, 130898395)
Exemplo n.º 4
0
 def blocks(self) -> Iterator[Block]:
     if self.mongo:
         return (Block.from_dict(b) for b in self._mblocks())
     return (b for b in self._blocks)