def hash_samples(samples): if len(samples) % 2 == 1: assert False hashes = [] for i in range(0, len(samples), 2): hashes.append(sha256(samples[i] + samples[i + 1])) return hashes
def validate(self): # TODO: can calc merkle root here to verify there was # no bamboozle in transit data = self.prepareData(self.block.nonce) hash = util.sha256(data) hashInt = int.from_bytes(hash, 'big') return hashInt < self.target
def run(self): nonce = 0 powHash = b'' while nonce < maxInt64: data = self.prepareData(nonce) hash = util.sha256(data) hashInt = int.from_bytes(hash, 'big') if hashInt < self.target: powHash = hash break else: nonce += 1 return nonce, powHash
def test_root(): for sample in data: sample_list = sample[:] if len(sample_list) % 2 == 1: sample_list.append(sample[-1]) hashes = [sha256(s) for s in sample_list] while 1: hashes = hash_samples(hashes) if len(hashes) == 1: break if len(hashes) % 2 == 1: hashes.append(hashes[-1]) assert len(hashes) == 1 assert hashes[0] == merkle.MerkleTree(sample_list).root.data
def __init__(self, left=None, right=None, data=None): if left is not None and right is not None: data = left.data + right.data self.data = sha256(data) self.left = left self.right = right
def setId(self): # We want an empty id when we hash this tx self.id = b'' self.id = sha256(dumps(self))