| from hashlib import sha256 | |
| import time | |
| class AdBlockchain: | |
| def __init__(self): | |
| self.chain = [] | |
| self.create_block(proof=1, previous_hash='0') | |
| def create_block(self, proof, previous_hash, ad_data=None): | |
| block = { | |
| 'index': len(self.chain) + 1, | |
| 'timestamp': str(time.time()), | |
| 'proof': proof, | |
| 'previous_hash': previous_hash, | |
| 'ad_hash': self.hash_ad(ad_data) | |
| } | |
| self.chain.append(block) | |
| return block | |
| def hash_ad(self, ad_data): | |
| return sha256(str(ad_data).encode()).hexdigest() | |
| def is_chain_valid(self): | |
| for i in range(1, len(self.chain)): | |
| current_block = self.chain[i] | |
| previous_block = self.chain[i - 1] | |
| if current_block['previous_hash'] != self.hash_ad(previous_block): | |
| return False | |
| return True |