File size: 904 Bytes
d488241 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
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 |