def create_block(self, nonce, prev_hash, block_hash): block = Block(nonce, prev_hash, block_hash, len(self.chain) + 1, self.pendingTransaction) if block.is_block_valid(): self.chain.append(block) self.pendingTransaction = [] return block return False
def test_second_block(self): genesis_block = Block("other genesis transaction", ["other genesis transaction"]) second_block = Block(genesis_block.block_hash, ["second transaction"]) self.assertEqual( "bc614d0f64f95f3bebbed0fc562b5dd6dec85db07cd6c2d217936d34bb77fbe6", second_block.block_hash)
def generate_genesis_block(self): # add genesis block if len(self.chain) == 0: print('Mining Block #0...') genesis = Block([], 'genesis') genesis.mine(self.difficulty) self.chain.append(genesis.serialized)
def create_genesis_block(self): """ A function which generates the genesis block and appends it to the chain. * Such a block has an index of 0 * Such a block has a previous_hash of 0, and a valid hash. """ genesis_block = Block(0, [], 0, "0") genesis_block.hash = genesis_block.compute_hash() self.chain.append(genesis_block)
def add_block(self, miner_address): # We reward those who mine with 1 coin, so we append a new transaction with this reward self.pending_transactions.append( Transaction('blockchain', miner_address, 1).serialized) # from blockchain, to miner, 1 coin # construct a new block new_block = Block(self.pending_transactions, self.chain[-1]['hash']) print(f'Mining block #{len(self.chain)}...') # mine it new_block.mine(self.difficulty) # and append to chain, cleaning the pending transaction ('cos we've already written them in the block) self.chain.append(new_block.serialized) self.pending_transactions = []
def mine(self): """ This function serves as an interface to add the pending transactions to the blockchain by adding them to the block and figuring out Proof Of Work. """ if not self.unconfirmed_transactions: return False last_block = self.get_last_block new_block = Block(index=last_block.index + 1, data=self.unconfirmed_transactions, timestamp=time.time(), previous_hash=last_block.hash) proof = self.proof_of_work(new_block) self.add_block(new_block, proof) self.unconfirmed_transactions = [] return True
def verify_and_add_block(): """ Adds a block mined by someone else to the node's chain. The block is first verified by the node and then added to the chain. :return: a success/error string, HTTP error code """ block_data = request.get_json() block = Block(block_data["index"], block_data["transactions"], block_data["timestamp"], block_data["previous_hash"], block_data["nonce"]) proof = block_data['hash'] added = blockchain.add_block(block, proof) if not added: return "The block was discarded by the node", 400 return "Block added to the chain", 201
def create_chain_from_dump(chain_dump): """ Creates a blockchain from given data. :param chain_dump: the data :return: the generated blockchain """ generated_blockchain = Blockchain() generated_blockchain.create_genesis_block() for idx, block_data in enumerate(chain_dump): if idx == 0: # We don't care about the genesis block. continue 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
def test_genesis_block(self): genesis_block = Block("genesis transaction", ["genesis transaction"]) self.assertEqual( "46dcd5396770ce394ce0c8b259a0e6e7defb277e17bfc17ff17a51988fc9c2d2", genesis_block.block_hash)
def get_random_block(self, block_number=None, prevhash=None): block_number = block_number if block_number is not None else np.random.randint( 2000) block = Block().to_dict(block_number=block_number) block['transactions'] = self.get_transactions(2000) return block
from blockchain.Block import Block blockchain = [] genesis_block = Block("Chacellor on the brink...", [ "Satoshi sent 1 BTC to Victor", "Maria sent 5 BTC to Jenny", "Satoshi sent 5 BTC to Hal Finney" ]) second_block = Block(genesis_block.block_hash, [ "Ivan sent 1 BTC to Jhonn", "Maria sent 5 BTC to Ivan", "Satoshi sent 5 BTC to Victor" ]) third_block = Block(second_block.block_hash, ["A to C 5 BTC", "B to D 5 BTC"]) print("Block hash: Genesis Block") print(genesis_block.block_hash) print("Block hash: Second Block") print(second_block.block_hash) print("Block hash: Third Block") print(third_block.block_hash)
def __init__(self, PORT): self.chain = [] self.pendingTransaction = [] self.db = DBUtil('db/' + str(PORT)) self.networkNodes = self.db.get_network_node() self.db.store_genesis_block(Block(1, ' ', '00000', 1, []))
def get_block_from_dict(self, block_dict): return Block(nonce=block_dict['timestamp'], number=block_dict['number'], prevhash=block_dict['prevhash'])
def __init__(self, PORT): self.chain = [] self.pendingTransaction = [] self.networkNodes = [] self.file = FileUtil('json/' + str(PORT) + '.json') self.file.store_genesis_block(Block(1, '0000x', '0000x', 1, []))