Best Python code snippet using autotest_python
blockchain.py
Source:blockchain.py  
...13        the chain. The block has index 0, previous_hash as 0, and14        a valid hash.15        """16        genesis_block = Block(0,[],0,'0')17        genesis_block.hash = self._compute_hash()18        self.chain.append(genesis_block)19    @property20    def last_block(self):21        #gets the last block of chain22        last_block = self.chain[-1]23        return last_block24    def add_block(self, block, proof):25        """26        A function that adds the block to the chain after verification.27        Verification includes:28        * Checking if the proof is valid.29        * The previous_hash referred in the block and the hash of latest block30          in the chain match.31        """32        previous_hash = self.last_block.hash33        if previous_hash != block.previous_hash:34            return False35        if not Blockchain.is_valid_proof(block, proof):36            return False37        block.hash = proof38        self.chain.append(block)39        return True40    @staticmethod41    def proof_of_work(block):42        """43        Function that tries different values of nonce to get a hash44        that satisfies our difficulty criteria.45        """46        block.nonce = 047        48        computed_hash = block._compute_hash()49        while not computed_hash.startswith('0' * Blockchain.difficulty):50            block.nonce += 151            computed_hash = block._compute_hash()52        return computed_hash53    def add_new_transaction(self, transaction):54        self.unconfirmed_transactions.append(transaction)55    @classmethod56    def is_valid_proof(cls, block, block_hash):57        """58        Check if block_hash is valid hash of block and satisfies59        the difficulty criteria.60        """61        status = (block_hash.startswith('0' * Blockchain.difficulty) and62                block_hash == block._compute_hash())63        return status64    @classmethod65    def check_chain_validity(cls, chain):66        result = True67        previous_hash = "0"68        for block in chain:69            block_hash = block.hash70            # remove the hash field to recompute the hash again71            # using `compute_hash` method.72            delattr(block, "hash")73            if not cls.is_valid_proof(block, block_hash) or \74                    previous_hash != block.previous_hash:75                result = False76                break...merkle.py
Source:merkle.py  
...53        self._hash = None54        self.data = data55    def accept(self, visitor):56        visitor.visit_node(self)57    def _compute_hash(self):58        if self.is_leaf():59            s = str(self._data)60            self._hash = sha256(bytes(s, 'utf-8')).hexdigest()61        else:62            lh = self._left.hash if self._left else ''63            rh = self._right.hash if self._right else ''64            self._hash = sha256(bytes(lh + rh, 'utf-8')).hexdigest()65    def is_leaf(self):66        return self._data is not None67    @property68    def data(self):69        return self._data70    71    @data.setter72    def data(self, value):73        self._data = value74        self._compute_hash()75    @property76    def left(self):77        return self._left78    @left.setter79    def left(self, value):80        self._left = value81        self._compute_hash()82    @property83    def right(self):84        return self._right85    @right.setter86    def right(self, value):87        self._right = value88        self._compute_hash()89    @property90    def hash(self):91        return self._hash92    def __str__(self):93        return '<Node(hash={}, data={})>'.format(self.hash, self._data)...hash_tree.py
Source:hash_tree.py  
...18        if _compute_root_hash(self.divs) == self.root_hash:19            return []20        diffs = []21        for div in self.divs:22            div_hash = _compute_hash(div)23            if not div_hash in self.hashes:24                diffs.append(div)        25        return diffs26    def update(self):27        self.hashes = set()28        for div in self.divs:29            self.hashes.add(_compute_hash(div))30        self.root_hash = _compute_root_hash(self.divs)31        json_string = json.dumps({'root_hash': self.root_hash, 'hashes': list(self.hashes)}, indent=4)32        file = open(HASHES_FILE, 'w')33        file.write(json_string)34        file.close()35def _compute_hash(div):36    identifier = div.find("a", class_="internal-link")['href']37    hash = hashlib.sha256(identifier.encode('ascii')).hexdigest()38    return hash39def _compute_root_hash(divs):40    div_hashes = []41    for div in divs:42        div_hashes.append(_compute_hash(div))...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
