Allow bytes in streamable

This commit is contained in:
Mariano Sorgente 2020-02-06 10:12:33 -05:00
parent 9c7136f0a5
commit bda8f0e75e
No known key found for this signature in database
GPG Key ID: 0F866338C369278C
9 changed files with 35 additions and 16 deletions

View File

@ -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(

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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__"):

View File

@ -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:

View File

@ -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,

18
tests/test_simulation.py Normal file
View File

@ -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"

View File

@ -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