Example #1
0
    def _try_to_mine(self):
        fee = float(BankSettings.objects.all()[0].fee)
        valid_transactions = []
        for tid, not_mined_transaction in self.shell.blockchain.not_mined_transactions.items():
            if (len(valid_transactions) < self.shell.block_size - 1 and
                    not_mined_transaction.is_valid(self.shell.blockchain.all_utxos,
                                                   self.shell.blockchain.minimum_transaction, fee)):
                valid_transactions.append(not_mined_transaction)
        if len(valid_transactions) < self.shell.block_size - 1:
            return
        block = Block(self.shell.blockchain.last_block_hash())

        value = float(BankSettings.objects.all()[0].reward) + fee * (self.shell.block_size - 1)
        coinbase = Transaction('', self.bank.wallet.get_keys()[0], value=value, inputs=[])
        block.add_transaction(transaction=coinbase,
                              all_utxos=self.shell.blockchain.all_utxos,
                              minimum_transaction=self.shell.blockchain.minimum_transaction,
                              fee=fee, should_check=True, is_coinbase=True)
        self.shell.blockchain.append_transaction(coinbase)
        for valid_transaction in valid_transactions:
            block.add_transaction(valid_transaction, self.shell.blockchain.all_utxos,
                                  self.shell.blockchain.minimum_transaction,
                                  fee=fee, should_check=True)
        block.mine(BankSettings.objects.all()[0].difficulty)
        print('block mined: {}.'.format(block.calculate_hash()))
        self.shell.blockchain.append_block(block)
Example #2
0
def generate_block():

    index = request.form['index']
    content = request.form['content']
    previous = request.form['previous']

    transaction = Transaction(satoshi.private_key, satoshi.public_key, str(content))
    block = Block(int(index), [transaction], previous, 0)

    block.timestamp = datetime.datetime(2009, 1, 10, 12, 00)
    transaction.timestamp = datetime.datetime(2009, 1, 10, 12, 00)

    response = {'hash': block.calculate_hash() }

    return jsonify(response), 200
Example #3
0
def generate_genesis_block():
    b = Block(index=0, prev_hash="", validator=nodes.nodes[0].address)
    b.calculate_hash()
    blockchain.add_block(b)