def test_l2block_signing_hashing(self): block = make_l2_block() sig = crypto.sign_l2_block(blake2b, secp256k1, key, block) self.assertEqual( sig, "MEQCIAOSqqOeS8Wdor6AN5gvvqhdh+acAumfq7ImQglllCDbAiBEKs1ZShUUGWipcGB3hpCQVLpqYV3nY0ZufyrNT6j8JQ==" ) sig = crypto.sign_l2_block(sha256, secp256k1, key, block) self.assertEqual( sig, "MEQCIBsQGOpY1JHOhWbBpXmmhOjIwG3h9e6qUu7O6XkznbkFAiBbhfNiW7AwVf4Nx+I0SVnvGP83IJ61UeW5OQIlEnPbXw==" ) sig = crypto.sign_l2_block(sha3_256, secp256k1, key, block) self.assertEqual( sig, "MEQCIAnruzXCTx9AYT6+I+GrMLIWXkIIlg2sN3YVThgDrTgnAiACwUgzj3e6Ata6spob0WZNNfAP0uVumLHDZLEqq0sgqw==" ) block_hash, nonce = crypto.pow_l2_block(blake2b, block) self.assertEqual(block_hash, "AIqD5gtCg5/dgZBV105dz+dX2R7gxdw3QzsTeBV/6O8=") self.assertEqual(nonce, 661) block_hash, nonce = crypto.pow_l2_block(sha256, block) self.assertEqual(block_hash, "ANCH7glM8nNFL96qsIuIXPvEv91T9GX66gS7wwpNEjw=") self.assertEqual(nonce, 16) block_hash, nonce = crypto.pow_l2_block(sha3_256, block) self.assertEqual(block_hash, "AFt3k9xMCZbxb+itQxxd71Dk9vyde5Svvgbm5uA4gyk=") self.assertEqual(nonce, 254)
def test_l2block_pow_verifying(self): block = make_l2_block() block_hash, nonce = crypto.pow_l2_block(blake2b, block) block.proof = block_hash block.nonce = nonce block.scheme = "work" self.assertTrue(crypto.verify_l2_block_pow(blake2b, block)) valid_block = copy.deepcopy(block) block.dc_id = "forged" self.assertFalse(crypto.verify_l2_block_pow(blake2b, block)) block = copy.deepcopy(valid_block) block.block_id = "forged" self.assertFalse(crypto.verify_l2_block_pow(blake2b, block)) block = copy.deepcopy(valid_block) block.timestamp = "forged" self.assertFalse(crypto.verify_l2_block_pow(blake2b, block)) block = copy.deepcopy(valid_block) block.prev_proof = "forged==" self.assertFalse(crypto.verify_l2_block_pow(blake2b, block)) block = copy.deepcopy(valid_block) block.validations_str = '{"forged": true}' self.assertFalse(crypto.verify_l2_block_pow(blake2b, block)) block = copy.deepcopy(valid_block) block.l1_dc_id = "forged" self.assertFalse(crypto.verify_l2_block_pow(blake2b, block)) block = copy.deepcopy(valid_block) block.l1_block_id = "forged" self.assertFalse(crypto.verify_l2_block_pow(blake2b, block)) block = copy.deepcopy(valid_block) block.l1_proof = "forged" self.assertFalse(crypto.verify_l2_block_pow(blake2b, block)) block = copy.deepcopy(valid_block) block.proof = "47DEQpj8HBSa+/FORGEDJCeuQeRkm5NMpJWZG3hSuFU=" self.assertFalse(crypto.verify_l2_block_pow(blake2b, block)) block = copy.deepcopy(valid_block) block.nonce = 0 self.assertFalse(crypto.verify_l2_block_pow(blake2b, block))
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")
def test_pow_complexity(self): # With base64, each character is 6 bits, and A represents '000000' in binary # So we use that to check if our PoW complexity are valid block = make_l2_block() block_hash, _ = crypto.pow_l2_block(blake2b, block, complexity=6) self.assertEqual(block_hash[0], "A") block_hash, _ = crypto.pow_l2_block(blake2b, block, complexity=12) self.assertEqual(block_hash[0], "A") self.assertEqual(block_hash[1], "A") block_hash, _ = crypto.pow_l2_block(sha256, block, complexity=6) self.assertEqual(block_hash[0], "A") block_hash, _ = crypto.pow_l2_block(sha256, block, complexity=12) self.assertEqual(block_hash[0], "A") self.assertEqual(block_hash[1], "A") block_hash, _ = crypto.pow_l2_block(sha3_256, block, complexity=6) self.assertEqual(block_hash[0], "A") block_hash, _ = crypto.pow_l2_block(sha3_256, block, complexity=12) self.assertEqual(block_hash[0], "A") self.assertEqual(block_hash[1], "A")