Exemple #1
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)
Exemple #2
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)
Exemple #3
0
 def from_dict(block_dict):
     block = Block(
         index=block_dict['index'],
         transactions=[Transaction.from_dict(tx) for tx in block_dict['transactions']],
         previous_hash=block_dict['previous_hash'],
         timestamp=block_dict['timestamp'],
         nonce=int(block_dict['nonce'])
     )
     block.merkle_root = block_dict['merkle_root']
     block.current_hash = block_dict['current_hash']
     block.timestamp = block_dict['timestamp']
     return block
Exemple #4
0
 def pending_transactions(self) -> Iterator[Transaction]:
     if self.mongo:
         return (Transaction.from_dict(t)
                 for t in self._mpending_transactions())
     return (t for t in self.pending_transactions)
Exemple #5
0
 def create_transaction(self, destination, amount):
     transaction = Transaction(self.public_key, destination, amount)
     transaction.sign(self.private_key)
     return transaction