def __init__(self, previous_hash, transactions): self.timestamp = time.time() self.nonce = 0 self.previous_hash = previous_hash self.transactions = transactions self.merkle_root = merkle_root(transactions) self.hash = 0
def __init__(self, timestamp, previous_hash, transactions, nonce = 0): self.height = 0 self.timestamp = timestamp self.nonce = nonce self.previous_hash = previous_hash self.transactions = transactions self.merkle_root = merkle_root(transactions)
def is_valid_chain(self, utxo): item = [x for x in self.blocks if x.hash == self.last_hash] if (len(item) == 0): print ('wrong last hash') return False item = item[0] while (True): if int (item.hash, 16) > self.target: print ('wrong number of zeros in hash') return False test_hash = item.calculate_hash() if (test_hash != item.hash): print (item.hash, test_hash) print ('invalid hash of block') return False test_mroot = merkle_root(item.transactions) if (test_mroot != item.mroot): print ('merlke root mismatch') return False if (item.previous_hash == '0'): break item = [x for x in self.blocks if x.hash == item.previous_hash] if (len(item) == 0): print ('wrong prev hash') return False item = item[0] for i in range(len(self.blocks)): bl = self.blocks[i] bldiapp = self.blocks[i - 3: i] if i > 2 else [] if not bl.validate(bldiapp, utxo): return False if not self.muted: print ('Blockchain is valid') return True
def __init__(self, timestamp, previous_hash, transactions): self.timestamp = timestamp self.nonce = 0 self.previous_hash = previous_hash self.transactions = transactions self.merkle_root = merkle.merkle_root(transactions) self.hash = self.hash_block() self.complexity = None
def validate(block): assert merkle.merkle_root(block.transactions) == block.merkle_root, "Invlalid Merkle Root of transactions." assert '0' * block.complexity == block.hash[:block.complexity], "Invlalid proof of work result." assert block.hash == block.hash_block(), "Invalid hash of block." assert time.time() > float(block.timestamp), "Invalid timestamp" return True
def __init__(self, tm, non, ph, transs): self.timestamp = tm self.nonce = non self.previous_hash = ph self.transactions = transs self.mroot = merkle_root(transs) self.version = 47 self.target = 2 ** (256 - 1) self.heigth = 0 self.hash = self.calculate_hash()
def chain_verify(chain): x = 0 y = len(chain) while x < y: if x + 1 != y and chain[x]['hash'] == chain[x + 1]['previous_hash']: if merkle_root( chain[x]['transactions']) == chain[x]['merkle_root']: x += 1 else: return False elif x + 1 == y: return True else: return False
def __init__(self, previous_hash, transactions, last_length, target=2**(256 - 20)): if os.path.isfile('chain/utxo.pk1'): with open('chain/utxo.pk1', 'rb') as input: utxo = pickle.load(input) self.suply = utxo.get_suply() else: self.suply = 0 self.version = '16000000' self.target = target self.hight = last_length + 1 self.timestamp = str(time.time()) self.previous_hash = previous_hash self.transactions = transactions self.m_root = merkle_root(self.transactions.copy())[0]
def __init__(self, previous_hash, transactions): self.timestamp = str(time.time()) self.previous_hash = previous_hash self.transactions = transactions self.m_root = merkle_root(self.transactions.copy())[0]
def test_merkle_root(self): s = [str(i) for i in range(9)] self.assertEqual( 'cc64e53910aa6b6234017147332041f889d647e841222b0a252853c4e72cd21a', merkle_root(s), 'Invalid merkle root')
def calc_mekrle_tree(self, lst): self.merkle_tree = merkle_root(lst) return self.merkle_tree