remove redundant function, calculate_cost_of_program(). The cost is now part of the NPCResult object (#9964)

This commit is contained in:
Arvid Norberg 2022-02-01 03:49:12 +01:00 committed by GitHub
parent e49b11d486
commit 275969ddd1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 14 additions and 31 deletions

View File

@ -11,7 +11,7 @@ from chia.consensus.block_root_validation import validate_block_merkle_roots
from chia.consensus.blockchain_interface import BlockchainInterface
from chia.consensus.coinbase import create_farmer_coin, create_pool_coin
from chia.consensus.constants import ConsensusConstants
from chia.consensus.cost_calculator import NPCResult, calculate_cost_of_program
from chia.consensus.cost_calculator import NPCResult
from chia.consensus.find_fork_point import find_fork_point_in_chain
from chia.full_node.block_store import BlockStore
from chia.full_node.coin_store import CoinStore
@ -194,7 +194,7 @@ async def validate_block_body(
# Get List of names removed, puzzles hashes for removed coins and conditions created
assert npc_result is not None
cost = calculate_cost_of_program(block.transactions_generator, npc_result, constants.COST_PER_BYTE)
cost = npc_result.cost
npc_list = npc_result.npc_list
# 7. Check that cost <= MAX_BLOCK_COST_CLVM

View File

@ -12,7 +12,7 @@ from chia.consensus.block_rewards import calculate_base_farmer_reward, calculate
from chia.consensus.blockchain_interface import BlockchainInterface
from chia.consensus.coinbase import create_farmer_coin, create_pool_coin
from chia.consensus.constants import ConsensusConstants
from chia.consensus.cost_calculator import NPCResult, calculate_cost_of_program
from chia.consensus.cost_calculator import NPCResult
from chia.full_node.mempool_check_conditions import get_name_puzzle_conditions
from chia.full_node.signage_point import SignagePoint
from chia.types.blockchain_format.coin import Coin, hash_coin_list
@ -137,7 +137,7 @@ def create_foliage(
cost_per_byte=constants.COST_PER_BYTE,
mempool_mode=True,
)
cost = calculate_cost_of_program(block_generator.program, result, constants.COST_PER_BYTE)
cost = result.cost
removal_amount = 0
addition_amount = 0

View File

@ -1,7 +1,6 @@
from dataclasses import dataclass
from typing import List, Optional
from chia.types.blockchain_format.program import SerializedProgram
from chia.types.name_puzzle_condition import NPC
from chia.util.ints import uint16, uint64
from chia.util.streamable import Streamable, streamable
@ -14,10 +13,3 @@ class NPCResult(Streamable):
npc_list: List[NPC]
cost: uint64 # The total cost of the block, including CLVM cost, cost of
# conditions and cost of bytes
def calculate_cost_of_program(program: SerializedProgram, npc_result: NPCResult, cost_per_byte: int) -> uint64:
"""
This function calculates the total cost of either a block or a spendbundle
"""
return npc_result.cost

View File

@ -11,7 +11,7 @@ from chiabip158 import PyBIP158
from chia.util import cached_bls
from chia.consensus.block_record import BlockRecord
from chia.consensus.constants import ConsensusConstants
from chia.consensus.cost_calculator import NPCResult, calculate_cost_of_program
from chia.consensus.cost_calculator import NPCResult
from chia.full_node.bundle_tools import simple_solution_generator
from chia.full_node.coin_store import CoinStore
from chia.full_node.mempool import Mempool
@ -284,7 +284,7 @@ class MempoolManager:
assert npc_result.error is None
if program is None:
program = simple_solution_generator(new_spend).program
cost = calculate_cost_of_program(program, npc_result, self.constants.COST_PER_BYTE)
cost = npc_result.cost
log.debug(f"Cost: {cost}")

View File

@ -8,7 +8,7 @@ from typing import Any, Dict, List, Optional, Set, Tuple
from blspy import AugSchemeMPL, G2Element
from chia.consensus.cost_calculator import calculate_cost_of_program, NPCResult
from chia.consensus.cost_calculator import NPCResult
from chia.full_node.bundle_tools import simple_solution_generator
from chia.full_node.mempool_check_conditions import get_name_puzzle_conditions
from chia.protocols.wallet_protocol import PuzzleSolutionResponse, CoinState
@ -228,10 +228,7 @@ class CATWallet:
cost_per_byte=self.wallet_state_manager.constants.COST_PER_BYTE,
mempool_mode=True,
)
cost_result: uint64 = calculate_cost_of_program(
program.program, result, self.wallet_state_manager.constants.COST_PER_BYTE
)
self.cost_of_single_tx = cost_result
self.cost_of_single_tx = result.cost
self.log.info(f"Cost of a single tx for CAT wallet: {self.cost_of_single_tx}")
max_cost = self.wallet_state_manager.constants.MAX_BLOCK_COST_CLVM / 2 # avoid full block TXs

View File

@ -4,7 +4,7 @@ from typing import Any, Dict, List, Optional, Set
from blspy import G1Element
from chia.consensus.cost_calculator import calculate_cost_of_program, NPCResult
from chia.consensus.cost_calculator import NPCResult
from chia.full_node.bundle_tools import simple_solution_generator
from chia.full_node.mempool_check_conditions import get_name_puzzle_conditions
from chia.types.blockchain_format.coin import Coin
@ -84,10 +84,7 @@ class Wallet:
cost_per_byte=self.wallet_state_manager.constants.COST_PER_BYTE,
mempool_mode=True,
)
cost_result: uint64 = calculate_cost_of_program(
program.program, result, self.wallet_state_manager.constants.COST_PER_BYTE
)
self.cost_of_single_tx = cost_result
self.cost_of_single_tx = result.cost
self.log.info(f"Cost of a single tx for standard wallet: {self.cost_of_single_tx}")
max_cost = self.wallet_state_manager.constants.MAX_BLOCK_COST_CLVM / 5 # avoid full block TXs

View File

@ -1,7 +1,7 @@
from chia.types.blockchain_format.program import INFINITE_COST
from chia.types.spend_bundle import SpendBundle
from chia.types.generator_types import BlockGenerator
from chia.consensus.cost_calculator import calculate_cost_of_program, NPCResult
from chia.consensus.cost_calculator import NPCResult
from chia.consensus.default_constants import DEFAULT_CONSTANTS
from chia.full_node.bundle_tools import simple_solution_generator
from chia.full_node.mempool_check_conditions import get_name_puzzle_conditions
@ -12,5 +12,4 @@ def cost_of_spend_bundle(spend_bundle: SpendBundle) -> int:
npc_result: NPCResult = get_name_puzzle_conditions(
program, INFINITE_COST, cost_per_byte=DEFAULT_CONSTANTS.COST_PER_BYTE, mempool_mode=True
)
cost: int = calculate_cost_of_program(program.program, npc_result, DEFAULT_CONSTANTS.COST_PER_BYTE)
return cost
return npc_result.cost

View File

@ -7,7 +7,7 @@ import pytest
from clvm_tools import binutils
from chia.consensus.condition_costs import ConditionCost
from chia.consensus.cost_calculator import NPCResult, calculate_cost_of_program
from chia.consensus.cost_calculator import NPCResult
from chia.full_node.bundle_tools import simple_solution_generator
from chia.full_node.mempool_check_conditions import get_name_puzzle_conditions, get_puzzle_and_solution_for_coin
from chia.types.blockchain_format.program import Program, SerializedProgram
@ -83,8 +83,6 @@ class TestCostCalculation:
height=softfork_height,
)
cost = calculate_cost_of_program(program.program, npc_result, test_constants.COST_PER_BYTE)
assert npc_result.error is None
assert len(bytes(program.program)) == 433
@ -104,7 +102,7 @@ class TestCostCalculation:
# Create condition + agg_sig_condition + length + cpu_cost
assert (
cost
npc_result.cost
== ConditionCost.CREATE_COIN.value
+ ConditionCost.AGG_SIG.value
+ len(bytes(program.program)) * test_constants.COST_PER_BYTE