示例#1
0
def mine():
    global blockchain
    if blockchain == None:
        blockchain = Blockchain(difficulty)
        blockchain.create_genesis_block()
        return "Blockchain created \n", 200
    else:
        blockchain.mine_block()
        return "Block mined\n", 200
示例#2
0
 def mine_call(self):
     # need to call the mining function
     global blockchain
     if not blockchain:
         blockchain = Blockchain(difficulty)
         blockchain.create_genesis_block(wallet)
     else:
         blockchain.mine_block(wallet)
     print(blockchain.tokens)
     self.define_block(blockchain.blocks[-1])
     data = blockchain.to_dict()
     dataJson = json.dumps(data).encode()
     socket.send_multipart([b'chain', dataJson])
示例#3
0
def create_chain_from_dump(chain_dump):
    generated_blockchain = Blockchain()
    generated_blockchain.create_genesis_block()
    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 = generated_blockchain.add_block(block, proof)
        if not added:
            raise Exception("The chain dump is tampered!!")
    return generated_blockchain
示例#4
0
    def mine_call(self):
        global blockchain
        if not blockchain:
            blockchain = Blockchain(difficulty)
            blockchain.create_genesis_block(wallet)
        else:
            blockchain.mine_block(wallet)

        self.define_block(blockchain.chain[-1])
        chain = blockchain.to_dict()
        chain_data = json.dumps({
            "length": len(chain),
            "chain": chain
        }).encode()

        socket.send_multipart([b'chain', chain_data])
示例#5
0
from chain import Blockchain
from key import BitcoinAccount

wallet = BitcoinAccount()
address = wallet.to_address()
difficulty = 4

blockchain = Blockchain(difficulty)
blockchain.create_genesis_block()

print("blockchain: ")
print(blockchain.to_dict())

first_block = blockchain.blocks_list[-1]

print("First block: ")
print(first_block)

blockchain.add_transaction(address, "colas", 10)
blockchain.add_transaction(address, "salim", 30)
blockchain.mine_block()

print("blockchain: ")
print(blockchain.to_dict())
second_block = blockchain.blocks_list[-1]

print("Second block: ")
print(second_block)
示例#6
0
def create_chain():
    global blockchain
    blockchain = Blockchain(difficulty, address)
    blockchain.create_genesis_block()
    return "Success\n", 200