コード例 #1
0
ファイル: models.py プロジェクト: tonyle9/nekoyume
    def valid(self) -> bool:
        """Check if this object is valid or not"""
        stamp = self.serialize().decode('utf-8') + self.suffix
        valid = (self.hash == h(str.encode(stamp)).hexdigest())
        valid = valid and hashcash.check(stamp, self.suffix, self.difficulty)

        valid = valid and (len(self.serialize(True, True, True, True)) <=
                           Block.size_limit)

        if self.id > 1:
            prev_block = Block.query.get(self.id - 1)
            valid = valid and self.prev_hash == prev_block.hash

            difficulty = prev_block.difficulty
            difficulty_check_block = Block.query.get(max(1, self.id - 10))
            avg_timedelta = (
                (self.created_at - difficulty_check_block.created_at) /
                (self.id - difficulty_check_block.id))
            if avg_timedelta <= datetime.timedelta(0, 5):
                valid = valid and self.difficulty == max(0, difficulty + 1)
            elif avg_timedelta > datetime.timedelta(0, 15):
                valid = valid and self.difficulty == max(0, difficulty - 1)
            else:
                valid = valid and self.difficulty == difficulty
        else:
            valid = valid and self.prev_hash is None
            valid = valid and self.difficulty == 0

        valid = valid and self.root_hash == h(''.join(
            sorted((m.id for m in self.moves))).encode('utf-8')).hexdigest()

        for move in self.moves:
            valid = valid and move.valid
        return valid
コード例 #2
0
ファイル: hashcash_test.py プロジェクト: ziwon/nekoyume
def test_mint(challenge, bits):
    answer = _mint(challenge, bits)
    stamp = challenge + answer
    digest = hashlib.sha256(stamp)
    assert has_leading_zero_bits(digest.digest(), bits)
    assert check(stamp)