Пример #1
0
def create_wrong_blockchain_100():
    DNI = 19
    RSA = rsa_key()
    num_blocks = 100

    now = time()

    transactions = map(
        lambda i: transaction(
            int(hashlib.sha256(f"Transaction #{i}".encode()).hexdigest(), 16),
            RSA), range(100))
    blockChain = block_chain(next(transactions))

    for _ in range(1, DNI):
        blockChain.add_block(next(transactions))
        print(f'New block from transaction #{_ + 1} added!')
    print(f"Pre-verification: {blockChain.verify()}")
    for _ in range(DNI, num_blocks):
        print(f'Wrong block from transaction #{_ + 1} added!')
        blockChain.add_wrong_block(next(transactions))

    with open(f'100_blocks_{DNI}_wrong.pickle', 'wb') as f:
        pickle.dump(blockChain, f)

    print(f"Verification: {blockChain.verify()}\nTime elapsed: {time() - now}")
Пример #2
0
def fitxer_100_blocs_valids_fins_XX_DNI():
    """
	Un fitxer amb una cadena de 100 blocs que només sigui vàlida fins al bloc XX on XX són les dues darreres
	xifres del vostre DNI.
	"""
    XX_DNI = 81  # ara mateix NO INCLÒS EN ELS VÀLIDS
    rsa_0 = blockchain.rsa_key()
    size_message = 256
    m_0 = random.randint(2**(size_message // 2 - 1),
                         2**(size_message // 2) - 1)
    t_0 = blockchain.transaction(m_0, rsa_0)
    cadena_de_blocs = blockchain.block_chain(t_0)
    for i in range(1, XX_DNI):
        print(i)
        rsa = blockchain.rsa_key()
        m = random.randint(2**(size_message // 2 - 1),
                           2**(size_message // 2) - 1)
        t = blockchain.transaction(m, rsa)
        cadena_de_blocs.add_block(t)
    print(cadena_de_blocs.verify())
    for i in range(XX_DNI, 100):
        print(i)
        rsa = blockchain.rsa_key()
        m = random.randint(2**(size_message // 2 - 1),
                           2**(size_message // 2) - 1)
        t = blockchain.transaction(m, rsa)
        t.signature = random.randint(
            0, sys.maxsize
        )  # fem que sigui invàlida la transacció, també podríem alterar el hash etc
        # Nota: en tindríem prou amb alterar-ne un de sol (el 81) perquè els següents també fossin invàlids, es trenca la cadena
        cadena_de_blocs.add_block(t)
    print(cadena_de_blocs.verify())
    fitxer_de_sortida = '81_blocks_valids.block'
    with open(fitxer_de_sortida, 'wb') as file:
        pickle.dump(cadena_de_blocs, file)
Пример #3
0
def fitxer_100_blocs_valids():
	"""Un fitxer amb una cadena vàlida de 100 blocs"""
	rsa_0 = blockchain.rsa_key()
	m_0 = random.randint(0,sys.maxsize)
	t_0 = blockchain.transaction(m_0,rsa_0)
	cadena_de_blocs = blockchain.block_chain(t_0)
	for i in range (1,100):
		print(i)
		rsa = blockchain.rsa_key()
		m = random.randint(0,sys.maxsize)
		t = blockchain.transaction(m,rsa)
		cadena_de_blocs.add_block(t)
	print(cadena_de_blocs.verify())
	fitxer_de_sortida = '100_blocks_valids.block'
	with open(fitxer_de_sortida, 'wb') as file:
		pickle.dump(cadena_de_blocs, file)
Пример #4
0
def main(mode, data, height):
    """
    client implementation of block chain

    Argument : [AddBlock, PrintBlock, PrintChain]

        AddBlock : Mine a new block to the longest chain

        PrintBlock: Print the block of specify height

        PrintChain: Print the whole block chain
    """
    bc = block_chain()
    if mode == 'AddBlock':
        if data == None:
            sys.exit("Data not specify! --data/-d to specify data")
        bc.addBlock(data)
    elif mode == 'PrintChain':
        bc.printChain()
    elif mode == 'PrintBlock':
        if height == None:
            sys.exit("height not specify! --heihgt/-h to specify height")
        bc.printBlock(height)
Пример #5
0
from flask import Flask, jsonify, request

from blockchain import block_chain

# Instantiate the Node
app = Flask(__name__)

# Generate a globally unique address for this node
node_identifier = str(uuid4()).replace('-', '')

# Instantiate the Blockchain
blockchain = block_chain()


@app.route('/mine', methods=['GET'])
def mine():
    # We run the proof of work algorithm to get the next proof...
    last_block = blockchain.last_block
    proof = blockchain.proof_of_work(last_block)

    # We must receive a reward for finding the proof.
    # The sender is "0" to signify that this node has mined a new coin.
    blockchain.new_transaction(
        sender="0",
        recipient=node_identifier,
        amount=1,
    )

    # Forge the new Block by adding it to the chain
    previous_hash = blockchain.hash(last_block)
    block = blockchain.new_block(proof, previous_hash)