class TestBlock(unittest.TestCase): def setUp(self): t0 = Transaction(0, "hello world") t1 = Transaction(t0.hash, "woe is me") t2 = Transaction(t1.hash, "woe is me") self.b0 = Block(0, 0, t0, t1) self.b1 = Block(0, self.b0.hash, t2) def test_serialize(self): s = json.dumps(self.b1.serialize()) b = Block.deserialize(s) assert (b.equals(self.b1))
def create_gift_coin(self, blockchain): """ This method creates a new coin for the new client """ # Get address of the client hash_public_key = self.hash_pubkey() coin_gift_tx_input = TransactionInput(prev_tx="1" * 64, pk_spender="1" * 64, signature=bytes( "\x11" * 64, encoding="utf-8")) coin_gift_tx_output = TransactionOutput( value=100, hash_pubkey_recipient=hash_public_key) coin_gift_tx = Transaction(tx_input=coin_gift_tx_input, tx_output=coin_gift_tx_output) transactions = [coin_gift_tx] nonce = 0 new_block = None while True: if len(blockchain.blocks) != 0: hash_prev_block = blockchain.blocks[len(blockchain.blocks) - 1].get_hash() new_block = Block(transactions, nonce, hash_prev_block) else: new_block = Block(transactions=transactions, nonce=nonce, prev_block_hash="0" * 64) if new_block.get_hash().startswith("0" * blockchain.difficulty): print("Nonce found:", nonce) break nonce += 1 print(coin_gift_tx.serialize()) message = "\x12" + new_block.serialize() self.socket.sendall(message.encode('utf-8'))
def test_post_block(self): t0 = Transaction(0, "Foo") block = Block(0, self.chain.chain[-1].hash, t0) self.chain.add(block) response = requests.post('http://localhost:5000/block', data=block.serialize())