def server_program(): blockchain = BlockChain.BlockChain() # get the hostname host = '127.0.0.1' port = 5000 # initiate port no above 1024 server_socket = socket.socket() # get instance # look closely. The bind() function takes tuple as argument server_socket.bind((host, port)) # bind host address and port together # configure how many client the server can listen simultaneously server_socket.listen(2) conn, address = server_socket.accept() # accept new connection while True: # receive data stream. it won't accept data packet greater than 1024 bytes data = recieveMessages(conn) if not data: break if(data[0] == 100): print("data recieved") blockchain.add_block(data[1]) conn.send(pickle.dumps([2000])) # send data to the client elif(data[0] == 200): print("Authentication") break conn.close() # close the connection
def __init__(self, genesisBlock: Block = None, nodeID = None): self.id = nodeID self.miningNodeList = [] self.blockChain = BlockChain(genesisBlock) self.blockQueue = Queue() self.globalUnverifiedTxPool : List[Transaction] = [] self.alreadyMinedTx : List[Transaction] = [] self.miningDifficulty = 0x07FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
def main(): usr1 = GenAddress.GenAddress() usr2 = GenAddress.GenAddress() usr3 = GenAddress.GenAddress() minor = GenAddress.GenAddress() blockChain = BlockChain.BlockChain() # usr1がトランザクションを生成 transaction = { 'sender_blockchain_address': usr1.address, 'recipient_blockchain_address': usr2.address, 'value': float(30) } signature = Transaction.VerifyTransaction().generate_signature( usr1.privkey, transaction) blockChain.add_transaction(transaction, signature, usr1.pubkey) # usr2がトランザクションを生成 transaction = { 'sender_blockchain_address': usr2.address, 'recipient_blockchain_address': usr3.address, 'value': float(10) } signature = Transaction.VerifyTransaction().generate_signature( usr2.privkey, transaction) blockChain.add_transaction(transaction, signature, usr2.pubkey) # usr3がトランザクションを生成 transaction = { 'sender_blockchain_address': usr3.address, 'recipient_blockchain_address': usr1.address, 'value': float(10) } signature = Transaction.VerifyTransaction().generate_signature( usr3.privkey, transaction) blockChain.add_transaction(transaction, signature, usr3.pubkey) # minorがトランザクションをblockに格納 if blockChain.mining(minor.address): pprint.pprint(blockChain.chain) else: print("マイニング失敗")
import json app = Flask(__name__) from BlockChain import * from Transaction import * from Network import * from Node import * from Nodes import * n0 = Node("0.0.0.0", 1337, str(uuid4()).replace('-', '')) n1 = Node("0.0.0.0", 1338, str(uuid4()).replace('-', '')) nodes = Nodes([n0, n1]) blockchain = BlockChain() blockchain.chain.append(blockchain.genesis()) # add into db # share to other nodes walletAlice = Wallet(blockchain, "Alice") walletBob = Wallet(blockchain, "Bob") @app.route('/valid', methods=['GET']) def valid(): return jsonify(blockchain.is_valid()) @app.route('/minerate', methods=['POST']) def minerate():
import FunctionNetwork as Net import BlockChain import Wallet from pycoin import merkle bc = BlockChain.BlockChain() #bc.add_block(BlockChain.Block("0",1,0,0)) w = Wallet.Wallet() w.create_identity("ale", "nardo", "95042429425", "24/4/1995", "m") b = BlockChain.Block("0", merkle.merkle([w.my_identity.get_hash()]), "0", "0") b.set_identities([w.my_identity]) bc.add_block(b) bc.add_block(BlockChain.Block(bc.tip.get_hash(), 1, 0, 0)) a = Net.node(blockChain=bc, ip="127.0.0.1", port=Net.PORT, is_seed=1)
run_date=dd, kwargs={ 'scheduler': scheduler, 'blockchain': blockchain }) if (slot < 5): blockchain.forge_new_block() else: blockchain.new_genesis_block() stakeholder = Stakeholder.Stakeholder(wallet1['public_key'], "http://127.0.0.1:" + str(5000) + "/", wallet1['wallet_address']) blockchain = BlockChain.BlockChain(True, stakeholder, wallet1['private_key']) print(blockchain.get_current_slot()) print(blockchain.get_current_epoch()) time.sleep(6) print(blockchain.get_current_slot()) print(blockchain.get_current_epoch()) """ wallet2 = BlockChain.BlockChain.generate_new_wallet() wallet3 = BlockChain.BlockChain.generate_new_wallet() blockchain.register_new_node(wallet3['public_key'], "http://127.0.0.1:5001", wallet3['wallet_address']) transaction = { "sender_address": wallet1['wallet_address'],
from BlockChain import * if __name__ == '__main__': bc = BlockChain(INITIAL_BITS) print('generating genesis block ...') bc.create_genesis() for i in range(30): print('generating {}th block ...'.format(i + 2)) # genesis + 1-origin bc.add_new_block(i)
import os, pickle, io, BlockChain, FunctionNetwork, Authorization, sys, signal import threading from time import sleep import Wallet trusted_parties = [("10.6.123.65", FunctionNetwork.PORT), ("10.6.123.65", FunctionNetwork.PORT - 1), ("10.6.123.65", FunctionNetwork.PORT - 2)] ip = "10.6.123.65" port = FunctionNetwork.PORT kill = False bc = BlockChain.BlockChain("./BlockChain.sqlite3") trie = bc.trie addrSeed = ("10.6.123.65", FunctionNetwork.PORT) if os.path.exists("./netData") == False: file_net = io.open("./netData", 'xb') seed = 1 full = 0 print("desea ser un full node s/n: ") s = input() if s == "s": full = 1 elif s == "n": full = 0 else: print("\n argumento incorrecto") exit(0) net = FunctionNetwork.node(bc,
"private_key": private_key1, "public_key": public_key1, "address": "http://127.0.0.1:" + str(5001) + "/" } private_key2, public_key2 = EcdsaSigning.generate_keys() account3 = { "private_key": private_key2, "public_key": public_key2, "address": "http://127.0.0.1:" + str(5002) + "/" } stakeholder = Stakeholder.Stakeholder(account['public_key'], account['address']) block_chain = BlockChain.BlockChain(True, stakeholder, account['private_key']) values = { 'receiver_public_key': account2['public_key'], 'amount': 10000, 'sender_public_key': account['public_key'], 'sender_private_key': account['private_key'] } values1 = { 'receiver_public_key': account3['public_key'], 'amount': 10000, 'sender_public_key': account['public_key'], 'sender_private_key': account['private_key'] }
import socket import sys import threading import BlockChain import json thechain = BlockChain.BlockChain() theq = [] def getfile(cmt): rqd = [] for i in range(2, len(thechain.chain)): dt = blk.data.split(' ## ') if cmt == int(dt[0]): rqd = dt break return rdq def addFile(cmt, nme, fsize, fcode, pf): data = cmt + ' ## ' + nme + ' ## ' + fsize + ' ## ' + fcode + ' ## ' + pf theq.append(data) port = 5000 host = 'localhost' cl = set() class tr(threading.Thread): def __init__(self, port, host, data):
import hashlib import json import testdata import BlockChain # Main eth = BlockChain.BlockChain() eth.generate_genesis_block() eth.addblock(testdata.blocks[0]) eth.addblock(testdata.blocks[1]) eth.details() eth.hack(1, 'HACKED') eth.details() eth.check()