Skip to content

Commit

Permalink
respond to review comments. some minor improvements to tests, and enb…
Browse files Browse the repository at this point in the history
…le the 10000 block tests with hard-fork activation
  • Loading branch information
arvidn committed Sep 9, 2023
1 parent be5a392 commit 34041d4
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-single.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ jobs:
CHIA_ROOT: ${{ github.workspace }}/.chia/mainnet
CHIA_SIMULATOR_ROOT: ${{ github.workspace }}/.chia/simulator
JOB_FILE_NAME: tests_${{ matrix.os.file_name }}_python-${{ matrix.python.file_name }}_${{ matrix.configuration.name }}
BLOCKS_AND_PLOTS_VERSION: 0.31.0
BLOCKS_AND_PLOTS_VERSION: 0.32.0

steps:
- name: Configure git
Expand Down
29 changes: 13 additions & 16 deletions chia/simulator/block_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1844,41 +1844,40 @@ def conditions_cost(conds: Program, hard_fork: bool) -> uint64:
return uint64(condition_cost)


def compute_cost_test(generator: BlockGenerator, cost_per_byte: int, hard_fork: bool = False) -> uint64:
def compute_cost_test(generator: BlockGenerator, constants: ConsensusConstants, height: uint32) -> uint64:
# this function cannot *validate* the block or any of the transactions. We
# deliberately create invalid blocks as parts of the tests, and we still
# need to be able to compute the cost of it

condition_cost = 0
clvm_cost = 0

if hard_fork:
if height >= constants.HARD_FORK_FIX_HEIGHT:
blocks = [bytes(g) for g in generator.generator_refs]
cost, result = generator.program._run(INFINITE_COST, MEMPOOL_MODE | ALLOW_BACKREFS, DESERIALIZE_MOD, blocks)
clvm_cost += cost

for spend in result.first().as_iter():
spend = spend.rest() # skip parent coin id
puzzle = spend.first()
spend = spend.rest() # skip puzzle
spend = spend.rest() # skip amount
solution = spend.first()
# each spend is a list of:
# (parent-coin-id puzzle amount solution)
puzzle = spend.at("rf")
solution = spend.at("rrrf")

cost, result = puzzle._run(INFINITE_COST, MEMPOOL_MODE, solution)
clvm_cost += cost
condition_cost += conditions_cost(result, hard_fork)
condition_cost += conditions_cost(result, height >= constants.HARD_FORK_HEIGHT)

else:
block_program_args = Program.to([[bytes(g) for g in generator.generator_refs]])
clvm_cost, result = GENERATOR_MOD._run(INFINITE_COST, MEMPOOL_MODE, generator.program, block_program_args)

for res in result.first().as_iter():
res = res.rest() # skip parent coin id
res = res.rest() # skip puzzle hash
res = res.rest() # skip amount
condition_cost += conditions_cost(res.first(), hard_fork)
# each condition item is:
# (parent-coin-id puzzle-hash amount conditions)
conditions = res.at("rrrf")
condition_cost += conditions_cost(conditions, height >= constants.HARD_FORK_HEIGHT)

size_cost = len(bytes(generator.program)) * cost_per_byte
size_cost = len(bytes(generator.program)) * constants.COST_PER_BYTE

return uint64(clvm_cost + size_cost + condition_cost)

Expand Down Expand Up @@ -1975,9 +1974,7 @@ def create_test_foliage(
# Calculate the cost of transactions
if block_generator is not None:
generator_block_heights_list = block_generator.block_height_list
cost = compute_cost_test(
block_generator, constants.COST_PER_BYTE, hard_fork=height >= constants.HARD_FORK_HEIGHT
)
cost = compute_cost_test(block_generator, constants, height)

removal_amount = 0
addition_amount = 0
Expand Down
17 changes: 13 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,14 @@ def default_1500_blocks(bt, consensus_mode):
def default_10000_blocks(bt, consensus_mode):
from tests.util.blockchain import persistent_blocks

if consensus_mode == ConsensusMode.SOFT_FORK4 or consensus_mode == ConsensusMode.HARD_FORK_2_0:
if consensus_mode == ConsensusMode.SOFT_FORK4:
pytest.skip("Test cache not available yet")

return persistent_blocks(10000, f"test_blocks_10000_{saved_blocks_version}.db", bt, seed=b"10000")
version = ""
if consensus_mode == ConsensusMode.HARD_FORK_2_0:
version = "_hardfork"

return persistent_blocks(10000, f"test_blocks_10000_{saved_blocks_version}{version}.db", bt, seed=b"10000")


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -306,11 +310,16 @@ def default_2000_blocks_compact(bt, consensus_mode):
def default_10000_blocks_compact(bt, consensus_mode):
from tests.util.blockchain import persistent_blocks

if consensus_mode == ConsensusMode.SOFT_FORK4 or consensus_mode == ConsensusMode.HARD_FORK_2_0:
if consensus_mode == ConsensusMode.SOFT_FORK4:
pytest.skip("Test cache not available yet")

version = ""
if consensus_mode == ConsensusMode.HARD_FORK_2_0:
version = "_hardfork"

return persistent_blocks(
10000,
f"test_blocks_10000_compact_{saved_blocks_version}.db",
f"test_blocks_10000_compact_{saved_blocks_version}{version}.db",
bt,
normalized_to_identity_cc_eos=True,
normalized_to_identity_icc_eos=True,
Expand Down
4 changes: 4 additions & 0 deletions tests/core/full_node/test_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ async def test_unknown_conditions_with_cost(
# before the hard fork, all unknown conditions have 0 cost
expected_cost = 0

# once the hard fork activates, blocks no longer pay the cost of the ROM
# generator (which includes hashing all puzzles).
if consensus_mode == ConsensusMode.HARD_FORK_2_0:
block_base_cost = 756064
else:
Expand All @@ -175,6 +177,8 @@ async def test_softfork_condition(self, condition: str, expected_cost: int, bt,
expected_cost = 0
block_base_cost = 737056
else:
# once the hard fork activates, blocks no longer pay the cost of the ROM
# generator (which includes hashing all puzzles).
block_base_cost = 732064

# the block_base_cost includes the cost of the bytes for the condition
Expand Down
6 changes: 6 additions & 0 deletions tests/farmer_harvester/test_filter_prefix_bits.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@
from chia.types.full_block import FullBlock
from chia.util.ints import uint8, uint32, uint64
from chia.util.keychain import Keychain
from tests.conftest import ConsensusMode
from tests.core.test_farmer_harvester_rpc import wait_for_plot_sync


# these numbers are only valid for chains farmed with the fixed original plot
# filter. The HARD_FORK_2_0 consensus mode uses a chain where blocks are farmed
# with wider filters. i.e. some valid blocks may still not pass the filter in
# this test
@pytest.mark.limit_consensus_modes(allowed=[ConsensusMode.PLAIN])
@pytest.mark.parametrize(
argnames=["filter_prefix_bits", "should_pass"], argvalues=[(9, 33), (8, 66), (7, 138), (6, 265), (5, 607)]
)
Expand Down

0 comments on commit 34041d4

Please sign in to comment.