def mine(self): unmined_raw = self.unmined_raw() assert 0 < self.difficulty < 256 target = ((1 << 256 - self.difficulty) - 1).to_bytes(32, 'big') self.padding = 0 while double_sha(unmined_raw) > target: self.padding += 1 unmined_raw = unmined_raw[:-4] + self.padding.to_bytes(4, 'big') self.__checksum = double_sha(self.unmined_raw())
def test_4_wrong_previous_block(self): self.blocks[0].previous_block_rev = BlockRev.from_id(double_sha(b'something')) self.blocks[0].mine() sign_object(self.public_key, self.private_key, self.blocks[0]) with self.assertRaisesRegex(Block.ChainError, "previous_block_rev does not exist"): self.blocks[0].verify()
def id(self): if self.requires_signature_verification or self.__id is None: self.__id = double_sha(self.raw()) return self.__id
def test_wrong_checksum(self): self.block._Block__checksum = double_sha(b'something') with self.assertRaisesRegex(Block.VerifyError, "wrong checksum"): self.block.unsigned_raw()
def is_checksum_correct(self): """ Check if checksum is correct. """ return self.__checksum == double_sha(self.unmined_raw())