transition away from __bytes__ conversion for fixed-size integers (#16701)

This commit is contained in:
Arvid Norberg 2023-10-31 22:57:17 +01:00 committed by GitHub
parent dd3adc49c0
commit ca85e485e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 52 additions and 33 deletions

View File

@ -115,8 +115,8 @@ class SimBlockRecord(Streamable):
uint32(height - 1 if height > 0 else 0),
timestamp,
True,
std_hash(bytes(height)),
std_hash(std_hash(height)),
std_hash(height.stream_to_bytes()),
std_hash(std_hash(height.stream_to_bytes())),
)

View File

@ -187,7 +187,7 @@ async def validate_block_body(
return Err.INVALID_TRANSACTIONS_GENERATOR_REFS_ROOT, None
# The generator_refs_root must be the hash of the concatenation of the List[uint32]
generator_refs_hash = std_hash(b"".join([bytes(i) for i in block.transactions_generator_ref_list]))
generator_refs_hash = std_hash(b"".join([i.stream_to_bytes() for i in block.transactions_generator_ref_list]))
if block.transactions_info.generator_refs_root != generator_refs_hash:
return Err.INVALID_TRANSACTIONS_GENERATOR_REFS_ROOT, None
if len(block.transactions_generator_ref_list) > constants.MAX_GENERATOR_REF_LIST_SIZE:

View File

@ -247,7 +247,9 @@ class DataLayerStore:
"""
Add a new launcher coin's information to the DB
"""
launcher_bytes: bytes = launcher.parent_coin_info + launcher.puzzle_hash + bytes(uint64(launcher.amount))
launcher_bytes: bytes = (
launcher.parent_coin_info + launcher.puzzle_hash + uint64(launcher.amount).stream_to_bytes()
)
async with self.db_wrapper.writer_maybe_transaction() as conn:
await conn.execute_insert(
"INSERT OR REPLACE INTO launchers VALUES (?, ?)",
@ -305,8 +307,10 @@ class DataLayerStore:
(
mirror.coin_id,
mirror.launcher_id,
bytes(mirror.amount),
b"".join([bytes(uint16(len(url))) + url for url in mirror.urls]), # prefix each item with a length
mirror.amount.stream_to_bytes(),
b"".join(
[uint16(len(url)).stream_to_bytes() + url for url in mirror.urls]
), # prefix each item with a length
1 if mirror.ours else 0,
),
)

View File

@ -461,7 +461,7 @@ class CoinStore:
int(record.coinbase),
record.coin.puzzle_hash,
record.coin.parent_coin_info,
bytes(uint64(record.coin.amount)),
uint64(record.coin.amount).stream_to_bytes(),
record.timestamp,
)
)

View File

@ -375,9 +375,9 @@ class FullNodeAPI:
blocks_bytes.append(block_bytes)
respond_blocks_manually_streamed: bytes = (
bytes(uint32(request.start_height))
+ bytes(uint32(request.end_height))
+ len(blocks_bytes).to_bytes(4, "big", signed=False)
uint32(request.start_height).stream_to_bytes()
+ uint32(request.end_height).stream_to_bytes()
+ uint32(len(blocks_bytes)).stream_to_bytes()
)
for block_bytes in blocks_bytes:
respond_blocks_manually_streamed += block_bytes
@ -1375,9 +1375,9 @@ class FullNodeAPI:
# we start building RespondBlockHeaders response (start_height, end_height)
# and then need to define size of list object
respond_header_blocks_manually_streamed: bytes = (
bytes(uint32(request.start_height))
+ bytes(uint32(request.end_height))
+ len(header_blocks_bytes).to_bytes(4, "big", signed=False)
uint32(request.start_height).stream_to_bytes()
+ uint32(request.end_height).stream_to_bytes()
+ uint32(len(header_blocks_bytes)).stream_to_bytes()
)
# and now stream the whole list in bytes
respond_header_blocks_manually_streamed += b"".join(header_blocks_bytes)

View File

@ -2031,7 +2031,7 @@ def create_test_foliage(
generator_refs_hash = bytes32([1] * 32)
if generator_block_heights_list not in (None, []):
generator_ref_list_bytes = b"".join([bytes(i) for i in generator_block_heights_list])
generator_ref_list_bytes = b"".join([i.stream_to_bytes() for i in generator_block_heights_list])
generator_refs_hash = std_hash(generator_ref_list_bytes)
filter_hash: bytes32 = std_hash(encoded)

View File

@ -261,7 +261,7 @@ async def setup_simulators_and_wallets_inner(
spam_filter_after_n_txs,
xch_spam_amount,
None,
key_seed=std_hash(uint32(index)) if key_seed is None else key_seed,
key_seed=std_hash(uint32(index).stream_to_bytes()) if key_seed is None else key_seed,
initial_num_public_keys=initial_num_public_keys,
)
)

View File

@ -648,6 +648,11 @@ class Streamable:
assert f.read() == b""
return parsed
def stream_to_bytes(self) -> bytes:
f = io.BytesIO()
self.stream(f)
return bytes(f.getvalue())
def __bytes__(self: Any) -> bytes:
f = io.BytesIO()
self.stream(f)

View File

@ -78,7 +78,7 @@ class StructStream(int):
return cls.from_bytes(read_bytes)
def stream(self, f: BinaryIO) -> None:
f.write(bytes(self))
f.write(self.stream_to_bytes())
@classmethod
def from_bytes(cls: Type[_T_StructStream], blob: bytes) -> _T_StructStream: # type: ignore[override]
@ -86,5 +86,9 @@ class StructStream(int):
raise ValueError(f"{cls.__name__}.from_bytes() requires {cls.SIZE} bytes but got: {len(blob)}")
return cls(int.from_bytes(blob, "big", signed=cls.SIGNED))
def __bytes__(self) -> bytes:
def stream_to_bytes(self) -> bytes:
return super().to_bytes(length=self.SIZE, byteorder="big", signed=self.SIGNED)
# this is meant to avoid mixing up construcing a bytes object of a specific
# size (i.e. bytes(int)) vs. serializing the integer to bytes (i.e. bytes(uint32))
__bytes__ = None

View File

@ -45,7 +45,7 @@ class KeyValStore:
async with self.db_wrapper.writer_maybe_transaction() as conn:
cursor = await conn.execute(
"INSERT OR REPLACE INTO key_val_store VALUES(?, ?)",
(key, bytes(obj)),
(key, obj.stream_to_bytes()),
)
await cursor.close()

View File

@ -1073,7 +1073,7 @@ class NFTWallet:
"0x"
+ royalty_coin.parent_coin_info.hex()
+ royalty_coin.puzzle_hash.hex()
+ bytes(uint64(royalty_coin.amount)).hex()
+ uint64(royalty_coin.amount).stream_to_bytes().hex()
)
parent_spend_hex: str = "0x" + bytes(parent_spend).hex()
solver = Solver(

View File

@ -72,7 +72,7 @@ class NotificationStore:
(
notification.coin_id,
notification.message,
bytes(notification.amount),
notification.amount.stream_to_bytes(),
notification.height,
),
)

View File

@ -521,7 +521,7 @@ class Offer:
"0x"
+ sibling_coin.parent_coin_info.hex()
+ sibling_coin.puzzle_hash.hex()
+ bytes(uint64(sibling_coin.amount)).hex()
+ uint64(sibling_coin.amount).stream_to_bytes().hex()
+ " "
)
sibling_spends += "0x" + bytes(coin_to_spend_dict[sibling_coin]).hex() + " "
@ -539,7 +539,7 @@ class Offer:
"coin": "0x"
+ coin.parent_coin_info.hex()
+ coin.puzzle_hash.hex()
+ bytes(uint64(coin.amount)).hex(),
+ uint64(coin.amount).stream_to_bytes().hex(),
"parent_spend": "0x" + bytes(coin_to_spend_dict[coin]).hex(),
"siblings": siblings,
"sibling_spends": sibling_spends,

View File

@ -169,7 +169,7 @@ class VCStore:
record.vc.coin.name().hex(),
record.vc.coin.parent_coin_info.hex(),
record.vc.coin.puzzle_hash.hex(),
bytes(uint64(record.vc.coin.amount)),
uint64(record.vc.coin.amount).stream_to_bytes(),
bytes(record.vc.singleton_lineage_proof),
bytes(record.vc.eml_lineage_proof),
record.vc.inner_puzzle_hash.hex(),

View File

@ -104,7 +104,7 @@ class WalletCoinStore:
return self
async def count_small_unspent(self, cutoff: int, coin_type: CoinType = CoinType.NORMAL) -> int:
amount_bytes = bytes(uint64(cutoff))
amount_bytes = uint64(cutoff).stream_to_bytes()
async with self.db_wrapper.reader_no_transaction() as conn:
row = await execute_fetchone(
conn,
@ -131,7 +131,7 @@ class WalletCoinStore:
int(record.coinbase),
str(record.coin.puzzle_hash.hex()),
str(record.coin.parent_coin_info.hex()),
bytes(uint64(record.coin.amount)),
uint64(record.coin.amount).stream_to_bytes(),
record.wallet_type,
record.wallet_id,
record.coin_type,
@ -224,13 +224,14 @@ class WalletCoinStore:
if spent_range is not None and spent_range != UInt32Range():
conditions.append(f"spent_height BETWEEN {spent_range.start} AND {spent_range.stop}")
if amount_filter is not None:
entries = ",".join(f"X'{bytes(value).hex()}'" for value in amount_filter.values)
entries = ",".join(f"X'{value.stream_to_bytes().hex()}'" for value in amount_filter.values)
conditions.append(
f"amount {'not' if FilterMode(amount_filter.mode) == FilterMode.exclude else ''} in ({entries})"
)
if amount_range is not None and amount_range != UInt64Range():
conditions.append(
f"amount BETWEEN X'{bytes(amount_range.start).hex()}' AND X'{bytes(amount_range.stop).hex()}'"
f"amount BETWEEN X'{amount_range.start.stream_to_bytes().hex()}' "
f"AND X'{amount_range.stop.stream_to_bytes().hex()}'"
)
where_sql = "WHERE " + " AND ".join(conditions) if len(conditions) > 0 else ""

View File

@ -119,8 +119,8 @@ class WalletTransactionStore:
record.confirmed_at_height,
record.created_at_time,
record.to_puzzle_hash.hex(),
bytes(record.amount),
bytes(record.fee_amount),
record.amount.stream_to_bytes(),
record.fee_amount.stream_to_bytes(),
int(record.confirmed),
record.sent,
record.wallet_id,

View File

@ -40,7 +40,10 @@ def test_cat_outer_puzzle() -> None:
child_coin = Coin(parent_coin.name(), double_cat_puzzle.get_tree_hash(), uint64(100))
parent_spend = CoinSpend(parent_coin, SerializedProgram.from_program(double_cat_puzzle), Program.to([]))
child_coin_as_hex: str = (
"0x" + child_coin.parent_coin_info.hex() + child_coin.puzzle_hash.hex() + bytes(uint64(child_coin.amount)).hex()
"0x"
+ child_coin.parent_coin_info.hex()
+ child_coin.puzzle_hash.hex()
+ uint64(child_coin.amount).stream_to_bytes().hex()
)
parent_spend_as_hex: str = "0x" + bytes(parent_spend).hex()
inner_solution = Program.to([[51, ACS.get_tree_hash(), 100]])

View File

@ -867,8 +867,8 @@ async def test_valid_times_migration() -> None:
old_record.confirmed_at_height,
old_record.created_at_time,
old_record.to_puzzle_hash.hex(),
bytes(old_record.amount),
bytes(old_record.fee_amount),
old_record.amount.stream_to_bytes(),
old_record.fee_amount.stream_to_bytes(),
int(old_record.confirmed),
old_record.sent,
old_record.wallet_id,

View File

@ -43,7 +43,9 @@ def test_cat_outer_puzzle() -> None:
# Set up for solve
coin: Coin = Coin(bytes32([0] * 32), bytes32([0] * 32), uint64(0))
coin_as_hex: str = "0x" + coin.parent_coin_info.hex() + coin.puzzle_hash.hex() + bytes(uint64(coin.amount)).hex()
coin_as_hex: str = (
"0x" + coin.parent_coin_info.hex() + coin.puzzle_hash.hex() + uint64(coin.amount).stream_to_bytes().hex()
)
inner_solution = Program.to([[51, ACS.get_tree_hash(), 100]])
solution: Program = solve_puzzle(
cr_driver,