From bda8f0e75e9125bc6631d2217db30bc505a7bb0c Mon Sep 17 00:00:00 2001 From: Mariano Sorgente Date: Thu, 6 Feb 2020 10:12:33 -0500 Subject: [PATCH] Allow bytes in streamable --- src/harvester.py | 2 +- src/timelord.py | 4 ++-- src/types/proof_of_space.py | 4 ++-- src/types/proof_of_time.py | 3 +-- src/util/streamable.py | 6 ++++++ tests/block_tools.py | 4 ++-- tests/test_blockchain.py | 4 ++-- tests/test_simulation.py | 18 ++++++++++++++++++ tests/util/test_streamable.py | 6 +----- 9 files changed, 35 insertions(+), 16 deletions(-) create mode 100644 tests/test_simulation.py diff --git a/src/harvester.py b/src/harvester.py index 4564caf3..3e956e60 100644 --- a/src/harvester.py +++ b/src/harvester.py @@ -165,7 +165,7 @@ class Harvester: pool_pubkey, plot_pubkey, uint8(self.provers[filename].get_size()), - [uint8(b) for b in proof_xs], + proof_xs, ) response = harvester_protocol.RespondProofOfSpace( diff --git a/src/timelord.py b/src/timelord.py index 33439955..381c2c21 100644 --- a/src/timelord.py +++ b/src/timelord.py @@ -16,7 +16,7 @@ from src.types.classgroup import ClassgroupElement from src.types.proof_of_time import ProofOfTime from src.types.sized_bytes import bytes32 from src.util.api_decorators import api_request -from src.util.ints import uint8, uint64 +from src.util.ints import uint64 log = logging.getLogger(__name__) @@ -300,7 +300,7 @@ class Timelord: iterations_needed, output, self.config["n_wesolowski"], - [uint8(b) for b in proof_bytes], + proof_bytes, ) response = timelord_protocol.ProofOfTimeFinished(proof_of_time) diff --git a/src/types/proof_of_space.py b/src/types/proof_of_space.py index 8fa63725..e5d57b42 100644 --- a/src/types/proof_of_space.py +++ b/src/types/proof_of_space.py @@ -1,6 +1,6 @@ from dataclasses import dataclass from hashlib import sha256 -from typing import List, Optional +from typing import Optional from blspy import PublicKey @@ -17,7 +17,7 @@ class ProofOfSpace(Streamable): pool_pubkey: PublicKey plot_pubkey: PublicKey size: uint8 - proof: List[uint8] + proof: bytes def get_plot_seed(self) -> bytes32: return self.calculate_plot_seed(self.pool_pubkey, self.plot_pubkey) diff --git a/src/types/proof_of_time.py b/src/types/proof_of_time.py index 6348987d..15e4aef8 100644 --- a/src/types/proof_of_time.py +++ b/src/types/proof_of_time.py @@ -1,5 +1,4 @@ from dataclasses import dataclass -from typing import List from chiavdf import verify from lib.chiavdf.inkfish.classgroup import ClassGroup @@ -18,7 +17,7 @@ class ProofOfTime(Streamable): number_of_iterations: uint64 output: ClassgroupElement witness_type: uint8 - witness: List[uint8] + witness: bytes def is_valid_slow(self, discriminant_size_bits): disc: int = create_discriminant(self.challenge_hash, discriminant_size_bits) diff --git a/src/util/streamable.py b/src/util/streamable.py index e0b92565..16fa4e81 100644 --- a/src/util/streamable.py +++ b/src/util/streamable.py @@ -130,6 +130,9 @@ class Streamable: return cls.parse_one_item(inner_type, f) # type: ignore else: return None + if f_type == bytes: + list_size = uint32(int.from_bytes(f.read(4), "big")) + return f.read(list_size) if hasattr(f_type, "parse"): return f_type.parse(f) if hasattr(f_type, "from_bytes") and size_hints[f_type.__name__]: @@ -163,6 +166,9 @@ class Streamable: else: f.write(bytes([1])) self.stream_one_item(inner_type, item, f) + elif f_type == bytes: + f.write(uint32(len(item)).to_bytes(4, "big")) + f.write(item) elif hasattr(f_type, "stream"): item.stream(f) elif hasattr(f_type, "__bytes__"): diff --git a/tests/block_tools.py b/tests/block_tools.py index ff74135d..82307f4a 100644 --- a/tests/block_tools.py +++ b/tests/block_tools.py @@ -364,7 +364,7 @@ class BlockTools: proof_xs: bytes = prover.get_full_proof(challenge_hash, 0) proof_of_space: ProofOfSpace = ProofOfSpace( - challenge_hash, pool_pk, plot_pk, k, [uint8(b) for b in proof_xs] + challenge_hash, pool_pk, plot_pk, k, proof_xs ) number_iters: uint64 = pot_iterations.calculate_iterations( proof_of_space, difficulty, ips, test_constants["MIN_BLOCK_TIME"] @@ -385,7 +385,7 @@ class BlockTools: number_iters, output, n_wesolowski, - [uint8(b) for b in proof_bytes], + proof_bytes, ) if not reward_puzzlehash: diff --git a/tests/test_blockchain.py b/tests/test_blockchain.py index 210f215d..1a2b119d 100644 --- a/tests/test_blockchain.py +++ b/tests/test_blockchain.py @@ -241,7 +241,7 @@ class TestBlockValidation: async def test_invalid_pos(self, initial_blockchain): blocks, b = initial_blockchain - bad_pos = [i for i in blocks[9].header_block.proof_of_space.proof] + bad_pos = bytearray([i for i in blocks[9].header_block.proof_of_space.proof]) bad_pos[0] = uint8((bad_pos[0] + 1) % 256) # Proof of space invalid block_bad = FullBlock( @@ -251,7 +251,7 @@ class TestBlockValidation: blocks[9].header_block.proof_of_space.pool_pubkey, blocks[9].header_block.proof_of_space.plot_pubkey, blocks[9].header_block.proof_of_space.size, - bad_pos, + bytes(bad_pos), ), blocks[9].header_block.proof_of_time, blocks[9].header_block.challenge, diff --git a/tests/test_simulation.py b/tests/test_simulation.py new file mode 100644 index 00000000..ed21677e --- /dev/null +++ b/tests/test_simulation.py @@ -0,0 +1,18 @@ +import asyncio +import pytest + + +@pytest.fixture(scope="module") +def event_loop(): + loop = asyncio.get_event_loop() + yield loop + + +class TestSimulation: + @pytest.mark.asyncio + async def test_simulation_1(self): + db_id_1 = "1001" + db_id_2 = "1002" + db_id_3 = "1003" + + diff --git a/tests/util/test_streamable.py b/tests/util/test_streamable.py index b4ac7d1d..c9ed18b0 100644 --- a/tests/util/test_streamable.py +++ b/tests/util/test_streamable.py @@ -35,11 +35,7 @@ class TestStreamable(unittest.TestCase): c: bytes a = TestClass2(uint32(1), uint32(2), b"3") - try: - bytes(a) - assert False - except NotImplementedError: - pass + bytes(a) @dataclass(frozen=True) @streamable