Example #1
0
 def mine(self, start_nonce: int, end_nonce: int) -> Optional[MiningResult]:
     for nonce in range(start_nonce, end_nonce):
         nonce_bytes = nonce.to_bytes(8, byteorder="big")
         h = sha256(sha256(self.header_hash + nonce_bytes))
         if h < self.target:
             return MiningResult(self.header_hash, nonce, bytes(32))
     return None
Example #2
0
def validate_seal(
        block_header: Header,
        consensus_type: ConsensusType,
        adjusted_diff: int = None,  # for overriding
        **kwargs) -> None:
    diff = adjusted_diff if adjusted_diff is not None else block_header.difficulty
    nonce_bytes = block_header.nonce.to_bytes(8, byteorder="big")
    if consensus_type == ConsensusType.POW_ETHASH:
        if not check_pow(
                block_header.height,
                block_header.get_hash_for_mining(),
                block_header.mixhash,
                nonce_bytes,
                diff,
        ):
            raise ValueError("invalid pow proof")
    elif consensus_type == ConsensusType.POW_QKCHASH:
        if not qkchash_check_pow(
                block_header.height,
                block_header.get_hash_for_mining(),
                block_header.mixhash,
                nonce_bytes,
                diff,
                kwargs.get("qkchash_with_rotation_stats", False),
        ):
            raise ValueError("invalid pow proof")
    elif consensus_type == ConsensusType.POW_DOUBLESHA256:
        target = (2**256 // (diff or 1) - 1).to_bytes(32, byteorder="big")
        h = sha256(sha256(block_header.get_hash_for_mining() + nonce_bytes))
        if not h < target:
            raise ValueError("invalid pow proof")
Example #3
0
def validate_seal(
    block_header: Union[RootBlockHeader, MinorBlockHeader],
    consensus_type: ConsensusType,
    adjusted_diff: int = None,  # for overriding
) -> None:
    diff = adjusted_diff if adjusted_diff is not None else block_header.difficulty
    nonce_bytes = block_header.nonce.to_bytes(8, byteorder="big")
    if consensus_type == ConsensusType.POW_ETHASH:
        if not check_pow(
            block_header.height,
            block_header.get_hash_for_mining(),
            block_header.mixhash,
            nonce_bytes,
            diff,
        ):
            raise ValueError("invalid pow proof")
    elif consensus_type == ConsensusType.POW_QKCHASH:
        if not qkchash_check_pow(
            block_header.get_hash_for_mining(), block_header.mixhash, nonce_bytes, diff
        ):
            raise ValueError("invalid pow proof")
    elif consensus_type == ConsensusType.POW_SHA3SHA3:
        target = (2 ** 256 // (diff or 1) - 1).to_bytes(32, byteorder="big")
        h = sha256(sha256(block_header.get_hash_for_mining() + nonce_bytes))
        if not h < target:
            raise ValueError("invalid pow proof")