Fix endless recursion in list_to_binary_tree (#17219)

This commit is contained in:
Matt Hauff 2024-01-05 12:22:34 -08:00 committed by GitHub
parent 1a33cfdecc
commit c7c4c6b4b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 0 deletions

View File

@ -47,6 +47,8 @@ def build_merkle_tree_from_binary_tree(tuples: TupleTree) -> Tuple[bytes32, Dict
def list_to_binary_tree(objects: List[Any]) -> Any:
size = len(objects)
if size == 0:
raise ValueError("Cannot build a tree out of 0 objects")
if size == 1:
return objects[0]
midpoint = (size + 1) >> 1

View File

@ -10,6 +10,7 @@ from chia.types.coin_spend import make_spend
from chia.util.errors import ValidationError
from chia.util.ints import uint64
from chia.wallet.util.compute_hints import HintedCoin, compute_spend_hints_and_additions
from chia.wallet.util.merkle_utils import list_to_binary_tree
from chia.wallet.util.tx_config import (
DEFAULT_COIN_SELECTION_CONFIG,
DEFAULT_TX_CONFIG,
@ -92,3 +93,13 @@ def test_tx_config() -> None:
assert TXConfigLoader.from_json_dict({}).autofill(
constants=DEFAULT_CONSTANTS, config={"reuse_public_key_for_change": {"1": True}}, logged_in_fingerprint=1
).to_json_dict() == {**default_tx_config, "reuse_puzhash": True}
def test_list_to_binary_tree() -> None:
assert list_to_binary_tree([1]) == 1
assert list_to_binary_tree([1, 2]) == (1, 2)
assert list_to_binary_tree([1, 2, 3]) == ((1, 2), 3)
assert list_to_binary_tree([1, 2, 3, 4]) == ((1, 2), (3, 4))
assert list_to_binary_tree([1, 2, 3, 4, 5]) == (((1, 2), 3), (4, 5))
with pytest.raises(ValueError):
list_to_binary_tree([])