def mine_block(self, wallet: BitcoinAccount) -> Optional[Block]: if not self.tx_pool: return None if self.head.hashval == None: raise Exception("hashval of blockchain head is None.") new_block = Block( index=self.head.index + 1, timestamp=time.time(), previous_hash=self.head.hashval, miner=wallet.to_address(), ) reward = Transaction( receiver=wallet.to_address(), sender="NETWORK_ADMIN", amount=self.block_reward, timestamp=time.time(), signature="NETWORK_ADMIN", ) self.tx_pool.append(reward) new_block.add_transactions(self.tx_pool) new_block.mine(self.difficulty) result = self.__add_block(new_block) result.sign(wallet) self.tx_pool.clear() return result
def mine(self, difficulty, wallet: BitcoinAccount) -> int: self.timestamp = time.time() nonce = 0 prefix = '0' * difficulty hash_val = self.hashBlock(nonce) while not hash_val.startswith(prefix): nonce += 1 hash_val = self.hashBlock(nonce) self.hash_val = hash_val self.nonce = nonce if wallet: self.miner_name = wallet.to_address() self.sign(wallet) else: self.miner_name = "name" return nonce
import json import time from transaction import Transaction from block import Block from key import BitcoinAccount wallet = BitcoinAccount() difficulty = 4 first_block = Block(0, "") first_block.add_transaction(sender=wallet.to_address(), receiver="justine", amount=50) first_block.transactions[-1].sign(wallet) print(first_block.transactions[-1].verify_signature()) first_block.mine(difficulty, wallet) print(first_block.verify_signature()) last_hash = first_block.hash_val
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)
def create(cls, difficulty: int, wallet: BitcoinAccount): blockchain = cls(difficulty) blockchain.head.sign(wallet) blockchain.head.miner = wallet.to_address() return blockchain
def sign(self, wallet: BitcoinAccount): message = str(self.sender) + str(self.receiver) + str(self.amount) if wallet: self.signature = wallet.sign(message).hex()
port_bind = sys.argv[1] context = zmq.Context() socket = context.socket(zmq.PUB) socket.bind("tcp://*:%s" % port_bind) topic = "" ip = "localhost" + ":" + port_bind socket_sub = context.socket(zmq.SUB) socket_sub.setsockopt_string(zmq.SUBSCRIBE, topic) # wallet generation wallet = BitcoinAccount() address = wallet.to_address() file_name = "wallets/" + address + ".json" wallet.to_file(file_name) # blockchain data difficulty = 3 blockchain = None class Connection(QObject): chain = Signal(Blockchain) clean_tx = Signal() add_tx = Signal(dict)
import time from transaction import Transaction from block import Block from key import BitcoinAccount wallet = BitcoinAccount() difficulty = 4 first_block = Block(0, "") tx = Transaction("mohamed", "justine", 50, time.time()) first_block.add_transaction(tx) first_block.mine(difficulty) print("First block is: ") print(first_block) last_hash = first_block.hashval second_block = Block(1, last_hash) second_block.mine(difficulty) print("Second block is: ") print(second_block)
def sign(self, wallet: BitcoinAccount): message = str(self.hash_val) self.signature = wallet.sign(message).hex()