Exemplo n.º 1
0
def create_chain_from_dump(chain_dump):
    gen_blockchain = BlockChain()
    gen_blockchain.build_genesis()
    for idx, block_data in enumerate(chain_dump):
        if idx == 0:
            continue  # skip genesis block
        block = Block(block_data["index"], block_data["transactions"],
                      block_data["timestamp"], block_data["previous_hash"],
                      block_data["nonce"])
        proof = block_data['hash']
        added = gen_blockchain.add_block(block, proof)
        if not added:
            raise Exception("The chain dump is tampered")

    return gen_blockchain
Exemplo n.º 2
0
from flask import Flask, request
import requests
import json
import time

from blockchain import BlockChain
from block import Block

# Initialize flask aplication
app = Flask(__name__)
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True

#Initialize a blockchain object
blockchain = BlockChain()
blockchain.build_genesis()

# User ID
# TODO add account handling
userID = "miner1337"

# Contains the host addresses of other participating members of the network
peers = set()


@app.route('/new_transaction', methods=['POST'])
def new_transaction():
    tx_data = request.get_json()
    required_fields = ["sender", "recipient", "amount"]

    for field in required_fields:
        if not tx_data.get(field):