def car_history_is_valid(self, car_id): """ car history is valid if 1. chain is valid 2. entries in data base match corresponding chain entries 3. the hashes that link the car history are valid """ if self.chain_is_valid(): client = DBConnect() car_history = client.get_car_history(car_id) for stage in car_history: if stage != self.chain[stage["_id"]]: return False if len(car_history) <= 1: return True index = 1 while index < len(car_history): if self.get_hash( car_history[index - 1]) != car_history[index]["hash"]["car"]: return False index += 1 return True else: return False
def mine_block(self, block_type, block_data): client = DBConnect() last_block = self.csv_operator.get_last_block() _id = self.csv_operator.get_chain_length() car_id = block_data[0] nonce = self.proof_of_work(last_block["nonce"]) last_hash_block = self.get_hash(last_block) last_hash_car = "None" if block_type == "Production": block = { "_id": _id, "car_id": car_id, "nonce": nonce, "hash": { "block": last_hash_block, "car": last_hash_car }, "details": { "block_type": "Production" } } elif block_type == "NewRegister": block = { "_id": _id, "car_id": car_id, "nonce": nonce, "hash": { "block": last_hash_block, "car": last_hash_car }, "details": { "block_type": "NewRegister" } } elif block_type == "Repair": last_car_entry = client.get_car_history(car_id)[-1] last_hash_car = self.get_hash(last_car_entry) block = { "_id": _id, "car_id": car_id, "nonce": nonce, "hash": { "block": last_hash_block, "car": last_hash_car }, "details": { "block_type": "Repair", } } elif block_type == "Sale": last_car_entry = client.get_car_history(car_id)[-1] last_hash_car = self.get_hash(last_car_entry) block = { "_id": _id, "car_id": car_id, "nonce": nonce, "hash": { "block": last_hash_block, "car": last_hash_car }, "details": { "block_type": "Sale" } } else: return "wrong input" client.ingest_block(block) self.csv_operator.add_block(block)