def test_blockchain_monolithic(): genesis_payload = b"Genesis" genesis_hash_pointer = SHA256(genesis_payload).digest() genesis_block = Block(genesis_hash_pointer, genesis_payload) payload_1 = b"My payload" payload_2 = b"Another one of mine" payload_3 = b"My last payload" block_chain = BlockChain(genesis_block) assert len(block_chain) == 1 assert block_chain.latest_block.payload == genesis_payload block_chain.verify() block_chain.append(payload_1) assert len(block_chain) == 2 assert block_chain.latest_block.payload == payload_1 block_chain.verify() block_chain.append(payload_2) assert len(block_chain) == 3 assert block_chain.latest_block.payload == payload_2 block_chain.verify() block_chain.append(payload_3) assert len(block_chain) == 4 assert block_chain.latest_block.payload == payload_3 block_chain.verify() # Replace block in chain bad_block_payload = b"I am a malicious payload" bad_block_hash_pointer = block_chain.blocks[2].hash_pointer bad_block = Block(bad_block_hash_pointer, bad_block_payload) block_chain.blocks[2] = bad_block with pytest.raises(RuntimeError): block_chain.verify()
def __init__( self, port=9001, keys=None, identity=None # TODO ): # Initialize socket self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.socket.bind(("", port)) # Initialize key dictionary and identity # where is the key dictionary? self.keys = keys self.identity = identity # Initialize block chain genesis_payload = "".join( ("pay ", self.identity, " 0 ", self.identity, " 1000")) genesis_block = Block( SHA256(self.identity.encode()).digest(), genesis_payload) self.blockchain = BlockChain(genesis_block)
def test_block_wrong_type_argument_1(): with pytest.raises(TypeError): Block("Not a good hash pointer", string_payload)
def test_block_payload_encoding(): block = Block(good_hash_pointer, string_payload) assert block.payload.decode(Block.ENCODING) == string_payload
def test_block_wrong_value_argument_2(): with pytest.raises(ValueError): Block(good_hash_pointer, b"A" * (Block.PAYLOAD_SIZE + 1))
def test_block_wrong_value_argument_1(): with pytest.raises(ValueError): Block(bytes(Block.HASH_POINTER_SIZE + 1), bytes_payload)
def test_block_wrong_type_argument_2(): with pytest.raises(TypeError): Block(good_hash_pointer, 123)