def get_genesis(): #chain_test = blockchain.chain blockchain.create_genesis_block() chain_test = blockchain.last_block b_data = json.dumps(chain_test.__dict__, sort_keys=True) return b_data
def build_model_chain(list_of_files): # TODO: need to add the capability of build chain with list of list. Say a step can contains multiple files. recursively update them for that step. ''' :param list_of_files: list of files from genesis to the end block :return: a blockchain object ''' from blockchain import file_as_bytes from blockchain import create_genesis_block import datetime if len(list_of_files) == 0: raise ValueError("The list of files must not be empty") else: for i in range(len(list_of_files)): if i == 0: index = i timestamp = datetime.datetime.now() data = file_as_bytes(open(list_of_files[i],'rb')) # it will change image file (png,jpeg etc) data with rb on windows model_chain = create_genesis_block(0, timestamp, data) model_chain.print() else: index = i data = file_as_bytes(open(list_of_files[i],'rb')) timestamp = datetime.datetime.now() model_chain = create_next_block(data, model_chain) model_chain.print() return model_chain
def test_clean_block(self): chain = [blockchain.create_genesis_block()] for _ in range(1): new_block = blockchain.create_block(chain[-1], "Hello, this is a block") chain.append(new_block) self.assertTrue(blockchain.verify_chain(chain))
def test_dirty_block(self): chain = [blockchain.create_genesis_block()] for _ in range(10): new_block = blockchain.create_block(chain[-1], "Hello, this is a block") chain.append(new_block) chain[3].data = "Tampered data" self.assertFalse(blockchain.verify_chain(chain))
def __init__(self, chain=None): self.chain_lock = threading.Lock() self.trans_lock = threading.Lock() if chain is None: self.chain = [create_genesis_block(proof_of_work=1)] else: self.chain = chain.copy() self.forks = {} self.transactions = []
def test_dirty_new_block(self): chain = [blockchain.create_genesis_block()] for _ in range(10): new_block = blockchain.create_block(chain[-1], "Hello, this is a block") chain.append(new_block) chain[3].data = "Hello" new_block = blockchain.create_block(chain[-1], "Hello, this is a bad new block") self.assertFalse(blockchain.verify_new_block(chain, new_block))
def create_blockchain(): # Our block chain is a list blockchain = [create_genesis_block()] previous_block = blockchain[0] # Total blocks in chain number_of_blocks = 20 for each_block in range(0, number_of_blocks): next_block = new_block(previous_block) blockchain.append(next_block) previous_block = next_block # Print details print("New block: Block {} added!! ".format(previous_block.index)) print("Block data: {}".format(previous_block.data)) print("Block hash: {}".format(previous_block.hash))
import blockchain import pickle #Server ID SERVER_ID = "Server0001" #function to save block into file def saveObject(obj, filename): with open(filename, 'wb') as output: pickle.dump(obj, output, pickle.HIGHEST_PROTOCOL) genesisBlock = blockchain.create_genesis_block(SERVER_ID) saveObject(genesisBlock, 'genesisBlock.pk1')
import blockchain as bc blockchain = [bc.create_genesis_block()] previous_block = blockchain[0] num_of_blocks = 30 for i in range(0, num_of_blocks): current_block = bc.new_block(previous_block) blockchain.append(current_block) previous_block = current_block print("Block " + str(current_block.counter) + " has been added\n") print("Transactoin info:") print("Sender - " + current_block.data.sender) print("Recipient - " + current_block.data.recipient) print("Amount of transaction - " + str(current_block.data.amount)) print("\nHash - " + current_block.hash + "\n\n\n")
from django.shortcuts import render, redirect from django.http import HttpResponse from django.contrib.auth.models import User from django.contrib.auth import authenticate, login, logout from django.contrib import messages from .models import * from django.db.models import Q from django.http import JsonResponse from blockchain import Block from blockchain import create_genesis_block, next_block wishlist1 = [] ledger = [create_genesis_block()] #json_list = [] def postAd(request): if request.method == 'GET': return render(request, 'post.html') elif request.method == 'POST': post = request.POST file = request.FILES ad = Advertisment() ad.user = request.user ad.title = post.get('title') ad.category = post.get('category') ad.description = post.get('description') ad.name = post.get('name') ad.price = post.get('price') ad.mobileNumber = post.get('number')
index = i data = file_as_bytes(open(list_of_files[i],'rb')) timestamp = datetime.datetime.now() model_chain = create_next_block(data, model_chain) model_chain.print() return model_chain # unit test if __name__ == "__main__": from blockchain import create_genesis_block from blockchain import create_next_block from blockchain import compare_model_chains from blockchain import compare_nth_block blockChain = [create_genesis_block(0)] previous_block = blockChain[0] print(type(previous_block)) num_blocks = 10 for i in range(0, num_blocks): data = "Creating block" + str(i) block2add = create_next_block(data, last_block=previous_block) blockChain.append(block2add) previous_block = block2add print("Block %d has been added to the blockChain \n", block2add.index) print("Hash: %s\n", block2add.hash) last_block = blockChain[-1] print("Last block's index is %d"%last_block.index)
import blockchain import pickle import ast #layout of what a voting station would do #sample machine ID machine_id = '1234' block_to_add = None #Machine will receive genesis_block from server so that all machines have the same genesis block #In that case genesis_block = whatver server gives it genesis_block = blockchain.create_genesis_block() #machine will then create its own local blockchain utilitzing the genesis block given blockchain.create_new_chain(genesis_block) # Voter will vote and data will be packaged into proposed block proposed_data = [["Voter ID", "000001"], ["President", "Eric Schweitzer"], ["Senate", "Eric Schweitzer"], ["Mayor", "Eric Schweitzer"]] block_to_add = blockchain.next_block(machine_id, proposed_data) blockchain.chain.append(block_to_add) print("First Block") print("Machine ID: ", blockchain.chain[1].machine_id) print("Time: ", blockchain.chain[1].timestamp) print("Ballot: ", blockchain.chain[1].data) print("Current Hash: ", blockchain.chain[1].hash) print("Previous Hash: ", blockchain.chain[1].previous_hash, "\n")
import blockchain as bc import block if __name__ == "__main__": bc = bc.Blockchain() bc.create_genesis_block() bc.next_block(block.Data('a', 'b', 'c')) bc.next_block(block.Data('z', 'a', 'x')) # print(bc.get_last_block().hash) c = bc.next_block(block.Data('k', 'f', 'g')) print(bc.get_block('2').hash) print(bc.get_relate_blocks('a')) pass