def __init__(self,ip,port,key=None): self.p2p=None self.ip=ip self.port=port self.transitionPool=TransactionPool() self.wallet=Wallet() self.blockChain=BlockChain() if key is not None: self.wallet.formKey(key)
def __init__(self, recipient): # initialise blockchain blockchain = BlockChain() print("***Mining phoenixChain coin about to start***") # print the blockchain data print(blockchain.chain) # obtain last block last_block = blockchain.last_block # latest hashed block last_proof_no = last_block.production_no # construct a blockchain production_no = blockchain.construct_proof_of_work(last_proof_no) # send blockchain.new_data( sender="0", # it implies that this node has created a new block recipient=str(recipient), # send coins! quantity= 1, # creating a new block (or identifying the proof number) is awarded with 1 ) # stores the latest blocks created last_hash = last_block.production_no # stores the the amount produced and the latest hashed block in the new block block = blockchain.construct_block(production_no, last_hash) print("***Mining phoenixChain coin has been successful***") print(blockchain.chain);
def mine(): # Forge the new Block block = blockchain.new_block() response = { 'message': "New Block Forged", 'index': block['index'], 'transaction': block['transactions'], 'proof': block['proof'], 'previous_hash': block['previous_hash'], 'block_hash': BlockChain.hash(block) } for node in blockchain.nodes: if node == blockchain.host: continue try: requests.get(f"http://{node}/nodes/resolve", timeout=0.5) except requests.exceptions.Timeout: continue return jsonify(response), 200
import hashlib import random import time import math import json import KeyGen from Miner import Miner from Users import user_db from Blockchain import BlockChain from Transaction import Transaction if __name__ == "__main__": sutdcoin = BlockChain() users = list(user_db.keys()) t1 = Transaction(users[0], users[2], 80, "user0 -(80)-> user2") t2 = Transaction(users[0], users[1], 30, "user0 -(30)-> user1") sutdcoin.add_transaction_to_pool([t1, t2]) user3 = Miner(users[3], sutdcoin) user3.mine() print("\nsutdcoin blockchain graph:", sutdcoin.graph) print("\nsutdcoin user_db:", user_db)
class Node(): def __init__(self,ip,port,key=None): self.p2p=None self.ip=ip self.port=port self.transitionPool=TransactionPool() self.wallet=Wallet() self.blockChain=BlockChain() if key is not None: self.wallet.formKey(key) def startP2P(self): self.p2p=SocketCommunication(self.ip,self.port) self.p2p.startSocketCommunication(self) def startAPI(self,apiPort): self.api=NodeAPI() self.api.injectNode(self) self.api.start(apiPort) def handleTransaction(self,transaction): data=transaction.payload() signature=transaction.signature signerPublicKey=transaction.senderPublicKey signatureValid=Wallet.signatureValid(data,signature,signerPublicKey) transactionExists=self.transitionPool.transactionExists(transaction) transactionInBlockExists=self.blockChain.transactionExists(transaction) if not transactionExists and not transactionInBlockExists and signatureValid: self.transitionPool.addTransaction(transaction) message=Message(self.p2p.socketConnector,'TRANSACTION',transaction) encodedMessage=BlockchainUtils.encode(message) self.p2p.broadcast(encodedMessage) forginRequired=self.transitionPool.forgerRequired() if forginRequired: self.forge() def handleBlock(self,block): forger=block.forger blockHash=block.payload() signature=block.signature blockCountValid= self.blockChain.blockCountValid(block) lastBlockHashValid=self.blockChain.lastBlockHashValid(block) def forge(self): forger=self.blockChain.nextForger() print(forger) print(self.wallet.publicKeyString()) if forger == self.wallet.publicKeyString(): print("i am the next forger") block=self.blockChain.createBlock(self.transitionPool.transactions,self.wallet) self.transitionPool.removeFromPool(block.transactions) message=Message(self.p2p.socketConnector,'BLOCK',block) encodedMessage=BlockchainUtils.encode(message) self.p2p.broadcast(encodedMessage) else: print("i am not the next forger")
import curses from curses import KEY_RIGHT, KEY_LEFT, KEY_DOWN, KEY_UP import csv import time import datetime import hashlib import json import socket import select import sys import threading from Blockchain import NodoBlock, BlockChain from ArbolAVL import NodoAVL,ArbolAVL bloque=BlockChain() index=0 clase="" timestamp="" cadenajson="" salida="" prevhash="" Hash="" codificar=hashlib.sha256() flag_send=False username="" selectblock=0 inorden="" postorden="" preorden="" server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# -*- coding: utf-8 -*- # @Time : 2021/3/30 15:11 # @Author : LTstrange import requests from Blockchain import BlockChain from flask import Flask, jsonify, request, render_template, redirect # Instantiate our Node app = Flask(__name__) # Instantiate the Blockchain blockchain = BlockChain() # GUI part @app.route('/', methods=['POST', 'GET']) def index(): blockchain.host = request.host blockchain.register_node(blockchain.host) if request.method == 'GET': return render_template('index.html', host=blockchain.host, identifier=blockchain.Account) @app.route('/login/Page', methods=['GET']) def login(): return render_template('login.html'), 200 @app.route('/login_account', methods=['POST'])
from Blockchain import BlockChain # Creating a new block chain block_chain = BlockChain() # test case, operations on an empty block chain print("\nChecking TEST_CASE_1: Empty block chain\n") # size of block chain print("The size of block chain is: "+str(block_chain.get_size())) # block chain head block print("The head of block chain is: "+str(block_chain.get_head())) # searching for a block which doesn't exist print("Is there a block with data '1' in the block chain ? "+str(block_chain.find_block('1'))) # printing the list of all blocks in the block chain print("The list of blocks in the block chain are: "+str(block_chain.get_blocks())) # adding a block block_chain.add_block("1") print("Added a block with data '1' to the empty block chain...") ''' Checking TEST_CASE_1: Empty block chain The size of block chain is: 0 The head of block chain is: None Is there a block with data '1' in the block chain ? -1 The list of blocks in the block chain are: [] Added a block with data '1' to the empty block chain... ''' # test case, operations on a block chain which includes 1 block print("\nChecking TEST_CASE_2: block chain with a single block\n") # size of block chain