Exemplo n.º 1
0
 def test_l4block_signing_hashing(self):
     block = make_l4_block()
     sig = crypto.sign_l4_block(blake2b, secp256k1, key, block)
     self.assertEqual(
         sig,
         "MEUCIQDwOwwYlWivucunKJXm0KB+7BsEiHDmEKLg2wL6MKJIsQIgdtjZRBimLmsZjOcCkCmalLHf0sF2exAMsluuVxMaLvk="
     )
     sig = crypto.sign_l4_block(sha256, secp256k1, key, block)
     self.assertEqual(
         sig,
         "MEQCIHcVPm+NEIEJXCeoWgX94fo7Y+kMhZGKtroh94iNZTTbAiB7qWPI3B4vpj0Ir5+X0gHv/m9Xq6abQnWRagH64Xu4OA=="
     )
     sig = crypto.sign_l4_block(sha3_256, secp256k1, key, block)
     self.assertEqual(
         sig,
         "MEQCIDGi9WC8097OtOUXahHm0/uJP5PUv6isk1NVvulbQ2c2AiBULyExoupsgJ/2KFKDQYHF1qQa5++cZxFxyqlXPpY0lA=="
     )
     block_hash, nonce = crypto.pow_l4_block(blake2b, block)
     self.assertEqual(block_hash,
                      "ACZ1yEGzRKKDxm9jqk5j8U6QRyeVHzqRjcS/G2gnEy8=")
     self.assertEqual(nonce, 82)
     block_hash, nonce = crypto.pow_l4_block(sha256, block)
     self.assertEqual(block_hash,
                      "AEULvMdpuf6Sj7M3HxSCZLjQODViHxRRKMy4Ze0VrLY=")
     self.assertEqual(nonce, 3)
     block_hash, nonce = crypto.pow_l4_block(sha3_256, block)
     self.assertEqual(block_hash,
                      "AOEPG+CTLsfaa92DpnOrkro+8WLGUKSKD28I+kqS9Lo=")
     self.assertEqual(nonce, 182)
Exemplo n.º 2
0
 def test_l4block_pow_verifying(self):
     block = make_l4_block()
     block_hash, nonce = crypto.pow_l4_block(blake2b, block)
     block.proof = block_hash
     block.nonce = nonce
     block.scheme = "work"
     self.assertTrue(crypto.verify_l4_block_pow(blake2b, block))
     valid_block = copy.deepcopy(block)
     block.dc_id = "forged"
     self.assertFalse(crypto.verify_l4_block_pow(blake2b, block))
     block = copy.deepcopy(valid_block)
     block.block_id = "forged"
     self.assertFalse(crypto.verify_l4_block_pow(blake2b, block))
     block = copy.deepcopy(valid_block)
     block.timestamp = "forged"
     self.assertFalse(crypto.verify_l4_block_pow(blake2b, block))
     block = copy.deepcopy(valid_block)
     block.prev_proof = "forged=="
     self.assertFalse(crypto.verify_l4_block_pow(blake2b, block))
     block = copy.deepcopy(valid_block)
     block.l1_dc_id = "forged"
     self.assertFalse(crypto.verify_l4_block_pow(blake2b, block))
     block = copy.deepcopy(valid_block)
     block.l1_block_id = "forged"
     self.assertFalse(crypto.verify_l4_block_pow(blake2b, block))
     block = copy.deepcopy(valid_block)
     block.l1_proof = "forged"
     self.assertFalse(crypto.verify_l4_block_pow(blake2b, block))
     block = copy.deepcopy(valid_block)
     block.validations[0]["l3_dc_id"] = "forged"
     self.assertFalse(crypto.verify_l4_block_pow(blake2b, block))
     block = copy.deepcopy(valid_block)
     block.validations[0]["l3_block_id"] = "forged"
     self.assertFalse(crypto.verify_l4_block_pow(blake2b, block))
     block = copy.deepcopy(valid_block)
     block.validations[0]["l3_proof"] = "forged"
     self.assertFalse(crypto.verify_l4_block_pow(blake2b, block))
     block = copy.deepcopy(valid_block)
     block.validations[0]["valid"] = False
     self.assertFalse(crypto.verify_l4_block_pow(blake2b, block))
     block = copy.deepcopy(valid_block)
     block.proof = "47DEQpj8HBSa+/FORGEDJCeuQeRkm5NMpJWZG3hSuFU="
     self.assertFalse(crypto.verify_l4_block_pow(blake2b, block))
     block = copy.deepcopy(valid_block)
     block.nonce = 0
     self.assertFalse(crypto.verify_l4_block_pow(blake2b, block))
Exemplo n.º 3
0
 def pow_block(self, signable_block: "model.BlockModel") -> Tuple[str, int]:
     """Do proof of work on a block
     Args:
         block: BlockModel to do proof of work on
     Returns:
         Tuple where index 0 is a Base64 encoded string of the generated hash and index 1 is the nonce
     Raises:
         exceptions.InvalidNodeLevel when invalid level on self
     """
     if self.level == 1:
         return crypto.pow_l1_block(
             self.hash, cast("l1_block_model.L1BlockModel", signable_block))
     elif self.level == 2:
         return crypto.pow_l2_block(
             self.hash, cast("l2_block_model.L2BlockModel", signable_block))
     elif self.level == 3:
         return crypto.pow_l3_block(
             self.hash, cast("l3_block_model.L3BlockModel", signable_block))
     elif self.level == 4:
         return crypto.pow_l4_block(
             self.hash, cast("l4_block_model.L4BlockModel", signable_block))
     else:
         raise exceptions.InvalidNodeLevel(
             f"Node level {self.level} not implemented yet")