From 48901a02a78f6b96ba0ff78898db251e639fd1c8 Mon Sep 17 00:00:00 2001 From: Mariano Sorgente Date: Mon, 10 Feb 2020 19:07:20 -0500 Subject: [PATCH] Use standard hash everywhere --- scripts/check_plots.py | 4 ++-- src/full_node.py | 2 +- src/types/hashable/Program.py | 2 +- src/types/proof_of_space.py | 4 ++-- src/util/streamable.py | 4 ++-- tests/block_tools.py | 10 +++++----- tests/consensus/test_pot_iterations.py | 5 ++--- tests/test_blockchain_transactions.py | 6 ++++++ tests/test_spendbundle.py | 4 ++-- 9 files changed, 23 insertions(+), 18 deletions(-) diff --git a/scripts/check_plots.py b/scripts/check_plots.py index f1caf28f8e24..907e76cdb8de 100644 --- a/scripts/check_plots.py +++ b/scripts/check_plots.py @@ -1,5 +1,4 @@ import argparse -from hashlib import sha256 from pathlib import Path from blspy import PrivateKey, PublicKey @@ -9,6 +8,7 @@ from chiapos import DiskProver, Verifier from definitions import ROOT_DIR from src.types.proof_of_space import ProofOfSpace from src.types.sized_bytes import bytes32 +from src.util.hash import std_hash plot_root = ROOT_DIR / "plots" plot_config_filename = ROOT_DIR / "config" / "plots.yaml" @@ -49,7 +49,7 @@ def main(): total_proofs = 0 try: for i in range(args.num): - challenge = sha256(i.to_bytes(32, "big")).digest() + challenge = std_hash(i.to_bytes(32, "big")) for index, quality_str in enumerate( pr.get_qualities_for_challenge(challenge) ): diff --git a/src/full_node.py b/src/full_node.py index 26772ea09963..63859bb2084c 100644 --- a/src/full_node.py +++ b/src/full_node.py @@ -23,7 +23,7 @@ from src.types.challenge import Challenge from src.types.full_block import FullBlock from src.types.hashable.Coin import Coin from src.types.hashable.BLSSignature import BLSSignature -from src.util.Hash import std_hash +from src.util.hash import std_hash from src.types.hashable.SpendBundle import SpendBundle from src.types.hashable.Program import Program from src.types.header import Header, HeaderData diff --git a/src/types/hashable/Program.py b/src/types/hashable/Program.py index c25e709b8376..70f139f9ac1c 100644 --- a/src/types/hashable/Program.py +++ b/src/types/hashable/Program.py @@ -6,7 +6,7 @@ from clvm.serialize import sexp_from_stream, sexp_to_stream from clvm.subclass_sexp import BaseSExp from src.types.sized_bytes import bytes32 -from src.util.Hash import std_hash +from src.util.hash import std_hash SExp = to_sexp_f(1).__class__ diff --git a/src/types/proof_of_space.py b/src/types/proof_of_space.py index cb2d7ab50b06..6bd58ebb6d2c 100644 --- a/src/types/proof_of_space.py +++ b/src/types/proof_of_space.py @@ -1,5 +1,4 @@ from dataclasses import dataclass -from hashlib import sha256 from typing import Optional from blspy import PublicKey @@ -8,6 +7,7 @@ from chiapos import Verifier from src.types.sized_bytes import bytes32 from src.util.ints import uint8 from src.util.streamable import Streamable, streamable +from src.util.hash import std_hash @dataclass(frozen=True) @@ -34,4 +34,4 @@ class ProofOfSpace(Streamable): @staticmethod def calculate_plot_seed(pool_pubkey: PublicKey, plot_pubkey: PublicKey) -> bytes32: - return bytes32(sha256(bytes(pool_pubkey) + bytes(plot_pubkey)).digest()) + return bytes32(std_hash(bytes(pool_pubkey) + bytes(plot_pubkey)).digest()) diff --git a/src/util/streamable.py b/src/util/streamable.py index a8cef87c7b8f..97b33d4dfaac 100644 --- a/src/util/streamable.py +++ b/src/util/streamable.py @@ -5,10 +5,10 @@ import dataclasses import io import pprint import json -from hashlib import sha256 from typing import Any, BinaryIO, List, Type, get_type_hints, Union from src.util.byte_types import hexstr_to_bytes from src.types.hashable.Program import Program +from src.util.hash import std_hash from blspy import ( ChainCode, @@ -190,7 +190,7 @@ class Streamable: self.stream_one_item(f_type, getattr(self, f_name), f) def get_hash(self) -> bytes32: - return bytes32(sha256(bytes(self)).digest()) + return bytes32(std_hash(bytes(self))) @classmethod def from_bytes(cls: Any, blob: bytes) -> Any: diff --git a/tests/block_tools.py b/tests/block_tools.py index f2dda207c501..8fc6b19a33e8 100644 --- a/tests/block_tools.py +++ b/tests/block_tools.py @@ -1,6 +1,5 @@ import sys import time -from hashlib import sha256 from typing import Any, Dict, List, Tuple, Optional from pathlib import Path @@ -28,6 +27,7 @@ from src.types.proof_of_time import ProofOfTime from src.types.sized_bytes import bytes32 from src.util.errors import NoProofsOfSpaceFound from src.util.ints import uint8, uint32, uint64 +from src.util.hash import std_hash # Can't go much lower than 19, since plots start having no solutions k: uint8 = uint8(19) @@ -42,8 +42,8 @@ plot_sks: List[PrivateKey] = [ plot_pks: List[PublicKey] = [sk.get_public_key() for sk in plot_sks] farmer_sk: PrivateKey = PrivateKey.from_seed(b"coinbase") -coinbase_target = sha256(bytes(farmer_sk.get_public_key())).digest() -fee_target = sha256(bytes(farmer_sk.get_public_key())).digest() +coinbase_target = std_hash(bytes(farmer_sk.get_public_key())) +fee_target = std_hash(bytes(farmer_sk.get_public_key())) n_wesolowski = uint8(3) @@ -64,7 +64,7 @@ class BlockTools: self.filenames: List[str] = [ "genesis-plots-" + str(k) - + sha256(int.to_bytes(i, 4, "big")).digest().hex() + + std_hash(int.to_bytes(i, 4, "big")).hex() + ".dat" for i in range(num_plots) ] @@ -117,7 +117,7 @@ class BlockTools: else: block_list.append( self.create_genesis_block( - test_constants, sha256(seed).digest(), seed + test_constants, std_hash(seed), seed ) ) prev_difficulty = test_constants["DIFFICULTY_STARTING"] diff --git a/tests/consensus/test_pot_iterations.py b/tests/consensus/test_pot_iterations.py index 384d05d6839c..3d85c604fb88 100644 --- a/tests/consensus/test_pot_iterations.py +++ b/tests/consensus/test_pot_iterations.py @@ -1,8 +1,7 @@ -from hashlib import sha256 - from src.consensus.pot_iterations import calculate_iterations_quality from src.consensus.pos_quality import _expected_plot_size from src.util.ints import uint8, uint64 +from src.util.hash import std_hash class TestPotIterations: @@ -33,7 +32,7 @@ class TestPotIterations: for b_index in range(total_blocks): qualities = [ - sha256(b_index.to_bytes(32, "big") + bytes(farmer_index)).digest() + std_hash(b_index.to_bytes(32, "big") + bytes(farmer_index)) for farmer_index in range(len(farmer_ks)) ] iters = [ diff --git a/tests/test_blockchain_transactions.py b/tests/test_blockchain_transactions.py index ccda4fafdca8..e55a6ffdd3a9 100644 --- a/tests/test_blockchain_transactions.py +++ b/tests/test_blockchain_transactions.py @@ -240,6 +240,7 @@ class TestBlockchainTransactions: ) # Invalid block bundle + assert bad_spend_bundle is not None invalid_program = best_solution_program(bad_spend_bundle) aggsig = bad_spend_bundle.aggregated_signature @@ -258,6 +259,7 @@ class TestBlockchainTransactions: assert error is Err.ASSERT_MY_COIN_ID_FAILED # Valid block bundle + assert valid_spend_bundle is not None valid_program = best_solution_program(valid_spend_bundle) aggsig = valid_spend_bundle.aggregated_signature @@ -314,6 +316,7 @@ class TestBlockchainTransactions: ) # Invalid block bundle + assert block1_spend_bundle is not None solo_program = best_solution_program(block1_spend_bundle) aggsig = block1_spend_bundle.aggregated_signature @@ -384,6 +387,7 @@ class TestBlockchainTransactions: ) # program that will be sent to early + assert block1_spend_bundle is not None program = best_solution_program(block1_spend_bundle) aggsig = block1_spend_bundle.aggregated_signature @@ -452,6 +456,7 @@ class TestBlockchainTransactions: ) # program that will be sent to early + assert block1_spend_bundle is not None program = best_solution_program(block1_spend_bundle) aggsig = block1_spend_bundle.aggregated_signature @@ -520,6 +525,7 @@ class TestBlockchainTransactions: ) # program that will be sent to early + assert block1_spend_bundle is not None program = best_solution_program(block1_spend_bundle) aggsig = block1_spend_bundle.aggregated_signature diff --git a/tests/test_spendbundle.py b/tests/test_spendbundle.py index 178ba1eb226e..aae9428760a6 100644 --- a/tests/test_spendbundle.py +++ b/tests/test_spendbundle.py @@ -1,4 +1,4 @@ -from src.util.Hash import std_hash +from src.util.hash import std_hash from src.util.condition_tools import ( conditions_by_opcode, aggsig_in_conditions_dict, @@ -17,7 +17,7 @@ def test_1(): conditions = [ make_create_coin_condition(std_hash(bytes(pp)), amount) - for pp, amount in [(puzzle_program_1, 1000), (puzzle_program_2, 2000),] + for pp, amount in [(puzzle_program_1, 1000), (puzzle_program_2, 2000), ] ] assert conditions is not None